From 903a248817df1846affb30de76881455b880a311 Mon Sep 17 00:00:00 2001
From: Benjamin Bollen <ben@erisindustries.com>
Date: Sat, 2 Jul 2016 20:26:06 +0200
Subject: [PATCH] Better shield subtree extraction from configuration to avoid
 Viper panic

---
 consensus/tendermint/config.go     | 8 +++++++-
 consensus/tendermint/tendermint.go | 3 +++
 core/config.go                     | 9 ++++++++-
 3 files changed, 18 insertions(+), 2 deletions(-)

diff --git a/consensus/tendermint/config.go b/consensus/tendermint/config.go
index 54ee9034..aa1429d2 100644
--- a/consensus/tendermint/config.go
+++ b/consensus/tendermint/config.go
@@ -144,7 +144,13 @@ func (tmintConfig *TendermintConfig) GetMapString(key string) map[string]string
 }
 
 func (tmintConfig *TendermintConfig) GetConfig(key string) tendermintConfig.Config {
-  return &TendermintConfig {
+	// TODO: [ben] log out a warning as this indicates a potentially breaking code
+	// change from Tendermints side
+	if !tmintConfig.subTree.IsSet(key) {
+		return &TendermintConfig {
+			subTree: viper.New(),
+		}}
+	return &TendermintConfig {
     subTree: tmintConfig.subTree.Sub(key),
   }
 }
diff --git a/consensus/tendermint/tendermint.go b/consensus/tendermint/tendermint.go
index f48f7df7..e89bf3c1 100644
--- a/consensus/tendermint/tendermint.go
+++ b/consensus/tendermint/tendermint.go
@@ -62,6 +62,9 @@ func NewTendermintNode(moduleConfig *config.ModuleConfig,
 	// to be written in tendermint's root directory.
 	// NOTE: [ben] as elsewhere Sub panics if config file does not have this
 	// subtree. To shield in go-routine, or PR to viper.
+	if !moduleConfig.Config.IsSet("confighuration") {
+		return nil, fmt.Errorf("Failed to extract Tendermint configuration subtree.")
+	}
 	tendermintConfigViper := moduleConfig.Config.Sub("configuration")
 	if tendermintConfigViper == nil {
 		return nil,
diff --git a/core/config.go b/core/config.go
index 23f3f28d..f280dca9 100644
--- a/core/config.go
+++ b/core/config.go
@@ -71,6 +71,10 @@ func loadModuleConfig(do *definitions.Do, module string) (*config.ModuleConfig,
   // TODO: [ben] Viper internally panics if `moduleName` contains an unallowed
   // character (eg, a dash).  Either this needs to be wrapped in a go-routine
   // and recovered from or a PR to viper is needed to address this bug.
+	if !do.Config.IsSet(moduleName) {
+		return nil, fmt.Errorf("Failed to read configuration section for %s",
+			moduleName)
+	}
   subConfig := do.Config.Sub(moduleName)
   if subConfig == nil {
     return nil,
@@ -93,7 +97,10 @@ func loadModuleConfig(do *definitions.Do, module string) (*config.ModuleConfig,
 // LoadServerModuleConfig wraps specifically for the servers run by core
 func LoadServerConfig(do *definitions.Do) (*server.ServerConfig, error) {
   // load configuration subtree for servers
-  subConfig := do.Config.Sub("servers")
+	if !do.Config.IsSet("servers") {
+		return nil, fmt.Errorf("Failed to read configuration section for servers")
+	}
+	subConfig := do.Config.Sub("servers")
   if subConfig == nil {
     return nil,
       fmt.Errorf("Failed to read configuration section for servers")
-- 
GitLab