2021-11-24 11:45:23 +01:00
|
|
|
package federationapi
|
2020-06-16 14:11:20 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"crypto/ed25519"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2022-08-05 11:26:59 +02:00
|
|
|
"io"
|
2020-06-16 14:11:20 +02:00
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2022-09-22 15:53:15 +02:00
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
|
|
|
|
2021-11-24 11:45:23 +01:00
|
|
|
"github.com/matrix-org/dendrite/federationapi/api"
|
2020-06-16 14:11:20 +02:00
|
|
|
"github.com/matrix-org/dendrite/federationapi/routing"
|
|
|
|
"github.com/matrix-org/dendrite/internal/caching"
|
2021-11-24 11:45:23 +01:00
|
|
|
"github.com/matrix-org/dendrite/setup/base"
|
2020-12-02 18:41:00 +01:00
|
|
|
"github.com/matrix-org/dendrite/setup/config"
|
2020-06-16 14:11:20 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type server struct {
|
|
|
|
name gomatrixserverlib.ServerName // server name
|
|
|
|
validity time.Duration // key validity duration from now
|
2021-11-24 11:45:23 +01:00
|
|
|
config *config.FederationAPI // skeleton config, from TestMain
|
2020-06-16 14:11:20 +02:00
|
|
|
fedclient *gomatrixserverlib.FederationClient // uses MockRoundTripper
|
|
|
|
cache *caching.Caches // server-specific cache
|
2021-11-24 11:45:23 +01:00
|
|
|
api api.FederationInternalAPI // server-specific server key API
|
2020-06-16 14:11:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *server) renew() {
|
|
|
|
// This updates the validity period to be an hour in the
|
|
|
|
// future, which is particularly useful in server A and
|
|
|
|
// server C's cases which have validity either as now or
|
|
|
|
// in the past.
|
|
|
|
s.validity = time.Hour
|
|
|
|
s.config.Matrix.KeyValidityPeriod = s.validity
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
serverKeyID = gomatrixserverlib.KeyID("ed25519:auto")
|
|
|
|
serverA = &server{name: "a.com", validity: time.Duration(0)} // expires now
|
|
|
|
serverB = &server{name: "b.com", validity: time.Hour} // expires in an hour
|
|
|
|
serverC = &server{name: "c.com", validity: -time.Hour} // expired an hour ago
|
|
|
|
)
|
|
|
|
|
|
|
|
var servers = map[string]*server{
|
|
|
|
"a.com": serverA,
|
|
|
|
"b.com": serverB,
|
|
|
|
"c.com": serverC,
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMain(m *testing.M) {
|
|
|
|
// Set up the server key API for each "server" that we
|
|
|
|
// will use in our tests.
|
2022-05-23 15:54:43 +02:00
|
|
|
os.Exit(func() int {
|
|
|
|
for _, s := range servers {
|
|
|
|
// Generate a new key.
|
|
|
|
_, testPriv, err := ed25519.GenerateKey(nil)
|
|
|
|
if err != nil {
|
|
|
|
panic("can't generate identity key: " + err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a new cache but don't enable prometheus!
|
2022-07-11 15:31:31 +02:00
|
|
|
s.cache = caching.NewRistrettoCache(8*1024*1024, time.Hour, false)
|
2022-05-23 15:54:43 +02:00
|
|
|
|
|
|
|
// Create a temporary directory for JetStream.
|
2022-08-05 11:26:59 +02:00
|
|
|
d, err := os.MkdirTemp("./", "jetstream*")
|
2022-05-23 15:54:43 +02:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(d)
|
|
|
|
|
|
|
|
// Draw up just enough Dendrite config for the server key
|
|
|
|
// API to work.
|
|
|
|
cfg := &config.Dendrite{}
|
2022-09-01 15:15:41 +02:00
|
|
|
cfg.Defaults(config.DefaultOpts{
|
|
|
|
Generate: true,
|
|
|
|
Monolithic: true,
|
|
|
|
})
|
2022-05-23 15:54:43 +02:00
|
|
|
cfg.Global.ServerName = gomatrixserverlib.ServerName(s.name)
|
|
|
|
cfg.Global.PrivateKey = testPriv
|
|
|
|
cfg.Global.JetStream.InMemory = true
|
|
|
|
cfg.Global.JetStream.TopicPrefix = string(s.name[:1])
|
|
|
|
cfg.Global.JetStream.StoragePath = config.Path(d)
|
|
|
|
cfg.Global.KeyID = serverKeyID
|
|
|
|
cfg.Global.KeyValidityPeriod = s.validity
|
2022-10-26 13:59:19 +02:00
|
|
|
cfg.FederationAPI.KeyPerspectives = nil
|
2022-09-22 15:53:15 +02:00
|
|
|
f, err := os.CreateTemp(d, "federation_keys_test*.db")
|
|
|
|
if err != nil {
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
cfg.FederationAPI.Database.ConnectionString = config.DataSource("file:" + f.Name())
|
2022-05-23 15:54:43 +02:00
|
|
|
s.config = &cfg.FederationAPI
|
|
|
|
|
|
|
|
// Create a transport which redirects federation requests to
|
|
|
|
// the mock round tripper. Since we're not *really* listening for
|
|
|
|
// federation requests then this will return the key instead.
|
|
|
|
transport := &http.Transport{}
|
|
|
|
transport.RegisterProtocol("matrix", &MockRoundTripper{})
|
|
|
|
|
|
|
|
// Create the federation client.
|
|
|
|
s.fedclient = gomatrixserverlib.NewFederationClient(
|
2022-11-15 16:05:23 +01:00
|
|
|
s.config.Matrix.SigningIdentities(),
|
2022-05-23 15:54:43 +02:00
|
|
|
gomatrixserverlib.WithTransport(transport),
|
|
|
|
)
|
|
|
|
|
|
|
|
// Finally, build the server key APIs.
|
|
|
|
sbase := base.NewBaseDendrite(cfg, "Monolith", base.DisableMetrics)
|
|
|
|
s.api = NewInternalAPI(sbase, s.fedclient, nil, s.cache, nil, true)
|
2020-06-16 14:11:20 +02:00
|
|
|
}
|
|
|
|
|
2022-05-23 15:54:43 +02:00
|
|
|
// Now that we have built our server key APIs, start the
|
|
|
|
// rest of the tests.
|
|
|
|
return m.Run()
|
|
|
|
}())
|
2020-06-16 14:11:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type MockRoundTripper struct{}
|
|
|
|
|
|
|
|
func (m *MockRoundTripper) RoundTrip(req *http.Request) (res *http.Response, err error) {
|
|
|
|
// Check if the request is looking for keys from a server that
|
|
|
|
// we know about in the test. The only reason this should go wrong
|
|
|
|
// is if the test is broken.
|
|
|
|
s, ok := servers[req.Host]
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("server not known: %s", req.Host)
|
|
|
|
}
|
|
|
|
|
|
|
|
// We're intercepting /matrix/key/v2/server requests here, so check
|
|
|
|
// that the URL supplied in the request is for that.
|
|
|
|
if req.URL.Path != "/_matrix/key/v2/server" {
|
|
|
|
return nil, fmt.Errorf("unexpected request path: %s", req.URL.Path)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the keys and JSON-ify them.
|
2022-11-11 17:41:37 +01:00
|
|
|
keys := routing.LocalKeys(s.config, gomatrixserverlib.ServerName(req.Host))
|
2020-06-16 14:11:20 +02:00
|
|
|
body, err := json.MarshalIndent(keys.JSON, "", " ")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// And respond.
|
|
|
|
res = &http.Response{
|
|
|
|
StatusCode: 200,
|
2022-08-05 11:26:59 +02:00
|
|
|
Body: io.NopCloser(bytes.NewReader(body)),
|
2020-06-16 14:11:20 +02:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestServersRequestOwnKeys(t *testing.T) {
|
|
|
|
// Each server will request its own keys. There's no reason
|
|
|
|
// for this to fail as each server should know its own keys.
|
|
|
|
|
|
|
|
for name, s := range servers {
|
|
|
|
req := gomatrixserverlib.PublicKeyLookupRequest{
|
|
|
|
ServerName: s.name,
|
|
|
|
KeyID: serverKeyID,
|
|
|
|
}
|
|
|
|
res, err := s.api.FetchKeys(
|
|
|
|
context.Background(),
|
|
|
|
map[gomatrixserverlib.PublicKeyLookupRequest]gomatrixserverlib.Timestamp{
|
|
|
|
req: gomatrixserverlib.AsTimestamp(time.Now()),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("server could not fetch own key: %s", err)
|
|
|
|
}
|
|
|
|
if _, ok := res[req]; !ok {
|
|
|
|
t.Fatalf("server didn't return its own key in the results")
|
|
|
|
}
|
|
|
|
t.Logf("%s's key expires at %s\n", name, res[req].ValidUntilTS.Time())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRenewalBehaviour(t *testing.T) {
|
|
|
|
// Server A will request Server C's key but their validity period
|
|
|
|
// is an hour in the past. We'll retrieve the key as, even though it's
|
|
|
|
// past its validity, it will be able to verify past events.
|
|
|
|
|
|
|
|
req := gomatrixserverlib.PublicKeyLookupRequest{
|
|
|
|
ServerName: serverC.name,
|
|
|
|
KeyID: serverKeyID,
|
|
|
|
}
|
|
|
|
|
|
|
|
res, err := serverA.api.FetchKeys(
|
|
|
|
context.Background(),
|
|
|
|
map[gomatrixserverlib.PublicKeyLookupRequest]gomatrixserverlib.Timestamp{
|
|
|
|
req: gomatrixserverlib.AsTimestamp(time.Now()),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("server A failed to retrieve server C key: %s", err)
|
|
|
|
}
|
|
|
|
if len(res) != 1 {
|
|
|
|
t.Fatalf("server C should have returned one key but instead returned %d keys", len(res))
|
|
|
|
}
|
|
|
|
if _, ok := res[req]; !ok {
|
|
|
|
t.Fatalf("server C isn't included in the key fetch response")
|
|
|
|
}
|
|
|
|
|
2022-07-11 15:31:31 +02:00
|
|
|
originalValidity := res[req].ValidUntilTS
|
2020-06-16 14:11:20 +02:00
|
|
|
|
|
|
|
// We're now going to kick server C into renewing its key. Since we're
|
|
|
|
// happy at this point that the key that we already have is from the past
|
|
|
|
// then repeating a key fetch should cause us to try and renew the key.
|
|
|
|
// If so, then the new key will end up in our cache.
|
|
|
|
serverC.renew()
|
|
|
|
|
|
|
|
res, err = serverA.api.FetchKeys(
|
|
|
|
context.Background(),
|
|
|
|
map[gomatrixserverlib.PublicKeyLookupRequest]gomatrixserverlib.Timestamp{
|
|
|
|
req: gomatrixserverlib.AsTimestamp(time.Now()),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("server A failed to retrieve server C key: %s", err)
|
|
|
|
}
|
|
|
|
if len(res) != 1 {
|
|
|
|
t.Fatalf("server C should have returned one key but instead returned %d keys", len(res))
|
|
|
|
}
|
2022-07-11 15:31:31 +02:00
|
|
|
if _, ok := res[req]; !ok {
|
2020-06-16 14:11:20 +02:00
|
|
|
t.Fatalf("server C isn't included in the key fetch response")
|
|
|
|
}
|
|
|
|
|
2022-07-11 15:31:31 +02:00
|
|
|
currentValidity := res[req].ValidUntilTS
|
2020-06-16 14:11:20 +02:00
|
|
|
|
2022-07-11 15:31:31 +02:00
|
|
|
if originalValidity == currentValidity {
|
|
|
|
t.Fatalf("server C key should have renewed but didn't")
|
2020-06-16 14:11:20 +02:00
|
|
|
}
|
|
|
|
}
|