Convert serverkeys to inthttp (#1097)

main
Kegsay 2020-06-04 16:26:35 +01:00 committed by GitHub
parent 9834ac97db
commit 2bd12f635c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 164 additions and 169 deletions

View File

@ -32,7 +32,7 @@ func main() {
deviceDB := base.CreateDeviceDB() deviceDB := base.CreateDeviceDB()
federation := base.CreateFederationClient() federation := base.CreateFederationClient()
serverKeyAPI := base.CreateHTTPServerKeyAPIs() serverKeyAPI := base.ServerKeyAPIClient()
keyRing := serverKeyAPI.KeyRing() keyRing := serverKeyAPI.KeyRing()
asQuery := base.AppserviceHTTPClient() asQuery := base.AppserviceHTTPClient()

View File

@ -31,7 +31,7 @@ func main() {
deviceDB := base.CreateDeviceDB() deviceDB := base.CreateDeviceDB()
federation := base.CreateFederationClient() federation := base.CreateFederationClient()
serverKeyAPI := base.CreateHTTPServerKeyAPIs() serverKeyAPI := base.ServerKeyAPIClient()
keyRing := serverKeyAPI.KeyRing() keyRing := serverKeyAPI.KeyRing()
fsAPI := base.FederationSenderHTTPClient() fsAPI := base.FederationSenderHTTPClient()

View File

@ -26,7 +26,7 @@ func main() {
federation := base.CreateFederationClient() federation := base.CreateFederationClient()
serverKeyAPI := base.CreateHTTPServerKeyAPIs() serverKeyAPI := base.ServerKeyAPIClient()
keyRing := serverKeyAPI.KeyRing() keyRing := serverKeyAPI.KeyRing()
rsAPI := base.RoomserverHTTPClient() rsAPI := base.RoomserverHTTPClient()

View File

@ -74,7 +74,7 @@ func main() {
base, federation, base, federation,
) )
if base.UseHTTPAPIs { if base.UseHTTPAPIs {
serverKeyAPI = base.CreateHTTPServerKeyAPIs() serverKeyAPI = base.ServerKeyAPIClient()
} }
keyRing := serverKeyAPI.KeyRing() keyRing := serverKeyAPI.KeyRing()

View File

@ -25,7 +25,7 @@ func main() {
defer base.Close() // nolint: errcheck defer base.Close() // nolint: errcheck
federation := base.CreateFederationClient() federation := base.CreateFederationClient()
serverKeyAPI := base.CreateHTTPServerKeyAPIs() serverKeyAPI := base.ServerKeyAPIClient()
keyRing := serverKeyAPI.KeyRing() keyRing := serverKeyAPI.KeyRing()
fsAPI := base.FederationSenderHTTPClient() fsAPI := base.FederationSenderHTTPClient()

View File

@ -45,6 +45,7 @@ import (
roomserverAPI "github.com/matrix-org/dendrite/roomserver/api" roomserverAPI "github.com/matrix-org/dendrite/roomserver/api"
rsinthttp "github.com/matrix-org/dendrite/roomserver/inthttp" rsinthttp "github.com/matrix-org/dendrite/roomserver/inthttp"
serverKeyAPI "github.com/matrix-org/dendrite/serverkeyapi/api" serverKeyAPI "github.com/matrix-org/dendrite/serverkeyapi/api"
skinthttp "github.com/matrix-org/dendrite/serverkeyapi/inthttp"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
_ "net/http/pprof" _ "net/http/pprof"
@ -176,10 +177,9 @@ func (b *BaseDendrite) FederationSenderHTTPClient() federationSenderAPI.Federati
return f return f
} }
// CreateHTTPServerKeyAPIs returns ServerKeyInternalAPI for hitting the server key // ServerKeyAPIClient returns ServerKeyInternalAPI for hitting the server key API over HTTP
// API over HTTP func (b *BaseDendrite) ServerKeyAPIClient() serverKeyAPI.ServerKeyInternalAPI {
func (b *BaseDendrite) CreateHTTPServerKeyAPIs() serverKeyAPI.ServerKeyInternalAPI { f, err := skinthttp.NewServerKeyClient(
f, err := serverKeyAPI.NewServerKeyInternalAPIHTTP(
b.Cfg.ServerKeyAPIURL(), b.Cfg.ServerKeyAPIURL(),
b.httpClient, b.httpClient,
b.ImmutableCache, b.ImmutableCache,

View File

@ -2,11 +2,7 @@ package api
import ( import (
"context" "context"
"errors"
"net/http"
"time"
"github.com/matrix-org/dendrite/internal/caching"
"github.com/matrix-org/gomatrixserverlib" "github.com/matrix-org/gomatrixserverlib"
) )
@ -28,97 +24,17 @@ type ServerKeyInternalAPI interface {
) error ) error
} }
// NewRoomserverInputAPIHTTP creates a RoomserverInputAPI implemented by talking to a HTTP POST API. type QueryPublicKeysRequest struct {
// If httpClient is nil an error is returned Requests map[gomatrixserverlib.PublicKeyLookupRequest]gomatrixserverlib.Timestamp `json:"requests"`
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
} }
type httpServerKeyInternalAPI struct { type QueryPublicKeysResponse struct {
ServerKeyInternalAPI Results map[gomatrixserverlib.PublicKeyLookupRequest]gomatrixserverlib.PublicKeyLookupResult `json:"results"`
serverKeyAPIURL string
httpClient *http.Client
immutableCache caching.ImmutableCache
} }
func (s *httpServerKeyInternalAPI) KeyRing() *gomatrixserverlib.KeyRing { type InputPublicKeysRequest struct {
// This is a bit of a cheat - we tell gomatrixserverlib that this API is Keys map[gomatrixserverlib.PublicKeyLookupRequest]gomatrixserverlib.PublicKeyLookupResult `json:"keys"`
// both the key database and the key fetcher. While this does have the
// rather unfortunate effect of preventing gomatrixserverlib from handling
// key fetchers directly, we can at least reimplement this behaviour on
// the other end of the API.
return &gomatrixserverlib.KeyRing{
KeyDatabase: s,
KeyFetchers: []gomatrixserverlib.KeyFetcher{s},
}
} }
func (s *httpServerKeyInternalAPI) FetcherName() string { type InputPublicKeysResponse struct {
return "httpServerKeyInternalAPI"
}
func (s *httpServerKeyInternalAPI) StoreKeys(
_ context.Context,
results map[gomatrixserverlib.PublicKeyLookupRequest]gomatrixserverlib.PublicKeyLookupResult,
) error {
// Run in a background context - we don't want to stop this work just
// because the caller gives up waiting.
ctx := context.Background()
request := InputPublicKeysRequest{
Keys: make(map[gomatrixserverlib.PublicKeyLookupRequest]gomatrixserverlib.PublicKeyLookupResult),
}
response := InputPublicKeysResponse{}
for req, res := range results {
request.Keys[req] = res
s.immutableCache.StoreServerKey(req, res)
}
return s.InputPublicKeys(ctx, &request, &response)
}
func (s *httpServerKeyInternalAPI) FetchKeys(
_ context.Context,
requests map[gomatrixserverlib.PublicKeyLookupRequest]gomatrixserverlib.Timestamp,
) (map[gomatrixserverlib.PublicKeyLookupRequest]gomatrixserverlib.PublicKeyLookupResult, error) {
// Run in a background context - we don't want to stop this work just
// because the caller gives up waiting.
ctx := context.Background()
result := make(map[gomatrixserverlib.PublicKeyLookupRequest]gomatrixserverlib.PublicKeyLookupResult)
request := QueryPublicKeysRequest{
Requests: make(map[gomatrixserverlib.PublicKeyLookupRequest]gomatrixserverlib.Timestamp),
}
response := QueryPublicKeysResponse{
Results: make(map[gomatrixserverlib.PublicKeyLookupRequest]gomatrixserverlib.PublicKeyLookupResult),
}
now := gomatrixserverlib.AsTimestamp(time.Now())
for req, ts := range requests {
if res, ok := s.immutableCache.GetServerKey(req); ok {
if now > res.ValidUntilTS && res.ExpiredTS == gomatrixserverlib.PublicKeyNotExpired {
continue
}
result[req] = res
continue
}
request.Requests[req] = ts
}
err := s.QueryPublicKeys(ctx, &request, &response)
if err != nil {
return nil, err
}
for req, res := range response.Results {
result[req] = res
s.immutableCache.StoreServerKey(req, res)
}
return result, nil
} }

View File

@ -1,57 +0,0 @@
package api
import (
"context"
commonHTTP "github.com/matrix-org/dendrite/internal/http"
"github.com/matrix-org/gomatrixserverlib"
"github.com/opentracing/opentracing-go"
)
const (
// ServerKeyInputPublicKeyPath is the HTTP path for the InputPublicKeys API.
ServerKeyInputPublicKeyPath = "/serverkeyapi/inputPublicKey"
// ServerKeyQueryPublicKeyPath is the HTTP path for the QueryPublicKeys API.
ServerKeyQueryPublicKeyPath = "/serverkeyapi/queryPublicKey"
)
type InputPublicKeysRequest struct {
Keys map[gomatrixserverlib.PublicKeyLookupRequest]gomatrixserverlib.PublicKeyLookupResult `json:"keys"`
}
type InputPublicKeysResponse struct {
}
func (h *httpServerKeyInternalAPI) InputPublicKeys(
ctx context.Context,
request *InputPublicKeysRequest,
response *InputPublicKeysResponse,
) error {
span, ctx := opentracing.StartSpanFromContext(ctx, "InputPublicKey")
defer span.Finish()
apiURL := h.serverKeyAPIURL + ServerKeyInputPublicKeyPath
return commonHTTP.PostJSON(ctx, span, h.httpClient, apiURL, request, response)
}
type QueryPublicKeysRequest struct {
Requests map[gomatrixserverlib.PublicKeyLookupRequest]gomatrixserverlib.Timestamp `json:"requests"`
}
type QueryPublicKeysResponse struct {
Results map[gomatrixserverlib.PublicKeyLookupRequest]gomatrixserverlib.PublicKeyLookupResult `json:"results"`
}
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)
}

