Skip to content

Commit 0616a9d

Browse files
committed
[FAB-2883] Add option to build without PKCS11 support
https://jira.hyperledger.org/browse/FAB-2883 This patch enables the option to build components without PKCS11 support. The reason is due to the fact that PKCS11 support adds a dependency on libtool which makes it difficult to build binaries locally. Additonally, tools like configtxgen don't actually require PKCS11 support but due to their dependency on bccsp PKCS11 was being included. The following changes were made: - bccsp/factory - added a build tag "nopkcs11" and rearranged code into 2 files - pkcs11.go and nopkcs11.go - which use the tag. The default is to build with PKCS11 support - added a GO_TAGS variable to Makefile which is used for build/bin/* targets - modified the configtxgen target to always build without PKCS11 support Change-Id: I2d983d8c71eb6c214ad0d9e4d67e73310126afa2 Signed-off-by: Gari Singh <[email protected]>
1 parent 397f5de commit 0616a9d

File tree

6 files changed

+157
-57
lines changed

6 files changed

+157
-57
lines changed

Makefile

+5-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#
1919
# - all (default) - builds all targets and runs all tests/checks
2020
# - checks - runs all tests/checks
21+
# - configtxgen - builds a native configtxgen binary
2122
# - peer - builds a native fabric peer binary
2223
# - orderer - builds a native fabric orderer binary
2324
# - unit-test - runs the go-test based unit tests
@@ -58,6 +59,8 @@ METADATA_VAR += BaseDockerLabel=$(BASE_DOCKER_LABEL)
5859

5960
GO_LDFLAGS = $(patsubst %,-X $(PKGNAME)/common/metadata.%,$(METADATA_VAR))
6061

62+
GO_TAGS ?=
63+
6164
CHAINTOOL_URL ?= https://github.com/hyperledger/fabric-chaintool/releases/download/$(CHAINTOOL_RELEASE)/chaintool
6265

6366
export GO_LDFLAGS
@@ -105,6 +108,7 @@ orderer: build/bin/orderer
105108
orderer-docker: build/image/orderer/$(DUMMY)
106109

107110
.PHONY: configtxgen
111+
configtxgen: GO_TAGS+= nopkcs11
108112
configtxgen: build/bin/configtxgen
109113

