github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/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      list-ssh-key
    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  		Aliases: []string{"remove-ssh-keys"},
    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  }