mirror of
https://github.com/optim-enterprises-bv/vault.git
synced 2025-11-03 12:07:54 +00:00
command/delete
This commit is contained in:
@@ -133,14 +133,17 @@ func (c *Client) RawRequest(r *Request) (*Response, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var result *Response
|
||||||
resp, err := c.config.HttpClient.Do(req)
|
resp, err := c.config.HttpClient.Do(req)
|
||||||
|
if resp != nil {
|
||||||
|
result = &Response{Response: resp}
|
||||||
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return result, err
|
||||||
}
|
}
|
||||||
|
|
||||||
result := &Response{Response: resp}
|
|
||||||
if err := result.Error(); err != nil {
|
if err := result.Error(); err != nil {
|
||||||
return nil, err
|
return result, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return result, nil
|
return result, nil
|
||||||
|
|||||||
@@ -13,6 +13,9 @@ func (c *Client) Logical() *Logical {
|
|||||||
func (c *Logical) Read(path string) (*Secret, error) {
|
func (c *Logical) Read(path string) (*Secret, error) {
|
||||||
r := c.c.NewRequest("GET", "/v1/"+path)
|
r := c.c.NewRequest("GET", "/v1/"+path)
|
||||||
resp, err := c.c.RawRequest(r)
|
resp, err := c.c.RawRequest(r)
|
||||||
|
if resp != nil && resp.StatusCode == 404 {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -48,5 +51,9 @@ func (c *Logical) Delete(path string) (*Secret, error) {
|
|||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
|
|
||||||
return ParseSecret(resp.Body)
|
if resp.StatusCode == 200 {
|
||||||
|
return ParseSecret(resp.Body)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|||||||
79
command/delete.go
Normal file
79
command/delete.go
Normal file
@@ -0,0 +1,79 @@
|
|||||||
|
package command
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
// DeleteCommand is a Command that puts data into the Vault.
|
||||||
|
type DeleteCommand struct {
|
||||||
|
Meta
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *DeleteCommand) Run(args []string) int {
|
||||||
|
flags := c.Meta.FlagSet("delete", FlagSetDefault)
|
||||||
|
flags.Usage = func() { c.Ui.Error(c.Help()) }
|
||||||
|
if err := flags.Parse(args); err != nil {
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
|
||||||
|
args = flags.Args()
|
||||||
|
if len(args) != 1 {
|
||||||
|
c.Ui.Error("delete expects one argument")
|
||||||
|
flags.Usage()
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
|
||||||
|
path := args[0]
|
||||||
|
|
||||||
|
client, err := c.Client()
|
||||||
|
if err != nil {
|
||||||
|
c.Ui.Error(fmt.Sprintf(
|
||||||
|
"Error initializing client: %s", err))
|
||||||
|
return 2
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := client.Logical().Delete(path); err != nil {
|
||||||
|
c.Ui.Error(fmt.Sprintf(
|
||||||
|
"Error deleting '%s': %s", path, err))
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
|
||||||
|
c.Ui.Output(fmt.Sprintf("Success! Deleted '%s'", path))
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *DeleteCommand) Synopsis() string {
|
||||||
|
return "Delete operation on secrets in Vault"
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *DeleteCommand) Help() string {
|
||||||
|
helpText := `
|
||||||
|
Usage: vault delete [options] path
|
||||||
|
|
||||||
|
Delete data (secrets or configuration) from Vault.
|
||||||
|
|
||||||
|
Delete sends a delete operation request to the given path. The
|
||||||
|
behavior of the delete is determined by the backend at the given
|
||||||
|
path. For example, deleting "aws/policy/ops" will delete the "ops"
|
||||||
|
policy for the AWS backend. Use "vault help" for more details on
|
||||||
|
whether delete is supported for a path and what the behavior is.
|
||||||
|
|
||||||
|
General Options:
|
||||||
|
|
||||||
|
-address=TODO The address of the Vault server.
|
||||||
|
|
||||||
|
-ca-cert=path Path to a PEM encoded CA cert file to use to
|
||||||
|
verify the Vault server SSL certificate.
|
||||||
|
|
||||||
|
-ca-path=path Path to a directory of PEM encoded CA cert files
|
||||||
|
to verify the Vault server SSL certificate. If both
|
||||||
|
-ca-cert and -ca-path are specified, -ca-path is used.
|
||||||
|
|
||||||
|
-insecure Do not verify TLS certificate. This is highly
|
||||||
|
not recommended. This is especially not recommended
|
||||||
|
for unsealing a vault.
|
||||||
|
|
||||||
|
`
|
||||||
|
return strings.TrimSpace(helpText)
|
||||||
|
}
|
||||||
55
command/delete_test.go
Normal file
55
command/delete_test.go
Normal file
@@ -0,0 +1,55 @@
|
|||||||
|
package command
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/hashicorp/vault/http"
|
||||||
|
"github.com/hashicorp/vault/vault"
|
||||||
|
"github.com/mitchellh/cli"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestDelete(t *testing.T) {
|
||||||
|
core, _, token := vault.TestCoreUnsealed(t)
|
||||||
|
ln, addr := http.TestServer(t, core)
|
||||||
|
defer ln.Close()
|
||||||
|
|
||||||
|
ui := new(cli.MockUi)
|
||||||
|
c := &DeleteCommand{
|
||||||
|
Meta: Meta{
|
||||||
|
ClientToken: token,
|
||||||
|
Ui: ui,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
args := []string{
|
||||||
|
"-address", addr,
|
||||||
|
"secret/foo",
|
||||||
|
}
|
||||||
|
|
||||||
|
// Run once so the client is setup, ignore errors
|
||||||
|
c.Run(args)
|
||||||
|
|
||||||
|
// Get the client so we can write data
|
||||||
|
client, err := c.Client()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("err: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
data := map[string]interface{}{"value": "bar"}
|
||||||
|
if _, err := client.Logical().Write("secret/foo", data); err != nil {
|
||||||
|
t.Fatalf("err: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Run the delete
|
||||||
|
if code := c.Run(args); code != 0 {
|
||||||
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
||||||
|
}
|
||||||
|
|
||||||
|
resp, err := client.Logical().Read("secret/foo")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("err: %s", err)
|
||||||
|
}
|
||||||
|
if resp != nil {
|
||||||
|
t.Fatalf("bad: %#v", resp)
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user