110114
buildenv: build/image/buildenv/$(DUMMY)
@@ -184,7 +188,7 @@ build/image/peer/$(DUMMY): build/image/ccenv/$(DUMMY) build/image/javaenv/$(DUMM
184188
build/bin/%: $(PROJECT_FILES)
185189
@mkdir -p $(@D)
186190
@echo "$@"
187-
$(CGO_FLAGS) GOBIN=$(abspath $(@D)) go install -ldflags "$(GO_LDFLAGS)" $(pkgmap.$(@F))
191+
$(CGO_FLAGS) GOBIN=$(abspath $(@D)) go install -tags "$(GO_TAGS)" -ldflags "$(GO_LDFLAGS)" $(pkgmap.$(@F))
188192
@echo "Binary available as $@"
189193
@touch $@
190194

bccsp/factory/factory.go

-50
Original file line numberDiff line numberDiff line change
@@ -77,56 +77,6 @@ func GetBCCSP(name string) (bccsp.BCCSP, error) {
7777
return bccspMap[name], nil
7878
}
7979

80-
// InitFactories must be called before using factory interfaces
81-
// It is acceptable to call with config = nil, in which case
82-
// some defaults will get used
83-
// Error is returned only if defaultBCCSP cannot be found
84-
func InitFactories(config *FactoryOpts) error {
85-
factoriesInitOnce.Do(func() {
86-
// Take some precautions on default opts
87-
if config == nil {
88-
config = &DefaultOpts
89-
}
90-
91-
if config.ProviderName == "" {
92-
config.ProviderName = "SW"
93-
}
94-
95-
if config.SwOpts == nil {
96-
config.SwOpts = DefaultOpts.SwOpts
97-
}
98-
99-
// Initialize factories map
100-
bccspMap = make(map[string]bccsp.BCCSP)
101-
102-
// Software-Based BCCSP
103-
if config.SwOpts != nil {
104-
f := &SWFactory{}
105-
err := initBCCSP(f, config)
106-
if err != nil {
107-
factoriesInitError = fmt.Errorf("[%s]", err)
108-
}
109-
}
110-
111-
// PKCS11-Based BCCSP
112-
if config.Pkcs11Opts != nil {
113-
f := &PKCS11Factory{}
114-
err := initBCCSP(f, config)
115-
if err != nil {
116-
factoriesInitError = fmt.Errorf("%s\n[%s]", factoriesInitError, err)
117-
}
118-
}
119-
120-
var ok bool
121-
defaultBCCSP, ok = bccspMap[config.ProviderName]
122-
if !ok {
123-
factoriesInitError = fmt.Errorf("%s\nCould not find default `%s` BCCSP", factoriesInitError, config.ProviderName)
124-
}
125-
})
126-
127-
return factoriesInitError
128-
}
129-
13080
func initBCCSP(f BCCSPFactory, config *FactoryOpts) error {
13181
csp, err := f.Get(config)
13282
if err != nil {

bccsp/factory/nopkcs11.go

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// +build nopkcs11
2+
3+
/*
4+
Copyright IBM Corp. 2017 All Rights Reserved.
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
*/
18+
package factory
19+
20+
import (
21+
"fmt"
22+
23+
"github.com/hyperledger/fabric/bccsp"
24+
)
25+
26+
type FactoryOpts struct {
27+
ProviderName string `mapstructure:"default" json:"default" yaml:"Default"`
28+
SwOpts *SwOpts `mapstructure:"SW,omitempty" json:"SW,omitempty" yaml:"SwOpts"`
29+
}
30+
31+
// InitFactories must be called before using factory interfaces
32+
// It is acceptable to call with config = nil, in which case
33+
// some defaults will get used
34+
// Error is returned only if defaultBCCSP cannot be found
35+
func InitFactories(config *FactoryOpts) error {
36+
factoriesInitOnce.Do(func() {
37+
// Take some precautions on default opts
38+
if config == nil {
39+
config = &DefaultOpts
40+
}
41+
42+
if config.ProviderName == "" {
43+
config.ProviderName = "SW"
44+
}
45+
46+
if config.SwOpts == nil {
47+
config.SwOpts = DefaultOpts.SwOpts
48+
}
49+
50+
// Initialize factories map
51+
bccspMap = make(map[string]bccsp.BCCSP)
52+
53+
// Software-Based BCCSP
54+
if config.SwOpts != nil {
55+
f := &SWFactory{}
56+
err := initBCCSP(f, config)
57+
if err != nil {
58+
factoriesInitError = fmt.Errorf("[%s]", err)
59+
}
60+
}
61+
62+
var ok bool
63+
defaultBCCSP, ok = bccspMap[config.ProviderName]
64+
if !ok {
65+
factoriesInitError = fmt.Errorf("%s\nCould not find default `%s` BCCSP", factoriesInitError, config.ProviderName)
66+
}
67+
})
68+
69+
return factoriesInitError
70+
}

bccsp/factory/opts.go

-6
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,6 @@ limitations under the License.
1616
package factory
1717

1818
// DefaultOpts offers a default implementation for Opts
19-
type FactoryOpts struct {
20-
ProviderName string `mapstructure:"default" json:"default" yaml:"Default"`
21-
SwOpts *SwOpts `mapstructure:"SW,omitempty" json:"SW,omitempty" yaml:"SwOpts"`
22-
Pkcs11Opts *PKCS11Opts `mapstructure:"PKCS11,omitempty" json:"PKCS11,omitempty" yaml:"PKCS11"`
23-
}
24-
2519
var DefaultOpts = FactoryOpts{
2620
ProviderName: "SW",
2721
SwOpts: &SwOpts{

bccsp/factory/pkcs11.go

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
// +build !nopkcs11
2+
3+
/*
4+
Copyright IBM Corp. 2017 All Rights Reserved.
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
*/
18+
package factory
19+
20+
import (
21+
"fmt"
22+
23+
"github.com/hyperledger/fabric/bccsp"
24+
)
25+
26+
type FactoryOpts struct {
27+
ProviderName string `mapstructure:"default" json:"default" yaml:"Default"`
28+
SwOpts *SwOpts `mapstructure:"SW,omitempty" json:"SW,omitempty" yaml:"SwOpts"`
29+
Pkcs11Opts *PKCS11Opts `mapstructure:"PKCS11,omitempty" json:"PKCS11,omitempty" yaml:"PKCS11"`
30+
}
31+
32+
// InitFactories must be called before using factory interfaces
33+
// It is acceptable to call with config = nil, in which case
34+
// some defaults will get used
35+
// Error is returned only if defaultBCCSP cannot be found
36+
func InitFactories(config *FactoryOpts) error {
37+
factoriesInitOnce.Do(func() {
38+
// Take some precautions on default opts
39+
if config == nil {
40+
config = &DefaultOpts
41+
}
42+
43+
if config.ProviderName == "" {
44+
config.ProviderName = "SW"
45+
}
46+
47+
if config.SwOpts == nil {
48+
config.SwOpts = DefaultOpts.SwOpts
49+
}
50+
51+
// Initialize factories map
52+
bccspMap = make(map[string]bccsp.BCCSP)
53+
54+
// Software-Based BCCSP
55+
if config.SwOpts != nil {
56+
f := &SWFactory{}
57+
err := initBCCSP(f, config)
58+
if err != nil {
59+
factoriesInitError = fmt.Errorf("[%s]", err)
60+
}
61+
}
62+
63+
// PKCS11-Based BCCSP
64+
if config.Pkcs11Opts != nil {
65+
f := &PKCS11Factory{}
66+
err := initBCCSP(f, config)
67+
if err != nil {
68+
factoriesInitError = fmt.Errorf("%s\n[%s]", factoriesInitError, err)
69+
}
70+
}
71+
72+
var ok bool
73+
defaultBCCSP, ok = bccspMap[config.ProviderName]
74+
if !ok {
75+
factoriesInitError = fmt.Errorf("%s\nCould not find default `%s` BCCSP", factoriesInitError, config.ProviderName)
76+
}
77+
})
78+
79+
return factoriesInitError
80+
}

bccsp/factory/pkcs11factory.go

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// +build !nopkcs11
2+
13
/*
24
Copyright IBM Corp. 2016 All Rights Reserved.
35

0 commit comments

Comments
 (0)