Skip to content

Commit 3ee0333

Browse files
author
Volodymyr Paprotski
committed
[FAB-1647] Yaml used to configure BCCSP
This time use Yaml files in peer and orderer to configure BCCSP factory The main three changes are for the two executables: - peer - orderer - chaincode shim They each have their own config yaml file, added a BCCSP section. The BCCSP is paired with the MSP configuration directory. The SW BCCSP Filestore takes over the keystore directory. Where it gets complicated are all the tests. The tests working against MSP were easy to fix (circa patch set 8) Patch set https://gerrit.hyperledger.org/r/#/c/6235/4 introduced more uses of BCCSP. Configuring those mostly to SW/nil, except need to find an appropriate location for InitFactories(nil). Either: - many/most test cases have function called SetupTestConfig() - TestMain() otherwise Since the TestMain() updates were getting unmanageable and unscalable (everyone writting unit tests would now need to know about InitFactories()), added a concept of bootBCCSP to bccsp/factory. bootBCCSP is meant to be used by test cases that only need keyless crypto operations (i.e. SHA) or where keeping keys does not matter. Change-Id: I084d7927550e7fad7a25cf2062dc20220cf81ccd Signed-off-by: Volodymyr Paprotski <[email protected]>
1 parent 7c1934a commit 3ee0333

29 files changed

