2015-04-10 00:19:42 +00:00
|
|
|
---
|
|
|
|
layout: "intro"
|
2017-07-14 15:15:22 +00:00
|
|
|
page_title: "Policies - Getting Started"
|
|
|
|
sidebar_current: "gettingstarted-policies"
|
2015-04-10 00:19:42 +00:00
|
|
|
description: |-
|
2017-07-14 15:15:22 +00:00
|
|
|
Policies in Vault control what a user can access.
|
2015-04-10 00:19:42 +00:00
|
|
|
---
|
|
|
|
|
2017-07-14 15:15:22 +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
|
|
|
|
2017-09-13 01:48:52 +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
|
2015-07-06 00:40:05 +00:00
|
|
|
path "secret/*" {
|
2017-08-17 19:26:29 +00:00
|
|
|
capabilities = ["create"]
|
2015-04-10 00:19:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
path "secret/foo" {
|
2017-07-26 18:05:11 +00:00
|
|
|
capabilities = ["read"]
|
2015-04-10 00:19:42 +00:00
|
|
|
}
|
2017-09-21 17:39:26 +00:00
|
|
|
```
|
2015-10-02 15:06:42 +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
|
|
|
```
|
|
|
|
|
2017-09-13 01:48:52 +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 acl.hcl
|
|
|
|
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
|
|
|
|
path "secret/*" {
|
|
|
|
capabilities = ["create"]
|
|
|
|
}
|
|
|
|
|
|
|
|
path "secret/foo" {
|
|
|
|
capabilities = ["read"]
|
|
|
|
}
|
|
|
|
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
|
|
|
|
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`:
|
|
|
|
|
|
|
|
```text
|
2015-04-10 01:18:05 +00:00
|
|
|
$ vault write secret/bar value=yes
|
|
|
|
Success! Data written to: secret/bar
|
|
|
|
|
|
|
|
$ vault write secret/foo value=yes
|
|
|
|
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
|
|
|
|
2017-09-13 01:48:52 +00:00
|
|
|
## Mapping Policies to Auth Methods
|
2015-04-10 01:18:05 +00:00
|
|
|
|
2017-09-13 01:48:52 +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
|
|
|
|
2017-09-13 01:48:52 +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
|