2015-04-07 18:15:20 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2019-07-18 17:42:36 +00:00
|
|
|
"io"
|
|
|
|
"os"
|
2015-04-07 18:15:20 +00:00
|
|
|
"strings"
|
2016-04-01 17:16:05 +00:00
|
|
|
|
2017-09-05 04:00:06 +00:00
|
|
|
"github.com/mitchellh/cli"
|
|
|
|
"github.com/posener/complete"
|
2015-04-07 18:15:20 +00:00
|
|
|
)
|
|
|
|
|
2021-04-08 16:43:39 +00:00
|
|
|
var (
|
|
|
|
_ cli.Command = (*DeleteCommand)(nil)
|
|
|
|
_ cli.CommandAutocomplete = (*DeleteCommand)(nil)
|
|
|
|
)
|
2017-09-05 04:00:06 +00:00
|
|
|
|
2015-04-07 18:15:20 +00:00
|
|
|
type DeleteCommand struct {
|
2017-09-05 04:00:06 +00:00
|
|
|
*BaseCommand
|
2019-07-18 17:42:36 +00:00
|
|
|
|
|
|
|
testStdin io.Reader // for tests
|
2017-09-05 04:00:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *DeleteCommand) Synopsis() string {
|
2017-09-08 01:59:06 +00:00
|
|
|
return "Delete secrets and configuration"
|
2017-09-05 04:00:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *DeleteCommand) Help() string {
|
|
|
|
helpText := `
|
|
|
|
Usage: vault delete [options] PATH
|
|
|
|
|
|
|
|
Deletes secrets and configuration from Vault at the given path. The behavior
|
|
|
|
of "delete" is delegated to the backend corresponding to the given path.
|
|
|
|
|
|
|
|
Remove data in the status secret backend:
|
|
|
|
|
|
|
|
$ vault delete secret/my-secret
|
|
|
|
|
|
|
|
Uninstall an encryption key in the transit backend:
|
|
|
|
|
|
|
|
$ vault delete transit/keys/my-key
|
|
|
|
|
|
|
|
Delete an IAM role:
|
|
|
|
|
|
|
|
$ vault delete aws/roles/ops
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *DeleteCommand) Flags() *FlagSets {
|
|
|
|
return c.flagSet(FlagSetHTTP)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *DeleteCommand) AutocompleteArgs() complete.Predictor {
|
|
|
|
return c.PredictVaultFiles()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *DeleteCommand) AutocompleteFlags() complete.Flags {
|
|
|
|
return c.Flags().Completions()
|
2015-04-07 18:15:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *DeleteCommand) Run(args []string) int {
|
2017-09-05 04:00:06 +00:00
|
|
|
f := c.Flags()
|
|
|
|
|
|
|
|
if err := f.Parse(args); err != nil {
|
|
|
|
c.UI.Error(err.Error())
|
2015-04-07 18:15:20 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2017-09-05 04:00:06 +00:00
|
|
|
args = f.Args()
|
2017-09-08 01:59:06 +00:00
|
|
|
switch {
|
|
|
|
case len(args) < 1:
|
2019-07-18 17:42:36 +00:00
|
|
|
c.UI.Error(fmt.Sprintf("Not enough arguments (expected at least 1, got %d)", len(args)))
|
2017-09-05 04:00:06 +00:00
|
|
|
return 1
|
|
|
|
}
|
2015-04-07 18:15:20 +00:00
|
|
|
|
|
|
|
client, err := c.Client()
|
|
|
|
if err != nil {
|
2017-09-05 04:00:06 +00:00
|
|
|
c.UI.Error(err.Error())
|
2015-04-07 18:15:20 +00:00
|
|
|
return 2
|
|
|
|
}
|
|
|
|
|
2019-07-18 17:42:36 +00:00
|
|
|
// Pull our fake stdin if needed
|
|
|
|
stdin := (io.Reader)(os.Stdin)
|
|
|
|
if c.testStdin != nil {
|
|
|
|
stdin = c.testStdin
|
|
|
|
}
|
|
|
|
|
2017-09-08 01:59:06 +00:00
|
|
|
path := sanitizePath(args[0])
|
|
|
|
|
2019-07-18 17:42:36 +00:00
|
|
|
data, err := parseArgsDataStringLists(stdin, args[1:])
|
|
|
|
if err != nil {
|
|
|
|
c.UI.Error(fmt.Sprintf("Failed to parse K=V data: %s", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
secret, err := client.Logical().DeleteWithData(path, data)
|
2018-04-04 08:41:46 +00:00
|
|
|
if err != nil {
|
2017-09-05 04:00:06 +00:00
|
|
|
c.UI.Error(fmt.Sprintf("Error deleting %s: %s", path, err))
|
2018-04-04 08:41:46 +00:00
|
|
|
if secret != nil {
|
|
|
|
OutputSecret(c.UI, secret)
|
|
|
|
}
|
2017-09-05 04:00:06 +00:00
|
|
|
return 2
|
2015-04-07 18:15:20 +00:00
|
|
|
}
|
|
|
|
|
2017-09-05 04:00:06 +00:00
|
|
|
c.UI.Info(fmt.Sprintf("Success! Data deleted (if it existed) at: %s", path))
|
2015-04-07 18:15:20 +00:00
|
|
|
return 0
|
|
|
|
}
|