mirror of
https://github.com/optim-enterprises-bv/vault.git
synced 2025-11-01 02:57:59 +00:00
Add aliases for field flag to allow printing auth results.
Also fix the write command to use the shared function with aliases. Fixes #1566
This commit is contained in:
@@ -31,28 +31,45 @@ func DefaultTokenHelper() (token.TokenHelper, error) {
|
|||||||
|
|
||||||
func PrintRawField(ui cli.Ui, secret *api.Secret, field string) int {
|
func PrintRawField(ui cli.Ui, secret *api.Secret, field string) int {
|
||||||
var val interface{}
|
var val interface{}
|
||||||
|
switch {
|
||||||
|
case secret.Auth != nil:
|
||||||
|
switch field {
|
||||||
|
case "token":
|
||||||
|
val = secret.Auth.ClientToken
|
||||||
|
case "token_accessor":
|
||||||
|
val = secret.Auth.Accessor
|
||||||
|
case "token_duration":
|
||||||
|
val = secret.Auth.LeaseDuration
|
||||||
|
case "token_renewable":
|
||||||
|
val = secret.Auth.Renewable
|
||||||
|
case "token_policies":
|
||||||
|
val = secret.Auth.Policies
|
||||||
|
default:
|
||||||
|
val = secret.Data[field]
|
||||||
|
}
|
||||||
|
|
||||||
|
case secret.WrapInfo != nil:
|
||||||
switch field {
|
switch field {
|
||||||
case "wrapping_token":
|
case "wrapping_token":
|
||||||
if secret.WrapInfo != nil {
|
|
||||||
val = secret.WrapInfo.Token
|
val = secret.WrapInfo.Token
|
||||||
}
|
|
||||||
case "wrapping_token_ttl":
|
case "wrapping_token_ttl":
|
||||||
if secret.WrapInfo != nil {
|
|
||||||
val = secret.WrapInfo.TTL
|
val = secret.WrapInfo.TTL
|
||||||
}
|
|
||||||
case "wrapping_token_creation_time":
|
case "wrapping_token_creation_time":
|
||||||
if secret.WrapInfo != nil {
|
|
||||||
val = secret.WrapInfo.CreationTime.String()
|
val = secret.WrapInfo.CreationTime.String()
|
||||||
}
|
|
||||||
case "wrapped_accessor":
|
case "wrapped_accessor":
|
||||||
if secret.WrapInfo != nil {
|
|
||||||
val = secret.WrapInfo.WrappedAccessor
|
val = secret.WrapInfo.WrappedAccessor
|
||||||
|
default:
|
||||||
|
val = secret.Data[field]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
default:
|
||||||
|
switch field {
|
||||||
case "refresh_interval":
|
case "refresh_interval":
|
||||||
val = secret.LeaseDuration
|
val = secret.LeaseDuration
|
||||||
default:
|
default:
|
||||||
val = secret.Data[field]
|
val = secret.Data[field]
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if val != nil {
|
if val != nil {
|
||||||
// c.Ui.Output() prints a CR character which in this case is
|
// c.Ui.Output() prints a CR character which in this case is
|
||||||
|
|||||||
@@ -4,7 +4,6 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"reflect"
|
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/hashicorp/vault/helper/kv-builder"
|
"github.com/hashicorp/vault/helper/kv-builder"
|
||||||
@@ -75,23 +74,7 @@ func (c *WriteCommand) Run(args []string) int {
|
|||||||
|
|
||||||
// Handle single field output
|
// Handle single field output
|
||||||
if field != "" {
|
if field != "" {
|
||||||
if val, ok := secret.Data[field]; ok {
|
return PrintRawField(c.Ui, secret, field)
|
||||||
// c.Ui.Output() prints a CR character which in this case is
|
|
||||||
// not desired. Since Vault CLI currently only uses BasicUi,
|
|
||||||
// which writes to standard output, os.Stdout is used here to
|
|
||||||
// directly print the message. If mitchellh/cli exposes method
|
|
||||||
// to print without CR, this check needs to be removed.
|
|
||||||
if reflect.TypeOf(c.Ui).String() == "*cli.BasicUi" {
|
|
||||||
fmt.Fprintf(os.Stdout, fmt.Sprintf("%v", val))
|
|
||||||
} else {
|
|
||||||
c.Ui.Output(fmt.Sprintf("%v", val))
|
|
||||||
}
|
|
||||||
return 0
|
|
||||||
} else {
|
|
||||||
c.Ui.Error(fmt.Sprintf(
|
|
||||||
"Field %s not present in secret", field))
|
|
||||||
return 1
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return OutputSecret(c.Ui, format, secret)
|
return OutputSecret(c.Ui, format, secret)
|
||||||
|
|||||||
Reference in New Issue
Block a user