2017-09-02 22:50:42 +00:00
|
|
|
package token
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
2017-09-08 02:04:30 +00:00
|
|
|
"strconv"
|
2017-09-02 22:50:42 +00:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/hashicorp/vault/api"
|
|
|
|
"github.com/hashicorp/vault/helper/password"
|
|
|
|
)
|
|
|
|
|
|
|
|
type CLIHandler struct {
|
2017-09-03 04:09:18 +00:00
|
|
|
// for tests
|
|
|
|
testStdin io.Reader
|
|
|
|
testStdout io.Writer
|
2017-09-02 22:50:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *CLIHandler) Auth(c *api.Client, m map[string]string) (*api.Secret, error) {
|
2017-09-08 02:04:30 +00:00
|
|
|
// Parse "lookup" first - we want to return an early error if the user
|
|
|
|
// supplied an invalid value here before we prompt them for a token. It would
|
|
|
|
// be annoying to type your token and then be told you supplied an invalid
|
|
|
|
// value that we could have known in advance.
|
|
|
|
lookup := true
|
|
|
|
if x, ok := m["lookup"]; ok {
|
|
|
|
parsed, err := strconv.ParseBool(x)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Failed to parse \"lookup\" as boolean: %s", err)
|
|
|
|
}
|
|
|
|
lookup = parsed
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse the token.
|
2017-09-02 22:50:42 +00:00
|
|
|
token, ok := m["token"]
|
|
|
|
if !ok {
|
|
|
|
// Override the output
|
|
|
|
stdout := h.testStdout
|
|
|
|
if stdout == nil {
|
|
|
|
stdout = os.Stdout
|
|
|
|
}
|
|
|
|
|
|
|
|
// No arguments given, read the token from user input
|
|
|
|
fmt.Fprintf(stdout, "Token (will be hidden): ")
|
|
|
|
var err error
|
|
|
|
token, err = password.Read(os.Stdin)
|
|
|
|
fmt.Fprintf(stdout, "\n")
|
2017-09-03 04:09:18 +00:00
|
|
|
|
2017-09-02 22:50:42 +00:00
|
|
|
if err != nil {
|
2017-09-03 04:09:18 +00:00
|
|
|
if err == password.ErrInterrupted {
|
|
|
|
return nil, fmt.Errorf("user interrupted")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, fmt.Errorf("An error occurred attempting to "+
|
|
|
|
"ask for a token. The raw error message is shown below, but usually "+
|
|
|
|
"this is because you attempted to pipe a value into the command or "+
|
|
|
|
"you are executing outside of a terminal (tty). If you want to pipe "+
|
|
|
|
"the value, pass \"-\" as the argument to read from stdin. The raw "+
|
|
|
|
"error was: %s", err)
|
2017-09-02 22:50:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove any whitespace, etc.
|
|
|
|
token = strings.TrimSpace(token)
|
|
|
|
|
|
|
|
if token == "" {
|
|
|
|
return nil, fmt.Errorf(
|
2017-09-03 04:09:18 +00:00
|
|
|
"A token must be passed to auth. Please view the help for more " +
|
|
|
|
"information.")
|
2017-09-02 22:50:42 +00:00
|
|
|
}
|
|
|
|
|
2017-09-08 02:04:30 +00:00
|
|
|
// If the user declined verification, return now. Note that we will not have
|
|
|
|
// a lot of information about the token.
|
|
|
|
if !lookup {
|
|
|
|
return &api.Secret{
|
|
|
|
Auth: &api.SecretAuth{
|
|
|
|
ClientToken: token,
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we got this far, we want to lookup and lookup the token and pull it's
|
|
|
|
// list of policies an metadata.
|
|
|
|
c.SetToken(token)
|
|
|
|
c.SetWrappingLookupFunc(func(string, string) string { return "" })
|
|
|
|
|
|
|
|
secret, err := c.Auth().Token().LookupSelf()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Error looking up token: %s", err)
|
|
|
|
}
|
|
|
|
if secret == nil {
|
|
|
|
return nil, fmt.Errorf("Empty response from lookup-self")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return an auth struct that "looks" like the response from an auth backend.
|
|
|
|
// lookup and lookup-self return their data in data, not auth. We try to
|
|
|
|
// mirror that data here.
|
2017-09-02 22:50:42 +00:00
|
|
|
return &api.Secret{
|
|
|
|
Auth: &api.SecretAuth{
|
2017-09-08 02:04:30 +00:00
|
|
|
ClientToken: secret.TokenID(),
|
|
|
|
Accessor: secret.TokenAccessor(),
|
|
|
|
Policies: secret.TokenPolicies(),
|
|
|
|
Metadata: secret.TokenMetadata(),
|
|
|
|
|
|
|
|
LeaseDuration: secret.TokenTTLInt(),
|
|
|
|
Renewable: secret.TokenIsRenewable(),
|
2017-09-02 22:50:42 +00:00
|
|
|
},
|
|
|
|
}, nil
|
2017-09-08 02:04:30 +00:00
|
|
|
|
2017-09-02 22:50:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *CLIHandler) Help() string {
|
|
|
|
help := `
|
2017-09-06 14:02:15 +00:00
|
|
|
Usage: vault login TOKEN [CONFIG K=V...]
|
2017-09-02 22:50:42 +00:00
|
|
|
|
2017-09-06 14:02:15 +00:00
|
|
|
The token authentication method allows logging in directly with a token. This
|
|
|
|
can be a token from the "token-create" command or API. There are no
|
|
|
|
configuration options for this authentication method.
|
2017-09-02 22:50:42 +00:00
|
|
|
|
|
|
|
Authenticate using a token:
|
|
|
|
|
2017-09-06 14:02:15 +00:00
|
|
|
$ vault login 96ddf4bc-d217-f3ba-f9bd-017055595017
|
2017-09-02 22:50:42 +00:00
|
|
|
|
2017-09-08 02:04:30 +00:00
|
|
|
Authenticate but do not lookup information about the token:
|
|
|
|
|
|
|
|
$ vault login token=96ddf4bc-d217-f3ba-f9bd-017055595017 lookup=false
|
|
|
|
|
2017-09-02 22:50:42 +00:00
|
|
|
This token usually comes from a different source such as the API or via the
|
|
|
|
built-in "vault token-create" command.
|
|
|
|
|
|
|
|
Configuration:
|
|
|
|
|
|
|
|
token=<string>
|
|
|
|
The token to use for authentication. This is usually provided directly
|
2017-09-06 14:02:15 +00:00
|
|
|
via the "vault login" command.
|
2017-09-08 02:04:30 +00:00
|
|
|
|
|
|
|
lookup=<bool>
|
|
|
|
Perform a lookup of the token's metadata and policies.
|
2017-09-02 22:50:42 +00:00
|
|
|
`
|
|
|
|
|
|
|
|
return strings.TrimSpace(help)
|
|
|
|
}
|