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