forked from gravitational/teleport
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cachingaccesspoint.go
442 lines (407 loc) · 11.5 KB
/
cachingaccesspoint.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
/*
Copyright 2015 Gravitational, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package state
import (
"fmt"
"time"
"github.com/gravitational/teleport/lib/auth"
"github.com/gravitational/teleport/lib/backend"
"github.com/gravitational/teleport/lib/defaults"
"github.com/gravitational/teleport/lib/services"
"github.com/gravitational/teleport/lib/services/local"
log "github.com/Sirupsen/logrus"
"github.com/gravitational/trace"
"github.com/jonboulle/clockwork"
"github.com/prometheus/client_golang/prometheus"
)
var (
accessPointRequests = prometheus.NewCounter(
prometheus.CounterOpts{
Name: "access_point_requests",
Help: "Number of access point requests",
},
)
)
func init() {
// Metrics have to be registered to be exposed:
prometheus.MustRegister(accessPointRequests)
}
const (
backoffDuration = time.Second * 10
)
// CachingAuthClient implements auth.AccessPoint interface and remembers
// the previously returned upstream value for each API call.
//
// This which can be used if the upstream AccessPoint goes offline
type CachingAuthClient struct {
Config
// ap points to the access ponit we're caching access to:
ap auth.AccessPoint
// lastErrorTime is a timestamp of the last error when talking to the AP
lastErrorTime time.Time
identity services.Identity
access services.Access
trust services.Trust
presence services.Presence
}
// Config is CachingAuthClient config
type Config struct {
// CacheTTL sets maximum TTL the cache keeps the value
CacheTTL time.Duration
// NeverExpires if set, never expires cache values
NeverExpires bool
// AccessPoint is access point for this
AccessPoint auth.AccessPoint
// Backend is cache backend
Backend backend.Backend
// Clock can be set to control time
Clock clockwork.Clock
// SkipPreload turns off preloading on start
SkipPreload bool
}
// CheckAndSetDefaults checks parameters and sets default values
func (c *Config) CheckAndSetDefaults() error {
if !c.NeverExpires && c.CacheTTL == 0 {
c.CacheTTL = defaults.CacheTTL
}
if c.AccessPoint == nil {
return trace.BadParameter("missing AccessPoint parameter")
}
if c.Backend == nil {
return trace.BadParameter("missing Backend parameter")
}
if c.Clock == nil {
c.Clock = clockwork.NewRealClock()
}
return nil
}
// NewCachingAuthClient creates a new instance of CachingAuthClient using a
// live connection to the auth server (ap)
func NewCachingAuthClient(config Config) (*CachingAuthClient, error) {
if err := config.CheckAndSetDefaults(); err != nil {
return nil, trace.Wrap(err)
}
cs := &CachingAuthClient{
Config: config,
ap: config.AccessPoint,
identity: local.NewIdentityService(config.Backend),
trust: local.NewCAService(config.Backend),
access: local.NewAccessService(config.Backend),
presence: local.NewPresenceService(config.Backend),
}
if !cs.SkipPreload {
err := cs.fetchAll()
if err != nil {
log.Warningf("failed to fetch results for cache %v", err)
}
}
return cs, nil
}
func (cs *CachingAuthClient) fetchAll() error {
var errors []error
_, err := cs.GetDomainName()
errors = append(errors, err)
_, err = cs.GetRoles()
errors = append(errors, err)
namespaces, err := cs.GetNamespaces()
errors = append(errors, err)
if err == nil {
for _, ns := range namespaces {
_, err = cs.GetNodes(ns.Metadata.Name)
errors = append(errors, err)
}
}
_, err = cs.GetProxies()
errors = append(errors, err)
_, err = cs.GetReverseTunnels()
errors = append(errors, err)
_, err = cs.GetCertAuthorities(services.UserCA, false)
errors = append(errors, err)
_, err = cs.GetCertAuthorities(services.HostCA, false)
errors = append(errors, err)
_, err = cs.GetUsers()
errors = append(errors, err)
return trace.NewAggregate(errors...)
}
// GetDomainName is a part of auth.AccessPoint implementation
func (cs *CachingAuthClient) GetDomainName() (clusterName string, err error) {
err = cs.try(func() error {
clusterName, err = cs.ap.GetDomainName()
return err
})
if err != nil {
if trace.IsConnectionProblem(err) {
return cs.presence.GetLocalClusterName()
}
return clusterName, err
}
if err = cs.presence.UpsertLocalClusterName(clusterName); err != nil {
return "", trace.Wrap(err)
}
return clusterName, err
}
// GetRoles is a part of auth.AccessPoint implementation
func (cs *CachingAuthClient) GetRoles() (roles []services.Role, err error) {
err = cs.try(func() error {
roles, err = cs.ap.GetRoles()
return err
})
if err != nil {
if trace.IsConnectionProblem(err) {
return cs.access.GetRoles()
}
return roles, err
}
if err := cs.access.DeleteAllRoles(); err != nil {
if !trace.IsNotFound(err) {
return nil, trace.Wrap(err)
}
}
for _, role := range roles {
if err := cs.access.UpsertRole(role, backend.Forever); err != nil {
return nil, trace.Wrap(err)
}
}
return roles, err
}
func (cs *CachingAuthClient) setTTL(r services.Resource) {
if cs.NeverExpires {
return
}
// honor expiry set by user
if !r.Expiry().IsZero() {
return
}
// set TTL as a global setting
r.SetTTL(cs.Clock, cs.CacheTTL)
}
// GetRole is a part of auth.AccessPoint implementation
func (cs *CachingAuthClient) GetRole(name string) (role services.Role, err error) {
err = cs.try(func() error {
role, err = cs.ap.GetRole(name)
return err
})
if err != nil {
if trace.IsConnectionProblem(err) {
return cs.access.GetRole(name)
}
return role, err
}
if err := cs.access.DeleteRole(name); err != nil {
if !trace.IsNotFound(err) {
return nil, trace.Wrap(err)
}
}
cs.setTTL(role)
if err := cs.access.UpsertRole(role, backend.Forever); err != nil {
return nil, trace.Wrap(err)
}
return role, nil
}
// GetNamespace returns namespace
func (cs *CachingAuthClient) GetNamespace(name string) (namespace *services.Namespace, err error) {
err = cs.try(func() error {
namespace, err = cs.ap.GetNamespace(name)
return err
})
if err != nil {
if trace.IsConnectionProblem(err) {
return cs.presence.GetNamespace(name)
}
return namespace, err
}
if err := cs.presence.DeleteNamespace(name); err != nil {
if !trace.IsNotFound(err) {
return nil, trace.Wrap(err)
}
}
if err := cs.presence.UpsertNamespace(*namespace); err != nil {
return nil, trace.Wrap(err)
}
return namespace, err
}
// GetNamespaces is a part of auth.AccessPoint implementation
func (cs *CachingAuthClient) GetNamespaces() (namespaces []services.Namespace, err error) {
err = cs.try(func() error {
namespaces, err = cs.ap.GetNamespaces()
return err
})
if err != nil {
if trace.IsConnectionProblem(err) {
return cs.presence.GetNamespaces()
}
return namespaces, err
}
if err := cs.presence.DeleteAllNamespaces(); err != nil {
if !trace.IsNotFound(err) {
return nil, trace.Wrap(err)
}
}
for _, ns := range namespaces {
if err := cs.presence.UpsertNamespace(ns); err != nil {
return nil, trace.Wrap(err)
}
}
return namespaces, err
}
// GetNodes is a part of auth.AccessPoint implementation
func (cs *CachingAuthClient) GetNodes(namespace string) (nodes []services.Server, err error) {
err = cs.try(func() error {
nodes, err = cs.ap.GetNodes(namespace)
return err
})
if err != nil {
if trace.IsConnectionProblem(err) {
return cs.presence.GetNodes(namespace)
}
return nodes, err
}
if err := cs.presence.DeleteAllNodes(namespace); err != nil {
if !trace.IsNotFound(err) {
return nil, trace.Wrap(err)
}
}
for _, node := range nodes {
cs.setTTL(node)
if err := cs.presence.UpsertNode(node); err != nil {
return nil, trace.Wrap(err)
}
}
return nodes, err
}
func (cs *CachingAuthClient) GetReverseTunnels() (tunnels []services.ReverseTunnel, err error) {
err = cs.try(func() error {
tunnels, err = cs.ap.GetReverseTunnels()
return err
})
if err != nil {
if trace.IsConnectionProblem(err) {
return cs.presence.GetReverseTunnels()
}
return tunnels, err
}
if err := cs.presence.DeleteAllReverseTunnels(); err != nil {
if !trace.IsNotFound(err) {
return nil, trace.Wrap(err)
}
}
for _, tunnel := range tunnels {
cs.setTTL(tunnel)
if err := cs.presence.UpsertReverseTunnel(tunnel); err != nil {
return nil, trace.Wrap(err)
}
}
return tunnels, err
}
// GetProxies is a part of auth.AccessPoint implementation
func (cs *CachingAuthClient) GetProxies() (proxies []services.Server, err error) {
err = cs.try(func() error {
proxies, err = cs.ap.GetProxies()
return err
})
if err != nil {
if trace.IsConnectionProblem(err) {
return cs.presence.GetProxies()
}
return proxies, err
}
if err := cs.presence.DeleteAllProxies(); err != nil {
if !trace.IsNotFound(err) {
return nil, trace.Wrap(err)
}
}
for _, proxy := range proxies {
cs.setTTL(proxy)
if err := cs.presence.UpsertProxy(proxy); err != nil {
return nil, trace.Wrap(err)
}
}
return proxies, err
}
// GetCertAuthorities is a part of auth.AccessPoint implementation
func (cs *CachingAuthClient) GetCertAuthorities(ct services.CertAuthType, loadKeys bool) (cas []services.CertAuthority, err error) {
err = cs.try(func() error {
cas, err = cs.ap.GetCertAuthorities(ct, loadKeys)
return err
})
if err != nil {
if trace.IsConnectionProblem(err) {
return cs.trust.GetCertAuthorities(ct, loadKeys)
}
return cas, err
}
if err := cs.trust.DeleteAllCertAuthorities(ct); err != nil {
if !trace.IsNotFound(err) {
return nil, trace.Wrap(err)
}
}
for _, ca := range cas {
cs.setTTL(ca)
if err := cs.trust.UpsertCertAuthority(ca); err != nil {
return nil, trace.Wrap(err)
}
}
return cas, err
}
// GetUsers is a part of auth.AccessPoint implementation
func (cs *CachingAuthClient) GetUsers() (users []services.User, err error) {
err = cs.try(func() error {
users, err = cs.ap.GetUsers()
return err
})
if err != nil {
if trace.IsConnectionProblem(err) {
return cs.identity.GetUsers()
}
return users, err
}
if err := cs.identity.DeleteAllUsers(); err != nil {
if !trace.IsNotFound(err) {
return nil, trace.Wrap(err)
}
}
for _, user := range users {
cs.setTTL(user)
if err := cs.identity.UpsertUser(user); err != nil {
return nil, trace.Wrap(err)
}
}
return users, err
}
// UpsertNode is part of auth.AccessPoint implementation
func (cs *CachingAuthClient) UpsertNode(s services.Server) error {
cs.setTTL(s)
return cs.ap.UpsertNode(s)
}
// UpsertProxy is part of auth.AccessPoint implementation
func (cs *CachingAuthClient) UpsertProxy(s services.Server) error {
cs.setTTL(s)
return cs.ap.UpsertProxy(s)
}
// try calls a given function f and checks for errors. If f() fails, the current
// time is recorded. Future calls to f will be ingored until sufficient time passes
// since th last error
func (cs *CachingAuthClient) try(f func() error) error {
tooSoon := cs.lastErrorTime.Add(backoffDuration).After(time.Now())
if tooSoon {
log.Warnf("Backoff: using cached value due to recent errors")
return trace.ConnectionProblem(fmt.Errorf("backoff"), "backing off due to recent errors")
}
accessPointRequests.Inc()
err := trace.ConvertSystemError(f())
if trace.IsConnectionProblem(err) {
cs.lastErrorTime = time.Now()
log.Warningf("Connection Problem: failed connect to the auth servers, using local cache")
}
return err
}