-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
server.go
432 lines (391 loc) · 11 KB
/
server.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
/*
Copyright 2015-2019 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 services
import (
"encoding/json"
"fmt"
"time"
"github.com/google/go-cmp/cmp"
"github.com/gravitational/trace"
apidefaults "github.com/gravitational/teleport/api/defaults"
"github.com/gravitational/teleport/api/types"
apiutils "github.com/gravitational/teleport/api/utils"
"github.com/gravitational/teleport/lib/defaults"
"github.com/gravitational/teleport/lib/utils"
)
const (
// Equal means two objects are equal
Equal = iota
// OnlyTimestampsDifferent is true when only timestamps are different
OnlyTimestampsDifferent = iota
// Different means that some fields are different
Different = iota
)
// CompareServers compares two provided servers.
func CompareServers(a, b types.Resource) int {
if serverA, ok := a.(types.Server); ok {
if serverB, ok := b.(types.Server); ok {
return compareServers(serverA, serverB)
}
}
if appA, ok := a.(types.AppServer); ok {
if appB, ok := b.(types.AppServer); ok {
return compareApplicationServers(appA, appB)
}
}
if kubeA, ok := a.(types.KubeServer); ok {
if kubeB, ok := b.(types.KubeServer); ok {
return compareKubernetesServers(kubeA, kubeB)
}
}
if dbA, ok := a.(types.DatabaseServer); ok {
if dbB, ok := b.(types.DatabaseServer); ok {
return compareDatabaseServers(dbA, dbB)
}
}
if dbServiceA, ok := a.(types.DatabaseService); ok {
if dbServiceB, ok := b.(types.DatabaseService); ok {
return compareDatabaseServices(dbServiceA, dbServiceB)
}
}
if winA, ok := a.(types.WindowsDesktopService); ok {
if winB, ok := b.(types.WindowsDesktopService); ok {
return compareWindowsDesktopServices(winA, winB)
}
}
return Different
}
func compareServers(a, b types.Server) int {
if a.GetKind() != b.GetKind() {
return Different
}
if a.GetName() != b.GetName() {
return Different
}
if a.GetAddr() != b.GetAddr() {
return Different
}
if a.GetHostname() != b.GetHostname() {
return Different
}
if a.GetNamespace() != b.GetNamespace() {
return Different
}
if len(a.GetPublicAddrs()) != len(b.GetPublicAddrs()) {
return Different
}
for i := range a.GetPublicAddrs() {
if a.GetPublicAddrs()[i] != b.GetPublicAddrs()[i] {
return Different
}
}
r := a.GetRotation()
if !r.Matches(b.GetRotation()) {
return Different
}
if a.GetUseTunnel() != b.GetUseTunnel() {
return Different
}
if !utils.StringMapsEqual(a.GetStaticLabels(), b.GetStaticLabels()) {
return Different
}
if !cmp.Equal(a.GetCmdLabels(), b.GetCmdLabels()) {
return Different
}
if a.GetTeleportVersion() != b.GetTeleportVersion() {
return Different
}
if !cmp.Equal(a.GetApps(), b.GetApps()) {
return Different
}
if !cmp.Equal(a.GetKubernetesClusters(), b.GetKubernetesClusters()) {
return Different
}
if !cmp.Equal(a.GetProxyIDs(), b.GetProxyIDs()) {
return Different
}
// OnlyTimestampsDifferent check must be after all Different checks.
if !a.Expiry().Equal(b.Expiry()) {
return OnlyTimestampsDifferent
}
return Equal
}
func compareApplicationServers(a, b types.AppServer) int {
if a.GetKind() != b.GetKind() {
return Different
}
if a.GetName() != b.GetName() {
return Different
}
if a.GetNamespace() != b.GetNamespace() {
return Different
}
if a.GetTeleportVersion() != b.GetTeleportVersion() {
return Different
}
r := a.GetRotation()
if !r.Matches(b.GetRotation()) {
return Different
}
if !cmp.Equal(a.GetApp(), b.GetApp()) {
return Different
}
if !cmp.Equal(a.GetProxyIDs(), b.GetProxyIDs()) {
return Different
}
// OnlyTimestampsDifferent check must be after all Different checks.
if !a.Expiry().Equal(b.Expiry()) {
return OnlyTimestampsDifferent
}
return Equal
}
func compareDatabaseServices(a, b types.DatabaseService) int {
if a.GetKind() != b.GetKind() {
return Different
}
if a.GetName() != b.GetName() {
return Different
}
if a.GetNamespace() != b.GetNamespace() {
return Different
}
if !cmp.Equal(a.GetResourceMatchers(), b.GetResourceMatchers()) {
return Different
}
if !a.Expiry().Equal(b.Expiry()) {
return OnlyTimestampsDifferent
}
return Equal
}
func compareKubernetesServers(a, b types.KubeServer) int {
if a.GetKind() != b.GetKind() {
return Different
}
if a.GetName() != b.GetName() {
return Different
}
if a.GetNamespace() != b.GetNamespace() {
return Different
}
if a.GetTeleportVersion() != b.GetTeleportVersion() {
return Different
}
r := a.GetRotation()
if !r.Matches(b.GetRotation()) {
return Different
}
if !cmp.Equal(a.GetCluster(), b.GetCluster()) {
return Different
}
if !cmp.Equal(a.GetProxyIDs(), b.GetProxyIDs()) {
return Different
}
// OnlyTimestampsDifferent check must be after all Different checks.
if !a.Expiry().Equal(b.Expiry()) {
return OnlyTimestampsDifferent
}
return Equal
}
func compareDatabaseServers(a, b types.DatabaseServer) int {
if a.GetKind() != b.GetKind() {
return Different
}
if a.GetName() != b.GetName() {
return Different
}
if a.GetNamespace() != b.GetNamespace() {
return Different
}
if a.GetTeleportVersion() != b.GetTeleportVersion() {
return Different
}
r := a.GetRotation()
if !r.Matches(b.GetRotation()) {
return Different
}
if !cmp.Equal(a.GetDatabase(), b.GetDatabase()) {
return Different
}
if !cmp.Equal(a.GetProxyIDs(), b.GetProxyIDs()) {
return Different
}
// OnlyTimestampsDifferent check must be after all Different checks.
if !a.Expiry().Equal(b.Expiry()) {
return OnlyTimestampsDifferent
}
return Equal
}
func compareWindowsDesktopServices(a, b types.WindowsDesktopService) int {
if a.GetKind() != b.GetKind() {
return Different
}
if a.GetName() != b.GetName() {
return Different
}
if a.GetAddr() != b.GetAddr() {
return Different
}
if a.GetTeleportVersion() != b.GetTeleportVersion() {
return Different
}
if !cmp.Equal(a.GetProxyIDs(), b.GetProxyIDs()) {
return Different
}
// OnlyTimestampsDifferent check must be after all Different checks.
if !a.Expiry().Equal(b.Expiry()) {
return OnlyTimestampsDifferent
}
return Equal
}
// CommandLabels is a set of command labels
type CommandLabels map[string]types.CommandLabel
// Clone returns copy of the set
func (c *CommandLabels) Clone() CommandLabels {
out := make(CommandLabels, len(*c))
for name, label := range *c {
out[name] = label.Clone()
}
return out
}
// SetEnv sets the value of the label from environment variable
func (c *CommandLabels) SetEnv(v string) error {
if err := json.Unmarshal([]byte(v), c); err != nil {
return trace.Wrap(err, "can not parse Command Labels")
}
return nil
}
// SortedServers is a sort wrapper that sorts servers by name
type SortedServers []types.Server
func (s SortedServers) Len() int {
return len(s)
}
func (s SortedServers) Less(i, j int) bool {
return s[i].GetName() < s[j].GetName()
}
func (s SortedServers) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
// SortedReverseTunnels sorts reverse tunnels by cluster name
type SortedReverseTunnels []types.ReverseTunnel
func (s SortedReverseTunnels) Len() int {
return len(s)
}
func (s SortedReverseTunnels) Less(i, j int) bool {
return s[i].GetClusterName() < s[j].GetClusterName()
}
func (s SortedReverseTunnels) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
// GuessProxyHostAndVersion tries to find the first proxy with a public
// address configured and return that public addr and version.
// If no proxies are configured, it will return a guessed value by concatenating
// the first proxy's hostname with default port number, and the first proxy's
// version will also be returned.
//
// Returns empty value if there are no proxies.
func GuessProxyHostAndVersion(proxies []types.Server) (string, string, error) {
if len(proxies) == 0 {
return "", "", trace.NotFound("list of proxies empty")
}
// Find the first proxy with a public address set and return it.
for _, proxy := range proxies {
proxyHost := proxy.GetPublicAddr()
if proxyHost != "" {
return proxyHost, proxy.GetTeleportVersion(), nil
}
}
// No proxies have a public address set, return guessed value.
guessProxyHost := fmt.Sprintf("%v:%v", proxies[0].GetHostname(), defaults.HTTPListenPort)
return guessProxyHost, proxies[0].GetTeleportVersion(), nil
}
// UnmarshalServer unmarshals the Server resource from JSON.
func UnmarshalServer(bytes []byte, kind string, opts ...MarshalOption) (types.Server, error) {
cfg, err := CollectOptions(opts)
if err != nil {
return nil, trace.Wrap(err)
}
if len(bytes) == 0 {
return nil, trace.BadParameter("missing server data")
}
var s types.ServerV2
if err := utils.FastUnmarshal(bytes, &s); err != nil {
return nil, trace.BadParameter(err.Error())
}
s.Kind = kind
if err := s.CheckAndSetDefaults(); err != nil {
return nil, trace.Wrap(err)
}
if cfg.ID != 0 {
s.SetResourceID(cfg.ID)
}
if !cfg.Expires.IsZero() {
s.SetExpiry(cfg.Expires)
}
if s.Metadata.Expires != nil {
apiutils.UTC(s.Metadata.Expires)
}
// Force the timestamps to UTC for consistency.
// See https://github.com/gogo/protobuf/issues/519 for details on issues this causes for proto.Clone
apiutils.UTC(&s.Spec.Rotation.Started)
apiutils.UTC(&s.Spec.Rotation.LastRotated)
return &s, nil
}
// MarshalServer marshals the Server resource to JSON.
func MarshalServer(server types.Server, opts ...MarshalOption) ([]byte, error) {
if err := server.CheckAndSetDefaults(); err != nil {
return nil, trace.Wrap(err)
}
cfg, err := CollectOptions(opts)
if err != nil {
return nil, trace.Wrap(err)
}
switch server := server.(type) {
case *types.ServerV2:
if !cfg.PreserveResourceID {
// avoid modifying the original object
// to prevent unexpected data races
copy := *server
copy.SetResourceID(0)
server = ©
}
return utils.FastMarshal(server)
default:
return nil, trace.BadParameter("unrecognized server version %T", server)
}
}
// UnmarshalServers unmarshals a list of Server resources.
func UnmarshalServers(bytes []byte) ([]types.Server, error) {
var servers []types.ServerV2
err := utils.FastUnmarshal(bytes, &servers)
if err != nil {
return nil, trace.Wrap(err)
}
out := make([]types.Server, len(servers))
for i, v := range servers {
out[i] = types.Server(&v)
}
return out, nil
}
// MarshalServers marshals a list of Server resources.
func MarshalServers(s []types.Server) ([]byte, error) {
bytes, err := utils.FastMarshal(s)
if err != nil {
return nil, trace.Wrap(err)
}
return bytes, nil
}
// NodeHasMissedKeepAlives checks if node has missed its keep alive
func NodeHasMissedKeepAlives(s types.Server) bool {
serverExpiry := s.Expiry()
return serverExpiry.Before(time.Now().Add(apidefaults.ServerAnnounceTTL - (apidefaults.ServerKeepAliveTTL() * 2)))
}