2016-04-01 18:23:15 +00:00
|
|
|
package command
|
|
|
|
|
2016-05-19 15:25:15 +00:00
|
|
|
import (
|
|
|
|
"fmt"
|
2017-09-03 01:05:13 +00:00
|
|
|
"io"
|
2016-05-19 15:25:15 +00:00
|
|
|
"os"
|
2016-09-29 04:01:28 +00:00
|
|
|
"time"
|
2016-05-19 15:25:15 +00:00
|
|
|
|
2018-04-09 20:18:17 +00:00
|
|
|
"github.com/fatih/color"
|
2016-05-19 15:25:15 +00:00
|
|
|
"github.com/hashicorp/vault/api"
|
2018-03-01 01:09:21 +00:00
|
|
|
"github.com/hashicorp/vault/command/config"
|
2016-05-19 15:25:15 +00:00
|
|
|
"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) {
|
2018-03-01 01:09:21 +00:00
|
|
|
return config.DefaultTokenHelper()
|
2016-04-01 18:23:15 +00:00
|
|
|
}
|
2016-05-19 15:25:15 +00:00
|
|
|
|
2017-08-28 20:45:39 +00:00
|
|
|
// RawField extracts the raw field from the given data and returns it as a
|
|
|
|
// string for printing purposes.
|
2018-04-23 22:00:02 +00:00
|
|
|
func RawField(secret *api.Secret, field string) interface{} {
|
2016-05-19 15:25:15 +00:00
|
|
|
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":
|
2018-06-14 13:49:33 +00:00
|
|
|
val = secret.Auth.TokenPolicies
|
|
|
|
case "identity_policies":
|
|
|
|
val = secret.Auth.IdentityPolicies
|
|
|
|
case "policies":
|
2016-06-28 03:19:09 +00:00
|
|
|
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
|
2017-11-13 20:31:32 +00:00
|
|
|
case "wrapping_accessor":
|
|
|
|
val = secret.WrapInfo.Accessor
|
2016-06-28 03:19:09 +00:00
|
|
|
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-09-29 04:01:28 +00:00
|
|
|
val = secret.WrapInfo.CreationTime.Format(time.RFC3339Nano)
|
2017-08-02 22:28:58 +00:00
|
|
|
case "wrapping_token_creation_path":
|
|
|
|
val = secret.WrapInfo.CreationPath
|
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 {
|
2018-07-11 19:09:04 +00:00
|
|
|
case "lease_duration":
|
|
|
|
val = secret.LeaseDuration
|
|
|
|
case "lease_id":
|
|
|
|
val = secret.LeaseID
|
|
|
|
case "request_id":
|
|
|
|
val = secret.RequestID
|
|
|
|
case "renewable":
|
|
|
|
val = secret.Renewable
|
2016-06-28 03:19:09 +00:00
|
|
|
case "refresh_interval":
|
|
|
|
val = secret.LeaseDuration
|
2018-02-15 14:11:56 +00:00
|
|
|
case "data":
|
|
|
|
var ok bool
|
|
|
|
val, ok = secret.Data["data"]
|
|
|
|
if !ok {
|
|
|
|
val = secret.Data
|
|
|
|
}
|
2016-06-28 03:19:09 +00:00
|
|
|
default:
|
|
|
|
val = secret.Data[field]
|
|
|
|
}
|
2016-05-19 15:25:15 +00:00
|
|
|
}
|
|
|
|
|
2018-04-23 22:00:02 +00:00
|
|
|
return val
|
2017-08-28 20:45:39 +00:00
|
|
|
}
|
|
|
|
|
2017-09-05 03:56:58 +00:00
|
|
|
// PrintRawField prints raw field from the secret.
|
2018-04-23 22:00:02 +00:00
|
|
|
func PrintRawField(ui cli.Ui, data interface{}, field string) int {
|
|
|
|
var val interface{}
|
2021-09-30 11:33:14 +00:00
|
|
|
switch data := data.(type) {
|
2018-04-23 22:00:02 +00:00
|
|
|
case *api.Secret:
|
2021-09-30 11:33:14 +00:00
|
|
|
val = RawField(data, field)
|
2018-04-23 22:00:02 +00:00
|
|
|
case map[string]interface{}:
|
2021-09-30 11:33:14 +00:00
|
|
|
val = data[field]
|
2018-04-23 22:00:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if val == nil {
|
2017-09-03 01:05:13 +00:00
|
|
|
ui.Error(fmt.Sprintf("Field %q not present in secret", field))
|
2016-05-19 15:25:15 +00:00
|
|
|
return 1
|
|
|
|
}
|
2017-08-28 20:45:39 +00:00
|
|
|
|
2018-02-15 14:11:56 +00:00
|
|
|
format := Format(ui)
|
|
|
|
if format == "" || format == "table" {
|
|
|
|
return PrintRaw(ui, fmt.Sprintf("%v", val))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle specific format flags as best as possible
|
|
|
|
formatter, ok := Formatters[format]
|
|
|
|
if !ok {
|
|
|
|
ui.Error(fmt.Sprintf("Invalid output format: %s", format))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
b, err := formatter.Format(val)
|
|
|
|
if err != nil {
|
|
|
|
ui.Error(fmt.Sprintf("Error formatting output: %s", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
return PrintRaw(ui, string(b))
|
2017-09-05 03:56:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// PrintRaw prints a raw value to the terminal. If the process is being "piped"
|
|
|
|
// to something else, the "raw" value is printed without a newline character.
|
|
|
|
// Otherwise the value is printed as normal.
|
|
|
|
func PrintRaw(ui cli.Ui, str string) int {
|
2018-04-09 20:18:17 +00:00
|
|
|
if !color.NoColor {
|
2017-09-05 03:56:58 +00:00
|
|
|
ui.Output(str)
|
|
|
|
} else {
|
|
|
|
// The cli.Ui prints a CR, which is not wanted since the user probably wants
|
|
|
|
// just the raw value.
|
|
|
|
w := getWriterFromUI(ui)
|
2018-02-19 14:29:45 +00:00
|
|
|
fmt.Fprint(w, str)
|
2017-09-05 03:56:58 +00:00
|
|
|
}
|
2017-08-28 20:45:39 +00:00
|
|
|
return 0
|
2016-05-19 15:25:15 +00:00
|
|
|
}
|
2017-09-03 01:05:13 +00:00
|
|
|
|
|
|
|
// getWriterFromUI accepts a cli.Ui and returns the underlying io.Writer by
|
|
|
|
// unwrapping as many wrapped Uis as necessary. If there is an unknown UI
|
|
|
|
// type, this falls back to os.Stdout.
|
|
|
|
func getWriterFromUI(ui cli.Ui) io.Writer {
|
|
|
|
switch t := ui.(type) {
|
2018-02-12 23:12:16 +00:00
|
|
|
case *VaultUI:
|
|
|
|
return getWriterFromUI(t.Ui)
|
2017-09-03 01:05:13 +00:00
|
|
|
case *cli.BasicUi:
|
|
|
|
return t.Writer
|
|
|
|
case *cli.ColoredUi:
|
|
|
|
return getWriterFromUI(t.Ui)
|
|
|
|
case *cli.ConcurrentUi:
|
|
|
|
return getWriterFromUI(t.Ui)
|
|
|
|
case *cli.MockUi:
|
|
|
|
return t.OutputWriter
|
|
|
|
default:
|
|
|
|
return os.Stdout
|
|
|
|
}
|
|
|
|
}
|