2015-03-04 19:08:13 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
2015-09-14 19:42:12 +00:00
|
|
|
"flag"
|
2015-03-16 03:52:28 +00:00
|
|
|
"fmt"
|
2015-03-04 19:08:13 +00:00
|
|
|
"strings"
|
2015-09-14 19:42:12 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/vault/api"
|
2016-04-01 17:16:05 +00:00
|
|
|
"github.com/hashicorp/vault/meta"
|
2017-08-24 22:23:40 +00:00
|
|
|
"github.com/posener/complete"
|
2015-03-04 19:08:13 +00:00
|
|
|
)
|
|
|
|
|
2015-04-01 00:16:12 +00:00
|
|
|
// ReadCommand is a Command that reads data from the Vault.
|
2015-03-16 03:35:33 +00:00
|
|
|
type ReadCommand struct {
|
2016-04-01 17:16:05 +00:00
|
|
|
meta.Meta
|
2015-03-04 19:08:13 +00:00
|
|
|
}
|
|
|
|
|
2015-03-16 03:35:33 +00:00
|
|
|
func (c *ReadCommand) Run(args []string) int {
|
2015-04-01 03:50:05 +00:00
|
|
|
var format string
|
2015-05-22 18:28:23 +00:00
|
|
|
var field string
|
2015-09-14 19:42:12 +00:00
|
|
|
var err error
|
|
|
|
var secret *api.Secret
|
|
|
|
var flags *flag.FlagSet
|
2016-04-01 17:16:05 +00:00
|
|
|
flags = c.Meta.FlagSet("read", meta.FlagSetDefault)
|
2015-04-01 03:50:05 +00:00
|
|
|
flags.StringVar(&format, "format", "table", "")
|
2015-05-22 18:28:23 +00:00
|
|
|
flags.StringVar(&field, "field", "", "")
|
2015-03-04 19:08:13 +00:00
|
|
|
flags.Usage = func() { c.Ui.Error(c.Help()) }
|
|
|
|
if err := flags.Parse(args); err != nil {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2015-03-16 03:52:28 +00:00
|
|
|
args = flags.Args()
|
2016-01-12 13:46:49 +00:00
|
|
|
if len(args) != 1 || len(args[0]) == 0 {
|
2015-06-18 23:00:41 +00:00
|
|
|
c.Ui.Error("read expects one argument")
|
2015-03-16 03:52:28 +00:00
|
|
|
flags.Usage()
|
|
|
|
return 1
|
|
|
|
}
|
2015-06-18 01:33:15 +00:00
|
|
|
|
2015-03-16 03:52:28 +00:00
|
|
|
path := args[0]
|
2015-06-18 01:33:15 +00:00
|
|
|
if path[0] == '/' {
|
|
|
|
path = path[1:]
|
|
|
|
}
|
2015-03-16 03:52:28 +00:00
|
|
|
|
|
|
|
client, err := c.Client()
|
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf(
|
|
|
|
"Error initializing client: %s", err))
|
|
|
|
return 2
|
|
|
|
}
|
|
|
|
|
2016-01-14 19:18:27 +00:00
|
|
|
secret, err = client.Logical().Read(path)
|
2015-03-16 03:52:28 +00:00
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf(
|
|
|
|
"Error reading %s: %s", path, err))
|
|
|
|
return 1
|
|
|
|
}
|
2015-04-19 05:05:08 +00:00
|
|
|
if secret == nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf(
|
|
|
|
"No value found at %s", path))
|
|
|
|
return 1
|
|
|
|
}
|
2015-03-16 03:52:28 +00:00
|
|
|
|
2015-05-22 18:28:23 +00:00
|
|
|
// Handle single field output
|
|
|
|
if field != "" {
|
2016-05-19 15:25:15 +00:00
|
|
|
return PrintRawField(c.Ui, secret, field)
|
2015-05-22 18:28:23 +00:00
|
|
|
}
|
|
|
|
|
2015-04-27 21:55:29 +00:00
|
|
|
return OutputSecret(c.Ui, format, secret)
|
2015-04-01 03:50:05 +00:00
|
|
|
}
|
|
|
|
|
2015-03-16 03:35:33 +00:00
|
|
|
func (c *ReadCommand) Synopsis() string {
|
|
|
|
return "Read data or secrets from Vault"
|
2015-03-04 19:08:13 +00:00
|
|
|
}
|
|
|
|
|
2015-03-16 03:35:33 +00:00
|
|
|
func (c *ReadCommand) Help() string {
|
2015-03-04 19:08:13 +00:00
|
|
|
helpText := `
|
2015-04-01 00:16:12 +00:00
|
|
|
Usage: vault read [options] path
|
2015-03-04 19:08:13 +00:00
|
|
|
|
|
|
|
Read data from Vault.
|
|
|
|
|
2015-09-14 19:42:12 +00:00
|
|
|
Reads data at the given path from Vault. This can be used to read
|
|
|
|
secrets and configuration as well as generate dynamic values from
|
2015-03-04 19:08:13 +00:00
|
|
|
materialized backends. Please reference the documentation for the
|
|
|
|
backends in use to determine key structure.
|
|
|
|
|
|
|
|
General Options:
|
2016-04-01 20:50:12 +00:00
|
|
|
` + meta.GeneralOptionsUsage() + `
|
2015-04-01 03:50:05 +00:00
|
|
|
Read Options:
|
|
|
|
|
2015-04-01 23:44:20 +00:00
|
|
|
-format=table The format for output. By default it is a whitespace-
|
2015-12-10 10:32:31 +00:00
|
|
|
delimited table. This can also be json or yaml.
|
2015-04-01 03:50:05 +00:00
|
|
|
|
2015-06-18 01:33:15 +00:00
|
|
|
-field=field If included, the raw value of the specified field
|
2016-01-12 13:46:49 +00:00
|
|
|
will be output raw to stdout.
|
2015-05-22 18:28:23 +00:00
|
|
|
|
2015-03-04 19:08:13 +00:00
|
|
|
`
|
|
|
|
return strings.TrimSpace(helpText)
|
|
|
|
}
|
2017-08-24 22:23:40 +00:00
|
|
|
|
|
|
|
func (c *ReadCommand) AutocompleteArgs() complete.Predictor {
|
|
|
|
return complete.PredictNothing
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ReadCommand) AutocompleteFlags() complete.Flags {
|
|
|
|
return complete.Flags{
|
|
|
|
"-format": predictFormat,
|
|
|
|
"-field": complete.PredictNothing,
|
|
|
|
}
|
|
|
|
}
|