Skip to content
This repository has been archived by the owner on Dec 12, 2024. It is now read-only.

Uptake JSON Schema for FPWD #494

Merged
merged 12 commits into from
May 31, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
182 changes: 123 additions & 59 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,17 +64,6 @@ type ServerConfig struct {
EnableAllowAllCORS bool `toml:"enable_allow_all_cors" conf:"default:false"`
}

type IssuanceServiceConfig struct {
*BaseServiceConfig
}

func (s *IssuanceServiceConfig) IsEmpty() bool {
if s == nil {
return true
}
return reflect.DeepEqual(s, &IssuanceServiceConfig{})
}

// ServicesConfig represents configurable properties for the components of the SSI Service
type ServicesConfig struct {
// at present, it is assumed that a single storage provider works for all services
Expand All @@ -87,11 +76,12 @@ type ServicesConfig struct {
// Embed all service-specific configs here. The order matters: from which should be instantiated first, to last
KeyStoreConfig KeyStoreServiceConfig `toml:"keystore,omitempty"`
DIDConfig DIDServiceConfig `toml:"did,omitempty"`
IssuanceServiceConfig IssuanceServiceConfig `toml:"issuance,omitempty"`
SchemaConfig SchemaServiceConfig `toml:"schema,omitempty"`
CredentialConfig CredentialServiceConfig `toml:"credential,omitempty"`
ManifestConfig ManifestServiceConfig `toml:"manifest,omitempty"`
OperationConfig OperationServiceConfig `toml:"operation,omitempty"`
PresentationConfig PresentationServiceConfig `toml:"presentation,omitempty"`
ManifestConfig ManifestServiceConfig `toml:"manifest,omitempty"`
IssuanceServiceConfig IssuanceServiceConfig `toml:"issuance,omitempty"`
WebhookConfig WebhookServiceConfig `toml:"webhook,omitempty"`
}

Expand Down Expand Up @@ -164,16 +154,15 @@ func (c *CredentialServiceConfig) IsEmpty() bool {
return reflect.DeepEqual(c, &CredentialServiceConfig{})
}

type ManifestServiceConfig struct {
type OperationServiceConfig struct {
*BaseServiceConfig
ExpirationDuration time.Duration `toml:"expiration_duration" conf:"default:30m"`
}

func (m *ManifestServiceConfig) IsEmpty() bool {
if m == nil {
func (o *OperationServiceConfig) IsEmpty() bool {
if o == nil {
return true
}
return reflect.DeepEqual(m, &ManifestServiceConfig{})
return reflect.DeepEqual(o, &OperationServiceConfig{})
}

type PresentationServiceConfig struct {
Expand All @@ -188,6 +177,29 @@ func (p *PresentationServiceConfig) IsEmpty() bool {
return reflect.DeepEqual(p, &PresentationServiceConfig{})
}

type ManifestServiceConfig struct {
*BaseServiceConfig
ExpirationDuration time.Duration `toml:"expiration_duration" conf:"default:30m"`
}

func (m *ManifestServiceConfig) IsEmpty() bool {
if m == nil {
return true
}
return reflect.DeepEqual(m, &ManifestServiceConfig{})
}

type IssuanceServiceConfig struct {
*BaseServiceConfig
}

func (s *IssuanceServiceConfig) IsEmpty() bool {
if s == nil {
return true
}
return reflect.DeepEqual(s, &IssuanceServiceConfig{})
}

type WebhookServiceConfig struct {
*BaseServiceConfig
WebhookTimeout string `toml:"webhook_timeout"`
Expand Down Expand Up @@ -215,7 +227,8 @@ func LoadConfig(path string) (*SSIServiceConfig, error) {
}

if loadDefaultConfig {
loadDefaultServicesConfig(&config)
defaultServicesConfig := getDefaultServicesConfig()
config.Services = defaultServicesConfig
} else if err = loadTOMLConfig(path, &config); err != nil {
return nil, errors.Wrap(err, "load toml config")
}
Expand All @@ -236,74 +249,73 @@ func checkValidConfigPath(path string) (bool, error) {
} else if filepath.Ext(path) != Extension {
return false, fmt.Errorf("path<%s> did not match the expected TOML format", path)
}

return defaultConfig, nil
}

func parseAndApplyDefaults(config SSIServiceConfig) error {
// parse and apply defaults
if err := conf.Parse(os.Args[1:], ServiceName, &config); err != nil {
switch {
case errors.Is(err, conf.ErrHelpWanted):
usage, err := conf.Usage(ServiceName, &config)
if err != nil {
return errors.Wrap(err, "parsing config")
}
fmt.Println(usage)

return nil

case errors.Is(err, conf.ErrVersionWanted):
version, err := conf.VersionString(ServiceName, &config)
if err != nil {
return errors.Wrap(err, "generating config version")
}
err := conf.Parse(os.Args[1:], ServiceName, &config)
if err == nil {
return nil
}
switch {
case errors.Is(err, conf.ErrHelpWanted):
usage, err := conf.Usage(ServiceName, &config)
if err != nil {
return errors.Wrap(err, "parsing config")
}
logrus.Println(usage)

fmt.Println(version)
return nil
return nil
case errors.Is(err, conf.ErrVersionWanted):
version, err := conf.VersionString(ServiceName, &config)
if err != nil {
return errors.Wrap(err, "generating config version")
}

return errors.Wrap(err, "parsing config")
logrus.Println(version)
return nil
}

return nil
return errors.Wrap(err, "parsing config")
}

func loadDefaultServicesConfig(config *SSIServiceConfig) {
servicesConfig := ServicesConfig{
// TODO(gabe) remove this from config in https://github.com/TBD54566975/ssi-service/issues/502
func getDefaultServicesConfig() ServicesConfig {
return ServicesConfig{
StorageProvider: "bolt",
ServiceEndpoint: DefaultServiceEndpoint,
KeyStoreConfig: KeyStoreServiceConfig{
BaseServiceConfig: &BaseServiceConfig{Name: "keystore"},
BaseServiceConfig: &BaseServiceConfig{Name: "keystore", ServiceEndpoint: DefaultServiceEndpoint + "/v1/keys"},
MasterKeyPassword: "default-password",
},
DIDConfig: DIDServiceConfig{
BaseServiceConfig: &BaseServiceConfig{Name: "did"},
BaseServiceConfig: &BaseServiceConfig{Name: "did", ServiceEndpoint: DefaultServiceEndpoint + "/v1/dids"},
Methods: []string{"key", "web"},
LocalResolutionMethods: []string{"key", "peer", "web", "pkh"},
LocalResolutionMethods: []string{"key", "peer", "web", "jwk", "pkh"},
},
SchemaConfig: SchemaServiceConfig{
BaseServiceConfig: &BaseServiceConfig{Name: "schema"},
BaseServiceConfig: &BaseServiceConfig{Name: "schema", ServiceEndpoint: DefaultServiceEndpoint + "/v1/schemas"},
},
CredentialConfig: CredentialServiceConfig{
BaseServiceConfig: &BaseServiceConfig{Name: "credential", ServiceEndpoint: DefaultServiceEndpoint},
BaseServiceConfig: &BaseServiceConfig{Name: "credential", ServiceEndpoint: DefaultServiceEndpoint + "/v1/credentials"},
},
ManifestConfig: ManifestServiceConfig{
BaseServiceConfig: &BaseServiceConfig{Name: "manifest"},
OperationConfig: OperationServiceConfig{
BaseServiceConfig: &BaseServiceConfig{Name: "operation", ServiceEndpoint: DefaultServiceEndpoint + "/v1/operations"},
},
PresentationConfig: PresentationServiceConfig{
BaseServiceConfig: &BaseServiceConfig{Name: "presentation"},
BaseServiceConfig: &BaseServiceConfig{Name: "presentation", ServiceEndpoint: DefaultServiceEndpoint + "/v1/presentations"},
},
ManifestConfig: ManifestServiceConfig{
BaseServiceConfig: &BaseServiceConfig{Name: "manifest", ServiceEndpoint: DefaultServiceEndpoint + "/v1/manifests"},
},
IssuanceServiceConfig: IssuanceServiceConfig{
BaseServiceConfig: &BaseServiceConfig{Name: "issuance"},
BaseServiceConfig: &BaseServiceConfig{Name: "issuance", ServiceEndpoint: DefaultServiceEndpoint + "/v1/issuancetemplates"},
},
WebhookConfig: WebhookServiceConfig{
BaseServiceConfig: &BaseServiceConfig{Name: "webhook"},
BaseServiceConfig: &BaseServiceConfig{Name: "webhook", ServiceEndpoint: DefaultServiceEndpoint + "/v1/webhooks"},
WebhookTimeout: "10s",
},
}

config.Services = servicesConfig
}

func loadTOMLConfig(path string, config *SSIServiceConfig) error {
Expand All @@ -312,11 +324,63 @@ func loadTOMLConfig(path string, config *SSIServiceConfig) error {
return errors.Wrapf(err, "could not load config: %s", path)
}

// apply defaults if not included in toml file
if config.Services.CredentialConfig.BaseServiceConfig.ServiceEndpoint == "" {
config.Services.CredentialConfig.BaseServiceConfig.ServiceEndpoint = config.Services.ServiceEndpoint
// apply defaults
services := config.Services
endpoint := services.ServiceEndpoint + "/v1"
if services.KeyStoreConfig.IsEmpty() {
services.KeyStoreConfig = KeyStoreServiceConfig{
BaseServiceConfig: new(BaseServiceConfig),
}
}

services.KeyStoreConfig.ServiceEndpoint = endpoint + "/keys"
if services.DIDConfig.IsEmpty() {
services.DIDConfig = DIDServiceConfig{
BaseServiceConfig: new(BaseServiceConfig),
}
}
services.DIDConfig.ServiceEndpoint = endpoint + "/dids"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If someone sets the service_endpoint for a single service and not others, seems like this will override. Is that intentional? Should defaults be applied before decoding the file?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this needs to be refactored because we don't want to enable config for setting service endpoints. I'll open a new issue

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if services.SchemaConfig.IsEmpty() {
services.SchemaConfig = SchemaServiceConfig{
BaseServiceConfig: new(BaseServiceConfig),
}
}
services.SchemaConfig.ServiceEndpoint = endpoint + "/schemas"
if services.CredentialConfig.IsEmpty() {
services.CredentialConfig = CredentialServiceConfig{
BaseServiceConfig: new(BaseServiceConfig),
}
}
services.CredentialConfig.ServiceEndpoint = endpoint + "/credentials"
if services.OperationConfig.IsEmpty() {
services.OperationConfig = OperationServiceConfig{
BaseServiceConfig: new(BaseServiceConfig),
}
}
services.OperationConfig.ServiceEndpoint = endpoint + "/operations"
if services.PresentationConfig.IsEmpty() {
services.PresentationConfig = PresentationServiceConfig{
BaseServiceConfig: new(BaseServiceConfig),
}
}
services.PresentationConfig.ServiceEndpoint = endpoint + "/presentations"
if services.ManifestConfig.IsEmpty() {
services.ManifestConfig = ManifestServiceConfig{
BaseServiceConfig: new(BaseServiceConfig),
}
}
services.ManifestConfig.ServiceEndpoint = endpoint + "/manifests"
if services.IssuanceServiceConfig.IsEmpty() {
services.IssuanceServiceConfig = IssuanceServiceConfig{
BaseServiceConfig: new(BaseServiceConfig),
}
}
services.IssuanceServiceConfig.ServiceEndpoint = endpoint + "/issuancetemplates"
if services.WebhookConfig.IsEmpty() {
services.WebhookConfig = WebhookServiceConfig{
BaseServiceConfig: new(BaseServiceConfig),
}
}
services.WebhookConfig.ServiceEndpoint = endpoint + "/webhooks"
return nil
}

Expand Down
2 changes: 1 addition & 1 deletion doc/docs.go

Large diffs are not rendered by default.

Loading