2015-03-04 07:34:32 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
2015-03-30 17:55:41 +00:00
|
|
|
"fmt"
|
2015-05-21 02:43:47 +00:00
|
|
|
"io"
|
2015-03-30 17:55:41 +00:00
|
|
|
"os"
|
2015-03-04 07:34:32 +00:00
|
|
|
"strings"
|
2015-03-30 17:55:41 +00:00
|
|
|
|
2015-04-06 16:53:43 +00:00
|
|
|
"github.com/hashicorp/vault/api"
|
2017-08-24 22:23:40 +00:00
|
|
|
"github.com/posener/complete"
|
2015-03-04 07:34:32 +00:00
|
|
|
)
|
|
|
|
|
2015-04-06 03:50:18 +00:00
|
|
|
// AuthHandler is the interface that any auth handlers must implement
|
|
|
|
// to enable auth via the CLI.
|
|
|
|
type AuthHandler interface {
|
2017-08-31 20:57:00 +00:00
|
|
|
Auth(*api.Client, map[string]string) (*api.Secret, error)
|
2015-04-06 03:50:18 +00:00
|
|
|
Help() string
|
|
|
|
}
|
|
|
|
|
2015-03-04 07:34:32 +00:00
|
|
|
// AuthCommand is a Command that handles authentication.
|
|
|
|
type AuthCommand struct {
|
2017-09-05 03:59:24 +00:00
|
|
|
*BaseCommand
|
2015-04-06 03:50:18 +00:00
|
|
|
|
|
|
|
Handlers map[string]AuthHandler
|
2015-05-23 18:22:35 +00:00
|
|
|
|
2017-09-05 03:59:24 +00:00
|
|
|
flagMethod string
|
|
|
|
flagPath string
|
|
|
|
flagNoVerify bool
|
|
|
|
flagNoStore bool
|
|
|
|
flagOnlyToken bool
|
|
|
|
|
|
|
|
// Deprecations
|
|
|
|
// TODO: remove in 0.9.0
|
|
|
|
flagTokenOnly bool
|
|
|
|
flagMethods bool
|
|
|
|
flagMethodHelp bool
|
|
|
|
|
|
|
|
testStdin io.Reader // for tests
|
2015-03-04 07:34:32 +00:00
|
|
|
}
|
|
|
|
|
2017-09-05 03:59:24 +00:00
|
|
|
func (c *AuthCommand) Synopsis() string {
|
|
|
|
return "Authenticates users or machines"
|
|
|
|
}
|
2015-03-04 07:34:32 +00:00
|
|
|
|
2017-09-05 03:59:24 +00:00
|
|
|
func (c *AuthCommand) Help() string {
|
|
|
|
helpText := `
|
|
|
|
Usage: vault auth [options] [AUTH K=V...]
|
2015-04-02 00:01:10 +00:00
|
|
|
|
2017-09-05 03:59:24 +00:00
|
|
|
Authenticates users or machines to Vault using the provided arguments. By
|
|
|
|
default, the authentication method is "token". If not supplied via the CLI,
|
|
|
|
Vault will prompt for input. If argument is "-", the configuration options
|
|
|
|
are read from stdin.
|
2015-03-29 23:42:45 +00:00
|
|
|
|
2017-09-05 03:59:24 +00:00
|
|
|
The -method flag allows alternative authentication providers to be used,
|
|
|
|
such as userpass, github, or cert. For these, additional "key=value" pairs
|
|
|
|
may be required. For example, to authenticate to the userpass auth backend:
|
2015-03-30 17:55:41 +00:00
|
|
|
|
2017-09-05 03:59:24 +00:00
|
|
|
$ vault auth -method=userpass username=my-username
|
2015-05-21 02:43:47 +00:00
|
|
|
|
2017-09-05 03:59:24 +00:00
|
|
|
Use "vault auth-help TYPE" for more information about the list of
|
|
|
|
configuration parameters and examples for a particular provider. Use the
|
|
|
|
"vault auth-list" command to see a list of enabled authentication providers.
|
2015-05-23 18:22:35 +00:00
|
|
|
|
2017-09-05 03:59:24 +00:00
|
|
|
If an authentication provider is mounted at a different path, the -method
|
|
|
|
flag should by the canonical type, and the -path flag should be set to the
|
|
|
|
mount path. If a github authentication provider was mounted at "github-ent",
|
|
|
|
you would authenticate to this backend like this:
|
2015-05-21 02:43:47 +00:00
|
|
|
|
2017-09-05 03:59:24 +00:00
|
|
|
$ vault auth -method=github -path=github-prod
|
2015-03-30 17:55:41 +00:00
|
|
|
|
2017-09-05 03:59:24 +00:00
|
|
|
If the authentication is requested with response wrapping (via -wrap-ttl),
|
|
|
|
the returned token is automatically unwrapped unless:
|
2016-09-08 15:14:47 +00:00
|
|
|
|
2017-09-05 03:59:24 +00:00
|
|
|
- The -only-token flag is used, in which case this command will output
|
|
|
|
the wrapping token
|
2015-04-06 03:50:18 +00:00
|
|
|
|
2017-09-05 03:59:24 +00:00
|
|
|
- The -no-store flag is used, in which case this command will output
|
|
|
|
the details of the wrapping token.
|
2015-04-06 03:50:18 +00:00
|
|
|
|
2017-09-05 03:59:24 +00:00
|
|
|
For a full list of examples, please see the documentation.
|
2015-04-06 16:38:16 +00:00
|
|
|
|
2017-09-05 03:59:24 +00:00
|
|
|
` + c.Flags().Help()
|
2015-06-18 20:48:04 +00:00
|
|
|
|
2017-09-05 03:59:24 +00:00
|
|
|
return strings.TrimSpace(helpText)
|
|
|
|
}
|
2015-04-08 06:29:49 +00:00
|
|
|
|
2017-09-05 03:59:24 +00:00
|
|
|
func (c *AuthCommand) Flags() *FlagSets {
|
|
|
|
set := c.flagSet(FlagSetHTTP | FlagSetOutputField | FlagSetOutputFormat)
|
2015-04-08 06:29:49 +00:00
|
|
|
|
2017-09-05 03:59:24 +00:00
|
|
|
f := set.NewFlagSet("Command Options")
|
2015-04-06 16:53:43 +00:00
|
|
|
|
2017-09-05 03:59:24 +00:00
|
|
|
f.StringVar(&StringVar{
|
|
|
|
Name: "method",
|
|
|
|
Target: &c.flagMethod,
|
|
|
|
Default: "token",
|
|
|
|
Completion: c.PredictVaultAvailableAuths(),
|
|
|
|
Usage: "Type of authentication to use such as \"userpass\" or " +
|
|
|
|
"\"ldap\". Note this corresponds to the TYPE, not the mount path. Use " +
|
|
|
|
"-path to specify the path where the authentication is mounted.",
|
|
|
|
})
|
2016-06-15 20:45:07 +00:00
|
|
|
|
2017-09-05 03:59:24 +00:00
|
|
|
f.StringVar(&StringVar{
|
|
|
|
Name: "path",
|
|
|
|
Target: &c.flagPath,
|
|
|
|
Default: "",
|
|
|
|
Completion: c.PredictVaultAuths(),
|
|
|
|
Usage: "Mount point where the auth backend is enabled. This defaults to " +
|
|
|
|
"the TYPE of backend (e.g. userpass -> userpass/).",
|
|
|
|
})
|
2017-08-31 20:57:00 +00:00
|
|
|
|
2017-09-05 03:59:24 +00:00
|
|
|
f.BoolVar(&BoolVar{
|
|
|
|
Name: "no-verify",
|
|
|
|
Target: &c.flagNoVerify,
|
|
|
|
Default: false,
|
|
|
|
Usage: "Do not verify the token after authentication. By default, Vault " +
|
|
|
|
"issue a request to get more metdata about the token. This request " +
|
|
|
|
"against the use-limit of the token. Set this to true to disable the " +
|
|
|
|
"post-authenication lookup.",
|
2017-08-31 20:57:00 +00:00
|
|
|
})
|
|
|
|
|
2017-09-05 03:59:24 +00:00
|
|
|
f.BoolVar(&BoolVar{
|
|
|
|
Name: "no-store",
|
|
|
|
Target: &c.flagNoStore,
|
|
|
|
Default: false,
|
|
|
|
Usage: "Do not persist the token to the token helper (usually the " +
|
|
|
|
"local filesystem) after authentication for use in future requests. " +
|
|
|
|
"The token will only be displayed in the command output.",
|
|
|
|
})
|
2017-08-31 20:57:00 +00:00
|
|
|
|
2017-09-05 03:59:24 +00:00
|
|
|
f.BoolVar(&BoolVar{
|
|
|
|
Name: "only-token",
|
|
|
|
Target: &c.flagOnlyToken,
|
|
|
|
Default: false,
|
|
|
|
Usage: "Output only the token with no verification. This flag is a " +
|
|
|
|
"shortcut for \"-field=token -no-store -no-verify\". Setting those " +
|
|
|
|
"flags to other values will have no affect.",
|
|
|
|
})
|
2017-08-31 20:57:00 +00:00
|
|
|
|
2017-09-05 03:59:24 +00:00
|
|
|
// Deprecations
|
|
|
|
// TODO: remove in Vault 0.9.0
|
2017-08-31 20:57:00 +00:00
|
|
|
|
2017-09-05 03:59:24 +00:00
|
|
|
f.BoolVar(&BoolVar{
|
|
|
|
Name: "token-only", // Prefer only-token
|
|
|
|
Target: &c.flagTokenOnly,
|
|
|
|
Default: false,
|
|
|
|
Hidden: true,
|
|
|
|
})
|
2015-03-30 17:55:41 +00:00
|
|
|
|
2017-09-05 03:59:24 +00:00
|
|
|
f.BoolVar(&BoolVar{
|
|
|
|
Name: "methods", // Prefer auth-list
|
|
|
|
Target: &c.flagMethods,
|
|
|
|
Default: false,
|
|
|
|
Hidden: true,
|
|
|
|
})
|
2016-03-18 18:41:22 +00:00
|
|
|
|
2017-09-05 03:59:24 +00:00
|
|
|
f.BoolVar(&BoolVar{
|
|
|
|
Name: "method-help", // Prefer auth-help
|
|
|
|
Target: &c.flagMethodHelp,
|
|
|
|
Default: false,
|
|
|
|
Hidden: true,
|
|
|
|
})
|
2017-07-12 19:04:34 +00:00
|
|
|
|
2017-09-05 03:59:24 +00:00
|
|
|
return set
|
|
|
|
}
|
2015-03-30 17:55:41 +00:00
|
|
|
|
2017-09-05 03:59:24 +00:00
|
|
|
func (c *AuthCommand) AutocompleteArgs() complete.Predictor {
|
|
|
|
return nil
|
|
|
|
}
|
2015-08-19 02:18:23 +00:00
|
|
|
|
2017-09-05 03:59:24 +00:00
|
|
|
func (c *AuthCommand) AutocompleteFlags() complete.Flags {
|
|
|
|
return c.Flags().Completions()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *AuthCommand) Run(args []string) int {
|
|
|
|
f := c.Flags()
|
|
|
|
|
|
|
|
if err := f.Parse(args); err != nil {
|
|
|
|
c.UI.Error(err.Error())
|
2016-03-18 18:41:22 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2017-09-05 03:59:24 +00:00
|
|
|
args = f.Args()
|
2017-06-05 20:36:28 +00:00
|
|
|
|
2017-09-05 03:59:24 +00:00
|
|
|
// Deprecations - do this before any argument validations
|
|
|
|
// TODO: remove in 0.9.0
|
|
|
|
switch {
|
|
|
|
case c.flagMethods:
|
|
|
|
c.UI.Warn(wrapAtLength(
|
|
|
|
"WARNING! The -methods flag is deprecated. Please use " +
|
|
|
|
"\"vault auth-list\". This flag will be removed in the next major " +
|
|
|
|
"release of Vault."))
|
|
|
|
cmd := &AuthListCommand{
|
|
|
|
BaseCommand: &BaseCommand{
|
|
|
|
UI: c.UI,
|
|
|
|
client: c.client,
|
|
|
|
},
|
2016-03-18 18:41:22 +00:00
|
|
|
}
|
2017-09-05 03:59:24 +00:00
|
|
|
return cmd.Run(nil)
|
|
|
|
case c.flagMethodHelp:
|
|
|
|
c.UI.Warn(wrapAtLength(
|
|
|
|
"WARNING! The -method-help flag is deprecated. Please use " +
|
|
|
|
"\"vault auth-help\". This flag will be removed in the next major " +
|
|
|
|
"release of Vault."))
|
|
|
|
cmd := &AuthHelpCommand{
|
|
|
|
BaseCommand: &BaseCommand{
|
|
|
|
UI: c.UI,
|
|
|
|
client: c.client,
|
|
|
|
},
|
|
|
|
Handlers: c.Handlers,
|
2016-03-18 18:41:22 +00:00
|
|
|
}
|
2017-09-05 03:59:24 +00:00
|
|
|
return cmd.Run([]string{c.flagMethod})
|
2015-04-29 02:23:26 +00:00
|
|
|
}
|
2015-03-31 22:15:08 +00:00
|
|
|
|
2017-09-05 03:59:24 +00:00
|
|
|
// TODO: remove in 0.9.0
|
|
|
|
if c.flagTokenOnly {
|
|
|
|
c.UI.Warn(wrapAtLength(
|
|
|
|
"WARNING! The -token-only flag is deprecated. Plase use -only-token " +
|
|
|
|
"instead. This flag will be removed in the next major release of " +
|
|
|
|
"Vault."))
|
|
|
|
c.flagOnlyToken = c.flagTokenOnly
|
2017-06-05 20:36:28 +00:00
|
|
|
}
|
|
|
|
|
2017-09-05 03:59:24 +00:00
|
|
|
// Set the right flags if the user requested only-token - this overrides
|
|
|
|
// any previously configured values, as documented.
|
|
|
|
if c.flagOnlyToken {
|
|
|
|
c.flagNoStore = true
|
|
|
|
c.flagNoVerify = true
|
|
|
|
c.flagField = "token"
|
2015-03-31 22:15:08 +00:00
|
|
|
}
|
2017-09-05 03:59:24 +00:00
|
|
|
|
|
|
|
// Get the auth method
|
|
|
|
authMethod := sanitizePath(c.flagMethod)
|
|
|
|
if authMethod == "" {
|
|
|
|
authMethod = "token"
|
2015-03-31 22:15:08 +00:00
|
|
|
}
|
|
|
|
|
2017-09-05 03:59:24 +00:00
|
|
|
// If no path is specified, we default the path to the backend type
|
|
|
|
// or use the plugin name if it's a plugin backend
|
|
|
|
authPath := c.flagPath
|
|
|
|
if authPath == "" {
|
|
|
|
authPath = ensureTrailingSlash(authMethod)
|
2017-06-05 20:36:28 +00:00
|
|
|
}
|
2017-09-05 03:59:24 +00:00
|
|
|
|
|
|
|
// Get the handler function
|
|
|
|
authHandler, ok := c.Handlers[authMethod]
|
|
|
|
if !ok {
|
|
|
|
c.UI.Error(wrapAtLength(fmt.Sprintf(
|
|
|
|
"Unknown authentication method: %s. Use \"vault auth-list\" to see the "+
|
|
|
|
"complete list of authentication providers. Additionally, some "+
|
|
|
|
"authentication providers are only available via the HTTP API.",
|
|
|
|
authMethod)))
|
|
|
|
return 1
|
2016-04-13 18:45:48 +00:00
|
|
|
}
|
2017-09-05 03:59:24 +00:00
|
|
|
|
|
|
|
// Pull our fake stdin if needed
|
|
|
|
stdin := (io.Reader)(os.Stdin)
|
|
|
|
if c.testStdin != nil {
|
|
|
|
stdin = c.testStdin
|
2015-09-18 18:01:28 +00:00
|
|
|
}
|
|
|
|
|
2017-09-05 03:59:24 +00:00
|
|
|
// If the user provided a token, pass it along to the auth provier.
|
|
|
|
if authMethod == "token" && len(args) == 1 {
|
|
|
|
args = []string{"token=" + args[0]}
|
|
|
|
}
|
2015-03-31 06:10:59 +00:00
|
|
|
|
2017-09-05 03:59:24 +00:00
|
|
|
config, err := parseArgsDataString(stdin, args)
|
|
|
|
if err != nil {
|
|
|
|
c.UI.Error(fmt.Sprintf("Error parsing configuration: %s", err))
|
|
|
|
return 1
|
|
|
|
}
|
2016-03-18 18:41:22 +00:00
|
|
|
|
2017-09-05 03:59:24 +00:00
|
|
|
// If the user did not specify a mount path, use the provided mount path.
|
|
|
|
if config["mount"] == "" && authPath != "" {
|
|
|
|
config["mount"] = authPath
|
|
|
|
}
|
2015-03-04 07:34:32 +00:00
|
|
|
|
2017-09-05 03:59:24 +00:00
|
|
|
// Create the client
|
2015-04-02 00:01:10 +00:00
|
|
|
client, err := c.Client()
|
|
|
|
if err != nil {
|
2017-09-05 03:59:24 +00:00
|
|
|
c.UI.Error(err.Error())
|
|
|
|
return 2
|
2015-04-02 00:01:10 +00:00
|
|
|
}
|
|
|
|
|
2017-09-05 03:59:24 +00:00
|
|
|
// Authenticate delegation to the auth handler
|
|
|
|
secret, err := authHandler.Auth(client, config)
|
2017-08-24 22:23:40 +00:00
|
|
|
if err != nil {
|
2017-09-05 03:59:24 +00:00
|
|
|
c.UI.Error(wrapAtLength(fmt.Sprintf(
|
|
|
|
"Error authenticating: %s", err)))
|
|
|
|
return 2
|
2017-08-24 22:23:40 +00:00
|
|
|
}
|
|
|
|
|
2017-09-05 03:59:24 +00:00
|
|
|
// Unset any previous token wrapping functionality. If the original request
|
|
|
|
// was for a wrapped token, we don't want future requests to be wrapped.
|
|
|
|
client.SetWrappingLookupFunc(func(string, string) string { return "" })
|
2017-08-24 22:23:40 +00:00
|
|
|
|
2017-09-05 03:59:24 +00:00
|
|
|
// Recursively extract the token, handling wrapping
|
|
|
|
unwrap := !c.flagOnlyToken && !c.flagNoStore
|
|
|
|
secret, isWrapped, err := c.extractToken(client, secret, unwrap)
|
2015-04-02 00:01:10 +00:00
|
|
|
if err != nil {
|
2017-09-05 03:59:24 +00:00
|
|
|
c.UI.Error(fmt.Sprintf("Error extracting token: %s", err))
|
|
|
|
return 2
|
2015-04-02 00:01:10 +00:00
|
|
|
}
|
2017-09-05 03:59:24 +00:00
|
|
|
if secret == nil {
|
|
|
|
c.UI.Error("Vault returned an empty secret")
|
|
|
|
return 2
|
2015-04-02 00:01:10 +00:00
|
|
|
}
|
2017-09-05 03:59:24 +00:00
|
|
|
|
|
|
|
// Handle special cases if the token was wrapped
|
|
|
|
if isWrapped {
|
|
|
|
if c.flagOnlyToken {
|
|
|
|
return PrintRawField(c.UI, secret, "wrapping_token")
|
2016-06-15 16:35:30 +00:00
|
|
|
}
|
2017-09-05 03:59:24 +00:00
|
|
|
if c.flagNoStore {
|
|
|
|
return OutputSecret(c.UI, c.flagFormat, secret)
|
2017-02-16 16:37:27 +00:00
|
|
|
}
|
2015-04-02 00:01:10 +00:00
|
|
|
}
|
|
|
|
|
2017-09-05 03:59:24 +00:00
|
|
|
// If we got this far, verify we have authentication data before continuing
|
|
|
|
if secret.Auth == nil {
|
|
|
|
c.UI.Error(wrapAtLength(
|
|
|
|
"Vault returned a secret, but the secret has no authentication " +
|
|
|
|
"information attached. This should never happen and is likely a " +
|
|
|
|
"bug."))
|
|
|
|
return 2
|
|
|
|
}
|
2015-04-06 16:38:16 +00:00
|
|
|
|
2017-09-05 03:59:24 +00:00
|
|
|
// Pull the token itself out, since we don't need the rest of the auth
|
|
|
|
// information anymore/.
|
|
|
|
token := secret.Auth.ClientToken
|
2015-04-06 03:50:18 +00:00
|
|
|
|
2017-09-05 03:59:24 +00:00
|
|
|
tokenHelper, err := c.TokenHelper()
|
|
|
|
if err != nil {
|
|
|
|
c.UI.Error(fmt.Sprintf(
|
|
|
|
"Error initializing token helper: %s\n\n"+
|
|
|
|
"Please verify that the token helper is available and properly\n"+
|
|
|
|
"configured for your system. Please refer to the documentation\n"+
|
|
|
|
"on token helpers for more information.",
|
|
|
|
err))
|
|
|
|
return 1
|
|
|
|
}
|
2015-08-19 02:18:23 +00:00
|
|
|
|
2017-09-05 03:59:24 +00:00
|
|
|
if !c.flagNoVerify {
|
|
|
|
// Verify the token and pull it's list of policies
|
|
|
|
client.SetToken(token)
|
|
|
|
client.SetWrappingLookupFunc(func(string, string) string { return "" })
|
2017-06-05 20:36:28 +00:00
|
|
|
|
2017-09-05 03:59:24 +00:00
|
|
|
secret, err = client.Auth().Token().LookupSelf()
|
|
|
|
if err != nil {
|
|
|
|
c.UI.Error(fmt.Sprintf("Error verifying token: %s", err))
|
|
|
|
return 2
|
|
|
|
}
|
|
|
|
if secret == nil {
|
|
|
|
c.UI.Error("Empty response from lookup-self")
|
|
|
|
return 2
|
|
|
|
}
|
|
|
|
}
|
2017-07-12 19:04:34 +00:00
|
|
|
|
2017-09-05 03:59:24 +00:00
|
|
|
if !c.flagNoStore {
|
|
|
|
// Store the token in the local client
|
|
|
|
if err := tokenHelper.Store(token); err != nil {
|
|
|
|
c.UI.Error(fmt.Sprintf("Error storing token: %s", err))
|
|
|
|
c.UI.Error(wrapAtLength(
|
|
|
|
"Authentication was successful, but the token was not persisted. The " +
|
|
|
|
"resulting token is shown below for your records."))
|
|
|
|
OutputSecret(c.UI, c.flagFormat, secret)
|
|
|
|
return 2
|
|
|
|
}
|
2015-04-06 03:50:18 +00:00
|
|
|
|
2017-09-05 03:59:24 +00:00
|
|
|
// Warn if the VAULT_TOKEN environment variable is set, as that will take
|
|
|
|
// precedence. Don't output on token-only since we're likely piping output.
|
|
|
|
if c.flagField == "" && c.flagFormat == "table" {
|
|
|
|
if os.Getenv("VAULT_TOKEN") != "" {
|
|
|
|
c.UI.Warn(wrapAtLength("WARNING! The VAULT_TOKEN environment variable " +
|
|
|
|
"is set! This takes precedence over the value set by this command. To " +
|
|
|
|
"use the value set by this command, unset the VAULT_TOKEN environment " +
|
|
|
|
"variable or set it to the token displayed below."))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-04-06 03:50:18 +00:00
|
|
|
|
2017-09-05 03:59:24 +00:00
|
|
|
// If the user requested a particular field, print that out now since we
|
|
|
|
// are likely piping to another process.
|
|
|
|
if c.flagField != "" {
|
|
|
|
return PrintRawField(c.UI, secret, c.flagField)
|
|
|
|
}
|
2015-04-06 03:50:18 +00:00
|
|
|
|
2017-09-05 03:59:24 +00:00
|
|
|
// Output the secret as json or yaml if requested. We have to maintain
|
|
|
|
// backwards compatiability
|
|
|
|
if c.flagFormat != "table" {
|
|
|
|
return OutputSecret(c.UI, c.flagFormat, secret)
|
2015-04-06 03:50:18 +00:00
|
|
|
}
|
|
|
|
|
2017-09-05 03:59:24 +00:00
|
|
|
output := "Success! You are now authenticated. "
|
|
|
|
if c.flagNoVerify {
|
|
|
|
output += "The token was not verified for validity. "
|
|
|
|
}
|
|
|
|
if c.flagNoStore {
|
|
|
|
output += "The token was not stored in the token helper. "
|
|
|
|
} else {
|
|
|
|
output += "The token information displayed below is already stored in " +
|
|
|
|
"the token helper. You do NOT need to run \"vault auth\" again."
|
2015-04-06 03:50:18 +00:00
|
|
|
}
|
2017-09-05 03:59:24 +00:00
|
|
|
c.UI.Output(wrapAtLength(output) + "\n")
|
2015-04-06 03:50:18 +00:00
|
|
|
|
2017-09-05 03:59:24 +00:00
|
|
|
// TODO make this consistent with other printed token secrets.
|
|
|
|
c.UI.Output(fmt.Sprintf("token: %s", secret.TokenID()))
|
|
|
|
c.UI.Output(fmt.Sprintf("accessor: %s", secret.TokenAccessor()))
|
2015-04-06 03:50:18 +00:00
|
|
|
|
2017-09-05 03:59:24 +00:00
|
|
|
if ttl := secret.TokenTTL(); ttl != 0 {
|
|
|
|
c.UI.Output(fmt.Sprintf("duration: %s", ttl))
|
|
|
|
}
|
2015-04-06 16:38:16 +00:00
|
|
|
|
2017-09-05 03:59:24 +00:00
|
|
|
c.UI.Output(fmt.Sprintf("renewable: %t", secret.TokenIsRenewable()))
|
2015-04-06 16:38:16 +00:00
|
|
|
|
2017-09-05 03:59:24 +00:00
|
|
|
if policies := secret.TokenPolicies(); len(policies) > 0 {
|
|
|
|
c.UI.Output(fmt.Sprintf("policies: %s", policies))
|
|
|
|
}
|
2015-04-06 16:38:16 +00:00
|
|
|
|
2017-09-05 03:59:24 +00:00
|
|
|
return 0
|
2015-04-06 03:50:18 +00:00
|
|
|
}
|
2017-08-24 22:23:40 +00:00
|
|
|
|
2017-09-05 03:59:24 +00:00
|
|
|
// extractToken extracts the token from the given secret, automatically
|
|
|
|
// unwrapping responses and handling error conditions if unwrap is true. The
|
|
|
|
// result also returns whether it was a wrapped resonse that was not unwrapped.
|
|
|
|
func (c *AuthCommand) extractToken(client *api.Client, secret *api.Secret, unwrap bool) (*api.Secret, bool, error) {
|
|
|
|
switch {
|
|
|
|
case secret == nil:
|
|
|
|
return nil, false, fmt.Errorf("empty response from auth helper")
|
2017-08-24 22:23:40 +00:00
|
|
|
|
2017-09-05 03:59:24 +00:00
|
|
|
case secret.Auth != nil:
|
|
|
|
return secret, false, nil
|
|
|
|
|
|
|
|
case secret.WrapInfo != nil:
|
|
|
|
if secret.WrapInfo.WrappedAccessor == "" {
|
|
|
|
return nil, false, fmt.Errorf("wrapped response does not contain a token")
|
2017-08-24 22:23:40 +00:00
|
|
|
}
|
|
|
|
|
2017-09-05 03:59:24 +00:00
|
|
|
if !unwrap {
|
|
|
|
return secret, true, nil
|
2017-08-24 22:23:40 +00:00
|
|
|
}
|
|
|
|
|
2017-09-05 03:59:24 +00:00
|
|
|
client.SetToken(secret.WrapInfo.Token)
|
|
|
|
secret, err := client.Logical().Unwrap("")
|
|
|
|
if err != nil {
|
|
|
|
return nil, false, err
|
|
|
|
}
|
|
|
|
return c.extractToken(client, secret, unwrap)
|
2017-08-24 22:23:40 +00:00
|
|
|
|
2017-09-05 03:59:24 +00:00
|
|
|
default:
|
|
|
|
return nil, false, fmt.Errorf("no auth or wrapping info in response")
|
2017-08-24 22:23:40 +00:00
|
|
|
}
|
|
|
|
}
|