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