Skip to content

Commit

Permalink
[FAB-2197] Factor out configmap construction
Browse files Browse the repository at this point in the history
https://jira.hyperledger.org/browse/FAB-2197

The configtx processing currently performs validation while applying
configuration.  In order to split this process, the config map
construction will need to be used in two different code paths, so this
CR factors it out.

Change-Id: Ib230c3f363d490fb89b83fcd64bf2c9ec4fc67a4
Signed-off-by: Jason Yellick <jyellick@us.ibm.com>
  • Loading branch information
Jason Yellick committed Feb 14, 2017
1 parent c16f5b3 commit 75327ff
Show file tree
Hide file tree
Showing 5 changed files with 201 additions and 58 deletions.
1 change: 1 addition & 0 deletions common/configtx/compare.go
Expand Up @@ -26,6 +26,7 @@ type comparable struct {
*cb.ConfigGroup
*cb.ConfigValue
*cb.ConfigPolicy
key string
path []string
}

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

package configtx

import (
"fmt"
"strings"

cb "github.com/hyperledger/fabric/protos/common"
)

const (
RootGroupKey = "Channel"

GroupPrefix = "[Groups] "
ValuePrefix = "[Values] "
PolicyPrefix = "[Policy] " // The plurarility doesn't match, but, it makes the logs much easier being the same lenght as "Groups" and "Values"

PathSeparator = "/"
)

// mapConfig is the only method in this file intended to be called outside this file
// it takes a ConfigGroup and generates a map of fqPath to comparables (or error on invalid keys)
func mapConfig(channelGroup *cb.ConfigGroup) (map[string]comparable, error) {
result := make(map[string]comparable)
err := recurseConfig(result, []string{RootGroupKey}, channelGroup)
if err != nil {
return nil, err
}
return result, nil
}

func addToMap(cg comparable, result map[string]comparable) error {
var fqPath string

switch {
case cg.ConfigGroup != nil:
fqPath = GroupPrefix
case cg.ConfigValue != nil:
fqPath = ValuePrefix
case cg.ConfigPolicy != nil:
fqPath = PolicyPrefix
}

// TODO rename validateChainID to validateConfigID
if err := validateChainID(cg.key); err != nil {
return fmt.Errorf("Illegal characters in key: %s", fqPath)
}

if len(cg.path) == 0 {
fqPath += PathSeparator + cg.key
} else {
fqPath += PathSeparator + strings.Join(cg.path, PathSeparator) + PathSeparator + cg.key
}

logger.Debugf("Adding to config map: %s", fqPath)

result[fqPath] = cg

return nil
}

