2015-04-18 00:18:20 +00:00
---
2020-01-18 00:18:09 +00:00
layout: docs
page_title: Authentication
description: >-
Before performing any operation with Vault, the connecting client must be
authenticated.
2015-04-18 00:18:20 +00:00
---
# Authentication
2017-07-14 15:15:22 +00:00
Authentication in Vault is the process by which user or machine supplied
information is verified against an internal or external system. Vault supports
2023-01-26 00:12:15 +00:00
multiple [auth methods](/vault/docs/auth) including GitHub,
2017-09-13 01:48:52 +00:00
LDAP, AppRole, and more. Each auth method has a specific use case.
2015-04-18 00:18:20 +00:00
2017-07-14 15:15:22 +00:00
Before a client can interact with Vault, it must _authenticate_ against an
2017-09-13 01:48:52 +00:00
auth method. Upon authentication, a token is generated. This token is
2017-07-14 15:15:22 +00:00
conceptually similar to a session ID on a website. The token may have attached
policy, which is mapped at authentication time. This process is described in
2023-01-26 00:12:15 +00:00
detail in the [policies concepts](/vault/docs/concepts/policies) documentation.
2015-04-18 00:18:20 +00:00
2017-09-13 01:48:52 +00:00
## auth methods
2015-04-18 00:18:20 +00:00
2017-09-13 01:48:52 +00:00
Vault supports a number of auth methods. Some backends are targeted
2017-07-14 15:15:22 +00:00
toward users while others are targeted toward machines. Most authentication
2017-09-13 01:48:52 +00:00
backends must be enabled before use. To enable an auth method:
2015-04-18 00:18:20 +00:00
2020-05-21 17:18:17 +00:00
```shell-session
2017-07-14 15:15:22 +00:00
$ vault write sys/auth/my-auth type=userpass
```
2017-09-21 21:14:40 +00:00
This enables the "userpass" auth method at the path "my-auth". This
2017-07-14 15:15:22 +00:00
authentication will be accessible at the path "my-auth". Often you will see
authentications at the same path as their name, but this is not a requirement.
To learn more about this authentication, use the built-in `path-help` command:
2020-05-21 17:18:17 +00:00
```shell-session
2017-07-14 15:15:22 +00:00
$ vault path-help auth/my-auth
# ...
```
2015-04-18 00:18:20 +00:00
2017-09-13 01:48:52 +00:00
Vault supports multiple auth methods simultaneously, and you can even
mount the same type of auth method at different paths. Only one
2017-07-14 15:15:22 +00:00
authentication is required to gain access to Vault, and it is not currently
2017-09-13 01:48:52 +00:00
possible to force a user through multiple auth methods to gain
2017-07-14 15:15:22 +00:00
access, although some backends do support MFA.
2015-04-18 00:18:20 +00:00
## Tokens
2023-01-26 00:12:15 +00:00
There is an [entire page dedicated to tokens](/vault/docs/concepts/tokens),
2015-04-18 00:18:20 +00:00
but it is important to understand that authentication works by verifying
your identity and then generating a token to associate with that identity.
For example, even though you may authenticate using something like GitHub,
Vault generates a unique access token for you to use for future requests.
The CLI automatically attaches this token to requests, but if you're using
the API you'll have to do this manually.
This token given for authentication with any backend can also be used
with the full set of token commands, such as creating new sub-tokens,
revoking tokens, and renewing tokens. This is all covered on the
2023-01-26 00:12:15 +00:00
[token concepts page](/vault/docs/concepts/tokens).
2015-04-18 00:18:20 +00:00
## Authenticating
2017-07-14 15:15:22 +00:00
### Via the CLI
2015-04-18 00:18:20 +00:00
2018-04-25 21:59:38 +00:00
To authenticate with the CLI, `vault login` is used. This supports many
2017-09-13 01:48:52 +00:00
of the built-in auth methods. For example, with GitHub:
2015-04-18 00:18:20 +00:00
2020-05-21 17:18:17 +00:00
```shell-session
2017-09-13 01:48:52 +00:00
$ vault login -method=github token=<token>
2015-04-18 00:18:20 +00:00
...
```
After authenticating, you will be logged in. The CLI command will also
output your raw token. This token is used for revocation and renewal.
As the user logging in, the primary use case of the token is renewal,
covered below in the "Auth Leases" section.
2017-09-13 01:48:52 +00:00
To determine what variables are needed for an auth method,
2015-04-18 00:18:20 +00:00
supply the `-method` flag without any additional arguments and help
will be shown.
If you're using a method that isn't supported via the CLI, then the API
must be used.
2017-07-14 15:15:22 +00:00
### Via the API
2015-04-18 00:18:20 +00:00
API authentication is generally used for machine authentication. Each
2017-09-13 01:48:52 +00:00
auth method implements its own login endpoint. Use the `vault path-help`
2015-04-18 00:18:20 +00:00
mechanism to find the proper endpoint.
For example, the GitHub login endpoint is located at `auth/github/login`.
2015-07-13 10:12:09 +00:00
And to determine the arguments needed, `vault path-help auth/github/login` can
2015-04-18 00:18:20 +00:00
be used.
## Auth Leases
Just like secrets, identities have
2023-01-26 00:12:15 +00:00
[leases](/vault/docs/concepts/lease) associated with them. This means that
2015-04-18 00:18:20 +00:00
you must reauthenticate after the given lease period to continue accessing
Vault.
To set the lease associated with an identity, reference the help for
2017-09-13 01:48:52 +00:00
the specific auth method in use. It is specific to each backend
2015-04-18 00:18:20 +00:00
how leasing is implemented.
And just like secrets, identities can be renewed without having to
2018-01-15 20:19:28 +00:00
completely reauthenticate. Just use `vault token renew <token>` with the
2015-09-12 01:08:32 +00:00
leased token associated with your identity to renew it.
2021-09-30 17:46:01 +00:00
## Code Example
The following code snippet demonstrates how to renew auth tokens.
2021-12-22 17:33:12 +00:00
<CodeTabs heading="token renewal example">
2021-09-30 17:46:01 +00:00
<CodeBlockConfig lineNumbers>
```go
package main
import (
2021-11-17 19:52:38 +00:00
"context"
2021-09-30 17:46:01 +00:00
"fmt"
"log"
vault "github.com/hashicorp/vault/api"
2022-07-12 19:06:44 +00:00
auth "github.com/hashicorp/vault/api/auth/userpass"
2021-09-30 17:46:01 +00:00
)
2021-11-17 19:52:38 +00:00
// Once you've set the token for your Vault client, you will need to
// periodically renew its lease.
2021-09-30 17:46:01 +00:00
//
// A function like this should be run as a goroutine to avoid blocking.
//
2021-11-17 19:52:38 +00:00
// Production applications may also wish to be more tolerant of failures and
// retry rather than exiting.
2021-09-30 17:46:01 +00:00
//
2021-11-17 19:52:38 +00:00
// Additionally, enterprise Vault users should be aware that due to eventual
// consistency, the API may return unexpected errors when running Vault with
// performance standbys or performance replication, despite the client having
// a freshly renewed token. See https://www.vaultproject.io/docs/enterprise/consistency#vault-1-7-mitigations
// for several ways to mitigate this which are outside the scope of this code sample.
2021-09-30 17:46:01 +00:00
func renewToken(client *vault.Client) {
for {
vaultLoginResp, err := login(client)
if err != nil {
log.Fatalf("unable to authenticate to Vault: %v", err)
}
tokenErr := manageTokenLifecycle(client, vaultLoginResp)
if tokenErr != nil {
log.Fatalf("unable to start managing token lifecycle: %v", tokenErr)
}
}
}
2021-11-17 19:52:38 +00:00
// Starts token lifecycle management. Returns only fatal errors as errors,
// otherwise returns nil so we can attempt login again.
2021-09-30 17:46:01 +00:00
func manageTokenLifecycle(client *vault.Client, token *vault.Secret) error {
renew := token.Auth.Renewable // You may notice a different top-level field called Renewable. That one is used for dynamic secrets renewal, not token renewal.
if !renew {
log.Printf("Token is not configured to be renewable. Re-attempting login.")
return nil
}
watcher, err := client.NewLifetimeWatcher(&vault.LifetimeWatcherInput{
Secret: token,
Increment: 3600, // Learn more about this optional value in https://www.vaultproject.io/docs/concepts/lease#lease-durations-and-renewal
})
if err != nil {
return fmt.Errorf("unable to initialize new lifetime watcher for renewing auth token: %w", err)
}
go watcher.Start()
defer watcher.Stop()
for {
select {
2021-11-17 19:52:38 +00:00
// `DoneCh` will return if renewal fails, or if the remaining lease
// duration is under a built-in threshold and either renewing is not
// extending it or renewing is disabled. In any case, the caller
// needs to attempt to log in again.
case err := <-watcher.DoneCh():
if err != nil {
log.Printf("Failed to renew token: %v. Re-attempting login.", err)
return nil
}
2021-12-22 17:33:12 +00:00
// This occurs once the token has reached max TTL.
2021-11-17 19:52:38 +00:00
log.Printf("Token can no longer be renewed. Re-attempting login.")
return nil
// Successfully completed renewal
case renewal := <-watcher.RenewCh():
log.Printf("Successfully renewed: %#v", renewal)
}
}
2021-09-30 17:46:01 +00:00
}
func login(client *vault.Client) (*vault.Secret, error) {
2021-11-17 19:52:38 +00:00
// WARNING: A plaintext password like this is obviously insecure.
2021-12-22 17:33:12 +00:00
// See the hashicorp/vault-examples repo for full examples of how to securely
2021-11-17 19:52:38 +00:00
// log in to Vault using various auth methods. This function is just
// demonstrating the basic idea that a *vault.Secret is returned by
// the login call.
userpassAuth, err := auth.NewUserpassAuth("my-user", &auth.Password{FromString: "my-password"})
if err != nil {
return nil, fmt.Errorf("unable to initialize userpass auth method: %w", err)
}
authInfo, err := client.Auth().Login(context.TODO(), userpassAuth)
if err != nil {
return nil, fmt.Errorf("unable to login to userpass auth method: %w", err)
}
if authInfo == nil {
return nil, fmt.Errorf("no auth info was returned after login")
}
return authInfo, nil
2021-09-30 17:46:01 +00:00
}
```
</CodeBlockConfig>
</CodeTabs>