Skip to content

Commit 46f7af0

Browse files
author
Jason Yellick
committed
[FAB-1416] Make Policy message more generic
Today, the Policy message uses a oneof construct to enumerate the allowable values. This makes coding somewhat more convenient, but it makes extending things with additional types more difficult. This changeset switches the Policy message to use the more traditional integer enumerate type with marshaled bytes construct. Change-Id: Ic8e51ce4d1fa5e7fa99352e7dcb456a5b51f6e39 Signed-off-by: Jason Yellick <[email protected]>
1 parent 6e8d216 commit 46f7af0

File tree

6 files changed

+105
-148
lines changed

6 files changed

+105
-148
lines changed

common/policies/policy.go

+6-9
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ import (
2222
"github.com/hyperledger/fabric/common/cauthdsl"
2323
cb "github.com/hyperledger/fabric/protos/common"
2424

25-
"github.com/golang/protobuf/proto"
2625
"errors"
26+
"github.com/golang/protobuf/proto"
2727
)
2828

2929
// Policy is used to determine if a signature is valid
@@ -46,18 +46,15 @@ type policy struct {
4646
}
4747

4848
func newPolicy(policySource *cb.Policy, ch cauthdsl.CryptoHelper) (*policy, error) {
49-
envelopeWrapper, ok := policySource.Type.(*cb.Policy_SignaturePolicy)
50-
51-
if !ok {
52-
return nil, fmt.Errorf("Unknown policy type: %T", policySource.Type)
49+
if policySource.Type != int32(cb.Policy_SIGNATURE) {
50+
return nil, fmt.Errorf("Unknown policy type: %v", policySource.Type)
5351
}
5452

55-
if envelopeWrapper.SignaturePolicy == nil {
56-
return nil, errors.New("Nil signature policy received")
53+
sigPolicy := &cb.SignaturePolicyEnvelope{}
54+
if err := proto.Unmarshal(policySource.Policy, sigPolicy); err != nil {
55+
return nil, fmt.Errorf("Error unmarshaling to SignaturePolicy: %s", err)
5756
}
5857

59-
sigPolicy := envelopeWrapper.SignaturePolicy
60-
6158
evaluator, err := cauthdsl.NewSignaturePolicyEvaluator(sigPolicy, ch)
6259
if err != nil {
6360
return nil, err

common/policies/policy_test.go

+13-7
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package policies
1818

1919
import (
20+
"fmt"
2021
"testing"
2122

2223
"github.com/hyperledger/fabric/common/cauthdsl"
@@ -39,21 +40,26 @@ func init() {
3940
rejectAllPolicy = makePolicySource(false)
4041
}
4142

43+
// The proto utils has become a dumping ground of cyclic imports, it's easier to define this locally
44+
func marshalOrPanic(msg proto.Message) []byte {
45+
data, err := proto.Marshal(msg)
46+
if err != nil {
47+
panic(fmt.Errorf("Error marshaling messages: %s, %s", msg, err))
48+
}
49+
return data
50+
}
51+
4252
func makePolicySource(policyResult bool) []byte {
4353
var policyData *cb.SignaturePolicyEnvelope
4454
if policyResult {
4555
policyData = cauthdsl.AcceptAllPolicy
4656
} else {
4757
policyData = cauthdsl.RejectAllPolicy
4858
}
49-
marshaledPolicy, err := proto.Marshal(&cb.Policy{
50-
Type: &cb.Policy_SignaturePolicy{
51-
SignaturePolicy: policyData,
52-
},
59+
marshaledPolicy := marshalOrPanic(&cb.Policy{
60+
Type: int32(cb.Policy_SIGNATURE),
61+
Policy: marshalOrPanic(policyData),
5362
})
54-
if err != nil {
55-
panic("Error marshaling policy")
56-
}
5763
return marshaledPolicy
5864
}
5965

protos/common/configuration.pb.go

+65-117
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

protos/common/configuration.proto

+8-2
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,18 @@ message ConfigurationSignature {
7373
bytes signature = 2; // Signature over the concatenation of configurationItem bytes and signatureHeader bytes
7474
}
7575

76+
//
77+
7678
// Policy expresses a policy which the orderer can evaluate, because there has been some desire expressed to support
7779
// multiple policy engines, this is typed as a oneof for now
7880
message Policy {
79-
oneof Type {
80-
SignaturePolicyEnvelope SignaturePolicy = 1;
81+
enum PolicyType {
82+
UNKNOWN = 0; // Reserved to check for proper initialization
83+
SIGNATURE = 1;
84+
MSP = 2;
8185
}
86+
int32 type = 1; // For outside implementors, consider the first 1000 types reserved, otherwise one of PolicyType
87+
bytes policy = 2;
8288
}
8389

8490
// SignaturePolicyEnvelope wraps a SignaturePolicy and includes a version for future enhancements

protos/orderer/ab.pb.go

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

protos/utils/configurationutils.go

+12-13
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ import (
2020
"fmt"
2121

2222
cb "github.com/hyperledger/fabric/protos/common"
23+
24+
"github.com/golang/protobuf/proto"
2325
)
2426

2527
// MakeConfigurationItem makes a ConfigurationItem.
@@ -40,29 +42,26 @@ func MakeConfigurationEnvelope(items ...*cb.SignedConfigurationItem) *cb.Configu
4042
}
4143

4244
// MakePolicyOrPanic creates a Policy proto message out of a SignaturePolicyEnvelope, and panics if this operation fails.
43-
// NOTE Expand this as more policy types as supported.
4445
func MakePolicyOrPanic(policyEnvelope interface{}) *cb.Policy {
45-
switch pe := policyEnvelope.(type) {
46-
case *cb.SignaturePolicyEnvelope:
47-
return &cb.Policy{
48-
Type: &cb.Policy_SignaturePolicy{
49-
SignaturePolicy: pe,
50-
},
51-
}
52-
default:
53-
panic("Unknown policy envelope type given")
46+
policy, err := MakePolicy(policyEnvelope)
47+
if err != nil {
48+
panic(err)
5449
}
50+
return policy
5551
}
5652

5753
// MakePolicy creates a Policy proto message out of a SignaturePolicyEnvelope.
5854
// NOTE Expand this as more policy types as supported.
5955
func MakePolicy(policyEnvelope interface{}) (*cb.Policy, error) {
6056
switch pe := policyEnvelope.(type) {
6157
case *cb.SignaturePolicyEnvelope:
58+
m, err := proto.Marshal(pe)
59+
if err != nil {
60+
return nil, err
61+
}
6262
return &cb.Policy{
63-
Type: &cb.Policy_SignaturePolicy{
64-
SignaturePolicy: pe,
65-
},
63+
Type: int32(cb.Policy_SIGNATURE),
64+
Policy: m,
6665
}, nil
6766
default:
6867
return nil, fmt.Errorf("Unknown policy envelope type given")

0 commit comments

Comments
 (0)