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

Namespace config validation #833

Merged
merged 3 commits into from
May 26, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 2 additions & 0 deletions docs/reference/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -618,6 +618,8 @@ nav_order: 3
|---|-----------|----|-------------|
|description|A description for the namespace|`string`|`<nil>`
|name|The name of the namespace (must be unique)|`string`|`<nil>`
|plugins|The list of plugins for this namespace|`string`|`<nil>`
|remoteName|The namespace name to be sent in plugin calls, if it differs from namespace name|`string`|`<nil>`

## node

Expand Down
6 changes: 6 additions & 0 deletions internal/coreconfig/coreconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ const (
PluginConfigName = "name"
// PluginConfigType is the type of the plugin to be loaded
PluginConfigType = "type"
// NamespaceMode is a the configured mode of a namespace
NamespaceMode = "mode"
// NamespaceRemoteName is a the configured mode of a namespace
shorsher marked this conversation as resolved.
Show resolved Hide resolved
NamespaceRemoteName = "remoteName"
// NamespacePlugins is a the list of namespace plugins
shorsher marked this conversation as resolved.
Show resolved Hide resolved
NamespacePlugins = "plugins"
// NamespaceName is a short name for a pre-defined namespace
NamespaceName = "name"
// NamespaceName is a long description for a pre-defined namespace
Expand Down
3 changes: 3 additions & 0 deletions internal/coremsgs/en_config_descriptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,9 @@ var (
ConfigNamespacesDefault = ffc("config.namespaces.default", "The default namespace - must be in the predefined list", i18n.StringType)
ConfigNamespacesPredefined = ffc("config.namespaces.predefined", "A list of namespaces to ensure exists, without requiring a broadcast from the network", "List "+i18n.StringType)
ConfigNamespacesPredefinedName = ffc("config.namespaces.predefined[].name", "The name of the namespace (must be unique)", i18n.StringType)
ConfigNamespacesPredefinedPlugins = ffc("config.namespaces.predefined[].plugins", "The list of plugins for this namespace", i18n.StringType)
ConfigNamespacesPredefinedMode = ffc("config.namespaces.predefined[].mode", "The namespace mode", i18n.StringType)
shorsher marked this conversation as resolved.
Show resolved Hide resolved
ConfigNamespacesPredefinedRemoteName = ffc("config.namespaces.predefined[].remoteName", "The namespace name to be sent in plugin calls, if it differs from namespace name", i18n.StringType)
ConfigNamespacesPredefinedDescription = ffc("config.namespaces.predefined[].description", "A description for the namespace", i18n.StringType)

ConfigNodeDescription = ffc("config.node.description", "The description of this FireFly node", i18n.StringType)
Expand Down
421 changes: 214 additions & 207 deletions internal/coremsgs/en_error_messages.go

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions internal/namespace/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,12 @@ var (
func InitConfig(withDefaults bool) {
namespacePredefined.AddKnownKey(coreconfig.NamespaceName)
namespacePredefined.AddKnownKey(coreconfig.NamespaceDescription)
namespacePredefined.AddKnownKey(coreconfig.NamespaceRemoteName)
namespacePredefined.AddKnownKey(coreconfig.NamespacePlugins)
// do we need to set the default mode?
shorsher marked this conversation as resolved.
Show resolved Hide resolved
if withDefaults {
namespaceConfig.AddKnownKey(NamespacePredefined+".0."+coreconfig.NamespaceName, "default")
namespaceConfig.AddKnownKey(NamespacePredefined+".0."+coreconfig.NamespaceDescription, "Default predefined namespace")
namespaceConfig.AddKnownKey(NamespacePredefined+".0."+coreconfig.NamespaceMode, "multiparty")
shorsher marked this conversation as resolved.
Show resolved Hide resolved
}
}
153 changes: 147 additions & 6 deletions internal/namespace/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,20 @@ package namespace
import (
"context"
"fmt"
"strings"

"github.com/hyperledger/firefly-common/pkg/config"
"github.com/hyperledger/firefly-common/pkg/fftypes"
"github.com/hyperledger/firefly-common/pkg/i18n"
"github.com/hyperledger/firefly-common/pkg/log"
"github.com/hyperledger/firefly/internal/coreconfig"
"github.com/hyperledger/firefly/internal/coremsgs"
"github.com/hyperledger/firefly/pkg/blockchain"
"github.com/hyperledger/firefly/pkg/core"
"github.com/hyperledger/firefly/pkg/database"
"github.com/hyperledger/firefly/pkg/dataexchange"
"github.com/hyperledger/firefly/pkg/sharedstorage"
"github.com/hyperledger/firefly/pkg/tokens"
)

type Manager interface {
Expand All @@ -36,14 +41,24 @@ type Manager interface {
}

type namespaceManager struct {
ctx context.Context
nsConfig map[string]config.Section
ctx context.Context
nsConfig map[string]config.Section
bcPlugins map[string]blockchain.Plugin
dbPlugins map[string]database.Plugin
dxPlugins map[string]dataexchange.Plugin
ssPlugins map[string]sharedstorage.Plugin
tokensPlugins map[string]tokens.Plugin
}

func NewNamespaceManager(ctx context.Context) Manager {
func NewNamespaceManager(ctx context.Context, bc map[string]blockchain.Plugin, db map[string]database.Plugin, dx map[string]dataexchange.Plugin, ss map[string]sharedstorage.Plugin, tokens map[string]tokens.Plugin) Manager {
nm := &namespaceManager{
ctx: ctx,
nsConfig: buildNamespaceMap(ctx),
ctx: ctx,
nsConfig: buildNamespaceMap(ctx),
bcPlugins: bc,
dbPlugins: db,
dxPlugins: dx,
ssPlugins: ss,
tokensPlugins: tokens,
}
return nm
}
Expand Down Expand Up @@ -80,7 +95,7 @@ func (nm *namespaceManager) getPredefinedNamespaces(ctx context.Context) ([]*cor
i := 0
foundDefault := false
for name, nsObject := range nm.nsConfig {
if err := core.ValidateFFNameField(ctx, name, fmt.Sprintf("namespaces.predefined[%d].name", i)); err != nil {
if err := nm.validateNamespaceConfig(ctx, name, i, nsObject); err != nil {
return nil, err
}
i++
Expand Down Expand Up @@ -124,3 +139,129 @@ func (nm *namespaceManager) initNamespaces(ctx context.Context, di database.Plug
}
return nil
}

func (nm *namespaceManager) validateNamespaceConfig(ctx context.Context, name string, index int, conf config.Section) error {
if err := core.ValidateFFNameField(ctx, name, fmt.Sprintf("namespaces.predefined[%d].name", index)); err != nil {
return err
}

if strings.ToLower(name) == "ff_system" || strings.ToLower(conf.GetString(coreconfig.NamespaceRemoteName)) == "ff_system" {
shorsher marked this conversation as resolved.
Show resolved Hide resolved
return i18n.NewError(ctx, coremsgs.MsgFFSystemReservedName)
}

mode := conf.GetString(coreconfig.NamespaceMode)
plugins := conf.GetStringSlice(coreconfig.NamespacePlugins)

// If the no plugins are found when querying the config, assume older config file
if len(plugins) == 0 {
// Still check to ensure at least one bc, dx, ss, and db plugin exist
shorsher marked this conversation as resolved.
Show resolved Hide resolved
if len(nm.bcPlugins) == 0 || len(nm.dxPlugins) == 0 || len(nm.ssPlugins) == 0 || len(nm.dbPlugins) == 0 {
return i18n.NewError(ctx, coremsgs.MsgNamespaceMultipartyConfiguration, name)
}
return nil
}

switch mode {
// Multiparty is the default mode when none is provided
case "multiparty", "":
shorsher marked this conversation as resolved.
Show resolved Hide resolved
if err := nm.validateMultiPartyConfig(ctx, name, plugins); err != nil {
return err
}
case "gateway":
if err := nm.validateGatewayConfig(ctx, name, plugins); err != nil {
return err
}
default:
return i18n.NewError(ctx, coremsgs.MsgInvalidNamespaceMode, name)
}
return nil
}

func (nm *namespaceManager) validateMultiPartyConfig(ctx context.Context, name string, plugins []string) error {
var dbPlugin bool
var ssPlugin bool
var dxPlugin bool
var bcPlugin bool

for _, pluginName := range plugins {
if _, ok := nm.bcPlugins[pluginName]; ok {
if bcPlugin {
return i18n.NewError(ctx, coremsgs.MsgNamespaceGatewayMultiplePluginType, name, "blockchain")
}
bcPlugin = true
continue
}
if _, ok := nm.dxPlugins[pluginName]; ok {
if dxPlugin {
return i18n.NewError(ctx, coremsgs.MsgNamespaceGatewayMultiplePluginType, name, "dataexchange")
}
dxPlugin = true
continue
}
if _, ok := nm.ssPlugins[pluginName]; ok {
if ssPlugin {
return i18n.NewError(ctx, coremsgs.MsgNamespaceGatewayMultiplePluginType, name, "sharedstorage")
}
ssPlugin = true
continue
}
if _, ok := nm.dbPlugins[pluginName]; ok {
if dbPlugin {
return i18n.NewError(ctx, coremsgs.MsgNamespaceGatewayMultiplePluginType, name, "database")
}
dbPlugin = true
continue
}
if _, ok := nm.tokensPlugins[pluginName]; ok {
continue
}

return i18n.NewError(ctx, coremsgs.MsgNamespaceUnknownPlugin, name, pluginName)
}

if !dbPlugin || !ssPlugin || !dxPlugin || !bcPlugin {
fmt.Printf("plugins: %+v\n", nm)
shorsher marked this conversation as resolved.
Show resolved Hide resolved
return i18n.NewError(ctx, coremsgs.MsgNamespaceMultipartyConfiguration, name)
}

return nil
}

func (nm *namespaceManager) validateGatewayConfig(ctx context.Context, name string, plugins []string) error {
var dbPlugin bool
var bcPlugin bool

for _, pluginName := range plugins {
if _, ok := nm.bcPlugins[pluginName]; ok {
if bcPlugin {
return i18n.NewError(ctx, coremsgs.MsgNamespaceGatewayMultiplePluginType, name, "blockchain")
}
bcPlugin = true
continue
}
if _, ok := nm.dxPlugins[pluginName]; ok {
return i18n.NewError(ctx, coremsgs.MsgNamespaceGatewayInvalidPlugins, name)
}
if _, ok := nm.ssPlugins[pluginName]; ok {
return i18n.NewError(ctx, coremsgs.MsgNamespaceGatewayInvalidPlugins, name)
}
if _, ok := nm.dbPlugins[pluginName]; ok {
if dbPlugin {
return i18n.NewError(ctx, coremsgs.MsgNamespaceGatewayMultiplePluginType, name, "database")
}
dbPlugin = true
continue
}
if _, ok := nm.tokensPlugins[pluginName]; ok {
continue
}

return i18n.NewError(ctx, coremsgs.MsgNamespaceUnknownPlugin, name, pluginName)
}

if !dbPlugin {
return i18n.NewError(ctx, coremsgs.MsgNamespaceGatewayNoDB, name)
}

return nil
}
Loading