Skip to content

Commit

Permalink
[FAB-1416] Make Policy message more generic
Browse files Browse the repository at this point in the history
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 <jyellick@us.ibm.com>
  • Loading branch information
Jason Yellick committed Jan 2, 2017
1 parent 6e8d216 commit 46f7af0
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 148 deletions.
15 changes: 6 additions & 9 deletions common/policies/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ import (
"github.com/hyperledger/fabric/common/cauthdsl"
cb "github.com/hyperledger/fabric/protos/common"

"github.com/golang/protobuf/proto"
"errors"
"github.com/golang/protobuf/proto"
)

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

func newPolicy(policySource *cb.Policy, ch cauthdsl.CryptoHelper) (*policy, error) {
envelopeWrapper, ok := policySource.Type.(*cb.Policy_SignaturePolicy)

if !ok {
return nil, fmt.Errorf("Unknown policy type: %T", policySource.Type)
if policySource.Type != int32(cb.Policy_SIGNATURE) {
return nil, fmt.Errorf("Unknown policy type: %v", policySource.Type)
}

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

sigPolicy := envelopeWrapper.SignaturePolicy

evaluator, err := cauthdsl.NewSignaturePolicyEvaluator(sigPolicy, ch)
if err != nil {
return nil, err
Expand Down
20 changes: 13 additions & 7 deletions common/policies/policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package policies

import (
"fmt"
"testing"

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

// The proto utils has become a dumping ground of cyclic imports, it's easier to define this locally
func marshalOrPanic(msg proto.Message) []byte {
data, err := proto.Marshal(msg)
if err != nil {
panic(fmt.Errorf("Error marshaling messages: %s, %s", msg, err))
}
return data
}

func makePolicySource(policyResult bool) []byte {
var policyData *cb.SignaturePolicyEnvelope
if policyResult {
policyData = cauthdsl.AcceptAllPolicy
} else {
policyData = cauthdsl.RejectAllPolicy
}
marshaledPolicy, err := proto.Marshal(&cb.Policy{
Type: &cb.Policy_SignaturePolicy{
SignaturePolicy: policyData,
},
marshaledPolicy := marshalOrPanic(&cb.Policy{
Type: int32(cb.Policy_SIGNATURE),
Policy: marshalOrPanic(policyData),
})
if err != nil {
panic("Error marshaling policy")
}
return marshaledPolicy
}

Expand Down
182 changes: 65 additions & 117 deletions protos/common/configuration.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 8 additions & 2 deletions protos/common/configuration.proto
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,18 @@ message ConfigurationSignature {
bytes signature = 2; // Signature over the concatenation of configurationItem bytes and signatureHeader bytes
}

//

// Policy expresses a policy which the orderer can evaluate, because there has been some desire expressed to support
// multiple policy engines, this is typed as a oneof for now
message Policy {
oneof Type {
SignaturePolicyEnvelope SignaturePolicy = 1;
enum PolicyType {
UNKNOWN = 0; // Reserved to check for proper initialization
SIGNATURE = 1;
MSP = 2;
}
int32 type = 1; // For outside implementors, consider the first 1000 types reserved, otherwise one of PolicyType
bytes policy = 2;
}

// SignaturePolicyEnvelope wraps a SignaturePolicy and includes a version for future enhancements
Expand Down
1 change: 1 addition & 0 deletions protos/orderer/ab.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 12 additions & 13 deletions protos/utils/configurationutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import (
"fmt"

cb "github.com/hyperledger/fabric/protos/common"

"github.com/golang/protobuf/proto"
)

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

// MakePolicyOrPanic creates a Policy proto message out of a SignaturePolicyEnvelope, and panics if this operation fails.
// NOTE Expand this as more policy types as supported.
func MakePolicyOrPanic(policyEnvelope interface{}) *cb.Policy {
switch pe := policyEnvelope.(type) {
case *cb.SignaturePolicyEnvelope:
return &cb.Policy{
Type: &cb.Policy_SignaturePolicy{
SignaturePolicy: pe,
},
}
default:
panic("Unknown policy envelope type given")
policy, err := MakePolicy(policyEnvelope)
if err != nil {
panic(err)
}
return policy
}

// MakePolicy creates a Policy proto message out of a SignaturePolicyEnvelope.
// NOTE Expand this as more policy types as supported.
func MakePolicy(policyEnvelope interface{}) (*cb.Policy, error) {
switch pe := policyEnvelope.(type) {
case *cb.SignaturePolicyEnvelope:
m, err := proto.Marshal(pe)
if err != nil {
return nil, err
}
return &cb.Policy{
Type: &cb.Policy_SignaturePolicy{
SignaturePolicy: pe,
},
Type: int32(cb.Policy_SIGNATURE),
Policy: m,
}, nil
default:
return nil, fmt.Errorf("Unknown policy envelope type given")
Expand Down

0 comments on commit 46f7af0

Please sign in to comment.