Skip to content

Commit

Permalink
Enable -field in vault kv get/put
Browse files Browse the repository at this point in the history
Fixes #4424
  • Loading branch information
jefferai committed Apr 23, 2018
1 parent 43ca8d5 commit a03005d
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 8 deletions.
14 changes: 12 additions & 2 deletions command/kv_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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.
Expand Down
6 changes: 5 additions & 1 deletion command/kv_put.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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)
}
17 changes: 12 additions & 5 deletions command/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit a03005d

Please sign in to comment.