github.com/sykesm/fabric@v1.1.0-preview.0.20200129034918-2aa12b1a0181/core/common/privdata/util.go (about)

     1  /*
     2  Copyright IBM Corp. All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package privdata
     8  
     9  import (
    10  	"github.com/hyperledger/fabric-protos-go/peer"
    11  	"github.com/hyperledger/fabric/common/cauthdsl"
    12  	"github.com/hyperledger/fabric/common/policies"
    13  	"github.com/hyperledger/fabric/msp"
    14  	"github.com/pkg/errors"
    15  )
    16  
    17  // getPolicy creates a new policy from the policy envelope. It will return an error if the envelope has invalid policy config.
    18  // Some caller (e.g., MembershipProvider.AsMemberOf) may drop the error and treat it as a RejectAll policy.
    19  // In the future, we must revisit the callers if this method will return different types of errors.
    20  func getPolicy(collectionPolicyConfig *peer.CollectionPolicyConfig, deserializer msp.IdentityDeserializer) (policies.Policy, error) {
    21  	if collectionPolicyConfig == nil {
    22  		return nil, errors.New("collection policy config is nil")
    23  	}
    24  	accessPolicyEnvelope := collectionPolicyConfig.GetSignaturePolicy()
    25  	if accessPolicyEnvelope == nil {
    26  		return nil, errors.New("collection config access policy is nil")
    27  	}
    28  	// create access policy from the envelope
    29  
    30  	pp := cauthdsl.EnvelopeBasedPolicyProvider{Deserializer: deserializer}
    31  	accessPolicy, err := pp.NewPolicy(accessPolicyEnvelope)
    32  	if err != nil {
    33  		return nil, errors.WithMessage(err, "failed constructing policy object out of collection policy config")
    34  	}
    35  
    36  	return accessPolicy, nil
    37  }