Hopefully implement HTTP for server key API

This commit is contained in:
Neil Alexander 2020-05-20 11:01:32 +01:00
parent 45488487a2
commit 2f645cb6da
5 changed files with 108 additions and 14 deletions

View file

@ -0,0 +1,32 @@
// Copyright 2020 The Matrix.org Foundation C.I.C.
//
// 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 main
import (
"github.com/matrix-org/dendrite/common/basecomponent"
"github.com/matrix-org/dendrite/serverkeyapi"
)
func main() {
cfg := basecomponent.ParseFlags()
base := basecomponent.NewBaseDendrite(cfg, "ServerKeyAPI", true)
defer base.Close() // nolint: errcheck
federation := base.CreateFederationClient()
serverkeyapi.SetupServerKeyAPIComponent(base, federation)
base.SetupAndServeHTTP(string(base.Cfg.Bind.ServerKeyAPI), string(base.Cfg.Listen.ServerKeyAPI))
}

View file

@ -223,6 +223,7 @@ type Dendrite struct {
MediaAPI Address `yaml:"media_api"` MediaAPI Address `yaml:"media_api"`
ClientAPI Address `yaml:"client_api"` ClientAPI Address `yaml:"client_api"`
FederationAPI Address `yaml:"federation_api"` FederationAPI Address `yaml:"federation_api"`
ServerKeyAPI Address `yaml:"server_key_api"`
AppServiceAPI Address `yaml:"appservice_api"` AppServiceAPI Address `yaml:"appservice_api"`
SyncAPI Address `yaml:"sync_api"` SyncAPI Address `yaml:"sync_api"`
RoomServer Address `yaml:"room_server"` RoomServer Address `yaml:"room_server"`
@ -237,6 +238,7 @@ type Dendrite struct {
MediaAPI Address `yaml:"media_api"` MediaAPI Address `yaml:"media_api"`
ClientAPI Address `yaml:"client_api"` ClientAPI Address `yaml:"client_api"`
FederationAPI Address `yaml:"federation_api"` FederationAPI Address `yaml:"federation_api"`
ServerKeyAPI Address `yaml:"server_key_api"`
AppServiceAPI Address `yaml:"appservice_api"` AppServiceAPI Address `yaml:"appservice_api"`
SyncAPI Address `yaml:"sync_api"` SyncAPI Address `yaml:"sync_api"`
RoomServer Address `yaml:"room_server"` RoomServer Address `yaml:"room_server"`

View file

@ -4,6 +4,7 @@ import (
"context" "context"
commonHTTP "github.com/matrix-org/dendrite/common/http" commonHTTP "github.com/matrix-org/dendrite/common/http"
"github.com/matrix-org/gomatrixserverlib"
"github.com/opentracing/opentracing-go" "github.com/opentracing/opentracing-go"
) )
@ -17,6 +18,7 @@ const (
) )
type InputPublicKeysRequest struct { type InputPublicKeysRequest struct {
Keys map[gomatrixserverlib.ServerName]map[gomatrixserverlib.KeyID]gomatrixserverlib.PublicKeyLookupResult `json:"keys"`
} }
type InputPublicKeysResponse struct { type InputPublicKeysResponse struct {
@ -35,9 +37,11 @@ func (h *httpServerKeyInternalAPI) InputPublicKeys(
} }
type QueryPublicKeysRequest struct { type QueryPublicKeysRequest struct {
Requests map[gomatrixserverlib.ServerName]map[gomatrixserverlib.KeyID]gomatrixserverlib.Timestamp `json:"requests"`
} }
type QueryPublicKeysResponse struct { type QueryPublicKeysResponse struct {
Results map[gomatrixserverlib.ServerName]map[gomatrixserverlib.KeyID]gomatrixserverlib.PublicKeyLookupResult `json:"results"`
} }
func (h *httpServerKeyInternalAPI) QueryPublicKeys( func (h *httpServerKeyInternalAPI) QueryPublicKeys(

View file

@ -14,12 +14,42 @@ func (s *httpServerKeyInternalAPI) StoreKeys(
ctx context.Context, ctx context.Context,
results map[gomatrixserverlib.PublicKeyLookupRequest]gomatrixserverlib.PublicKeyLookupResult, results map[gomatrixserverlib.PublicKeyLookupRequest]gomatrixserverlib.PublicKeyLookupResult,
) error { ) error {
return nil request := InputPublicKeysRequest{}
response := InputPublicKeysResponse{}
for req, res := range results {
if _, ok := request.Keys[req.ServerName]; !ok {
request.Keys[req.ServerName] = map[gomatrixserverlib.KeyID]gomatrixserverlib.PublicKeyLookupResult{}
}
request.Keys[req.ServerName][req.KeyID] = res
}
return s.InputPublicKeys(ctx, &request, &response)
} }
func (s *httpServerKeyInternalAPI) FetchKeys( func (s *httpServerKeyInternalAPI) FetchKeys(
ctx context.Context, ctx context.Context,
requests map[gomatrixserverlib.PublicKeyLookupRequest]gomatrixserverlib.Timestamp, requests map[gomatrixserverlib.PublicKeyLookupRequest]gomatrixserverlib.Timestamp,
) (map[gomatrixserverlib.PublicKeyLookupRequest]gomatrixserverlib.PublicKeyLookupResult, error) { ) (map[gomatrixserverlib.PublicKeyLookupRequest]gomatrixserverlib.PublicKeyLookupResult, error) {
return nil, nil request := QueryPublicKeysRequest{}
response := QueryPublicKeysResponse{}
for req, ts := range requests {
if _, ok := request.Requests[req.ServerName]; !ok {
request.Requests[req.ServerName] = map[gomatrixserverlib.KeyID]gomatrixserverlib.Timestamp{}
}
request.Requests[req.ServerName][req.KeyID] = ts
}
err := s.QueryPublicKeys(ctx, &request, &response)
if err != nil {
return nil, err
}
result := map[gomatrixserverlib.PublicKeyLookupRequest]gomatrixserverlib.PublicKeyLookupResult{}
for serverName, byServerName := range response.Results {
for keyID, res := range byServerName {
key := gomatrixserverlib.PublicKeyLookupRequest{
ServerName: serverName,
KeyID: keyID,
}
result[key] = res
}
}
return result, nil
} }

View file

@ -6,37 +6,63 @@ import (
"github.com/matrix-org/dendrite/common" "github.com/matrix-org/dendrite/common"
"github.com/matrix-org/dendrite/serverkeyapi/api" "github.com/matrix-org/dendrite/serverkeyapi/api"
"github.com/matrix-org/gomatrixserverlib"
"github.com/matrix-org/util" "github.com/matrix-org/util"
) )
func (s *ServerKeyAPI) SetupHTTP(servMux *http.ServeMux) { func (s *ServerKeyAPI) SetupHTTP(servMux *http.ServeMux) {
servMux.Handle(api.ServerKeyQueryPublicKeyPath, servMux.Handle(api.ServerKeyQueryPublicKeyPath,
common.MakeInternalAPI("queryPublicKeys", func(req *http.Request) util.JSONResponse { common.MakeInternalAPI("queryPublicKeys", func(req *http.Request) util.JSONResponse {
var request api.QueryPublicKeysRequest request := api.QueryPublicKeysRequest{}
var response api.QueryPublicKeysResponse response := api.QueryPublicKeysResponse{
Results: map[gomatrixserverlib.ServerName]map[gomatrixserverlib.KeyID]gomatrixserverlib.PublicKeyLookupResult{},
}
if err := json.NewDecoder(req.Body).Decode(&request); err != nil { if err := json.NewDecoder(req.Body).Decode(&request); err != nil {
return util.MessageResponse(http.StatusBadRequest, err.Error()) return util.MessageResponse(http.StatusBadRequest, err.Error())
} }
/* lookup := make(map[gomatrixserverlib.PublicKeyLookupRequest]gomatrixserverlib.Timestamp)
if err := s.DB.FetchKeys(); err != nil { for serverName, byServerName := range request.Requests {
for keyID, timestamp := range byServerName {
key := gomatrixserverlib.PublicKeyLookupRequest{
ServerName: serverName,
KeyID: keyID,
}
lookup[key] = timestamp
}
}
keys, err := s.DB.FetchKeys(req.Context(), lookup)
if err != nil {
return util.ErrorResponse(err) return util.ErrorResponse(err)
} }
*/ for req, res := range keys {
if _, ok := response.Results[req.ServerName]; !ok {
response.Results[req.ServerName] = map[gomatrixserverlib.KeyID]gomatrixserverlib.PublicKeyLookupResult{}
}
response.Results[req.ServerName][req.KeyID] = res
}
return util.JSONResponse{Code: http.StatusOK, JSON: &response} return util.JSONResponse{Code: http.StatusOK, JSON: &response}
}), }),
) )
servMux.Handle(api.ServerKeyInputPublicKeyPath, servMux.Handle(api.ServerKeyInputPublicKeyPath,
common.MakeInternalAPI("inputPublicKeys", func(req *http.Request) util.JSONResponse { common.MakeInternalAPI("inputPublicKeys", func(req *http.Request) util.JSONResponse {
var request api.InputPublicKeysRequest request := api.InputPublicKeysRequest{}
var response api.InputPublicKeysResponse response := api.InputPublicKeysResponse{}
if err := json.NewDecoder(req.Body).Decode(&request); err != nil { if err := json.NewDecoder(req.Body).Decode(&request); err != nil {
return util.MessageResponse(http.StatusBadRequest, err.Error()) return util.MessageResponse(http.StatusBadRequest, err.Error())
} }
/* store := make(map[gomatrixserverlib.PublicKeyLookupRequest]gomatrixserverlib.PublicKeyLookupResult)
if err := s.DB.FetchKeys(); err != nil { for serverName, byServerName := range request.Keys {
for keyID, keyResult := range byServerName {
key := gomatrixserverlib.PublicKeyLookupRequest{
ServerName: serverName,
KeyID: keyID,
}
store[key] = keyResult
}
}
if err := s.DB.StoreKeys(req.Context(), store); err != nil {
return util.ErrorResponse(err) return util.ErrorResponse(err)
} }
*/
return util.JSONResponse{Code: http.StatusOK, JSON: &response} return util.JSONResponse{Code: http.StatusOK, JSON: &response}
}), }),
) )