open-vault/website/source/intro/getting-started/policies.html.md

232 lines
6.4 KiB
Markdown
Raw Normal View History

2015-04-10 00:19:42 +00:00
---
layout: "intro"
page_title: "Policies - Getting Started"
New Docs Website (#5535) * 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
2018-10-19 15:40:11 +00:00
sidebar_title: "Policies"
sidebar_current: "gettingstarted-policies"
2015-04-10 00:19:42 +00:00
description: |-
Policies in Vault control what a user can access.
2015-04-10 00:19:42 +00:00
---
# Policies
2015-04-10 00:19:42 +00:00
2017-09-21 17:39:26 +00:00
Policies in Vault control what a user can access. In the last section, we
learned about _authentication_. This section is about _authorization_.
2015-04-10 00:19:42 +00:00
For authentication Vault has multiple options or methods that can be enabled and
used. For authorization and policies Vault always uses the same format. All auth
2017-09-21 17:39:26 +00:00
methods map identities back to the core policies that are configured with Vault.
2015-04-10 00:19:42 +00:00
2017-09-21 17:39:26 +00:00
There are some built-in policies that cannot be removed. For example, the `root`
and `default` policies are required policies and cannot be deleted. The
`default` policy provides a common set of permissions and is included on all
tokens by default. The `root` policy gives a token super admin permissions,
similar to a root user on a linux machine.
2015-04-10 00:19:42 +00:00
## Policy Format
2017-09-21 17:39:26 +00:00
Policies are authored in [HCL][hcl], but it is JSON compatible. Here is an
example policy:
2015-04-10 00:19:42 +00:00
2017-09-21 17:39:26 +00:00
```hcl
# Normal servers have version 1 of KV mounted by default, so will need these
# paths:
path "secret/*" {
capabilities = ["create"]
2015-04-10 00:19:42 +00:00
}
path "secret/foo" {
capabilities = ["read"]
2015-04-10 00:19:42 +00:00
}
# Dev servers have version 2 of KV mounted by default, so will need these
# paths:
path "secret/data/*" {
capabilities = ["create"]
}
path "secret/data/foo" {
capabilities = ["read"]
}
2017-09-21 17:39:26 +00:00
```
2017-09-21 17:39:26 +00:00
With this policy, a user could write any secret to `secret/`, except to
`secret/foo`, where only read access is allowed. Policies default to deny, so
any access to an unspecified path is not allowed.
Do not worry about getting the exact policy format correct. Vault includes a
command that will format the policy automatically according to specification. It
also reports on any syntax errors.
```text
$ vault policy fmt my-policy.hcl
2015-04-10 00:19:42 +00:00
```
The policy format uses a prefix matching system on the API path to determine
access control. The most specific defined policy is used, either an exact match
or the longest-prefix glob match. Since everything in Vault must be accessed via
the API, this gives strict control over every aspect of Vault, including
2017-09-21 17:39:26 +00:00
enabling secrets engines, enabling auth methods, authenticating, as well as
secret access.
2015-04-10 00:19:42 +00:00
## Writing the Policy
2017-09-21 17:39:26 +00:00
To write a policy using the command line, specify the path to a policy file to
upload.
2015-04-10 00:19:42 +00:00
2017-09-21 17:39:26 +00:00
```text
$ vault policy write my-policy my-policy.hcl
2017-09-21 17:39:26 +00:00
Success! Uploaded policy: my-policy
2015-04-10 00:19:42 +00:00
```
2017-09-21 17:39:26 +00:00
Here is an example you can copy-paste in the terminal:
2015-04-10 00:19:42 +00:00
2017-09-21 17:39:26 +00:00
```text
$ vault policy write my-policy -<<EOF
# Normal servers have version 1 of KV mounted by default, so will need these
# paths:
2017-09-21 17:39:26 +00:00
path "secret/*" {
capabilities = ["create"]
}
path "secret/foo" {
capabilities = ["read"]
}
# Dev servers have version 2 of KV mounted by default, so will need these
# paths:
path "secret/data/*" {
capabilities = ["create"]
}
path "secret/data/foo" {
capabilities = ["read"]
}
2017-09-21 17:39:26 +00:00
EOF
```
2015-04-10 00:19:42 +00:00
2017-09-21 17:39:26 +00:00
To see the list of policies, run:
2015-04-10 00:19:42 +00:00
2017-09-21 17:39:26 +00:00
```text
$ vault policy list
default
my-policy
root
2015-04-10 00:19:42 +00:00
```
2017-09-21 17:39:26 +00:00
To view the contents of a policy, run:
```text
$ vault policy read my-policy
# Normal servers have version 1 of KV mounted by default, so will need these
# paths:
2017-09-21 17:39:26 +00:00
path "secret/*" {
capabilities = ["create"]
}
...
2015-04-10 01:18:05 +00:00
```
2017-09-21 17:39:26 +00:00
## Testing the Policy
2015-04-10 01:18:05 +00:00
2017-09-21 17:39:26 +00:00
To use the policy, create a token and assign it to that policy:
```text
$ vault token create -policy=my-policy
Key Value
--- -----
token a4ebda12-23bf-5cf4-f80e-803ee2f37aab
token_accessor aba6256e-401e-9591-31b2-a27048cb15ed
token_duration 768h
token_renewable true
token_policies [default my-policy]
$ vault login a4ebda12-23bf-5cf4-f80e-803ee2f37aab
Success! You are now authenticated. The token information displayed below
is already stored in the token helper. You do NOT need to run "vault login"
again. Future Vault requests will automatically use this token.
Key Value
--- -----
token a4ebda12-23bf-5cf4-f80e-803ee2f37aab
token_accessor aba6256e-401e-9591-31b2-a27048cb15ed
token_duration 767h59m18s
token_renewable true
token_policies [default my-policy]
2015-04-10 00:19:42 +00:00
```
2017-09-21 17:39:26 +00:00
Verify that you can write any data to `secret/`, but only read from
`secret/foo`:
### Dev servers
```text
$ vault kv put secret/bar robot=beepboop
Key Value
--- -----
created_time 2018-05-22T18:05:42.537496856Z
deletion_time n/a
destroyed false
version 1
$ vault kv put secret/foo robot=beepboop
Error writing data to secret/data/foo: Error making API request.
URL: PUT http://127.0.0.1:8200/v1/secret/data/foo
Code: 403. Errors:
* permission denied
```
### Non-dev servers
2017-09-21 17:39:26 +00:00
```text
$ vault kv put secret/bar robot=beepboop
2015-04-10 01:18:05 +00:00
Success! Data written to: secret/bar
$ vault kv put secret/foo robot=beepboop
2015-04-10 01:18:05 +00:00
Error writing data to secret/foo: Error making API request.
URL: PUT http://127.0.0.1:8200/v1/secret/foo
2016-11-25 14:47:27 +00:00
Code: 403. Errors:
2015-04-10 01:18:05 +00:00
* permission denied
```
2017-09-21 17:39:26 +00:00
You also do not have access to `sys` according to the policy, so commands like
`vault policy list` or `vault secrets list` will not work. Re-authenticate as
the initial root token to continue:
```text
$ vault login <initial-root-token>
```
2015-04-10 01:18:05 +00:00
## Mapping Policies to Auth Methods
2015-04-10 01:18:05 +00:00
Vault is the single policy authority, unlike auth where you can enable multiple
auth methods. Any enabled auth method must map identities to these core
policies.
2015-04-10 01:18:05 +00:00
We use the `vault path-help` system with your auth method to determine how the
2017-09-21 17:39:26 +00:00
mapping is done, since it is specific to each auth method. For example, with
GitHub, it is done by team using the `map/teams/<team>` path:
2015-04-10 01:18:05 +00:00
2017-09-21 17:39:26 +00:00
```text
$ vault write auth/github/map/teams/default value=my-policy
2015-04-10 01:18:05 +00:00
Success! Data written to: auth/github/map/teams/default
```
2017-09-21 17:39:26 +00:00
For GitHub, the `default` team is the default policy set that everyone is
assigned to no matter what team they're on.
2015-04-10 01:18:05 +00:00
2017-09-21 17:39:26 +00:00
Other auth methods use alternate, but likely similar mechanisms for mapping
policies to identity.
2015-04-10 01:18:05 +00:00
## Next
2017-09-21 17:39:26 +00:00
Policies are an important part of Vault. While using the root token is easiest
to get up and running, you will want to restrict access to Vault very quickly,
and the policy system is the way to do this.
The syntax and function of policies is easy to understand and work with, and
because auth methods all must map to the central policy system, you only have to
learn this policy system.
2015-04-10 01:18:05 +00:00
2017-09-21 17:39:26 +00:00
Next, we will cover how to [deploy Vault](/intro/getting-started/deploy.html).
2015-04-10 01:18:05 +00:00
2017-09-21 17:39:26 +00:00
[HCL]: https://github.com/hashicorp/hcl