2015-04-27 21:55:29 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2016-02-12 18:21:42 +00:00
|
|
|
"errors"
|
2015-04-27 21:55:29 +00:00
|
|
|
"fmt"
|
2018-02-12 23:12:16 +00:00
|
|
|
"os"
|
2015-12-10 09:01:22 +00:00
|
|
|
"sort"
|
2015-12-10 10:32:31 +00:00
|
|
|
"strings"
|
2015-04-27 21:55:29 +00:00
|
|
|
|
2015-12-10 10:32:31 +00:00
|
|
|
"github.com/ghodss/yaml"
|
2015-04-27 21:55:29 +00:00
|
|
|
"github.com/hashicorp/vault/api"
|
|
|
|
"github.com/mitchellh/cli"
|
|
|
|
"github.com/ryanuber/columnize"
|
|
|
|
)
|
|
|
|
|
2017-09-08 02:04:48 +00:00
|
|
|
const (
|
|
|
|
// hopeDelim is the delimiter to use when splitting columns. We call it a
|
|
|
|
// hopeDelim because we hope that it's never contained in a secret.
|
|
|
|
hopeDelim = "♨"
|
|
|
|
)
|
2017-08-24 22:23:40 +00:00
|
|
|
|
2018-02-12 23:12:16 +00:00
|
|
|
type FormatOptions struct {
|
|
|
|
Format string
|
2015-04-27 21:55:29 +00:00
|
|
|
}
|
|
|
|
|
2018-02-12 23:12:16 +00:00
|
|
|
func OutputSecret(ui cli.Ui, secret *api.Secret) int {
|
|
|
|
return outputWithFormat(ui, secret, secret)
|
2016-02-12 18:21:42 +00:00
|
|
|
}
|
|
|
|
|
2018-02-12 23:12:16 +00:00
|
|
|
func OutputList(ui cli.Ui, data interface{}) int {
|
|
|
|
switch data.(type) {
|
|
|
|
case *api.Secret:
|
|
|
|
secret := data.(*api.Secret)
|
|
|
|
return outputWithFormat(ui, secret, secret.Data["keys"])
|
|
|
|
default:
|
|
|
|
return outputWithFormat(ui, nil, data)
|
2017-09-05 04:01:44 +00:00
|
|
|
}
|
2018-02-12 23:12:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func OutputData(ui cli.Ui, data interface{}) int {
|
|
|
|
return outputWithFormat(ui, nil, data)
|
|
|
|
}
|
2017-09-05 04:01:44 +00:00
|
|
|
|
2018-02-12 23:12:16 +00:00
|
|
|
func outputWithFormat(ui cli.Ui, secret *api.Secret, data interface{}) int {
|
|
|
|
format := Format(ui)
|
|
|
|
formatter, ok := Formatters[format]
|
2016-02-12 18:21:42 +00:00
|
|
|
if !ok {
|
2016-01-14 19:18:27 +00:00
|
|
|
ui.Error(fmt.Sprintf("Invalid output format: %s", format))
|
|
|
|
return 1
|
|
|
|
}
|
2018-02-12 23:12:16 +00:00
|
|
|
|
2016-02-25 02:45:14 +00:00
|
|
|
if err := formatter.Output(ui, secret, data); err != nil {
|
2018-02-12 23:12:16 +00:00
|
|
|
ui.Error(fmt.Sprintf("Could not parse output: %s", err.Error()))
|
2015-04-27 21:55:29 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2016-02-12 18:21:42 +00:00
|
|
|
type Formatter interface {
|
2016-02-25 02:45:14 +00:00
|
|
|
Output(ui cli.Ui, secret *api.Secret, data interface{}) error
|
2018-02-15 14:11:56 +00:00
|
|
|
Format(data interface{}) ([]byte, error)
|
2016-02-12 18:21:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var Formatters = map[string]Formatter{
|
|
|
|
"json": JsonFormatter{},
|
|
|
|
"table": TableFormatter{},
|
|
|
|
"yaml": YamlFormatter{},
|
2016-09-16 14:43:23 +00:00
|
|
|
"yml": YamlFormatter{},
|
2016-02-12 18:21:42 +00:00
|
|
|
}
|
|
|
|
|
2018-02-12 23:12:16 +00:00
|
|
|
func format() string {
|
|
|
|
format := os.Getenv(EnvVaultFormat)
|
|
|
|
if format == "" {
|
|
|
|
format = "table"
|
|
|
|
}
|
|
|
|
return format
|
|
|
|
}
|
|
|
|
|
|
|
|
func Format(ui cli.Ui) string {
|
|
|
|
switch ui.(type) {
|
|
|
|
case *VaultUI:
|
|
|
|
return ui.(*VaultUI).format
|
|
|
|
}
|
|
|
|
return format()
|
|
|
|
}
|
|
|
|
|
2016-02-12 18:21:42 +00:00
|
|
|
// An output formatter for json output of an object
|
2017-08-28 20:45:04 +00:00
|
|
|
type JsonFormatter struct{}
|
2016-02-12 18:21:42 +00:00
|
|
|
|
2018-02-15 14:11:56 +00:00
|
|
|
func (j JsonFormatter) Format(data interface{}) ([]byte, error) {
|
|
|
|
return json.MarshalIndent(data, "", " ")
|
|
|
|
}
|
|
|
|
|
2016-02-25 02:45:14 +00:00
|
|
|
func (j JsonFormatter) Output(ui cli.Ui, secret *api.Secret, data interface{}) error {
|
2018-02-15 14:11:56 +00:00
|
|
|
b, err := j.Format(data)
|
2017-08-28 20:45:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2016-01-14 19:18:27 +00:00
|
|
|
}
|
2017-08-28 20:45:04 +00:00
|
|
|
ui.Output(string(b))
|
|
|
|
return nil
|
2016-02-12 18:21:42 +00:00
|
|
|
}
|
2016-01-14 19:18:27 +00:00
|
|
|
|
2016-02-12 18:21:42 +00:00
|
|
|
// An output formatter for yaml output format of an object
|
|
|
|
type YamlFormatter struct {
|
2016-01-14 19:18:27 +00:00
|
|
|
}
|
|
|
|
|
2018-02-15 14:11:56 +00:00
|
|
|
func (y YamlFormatter) Format(data interface{}) ([]byte, error) {
|
|
|
|
return yaml.Marshal(data)
|
|
|
|
}
|
|
|
|
|
2016-02-25 02:45:14 +00:00
|
|
|
func (y YamlFormatter) Output(ui cli.Ui, secret *api.Secret, data interface{}) error {
|
2018-02-15 14:11:56 +00:00
|
|
|
b, err := y.Format(data)
|
2016-02-12 18:21:42 +00:00
|
|
|
if err == nil {
|
|
|
|
ui.Output(strings.TrimSpace(string(b)))
|
2015-12-10 10:32:31 +00:00
|
|
|
}
|
2016-02-12 18:21:42 +00:00
|
|
|
return err
|
|
|
|
}
|
2015-12-10 10:32:31 +00:00
|
|
|
|
2016-02-12 18:21:42 +00:00
|
|
|
// An output formatter for table output of an object
|
|
|
|
type TableFormatter struct {
|
2015-12-10 10:32:31 +00:00
|
|
|
}
|
|
|
|
|
2018-02-15 14:11:56 +00:00
|
|
|
// We don't use this
|
|
|
|
func (t TableFormatter) Format(data interface{}) ([]byte, error) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2016-02-25 02:45:14 +00:00
|
|
|
func (t TableFormatter) Output(ui cli.Ui, secret *api.Secret, data interface{}) error {
|
2018-02-12 23:12:16 +00:00
|
|
|
switch data.(type) {
|
|
|
|
case *api.Secret:
|
|
|
|
return t.OutputSecret(ui, secret)
|
|
|
|
case []interface{}:
|
|
|
|
return t.OutputList(ui, secret, data)
|
|
|
|
case []string:
|
|
|
|
return t.OutputList(ui, nil, data)
|
2018-03-21 22:02:41 +00:00
|
|
|
case map[string]interface{}:
|
|
|
|
t.OutputMap(ui, data.(map[string]interface{}))
|
|
|
|
return nil
|
2018-02-12 23:12:16 +00:00
|
|
|
default:
|
|
|
|
return errors.New("Cannot use the table formatter for this type")
|
2016-02-12 18:21:42 +00:00
|
|
|
}
|
|
|
|
}
|
2016-01-14 19:18:27 +00:00
|
|
|
|
2018-02-12 23:12:16 +00:00
|
|
|
func (t TableFormatter) OutputList(ui cli.Ui, secret *api.Secret, data interface{}) error {
|
2017-09-08 02:04:48 +00:00
|
|
|
t.printWarnings(ui, secret)
|
2016-02-12 18:21:42 +00:00
|
|
|
|
2018-02-12 23:12:16 +00:00
|
|
|
switch data.(type) {
|
|
|
|
case []interface{}:
|
|
|
|
case []string:
|
|
|
|
ui.Output(tableOutput(data.([]string), nil))
|
|
|
|
return nil
|
|
|
|
default:
|
|
|
|
return errors.New("Error: table formatter cannot output list for this data type")
|
|
|
|
}
|
|
|
|
|
|
|
|
list := data.([]interface{})
|
|
|
|
|
2016-09-01 19:58:16 +00:00
|
|
|
if len(list) > 0 {
|
2017-09-08 02:04:48 +00:00
|
|
|
keys := make([]string, len(list))
|
|
|
|
for i, v := range list {
|
|
|
|
typed, ok := v.(string)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Error: %v is not a string", v)
|
|
|
|
}
|
|
|
|
keys[i] = typed
|
2016-09-01 19:58:16 +00:00
|
|
|
}
|
|
|
|
sort.Strings(keys)
|
2016-02-12 18:21:42 +00:00
|
|
|
|
2017-09-08 02:04:48 +00:00
|
|
|
// Prepend the header
|
|
|
|
keys = append([]string{"Keys"}, keys...)
|
|
|
|
|
|
|
|
ui.Output(tableOutput(keys, &columnize.Config{
|
|
|
|
Delim: hopeDelim,
|
|
|
|
}))
|
2016-02-12 18:21:42 +00:00
|
|
|
}
|
|
|
|
|
2017-09-08 02:04:48 +00:00
|
|
|
return nil
|
|
|
|
}
|
2016-09-01 19:58:16 +00:00
|
|
|
|
2017-09-08 02:04:48 +00:00
|
|
|
// printWarnings prints any warnings in the secret.
|
|
|
|
func (t TableFormatter) printWarnings(ui cli.Ui, secret *api.Secret) {
|
|
|
|
if secret != nil && len(secret.Warnings) > 0 {
|
|
|
|
ui.Warn("WARNING! The following warnings were returned from Vault:\n")
|
2016-02-25 02:45:14 +00:00
|
|
|
for _, warning := range secret.Warnings {
|
2017-09-08 02:04:48 +00:00
|
|
|
ui.Warn(wrapAtLengthWithPadding(fmt.Sprintf("* %s", warning), 2))
|
2016-02-25 02:45:14 +00:00
|
|
|
}
|
2017-09-08 02:04:48 +00:00
|
|
|
ui.Warn("")
|
2016-02-25 02:45:14 +00:00
|
|
|
}
|
2016-01-14 19:18:27 +00:00
|
|
|
}
|
|
|
|
|
2017-09-08 02:04:48 +00:00
|
|
|
func (t TableFormatter) OutputSecret(ui cli.Ui, secret *api.Secret) error {
|
|
|
|
if secret == nil {
|
|
|
|
return nil
|
2016-09-01 19:58:16 +00:00
|
|
|
}
|
2016-05-17 17:10:12 +00:00
|
|
|
|
2017-09-08 02:04:48 +00:00
|
|
|
t.printWarnings(ui, secret)
|
|
|
|
|
|
|
|
out := make([]string, 0, 8)
|
|
|
|
if secret.LeaseDuration > 0 {
|
|
|
|
if secret.LeaseID != "" {
|
|
|
|
out = append(out, fmt.Sprintf("lease_id %s %s", hopeDelim, secret.LeaseID))
|
|
|
|
out = append(out, fmt.Sprintf("lease_duration %s %s", hopeDelim, humanDurationInt(secret.LeaseDuration)))
|
|
|
|
out = append(out, fmt.Sprintf("lease_renewable %s %t", hopeDelim, secret.Renewable))
|
2016-05-17 17:10:12 +00:00
|
|
|
} else {
|
2017-09-08 02:04:48 +00:00
|
|
|
// This is probably the generic secret backend which has leases, but we
|
|
|
|
// print them as refresh_interval to reduce confusion.
|
|
|
|
out = append(out, fmt.Sprintf("refresh_interval %s %s", hopeDelim, humanDurationInt(secret.LeaseDuration)))
|
2015-09-20 12:34:34 +00:00
|
|
|
}
|
2015-04-27 21:55:29 +00:00
|
|
|
}
|
|
|
|
|
2017-09-08 02:04:48 +00:00
|
|
|
if secret.Auth != nil {
|
|
|
|
out = append(out, fmt.Sprintf("token %s %s", hopeDelim, secret.Auth.ClientToken))
|
|
|
|
out = append(out, fmt.Sprintf("token_accessor %s %s", hopeDelim, secret.Auth.Accessor))
|
|
|
|
// If the lease duration is 0, it's likely a root token, so output the
|
|
|
|
// duration as "infinity" to clear things up.
|
|
|
|
if secret.Auth.LeaseDuration == 0 {
|
|
|
|
out = append(out, fmt.Sprintf("token_duration %s %s", hopeDelim, "∞"))
|
|
|
|
} else {
|
|
|
|
out = append(out, fmt.Sprintf("token_duration %s %s", hopeDelim, humanDurationInt(secret.Auth.LeaseDuration)))
|
|
|
|
}
|
|
|
|
out = append(out, fmt.Sprintf("token_renewable %s %t", hopeDelim, secret.Auth.Renewable))
|
|
|
|
out = append(out, fmt.Sprintf("token_policies %s %v", hopeDelim, secret.Auth.Policies))
|
|
|
|
for k, v := range secret.Auth.Metadata {
|
|
|
|
out = append(out, fmt.Sprintf("token_meta_%s %s %v", k, hopeDelim, v))
|
2015-06-17 23:59:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-08 02:04:48 +00:00
|
|
|
if secret.WrapInfo != nil {
|
|
|
|
out = append(out, fmt.Sprintf("wrapping_token: %s %s", hopeDelim, secret.WrapInfo.Token))
|
2018-01-03 19:02:31 +00:00
|
|
|
out = append(out, fmt.Sprintf("wrapping_accessor: %s %s", hopeDelim, secret.WrapInfo.Accessor))
|
2017-09-08 02:04:48 +00:00
|
|
|
out = append(out, fmt.Sprintf("wrapping_token_ttl: %s %s", hopeDelim, humanDurationInt(secret.WrapInfo.TTL)))
|
|
|
|
out = append(out, fmt.Sprintf("wrapping_token_creation_time: %s %s", hopeDelim, secret.WrapInfo.CreationTime.String()))
|
|
|
|
out = append(out, fmt.Sprintf("wrapping_token_creation_path: %s %s", hopeDelim, secret.WrapInfo.CreationPath))
|
|
|
|
if secret.WrapInfo.WrappedAccessor != "" {
|
|
|
|
out = append(out, fmt.Sprintf("wrapped_accessor: %s %s", hopeDelim, secret.WrapInfo.WrappedAccessor))
|
2016-06-13 23:58:17 +00:00
|
|
|
}
|
2016-05-02 05:58:58 +00:00
|
|
|
}
|
|
|
|
|
2017-09-08 02:04:48 +00:00
|
|
|
if len(secret.Data) > 0 {
|
|
|
|
keys := make([]string, 0, len(secret.Data))
|
|
|
|
for k := range secret.Data {
|
2016-09-01 19:58:16 +00:00
|
|
|
keys = append(keys, k)
|
|
|
|
}
|
|
|
|
sort.Strings(keys)
|
2015-12-10 09:01:22 +00:00
|
|
|
|
2016-09-01 19:58:16 +00:00
|
|
|
for _, k := range keys {
|
2017-09-08 02:04:48 +00:00
|
|
|
out = append(out, fmt.Sprintf("%s %s %v", k, hopeDelim, secret.Data[k]))
|
2016-09-01 19:58:16 +00:00
|
|
|
}
|
2015-04-27 21:55:29 +00:00
|
|
|
}
|
|
|
|
|
2017-09-08 02:04:48 +00:00
|
|
|
// If we got this far and still don't have any data, there's nothing to print,
|
|
|
|
// sorry.
|
|
|
|
if len(out) == 0 {
|
|
|
|
return nil
|
2015-10-07 19:30:54 +00:00
|
|
|
}
|
|
|
|
|
2017-09-08 02:04:48 +00:00
|
|
|
// Prepend the header
|
|
|
|
out = append([]string{"Key" + hopeDelim + "Value"}, out...)
|
2016-06-15 21:13:14 +00:00
|
|
|
|
2017-09-08 02:04:48 +00:00
|
|
|
ui.Output(tableOutput(out, &columnize.Config{
|
|
|
|
Delim: hopeDelim,
|
|
|
|
}))
|
2016-02-12 18:21:42 +00:00
|
|
|
return nil
|
2016-01-14 19:18:27 +00:00
|
|
|
}
|
2017-09-21 17:38:39 +00:00
|
|
|
|
2018-03-21 22:02:41 +00:00
|
|
|
func (t TableFormatter) OutputMap(ui cli.Ui, data map[string]interface{}) {
|
|
|
|
out := make([]string, 0, len(data)+1)
|
|
|
|
if len(data) > 0 {
|
|
|
|
keys := make([]string, 0, len(data))
|
|
|
|
for k := range data {
|
|
|
|
keys = append(keys, k)
|
|
|
|
}
|
|
|
|
sort.Strings(keys)
|
|
|
|
|
|
|
|
for _, k := range keys {
|
|
|
|
out = append(out, fmt.Sprintf("%s %s %v", k, hopeDelim, data[k]))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we got this far and still don't have any data, there's nothing to print,
|
|
|
|
// sorry.
|
|
|
|
if len(out) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prepend the header
|
|
|
|
out = append([]string{"Key" + hopeDelim + "Value"}, out...)
|
|
|
|
|
|
|
|
ui.Output(tableOutput(out, &columnize.Config{
|
|
|
|
Delim: hopeDelim,
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
2018-02-12 23:12:16 +00:00
|
|
|
// OutputSealStatus will print *api.SealStatusResponse in the CLI according to the format provided
|
2017-09-21 17:38:39 +00:00
|
|
|
func OutputSealStatus(ui cli.Ui, client *api.Client, status *api.SealStatusResponse) int {
|
2018-02-12 23:12:16 +00:00
|
|
|
switch Format(ui) {
|
|
|
|
case "table":
|
|
|
|
default:
|
|
|
|
return OutputData(ui, status)
|
|
|
|
}
|
|
|
|
|
2018-01-03 19:02:31 +00:00
|
|
|
var sealPrefix string
|
|
|
|
if status.RecoverySeal {
|
|
|
|
sealPrefix = "Recovery "
|
2015-10-07 19:30:54 +00:00
|
|
|
}
|
|
|
|
|
2017-09-21 17:38:39 +00:00
|
|
|
out := []string{}
|
|
|
|
out = append(out, "Key | Value")
|
2018-01-03 19:02:31 +00:00
|
|
|
out = append(out, fmt.Sprintf("%sSeal Type | %s", sealPrefix, status.Type))
|
2017-09-21 17:38:39 +00:00
|
|
|
out = append(out, fmt.Sprintf("Sealed | %t", status.Sealed))
|
2018-01-03 19:02:31 +00:00
|
|
|
out = append(out, fmt.Sprintf("Total %sShares | %d", sealPrefix, status.N))
|
|
|
|
out = append(out, fmt.Sprintf("Threshold | %d", status.T))
|
2017-09-21 17:38:39 +00:00
|
|
|
|
|
|
|
if status.Sealed {
|
|
|
|
out = append(out, fmt.Sprintf("Unseal Progress | %d/%d", status.Progress, status.T))
|
|
|
|
out = append(out, fmt.Sprintf("Unseal Nonce | %s", status.Nonce))
|
|
|
|
}
|
|
|
|
|
|
|
|
out = append(out, fmt.Sprintf("Version | %s", status.Version))
|
|
|
|
|
|
|
|
if status.ClusterName != "" && status.ClusterID != "" {
|
|
|
|
out = append(out, fmt.Sprintf("Cluster Name | %s", status.ClusterName))
|
|
|
|
out = append(out, fmt.Sprintf("Cluster ID | %s", status.ClusterID))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mask the 'Vault is sealed' error, since this means HA is enabled, but that
|
|
|
|
// we cannot query for the leader since we are sealed.
|
|
|
|
leaderStatus, err := client.Sys().Leader()
|
|
|
|
if err != nil && strings.Contains(err.Error(), "Vault is sealed") {
|
|
|
|
leaderStatus = &api.LeaderResponse{HAEnabled: true}
|
2018-02-18 01:52:42 +00:00
|
|
|
err = nil
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
ui.Error(fmt.Sprintf("Error checking leader status: %s", err))
|
|
|
|
return 1
|
2017-09-21 17:38:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Output if HA is enabled
|
|
|
|
out = append(out, fmt.Sprintf("HA Enabled | %t", leaderStatus.HAEnabled))
|
|
|
|
if leaderStatus.HAEnabled {
|
|
|
|
mode := "sealed"
|
|
|
|
if !status.Sealed {
|
2018-02-18 01:52:42 +00:00
|
|
|
out = append(out, fmt.Sprintf("HA Cluster | %s", leaderStatus.LeaderClusterAddress))
|
2017-09-21 17:38:39 +00:00
|
|
|
mode = "standby"
|
2018-02-18 01:52:42 +00:00
|
|
|
showLeaderAddr := false
|
2017-09-21 17:38:39 +00:00
|
|
|
if leaderStatus.IsSelf {
|
|
|
|
mode = "active"
|
2018-02-18 01:52:42 +00:00
|
|
|
} else {
|
|
|
|
if leaderStatus.LeaderAddress == "" {
|
|
|
|
leaderStatus.LeaderAddress = "<none>"
|
|
|
|
}
|
|
|
|
showLeaderAddr = true
|
2017-09-21 17:38:39 +00:00
|
|
|
}
|
2018-02-18 01:52:42 +00:00
|
|
|
out = append(out, fmt.Sprintf("HA Mode | %s", mode))
|
2017-09-21 17:38:39 +00:00
|
|
|
|
2018-02-18 01:52:42 +00:00
|
|
|
// This is down here just to keep ordering consistent
|
|
|
|
if showLeaderAddr {
|
2018-03-05 12:40:59 +00:00
|
|
|
out = append(out, fmt.Sprintf("Active Node Address | %s", leaderStatus.LeaderAddress))
|
2018-02-18 01:52:42 +00:00
|
|
|
}
|
2017-09-21 17:38:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-22 00:51:12 +00:00
|
|
|
ui.Output(tableOutput(out, nil))
|
2017-09-21 17:38:39 +00:00
|
|
|
return 0
|
|
|
|
}
|