open-vault/command/revoke.go

145 lines
3.5 KiB
Go
Raw Normal View History

2015-04-01 02:21:02 +00:00
package command
import (
"fmt"
"strings"
2016-04-01 17:16:05 +00:00
2017-09-05 04:04:05 +00:00
"github.com/mitchellh/cli"
"github.com/posener/complete"
2015-04-01 02:21:02 +00:00
)
2017-09-05 04:04:05 +00:00
// Ensure we are implementing the right interfaces.
var _ cli.Command = (*ReadCommand)(nil)
var _ cli.CommandAutocomplete = (*ReadCommand)(nil)
2015-04-01 02:21:02 +00:00
// RevokeCommand is a Command that mounts a new mount.
type RevokeCommand struct {
2017-09-05 04:04:05 +00:00
*BaseCommand
flagForce bool
flagPrefix bool
2015-04-01 02:21:02 +00:00
}
2017-09-05 04:04:05 +00:00
func (c *RevokeCommand) Synopsis() string {
return "Revokes leases and secrets"
}
2015-04-01 02:21:02 +00:00
2017-09-05 04:04:05 +00:00
func (c *RevokeCommand) Help() string {
helpText := `
Usage: vault revoke [options] ID
2015-04-01 02:21:02 +00:00
2017-09-05 04:04:05 +00:00
Revokes secrets by their lease ID. This command can revoke a single secret
or multiple secrets based on a path-matched prefix.
2015-04-01 02:21:02 +00:00
2017-09-05 04:04:05 +00:00
Revoke a single lease:
$ vault revoke database/creds/readonly/2f6a614c...
Revoke all leases for a role:
$ vault revoke -prefix aws/creds/deploy
Force delete leases from Vault even if backend revocation fails:
$ vault revoke -force -prefix consul/creds
2015-04-01 02:21:02 +00:00
2017-09-05 04:04:05 +00:00
For a full list of examples and paths, please see the documentation that
corresponds to the secret backend in use.
` + c.Flags().Help()
return strings.TrimSpace(helpText)
2015-04-01 02:21:02 +00:00
}
2017-09-05 04:04:05 +00:00
func (c *RevokeCommand) Flags() *FlagSets {
set := c.flagSet(FlagSetHTTP)
f := set.NewFlagSet("Command Options")
f.BoolVar(&BoolVar{
Name: "force",
Aliases: []string{"f"},
Target: &c.flagForce,
Default: false,
Usage: "Delete the lease from Vault even if the backend revocation " +
"fails. This is meant for recovery situations where the secret " +
"in the backend was manually removed. If this flag is specified, " +
"-prefix is also required.",
})
f.BoolVar(&BoolVar{
Name: "prefix",
Target: &c.flagPrefix,
Default: false,
Usage: "Treat the ID as a prefix instead of an exact lease ID. This can " +
"revoke multiple leases simultaneously.",
})
return set
2015-04-01 02:21:02 +00:00
}
2017-09-05 04:04:05 +00:00
func (c *RevokeCommand) AutocompleteArgs() complete.Predictor {
return c.PredictVaultFiles()
}
2015-04-01 02:21:02 +00:00
2017-09-05 04:04:05 +00:00
func (c *RevokeCommand) AutocompleteFlags() complete.Flags {
return c.Flags().Completions()
}
2015-04-01 02:21:02 +00:00
2017-09-05 04:04:05 +00:00
func (c *RevokeCommand) Run(args []string) int {
f := c.Flags()
2017-09-05 04:04:05 +00:00
if err := f.Parse(args); err != nil {
c.UI.Error(err.Error())
return 1
}
2015-04-01 02:21:02 +00:00
2017-09-05 04:04:05 +00:00
args = f.Args()
leaseID, remaining, err := extractID(args)
if err != nil {
c.UI.Error(err.Error())
return 1
}
2015-04-01 02:21:02 +00:00
2017-09-05 04:04:05 +00:00
if len(remaining) > 0 {
c.UI.Error(fmt.Sprintf("Too many arguments (expected 1, got %d)", len(args)))
return 1
}
2015-04-01 02:21:02 +00:00
2017-09-05 04:04:05 +00:00
if c.flagForce && !c.flagPrefix {
c.UI.Error("Specifying -force requires also specifying -prefix")
return 1
}
2015-04-01 02:21:02 +00:00
2017-09-05 04:04:05 +00:00
client, err := c.Client()
if err != nil {
c.UI.Error(err.Error())
return 2
}
switch {
case c.flagForce && c.flagPrefix:
c.UI.Warn(wrapAtLength("Warning! Force-removing leases can cause Vault " +
"to become out of sync with credential backends!"))
if err := client.Sys().RevokeForce(leaseID); err != nil {
c.UI.Error(fmt.Sprintf("Error force revoking leases with prefix %s: %s", leaseID, err))
return 2
}
c.UI.Output(fmt.Sprintf("Success! Force revoked any leases with prefix: %s", leaseID))
return 0
case c.flagPrefix:
if err := client.Sys().RevokePrefix(leaseID); err != nil {
c.UI.Error(fmt.Sprintf("Error revoking leases with prefix %s: %s", leaseID, err))
return 2
}
c.UI.Output(fmt.Sprintf("Success! Revoked any leases with prefix: %s", leaseID))
return 0
default:
if err := client.Sys().Revoke(leaseID); err != nil {
c.UI.Error(fmt.Sprintf("Error revoking lease %s: %s", leaseID, err))
return 2
}
c.UI.Output(fmt.Sprintf("Success! Revoked lease: %s", leaseID))
return 0
}
2015-04-01 02:21:02 +00:00
}