github.com/darrenli6/fabric-sdk-example@v0.0.0-20220109053535-94b13b56df8c/common/config/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  	"github.com/hyperledger/fabric/common/flogging"
    27  )
    28  
    29  var logger = flogging.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  // TemplateGroupMSPWithAdminRolePrincipal creates an MSP ConfigValue at the given configPath with Admin policy
    46  // of role type ADMIN if admin==true or MEMBER otherwise
    47  func TemplateGroupMSPWithAdminRolePrincipal(configPath []string, mspConfig *mspprotos.MSPConfig, admin bool) *cb.ConfigGroup {
    48  	// check that the type for that MSP is supported
    49  	if mspConfig.Type != int32(msp.FABRIC) {
    50  		logger.Panicf("Setup error: unsupported msp type %d", mspConfig.Type)
    51  	}
    52  
    53  	// create the msp instance
    54  	mspInst, err := msp.NewBccspMsp()
    55  	if err != nil {
    56  		logger.Panicf("Creating the MSP manager failed, err %s", err)
    57  	}
    58  
    59  	// set it up
    60  	err = mspInst.Setup(mspConfig)
    61  	if err != nil {
    62  		logger.Panicf("Setting up the MSP manager failed, err %s", err)
    63  	}
    64  
    65  	// add the MSP to the map of pending MSPs
    66  	mspID, _ := mspInst.GetIdentifier()
    67  
    68  	memberPolicy := &cb.ConfigPolicy{
    69  		Policy: &cb.Policy{
    70  			Type:  int32(cb.Policy_SIGNATURE),
    71  			Value: utils.MarshalOrPanic(cauthdsl.SignedByMspMember(mspID)),
    72  		},
    73  	}
    74  
    75  	var adminSigPolicy []byte
    76  	if admin {
    77  		adminSigPolicy = utils.MarshalOrPanic(cauthdsl.SignedByMspAdmin(mspID))
    78  	} else {
    79  		adminSigPolicy = utils.MarshalOrPanic(cauthdsl.SignedByMspMember(mspID))
    80  	}
    81  
    82  	adminPolicy := &cb.ConfigPolicy{
    83  		Policy: &cb.Policy{
    84  			Type:  int32(cb.Policy_SIGNATURE),
    85  			Value: adminSigPolicy,
    86  		},
    87  	}
    88  
    89  	result := cb.NewConfigGroup()
    90  
    91  	intermediate := result
    92  	for _, group := range configPath {
    93  		intermediate.Groups[group] = cb.NewConfigGroup()
    94  		intermediate = intermediate.Groups[group]
    95  	}
    96  	intermediate.Values[MSPKey] = &cb.ConfigValue{
    97  		Value: utils.MarshalOrPanic(mspConfig),
    98  	}
    99  	intermediate.Policies[AdminsPolicyKey] = adminPolicy
   100  	intermediate.Policies[ReadersPolicyKey] = memberPolicy
   101  	intermediate.Policies[WritersPolicyKey] = memberPolicy
   102  	return result
   103  }
   104  
   105  // TemplateGroupMSP creates an MSP ConfigValue at the given configPath
   106  func TemplateGroupMSP(configPath []string, mspConfig *mspprotos.MSPConfig) *cb.ConfigGroup {
   107  	return TemplateGroupMSPWithAdminRolePrincipal(configPath, mspConfig, true)
   108  }