github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/cmd/juju/commands/import_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  
     9  	"github.com/juju/cmd"
    10  	"github.com/juju/errors"
    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 usageImportSSHKeySummary = `
    18  Adds a public SSH key from a trusted identity source to a model.`[1:]
    19  
    20  var usageImportSSHKeyDetails = `
    21  Juju can add SSH keys to its cache from reliable public sources (currently
    22  Launchpad and GitHub), allowing those users SSH access to Juju machines.
    23  
    24  The user identity supplied is the username on the respective service given by
    25  'lp:' or 'gh:'.
    26  
    27  If the user has multiple keys on the service, all the keys will be added.
    28  
    29  Once the keys are imported, they can be viewed with the `[1:] + "`juju ssh-keys`" + `
    30  command, where comments will indicate which ones were imported in
    31  this way.
    32  
    33  An alternative to this command is the more manual ` + "`juju add-ssh-key`" + `.
    34  
    35  Examples:
    36  Import all public keys associated with user account 'phamilton' on the
    37  GitHub service:
    38  
    39      juju import-ssh-key gh:phamilton
    40  
    41  Multiple identities may be specified in a space delimited list:
    42  
    43  juju import-ssh-key gh:rheinlein lp:iasmiov gh:hharrison
    44  
    45  See also: 
    46      add-ssh-key
    47      ssh-keys`
    48  
    49  // NewImportKeysCommand is used to add new authorized ssh keys to a model.
    50  func NewImportKeysCommand() cmd.Command {
    51  	return modelcmd.Wrap(&importKeysCommand{})
    52  }
    53  
    54  // importKeysCommand is used to import authorized ssh keys to a model.
    55  type importKeysCommand struct {
    56  	SSHKeysBase
    57  	user      string
    58  	sshKeyIds []string
    59  }
    60  
    61  // Info implements Command.Info.
    62  func (c *importKeysCommand) Info() *cmd.Info {
    63  	return jujucmd.Info(&cmd.Info{
    64  		Name:    "import-ssh-key",
    65  		Args:    "<lp|gh>:<user identity> ...",
    66  		Purpose: usageImportSSHKeySummary,
    67  		Doc:     usageImportSSHKeyDetails,
    68  	})
    69  }
    70  
    71  // Init implements Command.Init.
    72  func (c *importKeysCommand) Init(args []string) error {
    73  	if len(args) == 0 {
    74  		return errors.New("no ssh key id specified")
    75  	}
    76  	c.sshKeyIds = args
    77  	for _, k := range c.sshKeyIds {
    78  		if len(k) < 3 {
    79  			return errors.NotValidf("%q key ID", k)
    80  		}
    81  		switch k[:3] {
    82  		case "lp:", "gh:":
    83  		default:
    84  			return errors.NewNotSupported(nil,
    85  				fmt.Sprintf("prefix in Key ID %q not supported, only lp: and gh: are allowed", k))
    86  		}
    87  	}
    88  	return nil
    89  }
    90  
    91  // Run implemetns Command.Run.
    92  func (c *importKeysCommand) Run(context *cmd.Context) error {
    93  	client, err := c.NewKeyManagerClient()
    94  	if err != nil {
    95  		return err
    96  	}
    97  	defer client.Close()
    98  
    99  	// TODO(alexisb) - currently keys are global which is not ideal.
   100  	// keymanager needs to be updated to allow keys per user
   101  	c.user = "admin"
   102  	results, err := client.ImportKeys(c.user, c.sshKeyIds...)
   103  	if err != nil {
   104  		return block.ProcessBlockedError(err, block.BlockChange)
   105  	}
   106  	for i, result := range results {
   107  		if result.Error != nil {
   108  			fmt.Fprintf(context.Stderr, "cannot import key id %q: %v\n", c.sshKeyIds[i], result.Error)
   109  		}
   110  	}
   111  	return nil
   112  }