Skip to content

Commit

Permalink
Allow users to define certificate comment in agent
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
redrac committed Apr 23, 2024
1 parent 576d8ad commit 9cc3c76
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 6 deletions.
11 changes: 9 additions & 2 deletions command/ssh/certificate.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ func certificateCommand() cli.Command {
[**--not-after**=<time|duration>] [**--token**=<token>] [**--issuer**=<name>]
[**--no-password**] [**--insecure**] [**--force**] [**--x5c-cert**=<file>]
[**--x5c-key**=<file>] [**--k8ssa-token-path**=<file>] [**--no-agent**]
[**--ca-url**=<uri>] [**--root**=<file>] [**--context**=<name>]`,
[**--ca-url**=<uri>] [**--root**=<file>] [**--context**=<name>]
[**--comment**=<comment>]`,

Description: `**step ssh certificate** command generates an SSH key pair and creates a
certificate using [step certificates](https://github.com/smallstep/certificates).
Expand Down Expand Up @@ -185,6 +186,7 @@ $ step ssh certificate --token $TOKEN mariano@work id_ecdsa
flags.CaURL,
flags.Root,
flags.Context,
flags.Comment,
},
}
}
Expand All @@ -202,6 +204,11 @@ func certificateAction(ctx *cli.Context) error {
pubFile := baseName + ".pub"
crtFile := baseName + "-cert.pub"

comment := ctx.String("comment")
if comment == "" {
comment = subject
}

// Flags
token := ctx.String("token")
isHost := ctx.Bool("host")
Expand Down Expand Up @@ -480,7 +487,7 @@ func certificateAction(ctx *cli.Context) error {
ui.Printf(`{{ "%s" | red }} {{ "SSH Agent:" | bold }} %v`+"\n", ui.IconBad, err)
} else {
defer agent.Close()
if err := agent.AddCertificate(subject, resp.Certificate.Certificate, priv); err != nil {
if err := agent.AddCertificate(comment, resp.Certificate.Certificate, priv); err != nil {
ui.Printf(`{{ "%s" | red }} {{ "SSH Agent:" | bold }} %v`+"\n", ui.IconBad, err)
} else {
ui.PrintSelected("SSH Agent", "yes")
Expand Down
20 changes: 16 additions & 4 deletions command/ssh/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ func loginCommand() cli.Command {
[**--principal**=<string>] [**--not-before**=<time|duration>] [**--not-after**=<time|duration>]
[**--set**=<key=value>] [**--set-file**=<file>] [**--force**]
[**--offline**] [**--ca-config**=<file>]
[**--ca-url**=<uri>] [**--root**=<file>] [**--context**=<name>]`,
[**--ca-url**=<uri>] [**--root**=<file>] [**--context**=<name>]
[**--comment**=<comment>]`,
Description: `**step ssh login** generates a new SSH key pair and send a request to [step
certificates](https://github.com/smallstep/certificates) to sign a user
certificate. This certificate will be automatically added to the SSH agent.
Expand Down Expand Up @@ -64,6 +65,11 @@ $ step ssh login --not-after 1h alice
Request a new SSH certificate with multiple principals:
'''
$ step ssh login --principal admin --principal bob [email protected]
'''
Request a new SSH certificate and set a custom comment in the agent
'''
$ step ssh login --comment my-custom-comment [email protected]
'''`,
Flags: []cli.Flag{
flags.Token,
Expand All @@ -82,6 +88,7 @@ $ step ssh login --principal admin --principal bob [email protected]
flags.CaURL,
flags.Root,
flags.Context,
flags.Comment,
},
}
}
Expand All @@ -102,6 +109,11 @@ func loginAction(ctx *cli.Context) error {
principals = []string{subject}
}

comment := ctx.String("comment")
if comment == "" {
comment = subject
}

// Flags
token := ctx.String("token")
isAddUser := ctx.Bool("add-user")
Expand Down Expand Up @@ -140,7 +152,7 @@ func loginAction(ctx *cli.Context) error {
}

// Just return if key is present
if key, err := agent.GetKey(subject, opts...); err == nil {
if key, err := agent.GetKey(comment, opts...); err == nil {
ui.Printf("The key %s is already present in the SSH agent.\n", key.String())
return nil
}
Expand Down Expand Up @@ -248,15 +260,15 @@ func loginAction(ctx *cli.Context) error {
}

// Attempt to add key to agent if private key defined.
if err := agent.AddCertificate(subject, resp.Certificate.Certificate, priv); err != nil {
if err := agent.AddCertificate(comment, resp.Certificate.Certificate, priv); err != nil {
ui.Printf(`{{ "%s" | red }} {{ "SSH Agent:" | bold }} %v`+"\n", ui.IconBad, err)
} else {
ui.PrintSelected("SSH Agent", "yes")
}
if isAddUser {
if resp.AddUserCertificate == nil {
ui.Printf(`{{ "%s" | red }} {{ "Add User Certificate:" | bold }} failed to create a provisioner certificate`+"\n", ui.IconBad)
} else if err := agent.AddCertificate(subject, resp.AddUserCertificate.Certificate, auPriv); err != nil {
} else if err := agent.AddCertificate(comment, resp.AddUserCertificate.Certificate, auPriv); err != nil {
ui.Printf(`{{ "%s" | red }} {{ "Add User Certificate:" | bold }} %v`+"\n", ui.IconBad, err)
} else {
ui.PrintSelected("Add User Certificate", "yes")
Expand Down
5 changes: 5 additions & 0 deletions flags/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,11 @@ flag exists so it can be configured in $STEPPATH/config/defaults.json.`,
Name: "attestation-uri",
Usage: "The KMS <uri> used for attestation.",
}

Comment = cli.StringFlag{
Name: "comment",
Usage: "The comment used when adding the certificate to an agent. Defaults to the subject if not provided.",
}
)

// FingerprintFormatFlag returns a flag for configuring the fingerprint format.
Expand Down

0 comments on commit 9cc3c76

Please sign in to comment.