Skip to content

Commit

Permalink
Merge "[FAB-1565] Add signature filter"
Browse files Browse the repository at this point in the history
  • Loading branch information
binhn authored and Gerrit Code Review committed Jan 10, 2017
2 parents 2e2e746 + c604d8e commit 0587bb2
Show file tree
Hide file tree
Showing 2 changed files with 153 additions and 0 deletions.
70 changes: 70 additions & 0 deletions orderer/common/sigfilter/sigfilter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
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 sigfilter

import (
"github.com/hyperledger/fabric/common/policies"
"github.com/hyperledger/fabric/orderer/common/filter"
cb "github.com/hyperledger/fabric/protos/common"

"github.com/op/go-logging"
)

var logger = logging.MustGetLogger("orderer/common/sigfilter")

type sigFilter struct {
policySource func() string
policyManager policies.Manager
}

// New creates a new signature filter, at every evaluation, the policySource is called
// just before evaluation to get the policy name to use when evaluating the filter
func New(policySource func() string, policyManager policies.Manager) filter.Rule {
return &sigFilter{
policySource: policySource,
policyManager: policyManager,
}
}

// Apply applies the policy given, resulting in Reject or Forward, never Accept and always with nil Committer
func (sf *sigFilter) Apply(message *cb.Envelope) (filter.Action, filter.Committer) {
signedData, err := message.AsSignedData()

if err != nil {
if logger.IsEnabledFor(logging.DEBUG) {
logger.Debugf("Rejecting because of err: %s", err)
}
return filter.Reject, nil
}

policy, ok := sf.policyManager.GetPolicy(sf.policySource())
if !ok {
logger.Debugf("Rejecting because policy was not found")
return filter.Reject, nil
}

err = policy.Evaluate(signedData)

if err != nil {
if logger.IsEnabledFor(logging.DEBUG) {
logger.Debugf("Rejecting because policy did not evaluate without error: %s", err)
}
return filter.Reject, nil
}

return filter.Forward, nil
}
83 changes: 83 additions & 0 deletions orderer/common/sigfilter/sigfilter_test.go
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 sigfilter

import (
"fmt"
"testing"

"github.com/hyperledger/fabric/orderer/common/filter"
mockpolicies "github.com/hyperledger/fabric/orderer/mocks/policies"
cb "github.com/hyperledger/fabric/protos/common"
"github.com/hyperledger/fabric/protos/utils"

"github.com/op/go-logging"
)

func init() {
logging.SetLevel(logging.DEBUG, "")
}

func makeEnvelope() *cb.Envelope {
return &cb.Envelope{
Payload: utils.MarshalOrPanic(&cb.Payload{
Header: &cb.Header{
SignatureHeader: &cb.SignatureHeader{},
},
}),
}
}

func fooSource() string {
return "foo"
}

func TestAccept(t *testing.T) {
mpm := &mockpolicies.Manager{Policy: &mockpolicies.Policy{}}
sf := New(fooSource, mpm)
result, _ := sf.Apply(makeEnvelope())
if result != filter.Forward {
t.Fatalf("Should have accepted envelope")
}
}

func TestMissingPolicy(t *testing.T) {
mpm := &mockpolicies.Manager{}
sf := New(fooSource, mpm)
result, _ := sf.Apply(makeEnvelope())
if result != filter.Reject {
t.Fatalf("Should have rejected when missing policy")
}
}

func TestEmptyPayload(t *testing.T) {
mpm := &mockpolicies.Manager{Policy: &mockpolicies.Policy{}}
sf := New(fooSource, mpm)
result, _ := sf.Apply(&cb.Envelope{})
if result != filter.Reject {
t.Fatalf("Should have rejected when payload empty")
}
}

func TestErrorOnPolicy(t *testing.T) {
mpm := &mockpolicies.Manager{Policy: &mockpolicies.Policy{Err: fmt.Errorf("Error")}}
sf := New(fooSource, mpm)
result, _ := sf.Apply(makeEnvelope())
if result != filter.Reject {
t.Fatalf("Should have rejected when policy evaluated to err")
}
}

0 comments on commit 0587bb2

Please sign in to comment.