github.com/tenywen/fabric@v1.0.0-beta.0.20170620030522-a5b1ed380643/orderer/common/sigfilter/sigfilter.go (about)

     1  /*
     2  Copyright IBM Corp. 2016 All Rights Reserved.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8                   http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package sigfilter
    18  
    19  import (
    20  	"github.com/hyperledger/fabric/common/policies"
    21  	"github.com/hyperledger/fabric/orderer/common/filter"
    22  	cb "github.com/hyperledger/fabric/protos/common"
    23  
    24  	"github.com/op/go-logging"
    25  )
    26  
    27  var logger = logging.MustGetLogger("orderer/common/sigfilter")
    28  
    29  type sigFilter struct {
    30  	policySource  string
    31  	policyManager policies.Manager
    32  }
    33  
    34  // New creates a new signature filter, at every evaluation, the policySource is called
    35  // just before evaluation to get the policy name to use when evaluating the filter
    36  // In general, both the policy name and the policy itself are mutable, this is why
    37  // not only the policy is retrieved at each invocation, but also the name of which
    38  // policy to retrieve
    39  func New(policySource string, policyManager policies.Manager) filter.Rule {
    40  	return &sigFilter{
    41  		policySource:  policySource,
    42  		policyManager: policyManager,
    43  	}
    44  }
    45  
    46  // Apply applies the policy given, resulting in Reject or Forward, never Accept and always with nil Committer
    47  func (sf *sigFilter) Apply(message *cb.Envelope) (filter.Action, filter.Committer) {
    48  	signedData, err := message.AsSignedData()
    49  
    50  	if err != nil {
    51  		if logger.IsEnabledFor(logging.DEBUG) {
    52  			logger.Debugf("Rejecting because of err: %s", err)
    53  		}
    54  		return filter.Reject, nil
    55  	}
    56  
    57  	policy, ok := sf.policyManager.GetPolicy(sf.policySource)
    58  	if !ok {
    59  		if logger.IsEnabledFor(logging.DEBUG) {
    60  			logger.Debugf("Could not find policy %s", sf.policySource)
    61  		}
    62  		return filter.Reject, nil
    63  	}
    64  
    65  	err = policy.Evaluate(signedData)
    66  
    67  	if err == nil {
    68  		if logger.IsEnabledFor(logging.DEBUG) {
    69  			logger.Debugf("Forwarding validly signed message for policy %s", policy)
    70  		}
    71  		return filter.Forward, nil
    72  	}
    73  
    74  	return filter.Reject, nil
    75  }