forked from juju/juju
/
list_sshkeys.go
100 lines (85 loc) · 2.6 KB
/
list_sshkeys.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
96
97
98
99
100
// Copyright 2013 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package commands
import (
"fmt"
"strings"
"github.com/juju/cmd"
"github.com/juju/errors"
"github.com/juju/gnuflag"
"github.com/juju/utils/ssh"
jujucmd "github.com/juju/juju/cmd"
"github.com/juju/juju/cmd/modelcmd"
)
var usageListSSHKeysSummary = `
Lists the currently known SSH keys for the current (or specified) model.`[1:]
var usageListSSHKeysDetails = `
Juju maintains a per-model cache of SSH keys which it copies to each newly
created unit.
This command will display a list of all the keys currently used by Juju in
the current model (or the model specified, if the '-m' option is used).
By default a minimal list is returned, showing only the fingerprint of
each key and its text identifier. By using the '--full' option, the entire
key may be displayed.
Examples:
juju ssh-keys
To examine the full key, use the '--full' option:
juju ssh-keys -m jujutest --full`[1:]
// NewListKeysCommand returns a command used to list the authorized ssh keys.
func NewListKeysCommand() cmd.Command {
return modelcmd.Wrap(&listKeysCommand{})
}
// listKeysCommand is used to list the authorized ssh keys.
type listKeysCommand struct {
SSHKeysBase
showFullKey bool
user string
}
// Info implements Command.Info.
func (c *listKeysCommand) Info() *cmd.Info {
return jujucmd.Info(&cmd.Info{
Name: "ssh-keys",
Purpose: usageListSSHKeysSummary,
Doc: usageListSSHKeysDetails,
Aliases: []string{"list-ssh-keys"},
})
}
// SetFlags implements Command.SetFlags.
func (c *listKeysCommand) SetFlags(f *gnuflag.FlagSet) {
c.SSHKeysBase.SetFlags(f)
f.BoolVar(&c.showFullKey, "full", false, "Show full key instead of just the fingerprint")
}
// Run implements Command.Run.
func (c *listKeysCommand) Run(context *cmd.Context) error {
client, err := c.NewKeyManagerClient()
if err != nil {
return err
}
defer client.Close()
mode := ssh.Fingerprints
if c.showFullKey {
mode = ssh.FullKeys
}
// TODO(alexisb) - currently keys are global which is not ideal.
// keymanager needs to be updated to allow keys per user
c.user = "admin"
results, err := client.ListKeys(mode, c.user)
if err != nil {
return errors.Trace(err)
}
result := results[0]
if result.Error != nil {
return errors.Trace(result.Error)
}
if len(result.Result) == 0 {
context.Infof("No keys to display.")
return nil
}
modelName, err := c.ModelName()
if err != nil {
return errors.Trace(err)
}
fmt.Fprintf(context.Stdout, "Keys used in model: %s\n", modelName)
fmt.Fprintln(context.Stdout, strings.Join(result.Result, "\n"))
return nil
}