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
|
package ssh
import (
"fmt"
"github.com/pkg/errors"
"github.com/smallstep/cli/command"
"github.com/smallstep/cli/crypto/sshutil"
"github.com/smallstep/cli/errs"
"github.com/urfave/cli"
)
func listCommand() cli.Command {
return cli.Command{
Name: "list",
Action: command.ActionFunc(listAction),
Usage: "list public keys known to the ssh agent",
UsageText: `**step ssh list** [<subject>] [**--raw**]`,
Description: `**step ssh list** list public key identities known to the ssh agent.
By default it prints key fingerprints, to list the raw key use the flag **--raw**.
## POSITIONAL ARGUMENTS
<subject>
: Optional subject or comment to filter keys by.
## EXAMPLES
List all key fingerprints known to the agent:
'''
$ step ssh list
'''
List all the key fingerprints with the comment joe@work:
'''
$ step ssh list joe@work
'''
List all keys known to the agent:
'''
$ step ssh list --raw
'''
List all the keys with the comment joe@work:
'''
$ step ssh list --raw joe@work
'''`,
Flags: []cli.Flag{
cli.BoolFlag{
Name: "raw",
Usage: "List public keys instead of fingerprints.",
},
},
}
}
func listAction(ctx *cli.Context) error {
if err := errs.MinMaxNumberOfArguments(ctx, 0, 1); err != nil {
return err
}
var subject string
if ctx.NArg() > 0 {
subject = ctx.Args().First()
}
agent, err := sshutil.DialAgent()
if err != nil {
return err
}
keys, err := agent.List()
if err != nil {
return errors.Wrap(err, "error listing identities")
}
if len(keys) == 0 {
fmt.Println("The agent has no identities.")
return nil
}
if ctx.Bool("raw") {
for _, k := range keys {
if ctx.NArg() == 0 || k.Comment == subject {
fmt.Println(k.String())
}
}
} else {
for _, k := range keys {
if ctx.NArg() == 0 || k.Comment == subject {
s, err := sshutil.Fingerprint([]byte(k.String()))
if err != nil {
return err
}
fmt.Println(s)
}
}
}
return nil
}
|