func recurseConfig(result map[string]comparable, path []string, group *cb.ConfigGroup) error {
if err := addToMap(comparable{key: path[len(path)-1], path: path[:len(path)-1], ConfigGroup: group}, result); err != nil {
return err
}

for key, group := range group.Groups {
nextPath := append(path, key)
if err := recurseConfig(result, nextPath, group); err != nil {
return err
}
}

for key, value := range group.Values {
if err := addToMap(comparable{key: key, path: path, ConfigValue: value}, result); err != nil {
return err
}
}

for key, policy := range group.Policies {
if err := addToMap(comparable{key: key, path: path, ConfigPolicy: policy}, result); err != nil {
return err
}
}

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

package configtx

import (
"github.com/stretchr/testify/assert"
"testing"

cb "github.com/hyperledger/fabric/protos/common"
)

func TestBadKey(t *testing.T) {
assert.Error(t, addToMap(comparable{key: "[Label]", path: []string{}}, make(map[string]comparable)),
"Should have errored on key with illegal characters")
}

func TestConfigMap(t *testing.T) {
config := cb.NewConfigGroup()
config.Groups["0DeepGroup"] = cb.NewConfigGroup()
config.Values["0DeepValue1"] = &cb.ConfigValue{}
config.Values["0DeepValue2"] = &cb.ConfigValue{}
config.Groups["0DeepGroup"].Policies["1DeepPolicy"] = &cb.ConfigPolicy{}
config.Groups["0DeepGroup"].Groups["1DeepGroup"] = cb.NewConfigGroup()
config.Groups["0DeepGroup"].Groups["1DeepGroup"].Values["2DeepValue"] = &cb.ConfigValue{}

confMap, err := mapConfig(config)
assert.NoError(t, err, "Should not have errored building map")

assert.Len(t, confMap, 7, "There should be 7 entries in the config map")

assert.Equal(t, comparable{key: "Channel", path: []string{}, ConfigGroup: config},
confMap["[Groups] /Channel"])
assert.Equal(t, comparable{key: "0DeepGroup", path: []string{"Channel"}, ConfigGroup: config.Groups["0DeepGroup"]},
confMap["[Groups] /Channel/0DeepGroup"])
assert.Equal(t, comparable{key: "0DeepValue1", path: []string{"Channel"}, ConfigValue: config.Values["0DeepValue1"]},
confMap["[Values] /Channel/0DeepValue1"])
assert.Equal(t, comparable{key: "0DeepValue2", path: []string{"Channel"}, ConfigValue: config.Values["0DeepValue2"]},
confMap["[Values] /Channel/0DeepValue2"])
assert.Equal(t, comparable{key: "1DeepPolicy", path: []string{"Channel", "0DeepGroup"}, ConfigPolicy: config.Groups["0DeepGroup"].Policies["1DeepPolicy"]},
confMap["[Policy] /Channel/0DeepGroup/1DeepPolicy"])
assert.Equal(t, comparable{key: "1DeepGroup", path: []string{"Channel", "0DeepGroup"}, ConfigGroup: config.Groups["0DeepGroup"].Groups["1DeepGroup"]},
confMap["[Groups] /Channel/0DeepGroup/1DeepGroup"])
assert.Equal(t, comparable{key: "2DeepValue", path: []string{"Channel", "0DeepGroup", "1DeepGroup"}, ConfigValue: config.Groups["0DeepGroup"].Groups["1DeepGroup"].Values["2DeepValue"]},
confMap["[Values] /Channel/0DeepGroup/1DeepGroup/2DeepValue"])
}
14 changes: 13 additions & 1 deletion common/configtx/initializer.go
Expand Up @@ -17,6 +17,8 @@ limitations under the License.
package configtx

import (
"fmt"

"github.com/hyperledger/fabric/common/cauthdsl"
"github.com/hyperledger/fabric/common/configtx/api"
configtxapplication "github.com/hyperledger/fabric/common/configtx/handlers/application"
Expand Down Expand Up @@ -134,5 +136,15 @@ func (i *initializer) PolicyProposer() api.Handler {
}

func (i *initializer) Handler(path []string) (api.Handler, error) {
return i.channelConfig.Handler(path)
if len(path) == 0 {
return nil, fmt.Errorf("Empty path")
}

switch path[0] {
case RootGroupKey:
return i.channelConfig.Handler(path[1:])
default:
return nil, fmt.Errorf("Unknown root group: %s", path[0])
}

}
83 changes: 26 additions & 57 deletions common/configtx/manager.go
Expand Up @@ -20,7 +20,6 @@ import (
"errors"
"fmt"
"regexp"
"strings"

"github.com/hyperledger/fabric/common/configtx/api"
"github.com/hyperledger/fabric/common/policies"
Expand Down Expand Up @@ -186,66 +185,32 @@ func (cm *configManager) commitHandlers() {
}
}

func (cm *configManager) recurseConfig(result map[string]comparable, path []string, group *cb.ConfigGroup) error {

for key, group := range group.Groups {
// TODO rename validateChainID to validateConfigID
if err := validateChainID(key); err != nil {
return fmt.Errorf("Illegal characters in group key: %s", key)
}

if err := cm.recurseConfig(result, append(path, key), group); err != nil {
return err
}

// TODO, uncomment to validate version validation on groups
// result["[Groups]"+strings.Join(path, ".")+key] = comparable{path: path, ConfigGroup: group}
}

valueHandler, err := cm.initializer.Handler(path)
if err != nil {
return err
}

for key, value := range group.Values {
if err := validateChainID(key); err != nil {
return fmt.Errorf("Illegal characters in values key: %s", key)
}

err := valueHandler.ProposeConfig(key, &cb.ConfigValue{
Value: value.Value,
})
if err != nil {
return err
}

result["[Values]"+strings.Join(path, ".")+key] = comparable{path: path, ConfigValue: value}
}

logger.Debugf("Found %d policies", len(group.Policies))
for key, policy := range group.Policies {
if err := validateChainID(key); err != nil {
return fmt.Errorf("Illegal characters in policies key: %s", key)
}

logger.Debugf("Proposing policy: %s", key)
err := cm.initializer.PolicyProposer().ProposeConfig(key, &cb.ConfigValue{
// TODO, fix policy interface to take the policy directly
Value: utils.MarshalOrPanic(policy.Policy),
})

if err != nil {
return err
func (cm *configManager) proposeConfig(config map[string]comparable) error {
for fqPath, c := range config {
logger.Debugf("Proposing: %s", fqPath)
switch {
case c.ConfigValue != nil:
valueHandler, err := cm.initializer.Handler(c.path)
if err != nil {
return err
}
if err := valueHandler.ProposeConfig(c.key, c.ConfigValue); err != nil {
return err
}
case c.ConfigPolicy != nil:
if err := cm.initializer.PolicyProposer().ProposeConfig(c.key, &cb.ConfigValue{
// TODO, fix policy interface to take the policy directly
Value: utils.MarshalOrPanic(c.ConfigPolicy.Policy),
}); err != nil {
return err
}
}

// TODO, uncomment to validate version validation on policies
//result["[Policies]"+strings.Join(path, ".")+key] = comparable{path: path, ConfigPolicy: policy}
}

return nil
}

func (cm *configManager) processConfig(configtx *cb.ConfigEnvelope) (configMap map[string]comparable, err error) {
func (cm *configManager) processConfig(configtx *cb.ConfigEnvelope) (map[string]comparable, error) {
// XXX as a temporary hack to get the new protos working, we assume entire config is always in the ConfigUpdate.WriteSet

if configtx.LastUpdate == nil {
Expand Down Expand Up @@ -284,9 +249,13 @@ func (cm *configManager) processConfig(configtx *cb.ConfigEnvelope) (configMap m
defaultModificationPolicy = &acceptAllPolicy{}
}

configMap = make(map[string]comparable)
configMap, err := mapConfig(config.WriteSet)
if err != nil {
return nil, err
}
delete(configMap, "[Groups] /Channel") // XXX temporary hack to prevent evaluating groups for modification

if err := cm.recurseConfig(configMap, []string{}, config.WriteSet); err != nil {
if err := cm.proposeConfig(configMap); err != nil {
return nil, err
}

Expand Down

0 comments on commit 75327ff

Please sign in to comment.