forked from smallstep/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
add.go
726 lines (640 loc) · 21.8 KB
/
add.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
package provisioner
import (
"crypto/ecdsa"
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"io/ioutil"
"net/url"
"strings"
"github.com/RTradeLtd/ca-certificates/authority"
"github.com/RTradeLtd/ca-certificates/authority/provisioner"
"github.com/RTradeLtd/ca-cli/crypto/pemutil"
"github.com/RTradeLtd/ca-cli/errs"
"github.com/RTradeLtd/ca-cli/flags"
"github.com/RTradeLtd/ca-cli/jose"
"github.com/RTradeLtd/ca-cli/ui"
"github.com/RTradeLtd/ca-cli/utils"
"github.com/pkg/errors"
"github.com/urfave/cli"
"golang.org/x/crypto/ed25519"
)
func addCommand() cli.Command {
return cli.Command{
Name: "add",
Action: cli.ActionFunc(addAction),
Usage: "add one or more provisioners the CA configuration",
UsageText: `**step ca provisioner add** <name> <jwk-file> [<jwk-file> ...]
**--ca-config**=<file> [**--type**=JWK] [**--create**] [**--password-file**=<file>]
**step ca provisioner add** <name> **--type**=OIDC **--ca-config**=<file>
[**--client-id**=<id>] [**--client-secret**=<secret>]
[**--configuration-endpoint**=<url>] [**--domain**=<domain>]
[**--admin**=<email>]...
**step ca provisioner add** <name> **--type**=x5c **--x5c-root**=<file>
[**--ca-config**=<file>]...
**step ca provisioner add** <name> **--type**=k8sSA
[**--pem-keys=<file>**] [**--ca-config**=<file>]...
**step ca provisioner add** <name> **--type**=[AWS|Azure|GCP]
[**--ca-config**=<file>] [**--aws-account**=<id>]
[**--gcp-service-account**=<name>] [**--gcp-project**=<name>]
[**--azure-tenant**=<id>] [**--azure-resource-group**=<name>]
[**--instance-age**=<duration>] [**--disable-custom-sans**] [**--disable-trust-on-first-use**]
**step ca provisioner add** <name> **--type**=ACME **--ca-config**=<file>`,
Flags: []cli.Flag{
flags.CaConfig,
cli.StringFlag{
Name: "type",
Value: provisioner.TypeJWK.String(),
Usage: `The <type> of provisioner to create. Type is a case-insensitive string
and must be one of:
**JWK**
: Uses an JWK key pair to sign provisioning tokens. (default)
**OIDC**
: Uses an OpenID Connect provider to sign provisioning tokens.
**AWS**
: Uses Amazon AWS instance identity documents.
**GCP**
: Use Google instance identity tokens.
**Azure**
: Uses Microsoft Azure identity tokens.
**ACME**
: Uses the ACME protocol to create certificates.
**X5C**
: Uses an X509 Certificate / private key pair to sign provisioning tokens.
**K8sSA**
: Uses Kubernetes Service Account tokens.`,
},
flags.PasswordFile,
cli.BoolFlag{
Name: "create",
Usage: `Create a new ECDSA key pair using curve P-256 and populate a new JWK
provisioner with it.`,
},
cli.BoolFlag{
Name: "ssh",
Usage: `Enable SSH on the new provisioners.`,
},
// OIDC provisioner flags
cli.StringFlag{
Name: "client-id",
Usage: `The <id> used to validate the audience in an OpenID Connect token.`,
},
cli.StringFlag{
Name: "client-secret",
Usage: `The <secret> used to obtain the OpenID Connect tokens.`,
},
cli.StringFlag{
Name: "configuration-endpoint",
Usage: `OpenID Connect configuration <url>.`,
},
cli.StringSliceFlag{
Name: "admin",
Usage: `The <email> of an admin user in an OpenID Connect provisioner, this user
will not have restrictions in the certificates to sign. Use the
'--admin' flag multiple times to configure multiple administrators.`,
},
cli.StringSliceFlag{
Name: "domain",
Usage: `The <domain> used to validate the email claim in an OpenID Connect provisioner.
Use the '--domain' flag multiple times to configure multiple domains.`,
},
// Cloud provisioner flags
cli.StringSliceFlag{
Name: "aws-account",
Usage: `The AWS account <id> used to validate the identity documents.
Use the flag multiple times to configure multiple accounts.`,
},
cli.StringFlag{
Name: "azure-tenant",
Usage: `The Microsoft Azure tenant <id> used to validate the identity tokens.`,
},
cli.StringSliceFlag{
Name: "azure-resource-group",
Usage: `The Microsoft Azure resource group <name> used to validate the identity tokens.
Use the flag multipl etimes to configure multiple resource groups`,
},
cli.StringSliceFlag{
Name: "gcp-service-account",
Usage: `The Google service account <email> or <id> used to validate the identity tokens.
Use the flag multiple times to configure multiple service accounts.`,
},
cli.StringSliceFlag{
Name: "gcp-project",
Usage: `The Google project <id> used to validate the identity tokens.
Use the flag multipl etimes to configure multiple projects`,
},
cli.DurationFlag{
Name: "instance-age",
Usage: `The maximum <duration> to grant a certificate in AWS and GCP provisioners.
A <duration> is sequence of decimal numbers, each with optional fraction and a
unit suffix, such as "300ms", "-1.5h" or "2h45m". Valid time units are "ns",
"us" (or "µs"), "ms", "s", "m", "h".`,
},
cli.BoolFlag{
Name: "disable-custom-sans",
Usage: `On cloud provisioners, if anabled only the internal DNS and IP will be added as a SAN.
By default it will accept any SAN in the CSR.`,
},
cli.BoolFlag{
Name: "disable-trust-on-first-use,disable-tofu",
Usage: `On cloud provisioners, if enabled multiple sign request for this provisioner
with the same instance will be accepted. By default only the first request
will be accepted.`,
},
// X5C provisioner flags
cli.StringFlag{
Name: "x5c-root",
Usage: `Root certificate (chain) <file> used to validate the signature on X5C
provisioning tokens.`,
},
// K8sSA provisioner flags
cli.StringFlag{
Name: "pem-keys",
Usage: `Public key <file> for validating signatures on K8s Service Account Tokens.
PEM formatted bundle (can have multiple PEM blocks in the same file) of public
keys and x509 Certificates.`,
},
},
Description: `**step ca provisioner add** adds one or more provisioners
to the configuration and writes the new configuration back to the CA config.
## POSITIONAL ARGUMENTS
<name>
: The name of the provisioners, if a list of JWK files are passed, this name
will be linked to all the keys.
<jwk-path>
: List of private (or public) keys in JWK or PEM format.
## EXAMPLES
Add a single JWK provisioner:
'''
$ step ca provisioner add max@smallstep.com ./max-laptop.jwk --ca-config ca.json
'''
Add a single JWK provisioner using an auto-generated asymmetric key pair:
'''
$ step ca provisioner add max@smallstep.com --ca-config ca.json \
--create
'''
Add a single JWK provisioner with ssh enabled:
'''
$ step ca provisioner add max@smallstep.com --ca-config ca.json --ssh --create
'''
Add a list of provisioners for a single name:
'''
$ step ca provisioner add max@smallstep.com ./max-laptop.jwk ./max-phone.pem ./max-work.pem \
--ca-config ca.json
'''
Add a single OIDC provisioner:
'''
$ step ca provisioner add Google --type oidc --ca-config ca.json \
--client-id 1087160488420-8qt7bavg3qesdhs6it824mhnfgcfe8il.apps.googleusercontent.com \
--configuration-endpoint https://accounts.google.com/.well-known/openid-configuration
'''
Add an OIDC provisioner with two administrators:
'''
$ step ca provisioner add Google --type oidc --ca-config ca.json \
--client-id 1087160488420-8qt7bavg3qesdhs6it824mhnfgcfe8il.apps.googleusercontent.com \
--client-secret udTrOT3gzrO7W9fDPgZQLfYJ \
--configuration-endpoint https://accounts.google.com/.well-known/openid-configuration \
--admin mariano@smallstep.com --admin max@smallstep.com \
--domain smallstep.com
'''
Add an AWS provisioner on one account with a one hour of intance age:
'''
$ step ca provisioner add Amazon --type AWS --ca-config ca.json \
--aws-account 123456789 --instance-age 1h
'''
Add an GCP provisioner with two service accounts and two project ids:
'''
$ step ca provisioner add Google --type GCP --ca-config ca.json \
--gcp-service-account 1234567890-compute@developer.gserviceaccount.com \
--gcp-service-account 9876543210-compute@developer.gserviceaccount.com \
--gcp-project identity --gcp-project accounting
'''
Add an Azure provisioner with two service groups:
'''
$ step ca provisioner add Azure --type Azure --ca-config ca.json \
--azure-tenant bc9043e2-b645-4c1c-a87a-78f8644bfe57 \
--azure-resource-group identity --azure-resource-group accounting
'''
Add an GCP provisioner that will only accept the SANs provided in the identity token:
'''
$ step ca provisioner add Google --type GCP --ca-config ca.json \
--disable-custom-sans --gcp-project internal
'''
Add an AWS provisioner that will only accept the SANs provided in the identity
document and will allow multiple certificates from the same instance:
'''
$ step ca provisioner add Amazon --type AWS --ca-config ca.json \
--aws-account 123456789 --disable-custom-sans --disable-trust-on-first-use
'''
Add an ACME provisioner.
'''
$ step ca provisioner add acme-smallstep --type ACME --ca-config ca.json
'''
Add an X5C provisioner.
'''
$ step ca provisioner add x5c-smallstep --type X5C --x5c-root x5cRoot.crt
'''
Add a K8s Service Account provisioner.
'''
$ step ca provisioner add my-kube-provisioner --type K8sSA --pem-keys keys.pub
'''`,
}
}
func addAction(ctx *cli.Context) (err error) {
if ctx.NArg() == 0 {
return errs.TooFewArguments(ctx)
}
args := ctx.Args()
name := args[0]
config := ctx.String("ca-config")
if len(config) == 0 {
return errs.RequiredFlag(ctx, "ca-config")
}
c, err := authority.LoadConfiguration(config)
if err != nil {
return errors.Wrapf(err, "error loading configuration")
}
typ, err := parseProvisionerType(ctx)
if err != nil {
return err
}
provMap := make(map[string]bool)
for _, p := range c.AuthorityConfig.Provisioners {
provMap[p.GetID()] = true
}
var list provisioner.List
switch typ {
case provisioner.TypeJWK:
list, err = addJWKProvisioner(ctx, name, provMap)
case provisioner.TypeOIDC:
list, err = addOIDCProvisioner(ctx, name, provMap)
case provisioner.TypeAWS:
list, err = addAWSProvisioner(ctx, name, provMap)
case provisioner.TypeAzure:
list, err = addAzureProvisioner(ctx, name, provMap)
case provisioner.TypeGCP:
list, err = addGCPProvisioner(ctx, name, provMap)
case provisioner.TypeACME:
list, err = addACMEProvisioner(ctx, name, provMap)
case provisioner.TypeX5C:
list, err = addX5CProvisioner(ctx, name, provMap)
case provisioner.TypeK8sSA:
list, err = addK8sSAProvisioner(ctx, name, provMap)
default:
return errors.Errorf("unknown type %s: this should not happen", typ)
}
if err != nil {
return err
}
c.AuthorityConfig.Provisioners = append(c.AuthorityConfig.Provisioners, list...)
return c.Save(config)
}
func addJWKProvisioner(ctx *cli.Context, name string, provMap map[string]bool) (list provisioner.List, err error) {
var password string
if passwordFile := ctx.String("password-file"); len(passwordFile) > 0 {
password, err = utils.ReadStringPasswordFromFile(passwordFile)
if err != nil {
return nil, err
}
}
if ctx.Bool("create") {
if ctx.NArg() > 1 {
return nil, errs.IncompatibleFlag(ctx, "create", "<jwk-path> positional arg")
}
pass, err := ui.PromptPasswordGenerate("Please enter a password to encrypt the provisioner private key? [leave empty and we'll generate one]", ui.WithValue(password))
if err != nil {
return nil, err
}
jwk, jwe, err := jose.GenerateDefaultKeyPair(pass)
if err != nil {
return nil, err
}
encryptedKey, err := jwe.CompactSerialize()
if err != nil {
return nil, errors.Wrap(err, "error serializing private key")
}
// Create provisioner
p := &provisioner.JWK{
Type: provisioner.TypeJWK.String(),
Name: name,
Key: jwk,
EncryptedKey: encryptedKey,
Claims: getClaims(ctx),
}
// Check for duplicates
if _, ok := provMap[p.GetID()]; !ok {
provMap[p.GetID()] = true
} else {
return nil, errors.Errorf("duplicated provisioner: CA config already contains a provisioner with name=%s and kid=%s", name, jwk.KeyID)
}
list = append(list, p)
return list, nil
}
// Add multiple provisioners using JWK files.
if ctx.NArg() < 2 {
return nil, errs.TooFewArguments(ctx)
}
jwkFiles := ctx.Args()[1:]
for _, filename := range jwkFiles {
jwk, err := jose.ParseKey(filename)
if err != nil {
return nil, errs.FileError(err, filename)
}
// Only use asymmetric cryptography
if _, ok := jwk.Key.([]byte); ok {
return nil, errors.New("invalid JWK: a symmetric key cannot be used as a provisioner")
}
// Create kid if not present
if len(jwk.KeyID) == 0 {
jwk.KeyID, err = jose.Thumbprint(jwk)
if err != nil {
return nil, err
}
}
key := jwk.Public()
// Initialize provisioner and check for duplicates
p := &provisioner.JWK{
Type: provisioner.TypeJWK.String(),
Name: name,
Key: &key,
Claims: getClaims(ctx),
}
if _, ok := provMap[p.GetID()]; !ok {
provMap[p.GetID()] = true
} else {
return nil, errors.Errorf("duplicated provisioner: CA config already contains a provisioner with name=%s and kid=%s", name, jwk.KeyID)
}
// Encrypt JWK
if !jwk.IsPublic() {
jwe, err := jose.EncryptJWK(jwk)
if err != nil {
return nil, err
}
encryptedKey, err := jwe.CompactSerialize()
if err != nil {
return nil, errors.Wrap(err, "error serializing private key")
}
p.EncryptedKey = encryptedKey
}
list = append(list, p)
}
return list, nil
}
func addOIDCProvisioner(ctx *cli.Context, name string, provMap map[string]bool) (list provisioner.List, err error) {
clientID := ctx.String("client-id")
if len(clientID) == 0 {
return nil, errs.RequiredWithFlagValue(ctx, "type", ctx.String("type"), "client-id")
}
confURL := ctx.String("configuration-endpoint")
if len(confURL) == 0 {
return nil, errs.RequiredWithFlagValue(ctx, "type", ctx.String("type"), "configuration-endpoint")
}
u, err := url.Parse(confURL)
if err != nil || (u.Scheme != "https" && u.Scheme != "http") {
return nil, errs.InvalidFlagValue(ctx, "configuration-endpoint", confURL, "")
}
// Create provisioner
p := &provisioner.OIDC{
Type: provisioner.TypeOIDC.String(),
Name: name,
ClientID: clientID,
ClientSecret: ctx.String("client-secret"),
ConfigurationEndpoint: confURL,
Admins: ctx.StringSlice("admin"),
Domains: ctx.StringSlice("domain"),
Claims: getClaims(ctx),
}
// Check for duplicates
if _, ok := provMap[p.GetID()]; !ok {
provMap[p.GetID()] = true
} else {
return nil, errors.Errorf("duplicated provisioner: CA config already contains a provisioner with name=%s and client-id=%s", p.GetName(), p.GetID())
}
list = append(list, p)
return
}
func addAWSProvisioner(ctx *cli.Context, name string, provMap map[string]bool) (list provisioner.List, err error) {
d, err := parseIntaceAge(ctx)
if err != nil {
return nil, err
}
p := &provisioner.AWS{
Type: provisioner.TypeAWS.String(),
Name: name,
Accounts: ctx.StringSlice("aws-account"),
DisableCustomSANs: ctx.Bool("disable-custom-sans"),
DisableTrustOnFirstUse: ctx.Bool("disable-trust-on-first-use"),
InstanceAge: d,
Claims: getClaims(ctx),
}
// Check for duplicates
if _, ok := provMap[p.GetID()]; !ok {
provMap[p.GetID()] = true
} else {
return nil, errors.Errorf("duplicated provisioner: CA config already contains a provisioner with type=AWS and name=%s", p.GetName())
}
list = append(list, p)
return
}
func addAzureProvisioner(ctx *cli.Context, name string, provMap map[string]bool) (list provisioner.List, err error) {
tenantID := ctx.String("azure-tenant")
if tenantID == "" {
return nil, errs.RequiredWithFlagValue(ctx, "type", ctx.String("type"), "azure-tenant")
}
p := &provisioner.Azure{
Type: provisioner.TypeAzure.String(),
Name: name,
TenantID: tenantID,
ResourceGroups: ctx.StringSlice("azure-resource-group"),
DisableCustomSANs: ctx.Bool("disable-custom-sans"),
DisableTrustOnFirstUse: ctx.Bool("disable-trust-on-first-use"),
Claims: getClaims(ctx),
}
// Check for duplicates
if _, ok := provMap[p.GetID()]; !ok {
provMap[p.GetID()] = true
} else {
return nil, errors.Errorf("duplicated provisioner: CA config already contains a provisioner with type=Azure and name=%s", p.GetName())
}
list = append(list, p)
return
}
func addGCPProvisioner(ctx *cli.Context, name string, provMap map[string]bool) (list provisioner.List, err error) {
d, err := parseIntaceAge(ctx)
if err != nil {
return nil, err
}
p := &provisioner.GCP{
Type: provisioner.TypeGCP.String(),
Name: name,
ServiceAccounts: ctx.StringSlice("gcp-service-account"),
ProjectIDs: ctx.StringSlice("gcp-project"),
DisableCustomSANs: ctx.Bool("disable-custom-sans"),
DisableTrustOnFirstUse: ctx.Bool("disable-trust-on-first-use"),
InstanceAge: d,
Claims: getClaims(ctx),
}
// Check for duplicates
if _, ok := provMap[p.GetID()]; !ok {
provMap[p.GetID()] = true
} else {
return nil, errors.Errorf("duplicated provisioner: CA config already contains a provisioner with type=GCP and name=%s", p.GetName())
}
list = append(list, p)
return
}
func addACMEProvisioner(ctx *cli.Context, name string, provMap map[string]bool) (list provisioner.List, err error) {
p := &provisioner.ACME{
Type: provisioner.TypeACME.String(),
Name: name,
Claims: getClaims(ctx),
}
// Check for duplicates
if _, ok := provMap[p.GetID()]; !ok {
provMap[p.GetID()] = true
} else {
return nil, errors.Errorf("duplicated provisioner: CA config already contains a provisioner with ID==%s", p.GetID())
}
list = append(list, p)
return
}
func addX5CProvisioner(ctx *cli.Context, name string, provMap map[string]bool) (list provisioner.List, err error) {
x5cRootFile := ctx.String("x5c-root")
if len(x5cRootFile) == 0 {
return nil, errs.RequiredWithFlagValue(ctx, "type", "x5c", "x5c-root")
}
roots, err := pemutil.ReadCertificateBundle(x5cRootFile)
if err != nil {
return nil, errors.Wrapf(err, "error loading X5C Root certificates from %s", x5cRootFile)
}
var rootBytes []byte
for _, r := range roots {
if r.KeyUsage&x509.KeyUsageCertSign == 0 {
return nil, errors.Errorf("error: certificate with common name '%s' cannot be "+
"used as an X5C root certificate.\n\n"+
"X5C provisioner root certificates must have the 'Certificate Sign' key "+
"usage extension.", r.Subject.CommonName)
}
rootBytes = append(rootBytes, pem.EncodeToMemory(&pem.Block{
Type: "CERTIFICATE",
Bytes: r.Raw,
})...)
}
p := &provisioner.X5C{
Type: provisioner.TypeX5C.String(),
Name: name,
Claims: getClaims(ctx),
Roots: rootBytes,
}
// Check for duplicates
if _, ok := provMap[p.GetID()]; !ok {
provMap[p.GetID()] = true
} else {
return nil, errors.Errorf("duplicated provisioner: CA config already contains a provisioner with ID=%s", p.GetID())
}
list = append(list, p)
return
}
// addK8sSAProvisioner returns a provisioner list containing a kubernetes
// service account provisioner.
// NOTE: step-ca currently only supports one k8sSA provisioner (because we do
// not have a good way of distinguishing between tokens), therefore w/e `name`
// is entered by the user will be overwritten by a default value.
func addK8sSAProvisioner(ctx *cli.Context, name string, provMap map[string]bool) (list provisioner.List, err error) {
if ctx.Bool("ssh") {
return nil, errors.New("kubernetes service account provisioner does not support ssh certificate actions")
}
pemKeysF := ctx.String("pem-keys")
if len(pemKeysF) == 0 {
return nil, errs.RequiredWithFlagValue(ctx, "type", "k8sSA", "pem-keys")
}
pemKeysB, err := ioutil.ReadFile(pemKeysF)
if err != nil {
return nil, errors.Wrap(err, "error reading pem keys")
}
var (
block *pem.Block
rest = pemKeysB
pemKeys = []interface{}{}
)
for rest != nil {
block, rest = pem.Decode(rest)
if block == nil {
break
}
key, err := pemutil.ParseKey(pem.EncodeToMemory(block))
if err != nil {
return nil, errors.Wrapf(err, "error parsing public key from %s", pemKeysF)
}
switch q := key.(type) {
case *rsa.PublicKey, *ecdsa.PublicKey, ed25519.PublicKey:
default:
return nil, errors.Errorf("Unexpected public key type %T in %s", q, pemKeysF)
}
pemKeys = append(pemKeys, key)
}
var pubKeyBytes []byte
for _, k := range pemKeys {
blk, err := pemutil.Serialize(k)
if err != nil {
return nil, errors.Wrap(err, "error serializing pem key")
}
pubKeyBytes = append(pubKeyBytes, pem.EncodeToMemory(blk)...)
}
p := &provisioner.K8sSA{
Type: provisioner.TypeK8sSA.String(),
Name: name,
Claims: getClaims(ctx),
PubKeys: pubKeyBytes,
}
// Check for duplicates
if _, ok := provMap[p.GetID()]; !ok {
provMap[p.GetID()] = true
} else {
return nil, errors.Errorf("duplicated provisioner: CA config already contains a provisioner with ID=%s", p.GetID())
}
list = append(list, p)
return
}
func getClaims(ctx *cli.Context) *provisioner.Claims {
if ctx.Bool("ssh") {
enable := true
return &provisioner.Claims{
EnableSSHCA: &enable,
}
}
return nil
}
func parseIntaceAge(ctx *cli.Context) (provisioner.Duration, error) {
age := ctx.Duration("instance-age")
if age == 0 {
return provisioner.Duration{}, nil
}
if age < 0 {
return provisioner.Duration{}, errs.MinSizeFlag(ctx, "instance-age", "0s")
}
return provisioner.Duration{Duration: age}, nil
}
func parseProvisionerType(ctx *cli.Context) (provisioner.Type, error) {
typ := ctx.String("type")
switch strings.ToLower(typ) {
case "", "jwk":
return provisioner.TypeJWK, nil
case "oidc":
return provisioner.TypeOIDC, nil
case "gcp":
return provisioner.TypeGCP, nil
case "aws":
return provisioner.TypeAWS, nil
case "azure":
return provisioner.TypeAzure, nil
case "acme":
return provisioner.TypeACME, nil
case "x5c":
return provisioner.TypeX5C, nil
case "k8ssa":
return provisioner.TypeK8sSA, nil
default:
return 0, errs.InvalidFlagValue(ctx, "type", typ, "JWK, OIDC, AWS, Azure, GCP, ACME, X5C, K8sSA")
}
}