github.com/ewagmig/fabric@v2.1.1+incompatible/common/channelconfig/application.go (about)

     1  /*
     2  Copyright IBM Corp. 2017 All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package channelconfig
     8  
     9  import (
    10  	cb "github.com/hyperledger/fabric-protos-go/common"
    11  	pb "github.com/hyperledger/fabric-protos-go/peer"
    12  	"github.com/hyperledger/fabric/common/capabilities"
    13  	"github.com/pkg/errors"
    14  )
    15  
    16  const (
    17  	// ApplicationGroupKey is the group name for the Application config
    18  	ApplicationGroupKey = "Application"
    19  
    20  	// ACLsKey is the name of the ACLs config
    21  	ACLsKey = "ACLs"
    22  )
    23  
    24  // ApplicationProtos is used as the source of the ApplicationConfig
    25  type ApplicationProtos struct {
    26  	ACLs         *pb.ACLs
    27  	Capabilities *cb.Capabilities
    28  }
    29  
    30  // ApplicationConfig implements the Application interface
    31  type ApplicationConfig struct {
    32  	applicationOrgs map[string]ApplicationOrg
    33  	protos          *ApplicationProtos
    34  }
    35  
    36  // NewApplicationConfig creates config from an Application config group
    37  func NewApplicationConfig(appGroup *cb.ConfigGroup, mspConfig *MSPConfigHandler) (*ApplicationConfig, error) {
    38  	ac := &ApplicationConfig{
    39  		applicationOrgs: make(map[string]ApplicationOrg),
    40  		protos:          &ApplicationProtos{},
    41  	}
    42  
    43  	if err := DeserializeProtoValuesFromGroup(appGroup, ac.protos); err != nil {
    44  		return nil, errors.Wrap(err, "failed to deserialize values")
    45  	}
    46  
    47  	if !ac.Capabilities().ACLs() {
    48  		if _, ok := appGroup.Values[ACLsKey]; ok {
    49  			return nil, errors.New("ACLs may not be specified without the required capability")
    50  		}
    51  	}
    52  
    53  	var err error
    54  	for orgName, orgGroup := range appGroup.Groups {
    55  		ac.applicationOrgs[orgName], err = NewApplicationOrgConfig(orgName, orgGroup, mspConfig)
    56  		if err != nil {
    57  			return nil, err
    58  		}
    59  	}
    60  
    61  	return ac, nil
    62  }
    63  
    64  // Organizations returns a map of org ID to ApplicationOrg
    65  func (ac *ApplicationConfig) Organizations() map[string]ApplicationOrg {
    66  	return ac.applicationOrgs
    67  }
    68  
    69  // Capabilities returns a map of capability name to Capability
    70  func (ac *ApplicationConfig) Capabilities() ApplicationCapabilities {
    71  	return capabilities.NewApplicationProvider(ac.protos.Capabilities.Capabilities)
    72  }
    73  
    74  // APIPolicyMapper returns a PolicyMapper that maps API names to policies
    75  func (ac *ApplicationConfig) APIPolicyMapper() PolicyMapper {
    76  	pm := newAPIsProvider(ac.protos.ACLs.Acls)
    77  
    78  	return pm
    79  }