Skip to content

Commit 79c2b99

Browse files
author
Jason Yellick
committedOct 6, 2017
[FAB-6138] Add simple config policy util functions
This CR mirrors the functionality added in FAB-6137, but for policies instead of config values. There are currently no consumers of these new util functions, but new users will be added later in the series. Change-Id: I963db91d1158caf3931a74a1f83a2bf2b4924807 Signed-off-by: Jason Yellick <jyellick@us.ibm.com>
1 parent 49e427d commit 79c2b99

File tree

2 files changed

+110
-0
lines changed

2 files changed

+110
-0
lines changed
 

‎common/policies/util.go

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
Copyright IBM Corp. All Rights Reserved.
3+
4+
SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
package policies
8+
9+
import (
10+
cb "github.com/hyperledger/fabric/protos/common"
11+
"github.com/hyperledger/fabric/protos/utils"
12+
)
13+
14+
// ConfigPolicy defines a common representation for different *cb.ConfigPolicy values.
15+
type ConfigPolicy interface {
16+
// Key is the key this value should be stored in the *cb.ConfigGroup.Policies map.
17+
Key() string
18+
19+
// Value is the backing policy implementation for this ConfigPolicy
20+
Value() *cb.Policy
21+
}
22+
23+
// StandardConfigValue implements the ConfigValue interface.
24+
type StandardConfigPolicy struct {
25+
key string
26+
value *cb.Policy
27+
}
28+
29+
// Key is the key this value should be stored in the *cb.ConfigGroup.Values map.
30+
func (scv *StandardConfigPolicy) Key() string {
31+
return scv.key
32+
}
33+
34+
// Value is the *cb.Policy which should be stored as the *cb.ConfigPolicy.Policy.
35+
func (scv *StandardConfigPolicy) Value() *cb.Policy {
36+
return scv.value
37+
}
38+
39+
func makeImplicitMetaPolicy(subPolicyName string, rule cb.ImplicitMetaPolicy_Rule) *cb.Policy {
40+
return &cb.Policy{
41+
Type: int32(cb.Policy_IMPLICIT_META),
42+
Value: utils.MarshalOrPanic(&cb.ImplicitMetaPolicy{
43+
Rule: rule,
44+
SubPolicy: subPolicyName,
45+
}),
46+
}
47+
}
48+
49+
// ImplicitMetaAllPolicy defines an implicit meta policy whose sub_policy and key is policyname with rule ALL.
50+
func ImplicitMetaAllPolicy(policyName string) *StandardConfigPolicy {
51+
return &StandardConfigPolicy{
52+
key: policyName,
53+
value: makeImplicitMetaPolicy(policyName, cb.ImplicitMetaPolicy_ALL),
54+
}
55+
}
56+
57+
// ImplicitMetaAnyPolicy defines an implicit meta policy whose sub_policy and key is policyname with rule ANY.
58+
func ImplicitMetaAnyPolicy(policyName string) *StandardConfigPolicy {
59+
return &StandardConfigPolicy{
60+
key: policyName,
61+
value: makeImplicitMetaPolicy(policyName, cb.ImplicitMetaPolicy_ANY),
62+
}
63+
}
64+
65+
// ImplicitMetaMajorityPolicy defines an implicit meta policy whose sub_policy and key is policyname with rule MAJORITY.
66+
func ImplicitMetaMajorityPolicy(policyName string) *StandardConfigPolicy {
67+
return &StandardConfigPolicy{
68+
key: policyName,
69+
value: makeImplicitMetaPolicy(policyName, cb.ImplicitMetaPolicy_MAJORITY),
70+
}
71+
}
72+
73+
// ImplicitMetaMajorityPolicy defines a policy with key policyName and the given signature policy.
74+
func SignaturePolicy(policyName string, sigPolicy *cb.SignaturePolicyEnvelope) *StandardConfigPolicy {
75+
return &StandardConfigPolicy{
76+
key: policyName,
77+
value: &cb.Policy{
78+
Type: int32(cb.Policy_SIGNATURE),
79+
Value: utils.MarshalOrPanic(sigPolicy),
80+
},
81+
}
82+
}

‎common/policies/util_test.go

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
Copyright IBM Corp. All Rights Reserved.
3+
4+
SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
package policies
8+
9+
import (
10+
"testing"
11+
12+
cb "github.com/hyperledger/fabric/protos/common"
13+
14+
"github.com/stretchr/testify/assert"
15+
)
16+
17+
func basicTest(t *testing.T, sv *StandardConfigPolicy) {
18+
assert.NotNil(t, sv)
19+
assert.NotEmpty(t, sv.Key())
20+
assert.NotNil(t, sv.Value())
21+
}
22+
23+
func TestUtilsBasic(t *testing.T) {
24+
basicTest(t, ImplicitMetaAnyPolicy("foo"))
25+
basicTest(t, ImplicitMetaAllPolicy("foo"))
26+
basicTest(t, ImplicitMetaMajorityPolicy("foo"))
27+
basicTest(t, SignaturePolicy("foo", &cb.SignaturePolicyEnvelope{}))
28+
}

0 commit comments

Comments
 (0)
Please sign in to comment.