Skip to content
This repository has been archived by the owner on Mar 5, 2022. It is now read-only.

Commit

Permalink
feat: Validate Sidetree configuration
Browse files Browse the repository at this point in the history
Implemented configuration validators that validate both peer-specific Sudetree configuration and consortium config, which includes protocol config.

closes #126

Signed-off-by: Bob Stasyszyn <[email protected]>
  • Loading branch information
bstasyszyn committed Feb 19, 2020
1 parent dcad027 commit 4cee675
Show file tree
Hide file tree
Showing 28 changed files with 598 additions and 44 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ FABRIC_TOOLS_TAG ?= $(ARCH)-$(FABRIC_TOOLS_VERSION)

# Fabric peer ext docker image (overridable)
FABRIC_PEER_EXT_IMAGE ?= trustbloc/fabric-peer
FABRIC_PEER_EXT_VERSION ?= 0.1.2-snapshot-e21ab2b
FABRIC_PEER_EXT_VERSION ?= 0.1.2-snapshot-fc25d20
FABRIC_PEER_EXT_TAG ?= $(ARCH)-$(FABRIC_PEER_EXT_VERSION)

export FABRIC_CLI_EXT_VERSION ?= 3fd66894726c1afcd904413dcfa3b4d586ea6c92
export FABRIC_CLI_EXT_VERSION ?= fce574c704389e1741dc9edb40456fd7877d42d5

