-
Notifications
You must be signed in to change notification settings - Fork 0
/
statictokens.go
290 lines (246 loc) · 7.71 KB
/
statictokens.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
/*
Copyright 2017-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 (
"fmt"
"time"
"github.com/gravitational/teleport/lib/defaults"
"github.com/gravitational/teleport/lib/utils"
"github.com/gravitational/trace"
"github.com/jonboulle/clockwork"
)
// StaticTokens define a list of static []ProvisionToken used to provision a
// node. StaticTokens is a configuration resource, never create more than one instance
// of it.
type StaticTokens interface {
// Resource provides common resource properties.
Resource
// SetStaticTokens sets the list of static tokens used to provision nodes.
SetStaticTokens([]ProvisionToken)
// GetStaticTokens gets the list of static tokens used to provision nodes.
GetStaticTokens() []ProvisionToken
// CheckAndSetDefaults checks and set default values for missing fields.
CheckAndSetDefaults() error
}
// NewStaticTokens is a convenience wrapper to create a StaticTokens resource.
func NewStaticTokens(spec StaticTokensSpecV2) (StaticTokens, error) {
st := StaticTokensV2{
Kind: KindStaticTokens,
Version: V2,
Metadata: Metadata{
Name: MetaNameStaticTokens,
Namespace: defaults.Namespace,
},
Spec: spec,
}
if err := st.CheckAndSetDefaults(); err != nil {
return nil, trace.Wrap(err)
}
return &st, nil
}
// DefaultStaticTokens is used to get the default static tokens (empty list)
// when nothing is specified in file configuration.
func DefaultStaticTokens() StaticTokens {
return &StaticTokensV2{
Kind: KindStaticTokens,
Version: V2,
Metadata: Metadata{
Name: MetaNameStaticTokens,
Namespace: defaults.Namespace,
},
Spec: StaticTokensSpecV2{
StaticTokens: []ProvisionTokenV1{},
},
}
}
// GetVersion returns resource version
func (c *StaticTokensV2) GetVersion() string {
return c.Version
}
// GetKind returns resource kind
func (c *StaticTokensV2) GetKind() string {
return c.Kind
}
// GetSubKind returns resource sub kind
func (c *StaticTokensV2) GetSubKind() string {
return c.SubKind
}
// SetSubKind sets resource subkind
func (c *StaticTokensV2) SetSubKind(sk string) {
c.SubKind = sk
}
// GetResourceID returns resource ID
func (c *StaticTokensV2) GetResourceID() int64 {
return c.Metadata.ID
}
// SetResourceID sets resource ID
func (c *StaticTokensV2) SetResourceID(id int64) {
c.Metadata.ID = id
}
// GetName returns the name of the StaticTokens resource.
func (c *StaticTokensV2) GetName() string {
return c.Metadata.Name
}
// SetName sets the name of the StaticTokens resource.
func (c *StaticTokensV2) SetName(e string) {
c.Metadata.Name = e
}
// Expires returns object expiry setting
func (c *StaticTokensV2) Expiry() time.Time {
return c.Metadata.Expiry()
}
// SetExpiry sets expiry time for the object
func (c *StaticTokensV2) SetExpiry(expires time.Time) {
c.Metadata.SetExpiry(expires)
}
// SetTTL sets Expires header using realtime clock
func (c *StaticTokensV2) SetTTL(clock clockwork.Clock, ttl time.Duration) {
c.Metadata.SetTTL(clock, ttl)
}
// GetMetadata returns object metadata
func (c *StaticTokensV2) GetMetadata() Metadata {
return c.Metadata
}
// SetStaticTokens sets the list of static tokens used to provision nodes.
func (c *StaticTokensV2) SetStaticTokens(s []ProvisionToken) {
c.Spec.StaticTokens = ProvisionTokensToV1(s)
}
// GetStaticTokens gets the list of static tokens used to provision nodes.
func (c *StaticTokensV2) GetStaticTokens() []ProvisionToken {
return ProvisionTokensFromV1(c.Spec.StaticTokens)
}
// CheckAndSetDefaults checks validity of all parameters and sets defaults.
func (c *StaticTokensV2) CheckAndSetDefaults() error {
// make sure we have defaults for all metadata fields
err := c.Metadata.CheckAndSetDefaults()
if err != nil {
return trace.Wrap(err)
}
return nil
}
// String represents a human readable version of static provisioning tokens.
func (c *StaticTokensV2) String() string {
return fmt.Sprintf("StaticTokens(%v)", c.Spec.StaticTokens)
}
// StaticTokensSpecSchemaTemplate is a template for StaticTokens schema.
const StaticTokensSpecSchemaTemplate = `{
"type": "object",
"additionalProperties": false,
"properties": {
"static_tokens": {
"type": "array",
"items": {
"type": "object",
"additionalProperties": false,
"properties": {
"expires": {
"type": "string"
},
"roles": {
"type": "array",
"items": {
"type": "string"
}
},
"token": {
"type": "string"
}
}
}
}%v
}
}`
// GetStaticTokensSchema returns the schema with optionally injected
// schema for extensions.
func GetStaticTokensSchema(extensionSchema string) string {
var staticTokensSchema string
if staticTokensSchema == "" {
staticTokensSchema = fmt.Sprintf(StaticTokensSpecSchemaTemplate, "")
} else {
staticTokensSchema = fmt.Sprintf(StaticTokensSpecSchemaTemplate, ","+extensionSchema)
}
return fmt.Sprintf(V2SchemaTemplate, MetadataSchema, staticTokensSchema, DefaultDefinitions)
}
// StaticTokensMarshaler implements marshal/unmarshal of StaticTokens implementations
// mostly adds support for extended versions.
type StaticTokensMarshaler interface {
Marshal(c StaticTokens, opts ...MarshalOption) ([]byte, error)
Unmarshal(bytes []byte, opts ...MarshalOption) (StaticTokens, error)
}
var staticTokensMarshaler StaticTokensMarshaler = &TeleportStaticTokensMarshaler{}
// SetStaticTokensMarshaler sets the marshaler.
func SetStaticTokensMarshaler(m StaticTokensMarshaler) {
marshalerMutex.Lock()
defer marshalerMutex.Unlock()
staticTokensMarshaler = m
}
// GetStaticTokensMarshaler gets the marshaler.
func GetStaticTokensMarshaler() StaticTokensMarshaler {
marshalerMutex.Lock()
defer marshalerMutex.Unlock()
return staticTokensMarshaler
}
// TeleportStaticTokensMarshaler is used to marshal and unmarshal StaticTokens.
type TeleportStaticTokensMarshaler struct{}
// Unmarshal unmarshals StaticTokens from JSON.
func (t *TeleportStaticTokensMarshaler) Unmarshal(bytes []byte, opts ...MarshalOption) (StaticTokens, error) {
var staticTokens StaticTokensV2
if len(bytes) == 0 {
return nil, trace.BadParameter("missing resource data")
}
cfg, err := collectOptions(opts)
if err != nil {
return nil, trace.Wrap(err)
}
if cfg.SkipValidation {
if err := utils.FastUnmarshal(bytes, &staticTokens); err != nil {
return nil, trace.BadParameter(err.Error())
}
} else {
err = utils.UnmarshalWithSchema(GetStaticTokensSchema(""), &staticTokens, bytes)
if err != nil {
return nil, trace.BadParameter(err.Error())
}
}
err = staticTokens.CheckAndSetDefaults()
if err != nil {
return nil, trace.Wrap(err)
}
if cfg.ID != 0 {
staticTokens.SetResourceID(cfg.ID)
}
if !cfg.Expires.IsZero() {
staticTokens.SetExpiry(cfg.Expires)
}
return &staticTokens, nil
}
// Marshal marshals StaticTokens to JSON.
func (t *TeleportStaticTokensMarshaler) Marshal(c StaticTokens, opts ...MarshalOption) ([]byte, error) {
cfg, err := collectOptions(opts)
if err != nil {
return nil, trace.Wrap(err)
}
switch resource := c.(type) {
case *StaticTokensV2:
if !cfg.PreserveResourceID {
// avoid modifying the original object
// to prevent unexpected data races
copy := *resource
copy.SetResourceID(0)
resource = ©
}
return utils.FastMarshal(resource)
default:
return nil, trace.BadParameter("unrecognized resource version %T", c)
}
}