2023-03-15 16:00:52 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2015-04-01 02:21:02 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
2016-04-01 17:16:05 +00:00
|
|
|
|
2018-07-11 19:45:09 +00:00
|
|
|
"github.com/hashicorp/vault/api"
|
2017-09-05 04:04:05 +00:00
|
|
|
"github.com/mitchellh/cli"
|
|
|
|
"github.com/posener/complete"
|
2015-04-01 02:21:02 +00:00
|
|
|
)
|
|
|
|
|
2021-04-08 16:43:39 +00:00
|
|
|
var (
|
|
|
|
_ cli.Command = (*LeaseRevokeCommand)(nil)
|
|
|
|
_ cli.CommandAutocomplete = (*LeaseRevokeCommand)(nil)
|
|
|
|
)
|
2017-09-05 04:04:05 +00:00
|
|
|
|
2017-09-08 01:59:31 +00:00
|
|
|
type LeaseRevokeCommand struct {
|
2017-09-05 04:04:05 +00:00
|
|
|
*BaseCommand
|
|
|
|
|
|
|
|
flagForce bool
|
|
|
|
flagPrefix bool
|
2018-07-11 19:45:09 +00:00
|
|
|
flagSync bool
|
2015-04-01 02:21:02 +00:00
|
|
|
}
|
|
|
|
|
2017-09-08 01:59:31 +00:00
|
|
|
func (c *LeaseRevokeCommand) Synopsis() string {
|
2017-09-05 04:04:05 +00:00
|
|
|
return "Revokes leases and secrets"
|
|
|
|
}
|
2015-04-01 02:21:02 +00:00
|
|
|
|
2017-09-08 01:59:31 +00:00
|
|
|
func (c *LeaseRevokeCommand) Help() string {
|
2017-09-05 04:04:05 +00:00
|
|
|
helpText := `
|
2017-09-08 01:59:31 +00:00
|
|
|
Usage: vault lease 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
|
|
|
|
2018-07-11 19:45:09 +00:00
|
|
|
The default behavior when not using -force is to revoke asynchronously; Vault
|
|
|
|
will queue the revocation and keep trying if it fails (including across
|
|
|
|
restarts). The -sync flag can be used to force a synchronous operation, but
|
|
|
|
it is then up to the caller to retry on failure. Force mode always operates
|
|
|
|
synchronously.
|
|
|
|
|
2017-09-05 04:04:05 +00:00
|
|
|
Revoke a single lease:
|
|
|
|
|
2017-09-08 01:59:31 +00:00
|
|
|
$ vault lease revoke database/creds/readonly/2f6a614c...
|
2017-09-05 04:04:05 +00:00
|
|
|
|
|
|
|
Revoke all leases for a role:
|
|
|
|
|
2017-09-08 01:59:31 +00:00
|
|
|
$ vault lease revoke -prefix aws/creds/deploy
|
2017-09-05 04:04:05 +00:00
|
|
|
|
2017-09-08 01:59:31 +00:00
|
|
|
Force delete leases from Vault even if secret engine revocation fails:
|
2017-09-05 04:04:05 +00:00
|
|
|
|
2017-09-08 01:59:31 +00:00
|
|
|
$ vault lease 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
|
2017-09-08 01:59:31 +00:00
|
|
|
corresponds to the secret engine in use.
|
2017-09-05 04:04:05 +00:00
|
|
|
|
|
|
|
` + c.Flags().Help()
|
|
|
|
|
|
|
|
return strings.TrimSpace(helpText)
|
2015-04-01 02:21:02 +00:00
|
|
|
}
|
|
|
|
|
2017-09-08 01:59:31 +00:00
|
|
|
func (c *LeaseRevokeCommand) Flags() *FlagSets {
|
2017-09-05 04:04:05 +00:00
|
|
|
set := c.flagSet(FlagSetHTTP)
|
|
|
|
f := set.NewFlagSet("Command Options")
|
|
|
|
|
|
|
|
f.BoolVar(&BoolVar{
|
|
|
|
Name: "force",
|
|
|
|
Aliases: []string{"f"},
|
|
|
|
Target: &c.flagForce,
|
|
|
|
Default: false,
|
2017-09-08 01:59:31 +00:00
|
|
|
Usage: "Delete the lease from Vault even if the secret engine revocation " +
|
2017-09-05 04:04:05 +00:00
|
|
|
"fails. This is meant for recovery situations where the secret " +
|
2017-09-08 01:59:31 +00:00
|
|
|
"in the target secret engine was manually removed. If this flag is " +
|
|
|
|
"specified, -prefix is also required.",
|
2017-09-05 04:04:05 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
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.",
|
|
|
|
})
|
|
|
|
|
2018-07-11 19:45:09 +00:00
|
|
|
f.BoolVar(&BoolVar{
|
|
|
|
Name: "sync",
|
|
|
|
Target: &c.flagSync,
|
|
|
|
Default: false,
|
|
|
|
Usage: "Force a synchronous operation; on failure it is up to the client " +
|
|
|
|
"to retry.",
|
|
|
|
})
|
|
|
|
|
2017-09-05 04:04:05 +00:00
|
|
|
return set
|
2015-04-01 02:21:02 +00:00
|
|
|
}
|
|
|
|
|
2017-09-08 01:59:31 +00:00
|
|
|
func (c *LeaseRevokeCommand) AutocompleteArgs() complete.Predictor {
|
2017-09-05 04:04:05 +00:00
|
|
|
return c.PredictVaultFiles()
|
|
|
|
}
|
2015-04-01 02:21:02 +00:00
|
|
|
|
2017-09-08 01:59:31 +00:00
|
|
|
func (c *LeaseRevokeCommand) AutocompleteFlags() complete.Flags {
|
2017-09-05 04:04:05 +00:00
|
|
|
return c.Flags().Completions()
|
|
|
|
}
|
2015-04-01 02:21:02 +00:00
|
|
|
|
2017-09-08 01:59:31 +00:00
|
|
|
func (c *LeaseRevokeCommand) Run(args []string) int {
|
2017-09-05 04:04:05 +00:00
|
|
|
f := c.Flags()
|
Add forced revocation.
In some situations, it can be impossible to revoke leases (for instance,
if someone has gone and manually removed users created by Vault). This
can not only cause Vault to cycle trying to revoke them, but it also
prevents mounts from being unmounted, leaving them in a tainted state
where the only operations allowed are to revoke (or rollback), which
will never successfully complete.
This adds a new endpoint that works similarly to `revoke-prefix` but
ignores errors coming from a backend upon revocation (it does not ignore
errors coming from within the expiration manager, such as errors
accessing the data store). This can be used to force Vault to abandon
leases.
Like `revoke-prefix`, this is a very sensitive operation and requires
`sudo`. It is implemented as a separate endpoint, rather than an
argument to `revoke-prefix`, to ensure that control can be delegated
appropriately, as even most administrators should not normally have
this privilege.
Fixes #1135
2016-03-03 01:26:38 +00:00
|
|
|
|
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()
|
2017-09-08 01:59:31 +00:00
|
|
|
switch {
|
|
|
|
case len(args) < 1:
|
|
|
|
c.UI.Error(fmt.Sprintf("Not enough arguments (expected 1, got %d)", len(args)))
|
2017-09-05 04:04:05 +00:00
|
|
|
return 1
|
2017-09-08 01:59:31 +00:00
|
|
|
case len(args) > 1:
|
2017-09-05 04:04:05 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2017-09-08 01:59:31 +00:00
|
|
|
leaseID := strings.TrimSpace(args[0])
|
|
|
|
|
2018-07-11 19:45:09 +00:00
|
|
|
revokeOpts := &api.RevokeOptions{
|
|
|
|
LeaseID: leaseID,
|
|
|
|
Force: c.flagForce,
|
|
|
|
Prefix: c.flagPrefix,
|
|
|
|
Sync: c.flagSync,
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.flagForce {
|
2017-09-05 04:04:05 +00:00
|
|
|
c.UI.Warn(wrapAtLength("Warning! Force-removing leases can cause Vault " +
|
2017-09-08 01:59:31 +00:00
|
|
|
"to become out of sync with secret engines!"))
|
2018-07-11 19:45:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err = client.Sys().RevokeWithOptions(revokeOpts)
|
|
|
|
if err != nil {
|
|
|
|
switch {
|
|
|
|
case c.flagForce:
|
2017-09-05 04:04:05 +00:00
|
|
|
c.UI.Error(fmt.Sprintf("Error force revoking leases with prefix %s: %s", leaseID, err))
|
|
|
|
return 2
|
2018-07-11 19:45:09 +00:00
|
|
|
case c.flagPrefix:
|
2017-09-05 04:04:05 +00:00
|
|
|
c.UI.Error(fmt.Sprintf("Error revoking leases with prefix %s: %s", leaseID, err))
|
|
|
|
return 2
|
2018-07-11 19:45:09 +00:00
|
|
|
default:
|
2017-09-05 04:04:05 +00:00
|
|
|
c.UI.Error(fmt.Sprintf("Error revoking lease %s: %s", leaseID, err))
|
|
|
|
return 2
|
|
|
|
}
|
2018-07-11 19:45:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if c.flagForce {
|
|
|
|
c.UI.Output(fmt.Sprintf("Success! Force revoked any leases with prefix: %s", leaseID))
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.flagSync {
|
|
|
|
if c.flagPrefix {
|
|
|
|
c.UI.Output(fmt.Sprintf("Success! Revoked any leases with prefix: %s", leaseID))
|
|
|
|
return 0
|
|
|
|
}
|
2017-09-05 04:04:05 +00:00
|
|
|
c.UI.Output(fmt.Sprintf("Success! Revoked lease: %s", leaseID))
|
|
|
|
return 0
|
|
|
|
}
|
2018-07-11 19:45:09 +00:00
|
|
|
|
|
|
|
c.UI.Output("All revocation operations queued successfully!")
|
|
|
|
return 0
|
2015-04-01 02:21:02 +00:00
|
|
|
}
|