From b5e0d27b855242d51d4fdc7c422a3f7297c51b60 Mon Sep 17 00:00:00 2001 From: Vladyslav Kopaihorodskyi Date: Wed, 30 Jun 2021 22:38:20 +0300 Subject: [PATCH] Added a possibility to override chaincode.externalBuilders via env variable (#2643) Signed-off-by: Vladyslav Kopaihorodskyi --- common/viperutil/config_util.go | 16 ++++++++++++++++ core/chaincode/config_test.go | 2 ++ core/peer/config.go | 5 ++++- core/peer/config_test.go | 20 ++++++++++++++++++++ sampleconfig/core.yaml | 1 + 5 files changed, 43 insertions(+), 1 deletion(-) diff --git a/common/viperutil/config_util.go b/common/viperutil/config_util.go index 5f862a7dbcf..3986fb533be 100644 --- a/common/viperutil/config_util.go +++ b/common/viperutil/config_util.go @@ -449,3 +449,19 @@ func (c *ConfigParser) EnhancedExactUnmarshal(output interface{}) error { } return decoder.Decode(leafKeys) } + +// YamlStringToStructHook is a hook for viper(viper.Unmarshal(*,*, here)), it is able to parse a string of minified yaml into a slice of structs +func YamlStringToStructHook(m interface{}) func(rf reflect.Kind, rt reflect.Kind, data interface{}) (interface{}, error) { + return func(rf reflect.Kind, rt reflect.Kind, data interface{}) (interface{}, error) { + if rf != reflect.String || rt != reflect.Slice { + return data, nil + } + + raw := data.(string) + if raw == "" { + return m, nil + } + + return m, yaml.UnmarshalStrict([]byte(raw), &m) + } +} diff --git a/core/chaincode/config_test.go b/core/chaincode/config_test.go index dae7b66253f..70cf4fb193b 100644 --- a/core/chaincode/config_test.go +++ b/core/chaincode/config_test.go @@ -36,6 +36,7 @@ var _ = Describe("Config", func() { viper.Set("chaincode.logging.format", "test-chaincode-logging-format") viper.Set("chaincode.logging.level", "warning") viper.Set("chaincode.logging.shim", "warning") + viper.Set("chaincode.system.somecc", true) config := chaincode.GlobalConfig() Expect(config.TLSEnabled).To(BeTrue()) @@ -46,6 +47,7 @@ var _ = Describe("Config", func() { Expect(config.LogFormat).To(Equal("test-chaincode-logging-format")) Expect(config.LogLevel).To(Equal("warn")) Expect(config.ShimLogLevel).To(Equal("warn")) + Expect(config.SCCAllowlist).To(Equal(map[string]bool{"somecc": true})) }) Context("when an invalid keepalive is configured", func() { diff --git a/core/peer/config.go b/core/peer/config.go index 82af01b0c9f..a14830a872c 100644 --- a/core/peer/config.go +++ b/core/peer/config.go @@ -28,6 +28,7 @@ import ( "runtime" "time" + "github.com/hyperledger/fabric/common/viperutil" "github.com/hyperledger/fabric/core/config" "github.com/hyperledger/fabric/internal/pkg/comm" gatewayconfig "github.com/hyperledger/fabric/internal/pkg/gateway/config" @@ -286,10 +287,12 @@ func (c *Config) load() error { c.ChaincodePull = viper.GetBool("chaincode.pull") var externalBuilders []ExternalBuilder - err = viper.UnmarshalKey("chaincode.externalBuilders", &externalBuilders) + + err = viper.UnmarshalKey("chaincode.externalBuilders", &externalBuilders, viper.DecodeHook(viperutil.YamlStringToStructHook(externalBuilders))) if err != nil { return err } + c.ExternalBuilders = externalBuilders for builderIndex, builder := range c.ExternalBuilders { if builder.Path == "" { diff --git a/core/peer/config_test.go b/core/peer/config_test.go index eaa8b408438..8a88d8562d4 100644 --- a/core/peer/config_test.go +++ b/core/peer/config_test.go @@ -456,6 +456,26 @@ func TestPropagateEnvironment(t *testing.T) { require.Equal(t, expectedConfig, coreConfig) } +func TestExternalBuilderConfigAsEnvVar(t *testing.T) { + defer viper.Reset() + viper.Set("peer.address", "localhost:8080") + viper.Set("chaincode.externalBuilders", "[{name: relative, path: relative/plugin_dir, propagateEnvironment: [ENVVAR_NAME_TO_PROPAGATE_FROM_PEER, GOPROXY]}, {name: absolute, path: /absolute/plugin_dir}]") + coreConfig, err := GlobalConfig() + require.NoError(t, err) + + require.Equal(t, []ExternalBuilder{ + { + Path: "relative/plugin_dir", + Name: "relative", + PropagateEnvironment: []string{"ENVVAR_NAME_TO_PROPAGATE_FROM_PEER", "GOPROXY"}, + }, + { + Path: "/absolute/plugin_dir", + Name: "absolute", + }, + }, coreConfig.ExternalBuilders) +} + func TestMissingExternalBuilderPath(t *testing.T) { defer viper.Reset() viper.Set("peer.address", "localhost:8080") diff --git a/sampleconfig/core.yaml b/sampleconfig/core.yaml index 3486301b377..fffe1228534 100644 --- a/sampleconfig/core.yaml +++ b/sampleconfig/core.yaml @@ -562,6 +562,7 @@ chaincode: # List of directories to treat as external builders and launchers for # chaincode. The external builder detection processing will iterate over the # builders in the order specified below. + # To override this property via env variable use CORE_CHAINCODE_EXTERNALBUILDERS: [{name: x, path: dir1}, {name: y, path: dir2}] externalBuilders: [] # - path: /path/to/directory # name: descriptive-builder-name