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"
|
2015-12-10 09:01:22 +00:00
|
|
|
"sort"
|
2015-05-02 20:11:40 +00:00
|
|
|
"strconv"
|
2015-12-10 10:32:31 +00:00
|
|
|
"strings"
|
2016-09-01 19:58:16 +00:00
|
|
|
"sync"
|
2016-07-19 16:15:00 +00:00
|
|
|
"time"
|
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"
|
2017-08-24 22:23:40 +00:00
|
|
|
"github.com/posener/complete"
|
2015-04-27 21:55:29 +00:00
|
|
|
"github.com/ryanuber/columnize"
|
|
|
|
)
|
|
|
|
|
2017-08-24 22:23:40 +00:00
|
|
|
var predictFormat complete.Predictor = complete.PredictSet("json", "yaml")
|
|
|
|
|
2015-04-27 21:55:29 +00:00
|
|
|
func OutputSecret(ui cli.Ui, format string, secret *api.Secret) int {
|
2016-02-25 02:45:14 +00:00
|
|
|
return outputWithFormat(ui, format, secret, secret)
|
2015-04-27 21:55:29 +00:00
|
|
|
}
|
|
|
|
|
2016-01-14 19:18:27 +00:00
|
|
|
func OutputList(ui cli.Ui, format string, secret *api.Secret) int {
|
2016-02-25 02:45:14 +00:00
|
|
|
return outputWithFormat(ui, format, secret, secret.Data["keys"])
|
2016-02-12 18:21:42 +00:00
|
|
|
}
|
|
|
|
|
2016-02-25 02:45:14 +00:00
|
|
|
func outputWithFormat(ui cli.Ui, format string, secret *api.Secret, data interface{}) int {
|
2016-02-12 18:21:42 +00:00
|
|
|
formatter, ok := Formatters[strings.ToLower(format)]
|
|
|
|
if !ok {
|
2016-01-14 19:18:27 +00:00
|
|
|
ui.Error(fmt.Sprintf("Invalid output format: %s", format))
|
|
|
|
return 1
|
|
|
|
}
|
2016-02-25 02:45:14 +00:00
|
|
|
if err := formatter.Output(ui, secret, data); err != nil {
|
2016-02-12 18:21:42 +00:00
|
|
|
ui.Error(fmt.Sprintf("Could not output secret: %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
|
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
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
2016-02-25 02:45:14 +00:00
|
|
|
func (j JsonFormatter) Output(ui cli.Ui, secret *api.Secret, data interface{}) error {
|
2017-08-28 20:45:04 +00:00
|
|
|
b, err := json.MarshalIndent(data, "", " ")
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-02-25 02:45:14 +00:00
|
|
|
func (y YamlFormatter) Output(ui cli.Ui, secret *api.Secret, data interface{}) error {
|
2016-02-12 18:21:42 +00:00
|
|
|
b, err := yaml.Marshal(data)
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-02-25 02:45:14 +00:00
|
|
|
func (t TableFormatter) Output(ui cli.Ui, secret *api.Secret, data interface{}) error {
|
2016-02-12 18:21:42 +00:00
|
|
|
// TODO: this should really use reflection like the other formatters do
|
|
|
|
if s, ok := data.(*api.Secret); ok {
|
2016-02-25 02:45:14 +00:00
|
|
|
return t.OutputSecret(ui, secret, s)
|
2016-01-14 19:18:27 +00:00
|
|
|
}
|
2016-02-12 18:21:42 +00:00
|
|
|
if s, ok := data.([]interface{}); ok {
|
2016-02-25 02:45:14 +00:00
|
|
|
return t.OutputList(ui, secret, s)
|
2016-02-12 18:21:42 +00:00
|
|
|
}
|
|
|
|
return errors.New("Cannot use the table formatter for this type")
|
|
|
|
}
|
2016-01-14 19:18:27 +00:00
|
|
|
|
2016-02-25 02:45:14 +00:00
|
|
|
func (t TableFormatter) OutputList(ui cli.Ui, secret *api.Secret, list []interface{}) error {
|
2016-02-12 18:21:42 +00:00
|
|
|
config := columnize.DefaultConfig()
|
|
|
|
config.Delim = "♨"
|
|
|
|
config.Glue = "\t"
|
|
|
|
config.Prefix = ""
|
|
|
|
|
|
|
|
input := make([]string, 0, 5)
|
|
|
|
|
2016-09-01 19:58:16 +00:00
|
|
|
if len(list) > 0 {
|
|
|
|
input = append(input, "Keys")
|
|
|
|
input = append(input, "----")
|
2016-02-12 18:21:42 +00:00
|
|
|
|
2016-09-01 19:58:16 +00:00
|
|
|
keys := make([]string, 0, len(list))
|
|
|
|
for _, k := range list {
|
|
|
|
keys = append(keys, k.(string))
|
|
|
|
}
|
|
|
|
sort.Strings(keys)
|
2016-02-12 18:21:42 +00:00
|
|
|
|
2016-09-01 19:58:16 +00:00
|
|
|
for _, k := range keys {
|
|
|
|
input = append(input, fmt.Sprintf("%s", k))
|
|
|
|
}
|
2016-02-12 18:21:42 +00:00
|
|
|
}
|
|
|
|
|
2016-09-01 19:58:16 +00:00
|
|
|
tableOutputStr := columnize.Format(input, config)
|
|
|
|
|
|
|
|
// Print the warning separately because the length of first
|
|
|
|
// column in the output will be increased by the length of
|
|
|
|
// the longest warning string making the output look bad.
|
|
|
|
warningsInput := make([]string, 0, 5)
|
2016-02-25 02:45:14 +00:00
|
|
|
if len(secret.Warnings) != 0 {
|
2016-09-01 19:58:16 +00:00
|
|
|
warningsInput = append(warningsInput, "")
|
|
|
|
warningsInput = append(warningsInput, "The following warnings were returned from the Vault server:")
|
2016-02-25 02:45:14 +00:00
|
|
|
for _, warning := range secret.Warnings {
|
2016-09-01 19:58:16 +00:00
|
|
|
warningsInput = append(warningsInput, fmt.Sprintf("* %s", warning))
|
2016-02-25 02:45:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-01 19:58:16 +00:00
|
|
|
warningsOutputStr := columnize.Format(warningsInput, config)
|
|
|
|
|
|
|
|
ui.Output(fmt.Sprintf("%s\n%s", tableOutputStr, warningsOutputStr))
|
|
|
|
|
2016-02-12 18:21:42 +00:00
|
|
|
return nil
|
2016-01-14 19:18:27 +00:00
|
|
|
}
|
|
|
|
|
2016-02-25 02:45:14 +00:00
|
|
|
func (t TableFormatter) OutputSecret(ui cli.Ui, secret, s *api.Secret) error {
|
2015-04-27 21:55:29 +00:00
|
|
|
config := columnize.DefaultConfig()
|
|
|
|
config.Delim = "♨"
|
|
|
|
config.Glue = "\t"
|
|
|
|
config.Prefix = ""
|
|
|
|
|
|
|
|
input := make([]string, 0, 5)
|
2015-10-07 19:30:54 +00:00
|
|
|
|
2016-09-01 19:58:16 +00:00
|
|
|
onceHeader := &sync.Once{}
|
|
|
|
headerFunc := func() {
|
|
|
|
input = append(input, fmt.Sprintf("Key %s Value", config.Delim))
|
|
|
|
input = append(input, fmt.Sprintf("--- %s -----", config.Delim))
|
|
|
|
}
|
2016-05-17 17:10:12 +00:00
|
|
|
|
2015-09-20 12:30:08 +00:00
|
|
|
if s.LeaseDuration > 0 {
|
2016-09-01 19:58:16 +00:00
|
|
|
onceHeader.Do(headerFunc)
|
2015-09-20 12:34:34 +00:00
|
|
|
if s.LeaseID != "" {
|
|
|
|
input = append(input, fmt.Sprintf("lease_id %s %s", config.Delim, s.LeaseID))
|
2016-05-17 17:10:12 +00:00
|
|
|
input = append(input, fmt.Sprintf(
|
2016-07-19 16:15:00 +00:00
|
|
|
"lease_duration %s %s", config.Delim, (time.Second*time.Duration(s.LeaseDuration)).String()))
|
2016-05-17 17:10:12 +00:00
|
|
|
} else {
|
|
|
|
input = append(input, fmt.Sprintf(
|
2016-07-19 16:15:00 +00:00
|
|
|
"refresh_interval %s %s", config.Delim, (time.Second*time.Duration(s.LeaseDuration)).String()))
|
2015-09-20 12:34:34 +00:00
|
|
|
}
|
|
|
|
if s.LeaseID != "" {
|
|
|
|
input = append(input, fmt.Sprintf(
|
|
|
|
"lease_renewable %s %s", config.Delim, strconv.FormatBool(s.Renewable)))
|
|
|
|
}
|
2015-04-27 21:55:29 +00:00
|
|
|
}
|
|
|
|
|
2015-06-17 23:59:50 +00:00
|
|
|
if s.Auth != nil {
|
2016-09-01 19:58:16 +00:00
|
|
|
onceHeader.Do(headerFunc)
|
2015-06-17 23:59:50 +00:00
|
|
|
input = append(input, fmt.Sprintf("token %s %s", config.Delim, s.Auth.ClientToken))
|
2016-03-09 11:23:31 +00:00
|
|
|
input = append(input, fmt.Sprintf("token_accessor %s %s", config.Delim, s.Auth.Accessor))
|
2016-07-19 16:15:00 +00:00
|
|
|
input = append(input, fmt.Sprintf("token_duration %s %s", config.Delim, (time.Second*time.Duration(s.Auth.LeaseDuration)).String()))
|
2015-06-17 23:59:50 +00:00
|
|
|
input = append(input, fmt.Sprintf("token_renewable %s %v", config.Delim, s.Auth.Renewable))
|
|
|
|
input = append(input, fmt.Sprintf("token_policies %s %v", config.Delim, s.Auth.Policies))
|
|
|
|
for k, v := range s.Auth.Metadata {
|
|
|
|
input = append(input, fmt.Sprintf("token_meta_%s %s %#v", k, config.Delim, v))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-02 05:58:58 +00:00
|
|
|
if s.WrapInfo != nil {
|
2016-09-01 19:58:16 +00:00
|
|
|
onceHeader.Do(headerFunc)
|
2016-05-02 05:58:58 +00:00
|
|
|
input = append(input, fmt.Sprintf("wrapping_token: %s %s", config.Delim, s.WrapInfo.Token))
|
2016-07-19 16:15:00 +00:00
|
|
|
input = append(input, fmt.Sprintf("wrapping_token_ttl: %s %s", config.Delim, (time.Second*time.Duration(s.WrapInfo.TTL)).String()))
|
2016-06-07 20:01:09 +00:00
|
|
|
input = append(input, fmt.Sprintf("wrapping_token_creation_time: %s %s", config.Delim, s.WrapInfo.CreationTime.String()))
|
2017-08-02 22:28:58 +00:00
|
|
|
input = append(input, fmt.Sprintf("wrapping_token_creation_path: %s %s", config.Delim, s.WrapInfo.CreationPath))
|
2016-06-13 23:58:17 +00:00
|
|
|
if s.WrapInfo.WrappedAccessor != "" {
|
|
|
|
input = append(input, fmt.Sprintf("wrapped_accessor: %s %s", config.Delim, s.WrapInfo.WrappedAccessor))
|
|
|
|
}
|
2016-05-02 05:58:58 +00:00
|
|
|
}
|
|
|
|
|
2016-09-01 19:58:16 +00:00
|
|
|
if s.Data != nil && len(s.Data) > 0 {
|
|
|
|
onceHeader.Do(headerFunc)
|
|
|
|
keys := make([]string, 0, len(s.Data))
|
|
|
|
for k := range s.Data {
|
|
|
|
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 {
|
|
|
|
input = append(input, fmt.Sprintf("%s %s %v", k, config.Delim, s.Data[k]))
|
|
|
|
}
|
2015-04-27 21:55:29 +00:00
|
|
|
}
|
|
|
|
|
2016-06-20 19:26:33 +00:00
|
|
|
tableOutputStr := columnize.Format(input, config)
|
2016-06-15 21:13:14 +00:00
|
|
|
|
|
|
|
// Print the warning separately because the length of first
|
|
|
|
// column in the output will be increased by the length of
|
|
|
|
// the longest warning string making the output look bad.
|
|
|
|
warningsInput := make([]string, 0, 5)
|
2015-10-07 19:30:54 +00:00
|
|
|
if len(s.Warnings) != 0 {
|
2016-06-15 21:13:14 +00:00
|
|
|
warningsInput = append(warningsInput, "")
|
|
|
|
warningsInput = append(warningsInput, "The following warnings were returned from the Vault server:")
|
2015-10-07 19:30:54 +00:00
|
|
|
for _, warning := range s.Warnings {
|
2016-06-15 21:13:14 +00:00
|
|
|
warningsInput = append(warningsInput, fmt.Sprintf("* %s", warning))
|
2015-10-07 19:30:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-20 19:26:33 +00:00
|
|
|
warningsOutputStr := columnize.Format(warningsInput, config)
|
|
|
|
|
|
|
|
ui.Output(fmt.Sprintf("%s\n%s", tableOutputStr, warningsOutputStr))
|
2016-06-15 21:13:14 +00:00
|
|
|
|
2016-02-12 18:21:42 +00:00
|
|
|
return nil
|
2016-01-14 19:18:27 +00:00
|
|
|
}
|