open-vault/command/util.go

152 lines
3.6 KiB
Go
Raw Normal View History

2016-04-01 18:23:15 +00:00
package command
import (
"fmt"
"io"
"os"
2016-09-29 04:01:28 +00:00
"time"
"github.com/fatih/color"
"github.com/hashicorp/vault/api"
"github.com/hashicorp/vault/command/config"
"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.
func DefaultTokenHelper() (token.TokenHelper, error) {
return config.DefaultTokenHelper()
2016-04-01 18:23: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.
func RawField(secret *api.Secret, field string) 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.TokenPolicies
case "identity_policies":
val = secret.Auth.IdentityPolicies
case "policies":
val = secret.Auth.Policies
default:
val = secret.Data[field]
}
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
case "wrapping_token_ttl":
val = secret.WrapInfo.TTL
case "wrapping_token_creation_time":
2016-09-29 04:01:28 +00:00
val = secret.WrapInfo.CreationTime.Format(time.RFC3339Nano)
case "wrapping_token_creation_path":
val = secret.WrapInfo.CreationPath
case "wrapped_accessor":
val = secret.WrapInfo.WrappedAccessor
default:
val = secret.Data[field]
}
default:
switch field {
case "refresh_interval":
val = secret.LeaseDuration
case "data":
var ok bool
val, ok = secret.Data["data"]
if !ok {
val = secret.Data
}
default:
val = secret.Data[field]
}
}
return val
2017-08-28 20:45:39 +00:00
}
// PrintRawField prints raw field from the secret.
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
}
2017-08-28 20:45:39 +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))
}
// 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 {
if !color.NoColor {
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)
fmt.Fprint(w, str)
}
2017-08-28 20:45:39 +00:00
return 0
}
// 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) {
CLI Enhancements (#3897) * Use Colored UI if stdout is a tty * Add format options to operator unseal * Add format test on operator unseal * Add -no-color output flag, and use BasicUi if no-color flag is provided * Move seal status formatting logic to OutputSealStatus * Apply no-color to warnings from DeprecatedCommands as well * Add OutputWithFormat to support arbitrary data, add format option to auth list * Add ability to output arbitrary list data on TableFormatter * Clear up switch logic on format * Add format option for list-related commands * Add format option to rest of commands that returns a client API response * Remove initOutputYAML and initOutputJSON, and use OutputWithFormat instead * Remove outputAsYAML and outputAsJSON, and use OutputWithFormat instead * Remove -no-color flag, use env var exclusively to toggle colored output * Fix compile * Remove -no-color flag in main.go * Add missing FlagSetOutputFormat * Fix generate-root/decode test * Migrate init functions to main.go * Add no-color flag back as hidden * Handle non-supported data types for TableFormatter.OutputList * Pull formatting much further up to remove the need to use c.flagFormat (#3950) * Pull formatting much further up to remove the need to use c.flagFormat Also remove OutputWithFormat as the logic can cause issues. * Use const for env var * Minor updates * Remove unnecessary check * Fix SSH output and some tests * Fix tests * Make race detector not run on generate root since it kills Travis these days * Update docs * Update docs * Address review feedback * Handle --format as well as -format
2018-02-12 23:12:16 +00:00
case *VaultUI:
return getWriterFromUI(t.Ui)
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
}
}