+236
-55
lines changed

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ K := $(foreach exec,$(EXECUTABLES),\
6767
GOSHIM_DEPS = $(shell ./scripts/goListFiles.sh $(PKGNAME)/core/chaincode/shim)
6868
JAVASHIM_DEPS = $(shell git ls-files core/chaincode/shim/java)
6969
PROTOS = $(shell git ls-files *.proto | grep -v vendor)
70-
MSP_SAMPLECONFIG = $(shell git ls-files msp/sampleconfig/*.pem)
70+
MSP_SAMPLECONFIG = $(shell git ls-files msp/sampleconfig/*)
7171
PROJECT_FILES = $(shell git ls-files)
7272
IMAGES = peer orderer ccenv javaenv buildenv testenv zookeeper kafka couchdb
7373

bccsp/factory/factory.go

+15-1
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,16 @@ var (
2727
// Default BCCSP
2828
defaultBCCSP bccsp.BCCSP
2929

30+
// when InitFactories has not been called yet (should only happen
31+
// in test cases), use this BCCSP temporarily
32+
bootBCCSP bccsp.BCCSP
33+
3034
// BCCSP Factories
3135
bccspMap map[string]bccsp.BCCSP
3236

3337
// factories' Sync on Initialization
3438
factoriesInitOnce sync.Once
39+
bootBCCSPInitOnce sync.Once
3540

3641
// Factories' Initialization Error
3742
factoriesInitError error
@@ -53,7 +58,16 @@ type BCCSPFactory interface {
5358
// GetDefault returns a non-ephemeral (long-term) BCCSP
5459
func GetDefault() bccsp.BCCSP {
5560
if defaultBCCSP == nil {
56-
panic("BCCSP Factory: Must call InitFactories before using BCCSP!")
61+
logger.Warning("Before using BCCSP, please call InitFactories(). Falling back to bootBCCSP.")
62+
bootBCCSPInitOnce.Do(func() {
63+
var err error
64+
f := &SWFactory{}
65+
bootBCCSP, err = f.Get(&DefaultOpts)
66+
if err != nil {
67+
panic("BCCSP Internal error, failed initialization with DefaultOpts!")
68+
}
69+
})
70+
return bootBCCSP
5771
}
5872
return defaultBCCSP
5973
}

bccsp/factory/opts.go

-3
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,6 @@ var DefaultOpts = FactoryOpts{
3232
},
3333
}
3434

35-
// FIXME! FIXME! This needs to be moved to where viper reads in configs!
36-
var myHack = InitFactories(nil)
37-
3835
// FactoryName returns the name of the provider
3936
func (o *FactoryOpts) FactoryName() string {
4037
return o.ProviderName

common/configtx/test/helper.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ const sampleOrgID = "DEFAULT"
9191

9292
// ApplicationOrgTemplate returns the SAMPLE org with MSP template
9393
func ApplicationOrgTemplate() configtx.Template {
94-
mspConf, err := msp.GetLocalMspConfig(sampleMSPPath, sampleOrgID)
94+
mspConf, err := msp.GetLocalMspConfig(sampleMSPPath, nil, sampleOrgID)
9595
if err != nil {
9696
logger.Panicf("Could not load sample MSP config: %s", err)
9797
}
@@ -100,7 +100,7 @@ func ApplicationOrgTemplate() configtx.Template {
100100

101101
// OrdererOrgTemplate returns the SAMPLE org with MSP template
102102
func OrdererOrgTemplate() configtx.Template {
103-
mspConf, err := msp.GetLocalMspConfig(sampleMSPPath, sampleOrgID)
103+
mspConf, err := msp.GetLocalMspConfig(sampleMSPPath, nil, sampleOrgID)
104104
if err != nil {
105105
logger.Panicf("Could not load sample MSP config: %s", err)
106106
}

common/configtx/tool/configtx.yaml

+12
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,18 @@ Organizations:
5252
# MSPDir is the filesystem path which contains the MSP configuration
5353
MSPDir: msp/sampleconfig
5454

55+
# BCCSP (Blockchain crypto provider): Select which crypto implementation or
56+
# library to use
57+
BCCSP:
58+
Default: SW
59+
SW:
60+
Hash: SHA3
61+
Security: 256
62+
# Location of Key Store. If this is unset, a location will
63+
# be chosen using 'MSPDir'/keystore
64+
FileKeyStore:
65+
KeyStore:
66+
5567
AnchorPeers:
5668
# AnchorPeers defines the location of peers which can be used
5769
# for cross org gossip communication. Note, this value is only

common/configtx/tool/localconfig/config.go

+3
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ import (
2727

2828
"github.com/op/go-logging"
2929
"github.com/spf13/viper"
30+
31+
bccsp "github.com/hyperledger/fabric/bccsp/factory"
3032
)
3133

3234
const (
@@ -65,6 +67,7 @@ type Organization struct {
6567
Name string
6668
ID string
6769
MSPDir string
70+
BCCSP *bccsp.FactoryOpts
6871

6972
// Note, the viper deserialization does not seem to care for
7073
// embedding of types, so we use one organization structure for

common/configtx/tool/provisional/provisional.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ func New(conf *genesisconfig.Profile) Generator {
113113
}
114114

115115
for _, org := range conf.Orderer.Organizations {
116-
mspConfig, err := msp.GetLocalMspConfig(org.MSPDir, org.ID)
116+
mspConfig, err := msp.GetLocalMspConfig(org.MSPDir, org.BCCSP, org.ID)
117117
if err != nil {
118118
logger.Panicf("Error loading MSP configuration for org %s: %s", org.Name, err)
119119
}
@@ -143,7 +143,7 @@ func New(conf *genesisconfig.Profile) Generator {
143143
policies.TemplateImplicitMetaMajorityPolicy([]string{configtxapplication.GroupKey}, configvaluesmsp.AdminsPolicyKey),
144144
}
145145
for _, org := range conf.Application.Organizations {
146-
mspConfig, err := msp.GetLocalMspConfig(org.MSPDir, org.ID)
146+
mspConfig, err := msp.GetLocalMspConfig(org.MSPDir, org.BCCSP, org.ID)
147147
if err != nil {
148148
logger.Panicf("Error loading MSP configuration for org %s: %s", org.Name, err)
149149
}

common/configvalues/msp/config_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import (
2525
)
2626

2727
func TestMSPConfigManager(t *testing.T) {
28-
conf, err := msp.GetLocalMspConfig("../../../msp/sampleconfig/", "DEFAULT")
28+
conf, err := msp.GetLocalMspConfig("../../../msp/sampleconfig/", nil, "DEFAULT")
2929
assert.NoError(t, err)
3030

3131
// test success:

common/localmsp/signer_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func TestMain(m *testing.M) {
3737
mspMgrConfigDir = os.Getenv("GOPATH") + "/src/github.com/hyperledger/fabric/msp/sampleconfig/"
3838
}
3939

40-
if err := mspmgmt.LoadLocalMsp(mspMgrConfigDir, "DEFAULT"); err != nil {
40+
if err := mspmgmt.LoadLocalMsp(mspMgrConfigDir, nil, "DEFAULT"); err != nil {
4141
os.Exit(-1)
4242
}
4343

core/chaincode/config.go

+8
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ import (
2424

2525
"github.com/op/go-logging"
2626
"github.com/spf13/viper"
27+
28+
"github.com/hyperledger/fabric/bccsp/factory"
2729
)
2830

2931
// Config the config wrapper structure
@@ -71,4 +73,10 @@ func SetupTestConfig() {
7173
// Set the number of maxprocs
7274
var numProcsDesired = viper.GetInt("peer.gomaxprocs")
7375
chaincodeLogger.Debugf("setting Number of procs to %d, was %d\n", numProcsDesired, runtime.GOMAXPROCS(2))
76+
77+
// Init the BCCSP
78+
err = factory.InitFactories(nil)
79+
if err != nil {
80+
panic(fmt.Errorf("Could not initialize BCCSP Factories [%s]", err))
81+
}
7482
}

core/chaincode/shim/chaincode.go

+6
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929

3030
"github.com/golang/protobuf/proto"
3131
"github.com/golang/protobuf/ptypes/timestamp"
32+
"github.com/hyperledger/fabric/bccsp/factory"
3233
"github.com/hyperledger/fabric/core/comm"
3334
pb "github.com/hyperledger/fabric/protos/peer"
3435
"github.com/hyperledger/fabric/protos/utils"
@@ -76,6 +77,11 @@ func Start(cc Chaincode) error {
7677

7778
SetChaincodeLoggingLevel()
7879

80+
err := factory.InitFactories(&factory.DefaultOpts)
81+
if err != nil {
82+
return fmt.Errorf("Internal error, BCCSP could not be initialized with default options: %s", err)
83+
}
84+
7985
flag.StringVar(&peerAddress, "peer.address", "", "peer address")
8086

8187
flag.Parse()

core/config/config.go

+16
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ import (
2424

2525
"github.com/op/go-logging"
2626
"github.com/spf13/viper"
27+
28+
"github.com/hyperledger/fabric/bccsp/factory"
29+
"github.com/hyperledger/fabric/msp"
2730
)
2831

2932
// Config the config wrapper structure
@@ -74,4 +77,17 @@ func SetupTestConfig(pathToOpenchainYaml string) {
7477
var numProcsDesired = viper.GetInt("peer.gomaxprocs")
7578
configLogger.Debugf("setting Number of procs to %d, was %d\n", numProcsDesired, runtime.GOMAXPROCS(2))
7679

80+
// Init the BCCSP
81+
var bccspConfig *factory.FactoryOpts
82+
err = viper.UnmarshalKey("peer.BCCSP", &bccspConfig)
83+
if err != nil {
84+
bccspConfig = nil
85+
}
86+
87+
msp.SetupBCCSPKeystoreConfig(bccspConfig, viper.GetString("peer.mspConfigPath")+"/keystore")
88+
89+
err = factory.InitFactories(bccspConfig)
90+
if err != nil {
91+
panic(fmt.Errorf("Could not initialize BCCSP Factories [%s]", err))
92+
}
7793
}

core/container/config.go

+16
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ import (
2424

2525
"github.com/op/go-logging"
2626
"github.com/spf13/viper"
27+
28+
"github.com/hyperledger/fabric/bccsp/factory"
29+
"github.com/hyperledger/fabric/msp"
2730
)
2831

2932
// Config the config wrapper structure
@@ -73,4 +76,17 @@ func SetupTestConfig() {
7376
var numProcsDesired = viper.GetInt("peer.gomaxprocs")
7477
vmLogger.Debugf("setting Number of procs to %d, was %d\n", numProcsDesired, runtime.GOMAXPROCS(2))
7578

79+
// Init the BCCSP
80+
var bccspConfig *factory.FactoryOpts
81+
err = viper.UnmarshalKey("peer.BCCSP", &bccspConfig)
82+
if err != nil {
83+
bccspConfig = nil
84+
}
85+
86+
msp.SetupBCCSPKeystoreConfig(bccspConfig, viper.GetString("peer.mspConfigPath")+"/keystore")
87+
88+
err = factory.InitFactories(bccspConfig)
89+
if err != nil {
90+
panic(fmt.Errorf("Could not initialize BCCSP Factories [%s]", err))
91+
}
7692
}

core/endorser/config.go

+17
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ import (
2323

2424
"github.com/op/go-logging"
2525
"github.com/spf13/viper"
26+
27+
"github.com/hyperledger/fabric/bccsp/factory"
28+
"github.com/hyperledger/fabric/msp"
2629
)
2730

2831
// Config the config wrapper structure
@@ -68,4 +71,18 @@ func SetupTestConfig() {
6871

6972
// Set the number of maxprocs
7073
viper.GetInt("peer.gomaxprocs")
74+
75+
// Init the BCCSP
76+
var bccspConfig *factory.FactoryOpts
77+
err = viper.UnmarshalKey("peer.BCCSP", &bccspConfig)
78+
if err != nil {
79+
bccspConfig = nil
80+
}
81+
82+
msp.SetupBCCSPKeystoreConfig(bccspConfig, viper.GetString("peer.mspConfigPath")+"/keystore")
83+
84+
err = factory.InitFactories(bccspConfig)
85+
if err != nil {
86+
panic(fmt.Errorf("Could not initialize BCCSP Factories [%s]", err))
87+
}
7188
}

core/endorser/endorser_test.yaml

+10
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,16 @@ peer:
322322
enabled: false
323323
listenAddress: 0.0.0.0:6060
324324

325+
# BCCSP (Blockchain crypto provider): Select which crypto implementation or
326+
# library to use
327+
BCCSP:
328+
Default: SW
329+
SW:
330+
Hash: SHA3
331+
Security: 256
332+
FileKeyStore:
333+
KeyStore:
334+
325335
###############################################################################
326336
#
327337
# VM section

events/config.go

+17
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ import (
2424

2525
"github.com/op/go-logging"
2626
"github.com/spf13/viper"
27+
28+
"github.com/hyperledger/fabric/bccsp/factory"
29+
"github.com/hyperledger/fabric/msp"
2730
)
2831

2932
// Config the config wrapper structure
@@ -64,4 +67,18 @@ func SetupTestConfig() {
6467
}
6568

6669
SetupTestLogging()
70+
71+
// Init the BCCSP
72+
var bccspConfig *factory.FactoryOpts
73+
err = viper.UnmarshalKey("peer.BCCSP", &bccspConfig)
74+
if err != nil {
75+
bccspConfig = nil
76+
}
77+
78+
msp.SetupBCCSPKeystoreConfig(bccspConfig, viper.GetString("peer.mspConfigPath")+"/keystore")
79+
80+
err = factory.InitFactories(bccspConfig)
81+
if err != nil {
82+
panic(fmt.Errorf("Could not initialize BCCSP Factories [%s]", err))
83+
}
6784
}

msp/configbuilder.go

+34-13
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"encoding/pem"
2626
"path/filepath"
2727

28+
"github.com/hyperledger/fabric/bccsp/factory"
2829
"github.com/hyperledger/fabric/protos/msp"
2930
)
3031

@@ -87,13 +88,39 @@ const (
8788
intermediatecerts = "intermediatecerts"
8889
)
8990

90-
func GetLocalMspConfig(dir string, ID string) (*msp.MSPConfig, error) {
91+
func SetupBCCSPKeystoreConfig(bccspConfig *factory.FactoryOpts, keystoreDir string) {
92+
if bccspConfig == nil {
93+
bccspConfig = &factory.DefaultOpts
94+
}
95+
96+
if bccspConfig.ProviderName == "SW" {
97+
if bccspConfig.SwOpts == nil {
98+
bccspConfig.SwOpts = factory.DefaultOpts.SwOpts
99+
}
100+
101+
// Only override the KeyStorePath if it was left empty
102+
if bccspConfig.SwOpts.FileKeystore == nil ||
103+
bccspConfig.SwOpts.FileKeystore.KeyStorePath == "" {
104+
bccspConfig.SwOpts.Ephemeral = false
105+
bccspConfig.SwOpts.FileKeystore = &factory.FileKeystoreOpts{KeyStorePath: keystoreDir}
106+
}
107+
}
108+
}
109+
110+
func GetLocalMspConfig(dir string, bccspConfig *factory.FactoryOpts, ID string) (*msp.MSPConfig, error) {
91111
cacertDir := filepath.Join(dir, cacerts)
92112
signcertDir := filepath.Join(dir, signcerts)
93113
admincertDir := filepath.Join(dir, admincerts)
94114
keystoreDir := filepath.Join(dir, keystore)
95115
intermediatecertsDir := filepath.Join(dir, intermediatecerts)
96116

117+
SetupBCCSPKeystoreConfig(bccspConfig, keystoreDir)
118+
119+
err := factory.InitFactories(bccspConfig)
120+
if err != nil {
121+
return nil, fmt.Errorf("Could not initialize BCCSP Factories [%s]", err)
122+
}
123+
97124
cacerts, err := getPemMaterialFromDir(cacertDir)
98125
if err != nil || len(cacerts) == 0 {
99126
return nil, fmt.Errorf("Could not load a valid ca certificate from directory %s, err %s", cacertDir, err)
@@ -109,22 +136,16 @@ func GetLocalMspConfig(dir string, ID string) (*msp.MSPConfig, error) {
109136
return nil, fmt.Errorf("Could not load a valid admin certificate from directory %s, err %s", admincertDir, err)
110137
}
111138

112-
keys, err := getPemMaterialFromDir(keystoreDir)
113-
if err != nil || len(keys) == 0 {
114-
return nil, fmt.Errorf("Could not load a valid signing key from directory %s, err %s", keystoreDir, err)
115-
}
116-
117139
intermediatecert, _ := getPemMaterialFromDir(intermediatecertsDir)
118140
// intermediate certs are not mandatory
119141

120-
// FIXME: for now we're making the following assumptions
121-
// 1) there is exactly one signing cert
122-
// 2) there is exactly one signing key
123-
// 3) the cert and the key match
124-
125-
keyinfo := &msp.KeyInfo{KeyIdentifier: "PEER", KeyMaterial: keys[0]}
142+
/* FIXME: for now we're making the following assumptions
143+
1) there is exactly one signing cert
144+
2) BCCSP's KeyStore has the the private key that matches SKI of
145+
signing cert
146+
*/
126147

127-
sigid := &msp.SigningIdentityInfo{PublicSigner: signcert[0], PrivateSigner: keyinfo}
148+
sigid := &msp.SigningIdentityInfo{PublicSigner: signcert[0], PrivateSigner: nil}
128149

129150
fmspconf := &msp.FabricMSPConfig{
130151
Admins: admincert,

0 commit comments

Comments
 (0)