diff --git a/cmd/dendrite-client-api-server/main.go b/cmd/dendrite-client-api-server/main.go index d4ab9e42f..aa621b3af 100644 --- a/cmd/dendrite-client-api-server/main.go +++ b/cmd/dendrite-client-api-server/main.go @@ -17,7 +17,6 @@ package main import ( "github.com/matrix-org/dendrite/clientapi" "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" @@ -31,9 +30,10 @@ func main() { accountDB := base.CreateAccountsDB() deviceDB := base.CreateDeviceDB() - keyDB := base.CreateKeyDB() federation := base.CreateFederationClient() - keyRing := keydb.CreateKeyRing(federation.Client, keyDB, cfg.Matrix.KeyPerspectives) + + serverKeyAPI := base.CreateHTTPServerKeyAPIs() + keyRing := serverKeyAPI.KeyRing() asQuery := base.CreateHTTPAppServiceAPIs() rsAPI := base.CreateHTTPRoomserverAPIs() @@ -42,7 +42,7 @@ func main() { eduInputAPI := eduserver.SetupEDUServerComponent(base, cache.New()) clientapi.SetupClientAPIComponent( - base, deviceDB, accountDB, federation, &keyRing, + base, deviceDB, accountDB, federation, keyRing, rsAPI, eduInputAPI, asQuery, transactions.New(), fsAPI, ) diff --git a/cmd/dendrite-federation-api-server/main.go b/cmd/dendrite-federation-api-server/main.go index b37570201..28ba22896 100644 --- a/cmd/dendrite-federation-api-server/main.go +++ b/cmd/dendrite-federation-api-server/main.go @@ -17,7 +17,6 @@ package main import ( "github.com/matrix-org/dendrite/clientapi/producers" "github.com/matrix-org/dendrite/common/basecomponent" - "github.com/matrix-org/dendrite/common/keydb" "github.com/matrix-org/dendrite/eduserver" "github.com/matrix-org/dendrite/eduserver/cache" "github.com/matrix-org/dendrite/federationapi" @@ -30,10 +29,12 @@ func main() { accountDB := base.CreateAccountsDB() deviceDB := base.CreateDeviceDB() - keyDB := base.CreateKeyDB() federation := base.CreateFederationClient() + + serverKeyAPI := base.CreateHTTPServerKeyAPIs() + keyRing := serverKeyAPI.KeyRing() + fsAPI := base.CreateHTTPFederationSenderAPIs() - keyRing := keydb.CreateKeyRing(federation.Client, keyDB, cfg.Matrix.KeyPerspectives) rsAPI := base.CreateHTTPRoomserverAPIs() asAPI := base.CreateHTTPAppServiceAPIs() @@ -42,7 +43,7 @@ func main() { eduProducer := producers.NewEDUServerProducer(eduInputAPI) federationapi.SetupFederationAPIComponent( - base, accountDB, deviceDB, federation, &keyRing, + base, accountDB, deviceDB, federation, keyRing, rsAPI, asAPI, fsAPI, eduProducer, ) diff --git a/cmd/dendrite-federation-sender-server/main.go b/cmd/dendrite-federation-sender-server/main.go index c9bf6688f..758a5166b 100644 --- a/cmd/dendrite-federation-sender-server/main.go +++ b/cmd/dendrite-federation-sender-server/main.go @@ -16,7 +16,6 @@ package main import ( "github.com/matrix-org/dendrite/common/basecomponent" - "github.com/matrix-org/dendrite/common/keydb" "github.com/matrix-org/dendrite/federationsender" ) @@ -26,11 +25,13 @@ func main() { defer base.Close() // nolint: errcheck federation := base.CreateFederationClient() - keyDB := base.CreateKeyDB() - keyRing := keydb.CreateKeyRing(federation.Client, keyDB, cfg.Matrix.KeyPerspectives) + + serverKeyAPI := base.CreateHTTPServerKeyAPIs() + keyRing := serverKeyAPI.KeyRing() + rsAPI := base.CreateHTTPRoomserverAPIs() fsAPI := federationsender.SetupFederationSenderComponent( - base, federation, rsAPI, &keyRing, + base, federation, rsAPI, keyRing, ) rsAPI.SetFederationSenderAPI(fsAPI) diff --git a/cmd/dendrite-room-server/main.go b/cmd/dendrite-room-server/main.go index 4d450bd47..58088a031 100644 --- a/cmd/dendrite-room-server/main.go +++ b/cmd/dendrite-room-server/main.go @@ -16,7 +16,6 @@ package main import ( "github.com/matrix-org/dendrite/common/basecomponent" - "github.com/matrix-org/dendrite/common/keydb" "github.com/matrix-org/dendrite/roomserver" ) @@ -24,9 +23,10 @@ func main() { cfg := basecomponent.ParseFlags() base := basecomponent.NewBaseDendrite(cfg, "RoomServerAPI", true) defer base.Close() // nolint: errcheck - keyDB := base.CreateKeyDB() federation := base.CreateFederationClient() - keyRing := keydb.CreateKeyRing(federation.Client, keyDB, cfg.Matrix.KeyPerspectives) + + serverKeyAPI := base.CreateHTTPServerKeyAPIs() + keyRing := serverKeyAPI.KeyRing() fsAPI := base.CreateHTTPFederationSenderAPIs() rsAPI := roomserver.SetupRoomServerComponent(base, keyRing, federation) diff --git a/common/basecomponent/base.go b/common/basecomponent/base.go index 314c85015..86d597c97 100644 --- a/common/basecomponent/base.go +++ b/common/basecomponent/base.go @@ -186,29 +186,6 @@ func (b *BaseDendrite) CreateAccountsDB() accounts.Database { return db } -// 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), - b.Cfg.DbProperties(), - b.Cfg.Matrix.ServerName, - b.Cfg.Matrix.PrivateKey.Public().(ed25519.PublicKey), - b.Cfg.Matrix.KeyID, - ) - if err != nil { - logrus.WithError(err).Panicf("failed to connect to keys db") - } - - cachedDB, err := cache.NewKeyDatabase(db, b.ImmutableCache) - if err != nil { - logrus.WithError(err).Panicf("failed to create key cache wrapper") - } - return cachedDB -} -*/ - // CreateFederationClient creates a new federation client. Should only be called // once per component. func (b *BaseDendrite) CreateFederationClient() *gomatrixserverlib.FederationClient { diff --git a/serverkeyapi/api/api.go b/serverkeyapi/api/api.go index 539b1c9b3..2162ecfc9 100644 --- a/serverkeyapi/api/api.go +++ b/serverkeyapi/api/api.go @@ -1,6 +1,7 @@ package api import ( + "context" "errors" "net/http" @@ -10,6 +11,20 @@ import ( type ServerKeyInternalAPI interface { gomatrixserverlib.KeyDatabase + + KeyRing() *gomatrixserverlib.KeyRing + + InputPublicKeys( + ctx context.Context, + request *InputPublicKeysRequest, + response *InputPublicKeysResponse, + ) error + + QueryPublicKeys( + ctx context.Context, + request *QueryPublicKeysRequest, + response *QueryPublicKeysResponse, + ) error } // NewRoomserverInputAPIHTTP creates a RoomserverInputAPI implemented by talking to a HTTP POST API. @@ -28,3 +43,18 @@ func NewServerKeyInternalAPIHTTP( immutableCache: immutableCache, }, nil } + +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}, + } +} diff --git a/serverkeyapi/api/http.go b/serverkeyapi/api/http.go index 41189c686..af0bafe54 100644 --- a/serverkeyapi/api/http.go +++ b/serverkeyapi/api/http.go @@ -2,37 +2,52 @@ package api import ( "context" - "net/http" - "github.com/matrix-org/dendrite/common/caching" - "github.com/matrix-org/gomatrixserverlib" + commonHTTP "github.com/matrix-org/dendrite/common/http" + + "github.com/opentracing/opentracing-go" ) -type httpServerKeyInternalAPI struct { - ServerKeyInternalAPI +const ( + // RoomserverPerformJoinPath is the HTTP path for the PerformJoin API. + ServerKeyInputPublicKeyPath = "/api/serverkeyapi/inputPublicKey" - serverKeyAPIURL string - httpClient *http.Client - immutableCache caching.ImmutableCache + // RoomserverPerformLeavePath is the HTTP path for the PerformLeave API. + ServerKeyQueryPublicKeyPath = "/api/serverkeyapi/queryPublicKey" +) + +type InputPublicKeysRequest struct { } -func (s *httpServerKeyInternalAPI) KeyRing() *gomatrixserverlib.KeyRing { - return &gomatrixserverlib.KeyRing{ - KeyDatabase: s, - KeyFetchers: []gomatrixserverlib.KeyFetcher{s}, - } +type InputPublicKeysResponse struct { } -func (s *httpServerKeyInternalAPI) StoreKeys( +func (h *httpServerKeyInternalAPI) InputPublicKeys( ctx context.Context, - results map[gomatrixserverlib.PublicKeyLookupRequest]gomatrixserverlib.PublicKeyLookupResult, + request *InputPublicKeysRequest, + response *InputPublicKeysResponse, ) error { - return nil + span, ctx := opentracing.StartSpanFromContext(ctx, "InputPublicKey") + defer span.Finish() + + apiURL := h.serverKeyAPIURL + ServerKeyInputPublicKeyPath + return commonHTTP.PostJSON(ctx, span, h.httpClient, apiURL, request, response) } -func (s *httpServerKeyInternalAPI) FetchKeys( - ctx context.Context, - requests map[gomatrixserverlib.PublicKeyLookupRequest]gomatrixserverlib.Timestamp, -) (map[gomatrixserverlib.PublicKeyLookupRequest]gomatrixserverlib.PublicKeyLookupResult, error) { - return nil, nil +type QueryPublicKeysRequest struct { +} + +type QueryPublicKeysResponse struct { +} + +func (h *httpServerKeyInternalAPI) QueryPublicKeys( + ctx context.Context, + request *QueryPublicKeysRequest, + response *QueryPublicKeysResponse, +) error { + span, ctx := opentracing.StartSpanFromContext(ctx, "QueryPublicKey") + defer span.Finish() + + apiURL := h.serverKeyAPIURL + ServerKeyQueryPublicKeyPath + return commonHTTP.PostJSON(ctx, span, h.httpClient, apiURL, request, response) } diff --git a/serverkeyapi/api/satisfy.go b/serverkeyapi/api/satisfy.go new file mode 100644 index 000000000..e43c011d8 --- /dev/null +++ b/serverkeyapi/api/satisfy.go @@ -0,0 +1,25 @@ +package api + +import ( + "context" + + "github.com/matrix-org/gomatrixserverlib" +) + +func (s *httpServerKeyInternalAPI) FetcherName() string { + return "httpServerKeyInternalAPI" +} + +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 +} diff --git a/serverkeyapi/internal/api.go b/serverkeyapi/internal/api.go index 4a0f4e3e8..5b711156d 100644 --- a/serverkeyapi/internal/api.go +++ b/serverkeyapi/internal/api.go @@ -5,12 +5,13 @@ import ( "github.com/matrix-org/dendrite/common/caching" "github.com/matrix-org/dendrite/common/config" + "github.com/matrix-org/dendrite/serverkeyapi/api" "github.com/matrix-org/dendrite/serverkeyapi/storage" "github.com/matrix-org/gomatrixserverlib" ) type ServerKeyAPI struct { - gomatrixserverlib.KeyDatabase + api.ServerKeyInternalAPI DB storage.Database Cfg *config.Dendrite @@ -36,3 +37,7 @@ func (s *ServerKeyAPI) FetchKeys( ) (map[gomatrixserverlib.PublicKeyLookupRequest]gomatrixserverlib.PublicKeyLookupResult, error) { return s.DB.FetchKeys(ctx, requests) } + +func (s *ServerKeyAPI) FetcherName() string { + return s.DB.FetcherName() +} diff --git a/serverkeyapi/internal/http.go b/serverkeyapi/internal/http.go new file mode 100644 index 000000000..a13a62fef --- /dev/null +++ b/serverkeyapi/internal/http.go @@ -0,0 +1,43 @@ +package internal + +import ( + "encoding/json" + "net/http" + + "github.com/matrix-org/dendrite/common" + "github.com/matrix-org/dendrite/serverkeyapi/api" + "github.com/matrix-org/util" +) + +func (s *ServerKeyAPI) SetupHTTP(servMux *http.ServeMux) { + servMux.Handle(api.ServerKeyQueryPublicKeyPath, + common.MakeInternalAPI("queryPublicKeys", func(req *http.Request) util.JSONResponse { + var request api.QueryPublicKeysRequest + var response api.QueryPublicKeysResponse + if err := json.NewDecoder(req.Body).Decode(&request); err != nil { + return util.MessageResponse(http.StatusBadRequest, err.Error()) + } + /* + if err := s.DB.FetchKeys(); err != nil { + return util.ErrorResponse(err) + } + */ + return util.JSONResponse{Code: http.StatusOK, JSON: &response} + }), + ) + servMux.Handle(api.ServerKeyInputPublicKeyPath, + common.MakeInternalAPI("inputPublicKeys", func(req *http.Request) util.JSONResponse { + var request api.InputPublicKeysRequest + var response api.InputPublicKeysResponse + if err := json.NewDecoder(req.Body).Decode(&request); err != nil { + return util.MessageResponse(http.StatusBadRequest, err.Error()) + } + /* + if err := s.DB.FetchKeys(); err != nil { + return util.ErrorResponse(err) + } + */ + return util.JSONResponse{Code: http.StatusOK, JSON: &response} + }), + ) +}