View File

@ -5,7 +5,6 @@ import (
"fmt" "fmt"
"time" "time"
"github.com/matrix-org/dendrite/internal/caching"
"github.com/matrix-org/dendrite/serverkeyapi/api" "github.com/matrix-org/dendrite/serverkeyapi/api"
"github.com/matrix-org/gomatrixserverlib" "github.com/matrix-org/gomatrixserverlib"
) )
@ -13,9 +12,8 @@ import (
type ServerKeyAPI struct { type ServerKeyAPI struct {
api.ServerKeyInternalAPI api.ServerKeyInternalAPI
ImmutableCache caching.ImmutableCache OurKeyRing gomatrixserverlib.KeyRing
OurKeyRing gomatrixserverlib.KeyRing FedClient *gomatrixserverlib.FederationClient
FedClient *gomatrixserverlib.FederationClient
} }
func (s *ServerKeyAPI) KeyRing() *gomatrixserverlib.KeyRing { func (s *ServerKeyAPI) KeyRing() *gomatrixserverlib.KeyRing {

View File

@ -0,0 +1,137 @@
package inthttp
import (
"context"
"errors"
"net/http"
"time"
"github.com/matrix-org/dendrite/internal/caching"
internalHTTP "github.com/matrix-org/dendrite/internal/http"
"github.com/matrix-org/dendrite/serverkeyapi/api"
"github.com/matrix-org/gomatrixserverlib"
"github.com/opentracing/opentracing-go"
)
// HTTP paths for the internal HTTP APIs
const (
ServerKeyInputPublicKeyPath = "/serverkeyapi/inputPublicKey"
ServerKeyQueryPublicKeyPath = "/serverkeyapi/queryPublicKey"
)
// NewServerKeyClient creates a ServerKeyInternalAPI implemented by talking to a HTTP POST API.
// If httpClient is nil an error is returned
func NewServerKeyClient(
serverKeyAPIURL string,
httpClient *http.Client,
immutableCache caching.ImmutableCache,
) (api.ServerKeyInternalAPI, error) {
if httpClient == nil {
return nil, errors.New("NewRoomserverInternalAPIHTTP: httpClient is <nil>")
}
return &httpServerKeyInternalAPI{
serverKeyAPIURL: serverKeyAPIURL,
httpClient: httpClient,
immutableCache: immutableCache,
}, nil
}
type httpServerKeyInternalAPI struct {
serverKeyAPIURL string
httpClient *http.Client
immutableCache caching.ImmutableCache
}
func (s *httpServerKeyInternalAPI) KeyRing() *gomatrixserverlib.KeyRing {
// This is a bit of a cheat - we tell gomatrixserverlib that this API is
// both the key database and the key fetcher. While this does have the
// rather unfortunate effect of preventing gomatrixserverlib from handling
// key fetchers directly, we can at least reimplement this behaviour on
// the other end of the API.
return &gomatrixserverlib.KeyRing{
KeyDatabase: s,
KeyFetchers: []gomatrixserverlib.KeyFetcher{s},
}
}
func (s *httpServerKeyInternalAPI) FetcherName() string {
return "httpServerKeyInternalAPI"
}
func (s *httpServerKeyInternalAPI) StoreKeys(
_ context.Context,
results map[gomatrixserverlib.PublicKeyLookupRequest]gomatrixserverlib.PublicKeyLookupResult,
) error {
// Run in a background context - we don't want to stop this work just
// because the caller gives up waiting.
ctx := context.Background()
request := api.InputPublicKeysRequest{
Keys: make(map[gomatrixserverlib.PublicKeyLookupRequest]gomatrixserverlib.PublicKeyLookupResult),
}
response := api.InputPublicKeysResponse{}
for req, res := range results {
request.Keys[req] = res
s.immutableCache.StoreServerKey(req, res)
}
return s.InputPublicKeys(ctx, &request, &response)
}
func (s *httpServerKeyInternalAPI) FetchKeys(
_ context.Context,
requests map[gomatrixserverlib.PublicKeyLookupRequest]gomatrixserverlib.Timestamp,
) (map[gomatrixserverlib.PublicKeyLookupRequest]gomatrixserverlib.PublicKeyLookupResult, error) {
// Run in a background context - we don't want to stop this work just
// because the caller gives up waiting.
ctx := context.Background()
result := make(map[gomatrixserverlib.PublicKeyLookupRequest]gomatrixserverlib.PublicKeyLookupResult)
request := api.QueryPublicKeysRequest{
Requests: make(map[gomatrixserverlib.PublicKeyLookupRequest]gomatrixserverlib.Timestamp),
}
response := api.QueryPublicKeysResponse{
Results: make(map[gomatrixserverlib.PublicKeyLookupRequest]gomatrixserverlib.PublicKeyLookupResult),
}
now := gomatrixserverlib.AsTimestamp(time.Now())
for req, ts := range requests {
if res, ok := s.immutableCache.GetServerKey(req); ok {
if now > res.ValidUntilTS && res.ExpiredTS == gomatrixserverlib.PublicKeyNotExpired {
continue
}
result[req] = res
continue
}
request.Requests[req] = ts
}
err := s.QueryPublicKeys(ctx, &request, &response)
if err != nil {
return nil, err
}
for req, res := range response.Results {
result[req] = res
s.immutableCache.StoreServerKey(req, res)
}
return result, nil
}
func (h *httpServerKeyInternalAPI) InputPublicKeys(
ctx context.Context,
request *api.InputPublicKeysRequest,
response *api.InputPublicKeysResponse,
) error {
span, ctx := opentracing.StartSpanFromContext(ctx, "InputPublicKey")
defer span.Finish()
apiURL := h.serverKeyAPIURL + ServerKeyInputPublicKeyPath
return internalHTTP.PostJSON(ctx, span, h.httpClient, apiURL, request, response)
}
func (h *httpServerKeyInternalAPI) QueryPublicKeys(
ctx context.Context,
request *api.QueryPublicKeysRequest,
response *api.QueryPublicKeysResponse,
) error {
span, ctx := opentracing.StartSpanFromContext(ctx, "QueryPublicKey")
defer span.Finish()
apiURL := h.serverKeyAPIURL + ServerKeyQueryPublicKeyPath
return internalHTTP.PostJSON(ctx, span, h.httpClient, apiURL, request, response)
}

View File

@ -1,4 +1,4 @@
package internal package inthttp
import ( import (
"encoding/json" "encoding/json"
@ -6,13 +6,14 @@ import (
"github.com/gorilla/mux" "github.com/gorilla/mux"
"github.com/matrix-org/dendrite/internal" "github.com/matrix-org/dendrite/internal"
"github.com/matrix-org/dendrite/internal/caching"
"github.com/matrix-org/dendrite/serverkeyapi/api" "github.com/matrix-org/dendrite/serverkeyapi/api"
"github.com/matrix-org/gomatrixserverlib" "github.com/matrix-org/gomatrixserverlib"
"github.com/matrix-org/util" "github.com/matrix-org/util"
) )
func (s *ServerKeyAPI) SetupHTTP(internalAPIMux *mux.Router) { func AddRoutes(s api.ServerKeyInternalAPI, internalAPIMux *mux.Router, cache caching.ImmutableCache) {
internalAPIMux.Handle(api.ServerKeyQueryPublicKeyPath, internalAPIMux.Handle(ServerKeyQueryPublicKeyPath,
internal.MakeInternalAPI("queryPublicKeys", func(req *http.Request) util.JSONResponse { internal.MakeInternalAPI("queryPublicKeys", func(req *http.Request) util.JSONResponse {
request := api.QueryPublicKeysRequest{} request := api.QueryPublicKeysRequest{}
response := api.QueryPublicKeysResponse{} response := api.QueryPublicKeysResponse{}
@ -27,7 +28,7 @@ func (s *ServerKeyAPI) SetupHTTP(internalAPIMux *mux.Router) {
return util.JSONResponse{Code: http.StatusOK, JSON: &response} return util.JSONResponse{Code: http.StatusOK, JSON: &response}
}), }),
) )
internalAPIMux.Handle(api.ServerKeyInputPublicKeyPath, internalAPIMux.Handle(ServerKeyInputPublicKeyPath,
internal.MakeInternalAPI("inputPublicKeys", func(req *http.Request) util.JSONResponse { internal.MakeInternalAPI("inputPublicKeys", func(req *http.Request) util.JSONResponse {
request := api.InputPublicKeysRequest{} request := api.InputPublicKeysRequest{}
response := api.InputPublicKeysResponse{} response := api.InputPublicKeysResponse{}
@ -37,7 +38,7 @@ func (s *ServerKeyAPI) SetupHTTP(internalAPIMux *mux.Router) {
store := make(map[gomatrixserverlib.PublicKeyLookupRequest]gomatrixserverlib.PublicKeyLookupResult) store := make(map[gomatrixserverlib.PublicKeyLookupRequest]gomatrixserverlib.PublicKeyLookupResult)
for req, res := range request.Keys { for req, res := range request.Keys {
store[req] = res store[req] = res
s.ImmutableCache.StoreServerKey(req, res) cache.StoreServerKey(req, res)
} }
if err := s.StoreKeys(req.Context(), store); err != nil { if err := s.StoreKeys(req.Context(), store); err != nil {
return util.ErrorResponse(err) return util.ErrorResponse(err)

View File

@ -7,6 +7,7 @@ import (
"github.com/matrix-org/dendrite/internal/basecomponent" "github.com/matrix-org/dendrite/internal/basecomponent"
"github.com/matrix-org/dendrite/serverkeyapi/api" "github.com/matrix-org/dendrite/serverkeyapi/api"
"github.com/matrix-org/dendrite/serverkeyapi/internal" "github.com/matrix-org/dendrite/serverkeyapi/internal"
"github.com/matrix-org/dendrite/serverkeyapi/inthttp"
"github.com/matrix-org/dendrite/serverkeyapi/storage" "github.com/matrix-org/dendrite/serverkeyapi/storage"
"github.com/matrix-org/dendrite/serverkeyapi/storage/cache" "github.com/matrix-org/dendrite/serverkeyapi/storage/cache"
"github.com/matrix-org/gomatrixserverlib" "github.com/matrix-org/gomatrixserverlib"
@ -34,8 +35,7 @@ func SetupServerKeyAPIComponent(
} }
internalAPI := internal.ServerKeyAPI{ internalAPI := internal.ServerKeyAPI{
ImmutableCache: base.ImmutableCache, FedClient: fedClient,
FedClient: fedClient,
OurKeyRing: gomatrixserverlib.KeyRing{ OurKeyRing: gomatrixserverlib.KeyRing{
KeyFetchers: []gomatrixserverlib.KeyFetcher{ KeyFetchers: []gomatrixserverlib.KeyFetcher{
&gomatrixserverlib.DirectKeyFetcher{ &gomatrixserverlib.DirectKeyFetcher{
@ -77,7 +77,7 @@ func SetupServerKeyAPIComponent(
}).Info("Enabled perspective key fetcher") }).Info("Enabled perspective key fetcher")
} }
internalAPI.SetupHTTP(base.InternalAPIMux) inthttp.AddRoutes(&internalAPI, base.InternalAPIMux, base.ImmutableCache)
return &internalAPI return &internalAPI
} }