github.com/adnan-c/fabric_e2e_couchdb@v0.6.1-preview.0.20170228180935-21ce6b23cf91/common/configvalues/msp/config_util.go (about)

     1  /*
     2  Copyright IBM Corp. 2017 All Rights Reserved.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8                   http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package msp
    18  
    19  import (
    20  	"github.com/hyperledger/fabric/common/cauthdsl"
    21  	"github.com/hyperledger/fabric/msp"
    22  	cb "github.com/hyperledger/fabric/protos/common"
    23  	mspprotos "github.com/hyperledger/fabric/protos/msp"
    24  	"github.com/hyperledger/fabric/protos/utils"
    25  
    26  	logging "github.com/op/go-logging"
    27  )
    28  
    29  var logger = logging.MustGetLogger("configvalues/msp")
    30  
    31  const (
    32  	// ReadersPolicyKey is the key used for the read policy
    33  	ReadersPolicyKey = "Readers"
    34  
    35  	// WritersPolicyKey is the key used for the read policy
    36  	WritersPolicyKey = "Writers"
    37  
    38  	// AdminsPolicyKey is the key used for the read policy
    39  	AdminsPolicyKey = "Admins"
    40  
    41  	// MSPKey is the org key used for MSP configuration
    42  	MSPKey = "MSP"
    43  )
    44  
    45  // TemplateGroupMSP creates an MSP ConfigValue at the given configPath
    46  func TemplateGroupMSP(configPath []string, mspConfig *mspprotos.MSPConfig) *cb.ConfigGroup {
    47  	// check that the type for that MSP is supported
    48  	if mspConfig.Type != int32(msp.FABRIC) {
    49  		logger.Panicf("Setup error: unsupported msp type %d", mspConfig.Type)
    50  	}
    51  
    52  	// create the msp instance
    53  	mspInst, err := msp.NewBccspMsp()
    54  	if err != nil {
    55  		logger.Panicf("Creating the MSP manager failed, err %s", err)
    56  	}
    57  
    58  	// set it up
    59  	err = mspInst.Setup(mspConfig)
    60  	if err != nil {
    61  		logger.Panicf("Setting up the MSP manager failed, err %s", err)
    62  	}
    63  
    64  	// add the MSP to the map of pending MSPs
    65  	mspID, err := mspInst.GetIdentifier()
    66  	if err != nil {
    67  		logger.Panicf("Could not extract msp identifier, err %s", err)
    68  	}
    69  
    70  	memberPolicy := &cb.ConfigPolicy{
    71  		Policy: &cb.Policy{
    72  			Type:   int32(cb.Policy_SIGNATURE),
    73  			Policy: utils.MarshalOrPanic(cauthdsl.SignedByMspMember(mspID)),
    74  		},
    75  	}
    76  
    77  	adminPolicy := &cb.ConfigPolicy{
    78  		Policy: &cb.Policy{
    79  			Type:   int32(cb.Policy_SIGNATURE),
    80  			Policy: utils.MarshalOrPanic(cauthdsl.SignedByMspAdmin(mspID)),
    81  		},
    82  	}
    83  
    84  	result := cb.NewConfigGroup()
    85  
    86  	intermediate := result
    87  	for _, group := range configPath {
    88  		intermediate.Groups[group] = cb.NewConfigGroup()
    89  		intermediate = intermediate.Groups[group]
    90  	}
    91  	intermediate.Values[MSPKey] = &cb.ConfigValue{
    92  		Value: utils.MarshalOrPanic(mspConfig),
    93  	}
    94  	intermediate.Policies[AdminsPolicyKey] = adminPolicy
    95  	intermediate.Policies[ReadersPolicyKey] = memberPolicy
    96  	intermediate.Policies[WritersPolicyKey] = memberPolicy
    97  	return result
    98  }