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

[Release-2.2 BP FAB-2643] #3534

Merged
merged 3 commits into from
Jul 26, 2022
Merged
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
8 changes: 8 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", `{"lscc": "true", "escc": "true", "vscc": "true", "cscc": "true", "somecc": "enabled"}`)

config := chaincode.GlobalConfig()
Expect(config.TLSEnabled).To(BeTrue())
Expand All @@ -46,6 +47,13 @@ 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,
"lscc": true,
"escc": true,
"vscc": true,
"cscc": true,
}))
})

Context("when an invalid keepalive is configured", func() {
Expand Down
32 changes: 27 additions & 5 deletions core/peer/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ package peer
import (
"crypto/tls"
"fmt"
"gopkg.in/yaml.v2"
"io/ioutil"
"net"
"path/filepath"
Expand Down Expand Up @@ -275,20 +276,20 @@ func (c *Config) load() error {
}

c.ChaincodePull = viper.GetBool("chaincode.pull")
var externalBuilders []ExternalBuilder
err = viper.UnmarshalKey("chaincode.externalBuilders", &externalBuilders)

c.ExternalBuilders, err = getExternalBuildersConfig()
if err != nil {
return err
return errors.Wrap(err, "getting external builders config")
}
c.ExternalBuilders = externalBuilders

for builderIndex, builder := range c.ExternalBuilders {
if builder.Path == "" {
return fmt.Errorf("invalid external builder configuration, path attribute missing in one or more builders")
}
if builder.Name == "" {
return fmt.Errorf("external builder at path %s has no name attribute", builder.Path)
}
if builder.Environment != nil && builder.PropagateEnvironment == nil {
if builder.Environment != nil && len(builder.PropagateEnvironment) == 0 {
c.ExternalBuilders[builderIndex].PropagateEnvironment = builder.Environment
}
}
Expand Down Expand Up @@ -475,3 +476,24 @@ func GetClientCertificate() (tls.Certificate, error) {
}
return cert, nil
}

// this method is needed because old viper (< 1.1.0) has bugs in automatic env option
func getExternalBuildersConfig() ([]ExternalBuilder, error) {
var externalBuilders []ExternalBuilder

if viper.IsSet("chaincode.externalBuilders") {
// when defined as an env var the value is string, then we need to parse it as a yaml string
if extBuildersString := viper.GetString("chaincode.externalBuilders"); extBuildersString != "" {
if err := yaml.UnmarshalStrict([]byte(extBuildersString), &externalBuilders); err != nil {
return nil, errors.Wrap(err, "unmarshalling 'chaincode.externalBuilders' into yaml")
}
} else {
// otherwise we expect it to be a map, core.yaml is unmarshalled into a generic struct map[string]interface and then mapstructure.Decode has to be used.
if err := viper.UnmarshalKey("chaincode.externalBuilders", &externalBuilders); err != nil {
return nil, err
}
}
}

return externalBuilders, nil
}
74 changes: 63 additions & 11 deletions core/peer/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package peer

import (
"crypto/tls"
"gopkg.in/yaml.v2"
"io/ioutil"
"net"
"os"
Expand Down Expand Up @@ -308,7 +309,8 @@ func TestGlobalConfig(t *testing.T) {
viper.Set("metrics.statsd.prefix", "testPrefix")

viper.Set("chaincode.pull", false)
viper.Set("chaincode.externalBuilders", &[]ExternalBuilder{

extBuildersConfig, err := yaml.Marshal([]ExternalBuilder{
{
Path: "relative/plugin_dir",
Name: "relative",
Expand All @@ -318,6 +320,8 @@ func TestGlobalConfig(t *testing.T) {
Name: "absolute",
},
})
require.NoError(t, err)
viper.Set("chaincode.externalBuilders", extBuildersConfig)

coreConfig, err := GlobalConfig()
assert.NoError(t, err)
Expand Down Expand Up @@ -352,12 +356,16 @@ func TestGlobalConfig(t *testing.T) {
ChaincodePull: false,
ExternalBuilders: []ExternalBuilder{
{
Path: "relative/plugin_dir",
Name: "relative",
Path: "relative/plugin_dir",
Name: "relative",
PropagateEnvironment: []string{},
Environment: []string{},
},
{
Path: "/absolute/plugin_dir",
Name: "absolute",
Path: "/absolute/plugin_dir",
Name: "absolute",
PropagateEnvironment: []string{},
Environment: []string{},
},
},
OperationsListenAddress: "127.0.0.1:9443",
Expand Down Expand Up @@ -405,15 +413,17 @@ func TestGlobalConfigDefault(t *testing.T) {
func TestPropagateEnvironment(t *testing.T) {
defer viper.Reset()
viper.Set("peer.address", "localhost:8080")
viper.Set("chaincode.externalBuilders", &[]ExternalBuilder{
viper.Set("chaincode.externalBuilders", []ExternalBuilder{
{
Name: "testName",
Environment: []string{"KEY=VALUE"},
Path: "/testPath",
Name: "testName",
Environment: []string{"KEY=VALUE"},
PropagateEnvironment: []string{},
Path: "/testPath",
},
{
Name: "testName",
PropagateEnvironment: []string{"KEY=VALUE"},
Environment: []string{},
Path: "/testPath",
},
{
Expand Down Expand Up @@ -442,6 +452,7 @@ func TestPropagateEnvironment(t *testing.T) {
{
Name: "testName",
PropagateEnvironment: []string{"KEY=VALUE"},
Environment: []string{},
Path: "/testPath",
},
{
Expand All @@ -455,10 +466,49 @@ func TestPropagateEnvironment(t *testing.T) {
assert.Equal(t, expectedConfig, coreConfig)
}

func TestExternalBuilderConfigAsEnvVar(t *testing.T) {
defer viper.Reset()
viper.Set("peer.address", "localhost:8080")
extBuildersConfig, err := yaml.Marshal([]ExternalBuilder{
{
Path: "relative/plugin_dir",
Name: "relative",
PropagateEnvironment: []string{"ENVVAR_NAME_TO_PROPAGATE_FROM_PEER", "GOPROXY"},
},
{
Path: "/absolute/plugin_dir",
Name: "absolute",
PropagateEnvironment: nil,
},
})
require.NoError(t, err)

viper.Set("chaincode.externalBuilders", extBuildersConfig)

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"},
Environment: []string{},
},
{
Path: "/absolute/plugin_dir",
Name: "absolute",
PropagateEnvironment: []string{},
Environment: []string{},
},
}, coreConfig.ExternalBuilders)
}

func TestMissingExternalBuilderPath(t *testing.T) {
defer viper.Reset()
viper.Set("peer.address", "localhost:8080")
viper.Set("chaincode.externalBuilders", &[]ExternalBuilder{

viper.Set("chaincode.externalBuilders", []ExternalBuilder{
{
Name: "testName",
},
Expand All @@ -470,11 +520,13 @@ func TestMissingExternalBuilderPath(t *testing.T) {
func TestMissingExternalBuilderName(t *testing.T) {
defer viper.Reset()
viper.Set("peer.address", "localhost:8080")
viper.Set("chaincode.externalBuilders", &[]ExternalBuilder{

viper.Set("chaincode.externalBuilders", []ExternalBuilder{
{
Path: "relative/plugin_dir",
},
})

_, err := GlobalConfig()
assert.EqualError(t, err, "external builder at path relative/plugin_dir has no name attribute")
}
1 change: 1 addition & 0 deletions sampleconfig/core.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -602,6 +602,7 @@ chaincode:
keepalive: 0

# enabled system chaincodes
# To override this property via env variable use CORE_CHAINCODE_SYSTEM: {"xxx:": "enable"}
system:
_lifecycle: enable
cscc: enable
Expand Down