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

FAB-18482 Unable to specify peer's chaincode.externalBuilders as an env variable (backport #2643) #2752

Closed
wants to merge 1 commit into from
Closed
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
16 changes: 16 additions & 0 deletions common/viperutil/config_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -352,3 +352,19 @@ func EnhancedExactUnmarshal(v *viper.Viper, 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)
Copy link
Contributor

Choose a reason for hiding this comment

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

Looks like the yaml import is not there in release-2.2.

}
}
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"
"github.com/pkg/errors"
Expand Down Expand Up @@ -276,10 +277,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 @@ -445,6 +445,26 @@ func TestPropagateEnvironment(t *testing.T) {
assert.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 @@ -557,6 +557,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