# Namespace for the blocnode image
DOCKER_OUTPUT_NS ?= trustbloc
Expand Down
4 changes: 2 additions & 2 deletions cmd/peer/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ require (

replace github.com/hyperledger/fabric => github.com/trustbloc/fabric-mod v0.1.2-0.20200211211900-62c249e072e2

replace github.com/hyperledger/fabric/extensions => github.com/trustbloc/fabric-peer-ext/mod/peer v0.0.0-20200211231405-e21ab2bc259f
replace github.com/hyperledger/fabric/extensions => github.com/trustbloc/fabric-peer-ext/mod/peer v0.0.0-20200218213141-fc25d209285c

replace github.com/trustbloc/fabric-peer-ext => github.com/trustbloc/fabric-peer-ext v0.1.2-0.20200211231405-e21ab2bc259f
replace github.com/trustbloc/fabric-peer-ext => github.com/trustbloc/fabric-peer-ext v0.1.2-0.20200218213141-fc25d209285c

replace github.com/trustbloc/sidetree-fabric => ../..

Expand Down
8 changes: 4 additions & 4 deletions cmd/peer/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -340,10 +340,10 @@ github.com/tedsuo/ifrit v0.0.0-20180802180643-bea94bb476cc/go.mod h1:eyZnKCc955u
github.com/tmc/grpc-websocket-proxy v0.0.0-20170815181823-89b8d40f7ca8/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=
github.com/trustbloc/fabric-mod v0.1.2-0.20200211211900-62c249e072e2 h1:gHS0f6VQsWxMJrUshduwpHvQIApmBDCvdnrqKld9wfo=
github.com/trustbloc/fabric-mod v0.1.2-0.20200211211900-62c249e072e2/go.mod h1:2UoZyvViPfKpCemDr/N2uyo2S+dSI4KbDqVmMePyOes=
github.com/trustbloc/fabric-peer-ext v0.1.2-0.20200211231405-e21ab2bc259f h1:4MJmpaFGySX+hpX1AnvOqpJITza592hu+ADSfHTGlJg=
github.com/trustbloc/fabric-peer-ext v0.1.2-0.20200211231405-e21ab2bc259f/go.mod h1:zdtIRio2I5i5QxLyKZbTnQqL8xmcF45RdAlopd/Evqs=
github.com/trustbloc/fabric-peer-ext/mod/peer v0.0.0-20200211231405-e21ab2bc259f h1:krvThqJoCQpJPvf0lQv+zHesRj5SrbXhnhmrUaF6kdQ=
github.com/trustbloc/fabric-peer-ext/mod/peer v0.0.0-20200211231405-e21ab2bc259f/go.mod h1:ot55KksVO/lnRDVVNDF14AtdS7CSs//NE20WU6x/eiw=
github.com/trustbloc/fabric-peer-ext v0.1.2-0.20200218213141-fc25d209285c h1:Jx+movyiTkGV5eB5lVq3XXGv3vqU/af+rBWvIG+VGBs=
github.com/trustbloc/fabric-peer-ext v0.1.2-0.20200218213141-fc25d209285c/go.mod h1:zdtIRio2I5i5QxLyKZbTnQqL8xmcF45RdAlopd/Evqs=
github.com/trustbloc/fabric-peer-ext/mod/peer v0.0.0-20200218213141-fc25d209285c h1:8b1bjIJ5X8TTApy/3P6GDKx97qIx6mGyct9GUHlwnGw=
github.com/trustbloc/fabric-peer-ext/mod/peer v0.0.0-20200218213141-fc25d209285c/go.mod h1:ot55KksVO/lnRDVVNDF14AtdS7CSs//NE20WU6x/eiw=
github.com/trustbloc/fabric-protos-go-ext v0.1.2-0.20200205170340-c69bba6d7b81 h1:iant8lATTlHYUWVdL3llKZuGgZUHBL4q7JUkLmcBQXk=
github.com/trustbloc/fabric-protos-go-ext v0.1.2-0.20200205170340-c69bba6d7b81/go.mod h1:xVYTjK4DtZRBxZ2D9aE4y6AbLaPwue2o/criQyQbVD0=
github.com/trustbloc/sidetree-core-go v0.1.2-0.20200214144924-3e7aa7825416 h1:t9FawNEHc0R1PTYdDhWd/Q643KAN9IpJZMdlu2FlBxM=
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ require (

replace github.com/hyperledger/fabric => github.com/trustbloc/fabric-mod v0.1.2-0.20200211211900-62c249e072e2

replace github.com/hyperledger/fabric/extensions => github.com/trustbloc/fabric-peer-ext/mod/peer v0.0.0-20200211231405-e21ab2bc259f
replace github.com/hyperledger/fabric/extensions => github.com/trustbloc/fabric-peer-ext/mod/peer v0.0.0-20200218213141-fc25d209285c

replace github.com/trustbloc/fabric-peer-ext => github.com/trustbloc/fabric-peer-ext v0.1.2-0.20200211231405-e21ab2bc259f
replace github.com/trustbloc/fabric-peer-ext => github.com/trustbloc/fabric-peer-ext v0.1.2-0.20200218213141-fc25d209285c

replace github.com/hyperledger/fabric-protos-go => github.com/trustbloc/fabric-protos-go-ext v0.1.2-0.20200205170340-c69bba6d7b81

Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -352,10 +352,10 @@ github.com/tedsuo/ifrit v0.0.0-20180802180643-bea94bb476cc/go.mod h1:eyZnKCc955u
github.com/tmc/grpc-websocket-proxy v0.0.0-20170815181823-89b8d40f7ca8/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=
github.com/trustbloc/fabric-mod v0.1.2-0.20200211211900-62c249e072e2 h1:gHS0f6VQsWxMJrUshduwpHvQIApmBDCvdnrqKld9wfo=
github.com/trustbloc/fabric-mod v0.1.2-0.20200211211900-62c249e072e2/go.mod h1:2UoZyvViPfKpCemDr/N2uyo2S+dSI4KbDqVmMePyOes=
github.com/trustbloc/fabric-peer-ext v0.1.2-0.20200211231405-e21ab2bc259f h1:4MJmpaFGySX+hpX1AnvOqpJITza592hu+ADSfHTGlJg=
github.com/trustbloc/fabric-peer-ext v0.1.2-0.20200211231405-e21ab2bc259f/go.mod h1:zdtIRio2I5i5QxLyKZbTnQqL8xmcF45RdAlopd/Evqs=
github.com/trustbloc/fabric-peer-ext/mod/peer v0.0.0-20200211231405-e21ab2bc259f h1:krvThqJoCQpJPvf0lQv+zHesRj5SrbXhnhmrUaF6kdQ=
github.com/trustbloc/fabric-peer-ext/mod/peer v0.0.0-20200211231405-e21ab2bc259f/go.mod h1:ot55KksVO/lnRDVVNDF14AtdS7CSs//NE20WU6x/eiw=
github.com/trustbloc/fabric-peer-ext v0.1.2-0.20200218213141-fc25d209285c h1:Jx+movyiTkGV5eB5lVq3XXGv3vqU/af+rBWvIG+VGBs=
github.com/trustbloc/fabric-peer-ext v0.1.2-0.20200218213141-fc25d209285c/go.mod h1:zdtIRio2I5i5QxLyKZbTnQqL8xmcF45RdAlopd/Evqs=
github.com/trustbloc/fabric-peer-ext/mod/peer v0.0.0-20200218213141-fc25d209285c h1:8b1bjIJ5X8TTApy/3P6GDKx97qIx6mGyct9GUHlwnGw=
github.com/trustbloc/fabric-peer-ext/mod/peer v0.0.0-20200218213141-fc25d209285c/go.mod h1:ot55KksVO/lnRDVVNDF14AtdS7CSs//NE20WU6x/eiw=
github.com/trustbloc/fabric-protos-go-ext v0.1.2-0.20200205170340-c69bba6d7b81 h1:iant8lATTlHYUWVdL3llKZuGgZUHBL4q7JUkLmcBQXk=
github.com/trustbloc/fabric-protos-go-ext v0.1.2-0.20200205170340-c69bba6d7b81/go.mod h1:xVYTjK4DtZRBxZ2D9aE4y6AbLaPwue2o/criQyQbVD0=
github.com/trustbloc/sidetree-core-go v0.1.2-0.20200214144924-3e7aa7825416 h1:t9FawNEHc0R1PTYdDhWd/Q643KAN9IpJZMdlu2FlBxM=
Expand Down
11 changes: 7 additions & 4 deletions pkg/peer/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,17 @@ const (
// GlobalMSPID is used as the consortium-wide MSP ID (i.e. non org-specific)
GlobalMSPID = "general"

// SidetreeAppVersion is the version of the Sidetree config application
SidetreeAppVersion = "1"

// ProtocolComponentName is the name of the Sidetree protocol config component
ProtocolComponentName = "protocol"

// SidetreeAppName is the '=name of the Sidetree config application
SidetreeAppName = "sidetree"
// SidetreePeerAppName is the '=name of the Sidetree config application
SidetreePeerAppName = "sidetree"

// SidetreeAppVersion is the version of the Sidetree config application
SidetreeAppVersion = "1"
// SidetreePeerAppVersion is the version of the Sidetree config application
SidetreePeerAppVersion = "1"
)

// Namespace holds Sidetree namespace config
Expand Down
66 changes: 66 additions & 0 deletions pkg/peer/config/mocks/validatorregistry.gen.go

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

37 changes: 22 additions & 15 deletions pkg/peer/config/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ type configServiceProvider interface {
ForChannel(channelID string) ledgerconfig.Service
}

type validatorRegistry interface {
Register(v ledgerconfig.Validator)
}

// SidetreeProvider manages Sidetree configuration for the various channels
type SidetreeProvider struct {
configProvider configServiceProvider
Expand All @@ -35,9 +39,15 @@ type SidetreeService interface {
}

// NewSidetreeProvider returns a new SidetreeProvider instance
func NewSidetreeProvider(configProvider configServiceProvider) *SidetreeProvider {
func NewSidetreeProvider(configProvider configServiceProvider, registry validatorRegistry) *SidetreeProvider {
logger.Info("Creating Sidetree config provider")
return &SidetreeProvider{configProvider: configProvider}

registry.Register(&sidetreeValidator{})
registry.Register(&sidetreePeerValidator{})

return &SidetreeProvider{
configProvider: configProvider,
}
}

// ForChannel returns the service for the given channel
Expand All @@ -51,7 +61,7 @@ type sidetreeService struct {

// LoadSidetree loads the Sidetree configuration for the given namespace
func (c *sidetreeService) LoadSidetree(namespace string) (Sidetree, error) {
key := ledgerconfig.NewAppKey(GlobalMSPID, namespace, "1")
key := ledgerconfig.NewAppKey(GlobalMSPID, namespace, SidetreeAppVersion)

var sidetreeConfig Sidetree
if err := c.load(key, &sidetreeConfig); err != nil {
Expand All @@ -67,7 +77,7 @@ func (c *sidetreeService) LoadSidetree(namespace string) (Sidetree, error) {

// LoadSidetreePeer loads the peer-specific Sidetree configuration
func (c *sidetreeService) LoadSidetreePeer(mspID, peerID string) (SidetreePeer, error) {
key := ledgerconfig.NewPeerKey(mspID, peerID, SidetreeAppName, SidetreeAppVersion)
key := ledgerconfig.NewPeerKey(mspID, peerID, SidetreePeerAppName, SidetreePeerAppVersion)

var sidetreeConfig SidetreePeer
if err := c.load(key, &sidetreeConfig); err != nil {
Expand Down Expand Up @@ -111,10 +121,14 @@ func (c *sidetreeService) load(key *ledgerconfig.Key, v interface{}) error {
return errors.WithMessagef(err, "error getting Sidetree config for key %s", key)
}

return unmarshal(cfg, v)
}

func unmarshal(value *ledgerconfig.Value, v interface{}) error {
vp := viper.New()
vp.SetConfigType(string(cfg.Format))
vp.SetConfigType(string(value.Format))

if err := vp.ReadConfig(bytes.NewBufferString(cfg.Config)); err != nil {
if err := vp.ReadConfig(bytes.NewBufferString(value.Config)); err != nil {
return errors.WithMessage(err, "error reading config")
}

Expand All @@ -126,16 +140,9 @@ func (c *sidetreeService) load(key *ledgerconfig.Key, v interface{}) error {
}

func unmarshalProtocol(cfg *ledgerconfig.Value) (*protocolApi.Protocol, error) {
v := viper.New()
v.SetConfigType(string(cfg.Format))

err := v.ReadConfig(bytes.NewBufferString(cfg.Config))
if err != nil {
return nil, errors.WithMessage(err, "error reading Sidetree protocol config")
}

protocol := &protocolApi.Protocol{}
if err := v.Unmarshal(protocol); err != nil {

if err := unmarshal(cfg, protocol); err != nil {
return nil, errors.WithMessage(err, "error unmarshalling Sidetree protocol config")
}

Expand Down
7 changes: 5 additions & 2 deletions pkg/peer/config/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (

//go:generate counterfeiter -o ./mocks/configserviceprovider.gen.go --fake-name ConfigServiceProvider . configServiceProvider
//go:generate counterfeiter -o ./mocks/configservice.gen.go --fake-name ConfigService github.com/trustbloc/fabric-peer-ext/pkg/config/ledgerconfig/config.Service
//go:generate counterfeiter -o ./mocks/validatorregistry.gen.go --fake-name ValidatorRegistry . validatorRegistry

const (
channelID = "mychannel"
Expand All @@ -40,7 +41,9 @@ func TestNewSidetreeProvider(t *testing.T) {
configProvider := &mocks.ConfigServiceProvider{}
configProvider.ForChannelReturns(configService)

p := NewSidetreeProvider(configProvider)
validatorRegistry := &mocks.ValidatorRegistry{}

p := NewSidetreeProvider(configProvider, validatorRegistry)
require.NotNil(t, p)

s := p.ForChannel(channelID)
Expand Down Expand Up @@ -154,7 +157,7 @@ func TestNewSidetreeProvider(t *testing.T) {
cfgValue := &ledgercfg.Value{}
configService.GetReturns(cfgValue, nil)

err := s.(*sidetreeService).load(ledgercfg.NewAppKey(GlobalMSPID, SidetreeAppName, SidetreeAppVersion), func() {})
err := s.(*sidetreeService).load(ledgercfg.NewAppKey(GlobalMSPID, SidetreePeerAppName, SidetreePeerAppVersion), func() {})
require.Error(t, err)
})
}
69 changes: 69 additions & 0 deletions pkg/peer/config/sidetreepeervalidator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
Copyright SecureKey Technologies Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/

package config

import (
"github.com/pkg/errors"
"github.com/trustbloc/fabric-peer-ext/pkg/config/ledgerconfig/config"
)

// sidetreePeerValidator validates the SidetreePeer configuration
type sidetreePeerValidator struct {
}

func (v *sidetreePeerValidator) Validate(kv *config.KeyValue) error {
if kv.AppName != SidetreePeerAppName {
return nil
}

logger.Debugf("Validating config %s", kv)

if kv.ComponentName != "" {
return errors.Errorf("unexpected component [%s] for %s", kv.ComponentName, kv.Key)
}

if kv.PeerID == "" {
return errors.Errorf("field PeerID required for %s", kv.Key)
}

if kv.AppVersion != SidetreePeerAppVersion {
return errors.Errorf("unsupported application version [%s] for %s", kv.AppVersion, kv.Key)
}

var sidetreeCfg SidetreePeer
if err := unmarshal(kv.Value, &sidetreeCfg); err != nil {
return errors.WithMessagef(err, "invalid config %s", kv.Key)
}

if sidetreeCfg.Monitor.Period == 0 {
logger.Infof("The Sidetree monitor period is set to 0 and therefore will be disabled for peer [%s].", kv.PeerID)
}

for _, ns := range sidetreeCfg.Namespaces {
if err := v.validateNamespace(kv, ns); err != nil {
return err
}
}

return nil
}

func (v *sidetreePeerValidator) validateNamespace(kv *config.KeyValue, ns Namespace) error {
if ns.Namespace == "" {
return errors.Errorf("field 'Namespace' is required for %s", kv.Key)
}

if ns.BasePath == "" {
return errors.Errorf("field 'BasePath' is required for %s", kv.Key)
}

if ns.BasePath[0:1] != "/" {
return errors.Errorf("field 'BasePath' must begin with '/' for %s", kv.Key)
}

return nil
}
Loading

0 comments on commit 4cee675

Please sign in to comment.