From 8ce107372714e3fe6b25dba09ac5c9008b85d400 Mon Sep 17 00:00:00 2001 From: Gregory Haskins Date: Thu, 13 Apr 2017 19:45:14 -0400 Subject: [PATCH] [FAB-3160] Provide config-relative path feature Introduction ====================== The primary goal of this patch is to create the notion of a "config-relative" path reference. For example, a configuration file "/etc/foo/bar.yaml" that contains a key "bat" with a value "baz/blaz/blamo" can be used to specify that "baz/blaz/blamo" should be considered relative to the configuration file itself. In this case, it would be expected to be found at /etc/foo/baz/blaz/blamo. FAB-2037 does a much more thorough job of explaining the rationale on why config-relative is considered important/good-form. This is in stark contrast to what we have today, which is a jumbled mess of assumed GOPATH relative, CWD relative, ENVVAR absolute and sometimes even ENVVAR relative. Therefore, an additional positive side-effect of this endeavor is that this patch also substantially cleans up some technical debt that had been accumulating in the tree for some time related to ad-hoc pathing, DRY violations, and just general inconsistencies in how configuration files were managed. Design Details ========================== This patch refactors the basic configuration system into the notion of a tree rooted at a configuration-path. By default, this path is $GOROOT/..../fabric/sampleconfig during dev/test and /etc/hyperledger/fabric during runtime. The root may be overridden at any time by specifying the environment variable FABRIC_CFG_PATH. (Note that this variable unifies and replaces the former PEER_CFG_PATH and ORDERER_CFG_PATH). The dev/test environment will operate out of the ./fabric/sampleconfig configuration root. The build-system will package that root into /etc/hyperledger/fabric in the runtime context with the intention of the end-user/admin/deployer replacing parts or all of the sampleconfig to suit their application. Since configuration-relative paths are now possible, the configuration files may reference other relative files and they will behave appropriately regardless of the context in which they are executed. For example, consider the files ./sampleconfig/tls/server.[crt|key]. A configuration file may contain a key "tls/server.key" and the system will properly resolve this relative file even at runtime. This is (IMO) far more natural than assuming a path is relative to the CWD of where the command is executed, which is how most of the system behaves today (or requires awkward and very specific ENVVAR overrides). This will be conducive to something like a package-installer (e.g. RPM/DEB) or a docker environment to augment/replace elements of the configuration root and to freely move the configuration around as the package/deployer sees fit. As an example, a deployment on Kubernetes might opt to volume mount /etc/hyperledger/fabric to replace the entire config, or it might just use a secrets mount on /etc/hyperledger/fabric/peer/tls. An RPM packager might opt to install the configuration files in the default /etc/hyperledger/fabric, whereas an unprivledged user might install them in ~/hyperledger. The point is, it shouldn't matter where they are and the user shouldn't need a PhD in CORE_* variables to get it to work. This is part of an overall effort to improve the user-experience as we march towards a v1.0 release. Fixes FAB-3169 as part of FAB-2037 Change-Id: I5f47f554c2f956ec2e1afebd9bd82b0bbb62892a Signed-off-by: Greg Haskins --- Makefile | 18 +- bddtests/docker-compose-next-4.yml | 5 - bddtests/orderer-3-kafka-1.yml | 2 - bddtests/regression/go/ote/README.md | 4 +- bddtests/regression/go/ote/network.json | 4 - bddtests/regression/go/ote/ote.go | 2 +- bddtests/steps/endorser_util.py | 2 +- common/config/msp/config_test.go | 5 +- common/configtx/test/helper.go | 32 +-- common/configtx/test/helper_test.go | 10 +- common/configtx/tool/localconfig/config.go | 71 +++---- .../configtx/tool/provisional/provisional.go | 38 +--- common/localmsp/signer_test.go | 13 +- core/admin.go | 4 +- core/chaincode/chaincode_support.go | 7 +- core/chaincode/exectransaction_test.go | 9 +- .../platforms/car/test/platform_test.go | 2 +- .../platforms/java/test/java_test.go | 2 +- core/chaincode/platforms/platforms.go | 5 +- core/comm/connection.go | 5 +- core/comm/connection_test.go | 4 +- .../committer/txvalidator/txvalidator_test.go | 3 +- core/common/ccpackage/ccpackage_test.go | 13 +- core/common/validation/fullflow_test.go | 3 +- core/config/config.go | 191 ++++++++++++++++++ core/container/config.go | 10 +- .../dockercontroller/dockercontroller_test.go | 4 +- core/container/util/dockerutil.go | 7 +- core/deliverservice/deliveryclient_test.go | 2 +- core/endorser/endorser_test.go | 8 +- core/endorser/endorser_test.yaml | 2 +- core/ledger/kvledger/example/main/example.go | 2 +- core/ledger/kvledger/kv_ledger_test.go | 4 +- .../marble_example/main/marble_example.go | 2 +- .../statedb/statecouchdb/statecouchdb_test.go | 3 +- .../txmgmt/txmgr/commontests/txmgr_test.go | 2 +- core/ledger/ledgerconfig/ledger_config.go | 3 +- .../ledger/ledgerconfig/ledger_config_test.go | 2 +- core/ledger/testutil/test_util.go | 12 +- core/ledger/util/couchdb/couchdb_test.go | 2 +- core/peer/config.go | 9 +- core/peer/peer_test.go | 2 +- core/scc/cscc/configure_test.go | 2 +- .../escc/endorser_onevalidsignature_test.go | 3 +- .../vscc/validator_onevalidsignature_test.go | 3 +- core/testutil/config.go | 15 +- docs/source/Setup/Chaincode-setup.rst | 2 +- docs/source/Setup/logging-control.rst | 4 +- docs/source/configtxgen.rst | 4 +- docs/source/getting_started.rst | 2 +- docs/source/ledger.rst | 2 +- docs/source/peer-chaincode-devmode.rst | 4 +- events/config.go | 10 +- events/events_test.go | 3 +- events/producer/producer_test.go | 3 +- examples/ccchecker/init.go | 27 ++- examples/e2e_cli/configtx.yaml | 6 +- examples/e2e_cli/docker-compose-no-tls.yaml | 8 +- examples/e2e_cli/docker-compose.yaml | 32 +-- examples/e2e_cli/end-to-end.rst | 2 +- examples/e2e_cli/generateCfgTrx.sh | 29 +-- gossip/comm/comm_test.go | 3 +- gossip/discovery/discovery_test.go | 3 +- gossip/election/election_test.go | 3 +- gossip/gossip/algo/pull_test.go | 5 +- gossip/integration/integration.go | 3 +- gossip/integration/integration_test.go | 5 +- gossip/service/gossip_service_test.go | 2 +- images/orderer/Dockerfile.in | 9 +- images/peer/Dockerfile.in | 9 +- images/testenv/Dockerfile.in | 12 +- msp/mgmt/deserializer_test.go | 11 +- msp/mgmt/mgmt.go | 12 +- msp/mgmt/peermsp_test.go | 18 +- msp/mgmt/testtools/config.go | 26 +-- msp/mgmt/testtools/config_test.go | 14 +- msp/msp_test.go | 9 +- orderer/localconfig/config.go | 73 +++---- orderer/localconfig/config_test.go | 9 +- orderer/network_test.go | 3 +- orderer/sbft_test.go | 8 +- peer/chaincode/upgrade_test.go | 16 +- peer/channel/create_test.go | 15 +- peer/common/common.go | 19 +- peer/gossip/sa/sa_test.go | 3 +- peer/main.go | 3 +- peer/node/start.go | 5 +- peer/node/stop.go | 4 +- protos/testutils/txtestutils.go | 31 +-- protos/utils/proputils_test.go | 3 +- .../tool => sampleconfig}/configtx.yaml | 2 +- {peer => sampleconfig}/core.yaml | 18 +- .../msp}/admincerts/admincert.pem | 0 .../msp}/cacerts/cacert.pem | 0 .../msp}/keystore/key.pem | 0 .../msp}/signcerts/peer.pem | 0 {orderer => sampleconfig}/orderer.yaml | 4 +- unit-test/docker-compose.yml | 1 - 98 files changed, 542 insertions(+), 510 deletions(-) create mode 100644 core/config/config.go rename {common/configtx/tool => sampleconfig}/configtx.yaml (99%) rename {peer => sampleconfig}/core.yaml (97%) rename {msp/sampleconfig => sampleconfig/msp}/admincerts/admincert.pem (100%) rename {msp/sampleconfig => sampleconfig/msp}/cacerts/cacert.pem (100%) rename {msp/sampleconfig => sampleconfig/msp}/keystore/key.pem (100%) rename {msp/sampleconfig => sampleconfig/msp}/signcerts/peer.pem (100%) rename {orderer => sampleconfig}/orderer.yaml (99%) diff --git a/Makefile b/Makefile index 567f72d1535..9cba54548a4 100644 --- a/Makefile +++ b/Makefile @@ -75,7 +75,6 @@ K := $(foreach exec,$(EXECUTABLES),\ GOSHIM_DEPS = $(shell ./scripts/goListFiles.sh $(PKGNAME)/core/chaincode/shim) JAVASHIM_DEPS = $(shell git ls-files core/chaincode/shim/java) PROTOS = $(shell git ls-files *.proto | grep -v vendor) -MSP_SAMPLECONFIG = $(shell git ls-files msp/sampleconfig/*) PROJECT_FILES = $(shell git ls-files) IMAGES = peer orderer ccenv javaenv buildenv testenv zookeeper kafka couchdb RELEASE_PLATFORMS = windows-amd64 darwin-amd64 linux-amd64 linux-ppc64le linux-s390x @@ -208,21 +207,14 @@ build/image/javaenv/payload: build/javashim.tar.bz2 \ build/protos.tar.bz2 \ settings.gradle build/image/peer/payload: build/docker/bin/peer \ - peer/core.yaml \ - build/msp-sampleconfig.tar.bz2 \ - common/configtx/tool/configtx.yaml + build/sampleconfig.tar.bz2 build/image/orderer/payload: build/docker/bin/orderer \ - build/msp-sampleconfig.tar.bz2 \ - orderer/orderer.yaml \ - common/configtx/tool/configtx.yaml + build/sampleconfig.tar.bz2 build/image/buildenv/payload: build/gotools.tar.bz2 \ build/docker/gotools/bin/protoc-gen-go build/image/testenv/payload: build/docker/bin/orderer \ - orderer/orderer.yaml \ - common/configtx/tool/configtx.yaml \ build/docker/bin/peer \ - peer/core.yaml \ - build/msp-sampleconfig.tar.bz2 \ + build/sampleconfig.tar.bz2 \ images/testenv/install-softhsm2.sh build/image/zookeeper/payload: images/zookeeper/docker-entrypoint.sh build/image/kafka/payload: images/kafka/docker-entrypoint.sh \ @@ -261,9 +253,11 @@ build/goshim.tar.bz2: $(GOSHIM_DEPS) @echo "Creating $@" @tar -jhc -C $(GOPATH)/src $(patsubst $(GOPATH)/src/%,%,$(GOSHIM_DEPS)) > $@ +build/sampleconfig.tar.bz2: + (cd sampleconfig && tar -jc *) > $@ + build/javashim.tar.bz2: $(JAVASHIM_DEPS) build/protos.tar.bz2: $(PROTOS) -build/msp-sampleconfig.tar.bz2: $(MSP_SAMPLECONFIG) build/%.tar.bz2: @echo "Creating $@" diff --git a/bddtests/docker-compose-next-4.yml b/bddtests/docker-compose-next-4.yml index 58b3862753e..e2bb24c629e 100644 --- a/bddtests/docker-compose-next-4.yml +++ b/bddtests/docker-compose-next-4.yml @@ -7,7 +7,6 @@ services: file: docker-compose-orderer-solo.yml service: orderer0 environment: - - ORDERER_GENERAL_LOCALMSPDIR=${ORDERER0_ORDERER_GENERAL_LOCALMSPDIR} - ORDERER_GENERAL_LOCALMSPID=${ORDERER0_ORDERER_GENERAL_LOCALMSPID} - ORDERER_GENERAL_TLS_ENABLED=true - ORDERER_GENERAL_TLS_PRIVATEKEY=${ORDERER0_ORDERER_GENERAL_TLS_PRIVATEKEY} @@ -24,7 +23,6 @@ services: - CORE_PEER_GOSSIP_BOOTSTRAP=peer1:7051 - CORE_PEER_PROFILE_ENABLED=true - CORE_PEER_COMMITTER_LEDGER_ORDERER=orderer0:7050 - - CORE_PEER_MSPCONFIGPATH=${PEER0_CORE_PEER_MSPCFGPATH} - CORE_PEER_LOCALMSPID=${PEER0_CORE_PEER_LOCALMSPID} - CORE_PEER_TLS_CERT_FILE=${PEER0_CORE_PEER_TLS_CERT_FILE} - CORE_PEER_TLS_KEY_FILE=${PEER0_CORE_PEER_TLS_KEY_FILE} @@ -44,7 +42,6 @@ services: - CORE_PEER_ID=vp1 - CORE_PEER_GOSSIP_BOOTSTRAP=peer0:7051 - CORE_PEER_COMMITTER_LEDGER_ORDERER=orderer0:7050 - - CORE_PEER_MSPCONFIGPATH=${PEER1_CORE_PEER_MSPCFGPATH} - CORE_PEER_LOCALMSPID=${PEER1_CORE_PEER_LOCALMSPID} - CORE_PEER_TLS_CERT_FILE=${PEER1_CORE_PEER_TLS_CERT_FILE} - CORE_PEER_TLS_KEY_FILE=${PEER1_CORE_PEER_TLS_KEY_FILE} @@ -62,7 +59,6 @@ services: - CORE_PEER_ID=vp2 - CORE_PEER_GOSSIP_BOOTSTRAP=peer3:7051 - CORE_PEER_COMMITTER_LEDGER_ORDERER=orderer0:7050 - - CORE_PEER_MSPCONFIGPATH=${PEER2_CORE_PEER_MSPCFGPATH} - CORE_PEER_LOCALMSPID=${PEER2_CORE_PEER_LOCALMSPID} - CORE_PEER_TLS_CERT_FILE=${PEER2_CORE_PEER_TLS_CERT_FILE} - CORE_PEER_TLS_KEY_FILE=${PEER2_CORE_PEER_TLS_KEY_FILE} @@ -80,7 +76,6 @@ services: - CORE_PEER_ID=vp3 - CORE_PEER_GOSSIP_BOOTSTRAP=peer2:7051 - CORE_PEER_COMMITTER_LEDGER_ORDERER=orderer0:7050 - - CORE_PEER_MSPCONFIGPATH=${PEER3_CORE_PEER_MSPCFGPATH} - CORE_PEER_LOCALMSPID=${PEER3_CORE_PEER_LOCALMSPID} - CORE_PEER_TLS_CERT_FILE=${PEER3_CORE_PEER_TLS_CERT_FILE} - CORE_PEER_TLS_KEY_FILE=${PEER3_CORE_PEER_TLS_KEY_FILE} diff --git a/bddtests/orderer-3-kafka-1.yml b/bddtests/orderer-3-kafka-1.yml index 26e640275a6..7ad687170c5 100644 --- a/bddtests/orderer-3-kafka-1.yml +++ b/bddtests/orderer-3-kafka-1.yml @@ -5,7 +5,6 @@ services: service: orderer0 image: hyperledger/fabric-testenv-orderer environment: - - ORDERER_GENERAL_LOCALMSPDIR=${ORDERER1_ORDERER_GENERAL_LOCALMSPDIR} - ORDERER_GENERAL_LOCALMSPID=${ORDERER1_ORDERER_GENERAL_LOCALMSPID} - ORDERER_GENERAL_TLS_PRIVATEKEY=${ORDERER1_ORDERER_GENERAL_TLS_PRIVATEKEY} - ORDERER_GENERAL_TLS_CERTIFICATE=${ORDERER1_ORDERER_GENERAL_TLS_CERTIFICATE} @@ -19,7 +18,6 @@ services: service: orderer0 image: hyperledger/fabric-testenv-orderer environment: - - ORDERER_GENERAL_LOCALMSPDIR=${ORDERER2_ORDERER_GENERAL_LOCALMSPDIR} - ORDERER_GENERAL_LOCALMSPID=${ORDERER2_ORDERER_GENERAL_LOCALMSPID} - ORDERER_GENERAL_TLS_PRIVATEKEY=${ORDERER2_ORDERER_GENERAL_TLS_PRIVATEKEY} - ORDERER_GENERAL_TLS_CERTIFICATE=${ORDERER2_ORDERER_GENERAL_TLS_CERTIFICATE} diff --git a/bddtests/regression/go/ote/README.md b/bddtests/regression/go/ote/README.md index 5789a47c6d8..91865080d66 100644 --- a/bddtests/regression/go/ote/README.md +++ b/bddtests/regression/go/ote/README.md @@ -84,8 +84,8 @@ Check your Docker and Docker-Compose versions with the following commands: ``` ### Environment Variables for configuration -Find default values of all variables in hyperledger/fabric/orderer/orderer.yaml -and hyperledger/fabric/peer/core.yaml. +Find default values of all variables in hyperledger/fabric/sampleconfig/orderer.yaml +and hyperledger/fabric/sampleconfig/core.yaml. ``` CONFIGTX_ORDERER_ORDERERTYPE solo CONFIGTX_ORDERER_BATCHSIZE_MAXMESSAGECOUNT 10 diff --git a/bddtests/regression/go/ote/network.json b/bddtests/regression/go/ote/network.json index 4328713e779..dbb43de67f2 100644 --- a/bddtests/regression/go/ote/network.json +++ b/bddtests/regression/go/ote/network.json @@ -65,9 +65,6 @@ "zookeeper", "kafka0" ], - "volumes": [ - "/opt/gopath/src/github.com/hyperledger/fabric/msp/sampleconfig:/etc/hyperledger/msp/sampleconfig" - ], "container_name": "orderer0", "networks": [ "bridge" @@ -110,7 +107,6 @@ }, "working_dir": "/opt/gopath/src/github.com/hyperledger/fabric/peer", "volumes": [ - "/opt/gopath/src/github.com/hyperledger/fabric/msp/sampleconfig:/etc/hyperledger/msp/sampleconfig", "/var/run:/host/var/run/" ], "command": "peer node start", diff --git a/bddtests/regression/go/ote/ote.go b/bddtests/regression/go/ote/ote.go index a36bcb90fd1..51de7f997aa 100644 --- a/bddtests/regression/go/ote/ote.go +++ b/bddtests/regression/go/ote/ote.go @@ -20,7 +20,7 @@ package main // ====================== // // This file ote.go contains main(), for executing from command line -// using environment variables to override those in orderer/orderer.yaml +// using environment variables to override those in sampleconfig/orderer.yaml // or to set OTE test configuration parameters. // // Function ote() is called by main after reading environment variables, diff --git a/bddtests/steps/endorser_util.py b/bddtests/steps/endorser_util.py index 9701898d277..dd3d3a7da5f 100644 --- a/bddtests/steps/endorser_util.py +++ b/bddtests/steps/endorser_util.py @@ -157,7 +157,7 @@ def getExample02ChaincodeSpec(): def _createDeploymentSpecAsFile(ccSpec, outputPath): '''peer chaincode package -n myCC -c '{"Args":["init","a","100","b","200"]}' -p github.com/hyperledger/fabric/examples/chaincode/go/chaincode_example02 --logging-level=DEBUG test.file''' myEnv = os.environ.copy() - myEnv['CORE_PEER_MSPCONFIGPATH'] = "./../msp/sampleconfig" + myEnv['CORE_PEER_MSPCONFIGPATH'] = "./../sampleconfig/msp" nameArgs = ["-n", ccSpec.chaincode_id.name] ctorArgs = ["-c", json.dumps({'Args' : [item for item in ccSpec.input.args]})] pathArgs = ["-p", ccSpec.chaincode_id.path] diff --git a/common/config/msp/config_test.go b/common/config/msp/config_test.go index 2c451de90c2..2ce3041b082 100644 --- a/common/config/msp/config_test.go +++ b/common/config/msp/config_test.go @@ -19,13 +19,16 @@ package msp import ( "testing" + "github.com/hyperledger/fabric/core/config" "github.com/hyperledger/fabric/msp" mspprotos "github.com/hyperledger/fabric/protos/msp" "github.com/stretchr/testify/assert" ) func TestMSPConfigManager(t *testing.T) { - conf, err := msp.GetLocalMspConfig("../../../msp/sampleconfig/", nil, "DEFAULT") + mspDir, err := config.GetDevMspDir() + assert.NoError(t, err) + conf, err := msp.GetLocalMspConfig(mspDir, nil, "DEFAULT") assert.NoError(t, err) // test success: diff --git a/common/configtx/test/helper.go b/common/configtx/test/helper.go index cfcf5ddfbef..28963a8a1aa 100644 --- a/common/configtx/test/helper.go +++ b/common/configtx/test/helper.go @@ -17,9 +17,6 @@ limitations under the License. package test import ( - "os" - "path/filepath" - "github.com/hyperledger/fabric/common/config" configtxmsp "github.com/hyperledger/fabric/common/config/msp" "github.com/hyperledger/fabric/common/configtx" @@ -27,6 +24,7 @@ import ( "github.com/hyperledger/fabric/common/configtx/tool/provisional" "github.com/hyperledger/fabric/common/flogging" "github.com/hyperledger/fabric/common/genesis" + cf "github.com/hyperledger/fabric/core/config" "github.com/hyperledger/fabric/msp" cb "github.com/hyperledger/fabric/protos/common" mspproto "github.com/hyperledger/fabric/protos/msp" @@ -39,33 +37,13 @@ const ( AcceptAllPolicyKey = "AcceptAllPolicy" ) -func dirExists(path string) bool { - _, err := os.Stat(path) - return err == nil -} - func getConfigDir() string { - mspSampleConfig := "/msp/sampleconfig" - peerPath := filepath.Join(os.Getenv("PEER_CFG_PATH"), mspSampleConfig) - ordererPath := filepath.Join(os.Getenv("ORDERER_CFG_PATH"), mspSampleConfig) - switch { - case dirExists(peerPath): - return peerPath - case dirExists(ordererPath): - return ordererPath - } - - gopath := os.Getenv("GOPATH") - for _, p := range filepath.SplitList(gopath) { - samplePath := filepath.Join(p, "src/github.com/hyperledger/fabric", mspSampleConfig) - if !dirExists(samplePath) { - continue - } - return samplePath + mspDir, err := cf.GetDevMspDir() + if err != nil { + logger.Panicf("Could not find genesis.yaml, try setting GOPATH correctly") } - logger.Panicf("Could not find genesis.yaml, try setting PEER_CFG_PATH, ORDERER_CFG_PATH, or GOPATH correctly") - return "" + return mspDir } // MakeGenesisBlock creates a genesis block using the test templates for the given chainID diff --git a/common/configtx/test/helper_test.go b/common/configtx/test/helper_test.go index 9fe0bdda644..e9dd7303073 100644 --- a/common/configtx/test/helper_test.go +++ b/common/configtx/test/helper_test.go @@ -21,6 +21,7 @@ import ( "path/filepath" "testing" + "github.com/hyperledger/fabric/core/config" "github.com/hyperledger/fabric/msp" logging "github.com/op/go-logging" ) @@ -43,10 +44,15 @@ func TestMakeGenesisBlock(t *testing.T) { func TestMakeGenesisBlockFromMSPs(t *testing.T) { + mspDir, err := config.GetDevMspDir() + if err != nil { + t.Fatalf("Error getting DevMspDir: %s", err) + } + ordererOrgID := "TestOrdererOrg" appOrgID := "TestAppOrg" - appMSPConf, err := msp.GetLocalMspConfig("msp/sampleconfig", nil, appOrgID) - ordererMSPConf, err := msp.GetLocalMspConfig("msp/sampleconfig", nil, ordererOrgID) + appMSPConf, err := msp.GetLocalMspConfig(mspDir, nil, appOrgID) + ordererMSPConf, err := msp.GetLocalMspConfig(mspDir, nil, ordererOrgID) if err != nil { t.Fatalf("Error making genesis block from MSPs: %s", err) } diff --git a/common/configtx/tool/localconfig/config.go b/common/configtx/tool/localconfig/config.go index a72fafd463e..094313f19de 100644 --- a/common/configtx/tool/localconfig/config.go +++ b/common/configtx/tool/localconfig/config.go @@ -18,8 +18,7 @@ package localconfig import ( "fmt" - "os" - "path/filepath" + "strings" "time" @@ -28,7 +27,10 @@ import ( "github.com/spf13/viper" + "path/filepath" + bccsp "github.com/hyperledger/fabric/bccsp/factory" + cf "github.com/hyperledger/fabric/core/config" ) var logger = flogging.MustGetLogger("configtx/tool/localconfig") @@ -43,16 +45,6 @@ const ( Prefix string = "CONFIGTX" ) -var ( - configName string - configFileName string -) - -func init() { - configName = strings.ToLower(Prefix) - configFileName = configName + ".yaml" -} - // TopLevel consists of the structs used by the configtxgen tool. type TopLevel struct { Profiles map[string]*Profile `yaml:"Profiles"` @@ -137,7 +129,7 @@ var genesisDefaults = TopLevel{ }, } -func (p *Profile) completeInitialization() { +func (p *Profile) initDefaults() { for { switch { case p.Orderer.OrdererType == "": @@ -167,46 +159,29 @@ func (p *Profile) completeInitialization() { } } -// Load returns the orderer/application config combination that corresponds to a given profile. -func Load(profile string) *Profile { - config := viper.New() - - config.SetConfigName(configName) - var cfgPath string +func translatePaths(configDir string, org *Organization) { + cf.TranslatePathInPlace(configDir, &org.MSPDir) + cf.TranslatePathInPlace(configDir, &org.BCCSP.SwOpts.FileKeystore.KeyStorePath) +} - // Candidate paths to look for the config file in, based on GOPATH - searchPath := []string{ - os.Getenv("ORDERER_CFG_PATH"), - os.Getenv("PEER_CFG_PATH"), - } +func (p *Profile) completeInitialization(configDir string) { + p.initDefaults() - for _, p := range filepath.SplitList(os.Getenv("GOPATH")) { - searchPath = append(searchPath, filepath.Join(p, "src/github.com/hyperledger/fabric/common/configtx/tool/")) + // Fix up any relative paths + for _, org := range p.Application.Organizations { + translatePaths(configDir, org) } - for _, path := range searchPath { - if len(path) == 0 { - // No point printing a "checking for" message below for an empty path - continue - } - logger.Infof("Looking for %s in: %s", configFileName, path) - if _, err := os.Stat(filepath.Join(path, configFileName)); err != nil { - // The YAML file does not exist in this component of the path - continue - } - cfgPath = path - logger.Infof("Found %s there", configFileName) - break + for _, org := range p.Orderer.Organizations { + translatePaths(configDir, org) } +} - if cfgPath == "" { - logger.Fatalf("Could not find %s in paths of %s."+ - "Try setting ORDERER_CFG_PATH, PEER_CFG_PATH, or GOPATH correctly.", - configFileName, searchPath) - } +// Load returns the orderer/application config combination that corresponds to a given profile. +func Load(profile string) *Profile { + config := viper.New() - // Path to look for the config file in - config.AddConfigPath(cfgPath) + cf.InitViper(config, "configtx") // For environment variables config.SetEnvPrefix(Prefix) @@ -217,7 +192,7 @@ func Load(profile string) *Profile { err := config.ReadInConfig() if err != nil { - logger.Panicf("Error reading configuration from %s in %s: %s", configFileName, cfgPath, err) + logger.Panicf("Error reading configuration: %s", err) } var uconf TopLevel @@ -231,7 +206,7 @@ func Load(profile string) *Profile { logger.Panicf("Could not find profile %s", profile) } - result.completeInitialization() + result.completeInitialization(filepath.Dir(config.ConfigFileUsed())) return result } diff --git a/common/configtx/tool/provisional/provisional.go b/common/configtx/tool/provisional/provisional.go index 7d19b6e507d..b11cd41b4e6 100644 --- a/common/configtx/tool/provisional/provisional.go +++ b/common/configtx/tool/provisional/provisional.go @@ -18,8 +18,6 @@ package provisional import ( "fmt" - "os" - "path/filepath" "github.com/hyperledger/fabric/common/cauthdsl" "github.com/hyperledger/fabric/common/config" @@ -69,38 +67,6 @@ const ( BlockValidationPolicyKey = "BlockValidation" ) -func resolveMSPDir(path string) string { - if path == "" || path[0] == os.PathSeparator { - return path - } - - // Look for MSP dir first in current path, then in ORDERER_CFG_PATH, then PEER_CFG_PATH, and finally in GOPATH - searchPath := []string{ - ".", - os.Getenv("ORDERER_CFG_PATH"), - os.Getenv("PEER_CFG_PATH"), - } - - for _, p := range filepath.SplitList(os.Getenv("GOPATH")) { - searchPath = append(searchPath, filepath.Join(p, "src/github.com/hyperledger/fabric/common/configtx/tool/")) - } - - for _, baseDir := range searchPath { - logger.Infof("Checking for MSPDir at: %s", baseDir) - fqPath := filepath.Join(baseDir, path) - if _, err := os.Stat(fqPath); err != nil { - // The mspdir does not exist - continue - } - return fqPath - } - - logger.Panicf("Unable to resolve a path for MSPDir: %s", path) - - // Unreachable - return "" -} - // DefaultChainCreationPolicyNames is the default value of ChainCreatorsKey. var DefaultChainCreationPolicyNames = []string{AcceptAllPolicyKey} @@ -150,7 +116,7 @@ func New(conf *genesisconfig.Profile) Generator { } for _, org := range conf.Orderer.Organizations { - mspConfig, err := msp.GetVerifyingMspConfig(resolveMSPDir(org.MSPDir), org.BCCSP, org.ID) + mspConfig, err := msp.GetVerifyingMspConfig(org.MSPDir, org.BCCSP, org.ID) if err != nil { logger.Panicf("Error loading MSP configuration for org %s: %s", org.Name, err) } @@ -180,7 +146,7 @@ func New(conf *genesisconfig.Profile) Generator { policies.TemplateImplicitMetaMajorityPolicy([]string{config.ApplicationGroupKey}, configvaluesmsp.AdminsPolicyKey), } for _, org := range conf.Application.Organizations { - mspConfig, err := msp.GetVerifyingMspConfig(resolveMSPDir(org.MSPDir), org.BCCSP, org.ID) + mspConfig, err := msp.GetVerifyingMspConfig(org.MSPDir, org.BCCSP, org.ID) if err != nil { logger.Panicf("Error loading MSP configuration for org %s: %s", org.Name, err) } diff --git a/common/localmsp/signer_test.go b/common/localmsp/signer_test.go index 7328a5b9e0d..b7038f0120b 100644 --- a/common/localmsp/signer_test.go +++ b/common/localmsp/signer_test.go @@ -26,18 +26,7 @@ import ( ) func TestMain(m *testing.M) { - // 1. Determine MSP configuration - var mspMgrConfigDir string - var alternativeCfgPath = os.Getenv("ORDERER_CFG_PATH") - if alternativeCfgPath != "" { - mspMgrConfigDir = alternativeCfgPath + "/msp/sampleconfig/" - } else if _, err := os.Stat("./msp/sampleconfig/"); err == nil { - mspMgrConfigDir = "./msp/sampleconfig/" - } else { - mspMgrConfigDir = os.Getenv("GOPATH") + "/src/github.com/hyperledger/fabric/msp/sampleconfig/" - } - - if err := mspmgmt.LoadLocalMsp(mspMgrConfigDir, nil, "DEFAULT"); err != nil { + if err := mspmgmt.LoadDevMsp(); err != nil { os.Exit(-1) } diff --git a/core/admin.go b/core/admin.go index 75ad7fdcf6a..182d5beb977 100644 --- a/core/admin.go +++ b/core/admin.go @@ -19,11 +19,11 @@ package core import ( "os" - "github.com/spf13/viper" "golang.org/x/net/context" "github.com/golang/protobuf/ptypes/empty" "github.com/hyperledger/fabric/common/flogging" + "github.com/hyperledger/fabric/core/config" pb "github.com/hyperledger/fabric/protos/peer" ) @@ -58,7 +58,7 @@ func (*ServerAdmin) StopServer(context.Context, *empty.Empty) (*pb.ServerStatus, status := &pb.ServerStatus{Status: pb.ServerStatus_STOPPED} log.Debugf("returning status: %s", status) - pidFile := viper.GetString("peer.fileSystemPath") + "/peer.pid" + pidFile := config.GetPath("peer.fileSystemPath") + "/peer.pid" log.Debugf("Remove pid file %s", pidFile) os.Remove(pidFile) defer os.Exit(0) diff --git a/core/chaincode/chaincode_support.go b/core/chaincode/chaincode_support.go index 167acf54825..4b3b8f162ed 100644 --- a/core/chaincode/chaincode_support.go +++ b/core/chaincode/chaincode_support.go @@ -35,6 +35,7 @@ import ( "github.com/hyperledger/fabric/core/chaincode/platforms" "github.com/hyperledger/fabric/core/chaincode/shim" "github.com/hyperledger/fabric/core/common/ccprovider" + "github.com/hyperledger/fabric/core/config" "github.com/hyperledger/fabric/core/container" "github.com/hyperledger/fabric/core/container/api" "github.com/hyperledger/fabric/core/container/ccintf" @@ -120,7 +121,7 @@ func (chaincodeSupport *ChaincodeSupport) chaincodeHasBeenLaunched(chaincode str // NewChaincodeSupport creates a new ChaincodeSupport instance func NewChaincodeSupport(getPeerEndpoint func() (*pb.PeerEndpoint, error), userrunsCC bool, ccstartuptimeout time.Duration) *ChaincodeSupport { - ccprovider.SetChaincodesPath(viper.GetString("peer.fileSystemPath") + string(filepath.Separator) + "chaincodes") + ccprovider.SetChaincodesPath(config.GetPath("peer.fileSystemPath") + string(filepath.Separator) + "chaincodes") pnid := viper.GetString("peer.networkId") pid := viper.GetString("peer.id") @@ -148,8 +149,8 @@ func NewChaincodeSupport(getPeerEndpoint func() (*pb.PeerEndpoint, error), userr theChaincodeSupport.peerTLS = viper.GetBool("peer.tls.enabled") if theChaincodeSupport.peerTLS { - theChaincodeSupport.peerTLSCertFile = viper.GetString("peer.tls.cert.file") - theChaincodeSupport.peerTLSKeyFile = viper.GetString("peer.tls.key.file") + theChaincodeSupport.peerTLSCertFile = config.GetPath("peer.tls.cert.file") + theChaincodeSupport.peerTLSKeyFile = config.GetPath("peer.tls.key.file") theChaincodeSupport.peerTLSSvrHostOrd = viper.GetString("peer.tls.serverhostoverride") } diff --git a/core/chaincode/exectransaction_test.go b/core/chaincode/exectransaction_test.go index 3435595f44b..7b802e593d1 100644 --- a/core/chaincode/exectransaction_test.go +++ b/core/chaincode/exectransaction_test.go @@ -37,6 +37,7 @@ import ( "github.com/hyperledger/fabric/common/policies" "github.com/hyperledger/fabric/common/util" "github.com/hyperledger/fabric/core/common/ccprovider" + "github.com/hyperledger/fabric/core/config" "github.com/hyperledger/fabric/core/container" "github.com/hyperledger/fabric/core/container/ccintf" "github.com/hyperledger/fabric/core/ledger" @@ -70,7 +71,7 @@ func initPeer(chainIDs ...string) (net.Listener, error) { var opts []grpc.ServerOption if viper.GetBool("peer.tls.enabled") { - creds, err := credentials.NewServerTLSFromFile(viper.GetString("peer.tls.cert.file"), viper.GetString("peer.tls.key.file")) + creds, err := credentials.NewServerTLSFromFile(config.GetPath("peer.tls.cert.file"), config.GetPath("peer.tls.key.file")) if err != nil { return nil, fmt.Errorf("Failed to generate credentials %v", err) } @@ -124,7 +125,7 @@ func finitPeer(lis net.Listener, chainIDs ...string) { closeListenerAndSleep(lis) } ledgermgmt.CleanupTestEnv() - ledgerPath := viper.GetString("peer.fileSystemPath") + ledgerPath := config.GetPath("peer.fileSystemPath") os.RemoveAll(ledgerPath) os.RemoveAll(filepath.Join(os.TempDir(), "hyperledger")) @@ -1740,9 +1741,7 @@ func TestChaincodeInitializeInitError(t *testing.T) { func TestMain(m *testing.M) { var err error - // setup the MSP manager so that we can sign/verify - mspMgrConfigDir := "../../msp/sampleconfig/" - msptesttools.LoadMSPSetupForTesting(mspMgrConfigDir) + msptesttools.LoadMSPSetupForTesting() signer, err = mspmgmt.GetLocalMSP().GetDefaultSigningIdentity() if err != nil { os.Exit(-1) diff --git a/core/chaincode/platforms/car/test/platform_test.go b/core/chaincode/platforms/car/test/platform_test.go index dfc7a45b364..1b4b3efa674 100644 --- a/core/chaincode/platforms/car/test/platform_test.go +++ b/core/chaincode/platforms/car/test/platform_test.go @@ -27,7 +27,7 @@ import ( ) func TestMain(m *testing.M) { - testutil.SetupTestConfig("../../../../../peer") + testutil.SetupTestConfig() os.Exit(m.Run()) } diff --git a/core/chaincode/platforms/java/test/java_test.go b/core/chaincode/platforms/java/test/java_test.go index 0db93e97e6c..a858093cec8 100644 --- a/core/chaincode/platforms/java/test/java_test.go +++ b/core/chaincode/platforms/java/test/java_test.go @@ -27,7 +27,7 @@ import ( ) func TestMain(m *testing.M) { - testutil.SetupTestConfig("../../../../../peer") + testutil.SetupTestConfig() os.Exit(m.Run()) } diff --git a/core/chaincode/platforms/platforms.go b/core/chaincode/platforms/platforms.go index 6010d4e01f9..6bbcfec3069 100644 --- a/core/chaincode/platforms/platforms.go +++ b/core/chaincode/platforms/platforms.go @@ -32,6 +32,7 @@ import ( "github.com/hyperledger/fabric/core/chaincode/platforms/car" "github.com/hyperledger/fabric/core/chaincode/platforms/golang" "github.com/hyperledger/fabric/core/chaincode/platforms/java" + "github.com/hyperledger/fabric/core/config" cutil "github.com/hyperledger/fabric/core/container/util" pb "github.com/hyperledger/fabric/protos/peer" "github.com/spf13/viper" @@ -82,10 +83,10 @@ func getPeerTLSCert() ([]byte, error) { } var path string // first we check for the rootcert - path = viper.GetString("peer.tls.rootcert.file") + path = config.GetPath("peer.tls.rootcert.file") if path == "" { // check for tls cert - path = viper.GetString("peer.tls.cert.file") + path = config.GetPath("peer.tls.cert.file") } // this should not happen if the peer is running with TLS enabled if _, err := os.Stat(path); err != nil { diff --git a/core/comm/connection.go b/core/comm/connection.go index 414e053dbcd..cac577f3cdb 100644 --- a/core/comm/connection.go +++ b/core/comm/connection.go @@ -29,6 +29,7 @@ import ( "google.golang.org/grpc/grpclog" "github.com/hyperledger/fabric/common/flogging" + "github.com/hyperledger/fabric/core/config" "github.com/spf13/viper" ) @@ -174,9 +175,9 @@ func InitTLSForPeer() credentials.TransportCredentials { sn = viper.GetString("peer.tls.serverhostoverride") } var creds credentials.TransportCredentials - if viper.GetString("peer.tls.rootcert.file") != "" { + if config.GetPath("peer.tls.rootcert.file") != "" { var err error - creds, err = credentials.NewClientTLSFromFile(viper.GetString("peer.tls.rootcert.file"), sn) + creds, err = credentials.NewClientTLSFromFile(config.GetPath("peer.tls.rootcert.file"), sn) if err != nil { grpclog.Fatalf("Failed to create TLS credentials %v", err) } diff --git a/core/comm/connection_test.go b/core/comm/connection_test.go index 3f04a248e75..0ed4bd79e83 100644 --- a/core/comm/connection_test.go +++ b/core/comm/connection_test.go @@ -52,7 +52,7 @@ VQQLDAtIeXBlcmxlZGdlcjESMBAGA1UEAwwJbG9jYWxob3N0MFkwEwYHKoZIzj0C ` func TestConnection_Correct(t *testing.T) { - testutil.SetupTestConfig("./../../peer") + testutil.SetupTestConfig() viper.Set("ledger.blockchain.deploy-system-chaincode", "false") peerAddress := GetPeerTestingAddress("7051") var tmpConn *grpc.ClientConn @@ -69,7 +69,7 @@ func TestConnection_Correct(t *testing.T) { } func TestConnection_WrongAddress(t *testing.T) { - testutil.SetupTestConfig("./../../peer") + testutil.SetupTestConfig() viper.Set("ledger.blockchain.deploy-system-chaincode", "false") peerAddress := GetPeerTestingAddress("7052") var tmpConn *grpc.ClientConn diff --git a/core/committer/txvalidator/txvalidator_test.go b/core/committer/txvalidator/txvalidator_test.go index be53a486411..36f279169ae 100644 --- a/core/committer/txvalidator/txvalidator_test.go +++ b/core/committer/txvalidator/txvalidator_test.go @@ -159,8 +159,7 @@ func createCCUpgradeEnvelope(chainID, chaincodeName, chaincodeVersion string, si func TestGetTxCCInstance(t *testing.T) { // setup the MSP manager so that we can sign/verify - mspMgrConfigDir := "../../../msp/sampleconfig/" - err := msptesttools.LoadMSPSetupForTesting(mspMgrConfigDir) + err := msptesttools.LoadMSPSetupForTesting() if err != nil { t.Fatalf("Could not initialize msp, err: %s", err) } diff --git a/core/common/ccpackage/ccpackage_test.go b/core/common/ccpackage/ccpackage_test.go index 8e0a8aba748..f3211224131 100644 --- a/core/common/ccpackage/ccpackage_test.go +++ b/core/common/ccpackage/ccpackage_test.go @@ -235,16 +235,11 @@ var signerSerialized []byte func TestMain(m *testing.M) { // setup the MSP manager so that we can sign/verify - mspMgrConfigFile := "../../msp/sampleconfig/" - err := msptesttools.LoadMSPSetupForTesting(mspMgrConfigFile) + err := msptesttools.LoadMSPSetupForTesting() if err != nil { - //be docker friendly - mspMgrConfigFile = "/etc/hyperledger/fabric/msp/sampleconfig" - if err = msptesttools.LoadMSPSetupForTesting(mspMgrConfigFile); err != nil { - os.Exit(-1) - fmt.Printf("Could not initialize msp") - return - } + os.Exit(-1) + fmt.Printf("Could not initialize msp") + return } localmsp = mspmgmt.GetLocalMSP() if localmsp == nil { diff --git a/core/common/validation/fullflow_test.go b/core/common/validation/fullflow_test.go index 8b9b413b431..27c80e084a0 100644 --- a/core/common/validation/fullflow_test.go +++ b/core/common/validation/fullflow_test.go @@ -328,8 +328,7 @@ var signerSerialized []byte func TestMain(m *testing.M) { // setup crypto algorithms // setup the MSP manager so that we can sign/verify - mspMgrConfigDir := "../../../msp/sampleconfig/" - err := msptesttools.LoadMSPSetupForTesting(mspMgrConfigDir) + err := msptesttools.LoadMSPSetupForTesting() if err != nil { fmt.Printf("Could not initialize msp, err %s", err) os.Exit(-1) diff --git a/core/config/config.go b/core/config/config.go new file mode 100644 index 00000000000..1d4caf55873 --- /dev/null +++ b/core/config/config.go @@ -0,0 +1,191 @@ +/* +Copyright Greg Haskins 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 config + +import ( + "fmt" + "os" + "path/filepath" + + "github.com/spf13/viper" +) + +func dirExists(path string) bool { + _, err := os.Stat(path) + return err == nil +} + +func addConfigPath(v *viper.Viper, p string) { + if v != nil { + v.AddConfigPath(p) + } else { + viper.AddConfigPath(p) + } +} + +//---------------------------------------------------------------------------------- +// GetDevConfigDir() +//---------------------------------------------------------------------------------- +// Returns the path to the default configuration that is maintained with the source +// tree. Only valid to call from a test/development context. +//---------------------------------------------------------------------------------- +func GetDevConfigDir() (string, error) { + gopath := os.Getenv("GOPATH") + if gopath == "" { + return "", fmt.Errorf("GOPATH not set") + } + + for _, p := range filepath.SplitList(gopath) { + devPath := filepath.Join(p, "src/github.com/hyperledger/fabric/sampleconfig") + if !dirExists(devPath) { + continue + } + + return devPath, nil + } + + return "", fmt.Errorf("DevConfigDir not found in %s", gopath) +} + +//---------------------------------------------------------------------------------- +// GetDevMspDir() +//---------------------------------------------------------------------------------- +// Builds upon GetDevConfigDir to return the path to our sampleconfig/msp that is +// maintained with the source tree. Only valid to call from a test/development +// context. Runtime environment should use configuration elements such as +// +// GetPath("peer.mspConfigDir") +//---------------------------------------------------------------------------------- +func GetDevMspDir() (string, error) { + devDir, err := GetDevConfigDir() + if err != nil { + return "", fmt.Errorf("Error obtaining DevConfigDir: %s", devDir) + } + + return filepath.Join(devDir, "msp"), nil +} + +//---------------------------------------------------------------------------------- +// TranslatePath() +//---------------------------------------------------------------------------------- +// Translates a relative path into a fully qualified path relative to the config +// file that specified it. Absolute paths are passed unscathed. +//---------------------------------------------------------------------------------- +func TranslatePath(base, p string) string { + if filepath.IsAbs(p) { + return p + } + + return filepath.Join(base, p) +} + +//---------------------------------------------------------------------------------- +// TranslatePathInPlace() +//---------------------------------------------------------------------------------- +// Translates a relative path into a fully qualified path in-place (updating the +// pointer) relative to the config file that specified it. Absolute paths are +// passed unscathed. +//---------------------------------------------------------------------------------- +func TranslatePathInPlace(base string, p *string) { + *p = TranslatePath(base, *p) +} + +//---------------------------------------------------------------------------------- +// GetPath() +//---------------------------------------------------------------------------------- +// GetPath allows configuration strings that specify a (config-file) relative path +// +// For example: Assume our config is located in /etc/hyperledger/fabric/core.yaml with +// a key "msp.configPath" = "msp/config.yaml". +// +// This function will return: +// GetConfigPath("msp.configPath") -> /etc/hyperledger/fabric/msp/config.yaml +// +//---------------------------------------------------------------------------------- +func GetPath(key string) string { + p := viper.GetString(key) + if p == "" { + return "" + } + + return TranslatePath(filepath.Dir(viper.ConfigFileUsed()), p) +} + +const OfficialPath = "/etc/hyperledger/fabric" + +//---------------------------------------------------------------------------------- +// InitViper() +//---------------------------------------------------------------------------------- +// Performs basic initialization of our viper-based configuration layer. +// Primary thrust is to establish the paths that should be consulted to find +// the configuration we need. If v == nil, we will initialize the global +// Viper instance +//---------------------------------------------------------------------------------- +func InitViper(v *viper.Viper, configName string) error { + var altPath = os.Getenv("FABRIC_CFG_PATH") + if altPath != "" { + // If the user has overridden the path with an envvar, its the only path + // we will consider + addConfigPath(v, altPath) + } else { + // If we get here, we should use the default paths in priority order: + // + // *) CWD + // *) The $GOPATH based development tree + // *) /etc/hyperledger/fabric + // + + // CWD + addConfigPath(v, "./") + + // DevConfigPath + err := AddDevConfigPath(v) + if err != nil { + return err + } + + // And finally, the official path + if dirExists(OfficialPath) { + addConfigPath(v, OfficialPath) + } + } + + // Now set the configuration file. + if v != nil { + v.SetConfigName(configName) + } else { + viper.SetConfigName(configName) + } + + return nil +} + +//---------------------------------------------------------------------------------- +// AddDevConfigPath() +//---------------------------------------------------------------------------------- +// Helper utility that automatically adds our DevConfigDir to the viper path +//---------------------------------------------------------------------------------- +func AddDevConfigPath(v *viper.Viper) error { + devPath, err := GetDevConfigDir() + if err != nil { + return err + } + + addConfigPath(v, devPath) + + return nil +} diff --git a/core/container/config.go b/core/container/config.go index dab45c94dd4..27e37c565fb 100644 --- a/core/container/config.go +++ b/core/container/config.go @@ -26,6 +26,7 @@ import ( "github.com/spf13/viper" "github.com/hyperledger/fabric/bccsp/factory" + "github.com/hyperledger/fabric/core/config" "github.com/hyperledger/fabric/msp" ) @@ -62,11 +63,10 @@ func SetupTestConfig() { viper.AutomaticEnv() replacer := strings.NewReplacer(".", "_") viper.SetEnvKeyReplacer(replacer) - viper.SetConfigName("core") // name of config file (without extension) - viper.AddConfigPath("./") // path to look for the config file in - viper.AddConfigPath("./../../peer/") // path to look for the config file in - err := viper.ReadInConfig() // Find and read the config file - if err != nil { // Handle errors reading the config file + viper.SetConfigName("core") // name of config file (without extension) + config.AddDevConfigPath(nil) // path to look for the config file in + err := viper.ReadInConfig() // Find and read the config file + if err != nil { // Handle errors reading the config file panic(fmt.Errorf("Fatal error config file: %s \n", err)) } diff --git a/core/container/dockercontroller/dockercontroller_test.go b/core/container/dockercontroller/dockercontroller_test.go index 4772f6d3865..c30d743a351 100644 --- a/core/container/dockercontroller/dockercontroller_test.go +++ b/core/container/dockercontroller/dockercontroller_test.go @@ -29,7 +29,7 @@ import ( ) func TestHostConfig(t *testing.T) { - coreutil.SetupTestConfig("./../../../peer") + coreutil.SetupTestConfig() var hostConfig = new(docker.HostConfig) err := viper.UnmarshalKey("vm.docker.hostConfig", hostConfig) if err != nil { @@ -44,7 +44,7 @@ func TestHostConfig(t *testing.T) { func TestGetDockerHostConfig(t *testing.T) { os.Setenv("HYPERLEDGER_VM_DOCKER_HOSTCONFIG_NETWORKMODE", "overlay") os.Setenv("HYPERLEDGER_VM_DOCKER_HOSTCONFIG_CPUSHARES", fmt.Sprint(1024*1024*1024*2)) - coreutil.SetupTestConfig("./../../../peer") + coreutil.SetupTestConfig() hostConfig := getDockerHostConfig() testutil.AssertNotNil(t, hostConfig) testutil.AssertEquals(t, hostConfig.NetworkMode, "overlay") diff --git a/core/container/util/dockerutil.go b/core/container/util/dockerutil.go index 42bc9913f5f..f5c59508389 100644 --- a/core/container/util/dockerutil.go +++ b/core/container/util/dockerutil.go @@ -22,6 +22,7 @@ import ( docker "github.com/fsouza/go-dockerclient" "github.com/hyperledger/fabric/common/metadata" + "github.com/hyperledger/fabric/core/config" "github.com/spf13/viper" ) @@ -30,9 +31,9 @@ func NewDockerClient() (client *docker.Client, err error) { endpoint := viper.GetString("vm.endpoint") tlsenabled := viper.GetBool("vm.docker.tls.enabled") if tlsenabled { - cert := viper.GetString("vm.docker.tls.cert.file") - key := viper.GetString("vm.docker.tls.key.file") - ca := viper.GetString("vm.docker.tls.ca.file") + cert := config.GetPath("vm.docker.tls.cert.file") + key := config.GetPath("vm.docker.tls.key.file") + ca := config.GetPath("vm.docker.tls.ca.file") client, err = docker.NewTLSClient(endpoint, cert, key, ca) } else { client, err = docker.NewClient(endpoint) diff --git a/core/deliverservice/deliveryclient_test.go b/core/deliverservice/deliveryclient_test.go index 81485783e0e..76cb4f5230d 100644 --- a/core/deliverservice/deliveryclient_test.go +++ b/core/deliverservice/deliveryclient_test.go @@ -34,7 +34,7 @@ import ( ) func init() { - msptesttools.LoadMSPSetupForTesting("../../msp/sampleconfig") + msptesttools.LoadMSPSetupForTesting() } const ( diff --git a/core/endorser/endorser_test.go b/core/endorser/endorser_test.go index 7d6adb8c85a..e7a378e1b64 100644 --- a/core/endorser/endorser_test.go +++ b/core/endorser/endorser_test.go @@ -34,6 +34,7 @@ import ( "github.com/hyperledger/fabric/common/util" "github.com/hyperledger/fabric/core/chaincode" "github.com/hyperledger/fabric/core/common/ccprovider" + "github.com/hyperledger/fabric/core/config" "github.com/hyperledger/fabric/core/container" "github.com/hyperledger/fabric/core/peer" syscc "github.com/hyperledger/fabric/core/scc" @@ -63,7 +64,7 @@ func initPeer(chainID string) (*testEnvironment, error) { // finitPeer(nil) var opts []grpc.ServerOption if viper.GetBool("peer.tls.enabled") { - creds, err := credentials.NewServerTLSFromFile(viper.GetString("peer.tls.cert.file"), viper.GetString("peer.tls.key.file")) + creds, err := credentials.NewServerTLSFromFile(config.GetPath("peer.tls.cert.file"), config.GetPath("peer.tls.key.file")) if err != nil { return nil, fmt.Errorf("Failed to generate credentials %v", err) } @@ -310,7 +311,7 @@ func invokeWithOverride(txid string, chainID string, spec *pb.ChaincodeSpec, non } func deleteChaincodeOnDisk(chaincodeID string) { - os.RemoveAll(filepath.Join(viper.GetString("peer.fileSystemPath"), "chaincodes", chaincodeID)) + os.RemoveAll(filepath.Join(config.GetPath("peer.fileSystemPath"), "chaincodes", chaincodeID)) } //begin tests. Note that we rely upon the system chaincode and peer to be created @@ -661,8 +662,7 @@ func TestMain(m *testing.M) { endorserServer = NewEndorserServer() // setup the MSP manager so that we can sign/verify - mspMgrConfigDir := "../../msp/sampleconfig/" - err = msptesttools.LoadMSPSetupForTesting(mspMgrConfigDir) + err = msptesttools.LoadMSPSetupForTesting() if err != nil { fmt.Printf("Could not initialize msp/signer, err %s", err) finitPeer(tev) diff --git a/core/endorser/endorser_test.yaml b/core/endorser/endorser_test.yaml index 06eb50c6e0f..b93557a5bbd 100644 --- a/core/endorser/endorser_test.yaml +++ b/core/endorser/endorser_test.yaml @@ -304,7 +304,7 @@ peer: enabled: false listenAddress: 0.0.0.0:6060 - mspConfigPath: ../../msp/sampleconfig + mspConfigPath: ../../sampleconfig/msp # BCCSP (Blockchain crypto provider): Select which crypto implementation or # library to use diff --git a/core/ledger/kvledger/example/main/example.go b/core/ledger/kvledger/example/main/example.go index 925c132ce2b..065c14159f4 100644 --- a/core/ledger/kvledger/example/main/example.go +++ b/core/ledger/kvledger/example/main/example.go @@ -46,7 +46,7 @@ var accounts = []string{"account1", "account2", "account3", "account4"} func init() { //call a helper method to load the core.yaml - testutil.SetupCoreYAMLConfig("./../../../../../peer") + testutil.SetupCoreYAMLConfig() // Initialization will get a handle to the ledger at the specified path // Note, if subledgers are supported in the future, diff --git a/core/ledger/kvledger/kv_ledger_test.go b/core/ledger/kvledger/kv_ledger_test.go index 286ab025c55..3b0b4442add 100644 --- a/core/ledger/kvledger/kv_ledger_test.go +++ b/core/ledger/kvledger/kv_ledger_test.go @@ -101,7 +101,7 @@ func TestKVLedgerBlockStorage(t *testing.T) { } func TestKVLedgerDBRecovery(t *testing.T) { - ledgertestutil.SetupCoreYAMLConfig("./../../../peer") + ledgertestutil.SetupCoreYAMLConfig() env := newTestEnv(t) defer env.cleanup() provider, _ := NewProvider() @@ -432,7 +432,7 @@ func TestKVLedgerDBRecovery(t *testing.T) { func TestLedgerWithCouchDbEnabledWithBinaryAndJSONData(t *testing.T) { //call a helper method to load the core.yaml - ledgertestutil.SetupCoreYAMLConfig("./../../../peer") + ledgertestutil.SetupCoreYAMLConfig() logger.Debugf("TestLedgerWithCouchDbEnabledWithBinaryAndJSONData IsCouchDBEnabled()value: %v , IsHistoryDBEnabled()value: %v\n", ledgerconfig.IsCouchDBEnabled(), ledgerconfig.IsHistoryDBEnabled()) diff --git a/core/ledger/kvledger/marble_example/main/marble_example.go b/core/ledger/kvledger/marble_example/main/marble_example.go index 75bd954c430..5cee900257b 100644 --- a/core/ledger/kvledger/marble_example/main/marble_example.go +++ b/core/ledger/kvledger/marble_example/main/marble_example.go @@ -50,7 +50,7 @@ func init() { logger.Debugf("Marble Example main init()") //call a helper method to load the core.yaml - testutil.SetupCoreYAMLConfig("./../../../../../peer") + testutil.SetupCoreYAMLConfig() cleanup() ledgermgmt.Initialize() diff --git a/core/ledger/kvledger/txmgmt/statedb/statecouchdb/statecouchdb_test.go b/core/ledger/kvledger/txmgmt/statedb/statecouchdb/statecouchdb_test.go index 921ca73b064..6efe78efc38 100644 --- a/core/ledger/kvledger/txmgmt/statedb/statecouchdb/statecouchdb_test.go +++ b/core/ledger/kvledger/txmgmt/statedb/statecouchdb/statecouchdb_test.go @@ -33,8 +33,7 @@ import ( func TestMain(m *testing.M) { // Read the core.yaml file for default config. - ledgertestutil.SetupCoreYAMLConfig("./../../../../../../peer") - + ledgertestutil.SetupCoreYAMLConfig() viper.Set("peer.fileSystemPath", "/tmp/fabric/ledgertests/kvledger/txmgmt/statedb/statecouchdb") // Switch to CouchDB diff --git a/core/ledger/kvledger/txmgmt/txmgr/commontests/txmgr_test.go b/core/ledger/kvledger/txmgmt/txmgr/commontests/txmgr_test.go index 47461bfaa00..4aadfcc2300 100644 --- a/core/ledger/kvledger/txmgmt/txmgr/commontests/txmgr_test.go +++ b/core/ledger/kvledger/txmgmt/txmgr/commontests/txmgr_test.go @@ -30,7 +30,7 @@ import ( ) func TestMain(m *testing.M) { - ledgertestutil.SetupCoreYAMLConfig("./../../../../../../peer") + ledgertestutil.SetupCoreYAMLConfig() os.Exit(m.Run()) } diff --git a/core/ledger/ledgerconfig/ledger_config.go b/core/ledger/ledgerconfig/ledger_config.go index a9ff636be94..93e1309a979 100644 --- a/core/ledger/ledgerconfig/ledger_config.go +++ b/core/ledger/ledgerconfig/ledger_config.go @@ -20,6 +20,7 @@ import ( "path/filepath" "time" + "github.com/hyperledger/fabric/core/config" "github.com/spf13/viper" ) @@ -45,7 +46,7 @@ func IsCouchDBEnabled() bool { // GetRootPath returns the filesystem path. // All ledger related contents are expected to be stored under this path func GetRootPath() string { - sysPath := viper.GetString("peer.fileSystemPath") + sysPath := config.GetPath("peer.fileSystemPath") return filepath.Join(sysPath, "ledgersData") } diff --git a/core/ledger/ledgerconfig/ledger_config_test.go b/core/ledger/ledgerconfig/ledger_config_test.go index 23acfc593c4..a7ac39f6b29 100644 --- a/core/ledger/ledgerconfig/ledger_config_test.go +++ b/core/ledger/ledgerconfig/ledger_config_test.go @@ -84,5 +84,5 @@ func TestIsHistoryDBEnabledFalse(t *testing.T) { func setUpCoreYAMLConfig() { //call a helper method to load the core.yaml - ledgertestutil.SetupCoreYAMLConfig("./../../../peer") + ledgertestutil.SetupCoreYAMLConfig() } diff --git a/core/ledger/testutil/test_util.go b/core/ledger/testutil/test_util.go index aba2180ff06..78e048ad86e 100644 --- a/core/ledger/testutil/test_util.go +++ b/core/ledger/testutil/test_util.go @@ -24,6 +24,7 @@ import ( "strings" "time" + "github.com/hyperledger/fabric/core/config" "github.com/op/go-logging" "github.com/spf13/viper" ) @@ -68,13 +69,18 @@ func SetupTestConfig() { } // SetupCoreYAMLConfig sets up configurations for testing -func SetupCoreYAMLConfig(coreYamlPath string) { +func SetupCoreYAMLConfig() { viper.SetConfigName("core") viper.SetEnvPrefix("CORE") - viper.AddConfigPath(coreYamlPath) viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_")) viper.AutomaticEnv() - err := viper.ReadInConfig() + + err := config.AddDevConfigPath(nil) + if err != nil { + panic(fmt.Errorf("Fatal error adding dev dir: %s \n", err)) + } + + err = viper.ReadInConfig() if err != nil { // Handle errors reading the config file panic(fmt.Errorf("Fatal error config file: %s \n", err)) } diff --git a/core/ledger/util/couchdb/couchdb_test.go b/core/ledger/util/couchdb/couchdb_test.go index 6b65ae10dc2..a753a47e4d3 100644 --- a/core/ledger/util/couchdb/couchdb_test.go +++ b/core/ledger/util/couchdb/couchdb_test.go @@ -65,7 +65,7 @@ var assetJSON = []byte(`{"asset_name":"marble1","color":"blue","size":"35","owne func TestMain(m *testing.M) { // Read the core.yaml file for default config. - ledgertestutil.SetupCoreYAMLConfig("./../../../../peer") + ledgertestutil.SetupCoreYAMLConfig() // Switch to CouchDB viper.Set("ledger.state.stateDatabase", "CouchDB") diff --git a/core/peer/config.go b/core/peer/config.go index 4d8c14e5f1d..a1438d3f502 100644 --- a/core/peer/config.go +++ b/core/peer/config.go @@ -37,6 +37,7 @@ import ( "github.com/spf13/viper" "github.com/hyperledger/fabric/core/comm" + "github.com/hyperledger/fabric/core/config" pb "github.com/hyperledger/fabric/protos/peer" ) @@ -176,8 +177,8 @@ func GetSecureConfig() (comm.SecureServerConfig, error) { } if secureConfig.UseTLS { // get the certs from the file system - serverKey, err := ioutil.ReadFile(viper.GetString("peer.tls.key.file")) - serverCert, err := ioutil.ReadFile(viper.GetString("peer.tls.cert.file")) + serverKey, err := ioutil.ReadFile(config.GetPath("peer.tls.key.file")) + serverCert, err := ioutil.ReadFile(config.GetPath("peer.tls.cert.file")) // must have both key and cert file if err != nil { return secureConfig, fmt.Errorf("Error loading TLS key and/or certificate (%s)", err) @@ -185,8 +186,8 @@ func GetSecureConfig() (comm.SecureServerConfig, error) { secureConfig.ServerCertificate = serverCert secureConfig.ServerKey = serverKey // check for root cert - if viper.GetString("peer.tls.rootcert.file") != "" { - rootCert, err := ioutil.ReadFile(viper.GetString("peer.tls.rootcert.file")) + if config.GetPath("peer.tls.rootcert.file") != "" { + rootCert, err := ioutil.ReadFile(config.GetPath("peer.tls.rootcert.file")) if err != nil { return secureConfig, fmt.Errorf("Error loading TLS root certificate (%s)", err) } diff --git a/core/peer/peer_test.go b/core/peer/peer_test.go index 808b2d3d6e9..67ab4b81a69 100644 --- a/core/peer/peer_test.go +++ b/core/peer/peer_test.go @@ -91,7 +91,7 @@ func TestCreateChainFromBlock(t *testing.T) { go grpcServer.Serve(socket) defer grpcServer.Stop() - msptesttools.LoadMSPSetupForTesting("../../msp/sampleconfig") + msptesttools.LoadMSPSetupForTesting() identity, _ := mgmt.GetLocalSigningIdentityOrPanic().Serialize() messageCryptoService := mcs.New(&mcs.MockChannelPolicyManagerGetter{}, localmsp.NewSigner(), mgmt.NewDeserializersManager()) diff --git a/core/scc/cscc/configure_test.go b/core/scc/cscc/configure_test.go index 4956d5b711b..d63525f8ffd 100644 --- a/core/scc/cscc/configure_test.go +++ b/core/scc/cscc/configure_test.go @@ -73,7 +73,7 @@ func (*mockDeliveryClientFactory) Service(g service.GossipService, endpoints []s } func TestMain(m *testing.M) { - msptesttools.LoadMSPSetupForTesting("../../../msp/sampleconfig") + msptesttools.LoadMSPSetupForTesting() os.Exit(m.Run()) } diff --git a/core/scc/escc/endorser_onevalidsignature_test.go b/core/scc/escc/endorser_onevalidsignature_test.go index 6e64922a768..05592bf433f 100644 --- a/core/scc/escc/endorser_onevalidsignature_test.go +++ b/core/scc/escc/endorser_onevalidsignature_test.go @@ -311,8 +311,7 @@ func validateProposalResponse(prBytes []byte, proposal *pb.Proposal, visibility } func TestMain(m *testing.M) { - mspMgrConfigDir := "../../../msp/sampleconfig/" - msptesttools.LoadMSPSetupForTesting(mspMgrConfigDir) + msptesttools.LoadMSPSetupForTesting() os.Exit(m.Run()) } diff --git a/core/scc/vscc/validator_onevalidsignature_test.go b/core/scc/vscc/validator_onevalidsignature_test.go index 57e7b0cf619..6196d334d0f 100644 --- a/core/scc/vscc/validator_onevalidsignature_test.go +++ b/core/scc/vscc/validator_onevalidsignature_test.go @@ -134,8 +134,7 @@ func TestMain(m *testing.M) { var err error // setup the MSP manager so that we can sign/verify - mspMgrConfigDir := "../../../msp/sampleconfig/" - msptesttools.LoadMSPSetupForTesting(mspMgrConfigDir) + msptesttools.LoadMSPSetupForTesting() id, err = mspmgmt.GetLocalMSP().GetDefaultSigningIdentity() if err != nil { diff --git a/core/testutil/config.go b/core/testutil/config.go index 90ded774a55..6d3c93eedc0 100644 --- a/core/testutil/config.go +++ b/core/testutil/config.go @@ -27,6 +27,7 @@ import ( "github.com/spf13/viper" "github.com/hyperledger/fabric/bccsp/factory" + "github.com/hyperledger/fabric/core/config" "github.com/hyperledger/fabric/msp" "github.com/hyperledger/fabric/common/flogging" @@ -59,7 +60,7 @@ func SetupTestLogging() { } // SetupTestConfig setup the config during test execution -func SetupTestConfig(pathToOpenchainYaml string) { +func SetupTestConfig() { flag.Parse() // Now set the configuration file @@ -67,10 +68,14 @@ func SetupTestConfig(pathToOpenchainYaml string) { viper.AutomaticEnv() replacer := strings.NewReplacer(".", "_") viper.SetEnvKeyReplacer(replacer) - viper.SetConfigName("core") // name of config file (without extension) - viper.AddConfigPath(pathToOpenchainYaml) // path to look for the config file in - err := viper.ReadInConfig() // Find and read the config file - if err != nil { // Handle errors reading the config file + viper.SetConfigName("core") // name of config file (without extension) + err := config.AddDevConfigPath(nil) + if err != nil { + panic(fmt.Errorf("Fatal error adding DevConfigPath: %s \n", err)) + } + + err = viper.ReadInConfig() // Find and read the config file + if err != nil { // Handle errors reading the config file panic(fmt.Errorf("Fatal error config file: %s \n", err)) } diff --git a/docs/source/Setup/Chaincode-setup.rst b/docs/source/Setup/Chaincode-setup.rst index ec189b3818f..3f4108fecdf 100644 --- a/docs/source/Setup/Chaincode-setup.rst +++ b/docs/source/Setup/Chaincode-setup.rst @@ -340,7 +340,7 @@ Terminal 3 (CLI or REST API) ^^^^^^^^^^^^^^^^^^^^^^^^^ The default REST interface port is ``7050``. It can be configured in -`core.yaml `__ +`core.yaml `__ using the ``rest.address`` property. If using Vagrant, the REST port mapping is defined in `Vagrantfile `__. diff --git a/docs/source/Setup/logging-control.rst b/docs/source/Setup/logging-control.rst index 26050f4d64e..2bfbf9f439c 100644 --- a/docs/source/Setup/logging-control.rst +++ b/docs/source/Setup/logging-control.rst @@ -55,7 +55,7 @@ example The default logging level for each individual ``peer`` subcommand can also be set in the -`core.yaml `__ +`core.yaml `__ file. For example the key ``logging.node`` sets the default level for the ``node`` subcommmand. Comments in the file also explain how the logging level can be overridden in various ways by using environment @@ -83,7 +83,7 @@ overrides for individual or groups of modules can be specified using the syntax. Examples of specifications (valid for all of ``--logging-level``, environment variable and -`core.yaml `__ +`core.yaml `__ settings): :: diff --git a/docs/source/configtxgen.rst b/docs/source/configtxgen.rst index ea9181b2c54..2b2fb24d472 100644 --- a/docs/source/configtxgen.rst +++ b/docs/source/configtxgen.rst @@ -21,7 +21,7 @@ Configuration Profiles The configuration parameters supplied to the ``configtxgen`` tool are primarily provided by the ``configtx.yaml`` file. This file is located -at ``fabric/common/configtx/tool/configtx.yaml`` in the fabric.git +at ``fabric/sampleconfig/configtx.yaml`` in the fabric.git repository. This configuration file is split primarily into three pieces. @@ -104,7 +104,7 @@ example: 2017/03/01 21:24:24 Checking for configtx.yaml at: 2017/03/01 21:24:24 Checking for configtx.yaml at: 2017/03/01 21:24:24 Checking for configtx.yaml at: /home/yellickj/go/src/github.com/hyperledger/fabric/common/configtx/tool - 2017/03/01 21:24:24 map[orderer:map[BatchSize:map[MaxMessageCount:10 AbsoluteMaxBytes:99 MB PreferredMaxBytes:512 KB] Kafka:map[Brokers:[127.0.0.1:9092]] Organizations: OrdererType:solo Addresses:[127.0.0.1:7050] BatchTimeout:10s] application:map[Organizations:] profiles:map[SampleInsecureSolo:map[Orderer:map[BatchTimeout:10s BatchSize:map[MaxMessageCount:10 AbsoluteMaxBytes:99 MB PreferredMaxBytes:512 KB] Kafka:map[Brokers:[127.0.0.1:9092]] Organizations: OrdererType:solo Addresses:[127.0.0.1:7050]] Application:map[Organizations:]] SampleInsecureKafka:map[Orderer:map[Addresses:[127.0.0.1:7050] BatchTimeout:10s BatchSize:map[AbsoluteMaxBytes:99 MB PreferredMaxBytes:512 KB MaxMessageCount:10] Kafka:map[Brokers:[127.0.0.1:9092]] Organizations: OrdererType:kafka] Application:map[Organizations:]] SampleSingleMSPSolo:map[Orderer:map[OrdererType:solo Addresses:[127.0.0.1:7050] BatchTimeout:10s BatchSize:map[MaxMessageCount:10 AbsoluteMaxBytes:99 MB PreferredMaxBytes:512 KB] Kafka:map[Brokers:[127.0.0.1:9092]] Organizations:[map[Name:SampleOrg ID:DEFAULT MSPDir:msp/sampleconfig BCCSP:map[Default:SW SW:map[Hash:SHA3 Security:256 FileKeyStore:map[KeyStore:]]] AnchorPeers:[map[Host:127.0.0.1 Port:7051]]]]] Application:map[Organizations:[map[Name:SampleOrg ID:DEFAULT MSPDir:msp/sampleconfig BCCSP:map[Default:SW SW:map[Hash:SHA3 Security:256 FileKeyStore:map[KeyStore:]]] AnchorPeers:[map[Port:7051 Host:127.0.0.1]]]]]]] organizations:[map[Name:SampleOrg ID:DEFAULT MSPDir:msp/sampleconfig BCCSP:map[Default:SW SW:map[Hash:SHA3 Security:256 FileKeyStore:map[KeyStore:]]] AnchorPeers:[map[Host:127.0.0.1 Port:7051]]]]] + 2017/03/01 21:24:24 map[orderer:map[BatchSize:map[MaxMessageCount:10 AbsoluteMaxBytes:99 MB PreferredMaxBytes:512 KB] Kafka:map[Brokers:[127.0.0.1:9092]] Organizations: OrdererType:solo Addresses:[127.0.0.1:7050] BatchTimeout:10s] application:map[Organizations:] profiles:map[SampleInsecureSolo:map[Orderer:map[BatchTimeout:10s BatchSize:map[MaxMessageCount:10 AbsoluteMaxBytes:99 MB PreferredMaxBytes:512 KB] Kafka:map[Brokers:[127.0.0.1:9092]] Organizations: OrdererType:solo Addresses:[127.0.0.1:7050]] Application:map[Organizations:]] SampleInsecureKafka:map[Orderer:map[Addresses:[127.0.0.1:7050] BatchTimeout:10s BatchSize:map[AbsoluteMaxBytes:99 MB PreferredMaxBytes:512 KB MaxMessageCount:10] Kafka:map[Brokers:[127.0.0.1:9092]] Organizations: OrdererType:kafka] Application:map[Organizations:]] SampleSingleMSPSolo:map[Orderer:map[OrdererType:solo Addresses:[127.0.0.1:7050] BatchTimeout:10s BatchSize:map[MaxMessageCount:10 AbsoluteMaxBytes:99 MB PreferredMaxBytes:512 KB] Kafka:map[Brokers:[127.0.0.1:9092]] Organizations:[map[Name:SampleOrg ID:DEFAULT MSPDir:msp BCCSP:map[Default:SW SW:map[Hash:SHA3 Security:256 FileKeyStore:map[KeyStore:]]] AnchorPeers:[map[Host:127.0.0.1 Port:7051]]]]] Application:map[Organizations:[map[Name:SampleOrg ID:DEFAULT MSPDir:msp BCCSP:map[Default:SW SW:map[Hash:SHA3 Security:256 FileKeyStore:map[KeyStore:]]] AnchorPeers:[map[Port:7051 Host:127.0.0.1]]]]]]] organizations:[map[Name:SampleOrg ID:DEFAULT MSPDir:msp BCCSP:map[Default:SW SW:map[Hash:SHA3 Security:256 FileKeyStore:map[KeyStore:]]] AnchorPeers:[map[Host:127.0.0.1 Port:7051]]]]] 2017/03/01 21:24:24 Generating genesis block 2017/03/01 21:24:24 Writing genesis block 2017/03/01 21:24:24 Inspecting block diff --git a/docs/source/getting_started.rst b/docs/source/getting_started.rst index b722a434897..1c5949cc9e7 100644 --- a/docs/source/getting_started.rst +++ b/docs/source/getting_started.rst @@ -237,7 +237,7 @@ material and network endpoint information. the ``generateCfgTrx.sh`` script. Open the script and inspect the syntax for the two commands. If you do elect to pursue this route, you must replace the default ``configtx.yaml`` in the fabric source tree. Navigate to the - ``common/configtx/tool`` directory and replace the ``configtx.yaml`` file with + ``sampleconfig`` directory and replace the ``configtx.yaml`` file with the supplied yaml file in the ``e2e_cli`` directory. Then return to the ``fabric`` directory to execute the commands (you will run these manual commands from ``fabric``, NOT from ``e2e_cli``). Be sure to remove any existing artifacts from diff --git a/docs/source/ledger.rst b/docs/source/ledger.rst index 69edd74e495..8ebfcb6d30b 100644 --- a/docs/source/ledger.rst +++ b/docs/source/ledger.rst @@ -104,6 +104,6 @@ default embedded LevelDB, and move to CouchDB if you require the additional comp It is a good practice to model chaincode asset data as JSON, so that you have the option to perform complex rich queries if needed in the future. -To enable CouchDB as the state database, configure the /fabric/peer/core.yaml ``stateDatabase`` +To enable CouchDB as the state database, configure the /fabric/sampleconfig/core.yaml ``stateDatabase`` section. diff --git a/docs/source/peer-chaincode-devmode.rst b/docs/source/peer-chaincode-devmode.rst index dd17f0504cd..a615cb1295a 100644 --- a/docs/source/peer-chaincode-devmode.rst +++ b/docs/source/peer-chaincode-devmode.rst @@ -19,7 +19,7 @@ Start the orderer orderer The above starts the orderer in the local environment using default -orderer configuration as defined in ``orderer/orderer.yaml``. +orderer configuration as defined in ``sampleconfig/orderer.yaml``. Start the peer in dev mode -------------------------- @@ -28,7 +28,7 @@ Start the peer in dev mode peer node start --peer-defaultchain=false --peer-chaincodedev=true -The above command starts the peer using the default ``msp/sampleconfig`` +The above command starts the peer using the default ``sampleconfig/msp`` MSP. The ``--peer-chaincodedev=true`` puts it in “dev” mode. Create channels ch1 and ch2 diff --git a/events/config.go b/events/config.go index 6c93a81b456..884666eaae5 100644 --- a/events/config.go +++ b/events/config.go @@ -27,6 +27,7 @@ import ( "github.com/spf13/viper" "github.com/hyperledger/fabric/bccsp/factory" + "github.com/hyperledger/fabric/core/config" "github.com/hyperledger/fabric/msp" ) @@ -59,11 +60,10 @@ func SetupTestConfig() { viper.AutomaticEnv() replacer := strings.NewReplacer(".", "_") viper.SetEnvKeyReplacer(replacer) - viper.SetConfigName("core") // name of config file (without extension) - viper.AddConfigPath("./") // path to look for the config file in - viper.AddConfigPath("./../peer/") // path to look for the config file in - err := viper.ReadInConfig() // Find and read the config file - if err != nil { // Handle errors reading the config file + viper.SetConfigName("core") // name of config file (without extension) + config.AddDevConfigPath(nil) // path to look for the config file in + err := viper.ReadInConfig() // Find and read the config file + if err != nil { // Handle errors reading the config file panic(fmt.Errorf("Fatal error config file: %s \n", err)) } diff --git a/events/events_test.go b/events/events_test.go index d3c3d8aca32..c76fefc73c8 100644 --- a/events/events_test.go +++ b/events/events_test.go @@ -25,6 +25,7 @@ import ( "time" "github.com/golang/protobuf/ptypes/timestamp" + "github.com/hyperledger/fabric/core/config" "github.com/hyperledger/fabric/events/consumer" "github.com/hyperledger/fabric/events/producer" "github.com/hyperledger/fabric/protos/common" @@ -333,7 +334,7 @@ func TestMain(m *testing.M) { SetupTestConfig() var opts []grpc.ServerOption if viper.GetBool("peer.tls.enabled") { - creds, err := credentials.NewServerTLSFromFile(viper.GetString("peer.tls.cert.file"), viper.GetString("peer.tls.key.file")) + creds, err := credentials.NewServerTLSFromFile(config.GetPath("peer.tls.cert.file"), config.GetPath("peer.tls.key.file")) if err != nil { grpclog.Fatalf("Failed to generate credentials %v", err) } diff --git a/events/producer/producer_test.go b/events/producer/producer_test.go index 87474033c66..7568209b878 100644 --- a/events/producer/producer_test.go +++ b/events/producer/producer_test.go @@ -123,8 +123,7 @@ var signerSerialized []byte func TestMain(m *testing.M) { // setup crypto algorithms // setup the MSP manager so that we can sign/verify - mspMgrConfigDir := "../../msp/sampleconfig/" - err := msptesttools.LoadMSPSetupForTesting(mspMgrConfigDir) + err := msptesttools.LoadMSPSetupForTesting() if err != nil { fmt.Printf("Could not initialize msp, err %s", err) os.Exit(-1) diff --git a/examples/ccchecker/init.go b/examples/ccchecker/init.go index 53422d112e4..e0120c7e733 100644 --- a/examples/ccchecker/init.go +++ b/examples/ccchecker/init.go @@ -24,6 +24,7 @@ import ( "github.com/spf13/pflag" "github.com/spf13/viper" + "github.com/hyperledger/fabric/core/config" "github.com/hyperledger/fabric/peer/common" ) @@ -44,30 +45,44 @@ func initCCCheckerParams(mainFlags *pflag.FlagSet) { //read yaml file from -y . Defaults to ../../peer func initYaml(mainFlags *pflag.FlagSet) { + defaultConfigPath, err := config.GetDevConfigDir() + if err != nil { + fmt.Printf("Fatal error when getting DevConfigDir: %s\n", err) + os.Exit(2) + } + // For environment variables. + viper.SetConfigName(cmdRoot) viper.SetEnvPrefix(cmdRoot) viper.AutomaticEnv() replacer := strings.NewReplacer(".", "_") viper.SetEnvKeyReplacer(replacer) pathToYaml := "" - mainFlags.StringVarP(&pathToYaml, "yamlfile", "y", "../../peer", "Path to core.yaml defined for peer") + mainFlags.StringVarP(&pathToYaml, "yamlfile", "y", defaultConfigPath, "Path to core.yaml defined for peer") + + viper.AddConfigPath(pathToYaml) - err := common.InitConfig(cmdRoot) - if err != nil { // Handle errors reading the config file + err = viper.ReadInConfig() // Find and read the config file + if err != nil { // Handle errors reading the config file fmt.Printf("Fatal error when reading %s config file: %s\n", cmdRoot, err) os.Exit(2) } } -//initialize MSP from -m . Defaults to ../../msp/sampleconfig +//initialize MSP from -m . Defaults to ../../sampleconfig/msp func initMSP(mainFlags *pflag.FlagSet) { + defaultMspDir, err := config.GetDevMspDir() + if err != nil { + panic(err.Error()) + } + mspMgrConfigDir := "" mspID := "" - mainFlags.StringVarP(&mspMgrConfigDir, "mspcfgdir", "m", "../../msp/sampleconfig/", "Path to MSP dir") + mainFlags.StringVarP(&mspMgrConfigDir, "mspcfgdir", "m", defaultMspDir, "Path to MSP dir") mainFlags.StringVarP(&mspID, "mspid", "i", "DEFAULT", "MSP ID") - err := common.InitCrypto(mspMgrConfigDir, mspID) + err = common.InitCrypto(mspMgrConfigDir, mspID) if err != nil { panic(err.Error()) } diff --git a/examples/e2e_cli/configtx.yaml b/examples/e2e_cli/configtx.yaml index a5e80478e0d..de06a51aadd 100644 --- a/examples/e2e_cli/configtx.yaml +++ b/examples/e2e_cli/configtx.yaml @@ -41,7 +41,7 @@ Organizations: ID: OrdererMSP # MSPDir is the filesystem path which contains the MSP configuration - MSPDir: examples/e2e_cli/crypto/orderer/localMspConfig + MSPDir: crypto/orderer/localMspConfig # BCCSP (Blockchain crypto provider): Select which crypto implementation or # library to use @@ -63,7 +63,7 @@ Organizations: # ID to load the MSP definition as ID: Org0MSP - MSPDir: examples/e2e_cli/crypto/peer/peer0/localMspConfig + MSPDir: crypto/peer/peer0/localMspConfig # BCCSP (Blockchain crypto provider): Select which crypto implementation or # library to use @@ -92,7 +92,7 @@ Organizations: # ID to load the MSP definition as ID: Org1MSP - MSPDir: examples/e2e_cli/crypto/peer/peer2/localMspConfig + MSPDir: crypto/peer/peer2/localMspConfig # BCCSP (Blockchain crypto provider): Select which crypto implementation or # library to use diff --git a/examples/e2e_cli/docker-compose-no-tls.yaml b/examples/e2e_cli/docker-compose-no-tls.yaml index 6128b2696e7..7152bac750c 100644 --- a/examples/e2e_cli/docker-compose-no-tls.yaml +++ b/examples/e2e_cli/docker-compose-no-tls.yaml @@ -29,7 +29,7 @@ services: - CORE_PEER_LOCALMSPID=Org0MSP volumes: - /var/run/:/host/var/run/ - - ./crypto/peer/peer0/localMspConfig:/etc/hyperledger/fabric/msp/sampleconfig + - ./crypto/peer/peer0/localMspConfig:/etc/hyperledger/fabric/msp ports: - 7051:7051 - 7053:7053 @@ -47,7 +47,7 @@ services: - CORE_PEER_LOCALMSPID=Org0MSP volumes: - /var/run/:/host/var/run/ - - ./crypto/peer/peer1/localMspConfig:/etc/hyperledger/fabric/msp/sampleconfig + - ./crypto/peer/peer1/localMspConfig:/etc/hyperledger/fabric/msp ports: - 8051:7051 - 8053:7053 @@ -66,7 +66,7 @@ services: - CORE_PEER_LOCALMSPID=Org1MSP volumes: - /var/run/:/host/var/run/ - - ./crypto/peer/peer2/localMspConfig:/etc/hyperledger/fabric/msp/sampleconfig + - ./crypto/peer/peer2/localMspConfig:/etc/hyperledger/fabric/msp ports: - 9051:7051 - 9053:7053 @@ -86,7 +86,7 @@ services: - CORE_PEER_LOCALMSPID=Org1MSP volumes: - /var/run/:/host/var/run/ - - ./crypto/peer/peer3/localMspConfig:/etc/hyperledger/fabric/msp/sampleconfig + - ./crypto/peer/peer3/localMspConfig:/etc/hyperledger/fabric/msp ports: - 10051:7051 - 10053:7053 diff --git a/examples/e2e_cli/docker-compose.yaml b/examples/e2e_cli/docker-compose.yaml index d73d470c261..a537ae2642f 100644 --- a/examples/e2e_cli/docker-compose.yaml +++ b/examples/e2e_cli/docker-compose.yaml @@ -48,15 +48,15 @@ services: - CORE_PEER_ADDRESS=peer0:7051 - CORE_PEER_GOSSIP_EXTERNALENDPOINT=peer0:7051 - CORE_PEER_LOCALMSPID=Org0MSP - - CORE_PEER_TLS_CERT_FILE=/etc/hyperledger/fabric/msp/sampleconfig/signcerts/peer0Signer.pem - - CORE_PEER_TLS_KEY_FILE=/etc/hyperledger/fabric/msp/sampleconfig/keystore/peer0Signer.pem - - CORE_PEER_TLS_ROOTCERT_FILE=/etc/hyperledger/fabric/msp/sampleconfig/cacerts/peerOrg0.pem + - CORE_PEER_TLS_CERT_FILE=/etc/hyperledger/fabric/msp/signcerts/peer0Signer.pem + - CORE_PEER_TLS_KEY_FILE=/etc/hyperledger/fabric/msp/keystore/peer0Signer.pem + - CORE_PEER_TLS_ROOTCERT_FILE=/etc/hyperledger/fabric/msp/cacerts/peerOrg0.pem # - CORE_PEER_TLS_SERVERHOSTOVERRIDE=peer0 # - CORE_LEDGER_STATE_STATEDATABASE=CouchDB # - CORE_LEDGER_STATE_COUCHDBCONFIG_COUCHDBADDRESS=couchdb0:5984 volumes: - /var/run/:/host/var/run/ - - ./crypto/peer/peer0/localMspConfig:/etc/hyperledger/fabric/msp/sampleconfig + - ./crypto/peer/peer0/localMspConfig:/etc/hyperledger/fabric/msp ports: - 7051:7051 - 7053:7053 @@ -83,15 +83,15 @@ services: - CORE_PEER_GOSSIP_EXTERNALENDPOINT=peer1:7051 - CORE_PEER_GOSSIP_BOOTSTRAP=peer0:7051 - CORE_PEER_LOCALMSPID=Org0MSP - - CORE_PEER_TLS_CERT_FILE=/etc/hyperledger/fabric/msp/sampleconfig/signcerts/peer1Signer.pem - - CORE_PEER_TLS_KEY_FILE=/etc/hyperledger/fabric/msp/sampleconfig/keystore/peer1Signer.pem - - CORE_PEER_TLS_ROOTCERT_FILE=/etc/hyperledger/fabric/msp/sampleconfig/cacerts/peerOrg0.pem + - CORE_PEER_TLS_CERT_FILE=/etc/hyperledger/fabric/msp/signcerts/peer1Signer.pem + - CORE_PEER_TLS_KEY_FILE=/etc/hyperledger/fabric/msp/keystore/peer1Signer.pem + - CORE_PEER_TLS_ROOTCERT_FILE=/etc/hyperledger/fabric/msp/cacerts/peerOrg0.pem # - CORE_PEER_TLS_SERVERHOSTOVERRIDE=peer1 # - CORE_LEDGER_STATE_STATEDATABASE=CouchDB # - CORE_LEDGER_STATE_COUCHDBCONFIG_COUCHDBADDRESS=couchdb1:5984 volumes: - /var/run/:/host/var/run/ - - ./crypto/peer/peer1/localMspConfig:/etc/hyperledger/fabric/msp/sampleconfig + - ./crypto/peer/peer1/localMspConfig:/etc/hyperledger/fabric/msp ports: - 8051:7051 - 8053:7053 @@ -119,15 +119,15 @@ services: - CORE_PEER_GOSSIP_EXTERNALENDPOINT=peer2:7051 - CORE_PEER_GOSSIP_BOOTSTRAP=peer2:7051 - CORE_PEER_LOCALMSPID=Org1MSP - - CORE_PEER_TLS_CERT_FILE=/etc/hyperledger/fabric/msp/sampleconfig/signcerts/peer2Signer.pem - - CORE_PEER_TLS_KEY_FILE=/etc/hyperledger/fabric/msp/sampleconfig/keystore/peer2Signer.pem - - CORE_PEER_TLS_ROOTCERT_FILE=/etc/hyperledger/fabric/msp/sampleconfig/admincerts/peerOrg1.pem + - CORE_PEER_TLS_CERT_FILE=/etc/hyperledger/fabric/msp/signcerts/peer2Signer.pem + - CORE_PEER_TLS_KEY_FILE=/etc/hyperledger/fabric/msp/keystore/peer2Signer.pem + - CORE_PEER_TLS_ROOTCERT_FILE=/etc/hyperledger/fabric/msp/admincerts/peerOrg1.pem #- CORE_PEER_TLS_SERVERHOSTOVERRIDE=peer2 # - CORE_LEDGER_STATE_STATEDATABASE=CouchDB # - CORE_LEDGER_STATE_COUCHDBCONFIG_COUCHDBADDRESS=couchdb2:5984 volumes: - /var/run/:/host/var/run/ - - ./crypto/peer/peer2/localMspConfig:/etc/hyperledger/fabric/msp/sampleconfig + - ./crypto/peer/peer2/localMspConfig:/etc/hyperledger/fabric/msp ports: - 9051:7051 - 9053:7053 @@ -156,15 +156,15 @@ services: - CORE_PEER_GOSSIP_EXTERNALENDPOINT=peer3:7051 - CORE_PEER_GOSSIP_BOOTSTRAP=peer2:7051 - CORE_PEER_LOCALMSPID=Org1MSP - - CORE_PEER_TLS_CERT_FILE=/etc/hyperledger/fabric/msp/sampleconfig/signcerts/peer3Signer.pem - - CORE_PEER_TLS_KEY_FILE=/etc/hyperledger/fabric/msp/sampleconfig/keystore/peer3Signer.pem - - CORE_PEER_TLS_ROOTCERT_FILE=/etc/hyperledger/fabric/msp/sampleconfig/cacerts/peerOrg1.pem + - CORE_PEER_TLS_CERT_FILE=/etc/hyperledger/fabric/msp/signcerts/peer3Signer.pem + - CORE_PEER_TLS_KEY_FILE=/etc/hyperledger/fabric/msp/keystore/peer3Signer.pem + - CORE_PEER_TLS_ROOTCERT_FILE=/etc/hyperledger/fabric/msp/cacerts/peerOrg1.pem # - CORE_PEER_TLS_SERVERHOSTOVERRIDE=peer3 # - CORE_LEDGER_STATE_STATEDATABASE=CouchDB # - CORE_LEDGER_STATE_COUCHDBCONFIG_COUCHDBADDRESS=couchdb3:5984 volumes: - /var/run/:/host/var/run/ - - ./crypto/peer/peer3/localMspConfig:/etc/hyperledger/fabric/msp/sampleconfig + - ./crypto/peer/peer3/localMspConfig:/etc/hyperledger/fabric/msp ports: - 10051:7051 - 10053:7053 diff --git a/examples/e2e_cli/end-to-end.rst b/examples/e2e_cli/end-to-end.rst index 4cc4d910a30..402e0ab502f 100644 --- a/examples/e2e_cli/end-to-end.rst +++ b/examples/e2e_cli/end-to-end.rst @@ -126,7 +126,7 @@ sign/verify/authenticate capabilities. Manually generate the artifacts (optional) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -In your vagrant environment, navigate to the ``/common/configtx/tool`` +In your vagrant environment, navigate to the ``/sampleconfig`` directory and replace the ``configtx.yaml`` file with the supplied yaml file in the ``/e2e_cli`` directory. Then return to the ``/e2e_cli`` directory. diff --git a/examples/e2e_cli/generateCfgTrx.sh b/examples/e2e_cli/generateCfgTrx.sh index ca512c4f25c..98973d74bfa 100755 --- a/examples/e2e_cli/generateCfgTrx.sh +++ b/examples/e2e_cli/generateCfgTrx.sh @@ -1,30 +1,31 @@ #!/bin/bash +set -e + CHANNEL_NAME=$1 if [ -z "$1" ]; then echo "Setting channel to default name 'mychannel'" CHANNEL_NAME="mychannel" fi +export FABRIC_ROOT=$PWD/../.. +export FABRIC_CFG_PATH=$PWD + echo "Channel name - "$CHANNEL_NAME echo -#Backup the original configtx.yaml -cp ../../common/configtx/tool/configtx.yaml ../../common/configtx/tool/configtx.yaml.orig -cp configtx.yaml ../../common/configtx/tool/configtx.yaml +CONFIGTXGEN=`which configtxgen || /bin/true` -cd $PWD/../../ -echo "Building configtxgen" -make configtxgen +if [ "$CONFIGTXGEN" == "" ]; then + echo "Building configtxgen" + make -C $FABRIC_ROOT configtxgen + CONFIGTXGEN=$FABRIC_ROOT/build/bin/configtxgen +else + echo "Using configtxgen -> $CONFIGTXGEN" +fi echo "Generating genesis block" -./build/bin/configtxgen -profile TwoOrgs -outputBlock orderer.block -mv orderer.block examples/e2e_cli/crypto/orderer/orderer.block +$CONFIGTXGEN -profile TwoOrgs -outputBlock crypto/orderer/orderer.block echo "Generating channel configuration transaction" -./build/bin/configtxgen -profile TwoOrgs -outputCreateChannelTx channel.tx -channelID $CHANNEL_NAME -mv channel.tx examples/e2e_cli/crypto/orderer/channel.tx - -#reset configtx.yaml file to its original -cp common/configtx/tool/configtx.yaml.orig common/configtx/tool/configtx.yaml -rm common/configtx/tool/configtx.yaml.orig +$CONFIGTXGEN -profile TwoOrgs -outputCreateChannelTx crypto/orderer/channel.tx -channelID $CHANNEL_NAME diff --git a/gossip/comm/comm_test.go b/gossip/comm/comm_test.go index 534d217a7df..274f015542c 100644 --- a/gossip/comm/comm_test.go +++ b/gossip/comm/comm_test.go @@ -30,6 +30,7 @@ import ( "time" "github.com/hyperledger/fabric/bccsp/factory" + "github.com/hyperledger/fabric/core/config" "github.com/hyperledger/fabric/gossip/api" "github.com/hyperledger/fabric/gossip/common" "github.com/hyperledger/fabric/gossip/identity" @@ -187,7 +188,7 @@ func handshaker(endpoint string, comm Comm, t *testing.T, sigMutator func([]byte func TestViperConfig(t *testing.T) { viper.SetConfigName("core") viper.SetEnvPrefix("CORE") - viper.AddConfigPath("./../../peer") + config.AddDevConfigPath(nil) viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_")) viper.AutomaticEnv() err := viper.ReadInConfig() diff --git a/gossip/discovery/discovery_test.go b/gossip/discovery/discovery_test.go index a160211892a..db84904d61b 100644 --- a/gossip/discovery/discovery_test.go +++ b/gossip/discovery/discovery_test.go @@ -29,6 +29,7 @@ import ( "testing" "time" + "github.com/hyperledger/fabric/core/config" "github.com/hyperledger/fabric/gossip/common" proto "github.com/hyperledger/fabric/protos/gossip" "github.com/spf13/viper" @@ -764,7 +765,7 @@ func TestConfigFromFile(t *testing.T) { aliveExpirationCheckInterval = 0 * time.Second viper.SetConfigName("core") viper.SetEnvPrefix("CORE") - viper.AddConfigPath("./../../peer") + config.AddDevConfigPath(nil) viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_")) viper.AutomaticEnv() err := viper.ReadInConfig() diff --git a/gossip/election/election_test.go b/gossip/election/election_test.go index f5305a3ac77..8bc7eda7730 100644 --- a/gossip/election/election_test.go +++ b/gossip/election/election_test.go @@ -24,6 +24,7 @@ import ( "testing" "time" + "github.com/hyperledger/fabric/core/config" "github.com/spf13/viper" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/mock" @@ -387,7 +388,7 @@ func TestConfigFromFile(t *testing.T) { viper.Reset() viper.SetConfigName("core") viper.SetEnvPrefix("CORE") - viper.AddConfigPath("./../../peer") + config.AddDevConfigPath(nil) viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_")) viper.AutomaticEnv() err := viper.ReadInConfig() diff --git a/gossip/gossip/algo/pull_test.go b/gossip/gossip/algo/pull_test.go index 90eb1ef527a..f3bae419eef 100644 --- a/gossip/gossip/algo/pull_test.go +++ b/gossip/gossip/algo/pull_test.go @@ -25,6 +25,7 @@ import ( "testing" "time" + "github.com/hyperledger/fabric/core/config" "github.com/hyperledger/fabric/gossip/util" "github.com/spf13/viper" "github.com/stretchr/testify/assert" @@ -566,12 +567,12 @@ func TestDefaultConfig(t *testing.T) { assert.Equal(t, time.Duration(1)*time.Second, util.GetDurationOrDefault("peer.gossip.requestWaitTime", defRequestWaitTime)) assert.Equal(t, time.Duration(2)*time.Second, util.GetDurationOrDefault("peer.gossip.responseWaitTime", defResponseWaitTime)) - // Check if the properties in the config file (peer/core.yaml) + // Check if the properties in the config file (core.yaml) // are set to the desired duration. viper.Reset() viper.SetConfigName("core") viper.SetEnvPrefix("CORE") - viper.AddConfigPath("./../../../peer") + config.AddDevConfigPath(nil) viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_")) viper.AutomaticEnv() err := viper.ReadInConfig() diff --git a/gossip/integration/integration.go b/gossip/integration/integration.go index e5973c5d325..5ceb65f1436 100644 --- a/gossip/integration/integration.go +++ b/gossip/integration/integration.go @@ -22,6 +22,7 @@ import ( "strings" "time" + "github.com/hyperledger/fabric/core/config" "github.com/hyperledger/fabric/gossip/api" "github.com/hyperledger/fabric/gossip/gossip" "github.com/hyperledger/fabric/gossip/identity" @@ -40,7 +41,7 @@ func newConfig(selfEndpoint string, externalEndpoint string, bootPeers ...string var cert *tls.Certificate if viper.GetBool("peer.tls.enabled") { - certTmp, err := tls.LoadX509KeyPair(viper.GetString("peer.tls.cert.file"), viper.GetString("peer.tls.key.file")) + certTmp, err := tls.LoadX509KeyPair(config.GetPath("peer.tls.cert.file"), config.GetPath("peer.tls.key.file")) if err != nil { panic(err) } diff --git a/gossip/integration/integration_test.go b/gossip/integration/integration_test.go index 06961186c1a..8c0f550cce9 100644 --- a/gossip/integration/integration_test.go +++ b/gossip/integration/integration_test.go @@ -23,6 +23,7 @@ import ( "testing" "time" + "github.com/hyperledger/fabric/core/config" "github.com/hyperledger/fabric/gossip/api" "github.com/hyperledger/fabric/gossip/common" "github.com/hyperledger/fabric/gossip/identity" @@ -47,7 +48,7 @@ func TestNewGossipCryptoService(t *testing.T) { endpoint2 := "localhost:5612" endpoint3 := "localhost:5613" - msptesttools.LoadMSPSetupForTesting("../../msp/sampleconfig") + msptesttools.LoadMSPSetupForTesting() peerIdentity, _ := mgmt.GetLocalSigningIdentityOrPanic().Serialize() cryptSvc := &cryptoService{} @@ -72,7 +73,7 @@ func TestNewGossipCryptoService(t *testing.T) { func setupTestEnv() { viper.SetConfigName("core") viper.SetEnvPrefix("CORE") - viper.AddConfigPath("./../../peer") + config.AddDevConfigPath(nil) viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_")) viper.AutomaticEnv() err := viper.ReadInConfig() diff --git a/gossip/service/gossip_service_test.go b/gossip/service/gossip_service_test.go index 56e78c6e66c..f2251f18559 100644 --- a/gossip/service/gossip_service_test.go +++ b/gossip/service/gossip_service_test.go @@ -53,7 +53,7 @@ func TestInitGossipService(t *testing.T) { go grpcServer.Serve(socket) defer grpcServer.Stop() - msptesttools.LoadMSPSetupForTesting("../../msp/sampleconfig") + msptesttools.LoadMSPSetupForTesting() identity, _ := mgmt.GetLocalSigningIdentityOrPanic().Serialize() wg := sync.WaitGroup{} diff --git a/images/orderer/Dockerfile.in b/images/orderer/Dockerfile.in index f7a4a77af51..99738d1a6fd 100644 --- a/images/orderer/Dockerfile.in +++ b/images/orderer/Dockerfile.in @@ -1,11 +1,8 @@ FROM _BASE_NS_/fabric-baseos:_BASE_TAG_ -ENV ORDERER_CFG_PATH /etc/hyperledger/fabric -ENV ORDERER_GENERAL_LOCALMSPDIR $ORDERER_CFG_PATH/msp/sampleconfig +ENV FABRIC_CFG_PATH /etc/hyperledger/fabric ENV ORDERER_GENERAL_GENESISPROFILE=SampleInsecureSolo -RUN mkdir -p /var/hyperledger/production $ORDERER_CFG_PATH +RUN mkdir -p /var/hyperledger/production $FABRIC_CFG_PATH COPY payload/orderer /usr/local/bin -COPY payload/configtx.yaml $ORDERER_CFG_PATH/ -COPY payload/orderer.yaml $ORDERER_CFG_PATH/ -ADD payload/msp-sampleconfig.tar.bz2 $ORDERER_CFG_PATH/ +ADD payload/sampleconfig.tar.bz2 $FABRIC_CFG_PATH/ EXPOSE 7050 CMD ["orderer"] diff --git a/images/peer/Dockerfile.in b/images/peer/Dockerfile.in index abfb4f51d50..1fd14c6deb2 100644 --- a/images/peer/Dockerfile.in +++ b/images/peer/Dockerfile.in @@ -1,9 +1,6 @@ FROM _BASE_NS_/fabric-baseos:_BASE_TAG_ -ENV PEER_CFG_PATH /etc/hyperledger/fabric -ENV CORE_PEER_MSPCONFIGPATH $PEER_CFG_PATH/msp/sampleconfig -RUN mkdir -p /var/hyperledger/production $PEER_CFG_PATH +ENV FABRIC_CFG_PATH /etc/hyperledger/fabric +RUN mkdir -p /var/hyperledger/production $FABRIC_CFG_PATH COPY payload/peer /usr/local/bin -COPY payload/core.yaml $PEER_CFG_PATH -ADD payload/msp-sampleconfig.tar.bz2 $PEER_CFG_PATH -ADD payload/configtx.yaml $PEER_CFG_PATH +ADD payload/sampleconfig.tar.bz2 $FABRIC_CFG_PATH CMD ["peer","node","start"] diff --git a/images/testenv/Dockerfile.in b/images/testenv/Dockerfile.in index 9606be5e5a0..640ff7eec8f 100644 --- a/images/testenv/Dockerfile.in +++ b/images/testenv/Dockerfile.in @@ -1,21 +1,15 @@ FROM _NS_/fabric-buildenv:_TAG_ # fabric configuration locations -ENV PEER_CFG_PATH /etc/hyperledger/fabric -ENV ORDERER_CFG_PATH /etc/hyperledger/fabric -ENV CORE_PEER_MSPCONFIGPATH $PEER_CFG_PATH/msp/sampleconfig -ENV ORDERER_GENERAL_LOCALMSPDIR $ORDERER_CFG_PATH/msp/sampleconfig +ENV FABRIC_CFG_PATH /etc/hyperledger/fabric # create needed directories RUN mkdir -p \ - $PEER_CFG_PATH \ + $FABRIC_CFG_PATH \ /var/hyperledger/production # fabric configuration files -COPY payload/orderer.yaml $ORDERER_CFG_PATH -COPY payload/configtx.yaml $ORDERER_CFG_PATH -COPY payload/core.yaml $PEER_CFG_PATH -ADD payload/msp-sampleconfig.tar.bz2 $PEER_CFG_PATH +ADD payload/sampleconfig.tar.bz2 $FABRIC_CFG_PATH # fabric binaries COPY payload/orderer /usr/local/bin diff --git a/msp/mgmt/deserializer_test.go b/msp/mgmt/deserializer_test.go index e8a89941c9f..b0f787e6b8f 100644 --- a/msp/mgmt/deserializer_test.go +++ b/msp/mgmt/deserializer_test.go @@ -22,6 +22,7 @@ import ( "testing" msp2 "github.com/hyperledger/fabric/common/config/msp" + "github.com/hyperledger/fabric/core/config" "github.com/hyperledger/fabric/msp" "github.com/stretchr/testify/assert" ) @@ -67,8 +68,14 @@ func TestMspDeserializersManager_GetLocalDeserializer(t *testing.T) { } func TestMain(m *testing.M) { - var err error - testConf, err := msp.GetLocalMspConfig("../sampleconfig/", nil, "DEFAULT") + + mspDir, err := config.GetDevMspDir() + if err != nil { + fmt.Printf("Error getting DevMspDir: %s", err) + os.Exit(-1) + } + + testConf, err := msp.GetLocalMspConfig(mspDir, nil, "DEFAULT") if err != nil { fmt.Printf("Setup should have succeeded, got err %s instead", err) os.Exit(-1) diff --git a/msp/mgmt/mgmt.go b/msp/mgmt/mgmt.go index 602d84fd9c8..02f4d652ad7 100644 --- a/msp/mgmt/mgmt.go +++ b/msp/mgmt/mgmt.go @@ -25,7 +25,7 @@ import ( "github.com/hyperledger/fabric/bccsp/factory" configvaluesmsp "github.com/hyperledger/fabric/common/config/msp" "github.com/hyperledger/fabric/common/flogging" - + "github.com/hyperledger/fabric/core/config" "github.com/hyperledger/fabric/msp" ) @@ -43,6 +43,16 @@ func LoadLocalMsp(dir string, bccspConfig *factory.FactoryOpts, mspID string) er return GetLocalMSP().Setup(conf) } +// Loads the development local MSP for use in testing. Not valid for production/runtime context +func LoadDevMsp() error { + mspDir, err := config.GetDevMspDir() + if err != nil { + return err + } + + return LoadLocalMsp(mspDir, nil, "DEFAULT") +} + // FIXME: AS SOON AS THE CHAIN MANAGEMENT CODE IS COMPLETE, // THESE MAPS AND HELPSER FUNCTIONS SHOULD DISAPPEAR BECAUSE // OWNERSHIP OF PER-CHAIN MSP MANAGERS WILL BE HANDLED BY IT; diff --git a/msp/mgmt/peermsp_test.go b/msp/mgmt/peermsp_test.go index 7ac86184a1b..8995095dbea 100644 --- a/msp/mgmt/peermsp_test.go +++ b/msp/mgmt/peermsp_test.go @@ -16,24 +16,10 @@ limitations under the License. package mgmt -import ( - "io/ioutil" - "os" - "testing" -) - -// getTestMSPConfigPath returns the path to sampleconfig for unit tests -func getTestMSPConfigPath() string { - cfgPath := os.Getenv("PEER_CFG_PATH") + "/msp/sampleconfig/" - if _, err := ioutil.ReadDir(cfgPath); err != nil { - cfgPath = os.Getenv("GOPATH") + "/src/github.com/hyperledger/fabric/msp/sampleconfig/" - } - return cfgPath -} +import "testing" func TestLocalMSP(t *testing.T) { - testMSPConfigPath := getTestMSPConfigPath() - err := LoadLocalMsp(testMSPConfigPath, nil, "DEFAULT") + err := LoadDevMsp() if err != nil { t.Fatalf("LoadLocalMsp failed, err %s", err) diff --git a/msp/mgmt/testtools/config.go b/msp/mgmt/testtools/config.go index 36cbe81bb18..f5f04109575 100644 --- a/msp/mgmt/testtools/config.go +++ b/msp/mgmt/testtools/config.go @@ -17,35 +17,17 @@ limitations under the License. package msptesttools import ( - "os" - "path/filepath" - "github.com/hyperledger/fabric/common/util" + "github.com/hyperledger/fabric/core/config" "github.com/hyperledger/fabric/msp" "github.com/hyperledger/fabric/msp/mgmt" ) -func getConfigPath(dir string) (string, error) { - // Try to read the dir - if _, err := os.Stat(dir); err != nil { - cfg := os.Getenv("PEER_CFG_PATH") - if cfg != "" { - dir = filepath.Join(cfg, dir) - } else { - dir = filepath.Join(os.Getenv("GOPATH"), "/src/github.com/hyperledger/fabric/msp/sampleconfig/") - } - if _, err := os.Stat(dir); err != nil { - return "", err - } - } - return dir, nil -} - // LoadTestMSPSetup sets up the local MSP // and a chain MSP for the default chain -func LoadMSPSetupForTesting(dir string) error { - var err error - if dir, err = getConfigPath(dir); err != nil { +func LoadMSPSetupForTesting() error { + dir, err := config.GetDevMspDir() + if err != nil { return err } conf, err := msp.GetLocalMspConfig(dir, nil, "DEFAULT") diff --git a/msp/mgmt/testtools/config_test.go b/msp/mgmt/testtools/config_test.go index 837ab7ed7c8..19cbd514ae2 100644 --- a/msp/mgmt/testtools/config_test.go +++ b/msp/mgmt/testtools/config_test.go @@ -19,24 +19,12 @@ package msptesttools import ( "testing" - "io/ioutil" - "os" - "github.com/hyperledger/fabric/common/util" "github.com/hyperledger/fabric/msp/mgmt" ) -func getTESTMSPConfigPath() string { - cfgPath := os.Getenv("PEER_CFG_PATH") + "/msp/sampleconfig/" - if _, err := ioutil.ReadDir(cfgPath); err != nil { - cfgPath = os.Getenv("GOPATH") + "/src/github.com/hyperledger/fabric/msp/sampleconfig/" - } - return cfgPath -} - func TestFakeSetup(t *testing.T) { - testMSPConfigPath := getTESTMSPConfigPath() - err := LoadMSPSetupForTesting(testMSPConfigPath) + err := LoadMSPSetupForTesting() if err != nil { t.Fatalf("LoadLocalMsp failed, err %s", err) } diff --git a/msp/msp_test.go b/msp/msp_test.go index 3a7bc3d0a98..bb891ae333f 100644 --- a/msp/msp_test.go +++ b/msp/msp_test.go @@ -25,6 +25,7 @@ import ( "github.com/golang/protobuf/proto" "github.com/hyperledger/fabric/bccsp" + "github.com/hyperledger/fabric/core/config" "github.com/hyperledger/fabric/protos/msp" "github.com/stretchr/testify/assert" ) @@ -462,7 +463,13 @@ var mspMgr MSPManager func TestMain(m *testing.M) { var err error - conf, err = GetLocalMspConfig("./sampleconfig/", nil, "DEFAULT") + mspDir, err := config.GetDevMspDir() + if err != nil { + fmt.Printf("Errog getting DevMspDir: %s", err) + os.Exit(-1) + } + + conf, err = GetLocalMspConfig(mspDir, nil, "DEFAULT") if err != nil { fmt.Printf("Setup should have succeeded, got err %s instead", err) os.Exit(-1) diff --git a/orderer/localconfig/config.go b/orderer/localconfig/config.go index 5b7d274b278..9b096a29a45 100644 --- a/orderer/localconfig/config.go +++ b/orderer/localconfig/config.go @@ -17,8 +17,6 @@ limitations under the License. package config import ( - "os" - "path/filepath" "strings" "time" @@ -29,6 +27,10 @@ import ( "github.com/op/go-logging" "github.com/spf13/viper" + cf "github.com/hyperledger/fabric/core/config" + + "path/filepath" + bccsp "github.com/hyperledger/fabric/bccsp/factory" ) @@ -158,13 +160,13 @@ var defaults = TopLevel{ ListenPort: 7050, GenesisMethod: "provisional", GenesisProfile: "SampleSingleMSPSolo", - GenesisFile: "./genesisblock", + GenesisFile: "genesisblock", Profile: Profile{ Enabled: false, Address: "0.0.0.0:6060", }, LogLevel: "INFO", - LocalMSPDir: "../msp/sampleconfig/", + LocalMSPDir: "msp", LocalMSPID: "DEFAULT", BCCSP: &bccsp.DefaultOpts, }, @@ -202,9 +204,7 @@ var defaults = TopLevel{ }, } -func (c *TopLevel) completeInitialization() { - defer logger.Infof("Validated configuration to: %+v", c) - +func (c *TopLevel) initDefaults() { for { switch { case c.General.LedgerType == "": @@ -236,10 +236,7 @@ func (c *TopLevel) completeInitialization() { c.General.Profile.Address = defaults.General.Profile.Address case c.General.LocalMSPDir == "": logger.Infof("General.LocalMSPDir unset, setting to %s", defaults.General.LocalMSPDir) - // Note, this is a bit of a weird one, the orderer may set the ORDERER_CFG_PATH after - // the file is initialized, so we cannot initialize this in the structure, so we - // deference the env portion here - c.General.LocalMSPDir = filepath.Join(os.Getenv("ORDERER_CFG_PATH"), defaults.General.LocalMSPDir) + c.General.LocalMSPDir = defaults.General.LocalMSPDir case c.General.LocalMSPID == "": logger.Infof("General.LocalMSPID unset, setting to %s", defaults.General.LocalMSPID) c.General.LocalMSPID = defaults.General.LocalMSPID @@ -261,32 +258,37 @@ func (c *TopLevel) completeInitialization() { } } +func translateCAs(configDir string, certificateAuthorities []string) []string { + + results := make([]string, 0) + + for _, ca := range certificateAuthorities { + result := cf.TranslatePath(configDir, ca) + results = append(results, result) + } + + return results +} + +func (c *TopLevel) completeInitialization(configDir string) { + defer logger.Infof("Validated configuration to: %+v", c) + c.initDefaults() + + // Translate any paths + c.General.TLS.RootCAs = translateCAs(configDir, c.General.TLS.RootCAs) + c.General.TLS.ClientRootCAs = translateCAs(configDir, c.General.TLS.ClientRootCAs) + cf.TranslatePathInPlace(configDir, &c.General.TLS.PrivateKey) + cf.TranslatePathInPlace(configDir, &c.General.TLS.Certificate) + + cf.TranslatePathInPlace(configDir, &c.General.GenesisFile) + cf.TranslatePathInPlace(configDir, &c.General.LocalMSPDir) +} + // Load parses the orderer.yaml file and environment, producing a struct suitable for config use func Load() *TopLevel { config := viper.New() - config.SetConfigName(configName) - - cfgPath := os.Getenv("ORDERER_CFG_PATH") - if cfgPath == "" { - logger.Infof("No ORDERER_CFG_PATH set, assuming development environment, deriving from Go path.") - // Path to look for the config file in based on GOPATH - gopath := os.Getenv("GOPATH") - for _, p := range filepath.SplitList(gopath) { - ordererPath := filepath.Join(p, "src/github.com/hyperledger/fabric/orderer/") - if _, err := os.Stat(filepath.Join(ordererPath, configFileName)); err != nil { - // The YAML file does not exist in this component of the go src - continue - } - cfgPath = ordererPath - } - if cfgPath == "" { - logger.Fatalf("Could not find %s, try setting ORDERER_CFG_PATH or GOPATH correctly.", configFileName) - } - logger.Infof("Setting ORDERER_CFG_PATH to: %s", cfgPath) - os.Setenv("ORDERER_CFG_PATH", cfgPath) - } - config.AddConfigPath(cfgPath) // Path to look for the config file in + cf.InitViper(config, configName) // for environment variables config.SetEnvPrefix(Prefix) @@ -296,7 +298,7 @@ func Load() *TopLevel { err := config.ReadInConfig() if err != nil { - logger.Panicf("Error reading configuration from %s in %s: %s", configFileName, cfgPath, err) + logger.Panicf("Error reading configuration %s: %s", configFileName, err) } var uconf TopLevel @@ -304,7 +306,8 @@ func Load() *TopLevel { if err != nil { logger.Panicf("Error unmarshaling config into struct: %s", err) } - uconf.completeInitialization() + + uconf.completeInitialization(filepath.Dir(config.ConfigFileUsed())) return &uconf } diff --git a/orderer/localconfig/config_test.go b/orderer/localconfig/config_test.go index 7e7e7db391e..3e0877aacbb 100644 --- a/orderer/localconfig/config_test.go +++ b/orderer/localconfig/config_test.go @@ -23,6 +23,7 @@ import ( "time" "github.com/hyperledger/fabric/common/viperutil" + cf "github.com/hyperledger/fabric/core/config" "github.com/spf13/viper" "github.com/stretchr/testify/assert" @@ -39,7 +40,7 @@ func TestGoodConfig(t *testing.T) { func TestBadConfig(t *testing.T) { config := viper.New() config.SetConfigName("orderer") - config.AddConfigPath("../") + cf.AddDevConfigPath(config) err := config.ReadInConfig() if err != nil { @@ -82,6 +83,8 @@ func TestEnvInnerVar(t *testing.T) { } } +const DummyPath = "/dummy/path" + func TestKafkaTLSConfig(t *testing.T) { testCases := []struct { name string @@ -97,9 +100,9 @@ func TestKafkaTLSConfig(t *testing.T) { t.Run(tc.name, func(t *testing.T) { uconf := &TopLevel{Kafka: Kafka{TLS: tc.tls}} if tc.shouldPanic { - assert.Panics(t, func() { uconf.completeInitialization() }, "should panic") + assert.Panics(t, func() { uconf.completeInitialization(DummyPath) }, "should panic") } else { - assert.NotPanics(t, func() { uconf.completeInitialization() }, "should not panic") + assert.NotPanics(t, func() { uconf.completeInitialization(DummyPath) }, "should not panic") } }) } diff --git a/orderer/network_test.go b/orderer/network_test.go index 52e5e2c3c81..1305249f870 100644 --- a/orderer/network_test.go +++ b/orderer/network_test.go @@ -317,8 +317,7 @@ func generateConfigEnv(peerNum uint64, grpcPort int, peerCommPort string, certFi tempDir, err := ioutil.TempDir("", "sbft_test_config") panicOnError(err) envs := []string{} - envs = append(envs, fmt.Sprintf("ORDERER_CFG_PATH=%s", ordererDir)) - envs = append(envs, fmt.Sprintf("ORDERER_GENERAL_LOCALMSPDIR=%s", ordererDir+"/../msp/sampleconfig")) + envs = append(envs, fmt.Sprintf("ORDERER_GENERAL_LOCALMSPDIR=%s", ordererDir+"/../sampleconfig/msp")) envs = append(envs, fmt.Sprintf("ORDERER_GENERAL_LISTENPORT=%d", grpcPort)) envs = append(envs, fmt.Sprintf("CONFIGTX_ORDERER_ORDERERTYPE=%s", "sbft")) envs = append(envs, fmt.Sprintf("ORDERER_GENERAL_GENESISPROFILE=%s", genesisconfig.SampleInsecureProfile)) diff --git a/orderer/sbft_test.go b/orderer/sbft_test.go index d787d19a063..ac28e6d814e 100644 --- a/orderer/sbft_test.go +++ b/orderer/sbft_test.go @@ -31,6 +31,7 @@ import ( genesisconfig "github.com/hyperledger/fabric/common/configtx/tool/localconfig" "github.com/hyperledger/fabric/common/configtx/tool/provisional" "github.com/hyperledger/fabric/common/localmsp" + cf "github.com/hyperledger/fabric/core/config" mspmgmt "github.com/hyperledger/fabric/msp/mgmt" "github.com/hyperledger/fabric/orderer/ledger" "github.com/hyperledger/fabric/orderer/ledger/ram" @@ -87,6 +88,11 @@ func TestSbftPeer(t *testing.T) { skipInShortMode(t) logging.SetLevel(logging.DEBUG, "") + mspDir, err := cf.GetDevMspDir() + if err != nil { + panic("could not get DevMspDir") + } + // Start SBFT dataTmpDir, err := ioutil.TempDir("", "sbft_test") if err != nil { @@ -114,7 +120,7 @@ func TestSbftPeer(t *testing.T) { // Start GRPC logger.Info("Creating a GRPC server.") conf := config.Load() - conf.General.LocalMSPDir = pwd + "/../msp/sampleconfig" + conf.General.LocalMSPDir = mspDir conf.General.LocalMSPID = "DEFAULT" lf := newRAMLedgerFactory() consenters := make(map[string]multichain.Consenter) diff --git a/peer/chaincode/upgrade_test.go b/peer/chaincode/upgrade_test.go index e5099b53b6f..60fdad9e4da 100644 --- a/peer/chaincode/upgrade_test.go +++ b/peer/chaincode/upgrade_test.go @@ -19,7 +19,6 @@ package chaincode import ( "errors" "fmt" - "os" "sync" "testing" @@ -36,20 +35,9 @@ func InitMSP() { } func initMSP() { - // TODO: determine the location of this config file - var alternativeCfgPath = os.Getenv("PEER_CFG_PATH") - var mspMgrConfigDir string - if alternativeCfgPath != "" { - mspMgrConfigDir = alternativeCfgPath + "/msp/sampleconfig/" - } else if _, err := os.Stat("./msp/sampleconfig/"); err == nil { - mspMgrConfigDir = "./msp/sampleconfig/" - } else { - mspMgrConfigDir = os.Getenv("GOPATH") + "/src/github.com/hyperledger/fabric/msp/sampleconfig/" - } - - err := msptesttools.LoadMSPSetupForTesting(mspMgrConfigDir) + err := msptesttools.LoadMSPSetupForTesting() if err != nil { - panic(fmt.Errorf("Fatal error when reading MSP config file %s: err %s\n", mspMgrConfigDir, err)) + panic(fmt.Errorf("Fatal error when reading MSP config: %s\n", err)) } } diff --git a/peer/channel/create_test.go b/peer/channel/create_test.go index aec313103e3..5314e94be3c 100644 --- a/peer/channel/create_test.go +++ b/peer/channel/create_test.go @@ -62,20 +62,9 @@ func InitMSP() { } func initMSP() { - // TODO: determine the location of this config file - var alternativeCfgPath = os.Getenv("PEER_CFG_PATH") - var mspMgrConfigDir string - if alternativeCfgPath != "" { - mspMgrConfigDir = alternativeCfgPath + "/msp/sampleconfig/" - } else if _, err := os.Stat("./msp/sampleconfig/"); err == nil { - mspMgrConfigDir = "./msp/sampleconfig/" - } else { - mspMgrConfigDir = os.Getenv("GOPATH") + "/src/github.com/hyperledger/fabric/msp/sampleconfig/" - } - - err := msptesttools.LoadMSPSetupForTesting(mspMgrConfigDir) + err := msptesttools.LoadMSPSetupForTesting() if err != nil { - panic(fmt.Errorf("Fatal error when reading MSP config file %s: err %s\n", mspMgrConfigDir, err)) + panic(fmt.Errorf("Fatal error when reading MSP config: err %s\n", err)) } } diff --git a/peer/common/common.go b/peer/common/common.go index 7191d496e51..7539b3df06d 100644 --- a/peer/common/common.go +++ b/peer/common/common.go @@ -18,13 +18,12 @@ package common import ( "fmt" - "os" - "path/filepath" "github.com/hyperledger/fabric/bccsp/factory" "github.com/hyperledger/fabric/common/errors" "github.com/hyperledger/fabric/common/flogging" "github.com/hyperledger/fabric/common/viperutil" + "github.com/hyperledger/fabric/core/config" "github.com/hyperledger/fabric/core/peer" "github.com/hyperledger/fabric/msp" mspmgmt "github.com/hyperledger/fabric/msp/mgmt" @@ -38,21 +37,7 @@ const UndefinedParamValue = "" //InitConfig initializes viper config func InitConfig(cmdRoot string) error { - var alternativeCfgPath = os.Getenv("PEER_CFG_PATH") - if alternativeCfgPath != "" { - viper.AddConfigPath(alternativeCfgPath) // Path to look for the config file in - } else { - viper.AddConfigPath("./") // Path to look for the config file in - // Path to look for the config file in based on GOPATH - gopath := os.Getenv("GOPATH") - for _, p := range filepath.SplitList(gopath) { - peerpath := filepath.Join(p, "src/github.com/hyperledger/fabric/peer") - viper.AddConfigPath(peerpath) - } - } - - // Now set the configuration file. - viper.SetConfigName(cmdRoot) // Name of config file (without extension) + config.InitViper(nil, cmdRoot) err := viper.ReadInConfig() // Find and read the config file if err != nil { // Handle errors reading the config file diff --git a/peer/gossip/sa/sa_test.go b/peer/gossip/sa/sa_test.go index a6666b06846..6fe2f37b288 100644 --- a/peer/gossip/sa/sa_test.go +++ b/peer/gossip/sa/sa_test.go @@ -30,8 +30,7 @@ import ( func TestMain(m *testing.M) { // Setup the MSP manager so that we can sign/verify - mspMgrConfigDir := "./../../../msp/sampleconfig/" - err := msptesttools.LoadMSPSetupForTesting(mspMgrConfigDir) + err := msptesttools.LoadMSPSetupForTesting() if err != nil { fmt.Printf("Failed LoadFakeSetupWithLocalMspAndTestChainMsp [%s]", err) os.Exit(-1) diff --git a/peer/main.go b/peer/main.go index da9e0c8de7f..abbdebfbf83 100644 --- a/peer/main.go +++ b/peer/main.go @@ -29,6 +29,7 @@ import ( "github.com/hyperledger/fabric/common/flogging" "github.com/hyperledger/fabric/core" + "github.com/hyperledger/fabric/core/config" "github.com/hyperledger/fabric/peer/chaincode" "github.com/hyperledger/fabric/peer/channel" "github.com/hyperledger/fabric/peer/clilogging" @@ -106,7 +107,7 @@ func main() { flogging.InitBackend(flogging.SetFormat(viper.GetString("logging.format")), logOutput) // Init the MSP - var mspMgrConfigDir = viper.GetString("peer.mspConfigPath") + var mspMgrConfigDir = config.GetPath("peer.mspConfigPath") var mspID = viper.GetString("peer.localMspId") err = common.InitCrypto(mspMgrConfigDir, mspID) if err != nil { // Handle errors reading the config file diff --git a/peer/node/start.go b/peer/node/start.go index f9219f7b031..17f42c74285 100644 --- a/peer/node/start.go +++ b/peer/node/start.go @@ -36,6 +36,7 @@ import ( "github.com/hyperledger/fabric/core" "github.com/hyperledger/fabric/core/chaincode" "github.com/hyperledger/fabric/core/comm" + "github.com/hyperledger/fabric/core/config" "github.com/hyperledger/fabric/core/endorser" "github.com/hyperledger/fabric/core/ledger/ledgermgmt" "github.com/hyperledger/fabric/core/peer" @@ -187,7 +188,7 @@ func serve(args []string) error { func() { defer func() { if err := recover(); err != nil { - logger.Fatalf("Peer configured to start with the default test chain, but supporting configuration files did not match. Please ensure that configtx.yaml contains the unmodified SampleSingleMSPSolo profile and that msp/sampleconfig is present.\n%s", err) + logger.Fatalf("Peer configured to start with the default test chain, but supporting configuration files did not match. Please ensure that configtx.yaml contains the unmodified SampleSingleMSPSolo profile and that sampleconfig/msp is present.\n%s", err) } }() @@ -240,7 +241,7 @@ func serve(args []string) error { serve <- grpcErr }() - if err := writePid(viper.GetString("peer.fileSystemPath")+"/peer.pid", os.Getpid()); err != nil { + if err := writePid(config.GetPath("peer.fileSystemPath")+"/peer.pid", os.Getpid()); err != nil { return err } diff --git a/peer/node/stop.go b/peer/node/stop.go index c1b16665550..d7bc618835a 100644 --- a/peer/node/stop.go +++ b/peer/node/stop.go @@ -25,16 +25,16 @@ import ( "syscall" "github.com/golang/protobuf/ptypes/empty" + "github.com/hyperledger/fabric/core/config" "github.com/hyperledger/fabric/core/peer" pb "github.com/hyperledger/fabric/protos/peer" "github.com/spf13/cobra" - "github.com/spf13/viper" "golang.org/x/net/context" ) func stopCmd() *cobra.Command { nodeStopCmd.Flags().StringVar(&stopPidFile, "stop-peer-pid-file", - viper.GetString("peer.fileSystemPath"), + config.GetPath("peer.fileSystemPath"), "Location of peer pid local file, for forces kill") return nodeStopCmd diff --git a/protos/testutils/txtestutils.go b/protos/testutils/txtestutils.go index 0d8c0500fae..3ee7cc27b7b 100644 --- a/protos/testutils/txtestutils.go +++ b/protos/testutils/txtestutils.go @@ -20,8 +20,6 @@ import ( "fmt" "os" - "path/filepath" - "github.com/hyperledger/fabric/msp" mspmgmt "github.com/hyperledger/fabric/msp/mgmt" "github.com/hyperledger/fabric/msp/mgmt/testtools" @@ -37,13 +35,7 @@ var ( func init() { var err error // setup the MSP manager so that we can sign/verify - mspMgrConfigDir, err := getMSPMgrConfigDir() - if err != nil { - fmt.Printf("Could not get location of msp manager config file") - os.Exit(-1) - return - } - err = msptesttools.LoadMSPSetupForTesting(mspMgrConfigDir) + err = msptesttools.LoadMSPSetupForTesting() if err != nil { fmt.Printf("Could not load msp config, err %s", err) os.Exit(-1) @@ -57,27 +49,6 @@ func init() { } } -func getMSPMgrConfigDir() (string, error) { - var pwd string - var err error - if pwd, err = os.Getwd(); err != nil { - return "", err - } - path := pwd - dir := "" - for { - path, dir = filepath.Split(path) - path = filepath.Clean(path) - fmt.Printf("path=%s, dir=%s\n", path, dir) - if dir == "fabric" { - break - } - } - filePath := filepath.Join(path, "fabric/msp/sampleconfig/") - fmt.Printf("filePath=%s\n", filePath) - return filePath, nil -} - // ConstructSingedTxEnvWithDefaultSigner constructs a transaction envelop for tests with a default signer. // This method helps other modules to construct a transaction with supplied parameters func ConstructSingedTxEnvWithDefaultSigner(chainID, ccName string, response *pb.Response, simulationResults []byte, events []byte, visibility []byte) (*common.Envelope, string, error) { diff --git a/protos/utils/proputils_test.go b/protos/utils/proputils_test.go index e3d0e747300..d12216bcf3d 100644 --- a/protos/utils/proputils_test.go +++ b/protos/utils/proputils_test.go @@ -391,8 +391,7 @@ var signerSerialized []byte func TestMain(m *testing.M) { // setup the MSP manager so that we can sign/verify - mspMgrConfigFile := "../../msp/sampleconfig/" - err := msptesttools.LoadMSPSetupForTesting(mspMgrConfigFile) + err := msptesttools.LoadMSPSetupForTesting() if err != nil { os.Exit(-1) fmt.Printf("Could not initialize msp") diff --git a/common/configtx/tool/configtx.yaml b/sampleconfig/configtx.yaml similarity index 99% rename from common/configtx/tool/configtx.yaml rename to sampleconfig/configtx.yaml index 6e612b1865c..7e51e3ca54c 100644 --- a/common/configtx/tool/configtx.yaml +++ b/sampleconfig/configtx.yaml @@ -60,7 +60,7 @@ Organizations: ID: DEFAULT # MSPDir is the filesystem path which contains the MSP configuration. - MSPDir: msp/sampleconfig + MSPDir: msp # BCCSP: Select which crypto implementation or library to use for the # blockchain crypto service provider. diff --git a/peer/core.yaml b/sampleconfig/core.yaml similarity index 97% rename from peer/core.yaml rename to sampleconfig/core.yaml index d5ede36d3c8..3593340907a 100644 --- a/peer/core.yaml +++ b/sampleconfig/core.yaml @@ -195,15 +195,16 @@ peer: tls: enabled: false cert: - file: testdata/server1.pem + file: peer/tls.crt key: - file: testdata/server1.key + file: peer/tls.key # Root cert file for selfsigned certificates # This represents a self-signed x509 cert that was used to sign the cert.file, # this is sent to client to validate the recived certificate from server when # establishing TLS connection rootcert: - file: testdata/server1.pem + file: peer/tls.crt + # The server name use to verify the hostname returned by TLS handshake serverhostoverride: @@ -223,10 +224,11 @@ peer: # Location of Key Store, can be subdirectory of SbftLocal.DataDir FileKeyStore: # If "", defaults to 'mspConfigPath'/keystore + # TODO: Ensure this is read with fabric/core/config.GetPath() once ready KeyStore: # Path on the file system where peer will find MSP local configurations - mspConfigPath: msp/sampleconfig + mspConfigPath: msp # Identifier of the local MSP # ----!!!!IMPORTANT!!!-!!!IMPORTANT!!!-!!!IMPORTANT!!!!---- @@ -260,12 +262,12 @@ vm: docker: tls: enabled: false - cert: - file: /path/to/server.pem ca: - file: /path/to/ca.pem + file: docker/ca.crt + cert: + file: docker/tls.crt key: - file: /path/to/server-key.pem + file: docker/tls.key # Enables/disables the standard out/err from chaincode containers for debugging purposes attachStdout: false diff --git a/msp/sampleconfig/admincerts/admincert.pem b/sampleconfig/msp/admincerts/admincert.pem similarity index 100% rename from msp/sampleconfig/admincerts/admincert.pem rename to sampleconfig/msp/admincerts/admincert.pem diff --git a/msp/sampleconfig/cacerts/cacert.pem b/sampleconfig/msp/cacerts/cacert.pem similarity index 100% rename from msp/sampleconfig/cacerts/cacert.pem rename to sampleconfig/msp/cacerts/cacert.pem diff --git a/msp/sampleconfig/keystore/key.pem b/sampleconfig/msp/keystore/key.pem similarity index 100% rename from msp/sampleconfig/keystore/key.pem rename to sampleconfig/msp/keystore/key.pem diff --git a/msp/sampleconfig/signcerts/peer.pem b/sampleconfig/msp/signcerts/peer.pem similarity index 100% rename from msp/sampleconfig/signcerts/peer.pem rename to sampleconfig/msp/signcerts/peer.pem diff --git a/orderer/orderer.yaml b/sampleconfig/orderer.yaml similarity index 99% rename from orderer/orderer.yaml rename to sampleconfig/orderer.yaml index f31cc3bf760..56e8aa0771e 100644 --- a/orderer/orderer.yaml +++ b/sampleconfig/orderer.yaml @@ -49,12 +49,12 @@ General: # Genesis file: The file containing the genesis block. Used by the orderer # when GenesisMethod is set to "file". - GenesisFile: ./genesisblock + GenesisFile: genesisblock # LocalMSPDir is where to find the crypto material needed for signing in the # orderer. It is set relative here as a default for dev environments but # should be changed to the real location in production. - LocalMSPDir: msp/sampleconfig + LocalMSPDir: msp # LocalMSPID is the identity to register the local MSP material with the MSP # manager. IMPORTANT: Deployers need to change the value of the localMspId diff --git a/unit-test/docker-compose.yml b/unit-test/docker-compose.yml index 0b24d91b69b..bc31d741faa 100644 --- a/unit-test/docker-compose.yml +++ b/unit-test/docker-compose.yml @@ -5,7 +5,6 @@ vp: - 7051 environment: - CORE_PEER_ADDRESSAUTODETECT=true - - CORE_PEER_MSPCONFIGPATH=/etc/hyperledger/fabric/msp/sampleconfig volumes: - /var/run/docker.sock:/var/run/docker.sock