Skip to content

Commit

Permalink
Added a possibility to override chaincode.externalBuilders via env va…
Browse files Browse the repository at this point in the history
…riable (hyperledger#2643)

Signed-off-by: Vladyslav Kopaihorodskyi <vlad.kopaygorodsky@gmail.com>
  • Loading branch information
kopaygorodsky authored Jun 30, 2021
1 parent 1f1c303 commit b5e0d27
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 1 deletion.
16 changes: 16 additions & 0 deletions common/viperutil/config_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
2 changes: 2 additions & 0 deletions core/chaincode/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -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() {
Expand Down
5 changes: 4 additions & 1 deletion core/peer/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 == "" {
Expand Down
20 changes: 20 additions & 0 deletions core/peer/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
1 change: 1 addition & 0 deletions sampleconfig/core.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit b5e0d27

Please sign in to comment.