2014-08-18 22:05:11 +00:00
---
layout: "docs"
page_title: "ACL System"
sidebar_current: "docs-internals-acl"
2014-10-19 23:40:10 +00:00
description: |-
Consul provides an optional Access Control List (ACL) system which can be used to control access to data and APIs. The ACL system is a Capability-based system that relies on tokens which can have fine grained rules applied to them. It is very similar to AWS IAM in many ways.
2014-08-18 22:05:11 +00:00
---
# ACL System
Consul provides an optional Access Control List (ACL) system which can be used to control
2015-03-28 15:26:26 +00:00
access to data and APIs. The ACL is
[Capability-based ](http://en.wikipedia.org/wiki/Capability-based_security ), relying
2014-11-26 12:57:15 +00:00
on tokens to which fine grained rules can be applied. It is very similar to
2014-08-18 22:05:11 +00:00
[AWS IAM ](http://aws.amazon.com/iam/ ) in many ways.
2015-03-28 15:26:26 +00:00
## Scope
When the ACL system was launched in Consul 0.4, it was only possible to specify
policies for the KV store. In Consul 0.5, ACL policies were extended to service
registrations.
2014-08-18 22:05:11 +00:00
## ACL Design
2015-03-28 15:26:26 +00:00
The ACL system is designed to be easy to use, fast to enforce, and flexible to new
policies, all while providing administrative insight.
2014-08-18 22:05:11 +00:00
2015-03-28 15:26:26 +00:00
Every token has an ID, name, type, and rule set. The ID is a randomly generated
UUID, making it unfeasible to guess. The name is opaque to Consul and human readable.
The type is either "client" (meaning the token cannot modify ACL rules) or "management"
(meaning the token is allowed to perform all actions).
2014-08-18 22:05:11 +00:00
The token ID is passed along with each RPC request to the servers. Agents
2015-03-22 15:44:16 +00:00
can be configured with an [`acl_token` ](/docs/agent/options.html#acl_token ) property
to provide a default token, but the token can also be specified by a client on a
2015-03-28 15:26:26 +00:00
[per-request basis ](/docs/agent/http.html ). ACLs were added in Consul 0.4, meaning
2015-03-22 15:44:16 +00:00
prior versions do not provide a token. This is handled by the special "anonymous"
2015-03-28 15:54:16 +00:00
token. If no token is provided, the rules associated with the anonymous token are
automatically applied: this allows policy to be enforced on legacy clients.
2015-03-22 15:44:16 +00:00
Enforcement is always done by the server nodes. All servers must be configured
to provide an [`acl_datacenter` ](/docs/agent/options.html#acl_datacenter ) which
enables ACL enforcement but also specifies the authoritative datacenter. Consul does not
2015-03-28 15:26:26 +00:00
replicate data cross-WAN and instead relies on [RPC forwarding ](/docs/internal/architecture.html )
to support Multi-Datacenter configurations. However, because requests can be made
across datacenter boundaries, ACL tokens must be valid globally. To avoid
2014-08-18 22:05:11 +00:00
replication issues, a single datacenter is considered authoritative and stores
all the tokens.
2015-03-28 15:26:26 +00:00
When a request is made to a server in a non-authoritative datacenter server, it
must be resolved into the appropriate policy. This is done by reading the token
from the authoritative server and caching the result for a configurable
2015-03-22 15:44:16 +00:00
[`acl_ttl` ](/docs/agent/options.html#acl_ttl ). The implication
2014-11-26 12:57:15 +00:00
of caching is that the cache TTL is an upper bound on the staleness of policy
2014-08-18 22:05:11 +00:00
that is enforced. It is possible to set a zero TTL, but this has adverse
2015-03-28 15:26:26 +00:00
performance impacts, as every request requires refreshing the policy via a
cross-datacenter WAN call.
2014-08-18 22:05:11 +00:00
2015-03-28 15:54:16 +00:00
The Consul ACL system is designed with flexible rules to accommodate for an outage
of the [`acl_datacenter` ](/docs/agent/options.html#acl_datacenter ) or networking
2015-03-28 15:26:26 +00:00
issues preventing access to it. In this case, it may be impossible for
servers in non-authoritative datacenters to resolve tokens. Consul provides
a number of configurable [`acl_down_policy` ](/docs/agent/options.html#acl_down_policy )
choices to tune behavior. It is possible to deny or permit all actions or to ignore
cache TTLs and enter a fail-safe mode. The default is to ignore cache TTLs
for any previously resolved tokens and to deny any uncached tokens.
2014-08-18 22:05:11 +00:00
2014-09-23 13:24:19 +00:00
ACLs can also act in either a whitelist or blacklist mode depending
2015-03-22 15:44:16 +00:00
on the configuration of
2015-03-28 15:54:16 +00:00
[`acl_default_policy` ](/docs/agent/options.html#acl_default_policy ). If the
default policy is to deny all actions, then token rules can be set to whitelist
specific actions. In the inverse, the allow all default behavior is a blacklist
2015-03-28 15:26:26 +00:00
where rules are used to prohibit actions. By default, Consul will allow all
actions.
2014-08-18 22:05:11 +00:00
2014-11-13 22:42:39 +00:00
### Blacklist mode and `consul exec`
2015-03-22 15:44:16 +00:00
If you set [`acl_default_policy` ](/docs/agent/options.html#acl_default_policy )
2015-03-28 15:26:26 +00:00
to `deny` , the `anonymous` token won't have permission to read the default
`_rexec` prefix; therefore, Consul agents using the `anonymous` token
2015-03-22 15:44:16 +00:00
won't be able to perform [`consul exec` ](/docs/commands/exec.html ) actions.
2014-11-13 22:42:39 +00:00
2015-03-28 15:26:26 +00:00
Here's why: the agents need read/write permission to the `_rexec` prefix for
[`consul exec` ](/docs/commands/exec.html ) to work properly. They use that prefix
as the transport for most data.
2014-11-13 22:42:39 +00:00
2015-03-28 15:26:26 +00:00
You can enable [`consul exec` ](/docs/commands/exec.html ) from agents that are not
configured with a token by allowing the `anonymous` token to access that prefix.
This can be done by giving this rule to the `anonymous` token:
2014-11-13 22:42:39 +00:00
```javascript
key "_rexec/" {
policy = "write"
}
```
2015-03-28 15:26:26 +00:00
Alternatively, you can, of course, add an explicit
[`acl_token` ](/docs/agent/options.html#acl_token ) to each agent, giving it access
to that prefix.
2014-11-13 22:42:39 +00:00
### Bootstrapping ACLs
2015-03-03 16:31:56 +00:00
Bootstrapping the ACL system is done by providing an initial [`acl_master_token`
2015-03-28 15:26:26 +00:00
configuration](/docs/agent/options.html#acl_master_token) which will be created
2015-03-22 15:44:16 +00:00
as a "management" type token if it does not exist. Note that the [`acl_master_token`
](/docs/agent/options.html#acl_master_token) is only installed when a server acquires
cluster leadership. If you would like to install or change the
[`acl_master_token` ](/docs/agent/options.html#acl_master_token ), set the new value for
[`acl_master_token` ](/docs/agent/options.html#acl_master_token ) in the configuration
for all servers. Once this is done, restart the current leader to force a leader election.
2014-08-18 22:05:11 +00:00
## Rule Specification
2015-03-28 15:26:26 +00:00
A core part of the ACL system is a rule language which is used to describe the policy
that must be enforced. Consul supports ACLs for both [K/Vs ](/intro/getting-started/kv.html )
and [services ](/intro/getting-started/services.html ).
Key policies are defined by coupling a prefix with a policy. The rules are enforced
2015-03-28 15:54:16 +00:00
using a longest-prefix match policy: Consul picks the most specific policy possible. The
policy is either "read", "write", or "deny". A "write" policy implies "read", and there is no
2015-03-28 15:26:26 +00:00
way to specify write-only. If there is no applicable rule, the
[`acl_default_policy` ](/docs/agent/options.html#acl_default_policy ) is applied.
Service policies are defined by coupling a service name and a policy. The rules are
enforced using an exact match policy. The default rule, applied to any service that doesn't
2015-03-28 15:54:16 +00:00
have a matching policy, is provided using the empty string. A service policy is either "read",
2015-03-28 15:26:26 +00:00
"write", or "deny". A "write" policy implies "read", and there is no way to specify write-only.
If there is no applicable rule, the
[`acl_default_policy` ](/docs/agent/options.html#acl_default_policy ) is
applied. Currently, only the "write" level is enforced for registration of
services; services can always be read.
The policy for the "consul" service is always "write" as it is managed internally by Consul.
We make use of
2014-08-18 22:05:11 +00:00
the [HashiCorp Configuration Language (HCL) ](https://github.com/hashicorp/hcl/ )
to specify policy. This language is human readable and interoperable
2015-03-28 15:26:26 +00:00
with JSON making it easy to machine-generate.
2014-08-18 22:05:11 +00:00
2015-03-28 15:26:26 +00:00
Specification in the HCL format looks like:
2014-08-18 22:05:11 +00:00
2014-10-19 23:40:10 +00:00
```javascript
# Default all keys to read-only
key "" {
policy = "read"
}
key "foo/" {
policy = "write"
}
key "foo/private/" {
2015-03-28 15:26:26 +00:00
# Deny access to the dir "foo/private"
2014-10-19 23:40:10 +00:00
policy = "deny"
}
2014-12-01 03:12:44 +00:00
2015-03-28 15:26:26 +00:00
# Default all services to allow registration
2014-12-01 03:12:44 +00:00
service "" {
policy = "write"
}
service "secure" {
2015-03-28 15:26:26 +00:00
# Deny registration access to service named "secure"
2014-12-01 03:12:44 +00:00
policy = "read"
}
2014-10-19 23:40:10 +00:00
```
2014-08-18 22:05:11 +00:00
This is equivalent to the following JSON input:
2014-10-19 23:40:10 +00:00
```javascript
{
"key": {
"": {
2014-12-01 03:12:44 +00:00
"policy": "read"
2014-10-19 23:40:10 +00:00
},
"foo/": {
2014-12-01 03:12:44 +00:00
"policy": "write"
2014-10-19 23:40:10 +00:00
},
"foo/private": {
2014-12-01 03:12:44 +00:00
"policy": "deny"
2014-08-18 22:05:11 +00:00
}
2014-12-01 03:12:44 +00:00
},
"service": {
"": {
"policy": "write"
},
"secure": {
"policy": "read"
}
2014-10-19 23:40:10 +00:00
}
}
```
2014-08-18 22:05:11 +00:00