diff options
author | Neil Alexander <neilalexander@users.noreply.github.com> | 2020-01-25 14:12:52 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-01-25 14:12:52 +0000 |
commit | 2cb7c91c5d55e39eda0eb1668292c0973b7fa65f (patch) | |
tree | 491371c81c307e2c9152f1e6a6e7ee4e6e86c6fe /common/keydb/postgres | |
parent | 37d117f2b7db9d04c0d6ee0e71baaad8d6680ddd (diff) |
Store our own keys in the keydb (#853)
* Store our own keys in the keydb
The DirectKeyFetcher makes the assumption that you can always reach the key/v2/server endpoint of any server, including our own. We previously haven't bothered to store our own keys in the keydb so this would mean we end up making key requests to ourselves.
In the libp2p world as an example, self-dialling is not possible, therefore this would render it impossible to get our own keys.
This commit adds our own keys into the keydb so that we don't create unnecessarily (and maybe impossible) requests.
* Use golang.org/x/crypto/ed25519 instead of crypto/ed25519 for pre-Go 1.13
Diffstat (limited to 'common/keydb/postgres')
-rw-r--r-- | common/keydb/postgres/keydb.go | 32 |
1 files changed, 31 insertions, 1 deletions
diff --git a/common/keydb/postgres/keydb.go b/common/keydb/postgres/keydb.go index bf0ff69c..2dd8c866 100644 --- a/common/keydb/postgres/keydb.go +++ b/common/keydb/postgres/keydb.go @@ -18,6 +18,9 @@ package postgres import ( "context" "database/sql" + "math" + + "golang.org/x/crypto/ed25519" "github.com/matrix-org/gomatrixserverlib" ) @@ -32,7 +35,12 @@ type Database struct { // It creates the necessary tables if they don't already exist. // It prepares all the SQL statements that it will use. // Returns an error if there was a problem talking to the database. -func NewDatabase(dataSourceName string) (*Database, error) { +func NewDatabase( + dataSourceName string, + serverName gomatrixserverlib.ServerName, + serverKey ed25519.PublicKey, + serverKeyID gomatrixserverlib.KeyID, +) (*Database, error) { db, err := sql.Open("postgres", dataSourceName) if err != nil { return nil, err @@ -42,6 +50,28 @@ func NewDatabase(dataSourceName string) (*Database, error) { if err != nil { return nil, err } + // Store our own keys so that we don't end up making HTTP requests to find our + // own keys + index := gomatrixserverlib.PublicKeyLookupRequest{ + ServerName: serverName, + KeyID: serverKeyID, + } + value := gomatrixserverlib.PublicKeyLookupResult{ + VerifyKey: gomatrixserverlib.VerifyKey{ + Key: gomatrixserverlib.Base64String(serverKey), + }, + ValidUntilTS: math.MaxUint64 >> 1, + ExpiredTS: gomatrixserverlib.PublicKeyNotExpired, + } + err = d.StoreKeys( + context.Background(), + map[gomatrixserverlib.PublicKeyLookupRequest]gomatrixserverlib.PublicKeyLookupResult{ + index: value, + }, + ) + if err != nil { + return nil, err + } return d, nil } |