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