github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/cmd/juju/commands/add_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  	"errors"
     8  	"fmt"
     9  
    10  	"github.com/juju/cmd"
    11  
    12  	jujucmd "github.com/juju/juju/cmd"
    13  	"github.com/juju/juju/cmd/juju/block"
    14  	"github.com/juju/juju/cmd/modelcmd"
    15  )
    16  
    17  var usageAddSSHKeySummary = `
    18  Adds a public SSH key to a model.`[1:]
    19  
    20  var usageAddSSHKeyDetails = `
    21  Juju maintains a per-model cache of public SSH keys which it copies to
    22  each unit (including units already deployed). By default this includes the
    23  key of the user who created the model (assuming it is stored in the
    24  default location ~/.ssh/). Additional keys may be added with this command,
    25  quoting the entire public key as an argument.
    26  
    27  Examples:
    28      juju add-ssh-key "ssh-rsa qYfS5LieM79HIOr535ret6xy
    29      AAAAB3NzaC1yc2EAAAADAQA6fgBAAABAQCygc6Rc9XgHdhQqTJ
    30      Wsoj+I3xGrOtk21xYtKijnhkGqItAHmrE5+VH6PY1rVIUXhpTg
    31      pSkJsHLmhE29OhIpt6yr8vQSOChqYfS5LieM79HIOJEgJEzIqC
    32      52rCYXLvr/BVkd6yr4IoM1vpb/n6u9o8v1a0VUGfc/J6tQAcPR
    33      ExzjZUVsfjj8HdLtcFq4JLYC41miiJtHw4b3qYu7qm3vh4eCiK
    34      1LqLncXnBCJfjj0pADXaL5OQ9dmD3aCbi8KFyOEs3UumPosgmh
    35      VCAfjjHObWHwNQ/ZU2KrX1/lv/+lBChx2tJliqQpyYMiA3nrtS
    36      jfqQgZfjVF5vz8LESQbGc6+vLcXZ9KQpuYDt joe@ubuntu"
    37  
    38  For ease of use it is possible to use shell substitution to pass the key 
    39  to the command:
    40  
    41  juju add-ssh-key "$(cat ~/mykey.pub)"
    42  
    43  See also: 
    44      ssh-keys
    45      remove-ssh-key
    46      import-ssh-key`[1:]
    47  
    48  // NewAddKeysCommand is used to add a new ssh key to a model.
    49  func NewAddKeysCommand() cmd.Command {
    50  	return modelcmd.Wrap(&addKeysCommand{})
    51  }
    52  
    53  // addKeysCommand is used to add a new authorized ssh key for a user.
    54  type addKeysCommand struct {
    55  	SSHKeysBase
    56  	user    string
    57  	sshKeys []string
    58  }
    59  
    60  // Info implements Command.Info.
    61  func (c *addKeysCommand) Info() *cmd.Info {
    62  	return jujucmd.Info(&cmd.Info{
    63  		Name:    "add-ssh-key",
    64  		Args:    "<ssh key> ...",
    65  		Purpose: usageAddSSHKeySummary,
    66  		Doc:     usageAddSSHKeyDetails,
    67  	})
    68  }
    69  
    70  // Init implements Command.Init.
    71  func (c *addKeysCommand) Init(args []string) error {
    72  	switch len(args) {
    73  	case 0:
    74  		return errors.New("no ssh key specified")
    75  	default:
    76  		c.sshKeys = args
    77  	}
    78  	return nil
    79  }
    80  
    81  // Run implements Command.Run.
    82  func (c *addKeysCommand) Run(context *cmd.Context) error {
    83  	client, err := c.NewKeyManagerClient()
    84  	if err != nil {
    85  		return err
    86  	}
    87  	defer client.Close()
    88  	// TODO(alexisb) - currently keys are global which is not ideal.
    89  	// keymanager needs to be updated to allow keys per user
    90  	c.user = "admin"
    91  	results, err := client.AddKeys(c.user, c.sshKeys...)
    92  	if err != nil {
    93  		return block.ProcessBlockedError(err, block.BlockChange)
    94  	}
    95  	for i, result := range results {
    96  		if result.Error != nil {
    97  			fmt.Fprintf(context.Stderr, "cannot add key %q: %v\n", c.sshKeys[i], result.Error)
    98  		}
    99  	}
   100  	return nil
   101  }