Skip to content

Commit

Permalink
[FAB-2883] Add option to build without PKCS11 support
Browse files Browse the repository at this point in the history
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 <gari.r.singh@gmail.com>
  • Loading branch information
mastersingh24 committed Apr 1, 2017
1 parent 397f5de commit 0616a9d
Show file tree
Hide file tree
Showing 6 changed files with 157 additions and 57 deletions.
6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#
# - all (default) - builds all targets and runs all tests/checks
# - checks - runs all tests/checks
# - configtxgen - builds a native configtxgen binary
# - peer - builds a native fabric peer binary
# - orderer - builds a native fabric orderer binary
# - unit-test - runs the go-test based unit tests
Expand Down Expand Up @@ -58,6 +59,8 @@ METADATA_VAR += BaseDockerLabel=$(BASE_DOCKER_LABEL)

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

GO_TAGS ?=

CHAINTOOL_URL ?= https://github.com/hyperledger/fabric-chaintool/releases/download/$(CHAINTOOL_RELEASE)/chaintool

export GO_LDFLAGS
Expand Down Expand Up @@ -105,6 +108,7 @@ orderer: build/bin/orderer
orderer-docker: build/image/orderer/$(DUMMY)

.PHONY: configtxgen
configtxgen: GO_TAGS+= nopkcs11
configtxgen: build/bin/configtxgen

