-
Notifications
You must be signed in to change notification settings - Fork 255
/
add.go
95 lines (75 loc) · 2.36 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
package eab
import (
"fmt"
"os"
"github.com/pkg/errors"
"github.com/urfave/cli"
"go.step.sm/cli-utils/errs"
adminAPI "github.com/smallstep/certificates/authority/admin/api"
"github.com/smallstep/cli/flags"
"github.com/smallstep/cli/utils/cautils"
)
func addCommand() cli.Command {
return cli.Command{
Name: "add",
Action: cli.ActionFunc(addAction),
Usage: "add ACME External Account Binding Key",
UsageText: `**step ca acme eab add** <provisioner> [<eab-key-reference>]
[**--admin-cert**=<file>] [**--admin-key**=<file>] [**--admin-subject**=<subject>]
[**--admin-provisioner**=<name>] [**--admin-password-file**=<file>]
[**--ca-url**=<uri>] [**--root**=<file>] [**--context**=<name>]`,
Flags: []cli.Flag{
flags.AdminCert,
flags.AdminKey,
flags.AdminSubject,
flags.AdminProvisioner,
flags.AdminPasswordFile,
flags.CaURL,
flags.Root,
flags.Context,
},
Description: `**step ca acme eab add** adds ACME External Account Binding Key.
## POSITIONAL ARGUMENTS
<provisioner>
: Name of the provisioner to which the ACME EAB key will be added
<eab-key-reference>
: (Optional) reference (from external system) for the key that will be added
## EXAMPLES
Add an ACME External Account Binding Key without reference:
'''
$ step ca acme eab add my_acme_provisioner
'''
Add an ACME External Account Binding Key with reference:
'''
$ step ca acme eab add my_acme_provisioner my_first_eab_key
'''`,
}
}
func addAction(ctx *cli.Context) (err error) {
if err := errs.MinMaxNumberOfArguments(ctx, 1, 2); err != nil {
return err
}
args := ctx.Args()
provisioner := args.Get(0)
reference := ""
if ctx.NArg() == 2 {
reference = args.Get(1)
}
client, err := cautils.NewAdminClient(ctx)
if err != nil {
return errors.Wrap(err, "error creating admin client")
}
eak, err := client.CreateExternalAccountKey(provisioner, &adminAPI.CreateExternalAccountKeyRequest{
Reference: reference,
})
if err != nil {
return errors.Wrap(notImplemented(err), "error creating ACME EAB key")
}
cliEAK := toCLI(ctx, client, eak)
// TODO(hs): JSON output, so that executing this command can be more easily automated?
out := os.Stdout
format := "%-36s%-28s%-48s%s\n"
fmt.Fprintf(out, format, "Key ID", "Provisioner", "Key (base64, raw url encoded)", "Reference")
fmt.Fprintf(out, format, cliEAK.id, cliEAK.provisioner, cliEAK.key, cliEAK.reference)
return nil
}