/
client.go
189 lines (155 loc) · 4.34 KB
/
client.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package policy
import (
"sync/atomic"
"github.com/coredns/coredns/plugin/pkg/trace"
"github.com/infobloxopen/themis/pdp"
"github.com/infobloxopen/themis/pep"
)
type pepCacheHitHandler struct{}
func (ch *pepCacheHitHandler) Handle(req interface{}, resp interface{}, err error) {
log.Infof("PEP responding to PDP request from cache %+v", req)
}
func newPepCacheHitHandler() *pepCacheHitHandler {
return &pepCacheHitHandler{}
}
// connect establishes connection to PDP server.
func (p *policyPlugin) connect() error {
log.Infof("Connecting %v", p)
for _, addr := range p.conf.endpoints {
p.connAttempts[addr] = new(uint32)
}
opts := []pep.Option{
pep.WithConnectionTimeout(p.conf.connTimeout),
pep.WithConnectionStateNotification(p.connStateCb),
}
if p.conf.cacheTTL > 0 {
if p.conf.cacheLimit > 0 {
opts = append(opts, pep.WithCacheTTLAndMaxSize(p.conf.cacheTTL, p.conf.cacheLimit))
} else {
opts = append(opts, pep.WithCacheTTL(p.conf.cacheTTL))
}
}
if p.conf.streams <= 0 || !p.conf.hotSpot {
opts = append(opts, pep.WithRoundRobinBalancer(p.conf.endpoints...))
}
if p.conf.streams > 0 {
opts = append(opts, pep.WithStreams(p.conf.streams))
if p.conf.hotSpot {
opts = append(opts, pep.WithHotSpotBalancer(p.conf.endpoints...))
}
}
opts = append(opts, pep.WithAutoRequestSize(p.conf.autoReqSize))
if p.conf.maxReqSize > 0 {
opts = append(opts, pep.WithMaxRequestSize(uint32(p.conf.maxReqSize)))
}
if p.trace != nil {
if t, ok := p.trace.(trace.Trace); ok {
opts = append(opts, pep.WithTracer(t.Tracer()))
}
}
if p.conf.log {
opts = append(opts, pep.WithOnCacheHitHandler(newPepCacheHitHandler()))
}
p.pdp = pep.NewClient(opts...)
return p.pdp.Connect("")
}
// closeConn terminates previously established connection.
func (p *policyPlugin) closeConn() {
if p.pdp != nil {
go func() {
p.wg.Wait()
p.pdp.Close()
}()
}
}
func (p *policyPlugin) validate(buf []pdp.AttributeAssignment, ah *attrHolder, lt int, dbg *dbgMessenger) (bool, error) {
req := ah.attrList(buf, lt)
if len(req) <= 0 {
return true, nil
}
if p.conf.log {
log.Infof("PDP request: %+v", req)
}
var respBuffer []pdp.AttributeAssignment
if !p.conf.autoResAttrs {
respBuffer = p.attrPool.Get()
defer p.attrPool.Put(respBuffer)
}
res := pdp.Response{Obligations: respBuffer}
err := p.pdp.Validate(req, &res)
ah.resetAttribute(attrIndexPolicyAction)
ah.resetAttribute(attrIndexRedirectTo)
ah.resetAttribute(attrIndexLog)
if err != nil {
log.Errorf("Policy validation failed due to error: %s", err)
if dbg != nil {
dbg.appendDefaultDecision(ah.attrList(buf, attrListTypeDefDecision))
} else {
ah.resetAttrList(attrListTypeDefDecision)
}
if ah.actionValue() != actionInvalid {
return true, nil
}
return false, err
}
if p.conf.log {
log.Infof("PDP response: %+v", res)
}
if dbg != nil {
dbg.appendResponse(&res)
}
if res.Effect != pdp.EffectPermit && res.Effect != pdp.EffectDeny {
log.Errorf("Policy validation failed due to PDP error: %s", res.Status)
if dbg != nil {
dbg.appendDefaultDecision(ah.attrList(buf, attrListTypeDefDecision))
} else {
ah.resetAttrList(attrListTypeDefDecision)
}
if ah.actionValue() != actionInvalid {
return true, nil
}
return false, err
}
ah.addAttrList(res.Obligations)
return res.Effect == pdp.EffectPermit, nil
}
func (p *policyPlugin) connStateCb(addr string, state int, err error) {
switch state {
default:
if err != nil {
log.Infof("Unknown connection notification %s (%s)", addr, err)
} else {
log.Infof("Unknown connection notification %s", addr)
}
case pep.StreamingConnectionEstablished:
ptr, ok := p.connAttempts[addr]
if !ok {
ptr = p.unkConnAttempts
}
atomic.StoreUint32(ptr, 0)
log.Infof("Connected to %s", addr)
case pep.StreamingConnectionBroken:
log.Errorf("Connection to %s has been broken", addr)
case pep.StreamingConnectionConnecting:
ptr, ok := p.connAttempts[addr]
if !ok {
ptr = p.unkConnAttempts
}
count := atomic.AddUint32(ptr, 1)
if count <= 1 {
log.Infof("Connecting to %s", addr)
}
if count > 100 {
log.Errorf("Connecting to %s", addr)
atomic.StoreUint32(ptr, 1)
}
case pep.StreamingConnectionFailure:
ptr, ok := p.connAttempts[addr]
if !ok {
ptr = p.unkConnAttempts
}
if atomic.LoadUint32(ptr) <= 1 {
log.Errorf("Failed to connect to %s (%s)", addr, err)
}
}
}