github.com/Aestek/consul@v1.2.4-0.20190309222502-b2c31e33971a/website/source/docs/guides/acl.html.md (about)

     1  ---
     2  layout: "docs"
     3  page_title: "Bootstrapping ACLs"
     4  sidebar_current: "docs-guides-acl"
     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  # Bootstrapping the ACL System
    10  
    11  Consul uses Access Control Lists (ACLs) to secure the UI, API, CLI, service communications, and agent communications. For securing gossip and RPC communication please review [this guide](/docs/guides/agent-encryption.html). When securing your cluster you should configure the ACLs first. 
    12  
    13  At the core, ACLs operate by grouping rules into policies, then associating one or more policies with a token.
    14  
    15  To complete this guide, you should have an operational Consul 1.4+ cluster. We also recommend reading the [ACL System documentation](/docs/agent/acl-system.html). For securing Consul version 1.3 and older, please read the [legacy ACL documentation](https://www.consul.io/docs/guides/acl-legacy.html).
    16  
    17  Bootstrapping the ACL system is a multi-step process, we will cover all the necessary steps in this guide. 
    18  
    19  * [Enable ACLs on all the servers](/docs/guides/acl.html#step-1-enable-acls-on-all-the-consul-servers).
    20  * [Create the initial bootstrap token](/docs/guides/acl.html#step-2-create-the-bootstrap-token).
    21  * [Create the agent policy](/docs/guides/acl.html#step-3-create-an-agent-token-policy).
    22  * [Create the agent token](/docs/guides/acl.html#step-4-create-an-agent-token).
    23  * [Apply the new token to the servers](/docs/guides/acl.html#step-5-add-the-agent-token-to-all-the-servers). 
    24  * [Enable ACLs on the clients and apply the agent token](/docs/guides/acl.html#step-6-enable-acls-on-the-consul-clients).
    25  
    26  At the end of this guide, there are also several additional and optional steps.
    27  
    28  ## Step 1: Enable ACLs on all the Consul Servers
    29  
    30  The first step for bootstrapping the ACL system is to enable ACLs on the Consul servers in the agent configuration file. In this example, we are configuring the default policy of "deny", which means we are in whitelist mode, and a down policy of "extend-cache", which means that we will ignore token TTLs during an outage.
    31  
    32  ```json
    33  {
    34    "acl" : {
    35      "enabled" : true,
    36      "default_policy" : "deny",
    37      "down_policy" : "extend-cache"
    38    }
    39  }
    40  ```
    41  
    42  The servers will need to be restarted to load the new configuration. Please take care
    43  to restart the servers one at a time and ensure each server has joined and is operating
    44  correctly before restarting another.
    45  
    46  If ACLs are enabled correctly, we will now see the following warnings and info in the leader's logs.
    47  
    48  ```sh
    49  2018/12/12 01:36:40 [INFO] acl: Created the anonymous token
    50  2018/12/12 01:36:40 [INFO] consul: ACL bootstrap enabled
    51  2018/12/12 01:36:41 [INFO] agent: Synced node info
    52  2018/12/12 01:36:58 [WARN] agent: Coordinate update blocked by ACLs
    53  2018/12/12 01:37:40 [INFO] acl: initializing acls
    54  2018/12/12 01:37:40 [INFO] consul: Created ACL 'global-management' policy
    55  ```
    56  
    57  If you do not see ACL bootstrap enabled, the anonymous token creation, and the `global-management` policy creation message in the logs, ACLs have not been properly enabled. 
    58  
    59  Note, now that we have enabled ACLs, we will need a token to complete any operation. We can't do anything else to the cluster until we bootstrap and generate the first master token. For simplicity we will use the master token created during the bootstrap for the remainder of the guide.
    60  
    61  ## Step 2: Create the Bootstrap Token
    62  
    63  Once ACLs have been enabled we can bootstrap our first token, the bootstrap token. 
    64  The bootstrap token is a management token with unrestricted privileges. It will
    65  be shared with all the servers in the quorum, since it will be added to the 
    66  state store. 
    67  
    68  ```bash
    69  $ consul acl bootstrap
    70  AccessorID: edcaacda-b6d0-1954-5939-b5aceaca7c9a
    71  SecretID: 4411f091-a4c9-48e6-0884-1fcb092da1c8
    72  Description: Bootstrap Token (Global Management)
    73  Local: false
    74  Create Time: 2018-12-06 18:03:23.742699239 +0000 UTC
    75  Policies:
    76  00000000-0000-0000-0000-000000000001 - global-management
    77  ```
    78  
    79  On the server where the `bootstrap` command was issued we should see the following log message. 
    80  
    81  ```sh
    82  2018/12/11 15:30:23 [INFO] consul.acl: ACL bootstrap completed
    83  2018/12/11 15:30:23 [DEBUG] http: Request PUT /v1/acl/bootstrap (2.347965ms) from=127.0.0.1:40566
    84  ```
    85  
    86  Since ACLs have been enabled, we will need to use it to complete any additional operations.
    87  For example, even checking the member list will require a token.  
    88  
    89  ```sh
    90  $ consul members -token "4411f091-a4c9-48e6-0884-1fcb092da1c8"
    91  Node  Address            Status  Type    Build  Protocol  DC   Segment
    92  fox   172.20.20.10:8301  alive   server  1.4.0  2         kc  <all>
    93  bear  172.20.20.11:8301  alive   server  1.4.0  2         kc  <all>
    94  wolf  172.20.20.12:8301  alive   server  1.4.0  2         kc  <all>
    95  ```
    96  
    97  Note using the token on the command line with the `-token` flag is not 
    98  recommended, instead we will set it as an environment variable once.
    99  
   100  ```sh
   101  $ export CONSUL_HTTP_TOKEN=4411f091-a4c9-48e6-0884-1fcb092da1c8
   102  ```
   103  
   104  The bootstrap token can also be used in the server configuration file as 
   105  the [`master`](https://www.consul.io/docs/agent/options.html#acl_tokens_master) token.
   106  
   107  Note, the bootstrap token can only be created once, bootstrapping will be disabled after the master token was created. Once the ACL system is bootstrapped, ACL tokens can be managed through the
   108  [ACL API](/api/acl/acl.html).
   109  
   110  ## Step 3: Create an Agent Token Policy
   111  
   112  Before we can create a token, we will need to create its associated policy. A policy is a set of rules that can be used to specify granular permissions. To learn more about rules, read the ACL rule specification [documentation](/docs/agent/acl-rules.html).
   113  
   114  ```bash
   115  # agent-policy.hcl contains the following:
   116  node_prefix "" {
   117     policy = "write"
   118  }
   119  service_prefix "" {
   120     policy = "read"
   121  }
   122  ```
   123  
   124  This policy will allow all nodes to be registered and accessed and any service to be read. 
   125  Note, this simple policy is not recommended in production.
   126  It is best practice to create separate node policies and tokens for each node in the cluster
   127  with an exact-match node rule.
   128  
   129  We only need to create one policy and can do this on any of the servers. If you have not set the 
   130  `CONSUL_HTTP_TOKEN` environment variable to the bootstrap token, please refer to the previous step. 
   131  
   132  ```
   133  $ consul acl policy create  -name "agent-token" -description "Agent Token Policy" -rules @agent-policy.hcl
   134  ID:           5102b76c-6058-9fe7-82a4-315c353eb7f7
   135  Name:         agent-policy
   136  Description:  Agent Token Policy
   137  Datacenters:
   138  Rules:
   139  node_prefix "" {
   140     policy = "write"
   141  }
   142  
   143  service_prefix "" {
   144     policy = "read"
   145  }
   146  ```
   147  
   148  The returned value is the newly-created policy that we can now use when creating our agent token. 
   149  
   150  ## Step 4: Create an Agent Token
   151  
   152  Using the newly created policy, we can create an agent token. Again we can complete this process on any of the servers. For this guide, all agents will share the same token. Note, the `SecretID` is the token used to authenticate API and CLI commands. 
   153  
   154  ```sh
   155  $ consul acl token create -description "Agent Token" -policy-name "agent-token"
   156  AccessorID:   499ab022-27f2-acb8-4e05-5a01fff3b1d1
   157  SecretID:     da666809-98ca-0e94-a99c-893c4bf5f9eb
   158  Description:  Agent Token
   159  Local:        false
   160  Create Time:  2018-10-19 14:23:40.816899 -0400 EDT
   161  Policies:
   162     fcd68580-c566-2bd2-891f-336eadc02357 - agent-token
   163  ```
   164  
   165  ## Step 5: Add the Agent Token to all the Servers
   166  
   167  Our final step for configuring the servers is to assign the token to all of our
   168  Consul servers via the configuration file and reload the Consul service 
   169  on all of the servers, one last time.
   170  
   171  ```json
   172  {
   173    "primary_datacenter": "dc1",
   174    "acl" : {
   175      "enabled" : true,
   176      "default_policy" : "deny",
   177      "down_policy" : "extend-cache",
   178      "tokens" : {
   179        "agent" : "da666809-98ca-0e94-a99c-893c4bf5f9eb"
   180      }
   181    }
   182  }
   183  ```
   184  
   185  ~> Note: In Consul version 1.4.2 and older any ACL updates
   186  in the agent configuration file will require a full restart of the 
   187  Consul service. 
   188  
   189  At this point we should no longer see the coordinate warning in the servers logs, however, we should continue to see that the node information is in sync.
   190  
   191  ```sh
   192  2018/12/11 15:34:20 [DEBUG] agent: Node info in sync
   193  ```
   194  
   195  It is important to ensure the servers are configured properly, before enable ACLs 
   196  on the clients. This will reduce any duplicate work and troubleshooting, if there
   197  is a misconfiguration.  
   198  
   199  #### Ensure the ACL System is Configured Properly
   200  
   201  Before configuring the clients, we should check that the servers are healthy. To do this, let's view the catalog.
   202  
   203  ```sh
   204  curl http://127.0.0.1:8500/v1/catalog/nodes -H 'x-consul-token: 4411f091-a4c9-48e6-0884-1fcb092da1c8' 
   205  [
   206      {
   207          "Address": "172.20.20.10",
   208          "CreateIndex": 7,
   209          "Datacenter": "kc",
   210          "ID": "881cfb69-2bcd-c2a9-d87c-cb79fc454df9",
   211          "Meta": {
   212              "consul-network-segment": ""
   213          },
   214          "ModifyIndex": 10,
   215          "Node": "fox",
   216          "TaggedAddresses": {
   217              "lan": "172.20.20.10",
   218              "wan": "172.20.20.10"
   219          }
   220      }
   221  ]
   222  ``` 
   223  
   224  All the values should be as expected. Particularly, if `TaggedAddresses` is `null` it is likely we have not configured ACLs correctly. A good place to start debugging is reviewing the Consul logs on all the servers.
   225  
   226  If you encounter issues that are unresolvable, or misplace the bootstrap token, you can reset the ACL system by updating the index. First re-run the bootstrap command to get the index number.
   227  
   228  ```
   229  $ consul acl bootstrap
   230  Failed ACL bootstrapping: Unexpected response code: 403 (Permission denied: ACL bootstrap no longer allowed (reset index: 13))
   231  ```
   232  
   233  Then write the reset index into the bootstrap reset file: (here the reset index is 13)
   234  
   235  ```
   236  $ echo 13 >> <data-directory>/acl-bootstrap-reset
   237  ```
   238  
   239  After reseting the ACL system you can start again at Step 2. 
   240  
   241  ## Step 6: Enable ACLs on the Consul Clients
   242  
   243  Since ACL enforcement also occurs on the Consul clients, we need to also restart them
   244  with a configuration file that enables ACLs. We can use the same ACL agent token that we created for the servers. The same token can be used because we did not specify any node or service prefixes.
   245  
   246  ```json
   247  {
   248    "acl" : {
   249      "enabled" : true,
   250      "default_policy" : "deny",
   251      "down_policy" : "extend-cache",
   252      "tokens" : {
   253        "agent" : "da666809-98ca-0e94-a99c-893c4bf5f9eb"
   254      }
   255    }
   256  }
   257  ```
   258  
   259  To ensure the agent's are configured correctly, we can again use the `/catalog` endpoint. 
   260  
   261  ## Additional ACL Configuration
   262  
   263  Now that the nodes have been configured to use ACLs, we can configure the CLI, UI, and nodes to use specific tokens. All of the following steps are optional examples. In your own environment you will likely need to create more fine grained policies.
   264  
   265  #### Configure the Anonymous Token (Optional)
   266  
   267  The anonymous token is created during the bootstrap process, `consul acl bootstrap`. It is implicitly used if no token is supplied. In this section we will update the existing token with a newly created policy.
   268  
   269  At this point ACLs are bootstrapped with ACL agent tokens configured, but there are no
   270  other policies set up. Even basic operations like `consul members` will be restricted
   271  by the ACL default policy of "deny":
   272  
   273  ```
   274  $ consul members
   275  ```
   276  
   277  We will not receive an error, since the ACL has filtered what we see and we are not allowed to
   278  see any nodes by default.
   279  
   280  If we supply the token we created above we will be able to see a listing of nodes because
   281  it has write privileges to an empty `node` prefix, meaning it has access to all nodes:
   282  
   283  ```bash
   284  $ CONSUL_HTTP_TOKEN=4411f091-a4c9-48e6-0884-1fcb092da1c8 consul members
   285  Node  Address            Status  Type    Build  Protocol  DC   Segment
   286  fox   172.20.20.10:8301  alive   server  1.4.0  2         kc  <all>
   287  bear  172.20.20.11:8301  alive   server  1.4.0  2         kc  <all>
   288  wolf  172.20.20.12:8301  alive   server  1.4.0  2         kc  <all>
   289  ```
   290  
   291  It is common in many environments to allow listing of all nodes, even without a
   292  token. The policies associated with the special anonymous token can be updated to
   293  configure Consul's behavior when no token is supplied. The anonymous token is managed
   294  like any other ACL token, except that `anonymous` is used for the ID. In this example
   295  we will give the anonymous token read privileges for all nodes:
   296  
   297  ```bash
   298  $ consul acl policy create -name 'list-all-nodes' -rules 'node_prefix "" { policy = "read" }'
   299  
   300  ID:           e96d0a33-28b4-d0dd-9b3f-08301700ac72
   301  Name:         list-all-nodes
   302  Description:
   303  Datacenters:
   304  Rules:
   305  node_prefix "" { policy = "read" }
   306  
   307  $ consul acl token update -id 00000000-0000-0000-0000-000000000002 -policy-name list-all-nodes -description "Anonymous Token - Can List Nodes"
   308  
   309  Token updated successfully.
   310  AccessorID:   00000000-0000-0000-0000-000000000002
   311  SecretID:     anonymous
   312  Description:  Anonymous Token - Can List Nodes
   313  Local:        false
   314  Create Time:  0001-01-01 00:00:00 +0000 UTC
   315  Hash:         ee4638968d9061647ac8c3c99e9d37bfdd2af4d1eaa07a7b5f80af0389460948
   316  Create Index: 5
   317  Modify Index: 38
   318  Policies:
   319     e96d0a33-28b4-d0dd-9b3f-08301700ac72 - list-all-nodes
   320  
   321  ```
   322  
   323  The anonymous token is implicitly used if no token is supplied, so now we can run
   324  `consul members` without supplying a token and we will be able to see the nodes:
   325  
   326  ```bash
   327  $ consul members
   328  Node  Address            Status  Type    Build  Protocol  DC   Segment
   329  fox   172.20.20.10:8301  alive   server  1.4.0  2         kc  <all>
   330  bear  172.20.20.11:8301  alive   server  1.4.0  2         kc  <all>
   331  wolf  172.20.20.12:8301  alive   server  1.4.0  2         kc  <all>
   332  ```
   333  
   334  The anonymous token is also used for DNS lookups since there is no way to pass a
   335  token as part of a DNS request. Here's an example lookup for the "consul" service:
   336  
   337  ```
   338  $ dig @127.0.0.1 -p 8600 consul.service.consul
   339  
   340  ; <<>> DiG 9.8.3-P1 <<>> @127.0.0.1 -p 8600 consul.service.consul
   341  ; (1 server found)
   342  ;; global options: +cmd
   343  ;; Got answer:
   344  ;; ->>HEADER<<- opcode: QUERY, status: NXDOMAIN, id: 9648
   345  ;; flags: qr aa rd; QUERY: 1, ANSWER: 0, AUTHORITY: 1, ADDITIONAL: 0
   346  ;; WARNING: recursion requested but not available
   347  
   348  ;; QUESTION SECTION:
   349  ;consul.service.consul.         IN      A
   350  
   351  ;; AUTHORITY SECTION:
   352  consul.                 0       IN      SOA     ns.consul. postmaster.consul. 1499584110 3600 600 86400 0
   353  ```
   354  
   355  Now we get an `NXDOMAIN` error because the anonymous token doesn't have access to the
   356  "consul" service. Let's update the anonymous token's policy to allow for service reads of the "consul" service.
   357  
   358  ```bash
   359  $ consul acl policy create -name 'service-consul-read' -rules 'service "consul" { policy = "read" }'
   360  ID:           3c93f536-5748-2163-bb66-088d517273ba
   361  Name:         service-consul-read
   362  Description:
   363  Datacenters:
   364  Rules:
   365  service "consul" { policy = "read" }
   366  
   367  $ consul acl token update -id 00000000-0000-0000-0000-000000000002 --merge-policies -description "Anonymous Token - Can List Nodes" -policy-name service-consul-read
   368  Token updated successfully.
   369  AccessorID:   00000000-0000-0000-0000-000000000002
   370  SecretID:     anonymous
   371  Description:  Anonymous Token - Can List Nodes
   372  Local:        false
   373  Create Time:  0001-01-01 00:00:00 +0000 UTC
   374  Hash:         2c641c4f73158ef6d62f6467c68d751fccd4db9df99b235373e25934f9bbd939
   375  Create Index: 5
   376  Modify Index: 43
   377  Policies:
   378     e96d0a33-28b4-d0dd-9b3f-08301700ac72 - list-all-nodes
   379     3c93f536-5748-2163-bb66-088d517273ba - service-consul-read
   380  ```
   381  
   382  With that new policy in place, the DNS lookup will succeed:
   383  
   384  ```
   385  $ dig @127.0.0.1 -p 8600 consul.service.consul
   386  
   387  ; <<>> DiG 9.8.3-P1 <<>> @127.0.0.1 -p 8600 consul.service.consul
   388  ; (1 server found)
   389  ;; global options: +cmd
   390  ;; Got answer:
   391  ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 46006
   392  ;; flags: qr aa rd; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 0
   393  ;; WARNING: recursion requested but not available
   394  
   395  ;; QUESTION SECTION:
   396  ;consul.service.consul.         IN      A
   397  
   398  ;; ANSWER SECTION:
   399  consul.service.consul.  0       IN      A       127.0.0.1
   400  ```
   401  
   402  The next section shows an alternative to the anonymous token.
   403  
   404  #### Set Agent-Specific Default Tokens (Optional)
   405  
   406  An alternative to the anonymous token is the [`acl.tokens.default`](/docs/agent/options.html#acl_tokens_default)
   407  configuration item. When a request is made to a particular Consul agent and no token is
   408  supplied, the [`acl.tokens.default`](/docs/agent/options.html#acl_tokens_default) will be used for the token, instead of being left empty which would normally invoke the anonymous token.
   409  
   410  This behaves very similarly to the anonymous token, but can be configured differently on each
   411  agent, if desired. For example, this allows more fine grained control of what DNS requests a
   412  given agent can service or can give the agent read access to some key-value store prefixes by
   413  default.
   414  
   415  If using [`acl.tokens.default`](/docs/agent/options.html#acl_tokens_default), then it's likely the anonymous token will have a more restrictive policy than shown in these examples.
   416  
   417  #### Create Tokens for UI Use (Optional)
   418  
   419  If you utilize the Consul UI with a restrictive ACL policy, as above, the UI will not function fully using the anonymous ACL token. It is recommended that a UI-specific ACL token is used, which can be set in the UI during the web browser session to authenticate the interface.
   420  
   421  First create the new policy.
   422  
   423  ```bash
   424  $ consul acl policy create -name "ui-policy" \
   425                             -description "Necessary permissions for UI functionality" \
   426                             -rules 'key_prefix "" { policy = "write" } node_prefix "" { policy = "read" } service_prefix "" { policy = "read" }'
   427  
   428  ID:           9cb99b2b-3c20-81d4-a7c0-9ffdc2fbf08a
   429  Name:         ui-policy
   430  Description:  Necessary permissions for UI functionality
   431  Datacenters:
   432  Rules:
   433  key "" { policy = "write" } node "" { policy = "read" } service "" { policy = "read" }
   434  ```
   435  
   436  With the new policy, create a token.
   437  
   438  ```sh
   439  $ consul acl token create -description "UI Token" -policy-name "ui-policy"
   440  
   441  AccessorID:   56e605cf-a6f9-5f9d-5c08-a0e1323cf016
   442  SecretID:     117842b6-6208-446a-0d1e-daf93854857d
   443  Description:  UI Token
   444  Local:        false
   445  Create Time:  2018-10-19 14:55:44.254063 -0400 EDT
   446  Policies:
   447     9cb99b2b-3c20-81d4-a7c0-9ffdc2fbf08a - ui-policy
   448  
   449  ```
   450  
   451  The token can then be set on the "settings" page of the UI.
   452  
   453  Note, in this example, we have also given full write access to the KV through the UI.
   454  
   455  ## Summary
   456  
   457  The [ACL API](/api/acl/acl.html) can be used to create tokens for applications specific to their intended use and to create more specific ACL agent tokens for each agent's expected role. 
   458  Now that you have bootstrapped ACLs, learn more about [ACL rules](/docs/agent/acl-rules.html)
   459  
   460  ### Notes on Security 
   461  
   462  In this guide we configured a basic ACL environment with the ability to see all nodes
   463  by default, but with limited access to discover only the "consul" service. If your environment has stricter security requirements we would like to note the following and make some additional recommendations. 
   464  
   465  1. In this guide we added the agent token to the configuration file. This means the tokens are now saved on disk. If this is a security concern, tokens can be added to agents using the [Consul CLI](https://www.consul.io/docs/commands/acl/acl-set-agent-token.html). However, this process is more complicated and takes additional care. 
   466  
   467  2. It is recommended that each client get an ACL agent token with `node` write privileges for just its own node name, and `service` read privileges for just the service prefixes expected to be registered on that client.
   468  
   469  3. [Anti-entropy](/docs/internals/anti-entropy.html) syncing requires the ACL agent token
   470  to have `service:write` privileges for all services that may be registered with the agent.
   471  We recommend providing `service:write` for each separate service via a separate token that 
   472  is used when registering via the API, or provided along with the [registration in the 
   473  configuration file](https://www.consul.io/docs/agent/services.html). Note that `service:write`
   474  is the privilege required to assume the identity of a service and so Consul Connect's
   475  intentions are only enforceable to the extent that each service instance is unable to gain 
   476  `service:write` on any other service name. For more details see the Connect security
   477  [documentation](https://www.consul.io/docs/connect/security.html).
   478  
   479