forked from gravitational/teleport
-
Notifications
You must be signed in to change notification settings - Fork 0
/
presence.go
356 lines (314 loc) · 11.1 KB
/
presence.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
/*
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 local
import (
"encoding/json"
"sort"
"github.com/gravitational/teleport/lib/backend"
"github.com/gravitational/teleport/lib/services"
"github.com/gravitational/trace"
)
// PresenceService records and reports the presence of all components
// of the cluster - Nodes, Proxies and SSH nodes
type PresenceService struct {
backend.Backend
}
// NewPresenceService returns new presence service instance
func NewPresenceService(backend backend.Backend) *PresenceService {
return &PresenceService{Backend: backend}
}
// UpsertLocalClusterName upserts local domain
func (s *PresenceService) UpsertLocalClusterName(name string) error {
return s.UpsertVal([]string{localClusterPrefix}, "val", []byte(name), backend.Forever)
}
// GetLocalClusterName upserts local domain
func (s *PresenceService) GetLocalClusterName() (string, error) {
data, err := s.GetVal([]string{localClusterPrefix}, "val")
if err != nil {
return "", trace.Wrap(err)
}
return string(data), nil
}
// DeleteAllNamespaces deletes all namespaces
func (s *PresenceService) DeleteAllNamespaces() error {
return s.DeleteBucket([]string{}, namespacesPrefix)
}
// GetNamespaces returns a list of namespaces
func (s *PresenceService) GetNamespaces() ([]services.Namespace, error) {
keys, err := s.GetKeys([]string{namespacesPrefix})
if err != nil {
return nil, trace.Wrap(err)
}
out := make([]services.Namespace, len(keys))
for i, name := range keys {
u, err := s.GetNamespace(name)
if err != nil {
return nil, trace.Wrap(err)
}
out[i] = *u
}
sort.Sort(services.SortedNamespaces(out))
return out, nil
}
// UpsertNamespace upserts namespace
func (s *PresenceService) UpsertNamespace(n services.Namespace) error {
data, err := json.Marshal(n)
if err != nil {
return trace.Wrap(err)
}
ttl := backend.TTL(s.Clock(), n.Metadata.Expiry())
err = s.UpsertVal([]string{namespacesPrefix, n.Metadata.Name}, "params", []byte(data), ttl)
if err != nil {
return trace.Wrap(err)
}
return nil
}
// GetNamespace returns a namespace by name
func (s *PresenceService) GetNamespace(name string) (*services.Namespace, error) {
if name == "" {
return nil, trace.BadParameter("missing namespace name")
}
data, err := s.GetVal([]string{namespacesPrefix, name}, "params")
if err != nil {
if trace.IsNotFound(err) {
return nil, trace.NotFound("namespace %v is not found", name)
}
return nil, trace.Wrap(err)
}
return services.UnmarshalNamespace(data)
}
// DeleteNamespace deletes a namespace with all the keys from the backend
func (s *PresenceService) DeleteNamespace(namespace string) error {
if namespace == "" {
return trace.BadParameter("missing namespace name")
}
err := s.DeleteBucket([]string{namespacesPrefix}, namespace)
if err != nil {
if trace.IsNotFound(err) {
return trace.NotFound("namespace '%v' is not found", namespace)
}
}
return trace.Wrap(err)
}
func (s *PresenceService) getServers(kind, prefix string) ([]services.Server, error) {
keys, err := s.GetKeys([]string{prefix})
if err != nil {
return nil, trace.Wrap(err)
}
servers := make([]services.Server, len(keys))
for i, key := range keys {
data, err := s.GetVal([]string{prefix}, key)
if err != nil {
return nil, trace.Wrap(err)
}
server, err := services.GetServerMarshaler().UnmarshalServer(data, kind)
if err != nil {
return nil, trace.Wrap(err)
}
servers[i] = server
}
// sorting helps with tests and makes it all deterministic
sort.Sort(services.SortedServers(servers))
return servers, nil
}
func (s *PresenceService) upsertServer(prefix string, server services.Server) error {
ttl := backend.TTL(s.Clock(), server.Expiry())
data, err := services.GetServerMarshaler().MarshalServer(server)
if err != nil {
return trace.Wrap(err)
}
err = s.UpsertVal([]string{prefix}, server.GetName(), data, ttl)
return trace.Wrap(err)
}
// DeleteAllNodes deletes all nodes in a namespace
func (s *PresenceService) DeleteAllNodes(namespace string) error {
return s.DeleteBucket([]string{namespacesPrefix, namespace}, nodesPrefix)
}
// GetNodes returns a list of registered servers
func (s *PresenceService) GetNodes(namespace string) ([]services.Server, error) {
if namespace == "" {
return nil, trace.BadParameter("missing namespace value")
}
keys, err := s.GetKeys([]string{namespacesPrefix, namespace, nodesPrefix})
if err != nil {
return nil, trace.Wrap(err)
}
servers := make([]services.Server, 0, len(keys))
for _, key := range keys {
data, err := s.GetVal([]string{namespacesPrefix, namespace, nodesPrefix}, key)
if err != nil {
if trace.IsNotFound(err) {
continue
}
return nil, trace.Wrap(err)
}
server, err := services.GetServerMarshaler().UnmarshalServer(data, services.KindNode)
if err != nil {
return nil, trace.Wrap(err)
}
servers = append(servers, server)
}
// sorting helps with tests and makes it all deterministic
sort.Sort(services.SortedServers(servers))
return servers, nil
}
// UpsertNode registers node presence, permanently if ttl is 0 or
// for the specified duration with second resolution if it's >= 1 second
func (s *PresenceService) UpsertNode(server services.Server) error {
if server.GetNamespace() == "" {
return trace.BadParameter("missing node namespace")
}
data, err := services.GetServerMarshaler().MarshalServer(server)
if err != nil {
return trace.Wrap(err)
}
ttl := backend.TTL(s.Clock(), server.Expiry())
err = s.UpsertVal([]string{namespacesPrefix, server.GetNamespace(), nodesPrefix}, server.GetName(), data, ttl)
return trace.Wrap(err)
}
// GetAuthServers returns a list of registered servers
func (s *PresenceService) GetAuthServers() ([]services.Server, error) {
return s.getServers(services.KindAuthServer, authServersPrefix)
}
// UpsertAuthServer registers auth server presence, permanently if ttl is 0 or
// for the specified duration with second resolution if it's >= 1 second
func (s *PresenceService) UpsertAuthServer(server services.Server) error {
return s.upsertServer(authServersPrefix, server)
}
// UpsertProxy registers proxy server presence, permanently if ttl is 0 or
// for the specified duration with second resolution if it's >= 1 second
func (s *PresenceService) UpsertProxy(server services.Server) error {
return s.upsertServer(proxiesPrefix, server)
}
// GetProxies returns a list of registered proxies
func (s *PresenceService) GetProxies() ([]services.Server, error) {
return s.getServers(services.KindProxy, proxiesPrefix)
}
// DeleteAllProxies deletes all proxies
func (s *PresenceService) DeleteAllProxies() error {
return s.DeleteBucket([]string{}, proxiesPrefix)
}
// DeleteAllReverseTunnels deletes all reverse tunnels
func (s *PresenceService) DeleteAllReverseTunnels() error {
return s.DeleteBucket([]string{}, reverseTunnelsPrefix)
}
// UpsertReverseTunnel upserts reverse tunnel entry temporarily or permanently
func (s *PresenceService) UpsertReverseTunnel(tunnel services.ReverseTunnel) error {
if err := tunnel.Check(); err != nil {
return trace.Wrap(err)
}
data, err := services.GetReverseTunnelMarshaler().MarshalReverseTunnel(tunnel)
if err != nil {
return trace.Wrap(err)
}
ttl := backend.TTL(s.Clock(), tunnel.Expiry())
err = s.UpsertVal([]string{reverseTunnelsPrefix}, tunnel.GetName(), data, ttl)
return trace.Wrap(err)
}
// GetReverseTunnels returns a list of registered servers
func (s *PresenceService) GetReverseTunnels() ([]services.ReverseTunnel, error) {
keys, err := s.GetKeys([]string{reverseTunnelsPrefix})
if err != nil {
return nil, trace.Wrap(err)
}
tunnels := make([]services.ReverseTunnel, len(keys))
for i, key := range keys {
data, err := s.GetVal([]string{reverseTunnelsPrefix}, key)
if err != nil {
return nil, trace.Wrap(err)
}
tunnel, err := services.GetReverseTunnelMarshaler().UnmarshalReverseTunnel(data)
if err != nil {
return nil, trace.Wrap(err)
}
tunnels[i] = tunnel
}
// sorting helps with tests and makes it all deterministic
sort.Sort(services.SortedReverseTunnels(tunnels))
return tunnels, nil
}
// DeleteReverseTunnel deletes reverse tunnel by it's domain name
func (s *PresenceService) DeleteReverseTunnel(domainName string) error {
err := s.DeleteKey([]string{reverseTunnelsPrefix}, domainName)
return trace.Wrap(err)
}
// UpsertTrustedCluster creates or updates a TrustedCluster in the backend.
func (s *PresenceService) UpsertTrustedCluster(trustedCluster services.TrustedCluster) error {
if err := trustedCluster.CheckAndSetDefaults(); err != nil {
return trace.Wrap(err)
}
data, err := services.GetTrustedClusterMarshaler().Marshal(trustedCluster)
if err != nil {
return trace.Wrap(err)
}
ttl := backend.TTL(s.Clock(), trustedCluster.Expiry())
err = s.UpsertVal([]string{"trustedclusters"}, trustedCluster.GetName(), []byte(data), ttl)
if err != nil {
return trace.Wrap(err)
}
return nil
}
// GetTrustedCluster returns a single TrustedCluster by name.
func (s *PresenceService) GetTrustedCluster(name string) (services.TrustedCluster, error) {
data, err := s.GetVal([]string{"trustedclusters"}, name)
if err != nil {
if trace.IsNotFound(err) {
return nil, trace.NotFound("trusted cluster not found")
}
return nil, trace.Wrap(err)
}
return services.GetTrustedClusterMarshaler().Unmarshal(data)
}
// GetTrustedClusters returns all TrustedClusters in the backend.
func (s *PresenceService) GetTrustedClusters() ([]services.TrustedCluster, error) {
keys, err := s.GetKeys([]string{"trustedclusters"})
if err != nil {
return nil, trace.Wrap(err)
}
out := make([]services.TrustedCluster, len(keys))
for i, name := range keys {
tc, err := s.GetTrustedCluster(name)
if err != nil {
return nil, trace.Wrap(err)
}
out[i] = tc
}
sort.Sort(services.SortedTrustedCluster(out))
return out, nil
}
// DeleteTrustedCluster removes a TrustedCluster from the backend by name.
func (s *PresenceService) DeleteTrustedCluster(name string) error {
err := s.DeleteKey([]string{"trustedclusters"}, name)
if err != nil {
if trace.IsNotFound(err) {
return trace.NotFound("trusted cluster %q not found", name)
}
}
return trace.Wrap(err)
}
// EnableTrustedCluster updates the resource on the backend with new state.
func (s *PresenceService) EnableTrustedCluster(t services.TrustedCluster) error {
return s.UpsertTrustedCluster(t)
}
// DisableTrustedCluster updates the resource on the backend with the new state.
func (s *PresenceService) DisableTrustedCluster(t services.TrustedCluster) error {
return s.UpsertTrustedCluster(t)
}
const (
localClusterPrefix = "localCluster"
reverseTunnelsPrefix = "reverseTunnels"
nodesPrefix = "nodes"
namespacesPrefix = "namespaces"
authServersPrefix = "authservers"
proxiesPrefix = "proxies"
)