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