Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Output full secret path in certain kv commands #14301

Merged
merged 10 commits into from
Mar 8, 2022
Merged
3 changes: 3 additions & 0 deletions changelog/14301.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:improvement
secrets/kv: add full secret path output to table-formatted responses
```
4 changes: 4 additions & 0 deletions command/kv_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,10 @@ func (c *KVGetCommand) Run(args []string) int {
tf.printWarnings(c.UI, secret)
}

if v2 {
outputPath(c.UI, path, "Secret Path")
}

if metadata, ok := secret.Data["metadata"]; ok && metadata != nil {
c.UI.Info(getHeaderForMap("Metadata", metadata.(map[string]interface{})))
OutputData(c.UI, metadata)
Expand Down
30 changes: 21 additions & 9 deletions command/kv_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/hashicorp/go-secure-stdlib/strutil"
"github.com/hashicorp/vault/api"
"github.com/mitchellh/cli"
)

func kvReadRequest(client *api.Client, path string, params map[string]string) (*api.Secret, error) {
Expand Down Expand Up @@ -141,6 +142,26 @@ func getHeaderForMap(header string, data map[string]interface{}) string {
// 4 for the column spaces and 5 for the len("value")
totalLen := maxKey + 4 + 5

return padEqualSigns(header, totalLen)
}

func kvParseVersionsFlags(versions []string) []string {
versionsOut := make([]string, 0, len(versions))
for _, v := range versions {
versionsOut = append(versionsOut, strutil.ParseStringSlice(v, ",")...)
}

return versionsOut
}

func outputPath(ui cli.Ui, path string, title string) {
ui.Info(padEqualSigns(title, len(path)))
ui.Info(path)
ui.Info("")
}

// Pad the table header with equal signs on each side
func padEqualSigns(header string, totalLen int) string {
equalSigns := totalLen - (len(header) + 2)

// If we have zero or fewer equal signs bump it back up to two on either
Expand All @@ -156,12 +177,3 @@ func getHeaderForMap(header string, data map[string]interface{}) string {

return fmt.Sprintf("%s %s %s", strings.Repeat("=", equalSigns/2), header, strings.Repeat("=", equalSigns/2))
}

func kvParseVersionsFlags(versions []string) []string {
versionsOut := make([]string, 0, len(versions))
for _, v := range versions {
versionsOut = append(versionsOut, strutil.ParseStringSlice(v, ",")...)
}

return versionsOut
}
2 changes: 2 additions & 0 deletions command/kv_metadata_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ func (c *KVMetadataGetCommand) Run(args []string) int {

delete(secret.Data, "versions")

outputPath(c.UI, path, "Metadata Path")

c.UI.Info(getHeaderForMap("Metadata", secret.Data))
OutputSecret(c.UI, secret)

Expand Down
12 changes: 11 additions & 1 deletion command/kv_patch.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,17 @@ func (c *KVPatchCommand) Run(args []string) int {
return code
}

return OutputSecret(c.UI, secret)
if Format(c.UI) != "table" {
return OutputSecret(c.UI, secret)
digivava marked this conversation as resolved.
Show resolved Hide resolved
}

outputPath(c.UI, path, "Secret Path")

metadata := secret.Data
c.UI.Info(getHeaderForMap("Metadata", metadata))
OutputData(c.UI, metadata)

return 0
}

func (c *KVPatchCommand) readThenWrite(client *api.Client, path string, newData map[string]interface{}) (*api.Secret, int) {
Expand Down
12 changes: 11 additions & 1 deletion command/kv_put.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,5 +161,15 @@ func (c *KVPutCommand) Run(args []string) int {
return PrintRawField(c.UI, secret, c.flagField)
}

return OutputSecret(c.UI, secret)
if Format(c.UI) != "table" {
return OutputSecret(c.UI, secret)
}

outputPath(c.UI, path, "Secret Path")

metadata := secret.Data
c.UI.Info(getHeaderForMap("Metadata", metadata))
OutputData(c.UI, metadata)

return 0
digivava marked this conversation as resolved.
Show resolved Hide resolved
}