Skip to content

Commit 627259c

Browse files
committed
Discovery: update client copy of Discovery Services
1 parent 96884a9 commit 627259c

File tree

10 files changed

+256
-189
lines changed

10 files changed

+256
-189
lines changed

README.rst

Lines changed: 76 additions & 75 deletions
Large diffs are not rendered by default.

discovery/client.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,15 @@ type clientUpdater struct {
1414
services map[string]ServiceDefinition
1515
store *sqlStore
1616
client client.HTTPClient
17+
verifier registrationVerifier
1718
}
1819

19-
func newClientUpdater(services map[string]ServiceDefinition, store *sqlStore, client client.HTTPClient) *clientUpdater {
20+
func newClientUpdater(services map[string]ServiceDefinition, store *sqlStore, verifier registrationVerifier, client client.HTTPClient) *clientUpdater {
2021
return &clientUpdater{
2122
services: services,
2223
store: store,
2324
client: client,
25+
verifier: verifier,
2426
}
2527
}
2628

@@ -67,8 +69,12 @@ func (u *clientUpdater) updateService(ctx context.Context, service ServiceDefini
6769
newTagStr := new(Tag)
6870
*newTagStr = Tag(*newTag)
6971
for _, presentation := range presentations {
72+
if err := u.verifier.verifyRegistration(service, presentation); err != nil {
73+
log.Logger().WithError(err).Warnf("Presentation verification failed, not adding it (service=%s, id=%s)", service.ID, presentation.ID)
74+
continue
75+
}
7076
if err := u.store.add(service.ID, presentation, newTagStr); err != nil {
71-
return fmt.Errorf("failed to store presentation (id=%s): %w", presentation.ID, err)
77+
return fmt.Errorf("failed to store presentation (service=%s, id=%s): %w", service.ID, presentation.ID, err)
7278
}
7379
}
7480
return nil

discovery/cmd/cmd.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,7 @@ func FlagSet() *pflag.FlagSet {
3333
flagSet.StringSlice("discovery.server.definition_ids", defs.Server.DefinitionIDs,
3434
"IDs of the Discovery Service Definitions for which to act as server. "+
3535
"If an ID does not map to a loaded service definition, the node will fail to start.")
36+
flagSet.Duration("discovery.client.update_interval", defs.Client.UpdateInterval, "How often to check for Discovery Services updates, "+
37+
"specified as Golang duration (e.g. 1m, 1h30m).")
3638
return flagSet
3739
}

discovery/config.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,12 @@
1818

1919
package discovery
2020

21+
import "time"
22+
2123
// Config holds the config of the module
2224
type Config struct {
2325
Server ServerConfig `koanf:"server"`
26+
Client ClientConfig `koanf:"client"`
2427
Definitions ServiceDefinitionsConfig `koanf:"definitions"`
2528
}
2629

@@ -35,10 +38,19 @@ type ServerConfig struct {
3538
DefinitionIDs []string `koanf:"definition_ids"`
3639
}
3740

41+
// ClientConfig holds the config for the client
42+
type ClientConfig struct {
43+
// UpdateInterval specifies how often the client should update the Discovery Services.
44+
UpdateInterval time.Duration `koanf:"update_interval"`
45+
}
46+
3847
// DefaultConfig returns the default configuration.
3948
func DefaultConfig() Config {
4049
return Config{
4150
Server: ServerConfig{},
51+
Client: ClientConfig{
52+
UpdateInterval: 1 * time.Minute,
53+
},
4254
}
4355
}
4456

discovery/interface.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,3 +108,7 @@ type SearchResult struct {
108108
// It only includes constraint fields that have an ID.
109109
Fields map[string]interface{} `json:"fields"`
110110
}
111+
112+
type registrationVerifier interface {
113+
verifyRegistration(definition ServiceDefinition, presentation vc.VerifiablePresentation) error
114+
}

discovery/mock.go

Lines changed: 37 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

discovery/module.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -122,11 +122,11 @@ func (m *Module) Start() error {
122122
if err != nil {
123123
return err
124124
}
125-
m.clientUpdater = newClientUpdater(m.serverDefinitions, m.store, m.httpClient)
125+
m.clientUpdater = newClientUpdater(m.serverDefinitions, m.store, m, m.httpClient)
126126
m.routines.Add(1)
127127
go func() {
128128
defer m.routines.Done()
129-
m.clientUpdater.update(m.ctx, 10*time.Second)
129+
m.clientUpdater.update(m.ctx, m.config.Client.UpdateInterval)
130130
}()
131131
return nil
132132
}
@@ -150,11 +150,13 @@ func (m *Module) Add(serviceID string, presentation vc.VerifiablePresentation) e
150150
if !isServer {
151151
return ErrServerModeDisabled
152152
}
153-
return m.add(definition, presentation)
153+
if err := m.verifyRegistration(definition, presentation); err != nil {
154+
return err
155+
}
156+
return m.store.add(definition.ID, presentation, nil)
154157
}
155158

156-
// add validates the presentation and adds it to the store, if all checks pass.
157-
func (m *Module) add(definition ServiceDefinition, presentation vc.VerifiablePresentation) error {
159+
func (m *Module) verifyRegistration(definition ServiceDefinition, presentation vc.VerifiablePresentation) error {
158160
// First, simple sanity checks
159161
if presentation.Format() != vc.JWTPresentationProofFormat {
160162
return errors.Join(ErrInvalidPresentation, errUnsupportedPresentationFormat)
@@ -200,7 +202,7 @@ func (m *Module) add(definition ServiceDefinition, presentation vc.VerifiablePre
200202
if err != nil {
201203
return errors.Join(ErrInvalidPresentation, fmt.Errorf("presentation verification failed: %w", err))
202204
}
203-
return m.store.add(definition.ID, presentation, nil)
205+
return nil
204206
}
205207

206208
func (m *Module) validateRegistration(definition ServiceDefinition, presentation vc.VerifiablePresentation) error {

docs/pages/deployment/cli-reference.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ The following options apply to the server commands below:
2929
--crypto.vault.timeout duration Timeout of client calls to Vault, in Golang time.Duration string format (e.g. 1s). (default 5s)
3030
--crypto.vault.token string The Vault token. If set it overwrites the VAULT_TOKEN env var.
3131
--datadir string Directory where the node stores its files. (default "./data")
32+
--discovery.client.update_interval duration How often to check for Discovery Services updates, specified as Golang duration (e.g. 1m, 1h30m). (default 1m0s)
3233
--discovery.definitions.directory string Directory to load Discovery Service Definitions from. If not set, the discovery service will be disabled. If the directory contains JSON files that can't be parsed as service definition, the node will fail to start.
3334
--discovery.server.definition_ids strings IDs of the Discovery Service Definitions for which to act as server. If an ID does not map to a loaded service definition, the node will fail to start.
3435
--events.nats.hostname string Hostname for the NATS server (default "0.0.0.0")
@@ -45,7 +46,7 @@ The following options apply to the server commands below:
4546
--http.default.log string What to log about HTTP requests. Options are 'nothing', 'metadata' (log request method, URI, IP and response code), and 'metadata-and-body' (log the request and response body, in addition to the metadata). (default "metadata")
4647
--http.default.tls string Whether to enable TLS for the default interface, options are 'disabled', 'server', 'server-client'. Leaving it empty is synonymous to 'disabled',
4748
--internalratelimiter When set, expensive internal calls are rate-limited to protect the network. Always enabled in strict mode. (default true)
48-
--jsonld.contexts.localmapping stringToString This setting allows mapping external URLs to local files for e.g. preventing external dependencies. These mappings have precedence over those in remoteallowlist. (default [https://schema.org=assets/contexts/schema-org-v13.ldjson,https://nuts.nl/credentials/v1=assets/contexts/nuts.ldjson,https://www.w3.org/2018/credentials/v1=assets/contexts/w3c-credentials-v1.ldjson,https://w3c-ccg.github.io/lds-jws2020/contexts/lds-jws2020-v1.json=assets/contexts/lds-jws2020-v1.ldjson,https://w3id.org/vc/status-list/2021/v1=assets/contexts/w3c-statuslist2021.ldjson])
49+
--jsonld.contexts.localmapping stringToString This setting allows mapping external URLs to local files for e.g. preventing external dependencies. These mappings have precedence over those in remoteallowlist. (default [https://schema.org=assets/contexts/schema-org-v13.ldjson,https://nuts.nl/credentials/v1=assets/contexts/nuts.ldjson,https://www.w3.org/2018/credentials/v1=assets/contexts/w3c-credentials-v1.ldjson,https://w3id.org/vc/status-list/2021/v1=assets/contexts/w3c-statuslist2021.ldjson,https://w3c-ccg.github.io/lds-jws2020/contexts/lds-jws2020-v1.json=assets/contexts/lds-jws2020-v1.ldjson])
4950
--jsonld.contexts.remoteallowlist strings In strict mode, fetching external JSON-LD contexts is not allowed except for context-URLs listed here. (default [https://schema.org,https://www.w3.org/2018/credentials/v1,https://w3c-ccg.github.io/lds-jws2020/contexts/lds-jws2020-v1.json,https://w3id.org/vc/status-list/2021/v1])
5051
--loggerformat string Log format (text, json) (default "text")
5152
--network.bootstrapnodes strings List of bootstrap nodes ('<host>:<port>') which the node initially connect to.

0 commit comments

Comments
 (0)