github.com/sl1pm4t/consul@v1.4.5-0.20190325224627-74c31c540f9c/website/source/docs/guides/acl-legacy.html.md (about)

     1  ---
     2  layout: "docs"
     3  page_title: "ACL System (Legacy)"
     4  sidebar_current: "docs-acl-legacy"
     5  description: |-
     6    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.
     7  ---
     8  
     9  -> **1.3.0 and earlier:** This guide only applies in Consul versions 1.3.0 and before. If you are using the 1.4.0 or later please use the updated guide [here](/docs/guides/acl.html)
    10  
    11  
    12  # Deprecation Notice
    13  
    14  The ACL system described here was Consul's original ACL implementation. In Consul 1.4.0
    15  the ACL system was rewritten and the legacy system was deprecated. The new ACL guide
    16  can be found [here](https://learn.hashicorp.com/consul/advanced/day-1-operations/acl-guide).
    17  
    18  # New ACL System Differences
    19  
    20  The [ACL guide](/docs/guides/acl.html) and [legacy ACL
    21  guide](/docs/guides/acl-legacy.html) describes the new and old systems in
    22  detail. Below is a summary of the changes that need to be considered when
    23  migrating legacy tokens to the new system.
    24  
    25  ## Token and Policy Separation
    26  
    27  You can use a single policy in the new system for all tokens that share access
    28  rules. For example, all tokens created using the clone endpoint in the legacy
    29  system can be represented with a single policy and a set of tokens that map to
    30  that policy.
    31  
    32  ## Rule Syntax Changes
    33  
    34  The most significant change is that rules with selectors _no longer prefix match
    35  by default_. In the legacy system the following rules would grant access to
    36  nodes, services and keys _prefixed_ with foo.
    37  
    38  ```
    39  node "foo" { policy = "write" }
    40  service "foo" { policy = "write" }
    41  key "foo" { policy = "write" }
    42  ```
    43  
    44  In the new system the same syntax will only perform _exact_ match on the whole
    45  node name, service name or key.
    46  
    47  In general, exact match is what most operators intended most of the time so the
    48  same policy can be kept, however if you rely on prefix match behavior then using
    49  the same syntax will break behavior.
    50  
    51  Prefix matching can be expressed in the new ACL system explicitly, making the
    52  following rules in the new system exactly the same as the rules above in the
    53  old.
    54  
    55  ```
    56  node_prefix "foo" { policy = "write" }
    57  service_prefix "foo" { policy = "write" }
    58  key_prefix "foo" { policy = "write" }
    59  ```
    60  
    61  ## API Separation
    62  
    63  The "old" API endpoints below continue to work for backwards compatibility but
    64  will continue to create or show only "legacy" tokens that can't take full
    65  advantage of the new ACL system improvements. They are documented fully under
    66  [Legacy Tokens](/api/acl/legacy.html).
    67  
    68  - [`PUT /acl/create` - Create Legacy Token](/api/acl/legacy.html#create-acl-token)
    69  - [`PUT /acl/update` - Update Legacy Token](/api/acl/legacy.html#update-acl-token)
    70  - [`PUT /acl/destroy/:uuid` - Delete Legacy Token](/api/acl/legacy.html#delete-acl-token)
    71  - [`GET /acl/info/:uuid` - Read Legacy Token](/api/acl/legacy.html#read-acl-token)
    72  - [`PUT /acl/clone/:uuid` - Clone Legacy Token](/api/acl/legacy.html#clone-acl-token)
    73  - [`GET /acl/list` - List Legacy Tokens](/api/acl/legacy.html#list-acls)
    74  
    75  The new ACL system includes new API endpoints to manage
    76  the [ACL System](/api/acl/acl.html), [Tokens](/api/acl/tokens.html)
    77  and [Policies](/api/acl/policies.html).
    78  
    79  # Legacy ACL System
    80  
    81  Consul provides an optional Access Control List (ACL) system which can be used to control
    82  access to data and APIs. The ACL is
    83  [Capability-based](https://en.wikipedia.org/wiki/Capability-based_security), relying
    84  on tokens to which fine grained rules can be applied. It is very similar to
    85  [AWS IAM](http://aws.amazon.com/iam/) in many ways.
    86  
    87  ## ACL System Overview
    88  
    89  The ACL system is designed to be easy to use, fast to enforce, and flexible to new policies,
    90  all while providing administrative insight.
    91  
    92  #### ACL Tokens
    93  
    94  The ACL system is based on tokens, which are managed by Consul operators via Consul's
    95  [ACL API](/api/acl/acl.html), or systems like
    96  [HashiCorp's Vault](https://www.vaultproject.io/docs/secrets/consul/index.html).
    97  
    98  Every token has an ID, name, type, and rule set. The ID is a randomly generated
    99  UUID, making it infeasible to guess. The name is opaque to Consul and human readable.
   100  The type is either "client" (meaning the token cannot modify ACL rules) or "management"
   101  (meaning the token is allowed to perform all actions).
   102  
   103  The token ID is passed along with each RPC request to the servers. Consul's
   104  [HTTP endpoints](/api/index.html) can accept tokens via the `token`
   105  query string parameter, or the `X-Consul-Token` request header, or Authorization Bearer
   106  token [RFC6750](https://tools.ietf.org/html/rfc6750). Consul's
   107  [CLI commands](/docs/commands/index.html) can accept tokens via the
   108  `token` argument, or the `CONSUL_HTTP_TOKEN` environment variable.
   109  
   110  If no token is provided, the rules associated with a special, configurable anonymous
   111  token are automatically applied. The anonymous token is managed using the
   112  [ACL API](/api/acl/acl.html) like any other ACL token, but using `anonymous` for the ID.
   113  
   114  #### ACL Rules and Scope
   115  
   116  Tokens are bound to a set of rules that control which Consul resources the token
   117  has access to. Policies can be defined in either a whitelist or blacklist mode
   118  depending on the configuration of
   119  [`acl_default_policy`](/docs/agent/options.html#acl_default_policy). If the default
   120  policy is to "deny" all actions, then token rules can be set to whitelist specific
   121  actions. In the inverse, the "allow" all default behavior is a blacklist where rules
   122  are used to prohibit actions. By default, Consul will allow all actions.
   123  
   124  The following table summarizes the ACL policies that are available for constructing
   125  rules:
   126  
   127  | Policy                   | Scope |
   128  | ------------------------ | ----- |
   129  | [`agent`](#agent-rules)          | Utility operations in the [Agent API](/api/agent.html), other than service and check registration |
   130  | [`event`](#event-rules)          | Listing and firing events in the [Event API](/api/event.html) |
   131  | [`key`](#key-value-rules)        | Key/value store operations in the [KV Store API](/api/kv.html) |
   132  | [`keyring`](#keyring-rules)      | Keyring operations in the [Keyring API](/api/operator/keyring.html) |
   133  | [`node`](#node-rules)            | Node-level catalog operations in the [Catalog API](/api/catalog.html), [Health API](/api/health.html), [Prepared Query API](/api/query.html), [Network Coordinate API](/api/coordinate.html), and [Agent API](/api/agent.html) |
   134  | [`operator`](#operator-rules)    | Cluster-level operations in the [Operator API](/api/operator.html), other than the [Keyring API](/api/operator/keyring.html) |
   135  | [`query`](#prepared-query-rules) | Prepared query operations in the [Prepared Query API](/api/query.html)
   136  | [`service`](#service-rules)      | Service-level catalog operations in the [Catalog API](/api/catalog.html), [Health API](/api/health.html), [Prepared Query API](/api/query.html), and [Agent API](/api/agent.html) |
   137  | [`session`](#session-rules)      | Session operations in the [Session API](/api/session.html) |
   138  
   139  Since Consul snapshots actually contain ACL tokens, the
   140  [Snapshot API](/api/snapshot.html) requires a management token for snapshot operations
   141  and does not use a special policy.
   142  
   143  The following resources are not covered by ACL policies:
   144  
   145  1. The [Status API](/api/status.html) is used by servers when bootstrapping and exposes
   146  basic IP and port information about the servers, and does not allow modification
   147  of any state.
   148  
   149  2. The datacenter listing operation of the
   150  [Catalog API](/api/catalog.html#list-datacenters) similarly exposes the names of known
   151  Consul datacenters, and does not allow modification of any state.
   152  
   153  Constructing rules from these policies is covered in detail in the
   154  [Rule Specification](#rule-specification) section below.
   155  
   156  #### ACL Datacenter
   157  
   158  All nodes (clients and servers) must be configured with a
   159  [`primary_datacenter`](/docs/agent/options.html#primary_datacenter) which enables ACL
   160  enforcement but also specifies the authoritative datacenter. Consul relies on
   161  [RPC forwarding](/docs/internals/architecture.html) to support multi-datacenter
   162  configurations. However, because requests can be made across datacenter boundaries,
   163  ACL tokens must be valid globally. To avoid consistency issues, a single datacenter
   164  is considered authoritative and stores the canonical set of tokens.
   165  
   166  When a request is made to an agent in a non-authoritative datacenter, it must be
   167  resolved into the appropriate policy. This is done by reading the token from the
   168  authoritative server and caching the result for a configurable
   169  [`acl_ttl`](/docs/agent/options.html#acl_ttl). The implication of caching is that
   170  the cache TTL is an upper bound on the staleness of policy that is enforced. It is
   171  possible to set a zero TTL, but this has adverse performance impacts, as every
   172  request requires refreshing the policy via an RPC call.
   173  
   174  During an outage of the ACL datacenter, or loss of connectivity, the cache will be
   175  used as long as the TTL is valid, or the cache may be extended if the
   176  [`acl_down_policy`](/docs/agent/options.html#acl_down_policy) is set accordingly.
   177  This configuration also allows the ACL system to fail open or closed.
   178  [ACL replication](#replication) is also available to allow for the full set of ACL
   179  tokens to be replicated for use during an outage.
   180  
   181  ## Configuring ACLs
   182  
   183  ACLs are configured using several different configuration options. These are marked
   184  as to whether they are set on servers, clients, or both.
   185  
   186  | Configuration Option | Servers | Clients | Purpose |
   187  | -------------------- | ------- | ------- | ------- |
   188  | [`primary_datacenter`](/docs/agent/options.html#primary_datacenter) | `REQUIRED` | `REQUIRED` | Master control that enables ACLs by defining the authoritative Consul datacenter for ACLs |
   189  | [`acl_default_policy`](/docs/agent/options.html#acl_default_policy_legacy) | `OPTIONAL` | `N/A` | Determines whitelist or blacklist mode |
   190  | [`acl_down_policy`](/docs/agent/options.html#acl_down_policy_legacy) | `OPTIONAL` | `OPTIONAL` | Determines what to do when the ACL datacenter is offline |
   191  | [`acl_ttl`](/docs/agent/options.html#acl_ttl_legacy) | `OPTIONAL` | `OPTIONAL` | Determines time-to-live for cached ACLs |
   192  
   193  There are some additional configuration items related to [ACL replication](#replication) and
   194  [Version 8 ACL support](#version_8_acls). These are discussed in those respective sections
   195  below.
   196  
   197  A number of special tokens can also be configured which allow for bootstrapping the ACL
   198  system, or accessing Consul in special situations:
   199  
   200  | Special Token | Servers | Clients | Purpose |
   201  | ------------- | ------- | ------- | ------- |
   202  | [`acl_agent_master_token`](/docs/agent/options.html#acl_agent_master_token_legacy) | `OPTIONAL` | `OPTIONAL` | Special token that can be used to access [Agent API](/api/agent.html) when the ACL datacenter isn't available, or servers are offline (for clients); used for setting up the cluster such as doing initial join operations, see the [ACL Agent Master Token](#acl-agent-master-token) section for more details |
   203  | [`acl_agent_token`](/docs/agent/options.html#acl_agent_token_legacy) | `OPTIONAL` | `OPTIONAL` | Special token that is used for an agent's internal operations, see the [ACL Agent Token](#acl-agent-token) section for more details |
   204  | [`acl_master_token`](/docs/agent/options.html#acl_master_token_legacy) | `REQUIRED` | `N/A` | Special token used to bootstrap the ACL system, see the [Bootstrapping ACLs](#bootstrapping-acls) section for more details |
   205  | [`acl_token`](/docs/agent/options.html#acl_token_legacy) | `OPTIONAL` | `OPTIONAL` | Default token to use for client requests where no token is supplied; this is often configured with read-only access to services to enable DNS service discovery on agents |
   206  
   207  In Consul 0.9.1 and later, the agent ACL tokens can be introduced or updated via the
   208  [/v1/agent/token API](/api/agent.html#update-acl-tokens).
   209  
   210  #### ACL Agent Master Token
   211  
   212  Since the [`acl_agent_master_token`](/docs/agent/options.html#acl_agent_master_token_legacy) is designed to be used when the Consul servers are not available, its policy is managed locally on the agent and does not need to have a token defined on the Consul servers via the ACL API. Once set, it implicitly has the following policy associated with it (the `node` policy was added in Consul 0.9.0):
   213  
   214  ```text
   215  agent "<node name of agent>" {
   216    policy = "write"
   217  }
   218  node "" {
   219    policy = "read"
   220  }
   221  ```
   222  
   223  In Consul 0.9.1 and later, the agent ACL tokens can be introduced or updated via the
   224  [/v1/agent/token API](/api/agent.html#update-acl-tokens).
   225  
   226  #### ACL Agent Token
   227  
   228  The [`acl_agent_token`](/docs/agent/options.html#acl_agent_token) is a special token that is used for an agent's internal operations. It isn't used directly for any user-initiated operations like the [`acl_token`](/docs/agent/options.html#acl_token), though if the `acl_agent_token` isn't configured the `acl_token` will be used. The ACL agent token is used for the following operations by the agent:
   229  
   230  1. Updating the agent's node entry using the [Catalog API](/api/catalog.html), including updating its node metadata, tagged addresses, and network coordinates
   231  2. Performing [anti-entropy](/docs/internals/anti-entropy.html) syncing, in particular reading the node metadata and services registered with the catalog
   232  3. Reading and writing the special `_rexec` section of the KV store when executing [`consul exec`](/docs/commands/exec.html) commands
   233  
   234  Here's an example policy sufficient to accomplish the above for a node called `mynode`:
   235  
   236  ```text
   237  node "mynode" {
   238    policy = "write"
   239  }
   240  service "" {
   241    policy = "read"
   242  }
   243  key "_rexec" {
   244    policy = "write"
   245  }
   246  ```
   247  
   248  The `service` policy needs `read` access for any services that can be registered on the agent. If [remote exec is disabled](/docs/agent/options.html#disable_remote_exec), the default, then the `key` policy can be omitted.
   249  
   250  In Consul 0.9.1 and later, the agent ACL tokens can be introduced or updated via the
   251  [/v1/agent/token API](/api/agent.html#update-acl-tokens).
   252  
   253  ## Bootstrapping ACLs
   254  
   255  Bootstrapping ACLs on a new cluster requires a few steps, outlined in the examples in this
   256  section.
   257  
   258  #### Enable ACLs on the Consul Servers
   259  
   260  The first step for bootstrapping ACLs is to enable ACLs on the Consul servers in the ACL
   261  datacenter. In this example, we are configuring the following:
   262  
   263  1. An ACL datacenter of "dc1", which is where these servers are
   264  2. An ACL master token of "b1gs33cr3t"; see below for an alternative using the [/v1/acl/bootstrap API](/api/acl/acl.html#bootstrap-acls)
   265  3. A default policy of "deny" which means we are in whitelist mode
   266  4. A down policy of "extend-cache" which means that we will ignore token TTLs during an
   267     outage
   268  
   269  Here's the corresponding JSON configuration file:
   270  
   271  ```json
   272  {
   273    "primary_datacenter": "dc1",
   274    "acl_master_token": "b1gs33cr3t",
   275    "acl_default_policy": "deny",
   276    "acl_down_policy": "extend-cache"
   277  }
   278  ```
   279  
   280  The servers will need to be restarted to load the new configuration. Please take care
   281  to start the servers one at a time, and ensure each server has joined and is operating
   282  correctly before starting another.
   283  
   284  The [`acl_master_token`](/docs/agent/options.html#acl_master_token) will be created
   285  as a "management" type token automatically. The
   286  [`acl_master_token`](/docs/agent/options.html#acl_master_token) is only installed when
   287  a server acquires cluster leadership. If you would like to install or change the
   288  [`acl_master_token`](/docs/agent/options.html#acl_master_token), set the new value for
   289  [`acl_master_token`](/docs/agent/options.html#acl_master_token) in the configuration
   290  for all servers. Once this is done, restart the current leader to force a leader election.
   291  
   292  In Consul 0.9.1 and later, you can use the [/v1/acl/bootstrap API](/api/acl/acl.html#bootstrap-acls)
   293  to make the initial master token, so a token never needs to be placed into a configuration
   294  file. To use this approach, omit `acl_master_token` from the above config and then call the API:
   295  
   296  ```text
   297  $ curl \
   298      --request PUT \
   299      http://127.0.0.1:8500/v1/acl/bootstrap
   300  
   301  {"ID":"fe3b8d40-0ee0-8783-6cc2-ab1aa9bb16c1"}
   302  ```
   303  
   304  The returned token is the initial management token, which is randomly generated by Consul.
   305  It's only possible to bootstrap one time, and bootstrapping will be disabled if a master
   306  token was configured and created.
   307  
   308  Once the ACL system is bootstrapped, ACL tokens can be managed through the
   309  [ACL API](/api/acl/acl.html).
   310  
   311  #### Create an Agent Token
   312  
   313  After the servers are restarted above, you will see new errors in the logs of the Consul
   314  servers related to permission denied errors:
   315  
   316  ```
   317  2017/07/08 23:38:24 [WARN] agent: Node info update blocked by ACLs
   318  2017/07/08 23:38:44 [WARN] agent: Coordinate update blocked by ACLs
   319  ```
   320  
   321  These errors are because the agent doesn't yet have a properly configured
   322  [`acl_agent_token`](/docs/agent/options.html#acl_agent_token) that it can use for its
   323  own internal operations like updating its node information in the catalog and performing
   324  [anti-entropy](/docs/internals/anti-entropy.html) syncing. We can create a token using the
   325  ACL API, and the ACL master token we set in the previous step:
   326  
   327  ```text
   328  $ curl \
   329      --request PUT \
   330      --header "X-Consul-Token: b1gs33cr3t" \
   331      --data \
   332  '{
   333    "Name": "Agent Token",
   334    "Type": "client",
   335    "Rules": "node \"\" { policy = \"write\" } service \"\" { policy = \"read\" }"
   336  }' http://127.0.0.1:8500/v1/acl/create
   337  
   338  {"ID":"fe3b8d40-0ee0-8783-6cc2-ab1aa9bb16c1"}
   339  ```
   340  
   341  The returned value is the newly-created token. We can now add this to our Consul server
   342  configuration and restart the servers once more to apply it:
   343  
   344  ```json
   345  {
   346    "primary_datacenter": "dc1",
   347    "acl_master_token": "b1gs33cr3t",
   348    "acl_default_policy": "deny",
   349    "acl_down_policy": "extend-cache",
   350    "acl_agent_token": "fe3b8d40-0ee0-8783-6cc2-ab1aa9bb16c1"
   351  }
   352  ```
   353  
   354  In Consul 0.9.1 and later you can also introduce the agent token using an API,
   355  so it doesn't need to be set in the configuration file:
   356  
   357  ```text
   358  $ curl \
   359      --request PUT \
   360      --header "X-Consul-Token: b1gs33cr3t" \
   361      --data \
   362  '{
   363    "Token": "fe3b8d40-0ee0-8783-6cc2-ab1aa9bb16c1"
   364  }' http://127.0.0.1:8500/v1/agent/token/acl_agent_token
   365  ```
   366  
   367  With that ACL agent token set, the servers will be able to sync themselves with the
   368  catalog:
   369  
   370  ```
   371  2017/07/08 23:42:59 [INFO] agent: Synced node info
   372  ```
   373  
   374  See the [ACL Agent Token](#acl-agent-token) section for more details.
   375  
   376  #### Enable ACLs on the Consul Clients
   377  
   378  Since ACL enforcement also occurs on the Consul clients, we need to also restart them
   379  with a configuration file that enables ACLs:
   380  
   381  ```json
   382  {
   383    "primary_datacenter": "dc1",
   384    "acl_down_policy": "extend-cache",
   385    "acl_agent_token": "fe3b8d40-0ee0-8783-6cc2-ab1aa9bb16c1"
   386  }
   387  ```
   388  
   389  Similar to the previous example, in Consul 0.9.1 and later you can also introduce the
   390  agent token using an API, so it doesn't need to be set in the configuration file:
   391  
   392  ```text
   393  $ curl \
   394      --request PUT \
   395      --header "X-Consul-Token: b1gs33cr3t" \
   396      --data \
   397  '{
   398    "Token": "fe3b8d40-0ee0-8783-6cc2-ab1aa9bb16c1"
   399  }' http://127.0.0.1:8500/v1/agent/token/acl_agent_token
   400  ```
   401  
   402  We used the same ACL agent token that we created for the servers, which will work since
   403  it was not specific to any node or set of service prefixes. In a more locked-down
   404  environment it is recommended that each client get an ACL agent token with `node` write
   405  privileges for just its own node name prefix, and `service` read privileges for just the
   406  service prefixes expected to be registered on that client.
   407  
   408  [Anti-entropy](/docs/internals/anti-entropy.html) syncing requires the ACL agent token
   409  to have `service` read privileges for all services that may be registered with the agent,
   410  so generally an empty `service` prefix can be used, as shown in the example.
   411  
   412  Clients will report similar permission denied errors until they are restarted with an ACL
   413  agent token.
   414  
   415  #### Set an Anonymous Policy (Optional)
   416  
   417  At this point ACLs are bootstrapped with ACL agent tokens configured, but there are no
   418  other policies set up. Even basic operations like `consul members` will be restricted
   419  by the ACL default policy of "deny":
   420  
   421  ```
   422  $ consul members
   423  ```
   424  
   425  We don't get an error since the ACL has filtered what we see, and we aren't allowed to
   426  see any nodes by default.
   427  
   428  If we supply the token we created above we will be able to see a listing of nodes because
   429  it has write privileges to an empty `node` prefix, meaning it has access to all nodes:
   430  
   431  ```
   432  $ CONSUL_HTTP_TOKEN=fe3b8d40-0ee0-8783-6cc2-ab1aa9bb16c1 consul members
   433  Node    Address         Status  Type    Build     Protocol  DC
   434  node-1  127.0.0.1:8301  alive   server  0.9.0dev  2         dc1
   435  node-2  127.0.0.2:8301  alive   client  0.9.0dev  2         dc1
   436  ```
   437  
   438  It's pretty common in many environments to allow listing of all nodes, even without a
   439  token. The policies associated with the special anonymous token can be updated to
   440  configure Consul's behavior when no token is supplied. The anonymous token is managed
   441  like any other ACL token, except that `anonymous` is used for the ID. In this example
   442  we will give the anonymous token read privileges for all nodes:
   443  
   444  ```text
   445  $ curl \
   446      --request PUT \
   447      --header "X-Consul-Token: b1gs33cr3t" \
   448      --data \
   449  '{
   450    "ID": "anonymous",
   451    "Type": "client",
   452    "Rules": "node \"\" { policy = \"read\" }"
   453  }' http://127.0.0.1:8500/v1/acl/update
   454  
   455  {"ID":"anonymous"}
   456  ```
   457  
   458  The anonymous token is implicitly used if no token is supplied, so now we can run
   459  `consul members` without supplying a token and we will be able to see the nodes:
   460  
   461  ```
   462  $ consul members
   463  Node    Address         Status  Type    Build     Protocol  DC
   464  node-1  127.0.0.1:8301  alive   server  0.9.0dev  2         dc1
   465  node-2  127.0.0.2:8301  alive   client  0.9.0dev  2         dc1
   466  ```
   467  
   468  The anonymous token is also used for DNS lookups since there's no way to pass a
   469  token as part of a DNS request. Here's an example lookup for the "consul" service:
   470  
   471  ```
   472  $ dig @127.0.0.1 -p 8600 consul.service.consul
   473  
   474  ; <<>> DiG 9.8.3-P1 <<>> @127.0.0.1 -p 8600 consul.service.consul
   475  ; (1 server found)
   476  ;; global options: +cmd
   477  ;; Got answer:
   478  ;; ->>HEADER<<- opcode: QUERY, status: NXDOMAIN, id: 9648
   479  ;; flags: qr aa rd; QUERY: 1, ANSWER: 0, AUTHORITY: 1, ADDITIONAL: 0
   480  ;; WARNING: recursion requested but not available
   481  
   482  ;; QUESTION SECTION:
   483  ;consul.service.consul.         IN      A
   484  
   485  ;; AUTHORITY SECTION:
   486  consul.                 0       IN      SOA     ns.consul. postmaster.consul. 1499584110 3600 600 86400 0
   487  
   488  ;; Query time: 2 msec
   489  ;; SERVER: 127.0.0.1#8600(127.0.0.1)
   490  ;; WHEN: Sun Jul  9 00:08:30 2017
   491  ;; MSG SIZE  rcvd: 89
   492  ```
   493  
   494  Now we get an `NXDOMAIN` error because the anonymous token doesn't have access to the
   495  "consul" service. Let's add that to the anonymous token's policy:
   496  
   497  ```text
   498  $ curl \
   499      --request PUT \
   500      --header "X-Consul-Token: b1gs33cr3t" \
   501      --data \
   502  '{
   503    "ID": "anonymous",
   504    "Type": "client",
   505    "Rules": "node \"\" { policy = \"read\" } service \"consul\" { policy = \"read\" }"
   506  }' http://127.0.0.1:8500/v1/acl/update
   507  
   508  {"ID":"anonymous"}
   509  ```
   510  
   511  With that new policy in place, the DNS lookup will succeed:
   512  
   513  ```
   514  $ dig @127.0.0.1 -p 8600 consul.service.consul
   515  
   516  ; <<>> DiG 9.8.3-P1 <<>> @127.0.0.1 -p 8600 consul.service.consul
   517  ; (1 server found)
   518  ;; global options: +cmd
   519  ;; Got answer:
   520  ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 46006
   521  ;; flags: qr aa rd; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 0
   522  ;; WARNING: recursion requested but not available
   523  
   524  ;; QUESTION SECTION:
   525  ;consul.service.consul.         IN      A
   526  
   527  ;; ANSWER SECTION:
   528  consul.service.consul.  0       IN      A       127.0.0.1
   529  
   530  ;; Query time: 0 msec
   531  ;; SERVER: 127.0.0.1#8600(127.0.0.1)
   532  ;; WHEN: Sun Jul  9 00:11:14 2017
   533  ;; MSG SIZE  rcvd: 55
   534  ```
   535  
   536  The next section shows an alternative to the anonymous token.
   537  
   538  #### Set Agent-Specific Default Tokens (Optional)
   539  
   540  An alternative to the anonymous token is the [`acl_token`](/docs/agent/options.html#acl_token)
   541  configuration item. When a request is made to a particular Consul agent and no token is
   542  supplied, the [`acl_token`](/docs/agent/options.html#acl_token) will be used for the token,
   543  instead of being left empty which would normally invoke the anonymous token.
   544  
   545  In Consul 0.9.1 and later, the agent ACL tokens can be introduced or updated via the
   546  [/v1/agent/token API](/api/agent.html#update-acl-tokens).
   547  
   548  This behaves very similarly to the anonymous token, but can be configured differently on each
   549  agent, if desired. For example, this allows more fine grained control of what DNS requests a
   550  given agent can service, or can give the agent read access to some key-value store prefixes by
   551  default.
   552  
   553  If using [`acl_token`](/docs/agent/options.html#acl_token), then it's likely the anonymous
   554  token will have a more restrictive policy than shown in the examples here.
   555  
   556  #### Create Tokens for UI Use (Optional)
   557  
   558  If you utilize the Consul UI with a restrictive ACL policy, as above, the UI will
   559  not function fully using the anonymous ACL token. It is recommended
   560  that a UI-specific ACL token is used, which can be set in the UI during the
   561  web browser session to authenticate the interface.
   562  
   563  ```text
   564  $ curl \
   565      --request PUT \
   566      --header "X-Consul-Token: b1gs33cr3t" \
   567      --data \
   568  '{
   569    "Name": "UI Token",
   570    "Type": "client",
   571    "Rules": "key \"\" { policy = \"write\" } node \"\" { policy = \"read\" } service \"\" { policy = \"read\" }"
   572  }' http://127.0.0.1:8500/v1/acl/create
   573  {"ID":"d0a9f330-2f9d-0a8c-d2af-1e9ceda354e6"}
   574  ```
   575  
   576  The token can then be set on the "settings" page of the UI.
   577  
   578  #### Next Steps
   579  
   580  The examples above configure a basic ACL environment with the ability to see all nodes
   581  by default, and limited access to just the "consul" service. The [ACL API](/api/acl/acl.html)
   582  can be used to create tokens for applications specific to their intended use, and to create
   583  more specific ACL agent tokens for each agent's expected role.
   584  
   585  Also see [HashiCorp's Vault](https://www.vaultproject.io/docs/secrets/consul/index.html), which
   586  has an integration with Consul that allows it to generate ACL tokens on the fly and to manage
   587  their lifetimes.
   588  
   589  ## Rule Specification
   590  
   591  A core part of the ACL system is the rule language which is used to describe the policy
   592  that must be enforced. Most of the ACL rules are prefix-based, allowing operators to
   593  define different namespaces within Consul's resource areas like the catalog and key/value
   594  store, in order to delegate responsibility for these namespaces. Policies can have several
   595  dispositions:
   596  
   597  * `read`: allow the resource to be read but not modified
   598  * `write`: allow the resource to be read and modified
   599  * `deny`: do not allow the resource to be read or modified
   600  
   601  With prefix-based rules, the most specific prefix match determines the action. This
   602  allows for flexible rules like an empty prefix to allow read-only access to all
   603  resources, along with some specific prefixes that allow write access or that are
   604  denied all access.
   605  
   606  We make use of the
   607  [HashiCorp Configuration Language (HCL)](https://github.com/hashicorp/hcl/) to specify
   608  rules. This language is human readable and interoperable with JSON making it easy to
   609  machine-generate. Rules can make use of one or more policies.
   610  
   611  Specification in the HCL format looks like:
   612  
   613  ```text
   614  # These control access to the key/value store.
   615  key "" {
   616    policy = "read"
   617  }
   618  key "foo/" {
   619    policy = "write"
   620  }
   621  key "foo/private/" {
   622    policy = "deny"
   623  }
   624  
   625  # This controls access to cluster-wide Consul operator information.
   626  operator = "read"
   627  ```
   628  
   629  This is equivalent to the following JSON input:
   630  
   631  ```javascript
   632  {
   633    "key": {
   634      "": {
   635        "policy": "read"
   636      },
   637      "foo/": {
   638        "policy": "write"
   639      },
   640      "foo/private/": {
   641        "policy": "deny"
   642      }
   643    },
   644    "operator": "read"
   645  }
   646  ```
   647  
   648  The [ACL API](/api/acl/acl.html) allows either HCL or JSON to be used to define the content
   649  of the rules section.
   650  
   651  Here's a sample request using the HCL form:
   652  
   653  ```text
   654  $ curl \
   655      --request PUT \
   656      --data \
   657  '{
   658    "Name": "my-app-token",
   659    "Type": "client",
   660    "Rules": "key \"\" { policy = \"read\" } key \"foo/\" { policy = \"write\" } key \"foo/private/\" { policy = \"deny\" } operator = \"read\""
   661  }' http://127.0.0.1:8500/v1/acl/create?token=<management token>
   662  ```
   663  
   664  Here's an equivalent request using the JSON form:
   665  
   666  ```text
   667  $ curl \
   668      --request PUT \
   669      --data \
   670  '{
   671    "Name": "my-app-token",
   672    "Type": "client",
   673    "Rules": "{\"key\":{\"\":{\"policy\":\"read\"},\"foo/\":{\"policy\":\"write\"},\"foo/private\":{\"policy\":\"deny\"}},\"operator\":\"read\"}"
   674  }' http://127.0.0.1:8500/v1/acl/create?token=<management token>
   675  ```
   676  
   677  On success, the token ID is returned:
   678  
   679  ```json
   680  {
   681    "ID": "adf4238a-882b-9ddc-4a9d-5b6758e4159e"
   682  }
   683  ```
   684  
   685  This token ID can then be passed into Consul's HTTP APIs via the `token`
   686  query string parameter, or the `X-Consul-Token` request header, or Authorization
   687  Bearer token header, or Consul's CLI commands via the `token` argument,
   688  or the `CONSUL_HTTP_TOKEN` environment variable.
   689  
   690  #### Agent Rules
   691  
   692  The `agent` policy controls access to the utility operations in the [Agent API](/api/agent.html),
   693  such as join and leave. All of the catalog-related operations are covered by the [`node`](#node-rules)
   694  and [`service`](#service-rules) policies instead.
   695  
   696  Agent rules look like this:
   697  
   698  ```text
   699  agent "" {
   700    policy = "read"
   701  }
   702  agent "foo" {
   703    policy = "write"
   704  }
   705  agent "bar" {
   706    policy = "deny"
   707  }
   708  ```
   709  
   710  Agent rules are keyed by the node name prefix they apply to, using the longest prefix match rule. In
   711  the example above, the rules allow read-only access to any node name with the empty prefix, allow
   712  read-write access to any node name that starts with "foo", and deny all access to any node name that
   713  starts with "bar".
   714  
   715  Since [Agent API](/api/agent.html) utility operations may be required before an agent is joined to
   716  a cluster, or during an outage of the Consul servers or ACL datacenter, a special token may be
   717  configured with [`acl_agent_master_token`](/docs/agent/options.html#acl_agent_master_token) to allow
   718  write access to these operations even if no ACL resolution capability is available.
   719  
   720  #### Event Rules
   721  
   722  The `event` policy controls access to event operations in the [Event API](/api/event.html), such as
   723  firing events and listing events.
   724  
   725  Event rules look like this:
   726  
   727  ```text
   728  event "" {
   729    policy = "read"
   730  }
   731  event "deploy" {
   732    policy = "write"
   733  }
   734  ```
   735  
   736  Event rules are keyed by the event name prefix they apply to, using the longest prefix match rule.
   737  In the example above, the rules allow read-only access to any event, and firing of any event that
   738  starts with "deploy".
   739  
   740  The [`consul exec`](/docs/commands/exec.html) command uses events with the "_rexec" prefix during
   741  operation, so to enable this feature in a Consul environment with ACLs enabled, you will need to
   742  give agents a token with access to this event prefix, in addition to configuring
   743  [`disable_remote_exec`](/docs/agent/options.html#disable_remote_exec) to `false`.
   744  
   745  #### Key/Value Rules
   746  
   747  The `key` policy controls access to key/value store operations in the [KV API](/api/kv.html). Key
   748  rules look like this:
   749  
   750  ```text
   751  key "" {
   752    policy = "read"
   753  }
   754  key "foo" {
   755    policy = "write"
   756  }
   757  key "bar" {
   758    policy = "deny"
   759  }
   760  ```
   761  
   762  Key rules are keyed by the key name prefix they apply to, using the longest prefix match rule. In
   763  the example above, the rules allow read-only access to any key name with the empty prefix, allow
   764  read-write access to any key name that starts with "foo", and deny all access to any key name that
   765  starts with "bar".
   766  
   767  #### List Policy for Keys
   768  
   769  Consul 1.0 introduces a new `list` policy for keys that is only enforced when opted in via the boolean config param "acl_enable_key_list_policy".
   770  `list` controls access to recursively list entries and keys, and enables more fine grained policies. With "acl_enable_key_list_policy",
   771  recursive reads via [the KV API](/api/kv.html#recurse) with an invalid token result in a 403. Example:
   772  
   773  ```text
   774  key "" {
   775   policy = "deny"
   776  }
   777  
   778  key "bar" {
   779   policy = "list"
   780  }
   781  
   782  key "baz" {
   783   policy = "read"
   784  }
   785  ```
   786  
   787  In the example above, the rules allow reading the key "baz", and only allow recursive reads on the prefix "bar".
   788  
   789  A token with `write` access on a prefix also has `list` access. A token with `list` access on a prefix also has `read` access on all its suffixes.
   790  
   791  #### Sentinel Integration
   792  
   793  Consul Enterprise supports additional optional fields for key write policies for
   794  [Sentinel](https://docs.hashicorp.com/sentinel/app/consul/) integration. An example key rule with a
   795  Sentinel code policy looks like this:
   796  
   797  ```text
   798  key "foo" {
   799    policy = "write"
   800    sentinel {
   801        code = <<EOF
   802  import "strings"
   803  main = rule { strings.has_suffix(value, "bar") }
   804  EOF
   805        enforcementlevel = "hard-mandatory"
   806    }
   807  }
   808  ```
   809  
   810  For more detailed documentation, see the [Consul Sentinel Guide](/docs/guides/sentinel.html).
   811  
   812  #### Keyring Rules
   813  
   814  The `keyring` policy controls access to keyring operations in the
   815  [Keyring API](/api/operator/keyring.html).
   816  
   817  Keyring rules look like this:
   818  
   819  ```text
   820  keyring = "write"
   821  ```
   822  
   823  There's only one keyring policy allowed per rule set, and its value is set to one of the policy
   824  dispositions. In the example above, the keyring may be read and updated.
   825  
   826  #### Node Rules
   827  
   828  The `node` policy controls node-level registration and read access to the [Catalog API](/api/catalog.html),
   829  service discovery with the [Health API](/api/health.html), and filters results in [Agent API](/api/agent.html)
   830  operations like fetching the list of cluster members.
   831  
   832  Node rules look like this:
   833  
   834  ```text
   835  node "" {
   836    policy = "read"
   837  }
   838  node "app" {
   839    policy = "write"
   840  }
   841  node "admin" {
   842    policy = "deny"
   843  }
   844  ```
   845  
   846  Node rules are keyed by the node name prefix they apply to, using the longest prefix match rule. In
   847  the example above, the rules allow read-only access to any node name with the empty prefix, allow
   848  read-write access to any node name that starts with "app", and deny all access to any node name that
   849  starts with "admin".
   850  
   851  Agents need to be configured with an [`acl_agent_token`](/docs/agent/options.html#acl_agent_token)
   852  with at least "write" privileges to their own node name in order to register their information with
   853  the catalog, such as node metadata and tagged addresses. If this is configured incorrectly, the agent
   854  will print an error to the console when it tries to sync its state with the catalog.
   855  
   856  Consul's DNS interface is also affected by restrictions on node rules. If the
   857  [`acl_token`](/docs/agent/options.html#acl_token) used by the agent does not have "read" access to a
   858  given node, then the DNS interface will return no records when queried for it.
   859  
   860  When reading from the catalog or retrieving information from the health endpoints, node rules are
   861  used to filter the results of the query. This allows for configurations where a token has access
   862  to a given service name, but only on an allowed subset of node names.
   863  
   864  Node rules come into play when using the [Agent API](/api/agent.html) to register node-level
   865  checks. The agent will check tokens locally as a check is registered, and Consul also performs
   866  periodic [anti-entropy](/docs/internals/anti-entropy.html) syncs, which may require an
   867  ACL token to complete. To accommodate this, Consul provides two methods of configuring ACL tokens
   868  to use for registration events:
   869  
   870  1. Using the [acl_token](/docs/agent/options.html#acl_token) configuration
   871     directive. This allows a single token to be configured globally and used
   872     during all check registration operations.
   873  2. Providing an ACL token with service and check definitions at
   874     registration time. This allows for greater flexibility and enables the use
   875     of multiple tokens on the same agent. Examples of what this looks like are
   876     available for both [services](/docs/agent/services.html) and
   877     [checks](/docs/agent/checks.html). Tokens may also be passed to the
   878     [HTTP API](/api/index.html) for operations that require them.
   879  
   880  In addition to ACLs, in Consul 0.9.0 and later, the agent must be configured with
   881  [`enable_script_checks`](/docs/agent/options.html#_enable_script_checks) set to `true` in order to enable
   882  script checks.
   883  
   884  #### Operator Rules
   885  
   886  The `operator` policy controls access to cluster-level operations in the
   887  [Operator API](/api/operator.html), other than the [Keyring API](/api/operator/keyring.html).
   888  
   889  Operator rules look like this:
   890  
   891  ```text
   892  operator = "read"
   893  ```
   894  
   895  There's only one operator policy allowed per rule set, and its value is set to one of the policy
   896  dispositions. In the example above, the token could be used to query the operator endpoints for
   897  diagnostic purposes but not make any changes.
   898  
   899  #### Prepared Query Rules
   900  
   901  The `query` policy controls access to create, update, and delete prepared queries in the
   902  [Prepared Query API](/api/query.html). Executing queries is subject to `node` and `service`
   903  policies, as will be explained below.
   904  
   905  Query rules look like this:
   906  
   907  ```text
   908  query "" {
   909    policy = "read"
   910  }
   911  query "foo" {
   912    policy = "write"
   913  }
   914  ```
   915  
   916  Query rules are keyed by the query name prefix they apply to, using the longest prefix match rule. In
   917  the example above, the rules allow read-only access to any query name with the empty prefix, and allow
   918  read-write access to any query name that starts with "foo". This allows control of the query namespace
   919  to be delegated based on ACLs.
   920  
   921  There are a few variations when using ACLs with prepared queries, each of which uses ACLs in one of two
   922  ways: open, protected by unguessable IDs or closed, managed by ACL policies. These variations are covered
   923  here, with examples:
   924  
   925  * Static queries with no `Name` defined are not controlled by any ACL policies.
   926    These types of queries are meant to be ephemeral and not shared to untrusted
   927    clients, and they are only reachable if the prepared query ID is known. Since
   928    these IDs are generated using the same random ID scheme as ACL Tokens, it is
   929    infeasible to guess them. When listing all prepared queries, only a management
   930    token will be able to see these types, though clients can read instances for
   931    which they have an ID. An example use for this type is a query built by a
   932    startup script, tied to a session, and written to a configuration file for a
   933    process to use via DNS.
   934  
   935  * Static queries with a `Name` defined are controlled by the `query` ACL policy.
   936    Clients are required to have an ACL token with a prefix sufficient to cover
   937    the name they are trying to manage, with a longest prefix match providing a
   938    way to define more specific policies. Clients can list or read queries for
   939    which they have "read" access based on their prefix, and similar they can
   940    update any queries for which they have "write" access. An example use for
   941    this type is a query with a well-known name (eg. `prod-master-customer-db`)
   942    that is used and known by many clients to provide geo-failover behavior for
   943    a database.
   944  
   945  * [Template queries](/api/query.html#templates)
   946    queries work like static queries with a `Name` defined, except that a catch-all
   947    template with an empty `Name` requires an ACL token that can write to any query
   948    prefix.
   949  
   950  When prepared queries are executed via DNS lookups or HTTP requests, the ACL
   951  checks are run against the service being queried, similar to how ACLs work with
   952  other service lookups. There are several ways the ACL token is selected for this
   953  check:
   954  
   955  * If an ACL Token was captured when the prepared query was defined, it will be
   956    used to perform the service lookup. This allows queries to be executed by
   957    clients with lesser or even no ACL Token, so this should be used with care.
   958  
   959  * If no ACL Token was captured, then the client's ACL Token will be used to
   960    perform the service lookup.
   961  
   962  * If no ACL Token was captured and the client has no ACL Token, then the
   963    anonymous token will be used to perform the service lookup.
   964  
   965  In the common case, the ACL Token of the invoker is used
   966  to test the ability to look up a service. If a `Token` was specified when the
   967  prepared query was created, the behavior changes and now the captured
   968  ACL Token set by the definer of the query is used when looking up a service.
   969  
   970  Capturing ACL Tokens is analogous to
   971  [PostgreSQL’s](http://www.postgresql.org/docs/current/static/sql-createfunction.html)
   972  `SECURITY DEFINER` attribute which can be set on functions, and using the client's ACL
   973  Token is similar to the complementary `SECURITY INVOKER` attribute.
   974  
   975  Prepared queries were originally introduced in Consul 0.6.0, and ACL behavior remained
   976  unchanged through version 0.6.3, but was then changed to allow better management of the
   977  prepared query namespace.
   978  
   979  These differences are outlined in the table below:
   980  
   981  <table class="table table-bordered table-striped">
   982    <tr>
   983      <th>Operation</th>
   984      <th>Version <= 0.6.3 </th>
   985      <th>Version > 0.6.3 </th>
   986    </tr>
   987    <tr>
   988      <td>Create static query without `Name`</td>
   989      <td>The ACL Token used to create the prepared query is checked to make sure it can access the service being queried. This token is captured as the `Token` to use when executing the prepared query.</td>
   990      <td>No ACL policies are used as long as no `Name` is defined. No `Token` is captured by default unless specifically supplied by the client when creating the query.</td>
   991    </tr>
   992    <tr>
   993      <td>Create static query with `Name`</td>
   994      <td>The ACL Token used to create the prepared query is checked to make sure it can access the service being queried. This token is captured as the `Token` to use when executing the prepared query.</td>
   995      <td>The client token's `query` ACL policy is used to determine if the client is allowed to register a query for the given `Name`. No `Token` is captured by default unless specifically supplied by the client when creating the query.</td>
   996    </tr>
   997    <tr>
   998      <td>Manage static query without `Name`</td>
   999      <td>The ACL Token used to create the query, or a management token must be supplied in order to perform these operations.</td>
  1000      <td>Any client with the ID of the query can perform these operations.</td>
  1001    </tr>
  1002    <tr>
  1003      <td>Manage static query with a `Name`</td>
  1004      <td>The ACL token used to create the query, or a management token must be supplied in order to perform these operations.</td>
  1005      <td>Similar to create, the client token's `query` ACL policy is used to determine if these operations are allowed.</td>
  1006    </tr>
  1007    <tr>
  1008      <td>List queries</td>
  1009      <td>A management token is required to list any queries.</td>
  1010      <td>The client token's `query` ACL policy is used to determine which queries they can see. Only management tokens can see prepared queries without `Name`.</td>
  1011    </tr>
  1012    <tr>
  1013      <td>Execute query</td>
  1014      <td>Since a `Token` is always captured when a query is created, that is used to check access to the service being queried. Any token supplied by the client is ignored.</td>
  1015      <td>The captured token, client's token, or anonymous token is used to filter the results, as described above.</td>
  1016    </tr>
  1017  </table>
  1018  
  1019  #### Service Rules
  1020  
  1021  The `service` policy controls service-level registration and read access to the [Catalog API](/api/catalog.html)
  1022  and service discovery with the [Health API](/api/health.html).
  1023  
  1024  Service rules look like this:
  1025  
  1026  ```text
  1027  service "" {
  1028    policy = "read"
  1029  }
  1030  service "app" {
  1031    policy = "write"
  1032  }
  1033  service "admin" {
  1034    policy = "deny"
  1035  }
  1036  ```
  1037  
  1038  Service rules are keyed by the service name prefix they apply to, using the longest prefix match rule. In
  1039  the example above, the rules allow read-only access to any service name with the empty prefix, allow
  1040  read-write access to any service name that starts with "app", and deny all access to any service name that
  1041  starts with "admin".
  1042  
  1043  Consul's DNS interface is affected by restrictions on service rules. If the
  1044  [`acl_token`](/docs/agent/options.html#acl_token) used by the agent does not have "read" access to a
  1045  given service, then the DNS interface will return no records when queried for it.
  1046  
  1047  When reading from the catalog or retrieving information from the health endpoints, service rules are
  1048  used to filter the results of the query.
  1049  
  1050  Service rules come into play when using the [Agent API](/api/agent.html) to register services or
  1051  checks. The agent will check tokens locally as a service or check is registered, and Consul also
  1052  performs periodic [anti-entropy](/docs/internals/anti-entropy.html) syncs, which may require an
  1053  ACL token to complete. To accommodate this, Consul provides two methods of configuring ACL tokens
  1054  to use for registration events:
  1055  
  1056  1. Using the [acl_token](/docs/agent/options.html#acl_token) configuration
  1057     directive. This allows a single token to be configured globally and used
  1058     during all service and check registration operations.
  1059  2. Providing an ACL token with service and check definitions at registration
  1060     time. This allows for greater flexibility and enables the use of multiple
  1061     tokens on the same agent. Examples of what this looks like are available for
  1062     both [services](/docs/agent/services.html) and
  1063     [checks](/docs/agent/checks.html). Tokens may also be passed to the [HTTP
  1064     API](/api/index.html) for operations that require them. **Note:** all tokens
  1065     passed to an agent are persisted on local disk to allow recovery from
  1066     restarts. See [`-data-dir` flag
  1067     documentation](/docs/agent/options.html#acl_token) for notes on securing
  1068     access.
  1069  
  1070  In addition to ACLs, in Consul 0.9.0 and later, the agent must be configured with
  1071  [`enable_script_checks`](/docs/agent/options.html#_enable_script_checks) or
  1072  [`enable_local_script_checks`](/docs/agent/options.html#_enable_local_script_checks)
  1073  set to `true` in order to enable script checks.
  1074  
  1075  
  1076  #### Session Rules
  1077  
  1078  The `session` policy controls access to [Session API](/api/session.html) operations.
  1079  
  1080  Session rules look like this:
  1081  
  1082  ```text
  1083  session "" {
  1084    policy = "read"
  1085  }
  1086  session "app" {
  1087    policy = "write"
  1088  }
  1089  session "admin" {
  1090    policy = "deny"
  1091  }
  1092  ```
  1093  
  1094  Session rules are keyed by the node name prefix they apply to, using the longest prefix match rule. In
  1095  the example above, the rules allow read-only access to sessions on node name with the empty prefix, allow
  1096  creating sessions on any node name that starts with "app", and deny all access to any sessions on a node
  1097  name that starts with "admin".
  1098  
  1099  ## Advanced Topics
  1100  
  1101  <a name="replication"></a>
  1102  #### Outages and ACL Replication
  1103  
  1104  The Consul ACL system is designed with flexible rules to accommodate for an outage
  1105  of the [`primary_datacenter`](/docs/agent/options.html#primary_datacenter) or networking
  1106  issues preventing access to it. In this case, it may be impossible for
  1107  agents in non-authoritative datacenters to resolve tokens. Consul provides
  1108  a number of configurable [`acl_down_policy`](/docs/agent/options.html#acl_down_policy)
  1109  choices to tune behavior. It is possible to deny or permit all actions or to ignore
  1110  cache TTLs and enter a fail-safe mode. The default is to ignore cache TTLs
  1111  for any previously resolved tokens and to deny any uncached tokens.
  1112  
  1113  Consul 0.7 added an ACL Replication capability that can allow non-authoritative
  1114  datacenter agents to resolve even uncached tokens. This is enabled by setting an
  1115  [`acl_replication_token`](/docs/agent/options.html#acl_replication_token) in the
  1116  configuration on the servers in the non-authoritative datacenters. In Consul
  1117  0.9.1 and later you can enable ACL replication using
  1118  [`enable_acl_replication`](/docs/agent/options.html#enable_acl_replication) and
  1119  then set the token later using the
  1120  [agent token API](/api/agent.html#update-acl-tokens) on each server. This can
  1121  also be used to rotate the token without restarting the Consul servers.
  1122  
  1123  With replication enabled, the servers will maintain a replica of the authoritative
  1124  datacenter's full set of ACLs on the non-authoritative servers. The ACL replication
  1125  token needs to be a valid ACL token with management privileges, it can also be the
  1126  same as the master ACL token.
  1127  
  1128  Replication occurs with a background process that looks for new ACLs approximately
  1129  every 30 seconds. Replicated changes are written at a rate that's throttled to
  1130  100 updates/second, so it may take several minutes to perform the initial sync of
  1131  a large set of ACLs.
  1132  
  1133  If there's a partition or other outage affecting the authoritative datacenter,
  1134  and the [`acl_down_policy`](/docs/agent/options.html#acl_down_policy)
  1135  is set to "extend-cache", tokens will be resolved during the outage using the
  1136  replicated set of ACLs. An [ACL replication status](/api/acl/acl.html#acl_replication_status)
  1137  endpoint is available to monitor the health of the replication process.
  1138  Also note that in recent versions of Consul (greater than 1.2.0), using
  1139  `acl_down_policy = "async-cache"` refreshes token asynchronously when an ACL is
  1140  already cached and is expired while similar semantics than "extend-cache".
  1141  It allows to avoid having issues when connectivity with the authoritative is not completely
  1142  broken, but very slow.
  1143  
  1144  Locally-resolved ACLs will be cached using the [`acl_ttl`](/docs/agent/options.html#acl_ttl)
  1145  setting of the non-authoritative datacenter, so these entries may persist in the
  1146  cache for up to the TTL, even after the authoritative datacenter comes back online.
  1147  
  1148  ACL replication can also be used to migrate ACLs from one datacenter to another
  1149  using a process like this:
  1150  
  1151  1. Enable ACL replication in all datacenters to allow continuation of service
  1152  during the migration, and to populate the target datacenter. Verify replication
  1153  is healthy and caught up to the current ACL index in the target datacenter
  1154  using the [ACL replication status](/api/acl/acl.html#acl_replication_status)
  1155  endpoint.
  1156  2. Turn down the old authoritative datacenter servers.
  1157  3. Rolling restart the agents in the target datacenter and change the
  1158  `primary_datacenter` servers to itself. This will automatically turn off
  1159  replication and will enable the datacenter to start acting as the authoritative
  1160  datacenter, using its replicated ACLs from before.
  1161  3. Rolling restart the agents in other datacenters and change their `primary_datacenter`
  1162  configuration to the target datacenter.
  1163  
  1164  <a name="version_8_acls"></a>
  1165  #### Complete ACL Coverage in Consul 0.8
  1166  
  1167  Consul 0.8 added many more ACL policy types and brought ACL enforcement to Consul
  1168  agents for the first time. To ease the transition to Consul 0.8 for existing ACL
  1169  users, there's a configuration option to disable these new features. To disable
  1170  support for these new ACLs, set the
  1171  [`acl_enforce_version_8`](/docs/agent/options.html#acl_enforce_version_8) configuration
  1172  option to `false` on Consul clients and servers.
  1173  
  1174  Here's a summary of the new features:
  1175  
  1176  * Agents now check [`node`](#node-rules) and [`service`](#service-rules) ACL policies
  1177    for catalog-related operations in `/v1/agent` endpoints, such as service and check
  1178    registration and health check updates.
  1179  * Agents enforce a new [`agent`](#agent-rules) ACL policy for utility operations in
  1180    `/v1/agent` endpoints, such as joins and leaves.
  1181  * A new [`node`](#node-rules) ACL policy is enforced throughout Consul, providing a
  1182    mechanism to restrict registration and discovery of nodes by name. This also applies
  1183    to service discovery, so provides an additional dimension for controlling access to
  1184    services.
  1185  * A new [`session`](#session-rules) ACL policy controls the ability to create session
  1186    objects by node name.
  1187  * Anonymous prepared queries (non-templates without a `Name`) now require a valid
  1188    session, which ties their creation to the new [`session`](#session-rules) ACL policy.
  1189  * The existing [`event`](#event-rules) ACL policy has been applied to the
  1190    `/v1/event/list` endpoint.
  1191  
  1192  Two new configuration options are used once version 8 ACLs are enabled:
  1193  
  1194  * [`acl_agent_master_token`](/docs/agent/options.html#acl_agent_master_token) is used as
  1195    a special access token that has `agent` ACL policy `write` privileges on each agent where
  1196    it is configured, as well as `node` ACL policy `read` privileges for all nodes. This token
  1197    should only be used by operators during outages when Consul servers aren't available to
  1198    resolve ACL tokens. Applications should use regular ACL tokens during normal operation.
  1199  * [`acl_agent_token`](/docs/agent/options.html#acl_agent_token) is used internally by
  1200    Consul agents to perform operations to the service catalog when registering themselves
  1201    or sending network coordinates to the servers. This token must at least have `node` ACL
  1202    policy `write` access to the node name it will register as in order to register any
  1203    node-level information like metadata or tagged addresses.
  1204  
  1205  Since clients now resolve ACLs locally, the [`acl_down_policy`](/docs/agent/options.html#acl_down_policy)
  1206  now applies to Consul clients as well as Consul servers. This will determine what the
  1207  client will do in the event that the servers are down.
  1208  
  1209  Consul clients must have [`primary_datacenter`](/docs/agent/options.html#primary_datacenter) configured
  1210  in order to enable agent-level ACL features. If this is set, the agents will contact the Consul
  1211  servers to determine if ACLs are enabled at the cluster level. If they detect that ACLs are not
  1212  enabled, they will check at most every 2 minutes to see if they have become enabled, and will
  1213  start enforcing ACLs automatically. If an agent has an `primary_datacenter` defined, operators will
  1214  need to use the [`acl_agent_master_token`](/docs/agent/options.html#acl_agent_master_token) to
  1215  perform agent-level operations if the Consul servers aren't present (such as for a manual join
  1216  to the cluster), unless the [`acl_down_policy`](/docs/agent/options.html#acl_down_policy) on the
  1217  agent is set to "allow".
  1218  
  1219  Non-server agents do not need to have the
  1220  [`acl_master_token`](/docs/agent/options.html#acl_master_token) configured; it is not
  1221  used by agents in any way.