Skip to content

Commit

Permalink
Add application service support to blueprints
Browse files Browse the repository at this point in the history
Split out from #68
  • Loading branch information
MadLittleMods committed Feb 22, 2021
1 parent 0952ac7 commit cf4d8e9
Show file tree
Hide file tree
Showing 7 changed files with 250 additions and 48 deletions.
7 changes: 7 additions & 0 deletions dockerfiles/synapse/homeserver.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,13 @@ rc_joins:

federation_rr_transactions_per_room_per_second: 9999

## API Configuration ##

# A list of application service config files to use
#
app_service_config_files:
AS_REGISTRATION_FILES

## Experimental Features ##

experimental_features:
Expand Down
15 changes: 14 additions & 1 deletion dockerfiles/synapse/start.sh
Original file line number Diff line number Diff line change
@@ -1,9 +1,22 @@
#!/bin/sh
#!/bin/bash

set -e

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

# Add the application service registration files to the homeserver.yaml config
for filename in /appservices/*.yaml; do
[ -f "$filename" ] || break

as_id=$(basename "$filename" .yaml)

# Insert the path to the registration file and the AS_REGISTRATION_FILES marker after
# so we can add the next application service in the next iteration of this for loop
sed -i "s/AS_REGISTRATION_FILES/ - \/appservices\/${as_id}.yaml\nAS_REGISTRATION_FILES/g" /conf/homeserver.yaml
done
# Remove the AS_REGISTRATION_FILES entry
sed -i "s/AS_REGISTRATION_FILES//g" /conf/homeserver.yaml

# generate an ssl cert for the server, signed by our dummy CA
openssl req -new -key /conf/server.tls.key -out /conf/server.tls.csr \
-subj "/CN=${SERVER_NAME}"
Expand Down
56 changes: 52 additions & 4 deletions internal/b/blueprints.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,13 @@
package b

import (
"crypto/rand"
"encoding/hex"
"fmt"
"strconv"
"strings"

"github.com/sirupsen/logrus"
)

// KnownBlueprints lists static blueprints
Expand All @@ -26,6 +30,7 @@ var KnownBlueprints = map[string]*Blueprint{
BlueprintAlice.Name: &BlueprintAlice,
BlueprintFederationOneToOneRoom.Name: &BlueprintFederationOneToOneRoom,
BlueprintFederationTwoLocalOneRemote.Name: &BlueprintFederationTwoLocalOneRemote,
BlueprintHSWithApplicationService.Name: &BlueprintHSWithApplicationService,
BlueprintOneToOneRoom.Name: &BlueprintOneToOneRoom,
BlueprintPerfManyMessages.Name: &BlueprintPerfManyMessages,
BlueprintPerfManyRooms.Name: &BlueprintPerfManyRooms,
Expand All @@ -46,6 +51,8 @@ type Homeserver struct {
Users []User
// The list of rooms to create on this homeserver
Rooms []Room
// The list of application services to create on the homeserver
ApplicationServices []ApplicationService
}

type User struct {
Expand All @@ -68,11 +75,22 @@ type Room struct {
Events []Event
}

type ApplicationService struct {
ID string
HSToken string
ASToken string
URL string
SenderLocalpart string
RateLimited bool
}

type Event struct {
Type string
Sender string
StateKey *string
Content map[string]interface{}
Type string
Sender string
OriginServerTS uint64
StateKey *string
PrevEvents []string
Content map[string]interface{}
// This field is ignored in blueprints as clients are unable to set it. Used with federation.Server
Unsigned map[string]interface{}
}
Expand Down Expand Up @@ -107,7 +125,18 @@ func Validate(bp Blueprint) (Blueprint, error) {
return bp, err
}
}
for i, as := range hs.ApplicationServices {
hs.ApplicationServices[i], err = normalizeApplicationService(as)
if err != nil {
return bp, err
}
}
}

logrus.WithFields(logrus.Fields{
"bp": bp.Homeservers[0].ApplicationServices,
}).Error("after modfiying bp")

return bp, nil
}

Expand Down Expand Up @@ -152,6 +181,25 @@ func normaliseUser(u string, hsName string) (string, error) {
return u, nil
}

func normalizeApplicationService(as ApplicationService) (ApplicationService, error) {
hsToken := make([]byte, 32)
_, err := rand.Read(hsToken)
if err != nil {
return as, err
}

asToken := make([]byte, 32)
_, err = rand.Read(asToken)
if err != nil {
return as, err
}

as.HSToken = hex.EncodeToString(hsToken)
as.ASToken = hex.EncodeToString(asToken)

return as, err
}

// Ptr returns a pointer to `in`, because Go doesn't allow you to inline this.
func Ptr(in string) *string {
return &in
Expand Down
25 changes: 25 additions & 0 deletions internal/b/hs_with_application_service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package b

// BlueprintHSWithApplicationService who has an application service to interact with
var BlueprintHSWithApplicationService = MustValidate(Blueprint{
Name: "alice",
Homeservers: []Homeserver{
{
Name: "hs1",
Users: []User{
{
Localpart: "@alice",
DisplayName: "Alice",
},
},
ApplicationServices: []ApplicationService{
{
ID: "my_as_id",
URL: "http://localhost:9000",
SenderLocalpart: "the-bridge-user",
RateLimited: false,
},
},
},
},
})
Loading

0 comments on commit cf4d8e9

Please sign in to comment.