* conversion stage 1 * correct image paths * add sidebar title to frontmatter * docs/concepts and docs/internals * configuration docs and multi-level nav corrections * commands docs, index file corrections, small item nav correction * secrets converted * auth * add enterprise and agent docs * add extra dividers * secret section, wip * correct sidebar nav title in front matter for apu section, start working on api items * auth and backend, a couple directory structure fixes * remove old docs * intro side nav converted * reset sidebar styles, add hashi-global-styles * basic styling for nav sidebar * folder collapse functionality * patch up border length on last list item * wip restructure for content component * taking middleman hacking to the extreme, but its working * small css fix * add new mega nav * fix a small mistake from the rebase * fix a content resolution issue with middleman * title a couple missing docs pages * update deps, remove temporary markup * community page * footer to layout, community page css adjustments * wip downloads page * deps updated, downloads page ready * fix community page * homepage progress * add components, adjust spacing * docs and api landing pages * a bunch of fixes, add docs and api landing pages * update deps, add deploy scripts * add readme note * update deploy command * overview page, index title * Update doc fields Note this still requires the link fields to be populated -- this is solely related to copy on the description fields * Update api_basic_categories.yml Updated API category descriptions. Like the document descriptions you'll still need to update the link headers to the proper target pages. * Add bottom hero, adjust CSS, responsive friendly * Add mega nav title * homepage adjustments, asset boosts * small fixes * docs page styling fixes * meganav title * some category link corrections * Update API categories page updated to reflect the second level headings for api categories * Update docs_detailed_categories.yml Updated to represent the existing docs structure * Update docs_detailed_categories.yml * docs page data fix, extra operator page remove * api data fix * fix makefile * update deps, add product subnav to docs and api landing pages * Rearrange non-hands-on guides to _docs_ Since there is no place for these on learn.hashicorp, we'll put them under _docs_. * WIP Redirects for guides to docs * content and component updates * font weight hotfix, redirects * fix guides and intro sidenavs * fix some redirects * small style tweaks * Redirects to learn and internally to docs * Remove redirect to `/vault` * Remove `.html` from destination on redirects * fix incorrect index redirect * final touchups * address feedback from michell for makefile and product downloads
7.2 KiB
layout | page_title | sidebar_title | sidebar_current | description |
---|---|---|---|---|
intro | Using the HTTP APIs with Authentication | HTTP API | gettingstarted-apis | Using the HTTP APIs for authentication and secret access. |
Using the HTTP APIs with Authentication
All of Vault's capabilities are accessible via the HTTP API in addition to the CLI. In fact, most calls from the CLI actually invoke the HTTP API. In some cases, Vault features are not available via the CLI and can only be accessed via the HTTP API.
Once you have started the Vault server, you can use curl
or any other http
client to make API calls. For example, if you started the Vault server in
dev mode, you could validate the
initialization status like this:
$ curl http://127.0.0.1:8200/v1/sys/init
This will return a JSON response:
{
"initialized": true
}
Accessing Secrets via the REST APIs
Machines that need access to information stored in Vault will most likely access Vault via its REST API. For example, if a machine were using AppRole for authentication, the application would first authenticate to Vault which would return a Vault API token. The application would use that token for future communication with Vault.
For the purpose of this guide, we will use the following configuration which disables TLS and uses a file-based backend. TLS is disabled here only for example purposes; it should never be disabled in production.
backend "file" {
path = "vault"
}
listener "tcp" {
tls_disable = 1
}
Save this file on disk as config.hcl
and then start the Vault server:
$ vault server -config=config.hcl
At this point, we can use Vault's API for all our interactions. For example, we can initialize Vault like this:
$ curl \
--request POST \
--data '{"secret_shares": 1, "secret_threshold": 1}' \
http://127.0.0.1:8200/v1/sys/init
The response should be JSON and looks something like this:
{
"keys": [
"373d500274dd8eb95271cb0f868e4ded27d9afa205d1741d60bb97cd7ce2fe41"
],
"keys_base64": [
"Nz1QAnTdjrlSccsPho5N7SfZr6IF0XQdYLuXzXzi/kE="
],
"root_token": "6fa4128e-8bd2-fd02-0ea8-a5e020d9b766"
}
This response contains our initial root token. It also includes the unseal key. You can use the unseal key to unseal the Vault and use the root token to perform other requests in Vault that require authentication.
To make this guide easy to copy-and-paste, we will be using the environment
variable $VAULT_TOKEN
to store the root token. You can export this Vault
token in your current shell like this:
$ export VAULT_TOKEN=6fa4128e-8bd2-fd02-0ea8-a5e020d9b766
Using the unseal key (not the root token) from above, you can unseal the Vault via the HTTP API:
$ curl \
--request POST \
--data '{"key": "Nz1QAnTdjrlSccsPho5N7SfZr6IF0XQdYLuXzXzi/kE="}' \
http://127.0.0.1:8200/v1/sys/unseal
Note that you should replace Nz1QAnT...
with the generated key from your
output. This will return a JSON response:
{
"sealed": false,
"t": 1,
"n": 1,
"progress": 0,
"nonce": "",
"version": "1.2.3",
"cluster_name": "vault-cluster-9d524900",
"cluster_id": "d69ab1b0-7e9a-2523-0d05-b0bfd09caeea"
}
Now any of the available auth methods can be enabled and configured. For the purposes of this guide lets enable AppRole authentication.
Start by enabling the AppRole authentication.
$ curl \
--header "X-Vault-Token: $VAULT_TOKEN" \
--request POST \
--data '{"type": "approle"}' \
http://127.0.0.1:8200/v1/sys/auth/approle
Notice that the request to enable the AppRole endpoint needed an authentication token. In this case we are passing the root token generated when we started the Vault server. We could also generate tokens using any other authentication mechanisms, but we will use the root token for simplicity.
Now create an AppRole with desired set of ACL
policies. In the following command, it is being
specified that the tokens issued under the AppRole my-role
, should be
associated with dev-policy
and the my-policy
.
$ curl \
--header "X-Vault-Token: $VAULT_TOKEN" \
--request POST \
--data '{"policies": ["dev-policy", "my-policy"]}' \
http://127.0.0.1:8200/v1/auth/approle/role/my-role
The AppRole backend, in its default configuration expects two hard to guess
credentials, a role ID and a secret ID. This command fetches the role ID of the
my-role
.
$ curl \
--header "X-Vault-Token: $VAULT_TOKEN" \
http://127.0.0.1:8200/v1/auth/approle/role/my-role/role-id
The response will include a data
key with the role_id
:
{
"data": {
"role_id": "86a32a73-1f2b-05e0-113a-dfa930145d72"
}
}
This command creates a new secret ID under the my-role
.
$ curl \
--header "X-Vault-Token: $VAULT_TOKEN" \
--request POST \
http://127.0.0.1:8200/v1/auth/approle/role/my-role/secret-id
The response will include the secret_id
in the data
key:
{
"data": {
"secret_id": "cd4b2002-3e3b-aceb-378d-5caa84dffd14",
"secret_id_accessor": "6b9b58f6-d11a-c73c-ffa8-04a47d42716b"
}
}
These two credentials can be supplied to the login endpoint to fetch a new Vault token.
$ curl \
--request POST \
--data '{"role_id": "86a32a73-1f2b-05e0-113a-dfa930145d72", "secret_id": "cd4b2002-3e3b-aceb-378d-5caa84dffd14"}' \
http://127.0.0.1:8200/v1/auth/approle/login
The response will be JSON, under the key auth
:
{
"auth": {
"client_token": "50617721-dfb5-1916-7b13-4091e169d28c",
"accessor": "ada8d354-47c0-5d9e-50f9-d74e6de2df9b",
"policies": [
"default",
"dev-policy",
"my-policy"
],
"metadata": {
"role_name": "my-role"
},
"lease_duration": 2764800,
"renewable": true
}
}
The returned client token (50617721...
) can be used to authenticate with
Vault. This token will be authorized with specific capabilities on all the
resources encompassed by the policies default
, dev-policy
and my-policy
.
The newly acquired token can be exported as a new VAULT_TOKEN
and use it to
authenticate Vault requests.
$ export VAULT_TOKEN="50617721-dfb5-1916-7b13-4091e169d28c"
$ curl \
--header "X-Vault-Token: $VAULT_TOKEN" \
--request POST \
--data '{"bar": "baz"}' \
http://127.0.0.1:8200/v1/secret/foo
This will create a new secret named "foo" with the given JSON contents. We can read this value back with the same token:
$ curl \
--header "X-Vault-Token: $VAULT_TOKEN" \
http://127.0.0.1:8200/v1/secret/foo
This should return a response like this:
{
"data": {
"bar": "baz"
},
"lease_duration": 2764800,
"renewable": false,
"request_id": "5e246671-ec05-6fc8-9f93-4fe4512f34ab"
}
You can see the documentation on the HTTP APIs for more details on other available endpoints.
Congratulations! You now know all the basics to get started with Vault.
Next
Next, we have a page dedicated to next steps depending on what you would like to achieve.