-
Notifications
You must be signed in to change notification settings - Fork 0
/
provisioning.go
360 lines (315 loc) · 9.03 KB
/
provisioning.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
/*
Copyright 2015-2018 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 (
"fmt"
"time"
"github.com/gravitational/teleport"
"github.com/gravitational/teleport/lib/defaults"
"github.com/gravitational/teleport/lib/utils"
"github.com/gravitational/trace"
"github.com/jonboulle/clockwork"
)
// MustCreateProvisionToken returns a new valid provision token
// or panics, used in testes
func MustCreateProvisionToken(token string, roles teleport.Roles, expires time.Time) ProvisionToken {
t, err := NewProvisionToken(token, roles, expires)
if err != nil {
panic(err)
}
return t
}
// NewProvisionToken returns a new instance of provision token resource
func NewProvisionToken(token string, roles teleport.Roles, expires time.Time) (ProvisionToken, error) {
t := &ProvisionTokenV2{
Kind: KindToken,
Version: V2,
Metadata: Metadata{
Name: token,
Expires: &expires,
Namespace: defaults.Namespace,
},
Spec: ProvisionTokenSpecV2{
Roles: roles,
},
}
if err := t.CheckAndSetDefaults(); err != nil {
return nil, trace.Wrap(err)
}
return t, nil
}
// Provisioner governs adding new nodes to the cluster
type Provisioner interface {
// UpsertToken adds provisioning tokens for the auth server
UpsertToken(ProvisionToken) error
// GetToken finds and returns token by id
GetToken(token string) (ProvisionToken, error)
// DeleteToken deletes provisioning token
DeleteToken(token string) error
// DeleteAllTokens deletes all provisioning tokens
DeleteAllTokens() error
// GetTokens returns all non-expired tokens
GetTokens(opts ...MarshalOption) ([]ProvisionToken, error)
}
// ProvisionToken is a provisioning token
type ProvisionToken interface {
Resource
// GetRoles returns a list of teleport roles
// that will be granted to the user of the token
// in the crendentials
GetRoles() teleport.Roles
// SetRoles sets teleport roles
SetRoles(teleport.Roles)
// V1 returns V1 version of the resource
V1() *ProvisionTokenV1
// String returns user friendly representation of the resource
String() string
// CheckAndSetDefaults checks parameters and sets default values
CheckAndSetDefaults() error
}
// ProvisionTokensToV1 converts provision tokens to V1 list
func ProvisionTokensToV1(in []ProvisionToken) []ProvisionTokenV1 {
if in == nil {
return nil
}
out := make([]ProvisionTokenV1, len(in))
for i := range in {
out[i] = *in[i].V1()
}
return out
}
// ProvisionTokensFromV1 converts V1 provision tokens to resource list
func ProvisionTokensFromV1(in []ProvisionTokenV1) []ProvisionToken {
if in == nil {
return nil
}
out := make([]ProvisionToken, len(in))
for i := range in {
out[i] = in[i].V2()
}
return out
}
// CheckAndSetDefaults checks and set default values for any missing fields.
func (p *ProvisionTokenV2) CheckAndSetDefaults() error {
p.Kind = KindToken
err := p.Metadata.CheckAndSetDefaults()
if err != nil {
return trace.Wrap(err)
}
if len(p.Spec.Roles) == 0 {
return trace.BadParameter("provisioning token is missing roles")
}
if err := teleport.Roles(p.Spec.Roles).Check(); err != nil {
return trace.Wrap(err)
}
return nil
}
// GetVersion returns resource version
func (p *ProvisionTokenV2) GetVersion() string {
return p.Version
}
// GetRoles returns a list of teleport roles
// that will be granted to the user of the token
// in the crendentials
func (p *ProvisionTokenV2) GetRoles() teleport.Roles {
return p.Spec.Roles
}
// SetRoles sets teleport roles
func (p *ProvisionTokenV2) SetRoles(r teleport.Roles) {
p.Spec.Roles = r
}
// GetKind returns resource kind
func (p *ProvisionTokenV2) GetKind() string {
return p.Kind
}
// GetSubKind returns resource sub kind
func (p *ProvisionTokenV2) GetSubKind() string {
return p.SubKind
}
// SetSubKind sets resource subkind
func (p *ProvisionTokenV2) SetSubKind(s string) {
p.SubKind = s
}
// GetResourceID returns resource ID
func (p *ProvisionTokenV2) GetResourceID() int64 {
return p.Metadata.ID
}
// SetResourceID sets resource ID
func (p *ProvisionTokenV2) SetResourceID(id int64) {
p.Metadata.ID = id
}
// GetMetadata returns metadata
func (p *ProvisionTokenV2) GetMetadata() Metadata {
return p.Metadata
}
// V1 returns V1 version of the resource
func (p *ProvisionTokenV2) V1() *ProvisionTokenV1 {
return &ProvisionTokenV1{
Roles: p.Spec.Roles,
Expires: p.Metadata.Expiry(),
Token: p.Metadata.Name,
}
}
// V2 returns V2 version of the resource
func (p *ProvisionTokenV2) V2() *ProvisionTokenV2 {
return p
}
// SetExpiry sets expiry time for the object
func (p *ProvisionTokenV2) SetExpiry(expires time.Time) {
p.Metadata.SetExpiry(expires)
}
// Expires returns object expiry setting
func (s *ProvisionTokenV2) Expiry() time.Time {
return s.Metadata.Expiry()
}
// SetTTL sets Expires header using realtime clock
func (p *ProvisionTokenV2) SetTTL(clock clockwork.Clock, ttl time.Duration) {
p.Metadata.SetTTL(clock, ttl)
}
// GetName returns server name
func (p *ProvisionTokenV2) GetName() string {
return p.Metadata.Name
}
// SetName sets the name of the TrustedCluster.
func (p *ProvisionTokenV2) SetName(e string) {
p.Metadata.Name = e
}
// String returns the human readable representation of a provisioning token.
func (p ProvisionTokenV2) String() string {
expires := "never"
if !p.Expiry().IsZero() {
expires = p.Expiry().String()
}
return fmt.Sprintf("ProvisionToken(Roles=%v, Expires=%v)", p.Spec.Roles, expires)
}
// V1 returns V1 version of the resource
func (p *ProvisionTokenV1) V1() *ProvisionTokenV1 {
return p
}
// V2 returns V2 version of the resource
func (p *ProvisionTokenV1) V2() *ProvisionTokenV2 {
t := &ProvisionTokenV2{
Kind: KindToken,
Version: V2,
Metadata: Metadata{
Name: p.Token,
Namespace: defaults.Namespace,
},
Spec: ProvisionTokenSpecV2{
Roles: p.Roles,
},
}
if !p.Expires.IsZero() {
t.SetExpiry(p.Expires)
}
return t
}
// String returns the human readable representation of a provisioning token.
func (p ProvisionTokenV1) String() string {
expires := "never"
if p.Expires.Unix() != 0 {
expires = p.Expires.String()
}
return fmt.Sprintf("ProvisionToken(Roles=%v, Expires=%v)",
p.Roles, expires)
}
// ProvisionTokenSpecV2Schema is a JSON schema for provision token
const ProvisionTokenSpecV2Schema = `{
"type": "object",
"additionalProperties": false,
"properties": {
"roles": {"type": "array", "items": {"type": "string"}}
}
}`
// GetProvisionTokenSchema returns provision token schema
func GetProvisionTokenSchema() string {
return fmt.Sprintf(V2SchemaTemplate, MetadataSchema, ProvisionTokenSpecV2Schema, DefaultDefinitions)
}
// UnmarshalProvisionToken unmarshals provision token from JSON or YAML,
// sets defaults and checks the schema
func UnmarshalProvisionToken(data []byte, opts ...MarshalOption) (ProvisionToken, error) {
if len(data) == 0 {
return nil, trace.BadParameter("missing provision token data")
}
cfg, err := collectOptions(opts)
if err != nil {
return nil, trace.Wrap(err)
}
var h ResourceHeader
err = utils.FastUnmarshal(data, &h)
if err != nil {
return nil, trace.Wrap(err)
}
switch h.Version {
case "":
var p ProvisionTokenV1
err := utils.FastUnmarshal(data, &p)
if err != nil {
return nil, trace.Wrap(err)
}
v2 := p.V2()
if cfg.ID != 0 {
v2.SetResourceID(cfg.ID)
}
return v2, nil
case V2:
var p ProvisionTokenV2
if cfg.SkipValidation {
if err := utils.FastUnmarshal(data, &p); err != nil {
return nil, trace.BadParameter(err.Error())
}
} else {
if err := utils.UnmarshalWithSchema(GetProvisionTokenSchema(), &p, data); err != nil {
return nil, trace.BadParameter(err.Error())
}
}
if err := p.CheckAndSetDefaults(); err != nil {
return nil, trace.Wrap(err)
}
if cfg.ID != 0 {
p.SetResourceID(cfg.ID)
}
return &p, nil
}
return nil, trace.BadParameter("server resource version %v is not supported", h.Version)
}
// MarshalProvisionToken marshals provisioning token into JSON.
func MarshalProvisionToken(t ProvisionToken, opts ...MarshalOption) ([]byte, error) {
cfg, err := collectOptions(opts)
if err != nil {
return nil, trace.Wrap(err)
}
type token1 interface {
V1() *ProvisionTokenV1
}
type token2 interface {
V2() *ProvisionTokenV2
}
version := cfg.GetVersion()
switch version {
case V1:
v, ok := t.(token1)
if !ok {
return nil, trace.BadParameter("don't know how to marshal %v", V1)
}
return utils.FastMarshal(v.V1())
case V2:
v, ok := t.(token2)
if !ok {
return nil, trace.BadParameter("don't know how to marshal %v", V2)
}
return utils.FastMarshal(v.V2())
default:
return nil, trace.BadParameter("version %v is not supported", version)
}
}