2015-03-04 07:34:32 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
2015-03-30 17:55:41 +00:00
|
|
|
"fmt"
|
|
|
|
"os"
|
2015-04-02 00:01:10 +00:00
|
|
|
"sort"
|
2015-03-04 07:34:32 +00:00
|
|
|
"strings"
|
2015-03-30 17:55:41 +00:00
|
|
|
|
2015-04-06 03:50:18 +00:00
|
|
|
"github.com/hashicorp/vault/helper/flag-kv"
|
2015-03-30 17:55:41 +00:00
|
|
|
"github.com/hashicorp/vault/helper/password"
|
2015-04-02 00:01:10 +00:00
|
|
|
"github.com/ryanuber/columnize"
|
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 {
|
|
|
|
Auth(map[string]string) (string, error)
|
|
|
|
Help() string
|
|
|
|
}
|
|
|
|
|
2015-03-04 07:34:32 +00:00
|
|
|
// AuthCommand is a Command that handles authentication.
|
|
|
|
type AuthCommand struct {
|
|
|
|
Meta
|
2015-04-06 03:50:18 +00:00
|
|
|
|
|
|
|
Handlers map[string]AuthHandler
|
2015-03-04 07:34:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *AuthCommand) Run(args []string) int {
|
|
|
|
var method string
|
2015-04-02 00:01:10 +00:00
|
|
|
var methods bool
|
2015-04-06 03:50:18 +00:00
|
|
|
var vars map[string]string
|
2015-03-04 07:34:32 +00:00
|
|
|
flags := c.Meta.FlagSet("auth", FlagSetDefault)
|
2015-04-02 00:01:10 +00:00
|
|
|
flags.BoolVar(&methods, "methods", false, "")
|
2015-03-04 07:34:32 +00:00
|
|
|
flags.StringVar(&method, "method", "", "method")
|
2015-04-06 03:50:18 +00:00
|
|
|
flags.Var((*kvFlag.Flag)(&vars), "var", "variables")
|
2015-03-04 07:34:32 +00:00
|
|
|
flags.Usage = func() { c.Ui.Error(c.Help()) }
|
|
|
|
if err := flags.Parse(args); err != nil {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2015-04-02 00:01:10 +00:00
|
|
|
if methods {
|
|
|
|
return c.listMethods()
|
|
|
|
}
|
|
|
|
|
2015-03-29 23:42:45 +00:00
|
|
|
args = flags.Args()
|
|
|
|
if len(args) > 1 {
|
|
|
|
flags.Usage()
|
|
|
|
c.Ui.Error("\nError: auth expects at most one argument")
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
if method != "" && len(args) > 0 {
|
|
|
|
flags.Usage()
|
|
|
|
c.Ui.Error("\nError: auth expects no arguments if -method is specified")
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2015-03-30 17:55:41 +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
|
|
|
|
}
|
|
|
|
|
|
|
|
// token is where the final token will go
|
2015-04-06 03:50:18 +00:00
|
|
|
handler := c.Handlers[method]
|
2015-03-30 17:55:41 +00:00
|
|
|
if method == "" {
|
2015-04-06 03:50:18 +00:00
|
|
|
token := ""
|
2015-03-30 17:55:41 +00:00
|
|
|
if len(args) > 0 {
|
|
|
|
token = args[0]
|
|
|
|
}
|
|
|
|
|
2015-04-06 03:50:18 +00:00
|
|
|
handler = &tokenAuthHandler{Token: token}
|
|
|
|
}
|
|
|
|
|
|
|
|
if handler == nil {
|
|
|
|
methods := make([]string, 0, len(c.Handlers))
|
|
|
|
for k, _ := range c.Handlers {
|
|
|
|
methods = append(methods, k)
|
2015-03-30 17:55:41 +00:00
|
|
|
}
|
2015-04-06 03:50:18 +00:00
|
|
|
sort.Strings(methods)
|
|
|
|
|
|
|
|
c.Ui.Error(fmt.Sprintf(
|
|
|
|
"Unknown authentication method: %s\n\n"+
|
|
|
|
"Please use a supported authentication method. The list of supported\n"+
|
|
|
|
"authentication methods is shown below. Note that this list may not\n"+
|
|
|
|
"be exhaustive: Vault may support other auth methods. For auth methods\n"+
|
|
|
|
"unsupported by the CLI, please use the HTTP API.\n\n"+
|
|
|
|
"%s",
|
|
|
|
method,
|
|
|
|
strings.Join(methods, ", ")))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
token, err := handler.Auth(vars)
|
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(err.Error())
|
|
|
|
return 1
|
2015-03-30 17:55:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Store the token!
|
|
|
|
if err := tokenHelper.Store(token); err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf(
|
|
|
|
"Error storing token: %s\n\n"+
|
|
|
|
"Authentication was not successful and did not persist.\n"+
|
|
|
|
"Please reauthenticate, or fix the issue above if possible.",
|
|
|
|
err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2015-03-31 22:15:08 +00:00
|
|
|
// Build the client so we can verify that the token is valid
|
|
|
|
client, err := c.Client()
|
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf(
|
|
|
|
"Error initializing client to verify the token: %s", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify the token
|
|
|
|
secret, err := client.Logical().Read("auth/token/lookup-self")
|
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf(
|
|
|
|
"Error validating token: %s", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the policies we have
|
|
|
|
policiesRaw, ok := secret.Data["policies"]
|
|
|
|
if !ok {
|
|
|
|
policiesRaw = []string{"unknown"}
|
|
|
|
}
|
|
|
|
var policies []string
|
|
|
|
for _, v := range policiesRaw.([]interface{}) {
|
|
|
|
policies = append(policies, v.(string))
|
|
|
|
}
|
|
|
|
|
2015-03-31 06:10:59 +00:00
|
|
|
c.Ui.Output(fmt.Sprintf(
|
2015-03-31 22:15:08 +00:00
|
|
|
"Successfully authenticated! The policies that are associated\n"+
|
|
|
|
"with this token are listed below:\n\n%s",
|
|
|
|
strings.Join(policies, ", "),
|
|
|
|
))
|
2015-03-31 06:10:59 +00:00
|
|
|
|
2015-03-04 07:34:32 +00:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2015-04-02 00:01:10 +00:00
|
|
|
func (c *AuthCommand) listMethods() int {
|
|
|
|
client, err := c.Client()
|
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf(
|
|
|
|
"Error initializing client: %s", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
auth, err := client.Sys().ListAuth()
|
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf(
|
|
|
|
"Error reading auth table: %s", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
paths := make([]string, 0, len(auth))
|
|
|
|
for path, _ := range auth {
|
|
|
|
paths = append(paths, path)
|
|
|
|
}
|
|
|
|
sort.Strings(paths)
|
|
|
|
|
|
|
|
columns := []string{"Path | Type | Description"}
|
|
|
|
for _, k := range paths {
|
|
|
|
a := auth[k]
|
|
|
|
columns = append(columns, fmt.Sprintf(
|
|
|
|
"%s | %s | %s", k, a.Type, a.Description))
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Ui.Output(columnize.SimpleFormat(columns))
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2015-03-04 07:34:32 +00:00
|
|
|
func (c *AuthCommand) Synopsis() string {
|
|
|
|
return "Prints information about how to authenticate with Vault"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *AuthCommand) Help() string {
|
|
|
|
helpText := `
|
2015-03-29 23:42:45 +00:00
|
|
|
Usage: vault auth [options] [token]
|
|
|
|
|
|
|
|
Authenticate with Vault with the given token or via any supported
|
|
|
|
authentication backend.
|
2015-03-04 07:34:32 +00:00
|
|
|
|
2015-03-29 23:42:45 +00:00
|
|
|
If no -method is specified, then the token is expected. If it is not
|
|
|
|
given on the command-line, it will be asked via user input. If the
|
|
|
|
token is "-", it will be read from stdin.
|
2015-03-04 07:34:32 +00:00
|
|
|
|
2015-03-29 23:42:45 +00:00
|
|
|
By specifying -method, alternate authentication methods can be used
|
|
|
|
such as OAuth or TLS certificates. For these, additional -var flags
|
|
|
|
may be required. It is an error to specify a token with -method.
|
2015-03-04 07:34:32 +00:00
|
|
|
|
2015-03-04 07:52:54 +00:00
|
|
|
General Options:
|
|
|
|
|
|
|
|
-address=TODO The address of the Vault server.
|
|
|
|
|
|
|
|
-ca-cert=path Path to a PEM encoded CA cert file to use to
|
|
|
|
verify the Vault server SSL certificate.
|
|
|
|
|
|
|
|
-ca-path=path Path to a directory of PEM encoded CA cert files
|
|
|
|
to verify the Vault server SSL certificate. If both
|
|
|
|
-ca-cert and -ca-path are specified, -ca-path is used.
|
|
|
|
|
|
|
|
-insecure Do not verify TLS certificate. This is highly
|
|
|
|
not recommended.
|
|
|
|
|
|
|
|
Auth Options:
|
2015-03-04 07:34:32 +00:00
|
|
|
|
2015-04-06 03:50:18 +00:00
|
|
|
-method=name Outputs help for the authentication method with the given
|
|
|
|
name for the remote server. If this authentication method
|
|
|
|
is not available, exit with code 1.
|
2015-04-02 00:01:10 +00:00
|
|
|
|
2015-04-06 03:50:18 +00:00
|
|
|
-methods List the available auth methods.
|
|
|
|
|
|
|
|
-var="key=value" Vars for the authentication method. These are determined
|
|
|
|
on a per-method basis.
|
2015-04-02 00:01:10 +00:00
|
|
|
|
2015-03-04 07:34:32 +00:00
|
|
|
`
|
|
|
|
return strings.TrimSpace(helpText)
|
|
|
|
}
|
2015-04-06 03:50:18 +00:00
|
|
|
|
|
|
|
// tokenAuthHandler handles retrieving the token from the command-line.
|
|
|
|
type tokenAuthHandler struct {
|
|
|
|
Token string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *tokenAuthHandler) Auth(map[string]string) (string, error) {
|
|
|
|
token := h.Token
|
|
|
|
if token == "" {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
// No arguments given, read the token from user input
|
|
|
|
fmt.Printf("Token (will be hidden): ")
|
|
|
|
token, err = password.Read(os.Stdin)
|
|
|
|
fmt.Printf("\n")
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf(
|
|
|
|
"Error attempting to ask for token. The raw error message\n"+
|
|
|
|
"is shown below, but the most common reason for this error is\n"+
|
|
|
|
"that you attempted to pipe a value into auth. If you want to\n"+
|
|
|
|
"pipe the token, please pass '-' as the token argument.\n\n"+
|
|
|
|
"Raw error: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if token == "" {
|
|
|
|
return "", fmt.Errorf(
|
|
|
|
"A token must be passed to auth. Please view the help\n" +
|
|
|
|
"for more information.")
|
|
|
|
}
|
|
|
|
|
|
|
|
return token, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *tokenAuthHandler) Help() string {
|
|
|
|
return ""
|
|
|
|
}
|