Skip to content

Commit 90673b7

Browse files
kopaygorodskydenyeart
authored andcommitted
Added a possibility to override chaincode.externalBuilders via env variable (#2643)
Signed-off-by: Vladyslav Kopaihorodskyi <[email protected]> (cherry picked from commit b5e0d27)
1 parent 43ef319 commit 90673b7

File tree

5 files changed

+43
-1
lines changed

5 files changed

+43
-1
lines changed

common/viperutil/config_util.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,3 +449,19 @@ func (c *ConfigParser) EnhancedExactUnmarshal(output interface{}) error {
449449
}
450450
return decoder.Decode(leafKeys)
451451
}
452+
453+
// YamlStringToStructHook is a hook for viper(viper.Unmarshal(*,*, here)), it is able to parse a string of minified yaml into a slice of structs
454+
func YamlStringToStructHook(m interface{}) func(rf reflect.Kind, rt reflect.Kind, data interface{}) (interface{}, error) {
455+
return func(rf reflect.Kind, rt reflect.Kind, data interface{}) (interface{}, error) {
456+
if rf != reflect.String || rt != reflect.Slice {
457+
return data, nil
458+
}
459+
460+
raw := data.(string)
461+
if raw == "" {
462+
return m, nil
463+
}
464+
465+
return m, yaml.UnmarshalStrict([]byte(raw), &m)
466+
}
467+
}

core/chaincode/config_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ var _ = Describe("Config", func() {
3636
viper.Set("chaincode.logging.format", "test-chaincode-logging-format")
3737
viper.Set("chaincode.logging.level", "warning")
3838
viper.Set("chaincode.logging.shim", "warning")
39+
viper.Set("chaincode.system.somecc", true)
3940

4041
config := chaincode.GlobalConfig()
4142
Expect(config.TLSEnabled).To(BeTrue())
@@ -46,6 +47,7 @@ var _ = Describe("Config", func() {
4647
Expect(config.LogFormat).To(Equal("test-chaincode-logging-format"))
4748
Expect(config.LogLevel).To(Equal("warn"))
4849
Expect(config.ShimLogLevel).To(Equal("warn"))
50+
Expect(config.SCCAllowlist).To(Equal(map[string]bool{"somecc": true}))
4951
})
5052

5153
Context("when an invalid keepalive is configured", func() {

core/peer/config.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
"runtime"
2929
"time"
3030

31+
"github.com/hyperledger/fabric/common/viperutil"
3132
"github.com/hyperledger/fabric/core/config"
3233
"github.com/hyperledger/fabric/internal/pkg/comm"
3334
"github.com/pkg/errors"
@@ -276,10 +277,12 @@ func (c *Config) load() error {
276277

277278
c.ChaincodePull = viper.GetBool("chaincode.pull")
278279
var externalBuilders []ExternalBuilder
279-
err = viper.UnmarshalKey("chaincode.externalBuilders", &externalBuilders)
280+
281+
err = viper.UnmarshalKey("chaincode.externalBuilders", &externalBuilders, viper.DecodeHook(viperutil.YamlStringToStructHook(externalBuilders)))
280282
if err != nil {
281283
return err
282284
}
285+
283286
c.ExternalBuilders = externalBuilders
284287
for builderIndex, builder := range c.ExternalBuilders {
285288
if builder.Path == "" {

core/peer/config_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,26 @@ func TestPropagateEnvironment(t *testing.T) {
445445
require.Equal(t, expectedConfig, coreConfig)
446446
}
447447

448+
func TestExternalBuilderConfigAsEnvVar(t *testing.T) {
449+
defer viper.Reset()
450+
viper.Set("peer.address", "localhost:8080")
451+
viper.Set("chaincode.externalBuilders", "[{name: relative, path: relative/plugin_dir, propagateEnvironment: [ENVVAR_NAME_TO_PROPAGATE_FROM_PEER, GOPROXY]}, {name: absolute, path: /absolute/plugin_dir}]")
452+
coreConfig, err := GlobalConfig()
453+
require.NoError(t, err)
454+
455+
require.Equal(t, []ExternalBuilder{
456+
{
457+
Path: "relative/plugin_dir",
458+
Name: "relative",
459+
PropagateEnvironment: []string{"ENVVAR_NAME_TO_PROPAGATE_FROM_PEER", "GOPROXY"},
460+
},
461+
{
462+
Path: "/absolute/plugin_dir",
463+
Name: "absolute",
464+
},
465+
}, coreConfig.ExternalBuilders)
466+
}
467+
448468
func TestMissingExternalBuilderPath(t *testing.T) {
449469
defer viper.Reset()
450470
viper.Set("peer.address", "localhost:8080")

sampleconfig/core.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -562,6 +562,7 @@ chaincode:
562562
# List of directories to treat as external builders and launchers for
563563
# chaincode. The external builder detection processing will iterate over the
564564
# builders in the order specified below.
565+
# To override this property via env variable use CORE_CHAINCODE_EXTERNALBUILDERS: [{name: x, path: dir1}, {name: y, path: dir2}]
565566
externalBuilders: []
566567
# - path: /path/to/directory
567568
# name: descriptive-builder-name

0 commit comments

Comments
 (0)