dendrite/serverkeyapi/serverkeyapi.go

84 lines
2.4 KiB
Go
Raw Normal View History

package serverkeyapi
import (
"crypto/ed25519"
"encoding/base64"
"github.com/matrix-org/dendrite/internal/basecomponent"
"github.com/matrix-org/dendrite/serverkeyapi/api"
"github.com/matrix-org/dendrite/serverkeyapi/internal"
2020-06-04 10:26:35 -05:00
"github.com/matrix-org/dendrite/serverkeyapi/inthttp"
"github.com/matrix-org/dendrite/serverkeyapi/storage"
"github.com/matrix-org/dendrite/serverkeyapi/storage/cache"
"github.com/matrix-org/gomatrixserverlib"
"github.com/sirupsen/logrus"
)
func SetupServerKeyAPIComponent(
base *basecomponent.BaseDendrite,
fedClient *gomatrixserverlib.FederationClient,
) api.ServerKeyInternalAPI {
innerDB, err := storage.NewDatabase(
string(base.Cfg.Database.ServerKey),
base.Cfg.DbProperties(),
base.Cfg.Matrix.ServerName,
base.Cfg.Matrix.PrivateKey.Public().(ed25519.PublicKey),
base.Cfg.Matrix.KeyID,
)
if err != nil {
logrus.WithError(err).Panicf("failed to connect to server key database")
}
2020-06-05 10:42:01 -05:00
serverKeyDB, err := cache.NewKeyDatabase(innerDB, base.Caches)
if err != nil {
logrus.WithError(err).Panicf("failed to set up caching wrapper for server key database")
}
internalAPI := internal.ServerKeyAPI{
2020-06-04 10:26:35 -05:00
FedClient: fedClient,
OurKeyRing: gomatrixserverlib.KeyRing{
KeyFetchers: []gomatrixserverlib.KeyFetcher{
&gomatrixserverlib.DirectKeyFetcher{
Client: fedClient.Client,
},
},
KeyDatabase: serverKeyDB,
},
}
var b64e = base64.StdEncoding.WithPadding(base64.NoPadding)
for _, ps := range base.Cfg.Matrix.KeyPerspectives {
perspective := &gomatrixserverlib.PerspectiveKeyFetcher{
PerspectiveServerName: ps.ServerName,
PerspectiveServerKeys: map[gomatrixserverlib.KeyID]ed25519.PublicKey{},
Client: fedClient.Client,
}
for _, key := range ps.Keys {
rawkey, err := b64e.DecodeString(key.PublicKey)
if err != nil {
logrus.WithError(err).WithFields(logrus.Fields{
"server_name": ps.ServerName,
"public_key": key.PublicKey,
}).Warn("Couldn't parse perspective key")
continue
}
perspective.PerspectiveServerKeys[key.KeyID] = rawkey
}
internalAPI.OurKeyRing.KeyFetchers = append(
internalAPI.OurKeyRing.KeyFetchers,
perspective,
)
logrus.WithFields(logrus.Fields{
"server_name": ps.ServerName,
"num_public_keys": len(ps.Keys),
}).Info("Enabled perspective key fetcher")
}
2020-06-05 10:42:01 -05:00
inthttp.AddRoutes(&internalAPI, base.InternalAPIMux, base.Caches)
return &internalAPI
}