diff --git a/command/kv_get.go b/command/kv_get.go index 88819951b8b5..3e84730b8b6e 100644 --- a/command/kv_get.go +++ b/command/kv_get.go @@ -43,7 +43,7 @@ Usage: vault kv get [options] KEY } func (c *KVGetCommand) Flags() *FlagSets { - set := c.flagSet(FlagSetHTTP | FlagSetOutputFormat) + set := c.flagSet(FlagSetHTTP | FlagSetOutputField | FlagSetOutputFormat) // Common Options f := set.NewFlagSet("Common Options") @@ -115,7 +115,17 @@ func (c *KVGetCommand) Run(args []string) int { } if c.flagField != "" { - return PrintRawField(c.UI, secret, c.flagField) + if metadata, ok := secret.Data["metadata"]; ok && metadata != nil { + // This is a v2, pass in the data field + if data, ok := secret.Data["data"]; ok && data != nil { + return PrintRawField(c.UI, data, c.flagField) + } else { + c.UI.Error(fmt.Sprintf("No data found at %s", path)) + return 2 + } + } else { + return PrintRawField(c.UI, secret, c.flagField) + } } // If we have wrap info print the secret normally. diff --git a/command/kv_put.go b/command/kv_put.go index ca981f885822..137f62eb7241 100644 --- a/command/kv_put.go +++ b/command/kv_put.go @@ -55,7 +55,7 @@ Usage: vault kv put [options] KEY [DATA] } func (c *KVPutCommand) Flags() *FlagSets { - set := c.flagSet(FlagSetHTTP | FlagSetOutputFormat) + set := c.flagSet(FlagSetHTTP | FlagSetOutputField | FlagSetOutputFormat) // Common Options f := set.NewFlagSet("Common Options") @@ -144,5 +144,9 @@ func (c *KVPutCommand) Run(args []string) int { return 0 } + if c.flagField != "" { + return PrintRawField(c.UI, secret, c.flagField) + } + return OutputSecret(c.UI, secret) } diff --git a/command/util.go b/command/util.go index b418ce7fc8ec..6298f89f6900 100644 --- a/command/util.go +++ b/command/util.go @@ -76,11 +76,18 @@ func RawField(secret *api.Secret, field string) (interface{}, bool) { } // PrintRawField prints raw field from the secret. -func PrintRawField(ui cli.Ui, secret *api.Secret, field string) int { - val, ok := RawField(secret, field) - if !ok { - ui.Error(fmt.Sprintf("Field %q not present in secret", field)) - return 1 +func PrintRawField(ui cli.Ui, data interface{}, field string) int { + var val interface{} + switch data.(type) { + case *api.Secret: + var ok bool + val, ok = RawField(data.(*api.Secret), field) + if !ok { + ui.Error(fmt.Sprintf("Field %q not present in secret", field)) + return 1 + } + case map[string]interface{}: + val = data.(map[string]interface{})[field] } format := Format(ui)