From f367cf0a371a3d0ebd30916110b9cfd24b71ca03 Mon Sep 17 00:00:00 2001 From: Jason Yellick Date: Fri, 2 Jun 2017 12:22:27 -0400 Subject: [PATCH] [FAB-4163] Nil dereference in configtxgen When attempting to generate a genesis block while failing to specify an orderer section, the result was a nil point dereference. This CR fixes the dereference by protecting it behind a nil check, and updates the tool logic to check for the existence of the section, and print an error and exit if it is not present. Change-Id: Id768f23d7e138fd7c8ca90889e805975fdf44bec Signed-off-by: Jason Yellick --- common/configtx/tool/configtxgen/main.go | 3 +++ common/configtx/tool/configtxgen/main_test.go | 10 ++++++++++ common/configtx/tool/provisional/provisional.go | 4 +++- 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/common/configtx/tool/configtxgen/main.go b/common/configtx/tool/configtxgen/main.go index af7a6e17161..3e1b575b9dd 100644 --- a/common/configtx/tool/configtxgen/main.go +++ b/common/configtx/tool/configtxgen/main.go @@ -44,6 +44,9 @@ var logger = flogging.MustGetLogger("common/configtx/tool") func doOutputBlock(config *genesisconfig.Profile, channelID string, outputBlock string) error { pgen := provisional.New(config) logger.Info("Generating genesis block") + if config.Orderer == nil { + return fmt.Errorf("config does not contain an Orderers section, necessary for all config blocks, aborting") + } if config.Consortiums == nil { logger.Warning("Genesis block does not contain a consortiums group definition. This block cannot be used for orderer bootstrap.") } diff --git a/common/configtx/tool/configtxgen/main_test.go b/common/configtx/tool/configtxgen/main_test.go index 3c10cec2da4..959be6c4ab8 100644 --- a/common/configtx/tool/configtxgen/main_test.go +++ b/common/configtx/tool/configtxgen/main_test.go @@ -51,6 +51,16 @@ func TestInspectBlock(t *testing.T) { assert.NoError(t, doInspectBlock(blockDest), "Good block inspection request") } +func TestMissingOrdererSection(t *testing.T) { + blockDest := tmpDir + string(os.PathSeparator) + "block" + + factory.InitFactories(nil) + config := genesisconfig.Load(genesisconfig.SampleInsecureProfile) + config.Orderer = nil + + assert.Error(t, doOutputBlock(config, "foo", blockDest), "Missing orderer section") +} + func TestInspectConfigTx(t *testing.T) { configTxDest := tmpDir + string(os.PathSeparator) + "configtx" diff --git a/common/configtx/tool/provisional/provisional.go b/common/configtx/tool/provisional/provisional.go index 074a92fb78b..3a0d283e57c 100644 --- a/common/configtx/tool/provisional/provisional.go +++ b/common/configtx/tool/provisional/provisional.go @@ -88,7 +88,6 @@ func New(conf *genesisconfig.Profile) Generator { // Chain Config Types config.DefaultHashingAlgorithm(), config.DefaultBlockDataHashingStructure(), - config.TemplateOrdererAddresses(conf.Orderer.Addresses), // TODO, move to conf.Channel when it exists // Default policies policies.TemplateImplicitMetaAnyPolicy([]string{}, configvaluesmsp.ReadersPolicyKey), @@ -99,6 +98,9 @@ func New(conf *genesisconfig.Profile) Generator { if conf.Orderer != nil { bs.ordererGroups = []*cb.ConfigGroup{ + // Orderer addresses + config.TemplateOrdererAddresses(conf.Orderer.Addresses), + // Orderer Config Types config.TemplateConsensusType(conf.Orderer.OrdererType), config.TemplateBatchSize(&ab.BatchSize{