1 Star 0 Fork 0

peter/fabric

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
validate.go 1.99 KB
一键复制 编辑 原始数据 按行查看 历史
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package admin
import (
"context"
"time"
"github.com/hyperledger/fabric/common/util"
"github.com/hyperledger/fabric/protos/common"
"github.com/hyperledger/fabric/protos/peer"
"github.com/hyperledger/fabric/protos/utils"
"github.com/pkg/errors"
)
var (
accessDenied = errors.New("access denied")
timeDiff = time.Minute * 15
)
type validator struct {
ace AccessControlEvaluator
}
func (v *validator) validate(ctx context.Context, env *common.Envelope) (*peer.AdminOperation, error) {
op, sd, err := validateStructure(ctx, env)
if err != nil {
return nil, err
}
addr := util.ExtractRemoteAddress(ctx)
if err := v.ace.Evaluate(sd); err != nil {
logger.Warningf("Request from %s unauthorized due to authentication: %v", addr, err)
return nil, accessDenied
}
return op, nil
}
func validateStructure(ctx context.Context, env *common.Envelope) (*peer.AdminOperation, []*common.SignedData, error) {
if ctx == nil {
return nil, nil, errors.New("nil context")
}
if env == nil {
return nil, nil, errors.New("nil envelope")
}
addr := util.ExtractRemoteAddress(ctx)
op := &peer.AdminOperation{}
ch, err := utils.UnmarshalEnvelopeOfType(env, common.HeaderType_PEER_ADMIN_OPERATION, op)
if err != nil {
logger.Warningf("Request from %s is badly formed: +%v", addr, err)
return nil, nil, errors.Wrap(err, "bad request")
}
if ch.Timestamp == nil {
logger.Warningf("Request from %s has no timestamp", addr)
return nil, nil, errors.Errorf("empty timestamp")
}
ts := ch.Timestamp
reqTs := time.Unix(ts.Seconds, int64(ts.Nanos))
now := time.Now()
if reqTs.Add(timeDiff).Before(now) || reqTs.Add(-timeDiff).After(now) {
logger.Warningf("Request from %s unauthorized due to incorrect time: %s", addr, reqTs.String())
return nil, nil, accessDenied
}
sd, err := env.AsSignedData()
if err != nil {
return nil, nil, errors.Errorf("bad request, cannot extract signed data: %v", err)
}
return op, sd, nil
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/peter_code_git/fabric.git
git@gitee.com:peter_code_git/fabric.git
peter_code_git
fabric
fabric
v1.4.6

搜索帮助