/
join_ec2_test.go
752 lines (712 loc) · 23.1 KB
/
join_ec2_test.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
/*
Copyright 2021 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 auth
import (
"context"
"crypto/x509"
"encoding/pem"
"testing"
"time"
"github.com/gravitational/teleport/api/defaults"
"github.com/gravitational/teleport/api/types"
"github.com/gravitational/trace"
"github.com/aws/aws-sdk-go-v2/service/ec2"
ec2types "github.com/aws/aws-sdk-go-v2/service/ec2/types"
"github.com/jonboulle/clockwork"
"github.com/stretchr/testify/require"
)
type ec2Instance struct {
iid []byte
account string
region string
instanceID string
pendingTime time.Time
}
var (
instance1 = ec2Instance{
iid: []byte(`MIAGCSqGSIb3DQEHAqCAMIACAQExDzANBglghkgBZQMEAgEFADCABgkqhkiG9w0BBwGggCSABIIB
23sKICAiYWNjb3VudElkIiA6ICIyNzg1NzYyMjA0NTMiLAogICJhcmNoaXRlY3R1cmUiIDogIng4
Nl82NCIsCiAgImF2YWlsYWJpbGl0eVpvbmUiIDogInVzLXdlc3QtMmEiLAogICJiaWxsaW5nUHJv
ZHVjdHMiIDogbnVsbCwKICAiZGV2cGF5UHJvZHVjdENvZGVzIiA6IG51bGwsCiAgIm1hcmtldHBs
YWNlUHJvZHVjdENvZGVzIiA6IG51bGwsCiAgImltYWdlSWQiIDogImFtaS0wZmE5ZTFmNjQxNDJj
ZGUxNyIsCiAgImluc3RhbmNlSWQiIDogImktMDc4NTE3Y2E4YTcwYTFkZGUiLAogICJpbnN0YW5j
ZVR5cGUiIDogInQyLm1lZGl1bSIsCiAgImtlcm5lbElkIiA6IG51bGwsCiAgInBlbmRpbmdUaW1l
IiA6ICIyMDIxLTA5LTAzVDIxOjI1OjQ0WiIsCiAgInByaXZhdGVJcCIgOiAiMTAuMC4wLjIwOSIs
CiAgInJhbWRpc2tJZCIgOiBudWxsLAogICJyZWdpb24iIDogInVzLXdlc3QtMiIsCiAgInZlcnNp
b24iIDogIjIwMTctMDktMzAiCn0AAAAAAAAxggIvMIICKwIBATBpMFwxCzAJBgNVBAYTAlVTMRkw
FwYDVQQIExBXYXNoaW5ndG9uIFN0YXRlMRAwDgYDVQQHEwdTZWF0dGxlMSAwHgYDVQQKExdBbWF6
b24gV2ViIFNlcnZpY2VzIExMQwIJALZL3lrQCSTMMA0GCWCGSAFlAwQCAQUAoIGYMBgGCSqGSIb3
DQEJAzELBgkqhkiG9w0BBwEwHAYJKoZIhvcNAQkFMQ8XDTIxMDkwMzIxMjU0N1owLQYJKoZIhvcN
AQk0MSAwHjANBglghkgBZQMEAgEFAKENBgkqhkiG9w0BAQsFADAvBgkqhkiG9w0BCQQxIgQgCH2d
JiKmdx9uhxlm8ObWAvFOhqJb7k79+DW/T3ezwVUwDQYJKoZIhvcNAQELBQAEggEANWautigs/qZ6
w8g5/EfWsAFj8kHgUD+xqsQ1HDrBUx3IQ498NMBZ78379B8RBfuzeVjbaf+yugov0fYrDbGvSRRw
myy49TfZ9gdlpWQXzwSg3OPMDNToRoKw00/LQjSxcTCaPP4vMDEIjYMUqZ3i4uWYJJJ0Lb7fDMDk
Anu7yHolVfbnvIAuZe8lGpc7ofCSBG5wulm+/pqzO25YPMH1cLEvOadE+3N2GxK6gRTLJoE98rsm
LDp6OuU/b2QfaxU0ec6OogdtSJto/URI0/ygHmNAzBis470A29yh5nVwm6AkY4krjPsK7uiBIRhs
lr5x0X6+ggQfF2BKAJ/BRcAHNgAAAAAAAA==`),
account: "278576220453",
region: "us-west-2",
instanceID: "i-078517ca8a70a1dde",
pendingTime: time.Date(2021, time.September, 3, 21, 25, 44, 0, time.UTC),
}
instance2 = ec2Instance{
iid: []byte(`MIAGCSqGSIb3DQEHAqCAMIACAQExDzANBglghkgBZQMEAgEFADCABgkqhkiG9w0BBwGggCSABIIB
3XsKICAiYWNjb3VudElkIiA6ICI4ODM0NzQ2NjI4ODgiLAogICJhcmNoaXRlY3R1cmUiIDogIng4
Nl82NCIsCiAgImF2YWlsYWJpbGl0eVpvbmUiIDogInVzLXdlc3QtMWMiLAogICJiaWxsaW5nUHJv
ZHVjdHMiIDogbnVsbCwKICAiZGV2cGF5UHJvZHVjdENvZGVzIiA6IG51bGwsCiAgIm1hcmtldHBs
YWNlUHJvZHVjdENvZGVzIiA6IG51bGwsCiAgImltYWdlSWQiIDogImFtaS0wY2UzYzU1YTMxZDI5
MDQwZSIsCiAgImluc3RhbmNlSWQiIDogImktMDFiOTQwYzQ1ZmQxMWZlNzQiLAogICJpbnN0YW5j
ZVR5cGUiIDogInQyLm1pY3JvIiwKICAia2VybmVsSWQiIDogbnVsbCwKICAicGVuZGluZ1RpbWUi
IDogIjIwMjEtMDktMTFUMDA6MTQ6MThaIiwKICAicHJpdmF0ZUlwIiA6ICIxNzIuMzEuMTIuMjUx
IiwKICAicmFtZGlza0lkIiA6IG51bGwsCiAgInJlZ2lvbiIgOiAidXMtd2VzdC0xIiwKICAidmVy
c2lvbiIgOiAiMjAxNy0wOS0zMCIKfQAAAAAAADGCAi8wggIrAgEBMGkwXDELMAkGA1UEBhMCVVMx
GTAXBgNVBAgTEFdhc2hpbmd0b24gU3RhdGUxEDAOBgNVBAcTB1NlYXR0bGUxIDAeBgNVBAoTF0Ft
YXpvbiBXZWIgU2VydmljZXMgTExDAgkA00+QilzIS0gwDQYJYIZIAWUDBAIBBQCggZgwGAYJKoZI
hvcNAQkDMQsGCSqGSIb3DQEHATAcBgkqhkiG9w0BCQUxDxcNMjEwOTExMDAxNDIyWjAtBgkqhkiG
9w0BCTQxIDAeMA0GCWCGSAFlAwQCAQUAoQ0GCSqGSIb3DQEBCwUAMC8GCSqGSIb3DQEJBDEiBCDS
1gNvxbYnEL6plVu8X/QmKPJFJwIJfi+2hIVjyKAOtjANBgkqhkiG9w0BAQsFAASCAQABKmghATg8
VXkdiIGcTIPfKrc2v/zEIdLUAi+Ew5lrGUVjnNqrP9irGK4d9sVtcu/8UKp9RDoeJOQ6I/pRcwvT
PJVHlhGnLyybr5ZVqkxiC09GASNnPe12dzCKkKD2rvW6mGR91cxpM94Xqi5UA/ZRqiXwpHo3LECN
Gu38Hpdv6sBgD/av2Ohd+vEH2zvYVkp7ZfnFuDLWRSBQZgmKwVKVdOjrMmP32vb3vzhMBuOj+jbQ
RwEmYIkRaEGNbrZgatjMJYmTWuLG26zws3avOK6kL6u38DV3wJPVB/G0Ira5MvC/ojGya+DrVngW
VUP+3jgenPrd7OyCWPSwOoOBMhSlAAAAAAAA`),
account: "883474662888",
region: "us-west-1",
instanceID: "i-01b940c45fd11fe74",
pendingTime: time.Date(2021, time.September, 11, 0, 14, 18, 0, time.UTC),
}
)
type ec2ClientNoInstance struct{}
type ec2ClientNotRunning struct{}
type ec2ClientRunning struct{}
func (c ec2ClientNoInstance) DescribeInstances(ctx context.Context, params *ec2.DescribeInstancesInput, optFns ...func(*ec2.Options)) (*ec2.DescribeInstancesOutput, error) {
return &ec2.DescribeInstancesOutput{}, nil
}
func (c ec2ClientNotRunning) DescribeInstances(ctx context.Context, params *ec2.DescribeInstancesInput, optFns ...func(*ec2.Options)) (*ec2.DescribeInstancesOutput, error) {
return &ec2.DescribeInstancesOutput{
Reservations: []ec2types.Reservation{
{
Instances: []ec2types.Instance{
{
InstanceId: ¶ms.InstanceIds[0],
State: &ec2types.InstanceState{
Name: ec2types.InstanceStateNameTerminated,
},
},
},
},
},
}, nil
}
func (c ec2ClientRunning) DescribeInstances(ctx context.Context, params *ec2.DescribeInstancesInput, optFns ...func(*ec2.Options)) (*ec2.DescribeInstancesOutput, error) {
return &ec2.DescribeInstancesOutput{
Reservations: []ec2types.Reservation{
{
Instances: []ec2types.Instance{
{
InstanceId: ¶ms.InstanceIds[0],
State: &ec2types.InstanceState{
Name: ec2types.InstanceStateNameRunning,
},
},
},
},
},
}, nil
}
func TestAuth_RegisterUsingToken_EC2(t *testing.T) {
ctx := context.Background()
p, err := newTestPack(ctx, t.TempDir())
require.NoError(t, err)
a := p.a
// upsert a node to test duplicates
node := &types.ServerV2{
Kind: types.KindNode,
Version: types.V2,
Metadata: types.Metadata{
Name: instance2.account + "-" + instance2.instanceID,
Namespace: defaults.Namespace,
},
}
_, err = a.UpsertNode(ctx, node)
require.NoError(t, err)
sshPrivateKey, sshPublicKey, err := a.GenerateKeyPair("")
require.NoError(t, err)
tlsPublicKey, err := PrivateKeyToPublicKeyTLS(sshPrivateKey)
require.NoError(t, err)
isNil := func(err error) bool {
return err == nil
}
testCases := []struct {
desc string
tokenSpec types.ProvisionTokenSpecV2
ec2Client ec2Client
request types.RegisterUsingTokenRequest
expectError func(error) bool
clock clockwork.Clock
}{
{
desc: "basic passing case",
tokenSpec: types.ProvisionTokenSpecV2{
Roles: []types.SystemRole{types.RoleNode},
Allow: []*types.TokenRule{
&types.TokenRule{
AWSAccount: instance1.account,
AWSRegions: []string{instance1.region},
},
},
},
ec2Client: ec2ClientRunning{},
request: types.RegisterUsingTokenRequest{
Token: "test_token",
NodeName: "node_name",
Role: types.RoleNode,
HostID: instance1.account + "-" + instance1.instanceID,
EC2IdentityDocument: instance1.iid,
},
expectError: isNil,
clock: clockwork.NewFakeClockAt(instance1.pendingTime),
},
{
desc: "pass with multiple rules",
tokenSpec: types.ProvisionTokenSpecV2{
Roles: []types.SystemRole{types.RoleNode},
Allow: []*types.TokenRule{
&types.TokenRule{
AWSAccount: instance2.account,
AWSRegions: []string{instance2.region},
},
&types.TokenRule{
AWSAccount: instance1.account,
AWSRegions: []string{instance1.region},
},
},
},
ec2Client: ec2ClientRunning{},
request: types.RegisterUsingTokenRequest{
Token: "test_token",
NodeName: "node_name",
Role: types.RoleNode,
HostID: instance1.account + "-" + instance1.instanceID,
EC2IdentityDocument: instance1.iid,
},
expectError: isNil,
clock: clockwork.NewFakeClockAt(instance1.pendingTime),
},
{
desc: "pass with multiple regions",
tokenSpec: types.ProvisionTokenSpecV2{
Roles: []types.SystemRole{types.RoleNode},
Allow: []*types.TokenRule{
&types.TokenRule{
AWSAccount: instance1.account,
AWSRegions: []string{"us-east-1", instance1.region, "us-east-2"},
},
},
},
ec2Client: ec2ClientRunning{},
request: types.RegisterUsingTokenRequest{
Token: "test_token",
NodeName: "node_name",
Role: types.RoleNode,
HostID: instance1.account + "-" + instance1.instanceID,
EC2IdentityDocument: instance1.iid,
},
expectError: isNil,
clock: clockwork.NewFakeClockAt(instance1.pendingTime),
},
{
desc: "pass with no regions",
tokenSpec: types.ProvisionTokenSpecV2{
Roles: []types.SystemRole{types.RoleNode},
Allow: []*types.TokenRule{
&types.TokenRule{
AWSAccount: instance1.account,
},
},
},
ec2Client: ec2ClientRunning{},
request: types.RegisterUsingTokenRequest{
Token: "test_token",
NodeName: "node_name",
Role: types.RoleNode,
HostID: instance1.account + "-" + instance1.instanceID,
EC2IdentityDocument: instance1.iid,
},
expectError: isNil,
clock: clockwork.NewFakeClockAt(instance1.pendingTime),
},
{
desc: "wrong account",
tokenSpec: types.ProvisionTokenSpecV2{
Roles: []types.SystemRole{types.RoleNode},
Allow: []*types.TokenRule{
&types.TokenRule{
AWSAccount: "bad account",
AWSRegions: []string{instance1.region},
},
},
},
ec2Client: ec2ClientRunning{},
request: types.RegisterUsingTokenRequest{
Token: "test_token",
NodeName: "node_name",
Role: types.RoleNode,
HostID: instance1.account + "-" + instance1.instanceID,
EC2IdentityDocument: instance1.iid,
},
expectError: trace.IsAccessDenied,
clock: clockwork.NewFakeClockAt(instance1.pendingTime),
},
{
desc: "wrong region",
tokenSpec: types.ProvisionTokenSpecV2{
Roles: []types.SystemRole{types.RoleNode},
Allow: []*types.TokenRule{
&types.TokenRule{
AWSAccount: instance1.account,
AWSRegions: []string{"bad region"},
},
},
},
ec2Client: ec2ClientRunning{},
request: types.RegisterUsingTokenRequest{
Token: "test_token",
NodeName: "node_name",
Role: types.RoleNode,
HostID: instance1.account + "-" + instance1.instanceID,
EC2IdentityDocument: instance1.iid,
},
expectError: trace.IsAccessDenied,
clock: clockwork.NewFakeClockAt(instance1.pendingTime),
},
{
desc: "bad HostID",
tokenSpec: types.ProvisionTokenSpecV2{
Roles: []types.SystemRole{types.RoleNode},
Allow: []*types.TokenRule{
&types.TokenRule{
AWSAccount: instance1.account,
AWSRegions: []string{instance1.region},
},
},
},
ec2Client: ec2ClientRunning{},
request: types.RegisterUsingTokenRequest{
Token: "test_token",
NodeName: "node_name",
Role: types.RoleNode,
HostID: "bad host id",
EC2IdentityDocument: instance1.iid,
},
expectError: trace.IsAccessDenied,
clock: clockwork.NewFakeClockAt(instance1.pendingTime),
},
{
desc: "no identity document",
tokenSpec: types.ProvisionTokenSpecV2{
Roles: []types.SystemRole{types.RoleNode},
Allow: []*types.TokenRule{
&types.TokenRule{
AWSAccount: instance1.account,
AWSRegions: []string{instance1.region},
},
},
},
ec2Client: ec2ClientRunning{},
request: types.RegisterUsingTokenRequest{
Token: "test_token",
NodeName: "node_name",
Role: types.RoleNode,
HostID: instance1.account + "-" + instance1.instanceID,
},
expectError: trace.IsAccessDenied,
clock: clockwork.NewFakeClockAt(instance1.pendingTime),
},
{
desc: "bad identity document",
tokenSpec: types.ProvisionTokenSpecV2{
Roles: []types.SystemRole{types.RoleNode},
Allow: []*types.TokenRule{
&types.TokenRule{
AWSAccount: instance1.account,
AWSRegions: []string{instance1.region},
},
},
},
ec2Client: ec2ClientRunning{},
request: types.RegisterUsingTokenRequest{
Token: "test_token",
NodeName: "node_name",
Role: types.RoleNode,
HostID: instance1.account + "-" + instance1.instanceID,
EC2IdentityDocument: []byte("bad document"),
},
expectError: trace.IsAccessDenied,
clock: clockwork.NewFakeClockAt(instance1.pendingTime),
},
{
desc: "instance already joined",
tokenSpec: types.ProvisionTokenSpecV2{
Roles: []types.SystemRole{types.RoleNode},
Allow: []*types.TokenRule{
&types.TokenRule{
AWSAccount: instance2.account,
AWSRegions: []string{instance2.region},
},
},
},
ec2Client: ec2ClientRunning{},
request: types.RegisterUsingTokenRequest{
Token: "test_token",
NodeName: "node_name",
Role: types.RoleNode,
HostID: instance2.account + "-" + instance2.instanceID,
EC2IdentityDocument: instance2.iid,
},
expectError: trace.IsAccessDenied,
clock: clockwork.NewFakeClockAt(instance2.pendingTime),
},
{
desc: "instance already joined, fake ID",
tokenSpec: types.ProvisionTokenSpecV2{
Roles: []types.SystemRole{types.RoleNode},
Allow: []*types.TokenRule{
&types.TokenRule{
AWSAccount: instance2.account,
AWSRegions: []string{instance2.region},
},
},
},
ec2Client: ec2ClientRunning{},
request: types.RegisterUsingTokenRequest{
Token: "test_token",
NodeName: "node_name",
Role: types.RoleNode,
HostID: "fake id",
EC2IdentityDocument: instance2.iid,
},
expectError: trace.IsAccessDenied,
clock: clockwork.NewFakeClockAt(instance2.pendingTime),
},
{
desc: "instance not running",
tokenSpec: types.ProvisionTokenSpecV2{
Roles: []types.SystemRole{types.RoleNode},
Allow: []*types.TokenRule{
&types.TokenRule{
AWSAccount: instance1.account,
AWSRegions: []string{instance1.region},
},
},
},
ec2Client: ec2ClientNotRunning{},
request: types.RegisterUsingTokenRequest{
Token: "test_token",
NodeName: "node_name",
Role: types.RoleNode,
HostID: instance1.account + "-" + instance1.instanceID,
EC2IdentityDocument: instance1.iid,
},
expectError: trace.IsAccessDenied,
clock: clockwork.NewFakeClockAt(instance1.pendingTime),
},
{
desc: "instance not exists",
tokenSpec: types.ProvisionTokenSpecV2{
Roles: []types.SystemRole{types.RoleNode},
Allow: []*types.TokenRule{
&types.TokenRule{
AWSAccount: instance1.account,
AWSRegions: []string{instance1.region},
},
},
},
ec2Client: ec2ClientNoInstance{},
request: types.RegisterUsingTokenRequest{
Token: "test_token",
NodeName: "node_name",
Role: types.RoleNode,
HostID: instance1.account + "-" + instance1.instanceID,
EC2IdentityDocument: instance1.iid,
},
expectError: trace.IsAccessDenied,
clock: clockwork.NewFakeClockAt(instance1.pendingTime),
},
{
desc: "TTL expired",
tokenSpec: types.ProvisionTokenSpecV2{
Roles: []types.SystemRole{types.RoleNode},
Allow: []*types.TokenRule{
&types.TokenRule{
AWSAccount: instance1.account,
AWSRegions: []string{instance1.region},
},
},
},
ec2Client: ec2ClientRunning{},
request: types.RegisterUsingTokenRequest{
Token: "test_token",
NodeName: "node_name",
Role: types.RoleNode,
HostID: instance1.account + "-" + instance1.instanceID,
EC2IdentityDocument: instance1.iid,
},
expectError: trace.IsAccessDenied,
clock: clockwork.NewFakeClockAt(instance1.pendingTime.Add(5*time.Minute + time.Second)),
},
{
desc: "custom TTL pass",
tokenSpec: types.ProvisionTokenSpecV2{
Roles: []types.SystemRole{types.RoleNode},
Allow: []*types.TokenRule{
&types.TokenRule{
AWSAccount: instance1.account,
AWSRegions: []string{instance1.region},
},
},
AWSIIDTTL: types.Duration(10 * time.Minute),
},
ec2Client: ec2ClientRunning{},
request: types.RegisterUsingTokenRequest{
Token: "test_token",
NodeName: "node_name",
Role: types.RoleNode,
HostID: instance1.account + "-" + instance1.instanceID,
EC2IdentityDocument: instance1.iid,
},
expectError: isNil,
clock: clockwork.NewFakeClockAt(instance1.pendingTime.Add(9 * time.Minute)),
},
{
desc: "custom TTL fail",
tokenSpec: types.ProvisionTokenSpecV2{
Roles: []types.SystemRole{types.RoleNode},
Allow: []*types.TokenRule{
&types.TokenRule{
AWSAccount: instance1.account,
AWSRegions: []string{instance1.region},
},
},
AWSIIDTTL: types.Duration(10 * time.Minute),
},
ec2Client: ec2ClientRunning{},
request: types.RegisterUsingTokenRequest{
Token: "test_token",
NodeName: "node_name",
Role: types.RoleNode,
HostID: instance1.account + "-" + instance1.instanceID,
EC2IdentityDocument: instance1.iid,
},
expectError: trace.IsAccessDenied,
clock: clockwork.NewFakeClockAt(instance1.pendingTime.Add(11 * time.Minute)),
},
}
for _, tc := range testCases {
t.Run(tc.desc, func(t *testing.T) {
clock := tc.clock
if clock == nil {
clock = clockwork.NewRealClock()
}
a.clock = clock
token, err := types.NewProvisionTokenFromSpec(
"test_token",
time.Now().Add(time.Minute),
tc.tokenSpec)
require.NoError(t, err)
err = a.UpsertToken(context.Background(), token)
require.NoError(t, err)
ctx := context.WithValue(context.Background(), ec2ClientKey{}, tc.ec2Client)
// set common request values here to avoid setting them in every
// testcase
tc.request.PublicSSHKey = sshPublicKey
tc.request.PublicTLSKey = tlsPublicKey
_, err = a.RegisterUsingToken(ctx, &tc.request)
require.True(t, tc.expectError(err))
err = a.DeleteToken(context.Background(), token.GetName())
require.NoError(t, err)
})
}
}
// TestAWSCerts asserts that all certificates parse
func TestAWSCerts(t *testing.T) {
for _, certBytes := range awsRSA2048CertBytes {
certPEM, _ := pem.Decode(certBytes)
_, err := x509.ParseCertificate(certPEM.Bytes)
require.NoError(t, err)
}
}
// TestHostUniqueCheck tests the uniqueness check used by checkEC2JoinRequest
func TestHostUniqueCheck(t *testing.T) {
ctx := context.Background()
p, err := newTestPack(ctx, t.TempDir())
require.NoError(t, err)
a := p.a
a.clock = clockwork.NewFakeClockAt(instance1.pendingTime)
token, err := types.NewProvisionTokenFromSpec(
"test_token",
time.Now().Add(time.Minute),
types.ProvisionTokenSpecV2{
Roles: []types.SystemRole{
types.RoleNode,
types.RoleProxy,
types.RoleKube,
types.RoleDatabase,
types.RoleApp,
types.RoleWindowsDesktop,
},
Allow: []*types.TokenRule{
{
AWSAccount: instance1.account,
AWSRegions: []string{instance1.region},
},
},
})
require.NoError(t, err)
err = a.UpsertToken(context.Background(), token)
require.NoError(t, err)
sshPrivateKey, sshPublicKey, err := a.GenerateKeyPair("")
require.NoError(t, err)
tlsPublicKey, err := PrivateKeyToPublicKeyTLS(sshPrivateKey)
require.NoError(t, err)
testCases := []struct {
role types.SystemRole
upserter func(name string)
}{
{
role: types.RoleNode,
upserter: func(name string) {
node := &types.ServerV2{
Kind: types.KindNode,
Version: types.V2,
Metadata: types.Metadata{
Name: name,
Namespace: defaults.Namespace,
},
}
_, err := a.UpsertNode(context.Background(), node)
require.NoError(t, err)
},
},
{
role: types.RoleProxy,
upserter: func(name string) {
proxy := &types.ServerV2{
Kind: types.KindProxy,
Version: types.V2,
Metadata: types.Metadata{
Name: name,
Namespace: defaults.Namespace,
},
}
err := a.UpsertProxy(proxy)
require.NoError(t, err)
},
},
{
role: types.RoleKube,
upserter: func(name string) {
kube := &types.ServerV2{
Kind: types.KindKubeService,
Version: types.V2,
Metadata: types.Metadata{
Name: name,
Namespace: defaults.Namespace,
},
}
_, err := a.UpsertKubeServiceV2(context.Background(), kube)
require.NoError(t, err)
},
},
{
role: types.RoleDatabase,
upserter: func(name string) {
db, err := types.NewDatabaseServerV3(
types.Metadata{
Name: name,
Namespace: defaults.Namespace,
},
types.DatabaseServerSpecV3{
HostID: name,
Hostname: "test-db",
})
require.NoError(t, err)
_, err = a.UpsertDatabaseServer(context.Background(), db)
require.NoError(t, err)
},
},
{
role: types.RoleApp,
upserter: func(name string) {
app, err := types.NewAppV3(
types.Metadata{
Name: "test-app",
Namespace: defaults.Namespace,
},
types.AppSpecV3{
URI: "https://app.localhost",
})
require.NoError(t, err)
appServer, err := types.NewAppServerV3(
types.Metadata{
Name: name,
Namespace: defaults.Namespace,
},
types.AppServerSpecV3{
HostID: name,
App: app,
})
require.NoError(t, err)
_, err = a.UpsertApplicationServer(context.Background(), appServer)
require.NoError(t, err)
},
},
{
role: types.RoleWindowsDesktop,
upserter: func(name string) {
wds, err := types.NewWindowsDesktopServiceV3(instance1.account+"-"+instance1.instanceID,
types.WindowsDesktopServiceSpecV3{
Addr: "localhost:3028",
TeleportVersion: "10.2.2",
})
require.NoError(t, err)
_, err = a.UpsertWindowsDesktopService(context.Background(), wds)
require.NoError(t, err)
},
},
}
ctx = context.WithValue(ctx, ec2ClientKey{}, ec2ClientRunning{})
for _, tc := range testCases {
t.Run(string(tc.role), func(t *testing.T) {
request := types.RegisterUsingTokenRequest{
Token: "test_token",
NodeName: "node_name",
Role: tc.role,
HostID: instance1.account + "-" + instance1.instanceID,
EC2IdentityDocument: instance1.iid,
PublicSSHKey: sshPublicKey,
PublicTLSKey: tlsPublicKey,
}
// request works with no existing host
_, err = a.RegisterUsingToken(ctx, &request)
require.NoError(t, err)
// add the server
name := instance1.account + "-" + instance1.instanceID
tc.upserter(name)
// request should fail
_, err = a.RegisterUsingToken(ctx, &request)
expectedErr := &trace.AccessDeniedError{}
require.ErrorAs(t, err, &expectedErr)
})
}
}