-
Notifications
You must be signed in to change notification settings - Fork 8.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FAB-2323] Implement ImplicitMetaPolicy
https://jira.hyperledger.org/browse/FAB-2323 This CR adds support to the policies manager for evaluating the new ImplicitMetaPolicy policy type. Unlike the cauthdsl policy provider, the implicit metapolicy needs to live inside the policy manager to hook into the transactional nature of the policy manager. Change-Id: I8f11ea46950da6566833daa65b2e056faa2f63d6 Signed-off-by: Jason Yellick <jyellick@us.ibm.com>
- Loading branch information
Jason Yellick
committed
Feb 17, 2017
1 parent
1ffb87e
commit 018d888
Showing
4 changed files
with
215 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/* | ||
Copyright IBM Corp. 2016 All Rights Reserved. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package policies | ||
|
||
import ( | ||
"fmt" | ||
|
||
cb "github.com/hyperledger/fabric/protos/common" | ||
|
||
"github.com/golang/protobuf/proto" | ||
) | ||
|
||
type implicitMetaPolicy struct { | ||
conf *cb.ImplicitMetaPolicy | ||
threshold int | ||
subPolicies []Policy | ||
} | ||
|
||
// NewPolicy creates a new policy based on the policy bytes | ||
func newImplicitMetaPolicy(data []byte) (*implicitMetaPolicy, error) { | ||
imp := &cb.ImplicitMetaPolicy{} | ||
if err := proto.Unmarshal(data, imp); err != nil { | ||
return nil, fmt.Errorf("Error unmarshaling to ImplicitMetaPolicy: %s", err) | ||
} | ||
|
||
return &implicitMetaPolicy{ | ||
conf: imp, | ||
}, nil | ||
} | ||
|
||
func (imp *implicitMetaPolicy) initialize(config *policyConfig) { | ||
imp.subPolicies = make([]Policy, len(config.managers)) | ||
i := 0 | ||
for _, manager := range config.managers { | ||
imp.subPolicies[i], _ = manager.GetPolicy(imp.conf.SubPolicy) | ||
i++ | ||
} | ||
|
||
switch imp.conf.Rule { | ||
case cb.ImplicitMetaPolicy_ANY: | ||
imp.threshold = 1 | ||
case cb.ImplicitMetaPolicy_ALL: | ||
imp.threshold = len(imp.subPolicies) | ||
case cb.ImplicitMetaPolicy_MAJORITY: | ||
imp.threshold = len(imp.subPolicies)/2 + 1 | ||
} | ||
|
||
// In the special case that there are no policies, consider 0 to be a majority or any | ||
if len(imp.subPolicies) == 0 { | ||
imp.threshold = 0 | ||
} | ||
} | ||
|
||
// Evaluate takes a set of SignedData and evaluates whether this set of signatures satisfies the policy | ||
func (imp *implicitMetaPolicy) Evaluate(signatureSet []*cb.SignedData) error { | ||
remaining := imp.threshold | ||
for _, policy := range imp.subPolicies { | ||
if policy.Evaluate(signatureSet) == nil { | ||
remaining-- | ||
if remaining == 0 { | ||
return nil | ||
} | ||
} | ||
} | ||
if remaining == 0 { | ||
return nil | ||
} | ||
return fmt.Errorf("Failed to reach implicit threshold of %d sub-policies, required %d remaining", imp.threshold, remaining) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
/* | ||
Copyright IBM Corp. 2017 All Rights Reserved. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package policies | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
cb "github.com/hyperledger/fabric/protos/common" | ||
"github.com/hyperledger/fabric/protos/utils" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
const TestPolicyName = "TestPolicyName" | ||
|
||
type acceptPolicy struct{} | ||
|
||
func (rp acceptPolicy) Evaluate(signedData []*cb.SignedData) error { | ||
return nil | ||
} | ||
|
||
func TestimplicitMarshalError(t *testing.T) { | ||
_, err := newImplicitMetaPolicy([]byte("GARBAGE")) | ||
assert.Error(t, err, "Should have errored unmarshaling garbage") | ||
} | ||
|
||
func makeManagers(count, passing int) map[string]*ManagerImpl { | ||
result := make(map[string]*ManagerImpl) | ||
remaining := passing | ||
for i := 0; i < count; i++ { | ||
policyMap := make(map[string]Policy) | ||
if remaining > 0 { | ||
policyMap[TestPolicyName] = acceptPolicy{} | ||
} | ||
remaining-- | ||
|
||
result[fmt.Sprintf("%d", i)] = &ManagerImpl{ | ||
config: &policyConfig{ | ||
policies: policyMap, | ||
}, | ||
} | ||
} | ||
return result | ||
} | ||
|
||
// makePolicyTest creates an implicitMetaPolicy with a set of | ||
func runPolicyTest(rule cb.ImplicitMetaPolicy_Rule, managerCount int, passingCount int) error { | ||
imp, err := newImplicitMetaPolicy(utils.MarshalOrPanic(&cb.ImplicitMetaPolicy{ | ||
Rule: rule, | ||
SubPolicy: TestPolicyName, | ||
})) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
imp.initialize(&policyConfig{ | ||
managers: makeManagers(managerCount, passingCount), | ||
}) | ||
|
||
return imp.Evaluate(nil) | ||
} | ||
|
||
func TestImplicitMetaAny(t *testing.T) { | ||
assert.NoError(t, runPolicyTest(cb.ImplicitMetaPolicy_ANY, 1, 1)) | ||
assert.NoError(t, runPolicyTest(cb.ImplicitMetaPolicy_ANY, 10, 1)) | ||
assert.NoError(t, runPolicyTest(cb.ImplicitMetaPolicy_ANY, 10, 8)) | ||
assert.Error(t, runPolicyTest(cb.ImplicitMetaPolicy_ANY, 10, 0)) | ||
assert.NoError(t, runPolicyTest(cb.ImplicitMetaPolicy_ANY, 0, 0)) | ||
} | ||
|
||
func TestImplicitMetaAll(t *testing.T) { | ||
assert.NoError(t, runPolicyTest(cb.ImplicitMetaPolicy_ALL, 1, 1)) | ||
assert.Error(t, runPolicyTest(cb.ImplicitMetaPolicy_ALL, 10, 1)) | ||
assert.NoError(t, runPolicyTest(cb.ImplicitMetaPolicy_ALL, 10, 10)) | ||
assert.Error(t, runPolicyTest(cb.ImplicitMetaPolicy_ALL, 10, 0)) | ||
assert.NoError(t, runPolicyTest(cb.ImplicitMetaPolicy_ALL, 0, 0)) | ||
} | ||
|
||
func TestImplicitMetaMajority(t *testing.T) { | ||
assert.NoError(t, runPolicyTest(cb.ImplicitMetaPolicy_MAJORITY, 1, 1)) | ||
assert.Error(t, runPolicyTest(cb.ImplicitMetaPolicy_MAJORITY, 10, 5)) | ||
assert.NoError(t, runPolicyTest(cb.ImplicitMetaPolicy_MAJORITY, 10, 6)) | ||
assert.NoError(t, runPolicyTest(cb.ImplicitMetaPolicy_MAJORITY, 3, 2)) | ||
assert.Error(t, runPolicyTest(cb.ImplicitMetaPolicy_MAJORITY, 10, 0)) | ||
assert.NoError(t, runPolicyTest(cb.ImplicitMetaPolicy_MAJORITY, 0, 0)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters