2016-04-01 18:23:15 +00:00
|
|
|
package command
|
|
|
|
|
2016-05-19 15:25:15 +00:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"reflect"
|
|
|
|
|
|
|
|
"github.com/hashicorp/vault/api"
|
|
|
|
"github.com/hashicorp/vault/command/token"
|
|
|
|
"github.com/mitchellh/cli"
|
|
|
|
)
|
2016-04-01 18:23:15 +00:00
|
|
|
|
|
|
|
// DefaultTokenHelper returns the token helper that is configured for Vault.
|
2016-04-01 20:02:18 +00:00
|
|
|
func DefaultTokenHelper() (token.TokenHelper, error) {
|
|
|
|
config, err := LoadConfig("")
|
2016-04-01 18:23:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
path := config.TokenHelper
|
|
|
|
if path == "" {
|
|
|
|
return &token.InternalTokenHelper{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
path, err = token.ExternalTokenHelperPath(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &token.ExternalTokenHelper{BinaryPath: path}, nil
|
|
|
|
}
|
2016-05-19 15:25:15 +00:00
|
|
|
|
|
|
|
func PrintRawField(ui cli.Ui, secret *api.Secret, field string) int {
|
|
|
|
var val interface{}
|
2016-06-28 03:19:09 +00:00
|
|
|
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]
|
2016-05-19 15:25:15 +00:00
|
|
|
}
|
2016-06-28 03:19:09 +00:00
|
|
|
|
|
|
|
case secret.WrapInfo != nil:
|
|
|
|
switch field {
|
|
|
|
case "wrapping_token":
|
|
|
|
val = secret.WrapInfo.Token
|
|
|
|
case "wrapping_token_ttl":
|
2016-05-19 15:25:15 +00:00
|
|
|
val = secret.WrapInfo.TTL
|
2016-06-28 03:19:09 +00:00
|
|
|
case "wrapping_token_creation_time":
|
2016-06-07 20:01:09 +00:00
|
|
|
val = secret.WrapInfo.CreationTime.String()
|
2016-06-28 03:19:09 +00:00
|
|
|
case "wrapped_accessor":
|
2016-06-13 23:58:17 +00:00
|
|
|
val = secret.WrapInfo.WrappedAccessor
|
2016-06-28 03:19:09 +00:00
|
|
|
default:
|
|
|
|
val = secret.Data[field]
|
2016-06-13 23:58:17 +00:00
|
|
|
}
|
2016-06-28 03:19:09 +00:00
|
|
|
|
2016-05-19 15:25:15 +00:00
|
|
|
default:
|
2016-06-28 03:19:09 +00:00
|
|
|
switch field {
|
|
|
|
case "refresh_interval":
|
|
|
|
val = secret.LeaseDuration
|
|
|
|
default:
|
|
|
|
val = secret.Data[field]
|
|
|
|
}
|
2016-05-19 15:25:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if val != nil {
|
|
|
|
// 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(ui).String() == "*cli.BasicUi" {
|
|
|
|
fmt.Fprintf(os.Stdout, fmt.Sprintf("%v", val))
|
|
|
|
} else {
|
|
|
|
ui.Output(fmt.Sprintf("%v", val))
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
} else {
|
|
|
|
ui.Error(fmt.Sprintf(
|
|
|
|
"Field %s not present in secret", field))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
}
|