Skip to content

Commit

Permalink
Fix writing to KVv2 root via kv put (#4726)
Browse files Browse the repository at this point in the history
* Fix writing to KVv2 root via `kv put`

The check that adds the API path wasn't taking into account the root,
e.g. if it's mounted at `kv`, `kv` and `kv/` would end up creating an
extra copy of the mount path in front, leading to paths like
`kv/data/kv`.

* Output warnings if they come back and fix a panic in metadata_get

* Also add to metadata put/delete
  • Loading branch information
jefferai authored Jun 8, 2018
1 parent 3a2ab5a commit e15501e
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 12 deletions.
21 changes: 13 additions & 8 deletions command/kv_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,44 +101,49 @@ func (c *KVDeleteCommand) Run(args []string) int {
return 2
}

var secret *api.Secret
if v2 {
err = c.deleteV2(path, mountPath, client)
secret, err = c.deleteV2(path, mountPath, client)
} else {
_, err = client.Logical().Delete(path)
secret, err = client.Logical().Delete(path)
}

if err != nil {
c.UI.Error(fmt.Sprintf("Error deleting %s: %s", path, err))
if secret != nil {
OutputSecret(c.UI, secret)
}
return 2
}

c.UI.Info(fmt.Sprintf("Success! Data deleted (if it existed) at: %s", path))
return 0
}

func (c *KVDeleteCommand) deleteV2(path, mountPath string, client *api.Client) error {
func (c *KVDeleteCommand) deleteV2(path, mountPath string, client *api.Client) (*api.Secret, error) {
var err error
var secret *api.Secret
switch {
case len(c.flagVersions) > 0:
path = addPrefixToVKVPath(path, mountPath, "delete")
if err != nil {
return err
return nil, err
}

data := map[string]interface{}{
"versions": kvParseVersionsFlags(c.flagVersions),
}

_, err = client.Logical().Write(path, data)
secret, err = client.Logical().Write(path, data)
default:

path = addPrefixToVKVPath(path, mountPath, "data")
if err != nil {
return err
return nil, err
}

_, err = client.Logical().Delete(path)
secret, err = client.Logical().Delete(path)
}

return err
return secret, err
}
3 changes: 3 additions & 0 deletions command/kv_destroy.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,9 @@ func (c *KVDestroyCommand) Run(args []string) int {
secret, err := client.Logical().Write(path, data)
if err != nil {
c.UI.Error(fmt.Sprintf("Error writing data to %s: %s", path, err))
if secret != nil {
OutputSecret(c.UI, secret)
}
return 2
}
if secret == nil {
Expand Down
9 changes: 7 additions & 2 deletions command/kv_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,13 @@ func isKVv2(path string, client *api.Client) (string, bool, error) {
}

func addPrefixToVKVPath(p, mountPath, apiPrefix string) string {
p = strings.TrimPrefix(p, mountPath)
return path.Join(mountPath, apiPrefix, p)
switch {
case p == mountPath, p == strings.TrimSuffix(mountPath, "/"):
return path.Join(mountPath, apiPrefix)
default:
p = strings.TrimPrefix(p, mountPath)
return path.Join(mountPath, apiPrefix, p)
}
}

func getHeaderForMap(header string, data map[string]interface{}) string {
Expand Down
5 changes: 4 additions & 1 deletion command/kv_metadata_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,11 @@ func (c *KVMetadataDeleteCommand) Run(args []string) int {
}

path = addPrefixToVKVPath(path, mountPath, "metadata")
if _, err := client.Logical().Delete(path); err != nil {
if secret, err := client.Logical().Delete(path); err != nil {
c.UI.Error(fmt.Sprintf("Error deleting %s: %s", path, err))
if secret != nil {
OutputSecret(c.UI, secret)
}
return 2
}

Expand Down
8 changes: 7 additions & 1 deletion command/kv_metadata_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,13 @@ func (c *KVMetadataGetCommand) Run(args []string) int {
return OutputSecret(c.UI, secret)
}

versions := secret.Data["versions"].(map[string]interface{})
versionsRaw, ok := secret.Data["versions"]
if !ok || versionsRaw == nil {
c.UI.Error(fmt.Sprintf("No value found at %s", path))
OutputSecret(c.UI, secret)
return 2
}
versions := versionsRaw.(map[string]interface{})

delete(secret.Data, "versions")

Expand Down
3 changes: 3 additions & 0 deletions command/kv_metadata_put.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,9 @@ func (c *KVMetadataPutCommand) Run(args []string) int {
secret, err := client.Logical().Write(path, data)
if err != nil {
c.UI.Error(fmt.Sprintf("Error writing data to %s: %s", path, err))
if secret != nil {
OutputSecret(c.UI, secret)
}
return 2
}
if secret == nil {
Expand Down
3 changes: 3 additions & 0 deletions command/kv_undelete.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@ func (c *KVUndeleteCommand) Run(args []string) int {
secret, err := client.Logical().Write(path, data)
if err != nil {
c.UI.Error(fmt.Sprintf("Error writing data to %s: %s", path, err))
if secret != nil {
OutputSecret(c.UI, secret)
}
return 2
}
if secret == nil {
Expand Down

0 comments on commit e15501e

Please sign in to comment.