2023-03-15 19:00:52 +03:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
2024-01-02 21:36:20 +03:00
|
|
|
// SPDX-License-Identifier: BUSL-1.1
|
2023-03-15 19:00:52 +03:00
|
|
|
|
2015-04-20 02:36:11 +03:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
2016-04-01 20:16:05 +03:00
|
|
|
|
2017-09-05 07:03:04 +03:00
|
|
|
"github.com/mitchellh/cli"
|
|
|
|
"github.com/posener/complete"
|
2015-04-20 02:36:11 +03:00
|
|
|
)
|
|
|
|
|
2021-04-08 19:43:39 +03:00
|
|
|
var (
|
|
|
|
_ cli.Command = (*PolicyDeleteCommand)(nil)
|
|
|
|
_ cli.CommandAutocomplete = (*PolicyDeleteCommand)(nil)
|
|
|
|
)
|
2017-09-05 07:03:04 +03:00
|
|
|
|
2015-04-20 02:36:11 +03:00
|
|
|
type PolicyDeleteCommand struct {
|
2017-09-05 07:03:04 +03:00
|
|
|
*BaseCommand
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *PolicyDeleteCommand) Synopsis() string {
|
|
|
|
return "Deletes a policy by name"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *PolicyDeleteCommand) Help() string {
|
|
|
|
helpText := `
|
2017-09-08 05:00:21 +03:00
|
|
|
Usage: vault policy delete [options] NAME
|
2017-09-05 07:03:04 +03:00
|
|
|
|
2017-09-08 05:00:21 +03:00
|
|
|
Deletes the policy named NAME in the Vault server. Once the policy is deleted,
|
|
|
|
all tokens associated with the policy are affected immediately.
|
2017-09-05 07:03:04 +03:00
|
|
|
|
|
|
|
Delete the policy named "my-policy":
|
|
|
|
|
2017-09-08 05:00:21 +03:00
|
|
|
$ vault policy delete my-policy
|
2017-09-05 07:03:04 +03:00
|
|
|
|
2017-09-08 05:00:21 +03:00
|
|
|
Note that it is not possible to delete the "default" or "root" policies.
|
|
|
|
These are built-in policies.
|
2017-09-05 07:03:04 +03:00
|
|
|
|
|
|
|
` + c.Flags().Help()
|
|
|
|
|
|
|
|
return strings.TrimSpace(helpText)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *PolicyDeleteCommand) Flags() *FlagSets {
|
|
|
|
return c.flagSet(FlagSetHTTP)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *PolicyDeleteCommand) AutocompleteArgs() complete.Predictor {
|
|
|
|
return c.PredictVaultPolicies()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *PolicyDeleteCommand) AutocompleteFlags() complete.Flags {
|
|
|
|
return c.Flags().Completions()
|
2015-04-20 02:36:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *PolicyDeleteCommand) Run(args []string) int {
|
2017-09-05 07:03:04 +03:00
|
|
|
f := c.Flags()
|
|
|
|
|
|
|
|
if err := f.Parse(args); err != nil {
|
|
|
|
c.UI.Error(err.Error())
|
2015-04-20 02:36:11 +03:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2017-09-05 07:03:04 +03:00
|
|
|
args = f.Args()
|
|
|
|
switch {
|
|
|
|
case len(args) < 1:
|
|
|
|
c.UI.Error(fmt.Sprintf("Not enough arguments (expected 1, got %d)", len(args)))
|
|
|
|
return 1
|
|
|
|
case len(args) > 1:
|
|
|
|
c.UI.Error(fmt.Sprintf("Too many arguments (expected 1, got %d)", len(args)))
|
2015-04-20 02:36:11 +03:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
client, err := c.Client()
|
|
|
|
if err != nil {
|
2017-09-05 07:03:04 +03:00
|
|
|
c.UI.Error(err.Error())
|
2015-04-20 02:36:11 +03:00
|
|
|
return 2
|
|
|
|
}
|
|
|
|
|
2017-09-05 07:03:04 +03:00
|
|
|
name := strings.TrimSpace(strings.ToLower(args[0]))
|
2015-04-20 02:36:11 +03:00
|
|
|
if err := client.Sys().DeletePolicy(name); err != nil {
|
2017-09-05 07:03:04 +03:00
|
|
|
c.UI.Error(fmt.Sprintf("Error deleting %s: %s", name, err))
|
|
|
|
return 2
|
2015-04-20 02:36:11 +03:00
|
|
|
}
|
|
|
|
|
2017-09-05 07:03:04 +03:00
|
|
|
c.UI.Output(fmt.Sprintf("Success! Deleted policy: %s", name))
|
2015-04-20 02:36:11 +03:00
|
|
|
return 0
|
|
|
|
}
|