Skip to content

Commit cf4d8e9

Browse files
committed
Add application service support to blueprints
Split out from #68
1 parent 0952ac7 commit cf4d8e9

File tree

7 files changed

+250
-48
lines changed

7 files changed

+250
-48
lines changed

dockerfiles/synapse/homeserver.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,13 @@ rc_joins:
9292

9393
federation_rr_transactions_per_room_per_second: 9999
9494

95+
## API Configuration ##
96+
97+
# A list of application service config files to use
98+
#
99+
app_service_config_files:
100+
AS_REGISTRATION_FILES
101+
95102
## Experimental Features ##
96103

97104
experimental_features:

dockerfiles/synapse/start.sh

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,22 @@
1-
#!/bin/sh
1+
#!/bin/bash
22

33
set -e
44

55
sed -i "s/SERVER_NAME/${SERVER_NAME}/g" /conf/homeserver.yaml
66

7+
# Add the application service registration files to the homeserver.yaml config
8+
for filename in /appservices/*.yaml; do
9+
[ -f "$filename" ] || break
10+
11+
as_id=$(basename "$filename" .yaml)
12+
13+
# Insert the path to the registration file and the AS_REGISTRATION_FILES marker after
14+
# so we can add the next application service in the next iteration of this for loop
15+
sed -i "s/AS_REGISTRATION_FILES/ - \/appservices\/${as_id}.yaml\nAS_REGISTRATION_FILES/g" /conf/homeserver.yaml
16+
done
17+
# Remove the AS_REGISTRATION_FILES entry
18+
sed -i "s/AS_REGISTRATION_FILES//g" /conf/homeserver.yaml
19+
720
# generate an ssl cert for the server, signed by our dummy CA
821
openssl req -new -key /conf/server.tls.key -out /conf/server.tls.csr \
922
-subj "/CN=${SERVER_NAME}"

internal/b/blueprints.go

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,13 @@
1515
package b
1616

1717
import (
18+
"crypto/rand"
19+
"encoding/hex"
1820
"fmt"
1921
"strconv"
2022
"strings"
23+
24+
"github.com/sirupsen/logrus"
2125
)
2226

2327
// KnownBlueprints lists static blueprints
@@ -26,6 +30,7 @@ var KnownBlueprints = map[string]*Blueprint{
2630
BlueprintAlice.Name: &BlueprintAlice,
2731
BlueprintFederationOneToOneRoom.Name: &BlueprintFederationOneToOneRoom,
2832
BlueprintFederationTwoLocalOneRemote.Name: &BlueprintFederationTwoLocalOneRemote,
33+
BlueprintHSWithApplicationService.Name: &BlueprintHSWithApplicationService,
2934
BlueprintOneToOneRoom.Name: &BlueprintOneToOneRoom,
3035
BlueprintPerfManyMessages.Name: &BlueprintPerfManyMessages,
3136
BlueprintPerfManyRooms.Name: &BlueprintPerfManyRooms,
@@ -46,6 +51,8 @@ type Homeserver struct {
4651
Users []User
4752
// The list of rooms to create on this homeserver
4853
Rooms []Room
54+
// The list of application services to create on the homeserver
55+
ApplicationServices []ApplicationService
4956
}
5057

5158
type User struct {
@@ -68,11 +75,22 @@ type Room struct {
6875
Events []Event
6976
}
7077

78+
type ApplicationService struct {
79+
ID string
80+
HSToken string
81+
ASToken string
82+
URL string
83+
SenderLocalpart string
84+
RateLimited bool
85+
}
86+
7187
type Event struct {
72-
Type string
73-
Sender string
74-
StateKey *string
75-
Content map[string]interface{}
88+
Type string
89+
Sender string
90+
OriginServerTS uint64
91+
StateKey *string
92+
PrevEvents []string
93+
Content map[string]interface{}
7694
// This field is ignored in blueprints as clients are unable to set it. Used with federation.Server
7795
Unsigned map[string]interface{}
7896
}
@@ -107,7 +125,18 @@ func Validate(bp Blueprint) (Blueprint, error) {
107125
return bp, err
108126
}
109127
}
128+
for i, as := range hs.ApplicationServices {
129+
hs.ApplicationServices[i], err = normalizeApplicationService(as)
130+
if err != nil {
131+
return bp, err
132+
}
133+
}
110134
}
135+
136+
logrus.WithFields(logrus.Fields{
137+
"bp": bp.Homeservers[0].ApplicationServices,
138+
}).Error("after modfiying bp")
139+
111140
return bp, nil
112141
}
113142

@@ -152,6 +181,25 @@ func normaliseUser(u string, hsName string) (string, error) {
152181
return u, nil
153182
}
154183

184+
func normalizeApplicationService(as ApplicationService) (ApplicationService, error) {
185+
hsToken := make([]byte, 32)
186+
_, err := rand.Read(hsToken)
187+
if err != nil {
188+
return as, err
189+
}
190+
191+
asToken := make([]byte, 32)
192+
_, err = rand.Read(asToken)
193+
if err != nil {
194+
return as, err
195+
}
196+
197+
as.HSToken = hex.EncodeToString(hsToken)
198+
as.ASToken = hex.EncodeToString(asToken)
199+
200+
return as, err
201+
}
202+
155203
// Ptr returns a pointer to `in`, because Go doesn't allow you to inline this.
156204
func Ptr(in string) *string {
157205
return &in
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package b
2+
3+
// BlueprintHSWithApplicationService who has an application service to interact with
4+
var BlueprintHSWithApplicationService = MustValidate(Blueprint{
5+
Name: "alice",
6+
Homeservers: []Homeserver{
7+
{
8+
Name: "hs1",
9+
Users: []User{
10+
{
11+
Localpart: "@alice",
12+
DisplayName: "Alice",
13+
},
14+
},
15+
ApplicationServices: []ApplicationService{
16+
{
17+
ID: "my_as_id",
18+
URL: "http://localhost:9000",
19+
SenderLocalpart: "the-bridge-user",
20+
RateLimited: false,
21+
},
22+
},
23+
},
24+
},
25+
})

0 commit comments

Comments
 (0)