Server key API (works for monolith but not for polylith yet)

This commit is contained in:
Neil Alexander 2020-05-19 17:49:49 +01:00
parent 8b3100935c
commit 14565ec4a4
18 changed files with 243 additions and 112 deletions

View file

@ -34,7 +34,6 @@ import (
"github.com/matrix-org/dendrite/cmd/dendrite-demo-libp2p/storage"
"github.com/matrix-org/dendrite/common"
"github.com/matrix-org/dendrite/common/config"
"github.com/matrix-org/dendrite/common/keydb"
"github.com/matrix-org/dendrite/common/transactions"
"github.com/matrix-org/dendrite/eduserver"
"github.com/matrix-org/dendrite/federationapi"
@ -42,6 +41,7 @@ import (
"github.com/matrix-org/dendrite/mediaapi"
"github.com/matrix-org/dendrite/publicroomsapi"
"github.com/matrix-org/dendrite/roomserver"
"github.com/matrix-org/dendrite/serverkeyapi"
"github.com/matrix-org/dendrite/syncapi"
"github.com/matrix-org/gomatrixserverlib"
@ -53,17 +53,8 @@ import (
func createKeyDB(
base *P2PDendrite,
) keydb.Database {
db, err := keydb.NewDatabase(
string(base.Base.Cfg.Database.ServerKey),
base.Base.Cfg.DbProperties(),
base.Base.Cfg.Matrix.ServerName,
base.Base.Cfg.Matrix.PrivateKey.Public().(ed25519.PublicKey),
base.Base.Cfg.Matrix.KeyID,
)
if err != nil {
logrus.WithError(err).Panicf("failed to connect to keys db")
}
db gomatrixserverlib.KeyDatabase,
) {
mdns := mDNSListener{
host: base.LibP2P,
keydb: db,
@ -78,7 +69,6 @@ func createKeyDB(
panic(err)
}
serv.RegisterNotifee(&mdns)
return db
}
func createFederationClient(
@ -145,9 +135,15 @@ func main() {
accountDB := base.Base.CreateAccountsDB()
deviceDB := base.Base.CreateDeviceDB()
keyDB := createKeyDB(base)
federation := createFederationClient(base)
keyRing := keydb.CreateKeyRing(federation.Client, keyDB, cfg.Matrix.KeyPerspectives)
serverKeyAPI := serverkeyapi.SetupServerKeyAPIComponent(
&base.Base, federation,
)
keyRing := serverKeyAPI.KeyRing()
createKeyDB(
base, serverKeyAPI,
)
rsAPI := roomserver.SetupRoomServerComponent(
&base.Base, keyRing, federation,
@ -159,17 +155,17 @@ func main() {
&base.Base, accountDB, deviceDB, federation, rsAPI, transactions.New(),
)
fsAPI := federationsender.SetupFederationSenderComponent(
&base.Base, federation, rsAPI, &keyRing,
&base.Base, federation, rsAPI, keyRing,
)
rsAPI.SetFederationSenderAPI(fsAPI)
clientapi.SetupClientAPIComponent(
&base.Base, deviceDB, accountDB,
federation, &keyRing, rsAPI,
federation, keyRing, rsAPI,
eduInputAPI, asAPI, transactions.New(), fsAPI,
)
eduProducer := producers.NewEDUServerProducer(eduInputAPI)
federationapi.SetupFederationAPIComponent(&base.Base, accountDB, deviceDB, federation, &keyRing, rsAPI, asAPI, fsAPI, eduProducer)
federationapi.SetupFederationAPIComponent(&base.Base, accountDB, deviceDB, federation, keyRing, rsAPI, asAPI, fsAPI, eduProducer)
mediaapi.SetupMediaAPIComponent(&base.Base, deviceDB)
publicRoomsDB, err := storage.NewPublicRoomsServerDatabaseWithPubSub(string(base.Base.Cfg.Database.PublicRoomsAPI), base.LibP2PPubsub)
if err != nil {

View file

@ -21,12 +21,11 @@ import (
"github.com/libp2p/go-libp2p-core/host"
"github.com/libp2p/go-libp2p-core/peer"
"github.com/matrix-org/dendrite/common/keydb"
"github.com/matrix-org/gomatrixserverlib"
)
type mDNSListener struct {
keydb keydb.Database
keydb gomatrixserverlib.KeyDatabase
host host.Host
}

View file

@ -23,7 +23,6 @@ import (
"github.com/matrix-org/dendrite/clientapi/producers"
"github.com/matrix-org/dendrite/common"
"github.com/matrix-org/dendrite/common/basecomponent"
"github.com/matrix-org/dendrite/common/keydb"
"github.com/matrix-org/dendrite/common/transactions"
"github.com/matrix-org/dendrite/eduserver"
"github.com/matrix-org/dendrite/eduserver/cache"
@ -34,6 +33,7 @@ import (
"github.com/matrix-org/dendrite/publicroomsapi"
"github.com/matrix-org/dendrite/publicroomsapi/storage"
"github.com/matrix-org/dendrite/roomserver"
"github.com/matrix-org/dendrite/serverkeyapi"
"github.com/matrix-org/dendrite/syncapi"
"github.com/prometheus/client_golang/prometheus/promhttp"
@ -55,9 +55,12 @@ func main() {
accountDB := base.CreateAccountsDB()
deviceDB := base.CreateDeviceDB()
keyDB := base.CreateKeyDB()
federation := base.CreateFederationClient()
keyRing := keydb.CreateKeyRing(federation.Client, keyDB, cfg.Matrix.KeyPerspectives)
serverKeyAPI := serverkeyapi.SetupServerKeyAPIComponent(
base, federation,
)
keyRing := serverKeyAPI.KeyRing()
rsAPI := roomserver.SetupRoomServerComponent(
base, keyRing, federation,
@ -69,20 +72,20 @@ func main() {
base, accountDB, deviceDB, federation, rsAPI, transactions.New(),
)
fsAPI := federationsender.SetupFederationSenderComponent(
base, federation, rsAPI, &keyRing,
base, federation, rsAPI, keyRing,
)
rsAPI.SetFederationSenderAPI(fsAPI)
clientapi.SetupClientAPIComponent(
base, deviceDB, accountDB,
federation, &keyRing, rsAPI,
federation, keyRing, rsAPI,
eduInputAPI, asAPI, transactions.New(), fsAPI,
)
keyserver.SetupKeyServerComponent(
base, deviceDB, accountDB,
)
eduProducer := producers.NewEDUServerProducer(eduInputAPI)
federationapi.SetupFederationAPIComponent(base, accountDB, deviceDB, federation, &keyRing, rsAPI, asAPI, fsAPI, eduProducer)
federationapi.SetupFederationAPIComponent(base, accountDB, deviceDB, federation, keyRing, rsAPI, asAPI, fsAPI, eduProducer)
mediaapi.SetupMediaAPIComponent(base, deviceDB)
publicRoomsDB, err := storage.NewPublicRoomsServerDatabase(string(base.Cfg.Database.PublicRoomsAPI), base.Cfg.DbProperties())
if err != nil {

View file

@ -37,6 +37,7 @@ import (
"github.com/matrix-org/dendrite/publicroomsapi"
"github.com/matrix-org/dendrite/publicroomsapi/storage"
"github.com/matrix-org/dendrite/roomserver"
"github.com/matrix-org/dendrite/serverkeyapi"
"github.com/matrix-org/dendrite/syncapi"
go_http_js_libp2p "github.com/matrix-org/go-http-js-libp2p"
"github.com/matrix-org/gomatrixserverlib"
@ -192,13 +193,16 @@ func main() {
accountDB := base.CreateAccountsDB()
deviceDB := base.CreateDeviceDB()
keyDB := base.CreateKeyDB()
federation := createFederationClient(cfg, node)
serverKeyAPI := serverkeyapi.SetupServerKeyAPIComponent(
base, federation,
)
keyRing := gomatrixserverlib.KeyRing{
KeyFetchers: []gomatrixserverlib.KeyFetcher{
&libp2pKeyFetcher{},
},
KeyDatabase: keyDB,
KeyDatabase: serverKeyAPI,
}
p2pPublicRoomProvider := NewLibP2PPublicRoomsProvider(node)

View file

@ -21,11 +21,7 @@ import (
"net/url"
"time"
"golang.org/x/crypto/ed25519"
"github.com/matrix-org/dendrite/common/caching"
"github.com/matrix-org/dendrite/common/keydb"
"github.com/matrix-org/dendrite/common/keydb/cache"
"github.com/matrix-org/dendrite/internal/sqlutil"
"github.com/matrix-org/gomatrixserverlib"
"github.com/matrix-org/naffka"
@ -42,6 +38,7 @@ import (
eduServerAPI "github.com/matrix-org/dendrite/eduserver/api"
federationSenderAPI "github.com/matrix-org/dendrite/federationsender/api"
roomserverAPI "github.com/matrix-org/dendrite/roomserver/api"
serverKeyAPI "github.com/matrix-org/dendrite/serverkeyapi/api"
"github.com/sirupsen/logrus"
_ "net/http/pprof"
@ -153,6 +150,20 @@ func (b *BaseDendrite) CreateHTTPFederationSenderAPIs() federationSenderAPI.Fede
return f
}
// CreateHTTPFederationSenderAPIs returns FederationSenderInternalAPI for hitting
// the federation sender over HTTP
func (b *BaseDendrite) CreateHTTPServerKeyAPIs() serverKeyAPI.ServerKeyInternalAPI {
f, err := serverKeyAPI.NewServerKeyInternalAPIHTTP(
b.Cfg.FederationSenderURL(),
b.httpClient,
b.ImmutableCache,
)
if err != nil {
logrus.WithError(err).Panic("NewServerKeyInternalAPIHTTP failed", b.httpClient)
}
return f
}
// CreateDeviceDB creates a new instance of the device database. Should only be
// called once per component.
func (b *BaseDendrite) CreateDeviceDB() devices.Database {
@ -177,6 +188,7 @@ func (b *BaseDendrite) CreateAccountsDB() accounts.Database {
// CreateKeyDB creates a new instance of the key database. Should only be called
// once per component.
/*
func (b *BaseDendrite) CreateKeyDB() keydb.Database {
db, err := keydb.NewDatabase(
string(b.Cfg.Database.ServerKey),
@ -195,6 +207,7 @@ func (b *BaseDendrite) CreateKeyDB() keydb.Database {
}
return cachedDB
}
*/
// CreateFederationClient creates a new federation client. Should only be called
// once per component.

View file

@ -1,74 +0,0 @@
// Copyright 2017 New Vector Ltd
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package keydb
import (
"encoding/base64"
"github.com/matrix-org/dendrite/common/config"
"github.com/matrix-org/gomatrixserverlib"
"github.com/sirupsen/logrus"
"golang.org/x/crypto/ed25519"
)
// CreateKeyRing creates and configures a KeyRing object.
//
// It creates the necessary key fetchers and collects them into a KeyRing
// backed by the given KeyDatabase.
func CreateKeyRing(client gomatrixserverlib.Client,
keyDB gomatrixserverlib.KeyDatabase,
cfg config.KeyPerspectives) gomatrixserverlib.KeyRing {
fetchers := gomatrixserverlib.KeyRing{
KeyFetchers: []gomatrixserverlib.KeyFetcher{
&gomatrixserverlib.DirectKeyFetcher{
Client: client,
},
},
KeyDatabase: keyDB,
}
logrus.Info("Enabled direct key fetcher")
var b64e = base64.StdEncoding.WithPadding(base64.NoPadding)
for _, ps := range cfg {
perspective := &gomatrixserverlib.PerspectiveKeyFetcher{
PerspectiveServerName: ps.ServerName,
PerspectiveServerKeys: map[gomatrixserverlib.KeyID]ed25519.PublicKey{},
Client: 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
}
fetchers.KeyFetchers = append(fetchers.KeyFetchers, perspective)
logrus.WithFields(logrus.Fields{
"server_name": ps.ServerName,
"num_public_keys": len(ps.Keys),
}).Info("Enabled perspective key fetcher")
}
return fetchers
}

30
serverkeyapi/api/api.go Normal file
View file

@ -0,0 +1,30 @@
package api
import (
"errors"
"net/http"
"github.com/matrix-org/dendrite/common/caching"
"github.com/matrix-org/gomatrixserverlib"
)
type ServerKeyInternalAPI interface {
gomatrixserverlib.KeyDatabase
}
// NewRoomserverInputAPIHTTP creates a RoomserverInputAPI implemented by talking to a HTTP POST API.
// If httpClient is nil an error is returned
func NewServerKeyInternalAPIHTTP(
serverKeyAPIURL string,
httpClient *http.Client,
immutableCache caching.ImmutableCache,
) (ServerKeyInternalAPI, error) {
if httpClient == nil {
return nil, errors.New("NewRoomserverInternalAPIHTTP: httpClient is <nil>")
}
return &httpServerKeyInternalAPI{
serverKeyAPIURL: serverKeyAPIURL,
httpClient: httpClient,
immutableCache: immutableCache,
}, nil
}

38
serverkeyapi/api/http.go Normal file
View file

@ -0,0 +1,38 @@
package api
import (
"context"
"net/http"
"github.com/matrix-org/dendrite/common/caching"
"github.com/matrix-org/gomatrixserverlib"
)
type httpServerKeyInternalAPI struct {
ServerKeyInternalAPI
serverKeyAPIURL string
httpClient *http.Client
immutableCache caching.ImmutableCache
}
func (s *httpServerKeyInternalAPI) KeyRing() *gomatrixserverlib.KeyRing {
return &gomatrixserverlib.KeyRing{
KeyDatabase: s,
KeyFetchers: []gomatrixserverlib.KeyFetcher{s},
}
}
func (s *httpServerKeyInternalAPI) StoreKeys(
ctx context.Context,
results map[gomatrixserverlib.PublicKeyLookupRequest]gomatrixserverlib.PublicKeyLookupResult,
) error {
return nil
}
func (s *httpServerKeyInternalAPI) FetchKeys(
ctx context.Context,
requests map[gomatrixserverlib.PublicKeyLookupRequest]gomatrixserverlib.Timestamp,
) (map[gomatrixserverlib.PublicKeyLookupRequest]gomatrixserverlib.PublicKeyLookupResult, error) {
return nil, nil
}

View file

@ -0,0 +1,38 @@
package internal
import (
"context"
"github.com/matrix-org/dendrite/common/caching"
"github.com/matrix-org/dendrite/common/config"
"github.com/matrix-org/dendrite/serverkeyapi/storage"
"github.com/matrix-org/gomatrixserverlib"
)
type ServerKeyAPI struct {
gomatrixserverlib.KeyDatabase
DB storage.Database
Cfg *config.Dendrite
ImmutableCache caching.ImmutableCache
OurKeyRing gomatrixserverlib.KeyRing
FedClient *gomatrixserverlib.FederationClient
}
func (s *ServerKeyAPI) KeyRing() *gomatrixserverlib.KeyRing {
return &s.OurKeyRing
}
func (s *ServerKeyAPI) StoreKeys(
ctx context.Context,
results map[gomatrixserverlib.PublicKeyLookupRequest]gomatrixserverlib.PublicKeyLookupResult,
) error {
return s.DB.StoreKeys(ctx, results)
}
func (s *ServerKeyAPI) FetchKeys(
ctx context.Context,
requests map[gomatrixserverlib.PublicKeyLookupRequest]gomatrixserverlib.Timestamp,
) (map[gomatrixserverlib.PublicKeyLookupRequest]gomatrixserverlib.PublicKeyLookupResult, error) {
return s.DB.FetchKeys(ctx, requests)
}

View file

@ -0,0 +1,82 @@
package serverkeyapi
import (
"crypto/ed25519"
"encoding/base64"
"github.com/matrix-org/dendrite/common/basecomponent"
"github.com/matrix-org/dendrite/serverkeyapi/internal"
"github.com/matrix-org/dendrite/serverkeyapi/storage"
"github.com/matrix-org/gomatrixserverlib"
"github.com/sirupsen/logrus"
)
func SetupServerKeyAPIComponent(
base *basecomponent.BaseDendrite,
fedClient *gomatrixserverlib.FederationClient,
) *internal.ServerKeyAPI {
serverKeyDB, 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 room server db")
}
internalAPI := internal.ServerKeyAPI{
DB: serverKeyDB,
Cfg: base.Cfg,
ImmutableCache: base.ImmutableCache,
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")
}
/*
if base.EnableHTTPAPIs {
internalAPI.SetupHTTP(http.DefaultServeMux)
}
*/
return &internalAPI
}

View file

@ -1,4 +1,4 @@
package keydb
package storage
import (
"context"

View file

@ -14,7 +14,7 @@
// +build !wasm
package keydb
package storage
import (
"net/url"
@ -22,8 +22,8 @@ import (
"golang.org/x/crypto/ed25519"
"github.com/matrix-org/dendrite/common"
"github.com/matrix-org/dendrite/common/keydb/postgres"
"github.com/matrix-org/dendrite/common/keydb/sqlite3"
"github.com/matrix-org/dendrite/serverkeyapi/storage/postgres"
"github.com/matrix-org/dendrite/serverkeyapi/storage/sqlite3"
"github.com/matrix-org/gomatrixserverlib"
)

View file

@ -12,7 +12,9 @@
// See the License for the specific language governing permissions and
// limitations under the License.
package keydb
// +build wasm
package storage
import (
"fmt"
@ -21,7 +23,7 @@ import (
"golang.org/x/crypto/ed25519"
"github.com/matrix-org/dendrite/common"
"github.com/matrix-org/dendrite/common/keydb/sqlite3"
"github.com/matrix-org/dendrite/serverkeyapi/storage/sqlite3"
"github.com/matrix-org/gomatrixserverlib"
)