Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Discovery: SQLite-based server implementation #2589

Merged
merged 24 commits into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -176,9 +176,9 @@ The following options can be configured on the server:
:widths: 20 30 50
:class: options-table

==================================== =============================================================================================================================================================================================================================================================================================================== ================================================================================================================================================================================================================================================
==================================== =============================================================================================================================================================================================================================================================================================================== ================================================================================================================================================================================================================================================================================================================================
Key Default Description
==================================== =============================================================================================================================================================================================================================================================================================================== ================================================================================================================================================================================================================================================
==================================== =============================================================================================================================================================================================================================================================================================================== ================================================================================================================================================================================================================================================================================================================================
configfile nuts.yaml Nuts config file
cpuprofile When set, a CPU profile is written to the given path. Ignored when strictmode is set.
datadir ./data Directory where the node stores its files.
Expand Down Expand Up @@ -252,12 +252,12 @@ The following options can be configured on the server:
storage.redis.sentinel.password Password for authenticating to Redis Sentinels.
storage.redis.sentinel.username Username for authenticating to Redis Sentinels.
storage.redis.tls.truststorefile PEM file containing the trusted CA certificate(s) for authenticating remote Redis servers. Can only be used when connecting over TLS (use 'rediss://' as scheme in address).
storage.sql.connection Connection string for the SQL database. If not set, it defaults to a SQLite database stored inside the configured data directory. If specifying a SQLite database, make sure to enable foreign keys with the '_foreign_keys=on' query parameter.
storage.sql.connection Connection string for the SQL database. If not set it, defaults to a SQLite database stored inside the configured data directory. Note: using SQLite is not recommended in production environments. If using SQLite anyways, remember to enable foreign keys ('_foreign_keys=on') and the write-ahead-log ('_journal_mode=WAL').
**VCR**
vcr.openid4vci.definitionsdir Directory with the additional credential definitions the node could issue (experimental, may change without notice).
vcr.openid4vci.enabled true Enable issuing and receiving credentials over OpenID4VCI.
vcr.openid4vci.timeout 30s Time-out for OpenID4VCI HTTP client operations.
==================================== =============================================================================================================================================================================================================================================================================================================== ================================================================================================================================================================================================================================================
==================================== =============================================================================================================================================================================================================================================================================================================== ================================================================================================================================================================================================================================================================================================================================

This table is automatically generated using the configuration flags in the core and engines. When they're changed
the options table must be regenerated using the Makefile:
Expand Down
12 changes: 5 additions & 7 deletions discoveryservice/config.go → discovery/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,23 @@
*
*/

package discoveryservice
package discovery

// Config holds the config of the module
type Config struct {
reinkrul marked this conversation as resolved.
Show resolved Hide resolved
Server ServerConfig `koanf:"server"`
Definitions DefinitionsConfig `koanf:"definitions"`
Server ServerConfig `koanf:"server"`
reinkrul marked this conversation as resolved.
Show resolved Hide resolved
Definitions ServiceDefinitionsConfig `koanf:"definitions"`
}

