Skip to content

Commit 32bdf40

Browse files
authored
Allow users to define certificate comment in agent (#1158)
* Allow users to define certificate comment in agent Added a comment flag which allows users to set the comment for a certificate when it gets added to an agent. It defaults to current behavior if not set, which is it uses the subject as the comment. This allows users who interact with mutliple CAs with the same identity (email) to have multiple certificates in the agent. It also allows for use cases when users generate SSH certs with different extensions to load multiple certificates in their agent.
1 parent aeee3d0 commit 32bdf40

File tree

3 files changed

+34
-13
lines changed

3 files changed

+34
-13
lines changed

command/ssh/certificate.go

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,12 @@ func certificateCommand() cli.Command {
3737
UsageText: `**step ssh certificate** <key-id> <key-file>
3838
[**--host**] [--**host-id**] [**--sign**] [**--principal**=<string>]
3939
[**--password-file**=<file>] [**--provisioner-password-file**=<file>]
40-
[**--add-user**] [**--not-before**=<time|duration>]
40+
[**--add-user**] [**--not-before**=<time|duration>] [**--comment**=<comment>]
4141
[**--not-after**=<time|duration>] [**--token**=<token>] [**--issuer**=<name>]
4242
[**--no-password**] [**--insecure**] [**--force**] [**--x5c-cert**=<file>]
4343
[**--x5c-key**=<file>] [**--k8ssa-token-path**=<file>] [**--no-agent**]
44-
[**--ca-url**=<uri>] [**--root**=<file>] [**--context**=<name>]
45-
[**--kty**=<key-type>] [**--curve**=<curve>] [**--size**=<size>]`,
44+
[**--kty**=<key-type>] [**--curve**=<curve>] [**--size**=<size>]
45+
[**--ca-url**=<uri>] [**--root**=<file>] [**--context**=<name>]`,
4646

4747
Description: `**step ssh certificate** command generates an SSH key pair and creates a
4848
certificate using [step certificates](https://github.com/smallstep/certificates).
@@ -184,6 +184,10 @@ $ step ssh certificate --kty OKP --curve Ed25519 mariano@work id_ed25519
184184
sshPrivateKeyFlag,
185185
sshProvisionerPasswordFlag,
186186
sshSignFlag,
187+
flags.KTY,
188+
flags.Curve,
189+
flags.Size,
190+
flags.Comment,
187191
flags.KMSUri,
188192
flags.X5cCert,
189193
flags.X5cKey,
@@ -199,9 +203,6 @@ $ step ssh certificate --kty OKP --curve Ed25519 mariano@work id_ed25519
199203
flags.CaURL,
200204
flags.Root,
201205
flags.Context,
202-
flags.KTY,
203-
flags.Curve,
204-
flags.Size,
205206
},
206207
}
207208
}
@@ -219,6 +220,11 @@ func certificateAction(ctx *cli.Context) error {
219220
pubFile := baseName + ".pub"
220221
crtFile := baseName + "-cert.pub"
221222

223+
comment := ctx.String("comment")
224+
if comment == "" {
225+
comment = subject
226+
}
227+
222228
// Flags
223229
token := ctx.String("token")
224230
isHost := ctx.Bool("host")
@@ -502,7 +508,7 @@ func certificateAction(ctx *cli.Context) error {
502508
ui.Printf(`{{ "%s" | red }} {{ "SSH Agent:" | bold }} %v`+"\n", ui.IconBad, err)
503509
} else {
504510
defer agent.Close()
505-
if err := agent.AddCertificate(subject, resp.Certificate.Certificate, priv); err != nil {
511+
if err := agent.AddCertificate(comment, resp.Certificate.Certificate, priv); err != nil {
506512
ui.Printf(`{{ "%s" | red }} {{ "SSH Agent:" | bold }} %v`+"\n", ui.IconBad, err)
507513
} else {
508514
ui.PrintSelected("SSH Agent", "yes")

command/ssh/login.go

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ func loginCommand() cli.Command {
2828
UsageText: `**step ssh login** [<identity>]
2929
[**--token**=<token>] [**--provisioner**=<name>] [**--provisioner-password-file**=<file>]
3030
[**--principal**=<string>] [**--not-before**=<time|duration>] [**--not-after**=<time|duration>]
31+
[**--kty**=<key-type>] [**--curve**=<curve>] [**--size**=<size>] [**--comment**=<comment>]
3132
[**--set**=<key=value>] [**--set-file**=<file>] [**--force**] [**--insecure**]
3233
[**--offline**] [**--ca-config**=<file>]
33-
[**--ca-url**=<uri>] [**--root**=<file>] [**--context**=<name>]
34-
[**--kty**=<key-type>] [**--curve**=<curve>] [**--size**=<size>]`,
34+
[**--ca-url**=<uri>] [**--root**=<file>] [**--context**=<name>]`,
3535
Description: `**step ssh login** generates a new SSH key pair and send a request to [step
3636
certificates](https://github.com/smallstep/certificates) to sign a user
3737
certificate. This certificate will be automatically added to the SSH agent.
@@ -68,13 +68,17 @@ Request a new SSH certificate with multiple principals:
6868
$ step ssh login --principal admin --principal bob [email protected]
6969
'''
7070
71+
Request a new SSH certificate and set a custom comment in the agent
72+
'''
73+
$ step ssh login --comment my-custom-comment [email protected]
74+
'''
75+
7176
Request a new SSH certificate with an EC key and P-521 curve:
7277
'''
7378
$ step ssh certificate --kty EC --curve "P-521" mariano@work id_ecdsa
7479
'''
7580
7681
Request a new SSH certificate with an Octet Key Pair and Ed25519 curve:
77-
7882
'''
7983
$ step ssh certificate --kty OKP --curve Ed25519 mariano@work id_ed25519
8084
'''`,
@@ -95,6 +99,7 @@ $ step ssh certificate --kty OKP --curve Ed25519 mariano@work id_ed25519
9599
flags.CaURL,
96100
flags.Root,
97101
flags.Context,
102+
flags.Comment,
98103
flags.KTY,
99104
flags.Curve,
100105
flags.Size,
@@ -119,6 +124,11 @@ func loginAction(ctx *cli.Context) error {
119124
principals = []string{subject}
120125
}
121126

127+
comment := ctx.String("comment")
128+
if comment == "" {
129+
comment = subject
130+
}
131+
122132
// Flags
123133
token := ctx.String("token")
124134
isAddUser := ctx.Bool("add-user")
@@ -163,7 +173,7 @@ func loginAction(ctx *cli.Context) error {
163173
}
164174

165175
// Just return if key is present
166-
if key, err := agent.GetKey(subject, opts...); err == nil {
176+
if key, err := agent.GetKey(comment, opts...); err == nil {
167177
ui.Printf("The key %s is already present in the SSH agent.\n", key.String())
168178
return nil
169179
}
@@ -270,15 +280,15 @@ func loginAction(ctx *cli.Context) error {
270280
}
271281

272282
// Attempt to add key to agent if private key defined.
273-
if err := agent.AddCertificate(subject, resp.Certificate.Certificate, priv); err != nil {
283+
if err := agent.AddCertificate(comment, resp.Certificate.Certificate, priv); err != nil {
274284
ui.Printf(`{{ "%s" | red }} {{ "SSH Agent:" | bold }} %v`+"\n", ui.IconBad, err)
275285
} else {
276286
ui.PrintSelected("SSH Agent", "yes")
277287
}
278288
if isAddUser {
279289
if resp.AddUserCertificate == nil {
280290
ui.Printf(`{{ "%s" | red }} {{ "Add User Certificate:" | bold }} failed to create a provisioner certificate`+"\n", ui.IconBad)
281-
} else if err := agent.AddCertificate(subject, resp.AddUserCertificate.Certificate, auPriv); err != nil {
291+
} else if err := agent.AddCertificate(comment, resp.AddUserCertificate.Certificate, auPriv); err != nil {
282292
ui.Printf(`{{ "%s" | red }} {{ "Add User Certificate:" | bold }} %v`+"\n", ui.IconBad, err)
283293
} else {
284294
ui.PrintSelected("Add User Certificate", "yes")

flags/flags.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,11 @@ flag exists so it can be configured in $STEPPATH/config/defaults.json.`,
462462
Name: "attestation-uri",
463463
Usage: "The KMS <uri> used for attestation.",
464464
}
465+
466+
Comment = cli.StringFlag{
467+
Name: "comment",
468+
Usage: "The comment used when adding the certificate to an agent. Defaults to the subject if not provided.",
469+
}
465470
)
466471

467472
// FingerprintFormatFlag returns a flag for configuring the fingerprint format.

0 commit comments

Comments
 (0)