-
Notifications
You must be signed in to change notification settings - Fork 0
/
cluster.go
448 lines (386 loc) · 13.5 KB
/
cluster.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
443
444
445
446
447
448
/*
Copyright 2017 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/gravitational/teleport/lib/defaults"
"github.com/gravitational/teleport/lib/utils"
log "github.com/Sirupsen/logrus"
"github.com/gravitational/trace"
"github.com/jonboulle/clockwork"
)
// TrustedCluster holds information needed for a cluster that can not be directly
// accessed (maybe be behind firewall without any open ports) to join a parent cluster.
type TrustedCluster interface {
// Resource provides common resource properties
Resource
// GetEnabled returns the state of the TrustedCluster.
GetEnabled() bool
// SetEnabled enables (handshake and add ca+reverse tunnel) or disables TrustedCluster.
SetEnabled(bool)
// CombinedMapping is used to specify combined mapping from legacy property Roles
// and new property RoleMap
CombinedMapping() RoleMap
// GetRoleMap returns role map property
GetRoleMap() RoleMap
// SetRoleMap sets role map
SetRoleMap(m RoleMap)
// GetRoles returns the roles for the certificate authority.
GetRoles() []string
// SetRoles sets the roles for the certificate authority.
SetRoles([]string)
// GetToken returns the authorization and authentication token.
GetToken() string
// SetToken sets the authorization and authentication.
SetToken(string)
// GetProxyAddress returns the address of the proxy server.
GetProxyAddress() string
// SetProxyAddress sets the address of the proxy server.
SetProxyAddress(string)
// GetReverseTunnelAddress returns the address of the reverse tunnel.
GetReverseTunnelAddress() string
// SetReverseTunnelAddress sets the address of the reverse tunnel.
SetReverseTunnelAddress(string)
// CheckAndSetDefaults checks and set default values for missing fields.
CheckAndSetDefaults() error
}
// NewTrustedCluster is a convenience wa to create a TrustedCluster resource.
func NewTrustedCluster(name string, spec TrustedClusterSpecV2) (TrustedCluster, error) {
return &TrustedClusterV2{
Kind: KindTrustedCluster,
Version: V2,
Metadata: Metadata{
Name: name,
Namespace: defaults.Namespace,
},
Spec: spec,
}, nil
}
// TrustedClusterV2 implements TrustedCluster.
type TrustedClusterV2 struct {
// Kind is a resource kind - always resource.
Kind string `json:"kind"`
// Version is a resource version.
Version string `json:"version"`
// Metadata is metadata about the resource.
Metadata Metadata `json:"metadata"`
// Spec is the specification of the resource.
Spec TrustedClusterSpecV2 `json:"spec"`
}
// TrustedClusterSpecV2 is the actual data we care about for TrustedClusterSpecV2.
type TrustedClusterSpecV2 struct {
// Enabled is a bool that indicates if the TrustedCluster is enabled or disabled.
// Setting Enabled to false has a side effect of deleting the user and host
// certificate authority (CA).
Enabled bool `json:"enabled"`
// Roles is a list of roles that users will be assuming when connecting to this cluster.
Roles []string `json:"roles,omitempty"`
// Token is the authorization token provided by another cluster needed by
// this cluster to join.
Token string `json:"token"`
// ProxyAddress is the address of the web proxy server of the cluster to join. If not set,
// it is derived from <metadata.name>:<default web proxy server port>.
ProxyAddress string `json:"web_proxy_addr"`
// ReverseTunnelAddress is the address of the SSH proxy server of the cluster to join. If
// not set, it is derived from <metadata.name>:<default reverse tunnel port>.
ReverseTunnelAddress string `json:"tunnel_addr"`
// RoleMap specifies role mappings to remote roles
RoleMap RoleMap `json:"role_map,omitempty"`
}
// RoleMap is a list of mappings
type RoleMap []RoleMapping
// String prints user friendly representation of role mapping
func (r RoleMap) String() string {
directMatch, wildcardMatch, err := r.parse()
if err != nil {
return fmt.Sprintf("<failed to parse: %v", err)
}
if len(wildcardMatch) != 0 {
directMatch[Wildcard] = wildcardMatch
}
if len(directMatch) != 0 {
return fmt.Sprintf("%v", directMatch)
}
return "<empty>"
}
func (r RoleMap) parse() (map[string][]string, []string, error) {
var wildcardMatch []string
directMatch := make(map[string][]string)
for i := range r {
roleMap := r[i]
if roleMap.Remote == "" {
return nil, nil, trace.BadParameter("missing 'remote' parameter for role_map")
}
if len(roleMap.Local) == 0 {
return nil, nil, trace.BadParameter("missing 'local' paramter for 'role_map'")
}
for _, local := range roleMap.Local {
if local == "" {
return nil, nil, trace.BadParameter("missing 'local' property of 'role_map' entry")
}
if local == Wildcard {
return nil, nil, trace.BadParameter("wilcard value is not supported for 'local' property of 'role_map' entry")
}
}
if roleMap.Remote == Wildcard {
if wildcardMatch != nil {
return nil, nil, trace.BadParameter("only one wildcard local role matcher is allowed")
}
wildcardMatch = roleMap.Local
} else {
_, ok := directMatch[roleMap.Remote]
if ok {
return nil, nil, trace.BadParameter("remote role '%v' match is already specified", roleMap.Remote)
}
directMatch[roleMap.Remote] = roleMap.Local
}
}
return directMatch, wildcardMatch, nil
}
// Map maps local roles to remote roles
func (r RoleMap) Map(remoteRoles []string) ([]string, error) {
directMatch, wildcardMatch, err := r.parse()
if err != nil {
return nil, trace.Wrap(err)
}
var outRoles []string
if len(remoteRoles) == 0 && len(wildcardMatch) != 0 {
outRoles = append(outRoles, wildcardMatch...)
return outRoles, nil
}
log.Debugf("%v: direct match: %v wildcard match: %v", r, directMatch, wildcardMatch)
for _, remoteRole := range remoteRoles {
match, ok := directMatch[remoteRole]
if ok {
outRoles = append(outRoles, match...)
}
if wildcardMatch != nil {
outRoles = append(outRoles, wildcardMatch...)
}
}
return outRoles, nil
}
// Check checks RoleMap for errors
func (r RoleMap) Check() error {
_, _, err := r.parse()
return trace.Wrap(err)
}
// RoleMappping provides mapping of remote roles to local roles
// for trusted clusters
type RoleMapping struct {
// Remote specifies remote role name to map from
Remote string `json:"remote"`
// Local specifies local roles to map to
Local []string `json:"local"`
}
// Check checks validity of all parameters and sets defaults
func (c *TrustedClusterV2) CheckAndSetDefaults() error {
// make sure we have defaults for all fields
if err := c.Metadata.CheckAndSetDefaults(); err != nil {
return trace.Wrap(err)
}
// This is to force users to migrate
if len(c.Spec.Roles) != 0 && len(c.Spec.RoleMap) != 0 {
return trace.BadParameter("should set either 'roles' or 'role_map', not both")
}
// we are not mentioning Roles parameter because we are deprecating it
if len(c.Spec.Roles) == 0 && len(c.Spec.RoleMap) == 0 {
return trace.BadParameter("missing 'role_map' parameter")
}
if err := c.Spec.RoleMap.Check(); err != nil {
return trace.Wrap(err)
}
return nil
}
// CombinedMapping is used to specify combined mapping from legacy property Roles
// and new property RoleMap
func (c *TrustedClusterV2) CombinedMapping() RoleMap {
if len(c.Spec.Roles) != 0 {
return []RoleMapping{{Remote: Wildcard, Local: c.Spec.Roles}}
}
return c.Spec.RoleMap
}
// GetRoleMap returns role map property
func (c *TrustedClusterV2) GetRoleMap() RoleMap {
return c.Spec.RoleMap
}
// SetRoleMap sets role map
func (c *TrustedClusterV2) SetRoleMap(m RoleMap) {
c.Spec.RoleMap = m
}
// GetMetadata returns object metadata
func (c *TrustedClusterV2) GetMetadata() Metadata {
return c.Metadata
}
// SetExpiry sets expiry time for the object
func (c *TrustedClusterV2) SetExpiry(expires time.Time) {
c.Metadata.SetExpiry(expires)
}
// Expires retuns object expiry setting
func (c *TrustedClusterV2) Expiry() time.Time {
return c.Metadata.Expiry()
}
// SetTTL sets Expires header using realtime clock
func (c *TrustedClusterV2) SetTTL(clock clockwork.Clock, ttl time.Duration) {
c.Metadata.SetTTL(clock, ttl)
}
// GetName returns the name of the TrustedCluster.
func (c *TrustedClusterV2) GetName() string {
return c.Metadata.Name
}
// SetName sets the name of the TrustedCluster.
func (c *TrustedClusterV2) SetName(e string) {
c.Metadata.Name = e
}
// GetEnabled returns the state of the TrustedCluster.
func (c *TrustedClusterV2) GetEnabled() bool {
return c.Spec.Enabled
}
// SetEnabled enables (handshake and add ca+reverse tunnel) or disables TrustedCluster.
func (c *TrustedClusterV2) SetEnabled(e bool) {
c.Spec.Enabled = e
}
// GetRoles returns the roles for the certificate authority.
func (c *TrustedClusterV2) GetRoles() []string {
return c.Spec.Roles
}
// SetRoles sets the roles for the certificate authority.
func (c *TrustedClusterV2) SetRoles(e []string) {
c.Spec.Roles = e
}
// GetToken returns the authorization and authentication token.
func (c *TrustedClusterV2) GetToken() string {
return c.Spec.Token
}
// SetToken sets the authorization and authentication.
func (c *TrustedClusterV2) SetToken(e string) {
c.Spec.Token = e
}
// GetProxyAddress returns the address of the proxy server.
func (c *TrustedClusterV2) GetProxyAddress() string {
return c.Spec.ProxyAddress
}
// SetProxyAddress sets the address of the proxy server.
func (c *TrustedClusterV2) SetProxyAddress(e string) {
c.Spec.ProxyAddress = e
}
// GetReverseTunnelAddress returns the address of the reverse tunnel.
func (c *TrustedClusterV2) GetReverseTunnelAddress() string {
return c.Spec.ReverseTunnelAddress
}
// SetReverseTunnelAddress sets the address of the reverse tunnel.
func (c *TrustedClusterV2) SetReverseTunnelAddress(e string) {
c.Spec.ReverseTunnelAddress = e
}
// String represents a human readable version of trusted cluster settings.
func (c *TrustedClusterV2) String() string {
return fmt.Sprintf("TrustedCluster(Enabled=%v,Roles=%v,Token=%v,ProxyAddress=%v,ReverseTunnelAddress=%v)",
c.Spec.Enabled, c.Spec.Roles, c.Spec.Token, c.Spec.ProxyAddress, c.Spec.ReverseTunnelAddress)
}
// TrustedClusterSpecSchemaTemplate is a template for trusted cluster schema
const TrustedClusterSpecSchemaTemplate = `{
"type": "object",
"additionalProperties": false,
"properties": {
"enabled": {"type": "boolean"},
"roles": {
"type": "array",
"items": {
"type": "string"
}
},
"role_map": %v,
"token": {"type": "string"},
"web_proxy_addr": {"type": "string"},
"tunnel_addr": {"type": "string"}%v
}
}`
// RoleMapSchema is a schema for role mappings of trusted clusters
const RoleMapSchema = `{
"type": "array",
"items": {
"type": "object",
"additionalProperties": false,
"properties": {
"local": {
"type": "array",
"items": {
"type": "string"
}
},
"remote": {"type": "string"}
}
}
}`
// GetTrustedClusterSchema returns the schema with optionally injected
// schema for extensions.
func GetTrustedClusterSchema(extensionSchema string) string {
var trustedClusterSchema string
if trustedClusterSchema == "" {
trustedClusterSchema = fmt.Sprintf(TrustedClusterSpecSchemaTemplate, RoleMapSchema, "")
} else {
trustedClusterSchema = fmt.Sprintf(TrustedClusterSpecSchemaTemplate, RoleMapSchema, ","+extensionSchema)
}
return fmt.Sprintf(V2SchemaTemplate, MetadataSchema, trustedClusterSchema)
}
// TrustedClusterMarshaler implements marshal/unmarshal of TrustedCluster implementations
// mostly adds support for extended versions.
type TrustedClusterMarshaler interface {
Marshal(c TrustedCluster, opts ...MarshalOption) ([]byte, error)
Unmarshal(bytes []byte) (TrustedCluster, error)
}
var trustedClusterMarshaler TrustedClusterMarshaler = &TeleportTrustedClusterMarshaler{}
func SetTrustedClusterMarshaler(m TrustedClusterMarshaler) {
marshalerMutex.Lock()
defer marshalerMutex.Unlock()
trustedClusterMarshaler = m
}
func GetTrustedClusterMarshaler() TrustedClusterMarshaler {
marshalerMutex.Lock()
defer marshalerMutex.Unlock()
return trustedClusterMarshaler
}
type TeleportTrustedClusterMarshaler struct{}
// Unmarshal unmarshals role from JSON or YAML.
func (t *TeleportTrustedClusterMarshaler) Unmarshal(bytes []byte) (TrustedCluster, error) {
var trustedCluster TrustedClusterV2
if len(bytes) == 0 {
return nil, trace.BadParameter("missing resource data")
}
err := utils.UnmarshalWithSchema(GetTrustedClusterSchema(""), &trustedCluster, bytes)
if err != nil {
return nil, trace.BadParameter(err.Error())
}
utils.UTC(&trustedCluster.Metadata.Expires)
return &trustedCluster, nil
}
// Marshal marshals role to JSON or YAML.
func (t *TeleportTrustedClusterMarshaler) Marshal(c TrustedCluster, opts ...MarshalOption) ([]byte, error) {
return json.Marshal(c)
}
// SortedTrustedCluster sorts clusters by name
type SortedTrustedCluster []TrustedCluster
// Len returns the length of a list.
func (s SortedTrustedCluster) Len() int {
return len(s)
}
// Less compares items by name.
func (s SortedTrustedCluster) Less(i, j int) bool {
return s[i].GetName() < s[j].GetName()
}
// Swap swaps two items in a list.
func (s SortedTrustedCluster) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}