Skip to content

Commit

Permalink
fix parsing of pkcs11 options
Browse files Browse the repository at this point in the history
Signed-off-by: Arne Rutjes <[email protected]>
  • Loading branch information
arner committed Sep 24, 2024
1 parent 63f2c1f commit c241fc8
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 24 deletions.
23 changes: 13 additions & 10 deletions platform/fabric/core/generic/msp/x509/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,16 @@ import (
"os"
"path/filepath"

"gopkg.in/yaml.v2"

"github.com/hyperledger-labs/fabric-smart-client/platform/fabric/core/generic/config"
"github.com/hyperledger-labs/fabric-smart-client/platform/fabric/core/generic/msp/driver"
"github.com/hyperledger-labs/fabric-smart-client/platform/view/services/flogging"
"github.com/mitchellh/mapstructure"
"github.com/pkg/errors"
)

const (
MSPType = "bccsp"
BCCSPOptField = "BCCSP"
BCCSPOptField = "bccsp" // viper converts map keys to lowercase
)

var logger = flogging.MustGetLogger("fabric-sdk.msp.x509")
Expand Down Expand Up @@ -104,13 +103,17 @@ func (f *FolderIdentityLoader) Load(manager driver.Manager, c config.MSP) error
}

func ToBCCSPOpts(boxed interface{}) (*config.BCCSP, error) {
raw, err := yaml.Marshal(boxed)
if err != nil {
return nil, errors.Wrap(err, "failed to marshal")
}
opts := &config.BCCSP{}
if err := yaml.Unmarshal(raw, opts); err != nil {
return nil, errors.Wrap(err, "failed to unmarshal")
config := &mapstructure.DecoderConfig{
WeaklyTypedInput: true, // allow pin to be a string
Result: &opts,
}
return opts, nil

decoder, err := mapstructure.NewDecoder(config)
if err != nil {
return opts, err
}

err = decoder.Decode(boxed)
return opts, err
}
6 changes: 3 additions & 3 deletions platform/fabric/core/generic/peer/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,13 +132,13 @@ func (cep *CachingClientFactory) NewClient(cc grpc.ConnectionConfig) (Client, er

type GRPCClientFactory struct {
ConfigService driver.ConfigService
Singer driver.Signer
Signer driver.Signer
}

func newFactory(configService driver.ConfigService, signer driver.Signer) *GRPCClientFactory {
return &GRPCClientFactory{
ConfigService: configService,
Singer: signer,
Signer: signer,
}
}

Expand Down Expand Up @@ -185,5 +185,5 @@ func (c *GRPCClientFactory) NewClient(cc grpc.ConnectionConfig) (Client, error)
if err != nil {
return nil, errors.WithMessage(err, "failed to create Client from config")
}
return NewGRPCClient(gClient, cc.Address, override, c.Singer.Sign), nil
return NewGRPCClient(gClient, cc.Address, override, c.Signer.Sign), nil
}
15 changes: 4 additions & 11 deletions platform/view/core/config/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,23 +138,16 @@ func (p *provider) substituteEnv() {

env := strings.Split(e, "=")
val := env[1]
if len(env) > 2 {
val = strings.Join(env[1:], "=")
}
if len(val) == 0 {
continue
}
noprefix := strings.TrimLeft(env[0], strings.ToUpper(CmdRoot)+"_")
key := strings.ToLower(strings.ReplaceAll(noprefix, "_", "."))
key, val := env[0], strings.Join(env[1:], "=")

noprefix := strings.TrimLeft(key, strings.ToUpper(CmdRoot)+"_")
key = strings.ToLower(strings.ReplaceAll(noprefix, "_", "."))

// nested key
keys := strings.Split(key, ".")
if len(keys) == 1 {
fmt.Println("applying " + env[0])
p.v.Set(key, val)
continue
}

parent := strings.Join(keys[:len(keys)-1], ".")
if !p.v.IsSet(parent) {
fmt.Println("applying " + env[0] + " - parent not found in core.yaml: " + parent)
Expand Down
21 changes: 21 additions & 0 deletions platform/view/core/config/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@ SPDX-License-Identifier: Apache-2.0
package config

import (
"fmt"
"os"
"path/filepath"
"testing"
"time"

mspdriver "github.com/hyperledger-labs/fabric-smart-client/platform/fabric/core/generic/config"
"github.com/hyperledger-labs/fabric-smart-client/platform/fabric/core/generic/msp/x509"
"github.com/hyperledger-labs/fabric-smart-client/platform/view/services/assert"
)

Expand Down Expand Up @@ -52,6 +55,24 @@ func TestReadFile(t *testing.T) {
assert.Equal(0, db.MaxOpenConns)
}

func TestMSPs(t *testing.T) {
var confs []mspdriver.MSP
p, _ := NewProvider("./testdata")
err := p.UnmarshalKey("msps", &confs)
assert.NoError(err)
fmt.Println(confs)

b, err := x509.ToBCCSPOpts(confs[0].Opts[x509.BCCSPOptField])
assert.NoError(err)
assert.NotNil(b.SW)
assert.Equal("SHA2", b.SW.Hash)
assert.NotNil(b.PKCS11)
assert.Equal(256, b.PKCS11.Security)
assert.Equal("someLabel", b.PKCS11.Label)
assert.Equal("98765432", b.PKCS11.Pin)

}

func TestEnvSubstitution(t *testing.T) {
os.Setenv("CORE_FSC_KVS_PERSISTENCE_OPTS_DATASOURCE", "new data source")
os.Setenv("CORE_STR", "new=string=with=characters.\\AND.CAPS")
Expand Down
14 changes: 14 additions & 0 deletions platform/view/core/config/testdata/core.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,17 @@ nested:
one: 1
two: 2
CAPITALS: true
msps:
- id: mymsp
opts:
BCCSP:
Default: SW
SW:
Hash: SHA2
Security: 256
PKCS11:
Library: /path/to/pkcs11_library.so
Label: someLabel
Pin: 98765432
Hash: SHA2
Security: 256

0 comments on commit c241fc8

Please sign in to comment.