Debian Codegraph @

command/ssh/logout.go

raw symbols SCIP
1 package ssh
2
3 import (
4 "fmt"
5
6 "github.com/pkg/errors"
7 "github.com/smallstep/cli/command"
8 "github.com/smallstep/cli/crypto/sshutil"
9 "github.com/smallstep/cli/errs"
10 "github.com/smallstep/cli/flags"
11 "github.com/smallstep/cli/utils/cautils"
12 "github.com/urfave/cli"
13 "golang.org/x/crypto/ssh"
14 )
15
16 func logoutCommand() cli.Command {
17 return cli.Command{
18 Name: "logout",
19 Action: command.ActionFunc(logoutAction),
20 Usage: "removes a private key from the ssh-agent",
21 UsageText: `**step ssh logout** <identity>
22 [**--all**] [**--ca-url**=<uri>] [**--root**=<file>]
23 [**--offline**] [**--ca-config**=<path>]`,
24 Description: `**step ssh logout** commands removes a key from the ssh-agent.
25
26 By default it only removes certificate keys signed by step-certificates, but the
27 flag **--all** can be used to remove all keys with a given subject or all keys.
28
29 ## POSITIONAL ARGUMENTS
30
31 <identity>
32 : The certificate identity or comment in the key.
33
34 ## EXAMPLES
35
36 Remove the certificate mariano@work from the SSH agent:
37 '''
38 $ step ssh logout mariano@work
39 '''
40
41 Remove the all the keys and certificates for mariano@work from the SSH agent:
42 '''
43 $ step ssh logout --all mariano@work
44 '''
45
46 Remove the key mariano@work from the agent listening in /tmp/ssh/agent:
47 '''
48 $ SSH_AUTH_SOCK=/tmp/ssh/agent step ssh logout mariano@work
49 '''
50
51 Remove all the keys stored in the SSH agent:
52 '''
53 $ step ssh logout --all
54 '''`,
55 Flags: []cli.Flag{
56 cli.BoolFlag{
57 Name: "all",
58 Usage: "Removes all the keys stored in the SSH agent.",
59 },
60 flags.Identity,
61 flags.CaURL,
62 flags.Root,
63 flags.Offline,
64 flags.CaConfig,
65 },
66 }
67 }
68
69 func logoutAction(ctx *cli.Context) error {
70 if err := errs.MinMaxNumberOfArguments(ctx, 0, 1); err != nil {
71 return err
72 }
73
74 all := ctx.Bool("all")
75 subject := ctx.Args().First()
76 if subject == "" {
77 subject = ctx.String("identity")
78 if subject == "" && !all {
79 return errs.TooFewArguments(ctx)
80 }
81 }
82
83 agent, err := sshutil.DialAgent()
84 if err != nil {
85 return err
86 }
87 defer agent.Close()
88
89 // Remove all
90 if all && ctx.NArg() == 0 {
91 if err := agent.RemoveAll(); err != nil {
92 return errors.Wrap(err, "error removing all keys")
93 }
94 fmt.Println("All identities removed.")
95 return nil
96 }
97
98 var opts []sshutil.AgentOption
99 if !all {
100 // Remove only keys signed by the CA
101 client, err := cautils.NewClient(ctx)
102 if err != nil {
103 return err
104 }
105 if roots, err := client.SSHRoots(); err == nil && len(roots.UserKeys) > 0 {
106 userKeys := make([]ssh.PublicKey, len(roots.UserKeys))
107 for i, uk := range roots.UserKeys {
108 userKeys[i] = uk.PublicKey
109 }
110 opts = append(opts, sshutil.WithSignatureKey(userKeys))
111 }
112 }
113
114 // Remove if comment == subject
115 found, err := agent.RemoveKeys(subject, opts...)
116 if err != nil {
117 return err
118 }
119
120 switch {
121 case !found:
122 fmt.Printf("Identity not found: %s\n", subject)
123 case all:
124 fmt.Printf("All identities removed: %s\n", subject)
125 default:
126 fmt.Printf("Identity removed: %s\n", subject)
127 }
128 return nil
129 }