-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
provisioning.go
814 lines (734 loc) · 23.8 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
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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
/*
Copyright 2020-2022 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 (
"crypto/x509"
"encoding/pem"
"fmt"
"slices"
"strings"
"time"
"github.com/gravitational/trace"
"github.com/gravitational/teleport/api/defaults"
apiutils "github.com/gravitational/teleport/api/utils"
)
// JoinMethod is the method used for new nodes to join the cluster.
type JoinMethod string
const (
JoinMethodUnspecified JoinMethod = ""
// JoinMethodToken is the default join method, nodes join the cluster by
// presenting a secret token.
JoinMethodToken JoinMethod = "token"
// JoinMethodEC2 indicates that the node will join with the EC2 join method.
JoinMethodEC2 JoinMethod = "ec2"
// JoinMethodIAM indicates that the node will join with the IAM join method.
JoinMethodIAM JoinMethod = "iam"
// JoinMethodGitHub indicates that the node will join with the GitHub join
// method. Documentation regarding the implementation of this can be found
// in lib/githubactions
JoinMethodGitHub JoinMethod = "github"
// JoinMethodCircleCI indicates that the node will join with the CircleCI\
// join method. Documentation regarding the implementation of this can be
// found in lib/circleci
JoinMethodCircleCI JoinMethod = "circleci"
// JoinMethodKubernetes indicates that the node will join with the
// Kubernetes join method. Documentation regarding implementation can be
// found in lib/kubernetestoken
JoinMethodKubernetes JoinMethod = "kubernetes"
// JoinMethodAzure indicates that the node will join with the Azure join
// method.
JoinMethodAzure JoinMethod = "azure"
// JoinMethodGitLab indicates that the node will join with the GitLab
// join method. Documentation regarding implementation of this
// can be found in lib/gitlab
JoinMethodGitLab JoinMethod = "gitlab"
// JoinMethodGCP indicates that the node will join with the GCP join method.
// Documentation regarding implementation of this can be found in lib/gcp.
JoinMethodGCP JoinMethod = "gcp"
// JoinMethodSpacelift indicates the node will join with the SpaceLift join
// method. Documentation regarding implementation of this can be found in
// lib/spacelift.
JoinMethodSpacelift JoinMethod = "spacelift"
// JoinMethodTPM indicates that the node will join with the TPM join method.
// The core implementation of this join method can be found in lib/tpm.
JoinMethodTPM JoinMethod = "tpm"
)
var JoinMethods = []JoinMethod{
JoinMethodAzure,
JoinMethodCircleCI,
JoinMethodEC2,
JoinMethodGCP,
JoinMethodGitHub,
JoinMethodGitLab,
JoinMethodIAM,
JoinMethodKubernetes,
JoinMethodSpacelift,
JoinMethodToken,
JoinMethodTPM,
}
func ValidateJoinMethod(method JoinMethod) error {
hasJoinMethod := slices.Contains(JoinMethods, method)
if !hasJoinMethod {
return trace.BadParameter("join method must be one of %s", apiutils.JoinStrings(JoinMethods, ", "))
}
return nil
}
type KubernetesJoinType string
var (
KubernetesJoinTypeUnspecified KubernetesJoinType = ""
KubernetesJoinTypeInCluster KubernetesJoinType = "in_cluster"
KubernetesJoinTypeStaticJWKS KubernetesJoinType = "static_jwks"
)
// ProvisionToken is a provisioning token
type ProvisionToken interface {
ResourceWithOrigin
// SetMetadata sets resource metatada
SetMetadata(meta Metadata)
// GetRoles returns a list of teleport roles
// that will be granted to the user of the token
// in the crendentials
GetRoles() SystemRoles
// SetRoles sets teleport roles
SetRoles(SystemRoles)
// SetLabels sets the tokens labels
SetLabels(map[string]string)
// GetAllowRules returns the list of allow rules
GetAllowRules() []*TokenRule
// SetAllowRules sets the allow rules
SetAllowRules([]*TokenRule)
// GetAWSIIDTTL returns the TTL of EC2 IIDs
GetAWSIIDTTL() Duration
// GetJoinMethod returns joining method that must be used with this token.
GetJoinMethod() JoinMethod
// GetBotName returns the BotName field which must be set for joining bots.
GetBotName() string
// GetSuggestedLabels returns the set of labels that the resource should add when adding itself to the cluster
GetSuggestedLabels() Labels
// GetSuggestedAgentMatcherLabels returns the set of labels that should be watched when an agent/service uses this token.
// An example of this is the Database Agent.
// When using the install-database.sh script, the script will add those labels as part of the `teleport.yaml` configuration.
// They are added to `db_service.resources.0.labels`.
GetSuggestedAgentMatcherLabels() Labels
// V1 returns V1 version of the resource
V1() *ProvisionTokenV1
// String returns user friendly representation of the resource
String() string
// GetSafeName returns the name of the token, sanitized appropriately for
// join methods where the name is secret. This should be used when logging
// the token name.
GetSafeName() string
}
// NewProvisionToken returns a new provision token with the given roles.
func NewProvisionToken(token string, roles SystemRoles, expires time.Time) (ProvisionToken, error) {
return NewProvisionTokenFromSpec(token, expires, ProvisionTokenSpecV2{
Roles: roles,
})
}
// NewProvisionTokenFromSpec returns a new provision token with the given spec.
func NewProvisionTokenFromSpec(token string, expires time.Time, spec ProvisionTokenSpecV2) (ProvisionToken, error) {
t := &ProvisionTokenV2{
Metadata: Metadata{
Name: token,
Expires: &expires,
},
Spec: spec,
}
if err := t.CheckAndSetDefaults(); err != nil {
return nil, trace.Wrap(err)
}
return t, nil
}
// MustCreateProvisionToken returns a new valid provision token
// or panics, used in tests
func MustCreateProvisionToken(token string, roles SystemRoles, expires time.Time) ProvisionToken {
t, err := NewProvisionToken(token, roles, expires)
if err != nil {
panic(err)
}
return t
}
// setStaticFields sets static resource header and metadata fields.
func (p *ProvisionTokenV2) setStaticFields() {
p.Kind = KindToken
p.Version = V2
}
// CheckAndSetDefaults checks and set default values for any missing fields.
func (p *ProvisionTokenV2) CheckAndSetDefaults() error {
p.setStaticFields()
if err := p.Metadata.CheckAndSetDefaults(); err != nil {
return trace.Wrap(err)
}
if len(p.Spec.Roles) == 0 {
return trace.BadParameter("provisioning token is missing roles")
}
roles, err := NewTeleportRoles(SystemRoles(p.Spec.Roles).StringSlice())
if err != nil {
return trace.Wrap(err)
}
p.Spec.Roles = roles
if roles.Include(RoleBot) && p.Spec.BotName == "" {
return trace.BadParameter("token with role %q must set bot_name", RoleBot)
}
if p.Spec.BotName != "" && !roles.Include(RoleBot) {
return trace.BadParameter("can only set bot_name on token with role %q", RoleBot)
}
hasAllowRules := len(p.Spec.Allow) > 0
if p.Spec.JoinMethod == JoinMethodUnspecified {
// Default to the ec2 join method if any allow rules were specified,
// else default to the token method. These defaults are necessary for
// backwards compatibility.
if hasAllowRules {
p.Spec.JoinMethod = JoinMethodEC2
} else {
p.Spec.JoinMethod = JoinMethodToken
}
}
switch p.Spec.JoinMethod {
case JoinMethodToken:
if hasAllowRules {
return trace.BadParameter("allow rules are not compatible with the %q join method", JoinMethodToken)
}
case JoinMethodEC2:
if !hasAllowRules {
return trace.BadParameter("the %q join method requires defined token allow rules", JoinMethodEC2)
}
for _, allowRule := range p.Spec.Allow {
if allowRule.AWSARN != "" {
return trace.BadParameter(`the %q join method does not support the "aws_arn" parameter`, JoinMethodEC2)
}
if allowRule.AWSAccount == "" && allowRule.AWSRole == "" {
return trace.BadParameter(`allow rule for %q join method must set "aws_account" or "aws_role"`, JoinMethodEC2)
}
}
if p.Spec.AWSIIDTTL == 0 {
// default to 5 minute ttl if unspecified
p.Spec.AWSIIDTTL = Duration(5 * time.Minute)
}
case JoinMethodIAM:
if !hasAllowRules {
return trace.BadParameter("the %q join method requires defined token allow rules", JoinMethodIAM)
}
for _, allowRule := range p.Spec.Allow {
if allowRule.AWSRole != "" {
return trace.BadParameter(`the %q join method does not support the "aws_role" parameter`, JoinMethodIAM)
}
if len(allowRule.AWSRegions) != 0 {
return trace.BadParameter(`the %q join method does not support the "aws_regions" parameter`, JoinMethodIAM)
}
if allowRule.AWSAccount == "" && allowRule.AWSARN == "" {
return trace.BadParameter(`allow rule for %q join method must set "aws_account" or "aws_arn"`, JoinMethodEC2)
}
}
case JoinMethodGitHub:
providerCfg := p.Spec.GitHub
if providerCfg == nil {
return trace.BadParameter(
`"github" configuration must be provided for join method %q`,
JoinMethodGitHub,
)
}
if err := providerCfg.checkAndSetDefaults(); err != nil {
return trace.Wrap(err)
}
case JoinMethodCircleCI:
providerCfg := p.Spec.CircleCI
if providerCfg == nil {
return trace.BadParameter(
`"cirleci" configuration must be provided for join method %q`,
JoinMethodCircleCI,
)
}
if err := providerCfg.checkAndSetDefaults(); err != nil {
return trace.Wrap(err)
}
case JoinMethodKubernetes:
providerCfg := p.Spec.Kubernetes
if providerCfg == nil {
return trace.BadParameter(
`"kubernetes" configuration must be provided for the join method %q`,
JoinMethodKubernetes,
)
}
if err := providerCfg.checkAndSetDefaults(); err != nil {
return trace.Wrap(err, "spec.kubernetes:")
}
case JoinMethodAzure:
providerCfg := p.Spec.Azure
if providerCfg == nil {
return trace.BadParameter(
`"azure" configuration must be provided for the join method %q`,
JoinMethodAzure,
)
}
if err := providerCfg.checkAndSetDefaults(); err != nil {
return trace.Wrap(err)
}
case JoinMethodGitLab:
providerCfg := p.Spec.GitLab
if providerCfg == nil {
return trace.BadParameter(
`"gitlab" configuration must be provided for the join method %q`,
JoinMethodGitLab,
)
}
if err := providerCfg.checkAndSetDefaults(); err != nil {
return trace.Wrap(err)
}
case JoinMethodGCP:
providerCfg := p.Spec.GCP
if providerCfg == nil {
return trace.BadParameter(
`"gcp" configuration must be provided for the join method %q`,
JoinMethodGCP,
)
}
if err := providerCfg.checkAndSetDefaults(); err != nil {
return trace.Wrap(err)
}
case JoinMethodSpacelift:
providerCfg := p.Spec.Spacelift
if providerCfg == nil {
return trace.BadParameter(
`spec.spacelift: must be configured for the join method %q`,
JoinMethodSpacelift,
)
}
if err := providerCfg.checkAndSetDefaults(); err != nil {
return trace.Wrap(err, "spec.spacelift: failed validation")
}
case JoinMethodTPM:
providerCfg := p.Spec.TPM
if providerCfg == nil {
return trace.BadParameter(
`spec.tpm: must be configured for the join method %q`,
JoinMethodTPM,
)
}
if err := providerCfg.validate(); err != nil {
return trace.Wrap(err, "spec.tpm: failed validation")
}
default:
return trace.BadParameter("unknown join method %q", p.Spec.JoinMethod)
}
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() SystemRoles {
// Ensure that roles are case-insensitive.
return normalizedSystemRoles(SystemRoles(p.Spec.Roles).StringSlice())
}
// SetRoles sets teleport roles
func (p *ProvisionTokenV2) SetRoles(r SystemRoles) {
p.Spec.Roles = r
}
func (p *ProvisionTokenV2) SetLabels(l map[string]string) {
p.Metadata.Labels = l
}
// GetAllowRules returns the list of allow rules
func (p *ProvisionTokenV2) GetAllowRules() []*TokenRule {
return p.Spec.Allow
}
// SetAllowRules sets the allow rules.
func (p *ProvisionTokenV2) SetAllowRules(rules []*TokenRule) {
p.Spec.Allow = rules
}
// GetAWSIIDTTL returns the TTL of EC2 IIDs
func (p *ProvisionTokenV2) GetAWSIIDTTL() Duration {
return p.Spec.AWSIIDTTL
}
// GetJoinMethod returns joining method that must be used with this token.
func (p *ProvisionTokenV2) GetJoinMethod() JoinMethod {
return p.Spec.JoinMethod
}
// GetBotName returns the BotName field which must be set for joining bots.
func (p *ProvisionTokenV2) GetBotName() string {
return p.Spec.BotName
}
// 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
}
// GetRevision returns the revision
func (p *ProvisionTokenV2) GetRevision() string {
return p.Metadata.GetRevision()
}
// SetRevision sets the revision
func (p *ProvisionTokenV2) SetRevision(rev string) {
p.Metadata.SetRevision(rev)
}
// GetMetadata returns metadata
func (p *ProvisionTokenV2) GetMetadata() Metadata {
return p.Metadata
}
// SetMetadata sets resource metatada
func (p *ProvisionTokenV2) SetMetadata(meta Metadata) {
p.Metadata = meta
}
// Origin returns the origin value of the resource.
func (p *ProvisionTokenV2) Origin() string {
return p.Metadata.Origin()
}
// SetOrigin sets the origin value of the resource.
func (p *ProvisionTokenV2) SetOrigin(origin string) {
p.Metadata.SetOrigin(origin)
}
// GetSuggestedLabels returns the labels the resource should set when using this token
func (p *ProvisionTokenV2) GetSuggestedLabels() Labels {
return p.Spec.SuggestedLabels
}
// GetAgentMatcherLabels returns the set of labels that should be watched when an agent/service uses this token.
// An example of this is the Database Agent.
// When using the install-database.sh script, the script will add those labels as part of the `teleport.yaml` configuration.
// They are added to `db_service.resources.0.labels`.
func (p *ProvisionTokenV2) GetSuggestedAgentMatcherLabels() Labels {
return p.Spec.SuggestedAgentMatcherLabels
}
// 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)
}
// Expiry returns object expiry setting
func (p *ProvisionTokenV2) Expiry() time.Time {
return p.Metadata.Expiry()
}
// GetName returns the name of the provision token. This value can be secret!
// Use GetSafeName where the name may be logged.
func (p *ProvisionTokenV2) GetName() string {
return p.Metadata.Name
}
// SetName sets the name of the provision token.
func (p *ProvisionTokenV2) SetName(e string) {
p.Metadata.Name = e
}
// GetSafeName returns the name of the token, sanitized appropriately for
// join methods where the name is secret. This should be used when logging
// the token name.
func (p *ProvisionTokenV2) GetSafeName() string {
name := p.GetName()
if p.GetJoinMethod() != JoinMethodToken {
return name
}
// If the token name is short, we just blank the whole thing.
if len(name) < 16 {
return strings.Repeat("*", len(name))
}
// If the token name is longer, we can show the last 25% of it to help
// the operator identify it.
hiddenBefore := int(0.75 * float64(len(name)))
name = name[hiddenBefore:]
name = strings.Repeat("*", hiddenBefore) + name
return name
}
// 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)
}
// 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
}
// 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)
}
t.CheckAndSetDefaults()
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)
}
func (a *ProvisionTokenSpecV2GitHub) checkAndSetDefaults() error {
if len(a.Allow) == 0 {
return trace.BadParameter("the %q join method requires at least one token allow rule", JoinMethodGitHub)
}
for _, rule := range a.Allow {
repoSet := rule.Repository != ""
ownerSet := rule.RepositoryOwner != ""
subSet := rule.Sub != ""
if !(subSet || ownerSet || repoSet) {
return trace.BadParameter(
`allow rule for %q must include at least one of "repository", "repository_owner" or "sub"`,
JoinMethodGitHub,
)
}
}
if strings.Contains(a.EnterpriseServerHost, "/") {
return trace.BadParameter("'spec.github.enterprise_server_host' should not contain the scheme or path")
}
if a.EnterpriseServerHost != "" && a.EnterpriseSlug != "" {
return trace.BadParameter("'spec.github.enterprise_server_host' and `spec.github.enterprise_slug` cannot both be set")
}
return nil
}
func (a *ProvisionTokenSpecV2CircleCI) checkAndSetDefaults() error {
if len(a.Allow) == 0 {
return trace.BadParameter("the %q join method requires at least one token allow rule", JoinMethodCircleCI)
}
if a.OrganizationID == "" {
return trace.BadParameter("the %q join method requires 'organization_id' to be set", JoinMethodCircleCI)
}
for _, rule := range a.Allow {
projectSet := rule.ProjectID != ""
contextSet := rule.ContextID != ""
if !projectSet && !contextSet {
return trace.BadParameter(
`allow rule for %q must include at least "project_id" or "context_id"`,
JoinMethodCircleCI,
)
}
}
return nil
}
func (a *ProvisionTokenSpecV2Kubernetes) checkAndSetDefaults() error {
if len(a.Allow) == 0 {
return trace.BadParameter("allow: at least one rule must be set")
}
for i, allowRule := range a.Allow {
if allowRule.ServiceAccount == "" {
return trace.BadParameter(
"allow[%d].service_account: name of service account must be set",
i,
)
}
if len(strings.Split(allowRule.ServiceAccount, ":")) != 2 {
return trace.BadParameter(
`allow[%d].service_account: name of service account should be in format "namespace:service_account", got %q instead`,
i,
allowRule.ServiceAccount,
)
}
}
if a.Type == KubernetesJoinTypeUnspecified {
// For compatibility with older resources which did not have a Type
// field we default to "in_cluster".
a.Type = KubernetesJoinTypeInCluster
}
switch a.Type {
case KubernetesJoinTypeInCluster:
if a.StaticJWKS != nil {
return trace.BadParameter("static_jwks: must not be set when type is %q", KubernetesJoinTypeInCluster)
}
case KubernetesJoinTypeStaticJWKS:
if a.StaticJWKS == nil {
return trace.BadParameter("static_jwks: must be set when type is %q", KubernetesJoinTypeStaticJWKS)
}
if a.StaticJWKS.JWKS == "" {
return trace.BadParameter("static_jwks.jwks: must be set when type is %q", KubernetesJoinTypeStaticJWKS)
}
default:
return trace.BadParameter(
"type: must be one of (%s), got %q",
apiutils.JoinStrings(JoinMethods, ", "),
a.Type,
)
}
return nil
}
func (a *ProvisionTokenSpecV2Azure) checkAndSetDefaults() error {
if len(a.Allow) == 0 {
return trace.BadParameter(
"the %q join method requires defined azure allow rules",
JoinMethodAzure,
)
}
for _, allowRule := range a.Allow {
if allowRule.Subscription == "" {
return trace.BadParameter(
"the %q join method requires azure allow rules with non-empty subscription",
JoinMethodAzure,
)
}
}
return nil
}
const defaultGitLabDomain = "gitlab.com"
func (a *ProvisionTokenSpecV2GitLab) checkAndSetDefaults() error {
if len(a.Allow) == 0 {
return trace.BadParameter(
"the %q join method requires defined gitlab allow rules",
JoinMethodGitLab,
)
}
for _, allowRule := range a.Allow {
if allowRule.Sub == "" && allowRule.NamespacePath == "" && allowRule.ProjectPath == "" && allowRule.CIConfigRefURI == "" {
return trace.BadParameter(
"the %q join method requires allow rules with at least one of ['sub', 'project_path', 'namespace_path', 'ci_config_ref_uri'] to ensure security.",
JoinMethodGitLab,
)
}
}
if a.Domain == "" {
a.Domain = defaultGitLabDomain
} else {
if strings.Contains(a.Domain, "/") {
return trace.BadParameter(
"'spec.gitlab.domain' should not contain the scheme or path",
)
}
}
return nil
}
func (a *ProvisionTokenSpecV2GCP) checkAndSetDefaults() error {
if len(a.Allow) == 0 {
return trace.BadParameter("the %q join method requires at least one token allow rule", JoinMethodGCP)
}
for _, allowRule := range a.Allow {
if len(allowRule.ProjectIDs) == 0 {
return trace.BadParameter(
"the %q join method requires gcp allow rules with at least one project ID",
JoinMethodGCP,
)
}
}
return nil
}
func (a *ProvisionTokenSpecV2Spacelift) checkAndSetDefaults() error {
if a.Hostname == "" {
return trace.BadParameter(
"hostname: should be set to the hostname of the spacelift tenant",
)
}
if strings.Contains(a.Hostname, "/") {
return trace.BadParameter(
"hostname: should not contain the scheme or path",
)
}
if len(a.Allow) == 0 {
return trace.BadParameter("allow: at least one rule must be set")
}
for i, allowRule := range a.Allow {
if allowRule.SpaceID == "" && allowRule.CallerID == "" {
return trace.BadParameter(
"allow[%d]: at least one of ['space_id', 'caller_id'] must be set",
i,
)
}
}
return nil
}
func (a *ProvisionTokenSpecV2TPM) validate() error {
for i, caData := range a.EKCertAllowedCAs {
p, _ := pem.Decode([]byte(caData))
if p == nil {
return trace.BadParameter(
"ekcert_allowed_cas[%d]: no pem block found",
i,
)
}
if p.Type != "CERTIFICATE" {
return trace.BadParameter(
"ekcert_allowed_cas[%d]: pem block is not 'CERTIFICATE' type",
i,
)
}
if _, err := x509.ParseCertificate(p.Bytes); err != nil {
return trace.Wrap(
err,
"ekcert_allowed_cas[%d]: parsing certificate",
i,
)
}
}
if len(a.Allow) == 0 {
return trace.BadParameter(
"allow: at least one rule must be set",
)
}
for i, allowRule := range a.Allow {
if len(allowRule.EKPublicHash) == 0 && len(allowRule.EKCertificateSerial) == 0 {
return trace.BadParameter(
"allow[%d]: at least one of ['ek_public_hash', 'ek_certificate_serial'] must be set",
i,
)
}
}
return nil
}