@@ -8,42 +8,27 @@ package endorsement
8
8
9
9
import (
10
10
"github.com/hyperledger/fabric/common/policies"
11
- "github.com/hyperledger/fabric/common/policies/inquire"
12
11
"github.com/hyperledger/fabric/core/common/privdata"
12
+ "github.com/hyperledger/fabric/gossip/api"
13
+ . "github.com/hyperledger/fabric/protos/discovery"
13
14
"github.com/pkg/errors"
14
15
)
15
16
16
- type filterPrincipalSets func (collectionName string , principalSets policies.PrincipalSets ) (policies.PrincipalSets , error )
17
-
18
- func (f filterPrincipalSets ) forCollections (ccName string , collections ... string ) filterFunc {
19
- return func (principalSets policies.PrincipalSets ) (policies.PrincipalSets , error ) {
20
- var err error
21
- for _ , col := range collections {
22
- principalSets , err = f (col , principalSets )
23
- if err != nil {
24
- logger .Warningf ("Failed filtering collection for chaincode %s, collection %s: %v" , ccName , col , err )
25
- return nil , err
26
- }
27
- }
28
- return principalSets , nil
29
- }
30
- }
31
-
32
- func newCollectionFilter (configBytes []byte ) (filterPrincipalSets , error ) {
33
- mapFilter := make (principalSetsByCollectionName )
17
+ func principalsFromCollectionConfig (configBytes []byte ) (principalSetsByCollectionName , error ) {
18
+ principalSetsByCollections := make (principalSetsByCollectionName )
34
19
if len (configBytes ) == 0 {
35
- return mapFilter . filter , nil
20
+ return principalSetsByCollections , nil
36
21
}
37
22
ccp , err := privdata .ParseCollectionConfig (configBytes )
38
23
if err != nil {
39
24
return nil , errors .Wrapf (err , "invalid collection bytes" )
40
25
}
41
- for _ , cfg := range ccp .Config {
42
- staticCol := cfg .GetStaticCollectionConfig ()
26
+ for _ , colConfig := range ccp .Config {
27
+ staticCol := colConfig .GetStaticCollectionConfig ()
43
28
if staticCol == nil {
44
29
// Right now we only support static collections, so if we got something else
45
30
// we should refuse to process further
46
- return nil , errors .Errorf ("expected a static collection but got %v instead" , cfg )
31
+ return nil , errors .Errorf ("expected a static collection but got %v instead" , colConfig )
47
32
}
48
33
if staticCol .MemberOrgsPolicy == nil {
49
34
return nil , errors .Errorf ("MemberOrgsPolicy of %s is nil" , staticCol .Name )
@@ -57,31 +42,56 @@ func newCollectionFilter(configBytes []byte) (filterPrincipalSets, error) {
57
42
for _ , principal := range pol .Identities {
58
43
principals = append (principals , principal )
59
44
}
60
- principalSet := inquire .NewComparablePrincipalSet (principals )
61
- if principalSet == nil {
62
- return nil , errors .Errorf ("failed constructing principal set for %s: principals given are %v" , staticCol .Name , pol .Identities )
63
- }
64
- mapFilter [staticCol .Name ] = principalSet
45
+ principalSetsByCollections [staticCol .Name ] = principals
65
46
}
66
- return mapFilter . filter , nil
47
+ return principalSetsByCollections , nil
67
48
}
68
49
69
- type principalSetsByCollectionName map [string ]inquire. ComparablePrincipalSet
50
+ type principalSetsByCollectionName map [string ]policies. PrincipalSet
70
51
71
- func (psbc principalSetsByCollectionName ) filter (collectionName string , principalSets policies.PrincipalSets ) (policies.PrincipalSets , error ) {
72
- collectionPrincipals , exists := psbc [collectionName ]
73
- if ! exists {
74
- return nil , errors .Errorf ("collection %s wasn't found in configuration" , collectionName )
52
+ // toIdentityFilter converts this principalSetsByCollectionName mapping to a filter
53
+ // which accepts or rejects identities of peers.
54
+ func (psbc principalSetsByCollectionName ) toIdentityFilter (channel string , evaluator principalEvaluator , cc * ChaincodeCall ) (identityFilter , error ) {
55
+ var principalSets policies.PrincipalSets
56
+ for _ , col := range cc .CollectionNames {
57
+ // Each collection we're interested in should exist in the principalSetsByCollectionName mapping.
58
+ // Otherwise, we have no way of computing a filter because we can't locate the principals the peer identities
59
+ // need to satisfy.
60
+ principalSet , exists := psbc [col ]
61
+ if ! exists {
62
+ return nil , errors .Errorf ("collection %s doesn't exist in collection config for chaincode %s" , col , cc .Name )
63
+ }
64
+ principalSets = append (principalSets , principalSet )
75
65
}
76
- var res policies.PrincipalSets
77
- for _ , ps := range principalSets {
78
- comparablePS := inquire .NewComparablePrincipalSet (ps )
79
- if comparablePS == nil {
80
- return nil , errors .Errorf ("principal set %v is invalid" , ps )
66
+ return filterForPrincipalSets (channel , evaluator , principalSets ), nil
67
+ }
68
+
69
+ // filterForPrincipalSets creates a filter of peer identities out of the given PrincipalSets
70
+ func filterForPrincipalSets (channel string , evaluator principalEvaluator , sets policies.PrincipalSets ) identityFilter {
71
+ return func (identity api.PeerIdentityType ) bool {
72
+ // Iterate over all principal sets and ensure each principal set
73
+ // authorizes the identity.
74
+ for _ , principalSet := range sets {
75
+ if ! isIdentityAuthorizedByPrincipalSet (channel , evaluator , principalSet , identity ) {
76
+ return false
77
+ }
81
78
}
82
- if comparablePS .IsCoveredBy (collectionPrincipals ) {
83
- res = append (res , ps )
79
+ return true
80
+ }
81
+ }
82
+
83
+ // isIdentityAuthorizedByPrincipalSet returns whether the given identity satisfies some principal out of the given PrincipalSet
84
+ func isIdentityAuthorizedByPrincipalSet (channel string , evaluator principalEvaluator , principalSet policies.PrincipalSet , identity api.PeerIdentityType ) bool {
85
+ // We look for a principal which authorizes the identity
86
+ // among all principals in the principalSet
87
+ for _ , principal := range principalSet {
88
+ err := evaluator .SatisfiesPrincipal (channel , identity , principal )
89
+ if err != nil {
90
+ continue
84
91
}
92
+ // Else, err is nil, so we found a principal which authorized
93
+ // the given identity.
94
+ return true
85
95
}
86
- return res , nil
96
+ return false
87
97
}
0 commit comments