/
pki.go
782 lines (718 loc) · 25.2 KB
/
pki.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
package cmd
import (
"encoding/base64"
"encoding/pem"
"errors"
"fmt"
"net/http"
"os"
"strconv"
"strings"
cst "github.com/DelineaXPM/dsv-cli/constants"
apperrors "github.com/DelineaXPM/dsv-cli/errors"
"github.com/DelineaXPM/dsv-cli/internal/predictor"
"github.com/DelineaXPM/dsv-cli/paths"
"github.com/DelineaXPM/dsv-cli/utils"
"github.com/DelineaXPM/dsv-cli/vaultcli"
"github.com/AlecAivazis/survey/v2"
"github.com/mitchellh/cli"
"github.com/spf13/viper"
)
func GetPkiCmd() (cli.Command, error) {
return NewCommand(CommandArgs{
Path: []string{cst.NounPki},
SynopsisText: "Manage certificates",
HelpText: "Work with certificates",
NoConfigRead: true,
NoPreAuth: true,
})
}
func GetPkiRegisterCmd() (cli.Command, error) {
return NewCommand(CommandArgs{
Path: []string{cst.NounPki, cst.Register},
SynopsisText: "Register an existing root certificate authority",
HelpText: fmt.Sprintf(`
Usage:
• %[1]s %[2]s --%[3]s @cert.pem --%[4]s @key.pem --%[5]s myroot --%[6]s google.com,yahoo.com --%[7]s 1000
`, cst.NounPki, cst.Register, cst.CertPath, cst.PrivKeyPath, cst.RootCAPath, cst.Domains, cst.MaxTTL),
FlagsPredictor: []*predictor.Params{
{Name: cst.CertPath, Usage: "Path to a file containing the root certificate (required)"},
{Name: cst.PrivKeyPath, Usage: "Path to a file containing the private key (required)"},
{Name: cst.RootCAPath, Usage: "Path to a secret which will contain the registered root certificate with private key (required)"},
{Name: cst.Domains, Usage: "List of domains for which certificates could be signed on behalf of the root CA (required)"},
{Name: cst.MaxTTL, Usage: "Maximum number of hours for which a signed certificate on behalf of the root CA can be valid (required)"},
{Name: cst.CRL, Usage: "URL of the CRL from which the revocation of leaf certificates can be checked"},
},
RunFunc: handleRegisterRootCmd,
WizardFunc: handleRegisterRootWizard,
})
}
func GetPkiSignCmd() (cli.Command, error) {
return NewCommand(CommandArgs{
Path: []string{cst.NounPki, cst.Sign},
SynopsisText: "Get a new certificate specified by a CSR and signed by a registered root CA",
HelpText: fmt.Sprintf(`
Usage:
• %[1]s %[2]s --%[3]s myroot --%[4]s @csr.pem --%[5]s google.com,android.com --%[6]s 1000h
`, cst.NounPki, cst.Sign, cst.RootCAPath, cst.CSRPath, cst.SubjectAltNames, cst.TTL),
FlagsPredictor: []*predictor.Params{
{Name: cst.CSRPath, Usage: "Path to a file containing the CSR (required)"},
{Name: cst.RootCAPath, Usage: "Path to a secret which contains the registered root certificate with private key (required)"},
{Name: cst.SubjectAltNames, Usage: "List of subject alternative names (domains) for a certificate signed on behalf of the root CA can also be valid"},
{Name: cst.TTL, Usage: "Number of hours for which a signed certificate on behalf of the root CA can be valid"},
{Name: cst.Chain, Usage: "Include root certificate in response", ValueType: "bool"},
},
RunFunc: handleSignCmd,
WizardFunc: handleSignWizard,
})
}
func GetPkiLeafCmd() (cli.Command, error) {
return NewCommand(CommandArgs{
Path: []string{cst.NounPki, cst.Leaf},
SynopsisText: "Get a new private key and leaf certificate signed by a registered root CA",
HelpText: fmt.Sprintf(`
Usage:
• %[1]s %[2]s --%[3]s myroot --%[4]s myleafcert --%[5]s delinea.com --%[6]s Delinea --%[7]s US --%[8]s DC --%[9]s Washington --%[10]s 100d
• %[1]s %[2]s --%[3]s myroot --%[5]s delinea.com
`, cst.NounPki, cst.Leaf, cst.RootCAPath, cst.PkiStorePath, cst.CommonName, cst.Organization, cst.Country, cst.State, cst.Locality, cst.TTL),
FlagsPredictor: []*predictor.Params{
{Name: cst.CommonName, Usage: "Domain for which a certificate is generated (required)"},
{Name: cst.Organization, Usage: ""},
{Name: cst.Country, Usage: ""},
{Name: cst.State, Usage: ""},
{Name: cst.Locality, Usage: ""},
{Name: cst.EmailAddress, Usage: ""},
{Name: cst.Description, Usage: ""},
{Name: cst.RootCAPath, Usage: "Path to a secret which contains the registered root certificate with private key (required)"},
{Name: cst.PkiStorePath, Usage: "Path to a new secret in which to store the generated certificate with private key"},
{Name: cst.TTL, Usage: "Number of hours for which a signed certificate on behalf of the root CA can be valid"},
{Name: cst.Chain, Usage: "Include root certificate in response", ValueType: "bool"},
},
RunFunc: handleLeafCmd,
WizardFunc: handleLeafWizard,
})
}
func GetPkiGenerateRootCmd() (cli.Command, error) {
return NewCommand(CommandArgs{
Path: []string{cst.NounPki, cst.GenerateRoot},
SynopsisText: "Generate and store a new root certificates with private key",
HelpText: fmt.Sprintf(`
Usage:
• %[1]s %[2]s --%[3]s myroot --%[4]s google.org,golang.org --%[5]s delinea.com --%[6]s Delinea --%[7]s US --%[8]s DC --%[9]s Washington --%[10]s 42d
• %[1]s %[2]s --%[3]s myroot --%[4]s google.org,golang.org --%[5]s delinea.com --%[10]s 52w
`, cst.NounPki, cst.GenerateRoot, cst.RootCAPath, cst.Domains, cst.CommonName, cst.Organization, cst.Country, cst.State, cst.Locality, cst.MaxTTL),
FlagsPredictor: []*predictor.Params{
{Name: cst.CommonName, Usage: "The domain name of the root CA (required)"},
{Name: cst.Organization, Usage: ""},
{Name: cst.Country, Usage: ""},
{Name: cst.State, Usage: ""},
{Name: cst.Locality, Usage: ""},
{Name: cst.EmailAddress, Usage: ""},
{Name: cst.Description, Usage: ""},
{Name: cst.RootCAPath, Usage: "Path of a new secret in which to store the generated root certificate with private key (required)"},
{Name: cst.Domains, Usage: "List of domains for which certificates could be signed on behalf of the root CA (required)"},
{Name: cst.MaxTTL, Usage: "Number of hours for which a generated root certificate can be valid (required)"},
{Name: cst.CRL, Usage: "URL of the CRL from which the revocation of leaf certificates can be checked"},
},
RunFunc: handleGenerateRootCmd,
WizardFunc: handleGenerateRootWizard,
})
}
func GetPkiSSHCertCmd() (cli.Command, error) {
return NewCommand(CommandArgs{
Path: []string{cst.NounPki, cst.Sign},
SynopsisText: "Get a new SSH certificate",
HelpText: fmt.Sprintf(`
Usage:
• %[1]s %[2]s --%[3]s myroot --%[4]s myleaf --%[5]s root,ubuntu --%[6]s 1000
`, cst.NounPki, cst.SSHCert, cst.RootCAPath, cst.LeafCAPath, cst.Principals, cst.TTL),
FlagsPredictor: []*predictor.Params{
{Name: cst.RootCAPath, Usage: "Path to a secret which contains the registered root certificate with private key (required)"},
{Name: cst.LeafCAPath, Usage: "Path to a secret which contains the leaf certificate with SSH-compatible public key (required)"},
{Name: cst.Principals, Usage: "List of principals on the certificate (required)"},
{Name: cst.TTL, Usage: "Number of hours for which a signed certificate can be valid (required)"},
},
MinNumberArgs: 8,
RunFunc: handleGetSSHCertificateCmd,
})
}
func handleRegisterRootCmd(vcli vaultcli.CLI, args []string) int {
params := map[string]string{
cst.CertPath: viper.GetString(cst.CertPath),
cst.PrivKeyPath: viper.GetString(cst.PrivKeyPath),
cst.RootCAPath: viper.GetString(cst.RootCAPath),
cst.Domains: viper.GetString(cst.Domains),
cst.MaxTTL: viper.GetString(cst.MaxTTL),
}
data, err := submitRoot(vcli, params)
vcli.Out().WriteResponse(data, apperrors.New(err))
return utils.GetExecStatus(err)
}
func handleSignCmd(vcli vaultcli.CLI, args []string) int {
params := map[string]string{
cst.CSRPath: viper.GetString(cst.CSRPath),
cst.RootCAPath: viper.GetString(cst.RootCAPath),
cst.SubjectAltNames: viper.GetString(cst.SubjectAltNames),
cst.TTL: viper.GetString(cst.TTL),
cst.Chain: viper.GetString(cst.Chain),
}
data, err := submitSign(vcli, params)
vcli.Out().WriteResponse(data, apperrors.New(err))
return utils.GetExecStatus(err)
}
func handleLeafCmd(vcli vaultcli.CLI, args []string) int {
params := map[string]string{
cst.RootCAPath: viper.GetString(cst.RootCAPath),
cst.PkiStorePath: viper.GetString(cst.PkiStorePath),
cst.TTL: viper.GetString(cst.TTL),
cst.CommonName: viper.GetString(cst.CommonName),
cst.Organization: viper.GetString(cst.Organization),
cst.Country: viper.GetString(cst.Country),
cst.State: viper.GetString(cst.State),
cst.Locality: viper.GetString(cst.Locality),
cst.EmailAddress: viper.GetString(cst.EmailAddress),
cst.Description: viper.GetString(cst.Description),
cst.Chain: viper.GetString(cst.Chain),
}
data, err := submitLeaf(vcli, params)
vcli.Out().WriteResponse(data, apperrors.New(err))
return utils.GetExecStatus(err)
}
func handleGenerateRootCmd(vcli vaultcli.CLI, args []string) int {
params := map[string]string{
cst.RootCAPath: viper.GetString(cst.RootCAPath),
cst.Domains: viper.GetString(cst.Domains),
cst.MaxTTL: viper.GetString(cst.MaxTTL),
cst.CommonName: viper.GetString(cst.CommonName),
cst.Organization: viper.GetString(cst.Organization),
cst.Country: viper.GetString(cst.Country),
cst.State: viper.GetString(cst.State),
cst.Locality: viper.GetString(cst.Locality),
cst.EmailAddress: viper.GetString(cst.EmailAddress),
cst.Description: viper.GetString(cst.Description),
}
data, err := submitGenerateRoot(vcli, params)
vcli.Out().WriteResponse(data, apperrors.New(err))
return utils.GetExecStatus(err)
}
func handleGetSSHCertificateCmd(vcli vaultcli.CLI, args []string) int {
params := map[string]string{
cst.RootCAPath: viper.GetString(cst.RootCAPath),
cst.LeafCAPath: viper.GetString(cst.LeafCAPath),
cst.Principals: viper.GetString(cst.Principals),
cst.TTL: viper.GetString(cst.TTL),
}
paramErr := ValidateParams(params, []string{cst.RootCAPath, cst.LeafCAPath, cst.Principals, cst.TTL})
if paramErr != nil {
vcli.Out().Fail(paramErr)
return utils.GetExecStatus(paramErr)
}
body := sshCertificateInformation{
RootCAPath: params[cst.RootCAPath],
LeafCAPAth: params[cst.LeafCAPath],
Principals: utils.StringToSlice(params[cst.Principals]),
}
if ttl, err := utils.ParseHours(params[cst.TTL]); err != nil {
vcli.Out().Fail(err)
return utils.GetExecStatus(err)
} else {
body.TTL = ttl
}
resp, apiErr := pkiSSHCert(vcli, &body)
vcli.Out().WriteResponse(resp, apiErr)
return utils.GetExecStatus(apiErr)
}
func handleRegisterRootWizard(vcli vaultcli.CLI) int {
qs := []*survey.Question{
{
Name: "CertFile",
Prompt: &survey.Input{Message: "Path to certificate file:"},
Validate: vaultcli.SurveyRequiredFile,
Transform: vaultcli.SurveyTrimSpace,
},
{
Name: "PrivKeyFile",
Prompt: &survey.Input{Message: "Path to private key file:"},
Validate: vaultcli.SurveyRequiredFile,
Transform: vaultcli.SurveyTrimSpace,
},
{
Name: "SecretPath",
Prompt: &survey.Input{Message: "Path to a new secret that will contain root CA registration information:"},
Validate: vaultcli.SurveyRequired,
Transform: vaultcli.SurveyTrimSpace,
},
{
Name: "Domains",
Prompt: &survey.Input{Message: "List of domains (comma-delimited strings):"},
Validate: vaultcli.SurveyRequired,
Transform: vaultcli.SurveyTrimSpace,
},
{
Name: "MaxTTL",
Prompt: &survey.Input{Message: "Maximum TTL:"},
Validate: vaultcli.SurveyRequired,
Transform: vaultcli.SurveyTrimSpace,
},
{
Name: "CRL",
Prompt: &survey.Input{Message: "Certificate Revocation List URL:"},
Transform: vaultcli.SurveyTrimSpace,
},
}
answers := struct {
CertFile string
PrivKeyFile string
SecretPath string
Domains string
MaxTTL string
CRL string
}{}
survErr := survey.Ask(qs, &answers)
if survErr != nil {
vcli.Out().Fail(survErr)
return utils.GetExecStatus(survErr)
}
params := map[string]string{
cst.RootCAPath: answers.SecretPath,
cst.Domains: answers.Domains,
cst.MaxTTL: answers.MaxTTL,
cst.CRL: answers.CRL,
}
data, err := os.ReadFile(answers.CertFile)
if err != nil {
vcli.Out().Fail(err)
return utils.GetExecStatus(err)
}
params[cst.CertPath] = string(data)
data, err = os.ReadFile(answers.PrivKeyFile)
if err != nil {
vcli.Out().Fail(err)
return utils.GetExecStatus(err)
}
params[cst.PrivKeyPath] = string(data)
resp, err := submitRoot(vcli, params)
vcli.Out().WriteResponse(resp, apperrors.New(err))
return utils.GetExecStatus(err)
}
func submitRoot(vcli vaultcli.CLI, params map[string]string) ([]byte, error) {
paramErr := ValidateParams(params, []string{cst.RootCAPath, cst.PrivKeyPath, cst.CertPath, cst.Domains, cst.MaxTTL})
if paramErr != nil {
return nil, paramErr
}
_, err := parsePem(params[cst.CertPath])
if err != nil {
return nil, err
}
_, err = parsePem(params[cst.PrivKeyPath])
if err != nil {
return nil, err
}
maxTTL, err := utils.ParseHours(params[cst.MaxTTL])
if err != nil {
return nil, err
}
body := rootCASecret{
RootCAPath: params[cst.RootCAPath],
PrivateKey: base64Encode(params[cst.PrivKeyPath]),
Certificate: base64Encode(params[cst.CertPath]),
Domains: utils.StringToSlice(params[cst.Domains]),
MaxTTL: maxTTL,
CRL: viper.GetString(cst.CRL),
}
return pkiRegister(vcli, &body)
}
func handleSignWizard(vcli vaultcli.CLI) int {
qs := []*survey.Question{
{
Name: "CSRPath",
Prompt: &survey.Input{Message: "Path to certificate signing request file:"},
Validate: vaultcli.SurveyRequiredFile,
Transform: vaultcli.SurveyTrimSpace,
},
{
Name: "SecretPath",
Prompt: &survey.Input{Message: "Path of an existing secret that contains root CA information:"},
Validate: vaultcli.SurveyRequired,
Transform: vaultcli.SurveyTrimSpace,
},
{
Name: "SubjectAltNames",
Prompt: &survey.Input{Message: "List of subject alternative names (comma-delimited strings):"},
Transform: vaultcli.SurveyTrimSpace,
},
{
Name: "TTL",
Prompt: &survey.Input{Message: "TTL:"},
Validate: vaultcli.SurveyRequired,
Transform: vaultcli.SurveyTrimSpace,
},
{
Name: "Chain",
Prompt: &survey.Confirm{Message: "Chain (optional - include root certificate):", Default: false},
},
}
answers := struct {
CSRPath string
SecretPath string
SubjectAltNames string
TTL string
Chain bool
}{}
survErr := survey.Ask(qs, &answers)
if survErr != nil {
vcli.Out().Fail(survErr)
return utils.GetExecStatus(survErr)
}
params := map[string]string{
cst.RootCAPath: answers.SecretPath,
cst.SubjectAltNames: answers.SubjectAltNames,
cst.TTL: answers.TTL,
cst.Chain: strconv.FormatBool(answers.Chain),
}
data, err := os.ReadFile(answers.CSRPath)
if err != nil {
vcli.Out().Fail(err)
return utils.GetExecStatus(err)
}
params[cst.CSRPath] = string(data)
resp, err := submitSign(vcli, params)
vcli.Out().WriteResponse(resp, apperrors.New(err))
return utils.GetExecStatus(err)
}
func submitSign(vcli vaultcli.CLI, params map[string]string) ([]byte, error) {
paramErr := ValidateParams(params, []string{cst.CSRPath, cst.RootCAPath})
if paramErr != nil {
return nil, paramErr
}
_, err := parsePem(params[cst.CSRPath])
if err != nil {
return nil, err
}
body := signingRequest{
RootCAPath: params[cst.RootCAPath],
CSR: base64Encode(params[cst.CSRPath]),
SubjectAltNames: utils.StringToSlice(params[cst.SubjectAltNames]),
}
if params[cst.TTL] != "" {
ttl, err := utils.ParseHours(params[cst.TTL])
if err != nil {
return nil, err
}
body.TTL = ttl
}
if c, err := strconv.ParseBool(params[cst.Chain]); err == nil {
body.Chain = c
}
return pkiSign(vcli, &body)
}
func handleLeafWizard(vcli vaultcli.CLI) int {
qs := []*survey.Question{
{
Name: "RootCAPath",
Prompt: &survey.Input{Message: "Path of an existing secret that contains root CA information:"},
Validate: vaultcli.SurveyRequired,
Transform: vaultcli.SurveyTrimSpace,
},
{
Name: "CommonName",
Prompt: &survey.Input{Message: "Common name:"},
Validate: vaultcli.SurveyRequired,
Transform: vaultcli.SurveyTrimSpace,
},
{
Name: "Organization",
Prompt: &survey.Input{Message: "Organization:"},
Transform: vaultcli.SurveyTrimSpace,
},
{
Name: "Country",
Prompt: &survey.Input{Message: "Country:"},
Transform: vaultcli.SurveyTrimSpace,
},
{
Name: "State",
Prompt: &survey.Input{Message: "State:"},
Transform: vaultcli.SurveyTrimSpace,
},
{
Name: "Locality",
Prompt: &survey.Input{Message: "Locality:"},
Transform: vaultcli.SurveyTrimSpace,
},
{
Name: "Email",
Prompt: &survey.Input{Message: "Email:"},
Transform: vaultcli.SurveyTrimSpace,
},
{
Name: "TTL",
Prompt: &survey.Input{Message: "TTL:"},
Transform: vaultcli.SurveyTrimSpace,
},
{
Name: "PkiStorePath",
Prompt: &survey.Input{Message: "Path to a new secret in which to store the generated certificate with private key:"},
Transform: vaultcli.SurveyTrimSpace,
},
{
Name: "Chain",
Prompt: &survey.Confirm{Message: "Chain (optional - include root certificate):", Default: false},
},
}
answers := struct {
RootCAPath string
CommonName string
Organization string
Country string
State string
Locality string
Email string
TTL string
PkiStorePath string
Chain bool
}{}
survErr := survey.Ask(qs, &answers)
if survErr != nil {
vcli.Out().Fail(survErr)
return utils.GetExecStatus(survErr)
}
params := map[string]string{
cst.RootCAPath: answers.RootCAPath,
cst.CommonName: answers.CommonName,
cst.Organization: answers.Organization,
cst.Country: answers.Country,
cst.State: answers.State,
cst.Locality: answers.Locality,
cst.EmailAddress: answers.Email,
cst.TTL: answers.TTL,
cst.PkiStorePath: answers.PkiStorePath,
cst.Chain: strconv.FormatBool(answers.Chain),
}
resp, err := submitLeaf(vcli, params)
vcli.Out().WriteResponse(resp, apperrors.New(err))
return utils.GetExecStatus(err)
}
func submitLeaf(vcli vaultcli.CLI, params map[string]string) ([]byte, error) {
paramErr := ValidateParams(params, []string{cst.RootCAPath, cst.CommonName})
if paramErr != nil {
return nil, paramErr
}
body := signingRequestInformation{
RootCAPath: params[cst.RootCAPath],
StorePath: params[cst.PkiStorePath],
}
body.CommonName = params[cst.CommonName]
body.Organization = params[cst.Organization]
body.Country = params[cst.Country]
body.State = params[cst.State]
body.Locality = params[cst.Locality]
body.EmailAddress = params[cst.EmailAddress]
body.Description = params[cst.Description]
if c, err := strconv.ParseBool(params[cst.Chain]); err == nil {
body.Chain = c
}
if params[cst.TTL] != "" {
ttl, err := utils.ParseHours(params[cst.TTL])
if err != nil {
return nil, err
}
body.TTL = ttl
}
return pkiLeaf(vcli, &body)
}
func handleGenerateRootWizard(vcli vaultcli.CLI) int {
qs := []*survey.Question{
{
Name: "RootCAPath",
Prompt: &survey.Input{Message: "Path of a new secret in which to store the generated root certificate with private key:"},
Validate: vaultcli.SurveyRequired,
Transform: vaultcli.SurveyTrimSpace,
},
{
Name: "CommonName",
Prompt: &survey.Input{Message: "Common name:"},
Validate: vaultcli.SurveyRequired,
Transform: vaultcli.SurveyTrimSpace,
},
{
Name: "Organization",
Prompt: &survey.Input{Message: "Organization:"},
Transform: vaultcli.SurveyTrimSpace,
},
{
Name: "Country",
Prompt: &survey.Input{Message: "Country:"},
Transform: vaultcli.SurveyTrimSpace,
},
{
Name: "State",
Prompt: &survey.Input{Message: "State:"},
Transform: vaultcli.SurveyTrimSpace,
},
{
Name: "Locality",
Prompt: &survey.Input{Message: "Locality:"},
Transform: vaultcli.SurveyTrimSpace,
},
{
Name: "Email",
Prompt: &survey.Input{Message: "Email:"},
Transform: vaultcli.SurveyTrimSpace,
},
{
Name: "Domains",
Prompt: &survey.Input{Message: "List of domains (comma-delimited strings):"},
Validate: vaultcli.SurveyRequired,
Transform: vaultcli.SurveyTrimSpace,
},
{
Name: "MaxTTL",
Prompt: &survey.Input{Message: "Maximum TTL:"},
Validate: vaultcli.SurveyRequired,
Transform: vaultcli.SurveyTrimSpace,
},
{
Name: "CRL",
Prompt: &survey.Input{Message: "Certificate Revocation List URL:"},
Transform: vaultcli.SurveyTrimSpace,
},
}
answers := struct {
RootCAPath string
CommonName string
Organization string
Country string
State string
Locality string
Email string
Domains string
MaxTTL string
CRL string
}{}
survErr := survey.Ask(qs, &answers)
if survErr != nil {
vcli.Out().Fail(survErr)
return utils.GetExecStatus(survErr)
}
params := map[string]string{
cst.RootCAPath: answers.RootCAPath,
cst.CommonName: answers.CommonName,
cst.Organization: answers.Organization,
cst.Country: answers.Country,
cst.State: answers.State,
cst.Locality: answers.Locality,
cst.EmailAddress: answers.Email,
cst.Domains: answers.Domains,
cst.MaxTTL: answers.MaxTTL,
cst.CRL: answers.CRL,
}
resp, err := submitGenerateRoot(vcli, params)
vcli.Out().WriteResponse(resp, apperrors.New(err))
return utils.GetExecStatus(err)
}
func submitGenerateRoot(vcli vaultcli.CLI, params map[string]string) ([]byte, error) {
paramErr := ValidateParams(params, []string{cst.RootCAPath, cst.CommonName, cst.Domains, cst.MaxTTL})
if paramErr != nil {
return nil, paramErr
}
body := generateRootInformation{
RootCAPath: params[cst.RootCAPath],
Domains: utils.StringToSlice(params[cst.Domains]),
CRL: viper.GetString(cst.CRL),
}
body.CommonName = params[cst.CommonName]
body.Organization = params[cst.Organization]
body.Country = params[cst.Country]
body.State = params[cst.State]
body.Locality = params[cst.Locality]
body.EmailAddress = params[cst.EmailAddress]
body.Description = params[cst.Description]
maxTTL, err := utils.ParseHours(params[cst.MaxTTL])
if err != nil {
return nil, err
}
body.MaxTTL = maxTTL
return pkiGenRoot(vcli, &body)
}
func parsePem(data string) (*pem.Block, error) {
block, _ := pem.Decode([]byte(data))
if block == nil {
return nil, errors.New("failed to decode the data into PEM format")
}
return block, nil
}
func base64Encode(data string) string {
return base64.StdEncoding.EncodeToString([]byte(data))
}
// API callers:
type rootCASecret struct {
RootCAPath string `json:"rootCAPath"`
PrivateKey string `json:"privateKey"`
Certificate string `json:"certificate"`
Domains []string `json:"domains"`
MaxTTL int `json:"maxTTL"`
CRL string `json:"crl"`
}
func pkiRegister(vcli vaultcli.CLI, body *rootCASecret) ([]byte, *apperrors.ApiError) {
basePath := strings.Join([]string{cst.NounPki, cst.Register}, "/")
uri := paths.CreateURI(basePath, nil)
return vcli.HTTPClient().DoRequest(http.MethodPost, uri, body)
}
type subjectInformation struct {
Country string `json:"country"`
State string `json:"state"`
Locality string `json:"locality"`
Organization string `json:"organization"`
OrganizationalUnit string `json:"organizationalUnit"`
CommonName string `json:"commonName"`
EmailAddress string `json:"emailAddress"`
Description string `json:"description"`
}
type generateRootInformation struct {
subjectInformation
RootCAPath string `json:"rootCAPath"`
Domains []string `json:"domains"`
MaxTTL int `json:"maxTTL"`
CRL string `json:"crl"`
}
func pkiGenRoot(vcli vaultcli.CLI, body *generateRootInformation) ([]byte, *apperrors.ApiError) {
basePath := strings.Join([]string{cst.NounPki, cst.Root}, "/")
uri := paths.CreateURI(basePath, nil)
return vcli.HTTPClient().DoRequest(http.MethodPost, uri, body)
}
type signingRequestInformation struct {
subjectInformation
RootCAPath string `json:"rootCAPath"`
StorePath string `json:"storePath"`
TTL int `json:"ttl"`
Chain bool `json:"chain"`
}
func pkiLeaf(vcli vaultcli.CLI, body *signingRequestInformation) ([]byte, *apperrors.ApiError) {
basePath := strings.Join([]string{cst.NounPki, cst.Leaf}, "/")
uri := paths.CreateURI(basePath, nil)
return vcli.HTTPClient().DoRequest(http.MethodPost, uri, body)
}
type sshCertificateInformation struct {
RootCAPath string `json:"rootCAPath"`
LeafCAPAth string `json:"leafCAPath"`
Principals []string `json:"principals"`
TTL int `json:"ttl"`
}
func pkiSSHCert(vcli vaultcli.CLI, body *sshCertificateInformation) ([]byte, *apperrors.ApiError) {
basePath := strings.Join([]string{cst.NounPki, cst.SSHCert}, "/")
uri := paths.CreateURI(basePath, nil)
return vcli.HTTPClient().DoRequest(http.MethodPost, uri, body)
}
type signingRequest struct {
RootCAPath string `json:"rootCAPath"`
CSR string `json:"csr"`
SubjectAltNames []string `json:"subjectAltNames"`
TTL int `json:"ttl"`
Chain bool `json:"chain"`
}
func pkiSign(vcli vaultcli.CLI, body *signingRequest) ([]byte, *apperrors.ApiError) {
basePath := strings.Join([]string{cst.NounPki, cst.Sign}, "/")
uri := paths.CreateURI(basePath, nil)
return vcli.HTTPClient().DoRequest(http.MethodPost, uri, body)
}