buildenv: build/image/buildenv/$(DUMMY)
Expand Down Expand Up @@ -184,7 +188,7 @@ build/image/peer/$(DUMMY): build/image/ccenv/$(DUMMY) build/image/javaenv/$(DUMM
build/bin/%: $(PROJECT_FILES)
@mkdir -p $(@D)
@echo "$@"
$(CGO_FLAGS) GOBIN=$(abspath $(@D)) go install -ldflags "$(GO_LDFLAGS)" $(pkgmap.$(@F))
$(CGO_FLAGS) GOBIN=$(abspath $(@D)) go install -tags "$(GO_TAGS)" -ldflags "$(GO_LDFLAGS)" $(pkgmap.$(@F))
@echo "Binary available as $@"
@touch $@

Expand Down
50 changes: 0 additions & 50 deletions bccsp/factory/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,56 +77,6 @@ func GetBCCSP(name string) (bccsp.BCCSP, error) {
return bccspMap[name], nil
}

// InitFactories must be called before using factory interfaces
// It is acceptable to call with config = nil, in which case
// some defaults will get used
// Error is returned only if defaultBCCSP cannot be found
func InitFactories(config *FactoryOpts) error {
factoriesInitOnce.Do(func() {
// Take some precautions on default opts
if config == nil {
config = &DefaultOpts
}

if config.ProviderName == "" {
config.ProviderName = "SW"
}

if config.SwOpts == nil {
config.SwOpts = DefaultOpts.SwOpts
}

// Initialize factories map
bccspMap = make(map[string]bccsp.BCCSP)

// Software-Based BCCSP
if config.SwOpts != nil {
f := &SWFactory{}
err := initBCCSP(f, config)
if err != nil {
factoriesInitError = fmt.Errorf("[%s]", err)
}
}

// PKCS11-Based BCCSP
if config.Pkcs11Opts != nil {
f := &PKCS11Factory{}
err := initBCCSP(f, config)
if err != nil {
factoriesInitError = fmt.Errorf("%s\n[%s]", factoriesInitError, err)
}
}

var ok bool
defaultBCCSP, ok = bccspMap[config.ProviderName]
if !ok {
factoriesInitError = fmt.Errorf("%s\nCould not find default `%s` BCCSP", factoriesInitError, config.ProviderName)
}
})

return factoriesInitError
}

func initBCCSP(f BCCSPFactory, config *FactoryOpts) error {
csp, err := f.Get(config)
if err != nil {
Expand Down
70 changes: 70 additions & 0 deletions bccsp/factory/nopkcs11.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// +build nopkcs11

/*
Copyright IBM Corp. 2017 All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package factory

import (
"fmt"

"github.com/hyperledger/fabric/bccsp"
)

type FactoryOpts struct {
ProviderName string `mapstructure:"default" json:"default" yaml:"Default"`
SwOpts *SwOpts `mapstructure:"SW,omitempty" json:"SW,omitempty" yaml:"SwOpts"`
}

// InitFactories must be called before using factory interfaces
// It is acceptable to call with config = nil, in which case
// some defaults will get used
// Error is returned only if defaultBCCSP cannot be found
func InitFactories(config *FactoryOpts) error {
factoriesInitOnce.Do(func() {
// Take some precautions on default opts
if config == nil {
config = &DefaultOpts
}

if config.ProviderName == "" {
config.ProviderName = "SW"
}

if config.SwOpts == nil {
config.SwOpts = DefaultOpts.SwOpts
}

// Initialize factories map
bccspMap = make(map[string]bccsp.BCCSP)

// Software-Based BCCSP
if config.SwOpts != nil {
f := &SWFactory{}
err := initBCCSP(f, config)
if err != nil {
factoriesInitError = fmt.Errorf("[%s]", err)
}
}

var ok bool
defaultBCCSP, ok = bccspMap[config.ProviderName]
if !ok {
factoriesInitError = fmt.Errorf("%s\nCould not find default `%s` BCCSP", factoriesInitError, config.ProviderName)
}
})

return factoriesInitError
}
6 changes: 0 additions & 6 deletions bccsp/factory/opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,6 @@ limitations under the License.
package factory

// DefaultOpts offers a default implementation for Opts
type FactoryOpts struct {
ProviderName string `mapstructure:"default" json:"default" yaml:"Default"`
SwOpts *SwOpts `mapstructure:"SW,omitempty" json:"SW,omitempty" yaml:"SwOpts"`
Pkcs11Opts *PKCS11Opts `mapstructure:"PKCS11,omitempty" json:"PKCS11,omitempty" yaml:"PKCS11"`
}

var DefaultOpts = FactoryOpts{
ProviderName: "SW",
SwOpts: &SwOpts{
Expand Down
80 changes: 80 additions & 0 deletions bccsp/factory/pkcs11.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// +build !nopkcs11

/*
Copyright IBM Corp. 2017 All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package factory

import (
"fmt"

"github.com/hyperledger/fabric/bccsp"
)

type FactoryOpts struct {
ProviderName string `mapstructure:"default" json:"default" yaml:"Default"`
SwOpts *SwOpts `mapstructure:"SW,omitempty" json:"SW,omitempty" yaml:"SwOpts"`
Pkcs11Opts *PKCS11Opts `mapstructure:"PKCS11,omitempty" json:"PKCS11,omitempty" yaml:"PKCS11"`
}

// InitFactories must be called before using factory interfaces
// It is acceptable to call with config = nil, in which case
// some defaults will get used
// Error is returned only if defaultBCCSP cannot be found
func InitFactories(config *FactoryOpts) error {
factoriesInitOnce.Do(func() {
// Take some precautions on default opts
if config == nil {
config = &DefaultOpts
}

if config.ProviderName == "" {
config.ProviderName = "SW"
}

if config.SwOpts == nil {
config.SwOpts = DefaultOpts.SwOpts
}

// Initialize factories map
bccspMap = make(map[string]bccsp.BCCSP)

// Software-Based BCCSP
if config.SwOpts != nil {
f := &SWFactory{}
err := initBCCSP(f, config)
if err != nil {
factoriesInitError = fmt.Errorf("[%s]", err)
}
}

// PKCS11-Based BCCSP
if config.Pkcs11Opts != nil {
f := &PKCS11Factory{}
err := initBCCSP(f, config)
if err != nil {
factoriesInitError = fmt.Errorf("%s\n[%s]", factoriesInitError, err)
}
}

var ok bool
defaultBCCSP, ok = bccspMap[config.ProviderName]
if !ok {
factoriesInitError = fmt.Errorf("%s\nCould not find default `%s` BCCSP", factoriesInitError, config.ProviderName)
}
})

return factoriesInitError
}
2 changes: 2 additions & 0 deletions bccsp/factory/pkcs11factory.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// +build !nopkcs11

/*
Copyright IBM Corp. 2016 All Rights Reserved.
Expand Down

0 comments on commit 0616a9d

Please sign in to comment.