2019-01-09 21:07:20 +00:00
---
2020-04-07 18:55:19 +00:00
layout: docs
2022-09-13 19:32:13 +00:00
page_title: ACL Rules
2020-04-07 18:55:19 +00:00
description: >-
2022-09-16 15:28:32 +00:00
Rules define read, write, and deny access controls for datacenter resources. Learn about these resources and how to assign rules to them, as well as their restrictions and API interactions.
2019-01-09 21:07:20 +00:00
---
2022-09-13 19:32:13 +00:00
# ACL Rules
2019-01-09 21:07:20 +00:00
2023-01-25 16:52:43 +00:00
This topic provides reference information for the types of access control list (ACL) rules you can create and how they affect access to datacenter resources. For details on how to create rules and group them into policies, see [Policies](/consul/docs/security/acl/acl-policies).
2020-04-09 00:09:01 +00:00
2022-02-10 00:07:49 +00:00
## Overview
2019-01-09 21:07:20 +00:00
2021-10-28 18:23:15 +00:00
The following table provides an overview of the resources you can use to create ACL rules.
2019-01-09 21:07:20 +00:00
2022-02-10 00:07:49 +00:00
| Resource | Description | Labels |
| ---------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------ |
2023-01-25 16:52:43 +00:00
| `acl` | Controls access to ACL operations in the [ACL API](/consul/api-docs/acl). <br/>See [ACL Resource Rules](#acl-resource-rules) for details. | No |
2022-02-10 00:07:49 +00:00
| `partition`<br/>`partition_prefix` | <EnterpriseAlert inline /> Controls access to one or more admin partitions. <br/>See [Admin Partition Rules](#admin-partition-rules) for details. | Yes |
2023-01-25 16:52:43 +00:00
| `agent`<br/>`agent_prefix` | Controls access to the utility operations in the [Agent API](/consul/api-docs/agent), such as `join` and `leave`. <br/>See [Agent Rules](#agent-rules) for details. | Yes |
| `event`<br/>`event_prefix` | Controls access to event operations in the [Event API](/consul/api-docs/event), such as firing and listing events. <br/>See [Event Rules](#event-rules) for details. | Yes |
| `key`<br/>`key_prefix` | Controls access to key/value store operations in the [KV API](/consul/api-docs/kv). <br/>Can also use the `list` access level when setting the policy disposition. <br/>Has additional value options in Consul Enterprise for integrating with [Sentinel](https://docs.hashicorp.com/sentinel/consul). <br/>See [Key/Value Rules](#key-value-rules) for details. | Yes |
| `keyring` | Controls access to keyring operations in the [Keyring API](/consul/api-docs/operator/keyring). <br/>See [Keyring Rules](#keyring-rules) for details. | No |
2022-02-10 00:07:49 +00:00
| `mesh` | Provides operator-level permissions for resources in the admin partition, such as ingress gateways or mesh proxy defaults. See [Mesh Rules](#mesh-rules) for details. | No |
2023-01-25 16:52:43 +00:00
| `peering` | Controls access to cluster peerings in the [Cluster Peering API](/consul/api-docs/peering). For more details, refer to [Peering Rules](#peering-rules). | No |
2022-02-10 00:07:49 +00:00
| `namespace`<br/>`namespace_prefix` | <EnterpriseAlert inline /> Controls access to one or more namespaces. <br/>See [Namespace Rules](#namespace-rules) for details. | Yes |
2023-01-25 16:52:43 +00:00
| `node`<br/>`node_prefix` | Controls access to node-level operations in the [Catalog API](/consul/api-docs/catalog), [Health API](/consul/api-docs/health), [Prepared Query API](/consul/api-docs/query), [Network Coordinate API](/consul/api-docs/coordinate), and [Agent API](/consul/api-docs/agent) <br/>See [Node Rules](#node-rules) for details. | Yes |
| `operator` | Controls access to cluster-level operations available in the [Operator API](/consul/api-docs/operator) excluding keyring API endpoints. <br/>See [Operator Rules](#operator-rules) for details. | No |
| `query`<br/>`query_prefix` | Controls access to create, update, and delete prepared queries in the [Prepared Query API](/consul/api-docs/query). Access to the [node](#node-rules) and [service](#service-rules) must also be granted. <br/>See [Prepared Query Rules](#prepared-query-rules) for details. | Yes |
| `service`<br/>`service_prefix` | Controls service-level operations in the [Catalog API](/consul/api-docs/catalog), [Health API](/consul/api-docs/health), [Intentions API](/consul/api-docs/connect/intentions), [Prepared Query API](/consul/api-docs/query), and [Agent API](/consul/api-docs/agent). <br/>See [Service Rules](#service-rules) for details. | Yes |
| `session`<br/>`session_prefix` | Controls access to operations in the [Session API](/consul/api-docs/session). <br/>See [Session Rules](#session-rules) for details. | Yes |
2022-02-10 00:07:49 +00:00
The following resources are not covered by ACL policies:
2023-01-25 16:52:43 +00:00
- The [Status API](/consul/api-docs/status) is used by servers when bootstrapping and exposes basic IP and port information about the servers, and does not allow modification of any state.
- The datacenter listing operation of the [Catalog API](/consul/api-docs/catalog#list-datacenters) similarly exposes the names of known Consul datacenters, and does not allow modification of any state.
2023-05-05 17:41:40 +00:00
- The [service mesh CA roots endpoint](/consul/api-docs/connect/ca#list-ca-root-certificates) exposes just the public TLS certificate which other systems can use to verify the TLS connection with Consul.
2022-02-10 00:07:49 +00:00
2023-01-25 16:52:43 +00:00
-> **Consul Enterprise Namespace** - In addition to directly-linked policies, roles, and service identities, Consul Enterprise enables ACL policies and roles to be defined in the [Namespaces definition](/consul/docs/enterprise/namespaces#namespace-definition) (Consul Enterprise 1.7.0+).
2019-01-09 21:07:20 +00:00
2021-10-28 18:23:15 +00:00
The following topics provide additional details about the available resources.
2022-02-10 00:07:49 +00:00
## ACL Resource Rules
2021-10-28 18:23:15 +00:00
2023-01-25 16:52:43 +00:00
The `acl` resource controls access to ACL operations in the [ACL API](/consul/api-docs/acl). Only one `acl` rule is allowed per policy. The value is set to one of the [policy dispositions](#policy-dispositions).
2021-10-28 18:23:15 +00:00
The `acl = "write"` rule is also required to create snapshots. This is because all token secrets are contained within the snapshot.
2021-11-23 20:51:55 +00:00
Rules for ACL resources do not use labels.
2021-10-28 18:23:15 +00:00
2022-05-09 06:25:01 +00:00
In the following example, the `acl` rule is configured with `write` access to the ACL API.
The rule enables the operator to read or write ACLs, as well as discover the secret ID of any token.
2019-01-09 21:07:20 +00:00
2021-11-10 02:19:04 +00:00
<CodeTabs heading="Example acl rule">
2019-05-01 21:11:23 +00:00
```hcl
2019-01-09 21:07:20 +00:00
acl = "write"
```
2021-11-10 02:19:04 +00:00
```json
2022-05-08 20:44:50 +00:00
{
"acl": "write"
}
2021-11-10 02:19:04 +00:00
```
</CodeTabs>
2022-02-10 00:07:49 +00:00
## Admin Partition Rules <EnterpriseAlert inline />
2021-10-28 18:23:15 +00:00
2021-11-23 20:51:55 +00:00
The `partition` and `partition_prefix` resource controls access to one or more admin partitions.
You can include any number of namespace rules inside the admin partition.
2019-01-09 21:07:20 +00:00
2022-05-09 06:25:01 +00:00
In the following example, the policy grants `write` access to the `ex-namespace`
namespace, as well as namespaces prefixed with `exns-` in the `example` partition.
2022-08-08 17:44:25 +00:00
The `mesh` and `peering` resources are also scoped to the admin partition rule, which grants
`write` access to the `mesh` and `peering` resources in the `example` partition.
2022-05-09 06:25:01 +00:00
In addition, the policy grants `read` access to the `ex-namespace` namespace, as
well as namespaces prefixed with `exns-` in all partitions containing the
2022-08-08 17:44:25 +00:00
`example-` prefix. Read access is granted for the `mesh` and `peering` resources
scoped within the associated partition.
2021-10-28 18:23:15 +00:00
2021-11-10 02:19:04 +00:00
<CodeTabs heading="Example admin partition rules">
2021-10-28 18:23:15 +00:00
```hcl
2021-11-23 20:51:55 +00:00
partition "example" {
2021-10-28 18:23:15 +00:00
mesh = "write"
2022-08-08 17:44:25 +00:00
peering = "write"
2021-11-23 20:51:55 +00:00
node "my-node" {
2021-10-28 18:23:15 +00:00
policy = "write"
}
2022-08-08 17:44:25 +00:00
2021-10-28 18:23:15 +00:00
namespace "ex-namespace" {
2022-05-08 20:44:50 +00:00
policy = "write"
2021-10-28 18:23:15 +00:00
}
2022-08-08 17:44:25 +00:00
2021-11-23 20:51:55 +00:00
namespace_prefix "exns-" {
2022-05-08 20:44:50 +00:00
policy = "write"
2021-11-23 20:51:55 +00:00
}
2021-10-28 18:23:15 +00:00
}
2022-05-08 20:44:50 +00:00
partition_prefix "example-" {
mesh = "read"
2022-08-08 17:44:25 +00:00
peering = "read"
2022-05-08 20:44:50 +00:00
node "my-node" {
policy = "read"
}
namespace "ex-namespace" {
policy = "read"
}
2021-10-28 18:23:15 +00:00
}
```
2021-11-10 02:19:04 +00:00
```json
2022-01-10 21:13:13 +00:00
{
2022-05-08 20:44:50 +00:00
"partition": {
"example": {
"mesh": "write",
"node": {
"my-node": {
"policy": "write"
}
},
"namespace": {
"ex-namespace": {
"policy": "write"
2022-02-10 00:07:49 +00:00
}
2022-05-08 20:44:50 +00:00
},
"namespace_prefix": {
"exns-": {
"policy": "write"
}
}
2022-02-10 00:07:49 +00:00
}
2022-05-08 20:44:50 +00:00
},
"partition_prefix": {
"example-": {
"mesh": "read",
"node": {
"my-node": {
2022-02-10 00:07:49 +00:00
"policy": "read"
}
2022-05-08 20:44:50 +00:00
},
"namespace": {
"ex-namespace": {
"policy": "read"
2022-02-10 00:07:49 +00:00
}
2022-05-08 20:44:50 +00:00
}
2022-02-10 00:07:49 +00:00
}
2022-05-08 20:44:50 +00:00
}
2022-01-10 21:13:13 +00:00
}
2021-11-10 02:19:04 +00:00
```
</CodeTabs>
2022-02-10 00:07:49 +00:00
## Agent Rules
2019-01-09 21:07:20 +00:00
2023-01-25 16:52:43 +00:00
The `agent` and `agent_prefix` resources control access to the utility operations in the [Agent API](/consul/api-docs/agent),
2019-01-09 21:07:20 +00:00
such as join and leave. All of the catalog-related operations are covered by the [`node` or `node_prefix`](#node-rules)
and [`service` or `service_prefix`](#service-rules) policies instead.
2021-11-10 02:19:04 +00:00
<CodeTabs heading="Example agent rules">
2019-01-09 21:07:20 +00:00
2019-05-01 21:11:23 +00:00
```hcl
2019-01-09 21:07:20 +00:00
agent "foo" {
policy = "write"
}
2022-05-08 20:44:50 +00:00
agent_prefix "" {
policy = "read"
}
2019-01-09 21:07:20 +00:00
agent_prefix "bar" {
policy = "deny"
}
```
2021-11-10 02:19:04 +00:00
```json
2022-05-08 20:44:50 +00:00
{
"agent": {
"foo": {
"policy": "write"
}
},
"agent_prefix": {
"": {
"policy": "read"
},
"bar": {
"policy": "deny"
}
}
}
2021-11-10 02:19:04 +00:00
```
</CodeTabs>
2019-01-09 21:07:20 +00:00
Agent rules are keyed by the node name they apply to. In the example above the rules
2022-05-09 06:25:01 +00:00
allow read-write access to the node with the _exact_ name `foo`, read-only access
to any node name by using the empty prefix, and denies all access to any node
name that starts with `bar`.
2019-01-09 21:07:20 +00:00
2023-01-25 16:52:43 +00:00
Since [Agent API](/consul/api-docs/agent) utility operations may be required before an agent is joined to
2019-01-09 21:07:20 +00:00
a cluster, or during an outage of the Consul servers or ACL datacenter, a special token may be
2023-01-25 16:52:43 +00:00
configured with [`acl.tokens.agent_recovery`](/consul/docs/agent/config/config-files#acl_tokens_agent_recovery) to allow
2019-01-09 21:07:20 +00:00
write access to these operations even if no ACL resolution capability is available.
2022-02-10 00:07:49 +00:00
## Event Rules
2019-01-09 21:07:20 +00:00
2023-01-25 16:52:43 +00:00
The `event` and `event_prefix` resources control access to event operations in the [Event API](/consul/api-docs/event), such as
2019-01-09 21:07:20 +00:00
firing events and listing events.
2021-11-10 02:19:04 +00:00
<CodeTabs heading="Example event rules">
2019-01-09 21:07:20 +00:00
2019-05-01 21:11:23 +00:00
```hcl
2019-01-09 21:07:20 +00:00
event_prefix "" {
policy = "read"
}
event "deploy" {
policy = "write"
}
```
2022-02-10 00:07:49 +00:00
2021-11-10 02:19:04 +00:00
```json
2022-05-08 20:44:50 +00:00
{
"event_prefix": {
"": {
"policy": "read"
}
},
"event": {
"deploy": {
"policy": "write"
}
}
}
2021-11-10 02:19:04 +00:00
```
2019-01-09 21:07:20 +00:00
2021-11-10 02:19:04 +00:00
</CodeTabs>
2019-01-09 21:07:20 +00:00
2021-11-03 22:16:42 +00:00
Event rules are labeled with the event name they apply to. In the example above, the rules allow
2019-01-09 21:07:20 +00:00
read-only access to any event, and firing of the "deploy" event.
2023-01-25 16:52:43 +00:00
The [`consul exec`](/consul/commands/exec) command uses events with the "\_rexec" prefix during
2019-01-09 21:07:20 +00:00
operation, so to enable this feature in a Consul environment with ACLs enabled, you will need to
give agents a token with access to this event prefix, in addition to configuring
2023-01-25 16:52:43 +00:00
[`disable_remote_exec`](/consul/docs/agent/config/config-files#disable_remote_exec) to `false`.
2019-01-09 21:07:20 +00:00
2022-02-10 00:07:49 +00:00
## Key/Value Rules
2019-01-09 21:07:20 +00:00
2023-01-25 16:52:43 +00:00
The `key` and `key_prefix` resources control access to key/value store operations in the [KV API](/consul/api-docs/kv).
2021-11-10 02:19:04 +00:00
<CodeTabs heading="Example key rules">
2019-01-09 21:07:20 +00:00
2019-05-01 21:11:23 +00:00
```hcl
2019-01-09 21:07:20 +00:00
key_prefix "" {
policy = "read"
}
key "foo" {
policy = "write"
}
key "bar" {
policy = "deny"
}
```
2022-02-10 00:07:49 +00:00
2021-11-10 02:19:04 +00:00
```json
2022-05-08 20:44:50 +00:00
{
"key_prefix": {
"": {
"policy": "read"
}
},
"key": {
"foo": {
"policy": "write"
},
"bar": {
"policy": "deny"
}
}
}
2021-11-10 02:19:04 +00:00
```
</CodeTabs>
2019-01-09 21:07:20 +00:00
2021-11-03 22:16:42 +00:00
Key rules are labeled with the key name they apply to. In the example above, the rules allow read-only access
2019-01-09 21:07:20 +00:00
to any key name with the empty prefix rule, allow read-write access to the "foo" key, and deny access to the "bar" key.
2022-02-10 00:07:49 +00:00
### List Policy for Keys
2019-01-09 21:07:20 +00:00
2022-05-09 06:25:01 +00:00
Enable the `list` policy disposition (Consul 1.0+) by setting the
`acl.enable_key_list_policy` parameter to `true`. The disposition provides
2023-01-25 16:52:43 +00:00
recursive access to `key` entries. Refer to the [KV API](/consul/api-docs/kv#recurse)
2022-05-09 06:25:01 +00:00
documentation for additional information.
In the following example, `key` resources that start with `bar` may be listed.
2021-11-10 02:19:04 +00:00
<CodeTabs heading="Example 'key' rules">
2019-01-09 21:07:20 +00:00
2019-05-01 21:11:23 +00:00
```hcl
2019-01-09 21:07:20 +00:00
key_prefix "" {
policy = "deny"
}
key_prefix "bar" {
policy = "list"
}
key_prefix "baz" {
policy = "read"
}
```
2021-11-10 02:19:04 +00:00
```json
2022-05-08 20:44:50 +00:00
{
"key_prefix": {
"": {
"policy": "deny"
},
"bar": {
"policy": "list"
},
"baz": {
"policy": "read"
}
}
}
2021-11-10 02:19:04 +00:00
```
</CodeTabs>
2019-01-09 21:07:20 +00:00
In the example above, the rules allow reading the key "baz", and only allow recursive reads on the prefix "bar".
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.
2020-04-23 22:13:18 +00:00
#### Sentinel Integration <EnterpriseAlert inline />
2019-01-09 21:07:20 +00:00
Consul Enterprise supports additional optional fields for key write policies for
2022-09-14 22:45:42 +00:00
[Sentinel](https://docs.hashicorp.com/sentinel/consul) integration.
2019-01-09 21:07:20 +00:00
2019-05-01 21:11:23 +00:00
```hcl
2019-01-09 21:07:20 +00:00
key "foo" {
policy = "write"
sentinel {
code = <<EOF
import "strings"
main = rule { strings.has_suffix(value, "bar") }
EOF
enforcementlevel = "hard-mandatory"
}
}
```
2023-01-25 16:52:43 +00:00
For more detailed information, see the [Consul Sentinel documentation](/consul/docs/agent/sentinel).
2019-01-09 21:07:20 +00:00
2022-02-10 00:07:49 +00:00
## Keyring Rules
2019-01-09 21:07:20 +00:00
2023-01-25 16:52:43 +00:00
The `keyring` resource controls access to keyring operations in the [Keyring API](/consul/api-docs/operator/keyring). Only one keyring policy is allowed per rule set. The value is set to one of the policy dispositions, but may be read and updated.
2019-01-09 21:07:20 +00:00
2021-11-10 02:19:04 +00:00
<CodeTabs heading="Example keyring rule">
2019-01-09 21:07:20 +00:00
2019-05-01 21:11:23 +00:00
```hcl
2019-01-09 21:07:20 +00:00
keyring = "write"
```
2021-11-23 20:51:55 +00:00
```json
2022-05-08 20:44:50 +00:00
{
"keyring": "write"
}
2021-11-10 02:19:04 +00:00
```
</CodeTabs>
2019-01-09 21:07:20 +00:00
2022-08-08 17:44:25 +00:00
## Mesh Rules
The `mesh` resource controls access to ingress gateways, terminating gateways, and mesh configuration entries.
In Consul Enterprise, mesh rules are scoped to an admin partition. Therefore, they can be nested in an
[admin partition rule](#admin-partition-rules) but not a [namespace rule](#namespace-rules).
2021-10-28 18:23:15 +00:00
2022-08-08 17:44:25 +00:00
The following rule grants read and write access:
2021-10-28 18:23:15 +00:00
2021-11-10 02:19:04 +00:00
<CodeTabs heading="Example mesh rule">
2021-11-03 22:16:42 +00:00
```hcl
mesh = "write"
```
2022-02-10 00:07:49 +00:00
2021-11-23 20:51:55 +00:00
```json
2022-05-08 20:44:50 +00:00
{
"mesh": "write"
}
2021-11-10 02:19:04 +00:00
```
2022-02-10 00:07:49 +00:00
2021-11-10 02:19:04 +00:00
</CodeTabs>
2021-11-03 22:16:42 +00:00
2021-11-23 20:51:55 +00:00
See [Admin Partition Rules](#admin-partition-rules) for another example rule that uses the `mesh` resource.
2021-10-28 18:23:15 +00:00
2022-02-10 00:07:49 +00:00
## Namespace Rules <EnterpriseAlert inline />
2021-10-28 18:23:15 +00:00
2021-11-23 20:51:55 +00:00
The `namespace` and `namespace_prefix` resource controls access to Consul namespaces. Namespaces define a scope of resources for which ACL rules apply. ACL rules, themselves, can then be defined to only to apply to specific namespaces.
2021-10-28 18:23:15 +00:00
2021-11-23 20:51:55 +00:00
-> **Consul 1.7.0 and later**: The ability to add many types of resources to separate namespaces was added to [Consul Enterprise](https://www.hashicorp.com/consul) 1.7.0.
2021-10-28 18:23:15 +00:00
The following examples describe how namespace rules can be defined in a policy:
2021-11-10 02:19:04 +00:00
<CodeTabs heading="Example namespace rules">
2021-10-28 18:23:15 +00:00
```hcl
namespace_prefix "" {
2022-05-08 20:44:50 +00:00
# grants permission to create and edit all namespaces
policy = "write"
2021-10-28 18:23:15 +00:00
# grant service:read for all services in all namespaces
service_prefix "" {
policy = "read"
}
# grant node:read for all nodes in all namespaces
node_prefix "" {
policy = "read"
}
}
namespace "foo" {
# grants permission to manage ACLs only for the foo namespace
acl = "write"
2021-11-03 22:16:42 +00:00
# grants permission to create and edit the foo namespace
2021-10-28 18:23:15 +00:00
policy = "write"
# grants write permissions to the KV for namespace foo
key_prefix "" {
policy = "write"
}
# grants write permissions for sessions for namespace foo
session_prefix "" {
policy = "write"
}
# grants service:write for all services in the foo namespace
service_prefix "" {
policy = "write"
}
# grants node:read for all nodes
node_prefix "" {
policy = "read"
}
}
```
2022-02-10 00:07:49 +00:00
2021-11-10 02:19:04 +00:00
```json
{
2022-05-08 20:44:50 +00:00
"namespace_prefix": {
"": {
"policy": "write",
"service_prefix": {
"": {
"policy": "read"
}
},
"node_prefix": {
"": {
"policy": "read"
2022-02-10 00:07:49 +00:00
}
2022-05-08 20:44:50 +00:00
}
2022-02-10 00:07:49 +00:00
}
2022-05-08 20:44:50 +00:00
},
"namespace": {
"foo": {
"acl": "write",
"policy": "write",
"key_prefix": {
"": {
"policy": "write"
2022-02-10 00:07:49 +00:00
}
2022-05-08 20:44:50 +00:00
},
"session_prefix": {
"": {
"policy": "write"
}
},
"service_prefix": {
"": {
"policy": "write"
}
},
"node_prefix": {
"": {
"policy": "read"
}
}
2022-02-10 00:07:49 +00:00
}
2022-05-08 20:44:50 +00:00
}
2021-11-10 02:19:04 +00:00
}
```
</CodeTabs>
2021-10-28 18:23:15 +00:00
2022-02-10 00:07:49 +00:00
### Restrictions
2021-10-28 18:23:15 +00:00
The following restrictions apply when a rule is defined in any user-created namespace:
1. `operator` rules are not allowed.
2. `event` rules are not allowed.
3. `keyring` rules are not allowed.
4. `query` rules are not allowed.
5. `node` rules that attempt to grant `write` privileges are not allowed.
These restrictions do not apply to the `default` namespace created by Consul. In general all of the
above are permissions that only an operator should have and thus granting these permissions can
only be done within the default namespace.
2022-02-10 00:07:49 +00:00
### Implicit Namespacing
2021-10-28 18:23:15 +00:00
Rules and policies created within a namespace will inherit the namespace configuration.
This means that rules and policies will be implicitly namespaced and do not need additional configuration.
The restrictions outlined above will apply to these rules and policies. Additionally, rules and policies within a
specific namespace are prevented from accessing resources in another namespace.
2022-02-10 00:07:49 +00:00
## Node Rules
2019-01-09 21:07:20 +00:00
2021-11-03 22:16:42 +00:00
The `node` and `node_prefix` resources control access to the following API behaviors:
2023-01-25 16:52:43 +00:00
- node-level registration and read access to the [Catalog API](/consul/api-docs/catalog)
- service discovery with the [Health API](/consul/api-docs/health)
- filtering results in [Agent API](/consul/api-docs/agent) operations, such as fetching the list of cluster members.
2019-01-09 21:07:20 +00:00
2021-11-03 22:16:42 +00:00
You can use resource labels to scope the rule to a specific resource or set of resources.
2021-11-23 20:51:55 +00:00
The following example rule uses an empty prefix label, which provides read-only access to all nodes.
2021-11-03 22:16:42 +00:00
The rule also provides read-write access to the `app` node and denies all access to the `admin` node:
2019-01-09 21:07:20 +00:00
2021-11-10 02:19:04 +00:00
<CodeTabs heading="Example node rules">
2019-01-09 21:07:20 +00:00
2019-05-01 21:11:23 +00:00
```hcl
2019-01-09 21:07:20 +00:00
node_prefix "" {
policy = "read"
}
node "app" {
policy = "write"
}
node "admin" {
policy = "deny"
}
```
2021-11-10 02:19:04 +00:00
```json
2022-05-08 20:44:50 +00:00
{
"node_prefix": {
"": {
"policy": "read"
2023-09-21 18:35:16 +00:00
}
2022-05-08 20:44:50 +00:00
},
"node": {
"app": {
"policy": "write"
},
"admin": {
"policy": "deny"
}
}
}
2021-11-10 02:19:04 +00:00
```
2022-02-10 00:07:49 +00:00
2021-11-10 02:19:04 +00:00
</CodeTabs>
2019-01-09 21:07:20 +00:00
2022-02-10 00:07:49 +00:00
### Registering and Querying Node Information
2019-01-09 21:07:20 +00:00
2021-11-23 20:51:55 +00:00
Agents must be configured with `write` privileges for their own node name so that the agent can register their node metadata, tagged addresses, and other information in the catalog.
2021-11-03 22:16:42 +00:00
If configured incorrectly, the agent will print an error to the console when it tries to sync its state with the catalog.
2023-01-25 16:52:43 +00:00
Configure `write` access in the [`acl.tokens.agent`](/consul/docs/agent/config/config-files#acl_tokens_agent) parameter.
2019-01-09 21:07:20 +00:00
2023-01-25 16:52:43 +00:00
The [`acl.token.default`](/consul/docs/agent/config/config-files#acl_tokens_default) used by the agent should have `read` access to a given node so that the DNS interface can be queried.
2019-01-09 21:07:20 +00:00
2021-11-03 22:16:42 +00:00
Node rules are used to filter query results when reading from the catalog or retrieving information from the health endpoints. This allows for configurations where a token has access to a given service name, but only on an allowed subset of node names.
2019-01-09 21:07:20 +00:00
2023-01-25 16:52:43 +00:00
Consul agents check tokens locally when health checks are registered and when Consul performs periodic [anti-entropy](/consul/docs/architecture/anti-entropy) syncs.
2021-11-03 22:16:42 +00:00
These actions may required an ACL token to complete. Use the following methods to configure ACL tokens for registration events:
2019-01-09 21:07:20 +00:00
2023-01-25 16:52:43 +00:00
* Configure a global token in the [acl.tokens.default](/consul/docs/agent/config/config-files#acl_tokens_default) parameter.
2022-03-15 23:13:58 +00:00
This allows a single token to be used during all check registration operations.
2022-01-10 21:13:13 +00:00
* Provide an ACL token with `service` and `check` definitions at registration time.
2022-03-15 23:13:58 +00:00
This allows for greater flexibility and enables the use of multiple tokens on the same agent.
2023-02-28 22:09:56 +00:00
Refer to the [services](/consul/docs/services/usage/define-services) and [checks](/consul/docs/services/usage/checks) documentation for examples. You can also pass tokens to the [HTTP API](/consul/api-docs) for operations that require them.
2019-01-09 21:07:20 +00:00
2022-11-18 22:39:41 +00:00
### Reading Imported Nodes
2023-01-25 16:52:43 +00:00
Nodes rules affect read access to nodes with services exported by [`exported-services` configuration entries](/consul/docs/connect/config-entries/exported-services#reading-services), including nodes imported from [cluster peerings](/consul/docs/connect/cluster-peering) or [admin partitions](/consul/docs/enterprise/admin-partitions) (Enterprise-only).
2022-11-18 22:39:41 +00:00
Read access to all imported nodes is granted when either of the following rule sets are attached to a token:
2023-11-22 17:29:43 +00:00
- `service:write` is granted to any service.
2022-11-18 22:39:41 +00:00
- `node:read` is granted to all nodes.
2024-05-22 22:36:57 +00:00
For Consul Enterprise, either set of rules must be scoped to the requesting service's partition and at least one namespace.
2022-11-18 22:39:41 +00:00
You may need similarly scoped [Service Rules](#reading-imported-services) to read Consul data, depending on the endpoint (e.g. `/v1/health/service/:name`).
2023-11-22 17:29:43 +00:00
These permissions are satisfied when using a [service identity](/consul/docs/security/acl/acl-roles#service-identities).
2022-11-18 22:39:41 +00:00
2023-01-25 16:52:43 +00:00
Refer to [Reading Services](/consul/docs/connect/config-entries/exported-services#reading-services) for example ACL policies used to read imported services using the health endpoint.
2022-11-18 22:39:41 +00:00
2022-02-10 00:07:49 +00:00
## Operator Rules
2019-01-09 21:07:20 +00:00
The `operator` resource controls access to cluster-level operations in the
2023-01-25 16:52:43 +00:00
[Operator API](/consul/api-docs/operator), other than the [Keyring API](/consul/api-docs/operator/keyring).
2019-01-09 21:07:20 +00:00
2021-11-10 02:19:04 +00:00
Only one operator rule allowed per rule set. In the following example, the token may be used to query the operator endpoints for
diagnostic purposes but it will not make changes.
<CodeTabs heading="Example operator rule">
2019-01-09 21:07:20 +00:00
2019-05-01 21:11:23 +00:00
```hcl
2019-01-09 21:07:20 +00:00
operator = "read"
```
2022-02-10 00:07:49 +00:00
2021-11-10 02:19:04 +00:00
```json
2022-05-08 20:44:50 +00:00
{
"operator": "read"
}
2021-11-10 02:19:04 +00:00
```
2022-02-10 00:07:49 +00:00
2021-11-10 02:19:04 +00:00
</CodeTabs>
2019-01-09 21:07:20 +00:00
2022-08-08 17:44:25 +00:00
## Peering Rules
2023-01-25 16:52:43 +00:00
The `peering` resource controls access to cluster peerings in the [Cluster Peering API](/consul/api-docs/peering).
2022-08-08 17:44:25 +00:00
In Consul Enterprise, peering rules are scoped to an admin partition. Therefore, they can be nested in an
[admin partition rule](#admin-partition-rules) but not a [namespace rule](#namespace-rules).
The following rule grants read and write access:
<CodeTabs heading="Example peering rule">
```hcl
peering = "write"
```
```json
{
"peering": "write"
}
```
</CodeTabs>
For an example of how to apply rules for the `peering` resource alongside other rules, refer to the example configuration in [Admin Partition Rules](#admin-partition-rules).
2022-02-10 00:07:49 +00:00
## Prepared Query Rules
2019-01-09 21:07:20 +00:00
The `query` and `query_prefix` resources control access to create, update, and delete prepared queries in the
2023-01-25 16:52:43 +00:00
[Prepared Query API](/consul/api-docs/query). Specify the resource label in query rules to determine the scope of the rule.
2021-11-23 20:51:55 +00:00
The resource label in the following example is empty. As a result, the rules allow read-only access to query resources with any name.
2021-11-03 22:16:42 +00:00
The rules also grant read-write access to the query named `foo`, which allows control of the query namespace to be delegated based on ACLs:
2019-01-09 21:07:20 +00:00
2021-11-10 02:19:04 +00:00
<CodeTabs heading="Example query rules">
2019-01-09 21:07:20 +00:00
2019-05-01 21:11:23 +00:00
```hcl
2019-01-09 21:07:20 +00:00
query_prefix "" {
policy = "read"
}
query "foo" {
policy = "write"
}
```
2021-11-10 02:19:04 +00:00
```json
2022-05-08 20:44:50 +00:00
{
"query_prefix": {
"": {
"policy": "read"
}
},
"query": {
"foo": {
"policy": "write"
}
}
}
2021-11-10 02:19:04 +00:00
```
2022-02-10 00:07:49 +00:00
2021-11-10 02:19:04 +00:00
</CodeTabs>
2021-11-03 22:16:42 +00:00
Executing queries is subject to `node`/`node_prefix` and `service`/`service_prefix`
policies.
2019-01-09 21:07:20 +00:00
There are a few variations when using ACLs with prepared queries, each of which uses ACLs in one of two
ways: open, protected by unguessable IDs or closed, managed by ACL policies. These variations are covered
here, with examples:
2020-04-07 18:55:19 +00:00
- Static queries with no `Name` defined are not controlled by any ACL policies.
2019-01-09 21:07:20 +00:00
These types of queries are meant to be ephemeral and not shared to untrusted
clients, and they are only reachable if the prepared query ID is known. Since
these IDs are generated using the same random ID scheme as ACL Tokens, it is
infeasible to guess them. When listing all prepared queries, only a management
token will be able to see these types, though clients can read instances for
which they have an ID. An example use for this type is a query built by a
startup script, tied to a session, and written to a configuration file for a
process to use via DNS.
2020-04-07 18:55:19 +00:00
- Static queries with a `Name` defined are controlled by the `query` and `query_prefix`
2019-01-09 21:07:20 +00:00
ACL resources. Clients are required to have an ACL token with permissions on to
access that query name. Clients can list or read queries for
which they have "read" access based on their prefix, and similar they can
update any queries for which they have "write" access. An example use for
2021-06-29 21:04:24 +00:00
this type is a query with a well-known name (eg. `prod-primary-customer-db`)
2019-01-09 21:07:20 +00:00
that is used and known by many clients to provide geo-failover behavior for
a database.
2023-01-25 16:52:43 +00:00
- [Template queries](/consul/api-docs/query#prepared-query-templates)
2019-01-09 21:07:20 +00:00
queries work like static queries with a `Name` defined, except that a catch-all
template with an empty `Name` requires an ACL token that can write to any query
prefix.
When prepared queries are executed via DNS lookups or HTTP requests, the ACL
checks are run against the service being queried, similar to how ACLs work with
other service lookups. There are several ways the ACL token is selected for this
check:
2020-04-07 18:55:19 +00:00
- If an ACL Token was captured when the prepared query was defined, it will be
2019-01-09 21:07:20 +00:00
used to perform the service lookup. This allows queries to be executed by
clients with lesser or even no ACL Token, so this should be used with care.
2020-04-07 18:55:19 +00:00
- If no ACL Token was captured, then the client's ACL Token will be used to
2019-01-09 21:07:20 +00:00
perform the service lookup.
2020-04-07 18:55:19 +00:00
- If no ACL Token was captured and the client has no ACL Token, then the
2019-01-09 21:07:20 +00:00
anonymous token will be used to perform the service lookup.
In the common case, the ACL Token of the invoker is used
to test the ability to look up a service. If a `Token` was specified when the
prepared query was created, the behavior changes and now the captured
ACL Token set by the definer of the query is used when looking up a service.
Capturing ACL Tokens is analogous to
2022-08-26 05:49:29 +00:00
[PostgreSQL's](http://www.postgresql.org/docs/current/static/sql-createfunction.html)
2019-01-09 21:07:20 +00:00
`SECURITY DEFINER` attribute which can be set on functions, and using the client's ACL
Token is similar to the complementary `SECURITY INVOKER` attribute.
2021-11-03 22:16:42 +00:00
Prepared queries were originally introduced in Consul 0.6.0. The ACL behavior remained
unchanged through version 0.6.3, but versions after 0.6.3 included changes that improve management of the
2019-01-09 21:07:20 +00:00
prepared query namespace.
These differences are outlined in the table below:
2020-04-07 18:55:19 +00:00
| Operation | Version <= 0.6.3 | Version > 0.6.3 |
| ---------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| Create static query without `Name` | 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. | 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. |
| Create static query with `Name` | 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. | 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. |
| Manage static query without `Name` | The ACL Token used to create the query or a token with management privileges must be supplied in order to perform these operations. | Any client with the ID of the query can perform these operations. |
| Manage static query with a `Name` | The ACL token used to create the query or a token with management privileges must be supplied in order to perform these operations. | Similar to create, the client token's `query` ACL policy is used to determine if these operations are allowed. |
| List queries | A token with management privileges is required to list any queries. | The client token's `query` ACL policy is used to determine which queries they can see. Only tokens with management privileges can see prepared queries without `Name`. |
| Execute query | 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. | The captured token, client's token, or anonymous token is used to filter the results, as described above. |
2019-01-09 21:07:20 +00:00
2022-02-10 00:07:49 +00:00
## Service Rules
2019-01-09 21:07:20 +00:00
2023-01-25 16:52:43 +00:00
The `service` and `service_prefix` resources control service-level registration and read access to the [Catalog API](/consul/api-docs/catalog) and service discovery with the [Health API](/consul/api-docs/health).
2021-11-23 20:51:55 +00:00
Specify the resource label in service rules to set the scope of the rule.
The resource label in the following example is empty. As a result, the rules allow read-only access to any service name with the empty prefix.
2021-11-03 22:16:42 +00:00
The rules also allow read-write access to the `app` service and deny all access to the `admin` service:
2019-01-09 21:07:20 +00:00
2021-11-10 02:19:04 +00:00
<CodeTabs heading="Example service rules">
2019-01-09 21:07:20 +00:00
2019-05-01 21:11:23 +00:00
```hcl
2019-01-09 21:07:20 +00:00
service_prefix "" {
policy = "read"
}
service "app" {
policy = "write"
}
service "admin" {
policy = "deny"
}
```
2022-02-10 00:07:49 +00:00
2021-11-10 02:19:04 +00:00
```json
2022-05-08 20:44:50 +00:00
{
"service_prefix": {
"": {
"policy": "read"
}
},
"service": {
"app": {
"policy": "write"
},
"admin": {
"policy": "deny"
}
}
}
2021-11-10 02:19:04 +00:00
```
2022-02-10 00:07:49 +00:00
2021-11-10 02:19:04 +00:00
</CodeTabs>
2019-01-09 21:07:20 +00:00
Consul's DNS interface is affected by restrictions on service rules. If the
2023-01-25 16:52:43 +00:00
[`acl.tokens.default`](/consul/docs/agent/config/config-files#acl_tokens_default) used by the agent does not have `read` access to a
2019-01-09 21:07:20 +00:00
given service, then the DNS interface will return no records when queried for it.
When reading from the catalog or retrieving information from the health endpoints, service rules are
used to filter the results of the query.
2023-01-25 16:52:43 +00:00
Service rules come into play when using the [Agent API](/consul/api-docs/agent) to register services or
2019-01-09 21:07:20 +00:00
checks. The agent will check tokens locally as a service or check is registered, and Consul also
2023-01-25 16:52:43 +00:00
performs periodic [anti-entropy](/consul/docs/architecture/anti-entropy) syncs, which may require an
2019-01-09 21:07:20 +00:00
ACL token to complete. To accommodate this, Consul provides two methods of configuring ACL tokens
to use for registration events:
2023-01-25 16:52:43 +00:00
1. Using the [acl.tokens.default](/consul/docs/agent/config/config-files#acl_tokens_default) configuration
2019-01-09 21:07:20 +00:00
directive. This allows a single token to be configured globally and used
during all service and check registration operations.
2. Providing an ACL token with service and check definitions at registration
time. This allows for greater flexibility and enables the use of multiple
tokens on the same agent. Examples of what this looks like are available for
2023-02-28 22:09:56 +00:00
both [services](/consul/docs/services/usage/define-services) and
[checks](/consul/docs/services/usage/checks). Tokens may also be passed to the [HTTP
API](/consul/api-docs) for operations that require them. Note that all tokens
2019-01-09 21:07:20 +00:00
passed to an agent are persisted on local disk to allow recovery from
2023-02-28 22:09:56 +00:00
restarts. Refer to [`-data-dir` flag
documentation](/consul/docs/agent/config/cli-flags#_data_dir) for information about securing
2019-01-09 21:07:20 +00:00
access.
In addition to ACLs, in Consul 0.9.0 and later, the agent must be configured with
2023-01-25 16:52:43 +00:00
[`enable_script_checks`](/consul/docs/agent/config/config-files#enable_script_checks) or
[`enable_local_script_checks`](/consul/docs/agent/config/config-files#enable_local_script_checks)
2019-01-09 21:07:20 +00:00
set to `true` in order to enable script checks.
2022-11-18 22:39:41 +00:00
### Reading Imported Services
2023-01-25 16:52:43 +00:00
Service rules affect read access to services exported by [`exported-services` configuration entries](/consul/docs/connect/config-entries/exported-services#reading-services), including services exported between [cluster peerings](/consul/docs/connect/cluster-peering) or [admin partitions](/consul/docs/enterprise/admin-partitions) (Enterprise-only).
2022-11-18 22:39:41 +00:00
Read access to all imported services is granted when either of the following rule sets are attached to a token:
2023-11-22 17:29:43 +00:00
- `service:write` is granted to any service.
2022-11-18 22:39:41 +00:00
- `service:read` is granted to all services.
2024-05-22 22:36:57 +00:00
For Consul Enterprise, either set of rules must be scoped to the requesting service's partition and at least one namespace.
2022-11-18 22:39:41 +00:00
You may need similarly scoped [Node Rules](#reading-imported-nodes) to read Consul data, depending on the endpoint (e.g. `/v1/health/service/:name`).
2023-11-22 17:29:43 +00:00
These permissions are satisfied when using a [service identity](/consul/docs/security/acl/acl-roles#service-identities).
2022-11-18 22:39:41 +00:00
2023-01-25 16:52:43 +00:00
Refer to [Reading Services](/consul/docs/connect/config-entries/exported-services#reading-services) for example ACL policies used to read imported services using the health endpoint.
2022-11-18 22:39:41 +00:00
### Intentions
2021-07-19 22:31:41 +00:00
Service rules are also used to grant read or write access to intentions. The
following policy provides read-write access to the "app" service, and explicitly
grants `intentions:read` access to view intentions associated with the "app" service.
2021-11-10 02:19:04 +00:00
<CodeTabs heading="Example service rule with intentions">
2021-07-19 22:31:41 +00:00
```hcl
service "app" {
policy = "write"
intentions = "read"
}
```
2021-11-10 02:19:04 +00:00
```json
2022-05-08 20:44:50 +00:00
{
"service": {
"app": {
"policy": "write",
"intentions": "read"
}
}
}
2021-11-10 02:19:04 +00:00
```
2022-02-10 00:07:49 +00:00
2021-11-10 02:19:04 +00:00
</CodeTabs>
2023-03-24 22:16:06 +00:00
Refer to [ACL requirements for intentions](/consul/docs/connect/intentions/create-manage-intentions#acl-requirements)
2021-07-19 22:31:41 +00:00
for more information about managing intentions access with service rules.
2019-01-09 21:07:20 +00:00
2022-02-10 00:07:49 +00:00
## Session Rules
2019-01-09 21:07:20 +00:00
2023-01-25 16:52:43 +00:00
The `session` and `session_prefix` resources controls access to [Session API](/consul/api-docs/session) operations.
2019-01-09 21:07:20 +00:00
2021-11-23 20:51:55 +00:00
Specify the resource label in session rules to set the scope of the rule.
The resource label in the following example is empty. As a result, the rules allow read-only access to all sessions.
2021-11-03 22:16:42 +00:00
The rules also allow creating sessions on the node named `app` and deny all access to any sessions on the `admin` node:
2019-01-09 21:07:20 +00:00
2021-11-10 02:19:04 +00:00
<CodeTabs heading="Example session rules">
2019-01-09 21:07:20 +00:00
2019-05-01 21:11:23 +00:00
```hcl
2019-01-09 21:07:20 +00:00
session_prefix "" {
policy = "read"
}
session "app" {
policy = "write"
}
session "admin" {
policy = "deny"
}
```
2021-11-10 02:19:04 +00:00
```json
2022-05-08 20:44:50 +00:00
{
"session_prefix": {
"": {
"policy": "read"
}
},
"session": {
"app": {
"policy": "write"
},
"admin": {
"policy": "deny"
}
}
}
2021-11-10 02:19:04 +00:00
```
2021-11-16 23:14:48 +00:00
</CodeTabs>