-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
trustedcluster.go
293 lines (246 loc) · 8.5 KB
/
trustedcluster.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
/*
Copyright 2020 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 types
import (
"fmt"
"time"
"github.com/google/go-cmp/cmp"
"github.com/gravitational/teleport/api/v7/utils"
"github.com/gravitational/trace"
)
// 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
// SetMetadata sets object metadata
SetMetadata(meta Metadata)
// 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)
// CanChangeStateTo checks the TrustedCluster can transform into another.
CanChangeStateTo(TrustedCluster) error
}
// NewTrustedCluster is a convenience way to create a TrustedCluster resource.
func NewTrustedCluster(name string, spec TrustedClusterSpecV2) (TrustedCluster, error) {
c := &TrustedClusterV2{
Metadata: Metadata{
Name: name,
},
Spec: spec,
}
if err := c.CheckAndSetDefaults(); err != nil {
return nil, trace.Wrap(err)
}
return c, nil
}
// setStaticFields sets static resource header and metadata fields.
func (c *TrustedClusterV2) setStaticFields() {
c.Kind = KindTrustedCluster
c.Version = V2
}
// CheckAndSetDefaults checks validity of all parameters and sets defaults
func (c *TrustedClusterV2) CheckAndSetDefaults() error {
c.setStaticFields()
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")
}
// Imply that by default proxy listens on the same port for
// web and reverse tunnel connections
if c.Spec.ReverseTunnelAddress == "" {
c.Spec.ReverseTunnelAddress = c.Spec.ProxyAddress
}
return nil
}
// GetVersion returns resource version
func (c *TrustedClusterV2) GetVersion() string {
return c.Version
}
// GetKind returns resource kind
func (c *TrustedClusterV2) GetKind() string {
return c.Kind
}
// GetSubKind returns resource sub kind
func (c *TrustedClusterV2) GetSubKind() string {
return c.SubKind
}
// SetSubKind sets resource subkind
func (c *TrustedClusterV2) SetSubKind(s string) {
c.SubKind = s
}
// GetResourceID returns resource ID
func (c *TrustedClusterV2) GetResourceID() int64 {
return c.Metadata.ID
}
// SetResourceID sets resource ID
func (c *TrustedClusterV2) SetResourceID(id int64) {
c.Metadata.ID = id
}
// 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
}
// SetMetadata sets object metadata
func (c *TrustedClusterV2) SetMetadata(meta Metadata) {
c.Metadata = meta
}
// SetExpiry sets expiry time for the object
func (c *TrustedClusterV2) SetExpiry(expires time.Time) {
c.Metadata.SetExpiry(expires)
}
// Expiry returns object expiry setting
func (c *TrustedClusterV2) Expiry() time.Time {
return c.Metadata.Expiry()
}
// 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
}
// CanChangeStateTo checks if the state change is allowed or not. If not, returns
// an error explaining the reason.
func (c *TrustedClusterV2) CanChangeStateTo(t TrustedCluster) error {
immutableFieldErr := func(name string) error {
return trace.BadParameter("can not update %s for existing leaf cluster, delete and re-create this leaf cluster with updated %s", name, name)
}
if c.GetToken() != t.GetToken() {
return immutableFieldErr("token")
}
if c.GetProxyAddress() != t.GetProxyAddress() {
return immutableFieldErr("web_proxy_address")
}
if c.GetReverseTunnelAddress() != t.GetReverseTunnelAddress() {
return immutableFieldErr("tunnel_addr")
}
if !utils.StringSlicesEqual(c.GetRoles(), t.GetRoles()) {
return immutableFieldErr("roles")
}
if !cmp.Equal(c.GetRoleMap(), t.GetRoleMap()) {
return immutableFieldErr("role_map")
}
if c.GetEnabled() == t.GetEnabled() {
if t.GetEnabled() {
return trace.AlreadyExists("leaf cluster is already enabled, this update would have no effect")
}
return trace.AlreadyExists("leaf cluster is already disabled, this update would have no effect")
}
return nil
}
// 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)
}
// RoleMap is a list of mappings
type RoleMap []RoleMapping
// 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]
}