github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/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/utils/ssh"
    12  	"launchpad.net/gnuflag"
    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 list-ssh-keys
    31  
    32  To examine the full key, use the '--full' option:
    33  
    34      juju list-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:    "list-ssh-keys",
    52  		Purpose: usageListSSHKeysSummary,
    53  		Doc:     usageListSSHKeysDetails,
    54  		Aliases: []string{"ssh-key", "ssh-keys", "list-ssh-key"},
    55  	}
    56  }
    57  
    58  // SetFlags implements Command.SetFlags.
    59  func (c *listKeysCommand) SetFlags(f *gnuflag.FlagSet) {
    60  	f.BoolVar(&c.showFullKey, "full", false, "Show full key instead of just the fingerprint")
    61  }
    62  
    63  // Run implements Command.Run.
    64  func (c *listKeysCommand) Run(context *cmd.Context) error {
    65  	client, err := c.NewKeyManagerClient()
    66  	if err != nil {
    67  		return err
    68  	}
    69  	defer client.Close()
    70  
    71  	mode := ssh.Fingerprints
    72  	if c.showFullKey {
    73  		mode = ssh.FullKeys
    74  	}
    75  	// TODO(alexisb) - currently keys are global which is not ideal.
    76  	// keymanager needs to be updated to allow keys per user
    77  	c.user = "admin"
    78  	results, err := client.ListKeys(mode, c.user)
    79  	if err != nil {
    80  		return err
    81  	}
    82  	result := results[0]
    83  	if result.Error != nil {
    84  		return result.Error
    85  	}
    86  	fmt.Fprintf(context.Stdout, "Keys used in model: %s\n", c.ConnectionName())
    87  	fmt.Fprintln(context.Stdout, strings.Join(result.Result, "\n"))
    88  	return nil
    89  }