-
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.
Merge "[FAB-6138] Add simple config policy util functions"
- Loading branch information
Showing
2 changed files
with
110 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/* | ||
Copyright IBM Corp. All Rights Reserved. | ||
SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package policies | ||
|
||
import ( | ||
cb "github.com/hyperledger/fabric/protos/common" | ||
"github.com/hyperledger/fabric/protos/utils" | ||
) | ||
|
||
// ConfigPolicy defines a common representation for different *cb.ConfigPolicy values. | ||
type ConfigPolicy interface { | ||
// Key is the key this value should be stored in the *cb.ConfigGroup.Policies map. | ||
Key() string | ||
|
||
// Value is the backing policy implementation for this ConfigPolicy | ||
Value() *cb.Policy | ||
} | ||
|
||
// StandardConfigValue implements the ConfigValue interface. | ||
type StandardConfigPolicy struct { | ||
key string | ||
value *cb.Policy | ||
} | ||
|
||
// Key is the key this value should be stored in the *cb.ConfigGroup.Values map. | ||
func (scv *StandardConfigPolicy) Key() string { | ||
return scv.key | ||
} | ||
|
||
// Value is the *cb.Policy which should be stored as the *cb.ConfigPolicy.Policy. | ||
func (scv *StandardConfigPolicy) Value() *cb.Policy { | ||
return scv.value | ||
} | ||
|
||
func makeImplicitMetaPolicy(subPolicyName string, rule cb.ImplicitMetaPolicy_Rule) *cb.Policy { | ||
return &cb.Policy{ | ||
Type: int32(cb.Policy_IMPLICIT_META), | ||
Value: utils.MarshalOrPanic(&cb.ImplicitMetaPolicy{ | ||
Rule: rule, | ||
SubPolicy: subPolicyName, | ||
}), | ||
} | ||
} | ||
|
||
// ImplicitMetaAllPolicy defines an implicit meta policy whose sub_policy and key is policyname with rule ALL. | ||
func ImplicitMetaAllPolicy(policyName string) *StandardConfigPolicy { | ||
return &StandardConfigPolicy{ | ||
key: policyName, | ||
value: makeImplicitMetaPolicy(policyName, cb.ImplicitMetaPolicy_ALL), | ||
} | ||
} | ||
|
||
// ImplicitMetaAnyPolicy defines an implicit meta policy whose sub_policy and key is policyname with rule ANY. | ||
func ImplicitMetaAnyPolicy(policyName string) *StandardConfigPolicy { | ||
return &StandardConfigPolicy{ | ||
key: policyName, | ||
value: makeImplicitMetaPolicy(policyName, cb.ImplicitMetaPolicy_ANY), | ||
} | ||
} | ||
|
||
// ImplicitMetaMajorityPolicy defines an implicit meta policy whose sub_policy and key is policyname with rule MAJORITY. | ||
func ImplicitMetaMajorityPolicy(policyName string) *StandardConfigPolicy { | ||
return &StandardConfigPolicy{ | ||
key: policyName, | ||
value: makeImplicitMetaPolicy(policyName, cb.ImplicitMetaPolicy_MAJORITY), | ||
} | ||
} | ||
|
||
// ImplicitMetaMajorityPolicy defines a policy with key policyName and the given signature policy. | ||
func SignaturePolicy(policyName string, sigPolicy *cb.SignaturePolicyEnvelope) *StandardConfigPolicy { | ||
return &StandardConfigPolicy{ | ||
key: policyName, | ||
value: &cb.Policy{ | ||
Type: int32(cb.Policy_SIGNATURE), | ||
Value: utils.MarshalOrPanic(sigPolicy), | ||
}, | ||
} | ||
} |
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,28 @@ | ||
/* | ||
Copyright IBM Corp. All Rights Reserved. | ||
SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package policies | ||
|
||
import ( | ||
"testing" | ||
|
||
cb "github.com/hyperledger/fabric/protos/common" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func basicTest(t *testing.T, sv *StandardConfigPolicy) { | ||
assert.NotNil(t, sv) | ||
assert.NotEmpty(t, sv.Key()) | ||
assert.NotNil(t, sv.Value()) | ||
} | ||
|
||
func TestUtilsBasic(t *testing.T) { | ||
basicTest(t, ImplicitMetaAnyPolicy("foo")) | ||
basicTest(t, ImplicitMetaAllPolicy("foo")) | ||
basicTest(t, ImplicitMetaMajorityPolicy("foo")) | ||
basicTest(t, SignaturePolicy("foo", &cb.SignaturePolicyEnvelope{})) | ||
} |