// DefinitionsConfig holds the config for loading Service Definitions.
type DefinitionsConfig struct {
// ServiceDefinitionsConfig holds the config for loading Service Definitions.
type ServiceDefinitionsConfig struct {
Directory string `koanf:"directory"`
}

// ServerConfig holds the config for the server
type ServerConfig struct {
// DefinitionIDs specifies which use case lists the server serves.
DefinitionIDs []string `koanf:"definition_ids"`
// Directory is the directory where the server stores the lists.
Directory string `koanf:"directory"`
}

// DefaultConfig returns the default configuration.
Expand Down
23 changes: 13 additions & 10 deletions discoveryservice/definition.go → discovery/definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*
*/

package discoveryservice
package discovery

import (
"bytes"
Expand All @@ -29,38 +29,41 @@ import (

//go:embed *.json
var jsonSchemaFiles embed.FS
var definitionJsonSchema *jsonschema.Schema
var serviceDefinitionJsonSchema *jsonschema.Schema

func init() {
serviceDefinitionSchemaData, err := jsonSchemaFiles.ReadFile("service-definition-schema.json")
if err != nil {
panic(err)
}
const schemaURL = "http://nuts.nl/schemas/discovery-service-v0.json"
if err := v2.Compiler.AddResource(schemaURL, bytes.NewReader(serviceDefinitionSchemaData)); err != nil {
compiler := v2.Compiler()
if err := compiler.AddResource(schemaURL, bytes.NewReader(serviceDefinitionSchemaData)); err != nil {
panic(err)
}
definitionJsonSchema = v2.Compiler.MustCompile(schemaURL)
serviceDefinitionJsonSchema = compiler.MustCompile(schemaURL)
}

// Definition holds the definition of a service.
type Definition struct {
// ServiceDefinition holds the definition of a service.
type ServiceDefinition struct {
// ID is the unique identifier of the use case.
ID string `json:"id"`
// Endpoint is the endpoint where the use case list is served.
Endpoint string `json:"endpoint"`
// PresentationDefinition specifies the Presentation Definition submissions to the list must conform to,
// PresentationDefinition specifies the Presentation ServiceDefinition submissions to the list must conform to,
// according to the Presentation Exchange specification.
PresentationDefinition pe.PresentationDefinition `json:"presentation_definition"`
// PresentationMaxValidity specifies how long submitted presentations are allowed to be valid (in seconds).
PresentationMaxValidity int `json:"presentation_max_validity"`
}

func ParseDefinition(data []byte) (*Definition, error) {
if err := definitionJsonSchema.Validate(bytes.NewReader(data)); err != nil {
// ParseServiceDefinition validates the input against the JSON schema for service definitions.
// If the input is valid, it is parsed and returned as a ServiceDefinition.
func ParseServiceDefinition(data []byte) (*ServiceDefinition, error) {
if err := serviceDefinitionJsonSchema.Validate(bytes.NewReader(data)); err != nil {
return nil, err
}
var definition Definition
var definition ServiceDefinition
if err := json.Unmarshal(data, &definition); err != nil {
return nil, err
}
Expand Down
16 changes: 13 additions & 3 deletions discoveryservice/interface.go → discovery/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@
*
*/

package discoveryservice
package discovery

import (
"errors"
"github.com/nuts-foundation/go-did/vc"
)

Expand All @@ -29,14 +30,23 @@ import (
// Pass 0 to start at the beginning of the list.
type Timestamp uint64
reinkrul marked this conversation as resolved.
Show resolved Hide resolved

// ErrServiceNotFound is returned when a service (ID) is not found in the discovery service.
var ErrServiceNotFound = errors.New("discovery service not found")

// ErrPresentationAlreadyExists is returned when a presentation is added to the discovery service,
// but a presentation with this ID already exists.
var ErrPresentationAlreadyExists = errors.New("presentation already exists")

// Server defines the API for Discovery Servers.
type Server interface {
// Add registers a presentation of the given Discovery Service.
// If the presentation is not valid or it does not conform to the Service Definition, it returns an error.
// Add registers a presentation on the given Discovery Service.
// If the presentation is not valid or it does not conform to the Service ServiceDefinition, it returns an error.
Add(serviceID string, presentation vc.VerifiablePresentation) error
// Get retrieves the presentations for the given service, starting at the given timestamp.
Get(serviceID string, startAt Timestamp) ([]vc.VerifiablePresentation, *Timestamp, error)
}

// Client defines the API for Discovery Clients.
type Client interface {
Search(serviceID string, query map[string]string) ([]vc.VerifiablePresentation, error)
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
"github.com/sirupsen/logrus"
)

var _logger = logrus.StandardLogger().WithField(core.LogFieldModule, "DiscoveryService")
var _logger = logrus.StandardLogger().WithField(core.LogFieldModule, "Discovery")

// Logger returns a logger with the module field set
func Logger() *logrus.Entry {
Expand Down
8 changes: 4 additions & 4 deletions discoveryservice/mock.go → discovery/mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading