github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/cmd/juju/commands/list_sshkeys.go (about) 1 // Copyright 2013 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package commands 5 6 import ( 7 "fmt" 8 "strings" 9 10 "github.com/juju/cmd" 11 "github.com/juju/gnuflag" 12 "github.com/juju/utils/ssh" 13 14 "github.com/juju/juju/cmd/modelcmd" 15 ) 16 17 var usageListSSHKeysSummary = ` 18 Lists the currently known SSH keys for the current (or specified) model.`[1:] 19 20 var usageListSSHKeysDetails = ` 21 Juju maintains a per-model cache of SSH keys which it copies to each newly 22 created unit. 23 This command will display a list of all the keys currently used by Juju in 24 the current model (or the model specified, if the '-m' option is used). 25 By default a minimal list is returned, showing only the fingerprint of 26 each key and its text identifier. By using the '--full' option, the entire 27 key may be displayed. 28 29 Examples: 30 juju ssh-keys 31 32 To examine the full key, use the '--full' option: 33 34 juju ssh-keys -m jujutest --full`[1:] 35 36 // NewListKeysCommand returns a command used to list the authorized ssh keys. 37 func NewListKeysCommand() cmd.Command { 38 return modelcmd.Wrap(&listKeysCommand{}) 39 } 40 41 // listKeysCommand is used to list the authorized ssh keys. 42 type listKeysCommand struct { 43 SSHKeysBase 44 showFullKey bool 45 user string 46 } 47 48 // Info implements Command.Info. 49 func (c *listKeysCommand) Info() *cmd.Info { 50 return &cmd.Info{ 51 Name: "ssh-keys", 52 Purpose: usageListSSHKeysSummary, 53 Doc: usageListSSHKeysDetails, 54 Aliases: []string{"list-ssh-keys"}, 55 } 56 } 57 58 // SetFlags implements Command.SetFlags. 59 func (c *listKeysCommand) SetFlags(f *gnuflag.FlagSet) { 60 c.SSHKeysBase.SetFlags(f) 61 f.BoolVar(&c.showFullKey, "full", false, "Show full key instead of just the fingerprint") 62 } 63 64 // Run implements Command.Run. 65 func (c *listKeysCommand) Run(context *cmd.Context) error { 66 client, err := c.NewKeyManagerClient() 67 if err != nil { 68 return err 69 } 70 defer client.Close() 71 72 mode := ssh.Fingerprints 73 if c.showFullKey { 74 mode = ssh.FullKeys 75 } 76 // TODO(alexisb) - currently keys are global which is not ideal. 77 // keymanager needs to be updated to allow keys per user 78 c.user = "admin" 79 results, err := client.ListKeys(mode, c.user) 80 if err != nil { 81 return err 82 } 83 result := results[0] 84 if result.Error != nil { 85 return result.Error 86 } 87 if len(result.Result) == 0 { 88 context.Infof("No keys to display.") 89 return nil 90 } 91 fmt.Fprintf(context.Stdout, "Keys used in model: %s\n", c.ConnectionName()) 92 fmt.Fprintln(context.Stdout, strings.Join(result.Result, "\n")) 93 return nil 94 }