-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathacl.go
75 lines (61 loc) · 2.21 KB
/
acl.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
75
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package deliver
import (
"time"
"github.com/hyperledger/fabric/protos/common"
"github.com/pkg/errors"
)
// ExpiresAtFunc is used to extract the time at which an identity expires.
type ExpiresAtFunc func(identityBytes []byte) time.Time
// ConfigSequencer provides the sequence number of the current config block.
type ConfigSequencer interface {
Sequence() uint64
}
// NewSessionAC creates an instance of SessionAccessControl. This constructor will
// return an error if a signature header cannot be extracted from the envelope.
func NewSessionAC(chain ConfigSequencer, env *common.Envelope, policyChecker PolicyChecker, channelID string, expiresAt ExpiresAtFunc) (*SessionAccessControl, error) {
signedData, err := env.AsSignedData()
if err != nil {
return nil, err
}
return &SessionAccessControl{
envelope: env,
channelID: channelID,
sequencer: chain,
policyChecker: policyChecker,
sessionEndTime: expiresAt(signedData[0].Identity),
}, nil
}
// SessionAccessControl holds access control related data for a common Envelope
// that is used to determine if a request is allowed for the identity
// associated with the request envelope.
type SessionAccessControl struct {
sequencer ConfigSequencer
policyChecker PolicyChecker
channelID string
envelope *common.Envelope
lastConfigSequence uint64
sessionEndTime time.Time
usedAtLeastOnce bool
}
// Evaluate uses the PolicyChecker to determine if a request should be allowed.
// The decision is cached until the identity expires or the chain configuration
// changes.
func (ac *SessionAccessControl) Evaluate() error {
if !ac.sessionEndTime.IsZero() && time.Now().After(ac.sessionEndTime) {
return errors.Errorf("client identity expired %v before", time.Since(ac.sessionEndTime))
}
policyCheckNeeded := !ac.usedAtLeastOnce
if currentConfigSequence := ac.sequencer.Sequence(); currentConfigSequence > ac.lastConfigSequence {
ac.lastConfigSequence = currentConfigSequence
policyCheckNeeded = true
}
if !policyCheckNeeded {
return nil
}
ac.usedAtLeastOnce = true
return ac.policyChecker.CheckPolicy(ac.envelope, ac.channelID)
}