2020-05-01 14:01:50 +02:00
|
|
|
package internal
|
2019-08-22 13:47:52 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-07-15 18:45:37 +02:00
|
|
|
"fmt"
|
|
|
|
"time"
|
2019-08-22 13:47:52 +02:00
|
|
|
|
|
|
|
"github.com/matrix-org/dendrite/federationsender/api"
|
2021-07-15 18:45:37 +02:00
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
|
|
|
"github.com/matrix-org/util"
|
2019-08-22 13:47:52 +02:00
|
|
|
)
|
|
|
|
|
2020-04-29 12:34:31 +02:00
|
|
|
// QueryJoinedHostServerNamesInRoom implements api.FederationSenderInternalAPI
|
|
|
|
func (f *FederationSenderInternalAPI) QueryJoinedHostServerNamesInRoom(
|
2019-08-22 13:47:52 +02:00
|
|
|
ctx context.Context,
|
|
|
|
request *api.QueryJoinedHostServerNamesInRoomRequest,
|
|
|
|
response *api.QueryJoinedHostServerNamesInRoomResponse,
|
|
|
|
) (err error) {
|
2020-11-19 12:34:59 +01:00
|
|
|
joinedHosts, err := f.db.GetJoinedHostsForRooms(ctx, []string{request.RoomID})
|
2019-08-22 13:47:52 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2020-11-19 12:34:59 +01:00
|
|
|
response.ServerNames = joinedHosts
|
2019-08-22 13:47:52 +02:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
2021-07-15 18:45:37 +02:00
|
|
|
|
2021-07-16 12:35:42 +02:00
|
|
|
func (a *FederationSenderInternalAPI) fetchServerKeysDirectly(ctx context.Context, serverName gomatrixserverlib.ServerName) (*gomatrixserverlib.ServerKeys, error) {
|
2021-07-15 18:45:37 +02:00
|
|
|
ctx, cancel := context.WithTimeout(ctx, time.Second*30)
|
|
|
|
defer cancel()
|
2021-07-16 12:35:42 +02:00
|
|
|
ires, err := a.doRequest(serverName, func() (interface{}, error) {
|
|
|
|
return a.federation.GetServerKeys(ctx, serverName)
|
2021-07-15 18:45:37 +02:00
|
|
|
})
|
|
|
|
if err != nil {
|
2021-07-16 12:35:42 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
sks := ires.(gomatrixserverlib.ServerKeys)
|
|
|
|
return &sks, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *FederationSenderInternalAPI) fetchServerKeysFromCache(
|
|
|
|
ctx context.Context, req *api.QueryServerKeysRequest,
|
|
|
|
) ([]gomatrixserverlib.ServerKeys, error) {
|
|
|
|
var results []gomatrixserverlib.ServerKeys
|
|
|
|
for keyID, criteria := range req.KeyIDToCriteria {
|
|
|
|
serverKeysResponses, _ := a.db.GetNotaryKeys(ctx, req.ServerName, []gomatrixserverlib.KeyID{keyID})
|
|
|
|
if len(serverKeysResponses) == 0 {
|
|
|
|
return nil, fmt.Errorf("failed to find server key response for key ID %s", keyID)
|
|
|
|
}
|
|
|
|
// we should only get 1 result as we only gave 1 key ID
|
|
|
|
sk := serverKeysResponses[0]
|
|
|
|
util.GetLogger(ctx).Infof("fetchServerKeysFromCache: minvalid:%v keys: %+v", criteria.MinimumValidUntilTS, sk)
|
|
|
|
if criteria.MinimumValidUntilTS != 0 {
|
|
|
|
// check if it's still valid. if they have the same value that's also valid
|
|
|
|
if sk.ValidUntilTS < criteria.MinimumValidUntilTS {
|
|
|
|
return nil, fmt.Errorf(
|
|
|
|
"found server response for key ID %s but it is no longer valid, min: %v valid_until: %v",
|
|
|
|
keyID, criteria.MinimumValidUntilTS, sk.ValidUntilTS,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
results = append(results, sk)
|
|
|
|
}
|
|
|
|
return results, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *FederationSenderInternalAPI) QueryServerKeys(
|
|
|
|
ctx context.Context, req *api.QueryServerKeysRequest, res *api.QueryServerKeysResponse,
|
|
|
|
) error {
|
|
|
|
// attempt to satisfy the entire request from the cache first
|
|
|
|
results, err := a.fetchServerKeysFromCache(ctx, req)
|
|
|
|
if err == nil {
|
|
|
|
// satisfied entirely from cache, return it
|
|
|
|
res.ServerKeys = results
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
util.GetLogger(ctx).WithField("server", req.ServerName).WithError(err).Warn("notary: failed to satisfy keys request entirely from cache, hitting direct")
|
|
|
|
|
|
|
|
serverKeys, err := a.fetchServerKeysDirectly(ctx, req.ServerName)
|
|
|
|
if err != nil {
|
|
|
|
// try to load as much as we can from the cache in a best effort basis
|
|
|
|
util.GetLogger(ctx).WithField("server", req.ServerName).WithError(err).Warn("notary: failed to ask server for keys, returning best effort keys")
|
|
|
|
serverKeysResponses, dbErr := a.db.GetNotaryKeys(ctx, req.ServerName, req.KeyIDs())
|
2021-07-15 18:45:37 +02:00
|
|
|
if dbErr != nil {
|
2021-07-16 12:35:42 +02:00
|
|
|
return fmt.Errorf("notary: server returned %s, and db returned %s", err, dbErr)
|
2021-07-15 18:45:37 +02:00
|
|
|
}
|
|
|
|
res.ServerKeys = serverKeysResponses
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
// cache it!
|
2021-07-16 12:35:42 +02:00
|
|
|
if err = a.db.UpdateNotaryKeys(context.Background(), req.ServerName, *serverKeys); err != nil {
|
2021-07-15 18:45:37 +02:00
|
|
|
// non-fatal, still return the response
|
|
|
|
util.GetLogger(ctx).WithError(err).Warn("failed to UpdateNotaryKeys")
|
|
|
|
}
|
2021-07-16 12:35:42 +02:00
|
|
|
res.ServerKeys = []gomatrixserverlib.ServerKeys{*serverKeys}
|
2021-07-15 18:45:37 +02:00
|
|
|
return nil
|
|
|
|
}
|