github.com/outbrain/consul@v1.4.5/website/source/docs/guides/production-acls.html.md (about)

     1  ---
     2  layout: "docs"
     3  page_title: "Securing Consul with ACLs"
     4  sidebar_current: "docs-guides-acl-production"
     5  description: |-
     6    This guide walks though securing your production Consul datacenter with ACLs.
     7  ---
     8  
     9  The [Bootstrapping the ACL System guide](/advanced/day-1-operations/acl-guide)
    10  walks you through how to set up ACLs on a single datacenter. Because it
    11  introduces the basic concepts and syntax we recommend completing it before
    12  starting this guide. This guide builds on the first guide with recommendations
    13  for production workloads on a single datacenter. 
    14  
    15  After [bootstrapping the ACL
    16  system](/advanced/day-1-operations/production-acls#bootstrap-the-acl-system),
    17  you will learn how to create tokens with minimum privileges for:
    18  
    19  * [Servers and Clients](/advanced/day-1-operations/production-acls#apply-individual-tokens-to-agents)
    20  * [Services](/advanced/day-1-operations/production-acls#apply-individual-tokens-to-services)
    21  * [DNS](/advanced/day-1-operations/production-acls#token-for-dns) 
    22  * [Consul KV](/advanced/day-1-operations/production-acls#consul-kv-tokens) 
    23  * [Consul UI](/advanced/day-1-operations/production-acls#consul-ui-tokens) 
    24  
    25  ~> **Important:** For best results, use this guide during the [initial
    26  deployment](/advanced/day-1-operations/deployment-guide) of a Consul (version
    27  1.4.3 or newer) datacenter. Specifically, you should have already installed all
    28  agents and configured initial service definitions, but you should not yet rely
    29  on Consul for any service discovery or service configuration operations.  
    30  
    31  ## Bootstrap the ACL System
    32  
    33  You will bootstrap the ACL system in two steps, enable ACLs and create the
    34  bootstrap token.  
    35  
    36  ### Enable ACLs on the Agents
    37  
    38  To enable ACLs, add the following [ACL
    39  parameters](https://www.consul.io/docs/agent/options.html#configuration-key-reference)
    40  to the agent's configuration file and then restart the Consul service. If you
    41  want to reduce Consul client restarts, you can enable the ACLs 
    42  on them when you apply the token. 
    43  
    44  ```
    45  # agent.hcl 
    46  { 
    47    acl = { 
    48    	enabled = true, 
    49    	default_policy = "deny",
    50      enable_token_persistence = true
    51    	} 
    52  } 
    53  ```
    54  
    55  ~> Note: Token persistence was introduced in Consul 1.4.3. In older versions
    56  of Consul, you cannot persist tokens when using the HTTP API. 
    57  
    58  In this example, you configured the default policy of "deny", which means you
    59  are in whitelist mode. You also enabled token persistence when using the HTTP
    60  API. With persistence enabled, tokens will be persisted to disk and 
    61  reloaded when an agent restarts
    62  
    63  ~> Note: If you are bootstrapping ACLs on an existing datacenter, enable the
    64  ACLs on the agents first with `default_policy=allow`. Default policy allow will
    65  enable ACLs, but will allow all operations, allowing the cluster to function
    66  normally while you create the tokens and apply them. This will reduce downtime.
    67  You should update the configuration files on all the servers first and then
    68  initiate a rolling restart. 
    69  
    70  ### Create the Initial Bootstrap Token
    71  
    72  To create the initial bootstrap token, use the `acl bootstrap` command on one
    73  of the servers. 
    74  
    75  ```sh
    76  $ consul acl bootstrap 
    77  ```
    78  
    79  The output gives you important information about the token, including the
    80  associated policy `global-management` and `SecretID`. 
    81  
    82  ~> Note: By default, Consul assigns the `global-management` policy to the
    83  bootstrap token, which has unrestricted privileges. It is important to have one
    84  token with unrestricted privileges in case of emergencies; however you should
    85  only give a small number of administrators access to it. The `SecretID` is a
    86  UUID that you will use to identify the token when using the Consul CLI or HTTP
    87  API. 
    88  
    89  While you are setting up the ACL system, set the `CONSUL_HTTP_TOKEN`
    90  environment variable to the bootstrap token on one server, for this guide
    91  the example is on server "consul-server-one". This gives you the necessary 
    92  privileges to continue
    93  creating policies and tokens. Set the environment variable temporarily with
    94  `export`, so that it will not persist once you’ve closed the session. 
    95  
    96  ```sh 
    97  $ export CONSUL_HTTP_TOKEN=<your_token_here> 
    98  ```
    99  
   100  Now, all of the following commands in this guide can 
   101  be completed on the same server, in this
   102  case server "consul-server-one". 
   103  
   104  ## Apply Individual Tokens to Agents
   105  
   106  Adding tokens to agents is a three step process.
   107   
   108  1. [Create the agent
   109  policy](/advanced/day-1-operations/production-acls/create-the-agent-policy).
   110  2. [Create the token with the newly created
   111  policy](/advanced/day-1-operations/production-acls/create-the-agent-token).
   112  3. [Add the token to the agent](/advanced/day-1-operations/production-acls/add-the-token-to-the-agent).
   113   
   114  
   115  ### Create the Agent Policy
   116  
   117  We recommend creating agent policies that have write privileges for node
   118  related actions including registering itself in the catalog, updating node
   119  level health checks, and having write access on its configuration file. The
   120  example below has unrestricted privileges for node related actions for
   121  "consul-server-one" only.
   122   
   123  ```
   124  # consul-server-one-policy.hcl
   125  node "consul-server-one" { 
   126    policy = "write"
   127  } 
   128  ```
   129  
   130  When creating agent policies, review the [node rules](
   131  https://www.consul.io/docs/agent/acl-rules.html#node-rules). Now that 
   132  you have
   133  specified the policy, you can initialize it using the Consul
   134  CLI. To create a programmatic process, you could also use
   135  the HTTP API.
   136  
   137  ```sh 
   138  $ consul acl policy create -name consul-server-one -rules @consul-server-one-policy.hcl 
   139  ```
   140  
   141  The command output will include the policy information. 
   142  
   143  Repeat this process for all servers and clients in the Consul datacenter. Each agent should have its own policy based on the
   144  node name, that grants write privileges to it. 
   145  
   146  ### Create the Agent Token
   147  
   148  After creating the per-agent policies, create individual tokens for all the
   149  agents. You will need to include the policy in the `consul acl token create`
   150  command.
   151  
   152  ```sh 
   153  $ consul acl token create -description "consul-server-one agent token" -policy-name consul-server-one 
   154  ``` 
   155  
   156  This command returns the token information, which should include a description
   157  and policy information.
   158  
   159  Repeat this process for each agent. It is the responsibility of the operator to
   160  save tokens in a secure location; we recommend
   161  [Vault](https://www.vaultproject.io/).  
   162  
   163  ### Add the Token to the Agent.
   164  
   165  Finally, apply the tokens to the agents using the HTTP API. 
   166  Start with the servers
   167  and ensure they are working correctly before applying the client tokens. Please
   168  review the Bootstrapping the ACL System [guide](/advanced/day-1-operations/acl-guide) for example of setting the token in the agent configuration 
   169  file.
   170  
   171  ```sh
   172  $ consul acl set-agent-token -token "<your token here>" agent "<agent token here>"
   173  ``` 
   174  
   175  The data file must contain a valid token. 
   176  
   177  ```
   178  # consul-server-one-token.json
   179  {
   180    "Token": "adf4238a-882b-9ddc-4a9d-5b6758e4159e"
   181  }
   182  ```
   183  
   184  At this point, every agent that has a token can once
   185  again read and write information to Consul, but only for node-related actions.
   186  Actions for individual services are not yet allowed.
   187  
   188  ~> Note: If you are bootstrapping ACLs on and existing datacenter, remember to
   189  update the default policy to `default_policy = deny` and initiate another
   190  rolling restart. After applying the token. 
   191    
   192  
   193  ## Apply Individual Tokens to the Services
   194  
   195  The token creation and application process for services is similar to agents. 
   196  Create a policy.  Use that policy to create a token.  Add the token to the
   197  service. Service tokens are necessary for
   198   agent anti-entropy, registering and de-registering the service, and
   199   registering and de-registering the service's checks.
   200  
   201  Review the [service
   202  rules](https://www.consul.io/docs/agent/acl-rules.html#service-rules) before
   203  getting started.
   204  
   205  Below is an example service definition that needs a token after bootstrapping
   206  the ACL system.
   207  
   208  ```json 
   209  { 
   210    "service": { 
   211      "name": "dashboard", 
   212      "port": 9002, 
   213      "check": { 
   214      	"id": "dashboard-check", 
   215      	"http": "http://localhost:9002/health", 
   216      	"method": "GET",
   217      	"interval": "1s", 
   218      	"timeout": "1s" 
   219      	} 
   220      } 
   221  } 
   222  ```
   223  
   224  This service definition should be located in the [configuration
   225  directory](https://www.consul.io/docs/agent/options.html#_config_dir) on one of
   226  the clients.  
   227  
   228  First, create the policy that will grant write privileges to only the
   229  "dashboard" service. This means the "dashboard" service can register
   230  itself, update it's health checks, and write any of the fields in the [service
   231  definition](https://www.consul.io/docs/agent/services.html).
   232  
   233  ```sh
   234  # dashboard-policy.hcl
   235  service "dashboard" { 
   236  	policy = "write" 
   237  } 
   238  ```
   239  
   240  Use the policy definition to initiate the policy.
   241  
   242  ```sh 
   243  $ consul acl policy create -name "dashboard-service" -rules @dashboard-policy.hcl 
   244  ```
   245  
   246  Next, create a token with the policy.
   247  
   248  ```sh 
   249  $ consul acl token create -description "Token for Dashboard Service" -policy-name dashboard-service 
   250  ```
   251  
   252  The command will return information about the token, which should include a
   253  description and policy information. As usual, save the token to a secure
   254  location.
   255  
   256  
   257  Finally, add the token to the service definition. 
   258  
   259  ``` 
   260  { 
   261    "service": { 
   262    	"name": "dashboard", 
   263    	"port": 9002, 
   264    	“token”: “57c5d69a-5f19-469b-0543-12a487eecc66”, 
   265    	"check": { 
   266    		"id": "dashboard-check",
   267    		"http": "http://localhost:9002/health", 
   268    		"method": "GET", 
   269    		"interval": "1s",
   270    		"timeout": "1s" 
   271    		} 
   272    	} 
   273   } 
   274  ```
   275  
   276  If the service is running, you will need to restart it. Unlike with agent 
   277  tokens, there is no HTTP API endpoint to apply the token directly to the
   278  service. If the service is registered with a configuration file, you must
   279  also set the token in the configuration file. However, if you register a
   280   service with the HTTP API, you can pass the token in the [header](https://www.consul.io/api/index.html#authentication) with
   281    `X-Consul-Token` and it will be used by the service.
   282  
   283  If you are using a sidecar proxy, it can inherit the token from the service
   284  definition. Alternatively, you can create a separate token. 
   285  
   286  ## Token for DNS
   287  
   288  Depending on your use case, the token used for DNS may need policy rules for
   289  [nodes](https://www.consul.io/docs/agent/acl-rules.html#node-rules),
   290  [services](https://www.consul.io/docs/agent/acl-rules.html#service-rules), and
   291  [prepared queries](https://www.consul.io/docs/agent/acl-rules.html#prepared-query-rules).
   292  You should apply the token to the Consul agent serving DNS requests. When the
   293  DNS server makes a request to Consul, it will include the token in the request.
   294  Consul can either authorize or revoke the request, depending on the token's
   295  privileges. The token creation for DNS is the same three step process you used
   296  for agents and services, create a policy, create a token, apply the
   297  token. 
   298  
   299  Below is an example of a policy that provides read privileges for all services,
   300  nodes, and prepared queries. 
   301  
   302  ```
   303  # dns-request-policy.hcl
   304  node_prefix "" { 
   305  	policy = "read" 
   306  } 
   307  service_prefix "" { 
   308  	policy = "read" 
   309  }
   310  # only needed if using prepared queries
   311  query_prefix "" { 
   312  	policy = "read" 
   313  } 
   314  ```
   315  
   316  First, create the policy.
   317  
   318  ```sh 
   319  $ consul acl policy create -name "dns-requests" -rules @dns-request-policy.hcl 
   320  ```
   321  
   322  Next, create the token.
   323  
   324  ```sh 
   325  $ consul acl token create -description "Token for DNS Requests" -policy-name dns-requests 
   326  ```
   327  
   328  Finally, apply the token to the Consul agent serving DNS request in default token ACL
   329  configuration parameter.
   330  
   331  ```sh
   332  $ consul acl set-agent-token -token "<your token here>" default "<dns token>"
   333  ```
   334  
   335  The data file must contain a valid token. 
   336  
   337  ``` 
   338  # dns-token.json
   339  { 
   340    "Token":"5467d69a-5f19-469b-0543-12a487eecc66" 
   341  }
   342  
   343  ``` 
   344  
   345  Note, if you have multiple agents serving DNS requests you can use the same
   346   policy to create individual tokens for all of them if they are using the same rules.
   347  
   348  
   349  ## Consul KV Tokens
   350  
   351  The  process of creating tokens for Consul KV follows the same three step
   352  process as nodes and services. First create a policy, then a token, and finally
   353  apply or use the token. However, unlike tokens for nodes and services Consul KV
   354  has many varied use cases. 
   355  
   356  - Services may need to access configuration data in the key-value store. 
   357  - You may want to store distributed lock information for sessions.  
   358  - Operators may need access to
   359  update configuration values in the key-value store. . 
   360  
   361  The [rules for
   362  KV](https://www.consul.io/docs/agent/acl-rules.html#key-value-rules) have four
   363  policy levels; `deny`, `write`, `read`, and `list`.  Let's review several
   364  examples of `read` and `write`.
   365  
   366  Depending on the use case, the token will be applied differently. For services
   367  you will add the token to the HTTP client. For operators use, the
   368  operator will use the token when issuing commands, either with the CLI or API.
   369  
   370  ### Recursive Reads
   371  
   372  ``` 
   373  key_prefix "redis/" { 
   374    policy = "read" 
   375  } 
   376  ```
   377  
   378  In the above example, we are allowing any key with the prefix `redis/` to be
   379  read. If you issued the command `consul kv get -recurse redis/ -token=<your
   380  token> ` you would get a list of key/values for `redis/`. 
   381  
   382  This type of policy is good for allowing operators to recursively read
   383  configuration parameters stored in the KV. Similarly, a "write" policy with the
   384  same prefix would allow you to update any keys that begin with "redis/".
   385  
   386  ### Write Privileges for One Key
   387  
   388  ``` 
   389  key "dashboard-app" {
   390    policy = "write" 
   391  } 
   392  ```
   393  
   394  In the above example, we are allowing read and write privileges to the
   395  dashboard-app key. This allows for `get`, `delete`,  and `put` operations. 
   396  
   397  This type of token would allow an application to update and read a value in the
   398  KV store. It would also be useful for operators who need access to set specific
   399  keys. 
   400  
   401  ### Read Privileges for One Key
   402  
   403  ``` 
   404  key "counting-app" { 
   405    policy = "read" 
   406  } 
   407  ```
   408  
   409  In the above example, we are setting a read privileges for a single key,
   410  “counting-app”. This allows for only `get` operations. 
   411  
   412  This type of token allows an application to simply read from a key to get the
   413  value. This is useful for configuration parameter updates.
   414  
   415  ## Consul UI Token
   416  
   417  Once you have bootstrapped the ACL system, access to the UI will be limited.
   418  The anonymous token grants UI access if no [default
   419  token](https://www.consul.io/docs/agent/options.html#acl_tokens_default) is set
   420  on the agents, and all operations will be denied, including viewing nodes and
   421  services. 
   422  
   423  You can re-enable UI features (with flexible levels of access) by creating and
   424  distributing tokens to operators. Once you have a token, you can use it in the
   425  UI by adding it to the "ACL" page:
   426  
   427  ![Access Controls](/assets/images/guides/access-controls.png "Access Controls")
   428  
   429  After saving a new token, you will be able to see your tokens.
   430  
   431  ![Tokens](/assets/images/guides/tokens.png "Tokens")
   432  
   433  The browser stores tokens that you add to the UI. This allows you to distribute
   434  different levels of privileges to operators. Like other tokens, it's up to the
   435  operator to decide the per-token privileges. 
   436  
   437  Below is an example of policy that
   438  will allow the operator to have read access to the UI for services, nodes,
   439  key/values, and intentions. You need to have  "acl = read"  to view policies
   440  and tokens. Otherwise you will not be able to access the ACL section of the UI,
   441  not even to view the token you used to access the UI.
   442  
   443  ```
   444  # operator-ui.hcl
   445  service_prefix "" { 
   446    policy = "read" 
   447    } 
   448  key_prefix "" { 
   449    policy = "read" 
   450    }
   451  node_prefix "" { 
   452    policy = "read" 
   453    }
   454  ```
   455  
   456  ## Summary
   457  
   458  In this guide you bootstrapped the ACL system for consul and applied tokens to agents and services. You assigned tokens for DNS, Consul KV, and the Consul UI. 
   459  
   460  To learn more about Consul’s security model read the [internals documentation](https://www.consul.io/docs/internals/security.html). You can find commands relating to ACLs in our [reference documentation](https://www.consul.io/docs/commands/acl.html).
   461  
   462