Add key validity fetching to server key API (#1094)

* Add key validity checks

* Store fetched keys

* Don't double-cache key results

* Perform server key API operations using new context

* Revert "Perform server key API operations using new context"

This reverts commit 02172223f5cb7850b0852c6ed6836ad82508ea76.

* Perform server key API operations using new context
This commit is contained in:
Neil Alexander 2020-06-04 15:40:23 +01:00 committed by GitHub
parent f4c676ccdd
commit d785ad82b9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 31 additions and 18 deletions

View file

@ -4,6 +4,7 @@ import (
"context"
"errors"
"net/http"
"time"
"github.com/matrix-org/dendrite/internal/caching"
"github.com/matrix-org/gomatrixserverlib"
@ -69,9 +70,12 @@ func (s *httpServerKeyInternalAPI) FetcherName() string {
}
func (s *httpServerKeyInternalAPI) StoreKeys(
ctx context.Context,
_ 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),
}
@ -84,9 +88,12 @@ func (s *httpServerKeyInternalAPI) StoreKeys(
}
func (s *httpServerKeyInternalAPI) FetchKeys(
ctx context.Context,
_ 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),
@ -94,8 +101,12 @@ func (s *httpServerKeyInternalAPI) FetchKeys(
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
}