github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/cmd/juju/commands/remove_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 usageRemoveSSHKeySummary = ` 18 Removes a public SSH key (or keys) from a model.`[1:] 19 20 var usageRemoveSSHKeyDetails = ` 21 Juju maintains a per-model cache of public SSH keys which it copies to 22 each unit. This command will remove a specified key (or space separated 23 list of keys) from the model cache and all current units deployed in that 24 model. The keys to be removed may be specified by the key's fingerprint, 25 or by the text label associated with them. 26 27 Examples: 28 juju remove-ssh-key ubuntu@ubuntu 29 juju remove-ssh-key 45:7f:33:2c:10:4e:6c:14:e3:a1:a4:c8:b2:e1:34:b4 30 juju remove-ssh-key bob@ubuntu carol@ubuntu 31 32 See also: 33 ssh-keys 34 add-ssh-key 35 import-ssh-key`[1:] 36 37 // NewRemoveKeysCommand is used to delete ssk keys for a user. 38 func NewRemoveKeysCommand() cmd.Command { 39 return modelcmd.Wrap(&removeKeysCommand{}) 40 } 41 42 // removeKeysCommand is used to delete authorised ssh keys for a user. 43 type removeKeysCommand struct { 44 SSHKeysBase 45 user string 46 keyIds []string 47 } 48 49 // Info implements Command.Info. 50 func (c *removeKeysCommand) Info() *cmd.Info { 51 return jujucmd.Info(&cmd.Info{ 52 Name: "remove-ssh-key", 53 Args: "<ssh key id> ...", 54 Purpose: usageRemoveSSHKeySummary, 55 Doc: usageRemoveSSHKeyDetails, 56 }) 57 } 58 59 // Init implements Command.Init. 60 func (c *removeKeysCommand) Init(args []string) error { 61 switch len(args) { 62 case 0: 63 return errors.New("no ssh key id specified") 64 default: 65 c.keyIds = args 66 } 67 return nil 68 } 69 70 // Run implements Command.Run. 71 func (c *removeKeysCommand) Run(context *cmd.Context) error { 72 client, err := c.NewKeyManagerClient() 73 if err != nil { 74 return err 75 } 76 defer client.Close() 77 78 // TODO(alexisb) - currently keys are global which is not ideal. 79 // keymanager needs to be updated to allow keys per user 80 c.user = "admin" 81 results, err := client.DeleteKeys(c.user, c.keyIds...) 82 if err != nil { 83 return block.ProcessBlockedError(err, block.BlockChange) 84 } 85 for i, result := range results { 86 if result.Error != nil { 87 fmt.Fprintf(context.Stderr, "cannot remove key id %q: %v\n", c.keyIds[i], result.Error) 88 } 89 } 90 return nil 91 }