Skip to content

Commit

Permalink
Enable -field in vault kv get/put (#4426)
Browse files Browse the repository at this point in the history
* Enable `-field` in `vault kv get/put`

Fixes #4424

* Unify nil value handling

* Use preflight helper
  • Loading branch information
jefferai authored and briankassouf committed Apr 23, 2018
1 parent c55797f commit eec334c
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 @@ -124,7 +124,17 @@ func (c *KVGetCommand) Run(args []string) int {
}

if c.flagField != "" {
return PrintRawField(c.UI, secret, c.flagField)
if v2 {
// 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 @@ -152,5 +152,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 @@ -20,7 +20,7 @@ func DefaultTokenHelper() (token.TokenHelper, error) {

// RawField extracts the raw field from the given data and returns it as a
// string for printing purposes.
func RawField(secret *api.Secret, field string) (interface{}, bool) {
func RawField(secret *api.Secret, field string) interface{} {
var val interface{}
switch {
case secret.Auth != nil:
Expand Down Expand Up @@ -72,13 +72,20 @@ func RawField(secret *api.Secret, field string) (interface{}, bool) {
}
}

return val, val != nil
return val
}

// 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 {
func PrintRawField(ui cli.Ui, data interface{}, field string) int {
var val interface{}
switch data.(type) {
case *api.Secret:
val = RawField(data.(*api.Secret), field)
case map[string]interface{}:
val = data.(map[string]interface{})[field]
}

if val == nil {
ui.Error(fmt.Sprintf("Field %q not present in secret", field))
return 1
}
Expand Down

0 comments on commit eec334c

Please sign in to comment.