-
Notifications
You must be signed in to change notification settings - Fork 0
/
validate.go
74 lines (64 loc) · 1.99 KB
/
validate.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/*
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
}