github.com/cloudbase/juju-core@v0.0.0-20140504232958-a7271ac7912f/cmd/juju/authorisedkeys_import.go (about) 1 // Copyright 2013 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package main 5 6 import ( 7 "errors" 8 "fmt" 9 10 "launchpad.net/gnuflag" 11 12 "launchpad.net/juju-core/cmd" 13 "launchpad.net/juju-core/juju" 14 ) 15 16 var importKeysDoc = ` 17 Import new authorised ssh keys to allow the holder of those keys to log on to Juju nodes or machines. 18 The keys are imported using ssh-import-id. 19 ` 20 21 // ImportKeysCommand is used to add new authorized ssh keys for a user. 22 type ImportKeysCommand struct { 23 cmd.EnvCommandBase 24 user string 25 sshKeyIds []string 26 } 27 28 func (c *ImportKeysCommand) Info() *cmd.Info { 29 return &cmd.Info{ 30 Name: "import", 31 Args: "<ssh key id> [...]", 32 Doc: importKeysDoc, 33 Purpose: "using ssh-import-id, import new authorized ssh keys for a Juju user", 34 } 35 } 36 37 func (c *ImportKeysCommand) Init(args []string) error { 38 switch len(args) { 39 case 0: 40 return errors.New("no ssh key id specified") 41 default: 42 c.sshKeyIds = args 43 } 44 return nil 45 } 46 47 func (c *ImportKeysCommand) SetFlags(f *gnuflag.FlagSet) { 48 c.EnvCommandBase.SetFlags(f) 49 f.StringVar(&c.user, "user", "admin", "the user for which to import the keys") 50 } 51 52 func (c *ImportKeysCommand) Run(context *cmd.Context) error { 53 client, err := juju.NewKeyManagerClient(c.EnvName) 54 if err != nil { 55 return err 56 } 57 defer client.Close() 58 59 results, err := client.ImportKeys(c.user, c.sshKeyIds...) 60 if err != nil { 61 return err 62 } 63 for i, result := range results { 64 if result.Error != nil { 65 fmt.Fprintf(context.Stderr, "cannot import key id %q: %v\n", c.sshKeyIds[i], result.Error) 66 } 67 } 68 return nil 69 }