Skip to content

Commit

Permalink
More templated policies docs (#19312)
Browse files Browse the repository at this point in the history
[NET-5327]More templated policies docs
  • Loading branch information
roncodingenthusiast authored Oct 23, 2023
1 parent 46804c0 commit fea35e6
Show file tree
Hide file tree
Showing 3 changed files with 159 additions and 51 deletions.
117 changes: 108 additions & 9 deletions website/content/docs/security/acl/acl-roles.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ They enable you to reuse policies by decoupling the policies from the token dist
Instead, the token is linked to the role, which is able to hold several policies that can be updated asynchronously without distributing new tokens to users.
As a result, roles can provide a more convenient authentication infrastructure than creating unique policies and tokens for each requester.

## Workflow Overview
## Workflow overview

Roles are configurations linking several policies to a token. The following procedure describes the workflow for implementing roles.

Expand All @@ -21,19 +21,20 @@ Roles are configurations linking several policies to a token. The following proc
1. Register the role in Consul and link it to a token.
1. Distribute the tokens to users for implementation.

## Creating Roles
## Creating roles

Creating roles is commonly the responsibility of the Consul ACLs administrator.
Roles have several attributes, including service identities and node identities.
Refer to the following documentation for details:

- [Role Attributes](#role-attributes)
- [Templated Policies](#templated-policies)
- [Service Identities](#service-identities)
- [Node Identities](#node-identities)

Use the Consul command line or API endpoint to create roles.

### Command Line
### Command line

Issue the `consul acl role create` command to create roles. In the following example, a role named `crawler` is created that contains a policy named `crawler-kv` and a policy named `crawler-key`.

Expand All @@ -53,28 +54,126 @@ $ curl --request PUT --data @payload.json http://127.0.0.1:8500/v1/acl/role

Refer to the [API documentation](/consul/api-docs/acl/roles) for details.

## Role Attributes
## Role attributes

Roles may contain the following attributes:

- `ID`: The `ID` is an auto-generated public identifier. You can specify the role `ID` when linking it to tokens.
- `Name`: A unique meaningful name for the role. You can specify the role `Name` when linking it to tokens.
- `Description`: (Optional) A human-readable description of the role.
- `Policies`: Specifies a the list of policies that are applicable for the role. The object can reference the policy `ID` or `Name` attribute.
- `TemplatedPolicies`: Specifies a list of templated polcicies that are applicable for the role. See [Templated Policies](#templated-policies) for details.
- `ServiceIdentities`: Specifies a list of services that are applicable for the role. See [Service Identities](#service-identities) for details.
- `NodeIdentities`: Specifies a list of nodes that are applicable for the role. See [Node Identities](#node-identities) for details.
- `Namespace`: <EnterpriseAlert inline /> The namespace that the policy resides in. Roles can only be linked to policies that are defined in the same namespace. See [Namespaces](/consul/docs/enterprise/namespaces) for additional information. Requires Consul Enterprise 1.7.0+
- `Partition`: <EnterpriseAlert inline/> The admin partition that the policy resides in. Roles can only be linked to policies that are defined in the same admin partition. See [Admin Partitions](/consul/docs/enterprise/admin-partitions) for additional information. Requires Consul Enterprise 1.10.0+.

## Service Identities
## Templated policies

You can specify a templated policy when configuring roles or linking tokens to policies. Templated policies enable you to quickly construct policies for common Consul use cases, rather than creating identical policies for each use cases.

Consul uses templated policies during the authorization process to automatically generate a policy for the use case specified. Consul links the generated policy to the role or token so that it will have permission for the specific use case.

### Templated policy specification

The following templated policy example configuration uses the `builtin/service` templated policy to give a service named `api` and its sidecar proxy the required permissions to register into the catalog.

```json
{
"TemplatedPolicies": [
{
"TemplateName": "builtin/service",
"TemplateVariables": {
"Name": "api"
}
}
]
}
```

- `TemplatedPolicies`: Declares a templated policy block.
- `TemplatedPolicies.TemplateName`: String value that specifies the name of the templated policy you want to use.
- `TemplatedPolicies.TemplateVariables`: Map that specifies the required variables for the templated policies. This field is optional as not all templated policies require variables.

Refer to the [API documentation for roles](/consul/api-docs/acl/roles#sample-payload) for additional information and examples.

-> In Consul Enterprise, templated policies inherit the namespace or admin partition scope of the corresponding ACL token or role.

<!-- -> Added in Consul 1.5.0 # Remove and lean on versioning?-->
The `builtin/service` sample templated policy generates the following policy for a service named `api`:

```hcl
# Allow the service and its sidecar proxy to register into the catalog.
service "api" {
policy = "write"
}
service "api-sidecar-proxy" {
policy = "write"
}
# Allow for any potential upstreams to be resolved.
service_prefix "" {
policy = "read"
}
node_prefix "" {
policy = "read"
}
```

Refer to the [rules reference](/consul/docs/security/acl/acl-rules) for information about the rules in the policy.

### Example

The following role configuration contains a templated policy that gives the role required permission for a service named `web` to register itself and its sidecar into the catalog.

<CodeBlockConfig filename="example-role.json">

```json
{
"Name": "example-role",
"Description": "Showcases all input parameters",
"TemplatedPolicies": [
{
"TemplateName": "builtin/service",
"TemplateVariables": {
"Name": "web"
}
}
]
}
```

</CodeBlockConfig>

During the authorization process, Consul generates the following policies for the `web` services and links it to the token:

<CodeBlockConfig filename="web-policy.hcl">

```hcl
# Allow the service and its sidecar proxy to register into the catalog.
service "web" {
policy = "write"
}
service "web-sidecar-proxy" {
policy = "write"
}
# Allow for any potential upstreams to be resolved.
service_prefix "" {
policy = "read"
}
node_prefix "" {
policy = "read"
}
```
</CodeBlockConfig>

## Service Identities

You can specify a service identity when configuring roles or linking tokens to policies. Service identities enable you to quickly construct policies for services, rather than creating identical polices for each service.

Service identities are used during the authorization process to automatically generate a policy for the service(s) specified. The policy will be linked to the role or token so that the service(s) can _be discovered_ and _discover other healthy service instances_ in a service mesh. Refer to the [service mesh](/consul/docs/connect) topic for additional information about Consul service mesh.

### Service Identity Specification
### Service identity specification

Use the following syntax to define a service identity:

Expand Down Expand Up @@ -211,7 +310,7 @@ You can specify a node identity when configuring roles or linking tokens to poli

Node identities enable you to quickly construct policies for nodes, rather than manually creating identical polices for each node. They are used during the authorization process to automatically generate a policy for the node(s) specified. You can specify the token linked to the policy in the [`acl_tokens_agent`](/consul/docs/agent/config/config-files#acl_tokens_agent) field when configuring the agent.

### Node Identity Specification
### Node identity specification

Use the following syntax to define a node identity:

Expand Down Expand Up @@ -299,4 +398,4 @@ service_prefix "" {
}
```

</CodeBlockConfig>
</CodeBlockConfig>
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ This topic describes how to create a token that you can use to register a servic

Services must present a token linked to policies that grant the appropriate set of permissions in order to be discoverable or to interact with other services in a mesh.

### Service identities versus custom policies
### Templated policies versus custom policies

You can create tokens linked to custom policies or to service identities. [Service identities](/consul/docs/security/acl#service-identities) are constructs in Consul that enable you to quickly grant permissions for a group of services, rather than creating similar policies for each service.
You can create tokens linked to custom policies or to templated policies. [Templated policies](/consul/docs/security/acl#templated-policies) are constructs in Consul that enable you to quickly grant permissions for common use cases, rather than creating similar policies.

We recommend using a service identity to grant permissions for service discovery and service mesh use cases rather than creating a custom policy. This is because service identities automatically grant the service and its sidecar proxy `service:write`, `service:read`, and `node:read`.
We recommend using the `builtin/service` templated policy to grant permissions for service discovery and service mesh use cases rather than creating a custom policy. This is because the `builtin/service` templated policy automatically grant the service and its sidecar proxy `service:write`, `service:read`, and `node:read`.

Your organization may have requirements or processes for deploying services in a way that is inconsistent with service and node identities. In these cases, you can create custom policies and link them to tokens.
Your organization may have requirements or processes for deploying services in a way that is inconsistent with the `builtin/service` templated policy. In these cases, you can create custom policies and link them to tokens.

## Requirements

Expand All @@ -33,41 +33,45 @@ The service token must be linked to policies that grant the following permission

@include 'create-token-requirements.mdx'

## Service identity in Consul CE
## Templated policies in Consul CE

Refer to [Service identities](/consul/docs/security/acl#service-identities) for information about creating service identities that you can link to tokens.
Refer to [Templated policies](/consul/docs/security/acl#templated-policies) for information about creating templated policies that you can link to tokens.

You can manually create tokens using the Consul command line or API endpoint. You can also enable Consul to dynamically create tokens from trusted external systems using an [auth method](/consul/docs/security/acl/auth-methods).

<Tabs>

<Tab heading="CLI" group="CLI">

Run the `consul acl token create` command and specify the policy or service identity to link to create a token. Refer to [Consul ACL Token Create](/consul/commands/acl/token/create) for details about the `consul acl token create` command.
Run the `consul acl token create` command and specify a templated policy to link to create a token. Refer to [Consul ACL Token Create](/consul/commands/acl/token/create) for details about the `consul acl token create` command.

The following example creates an ACL token linked to a service identity for a service named `svc1`.
The following example creates an ACL token linked to the `builtin/service` templated policy for a service named `svc1`.

```shell-session
$ consul acl token create \
-description "Service token for svc1" \
-service-identity "svc1"
-templated-policy "builtin/service" \
-var "name:api"
```

</Tab>

<Tab heading="API" group="API">

Send a PUT request to the `/acl/token` endpoint and specify a service identity in the request body to create a token linked to the service identity. An ACL token linked to a policy with permissions to use the API endpoint is required. Refer to [ACL Token HTTP API](/consul/api-docs/acl/tokens) for additional information about using the API endpoint.
Send a PUT request to the `/acl/token` endpoint and specify a templated policy in the request body to create a token linked to the templated policy. An ACL token linked to a policy with permissions to use the API endpoint is required. Refer to [ACL Token HTTP API](/consul/api-docs/acl/tokens) for additional information about using the API endpoint.

The following example creates a token linked to a service identity named `svc1`:
The following example creates a token linked to the `builtin/service` templated policy for a service named `svc1`:

```shell-session
$ curl --request PUT http://127.0.0.1:8500/v1/acl/token \
--header "X-Consul-Token: $CONSUL_HTTP_TOKEN" \
--data '{
"ServiceIdentities": [
"TemplatedPolicies": [
{
"ServiceName": "svc1"
"TemplateName": "builtin/service",
"TemplateVariables": {
"Name": "svc1"
}
}
]
}'
Expand All @@ -77,41 +81,45 @@ $ curl --request PUT http://127.0.0.1:8500/v1/acl/token \

</Tabs>

## Service identity in Consul Enterprise <EnterpriseAlert inline />
## Templated policies in Consul Enterprise <EnterpriseAlert inline />

Refer to [Service identities](/consul/docs/security/acl#service-identities) for information about creating service identities that you can link to tokens.
Refer to [Templated policies](/consul/docs/security/acl#templated-policies) for information about the use of templated policies in tokens and roles.

You can manually create tokens using the Consul command line or API endpoint. You can also enable Consul to dynamically create tokens from trusted external systems using an [auth method](/consul/docs/security/acl/auth-methods).

<Tabs>

<Tab heading="CLI" group="CLI">

Run the `consul acl token create` command and specify the policy or service identity to link to create a token. Refer to [Consul ACL Token Create](/consul/commands/acl/token/create) for details about the `consul acl token create` command.
Run the `consul acl token create` command and specify the policy or templated policy to link to create a token. Refer to [Consul ACL Token Create](/consul/commands/acl/token/create) for details about the `consul acl token create` command.

You can specify an admin partition, namespace, or both when creating tokens in Consul Enterprise. The token can only include permissions in the specified scope, if any. The following example creates an ACL token that the service can use to register in the `ns1` namespace of partition `ptn1`:

```shell-session
$ consul acl token create -partition "ptn1" -namespace "ns1" \
-description "Service token for svc1" \
-service-identity "svc1"
-templated-policy "builtin/service"
-var "name:svc1"
```

</Tab>

<Tab heading="API" group="API">

Send a PUT request to the `/acl/token` endpoint and specify a service identity in the request body to create a token linked to the service identity. An ACL token linked to a policy with permissions to use the API endpoint is required. Refer to [ACL Token HTTP API](/consul/api-docs/acl/tokens) for additional information about using the API endpoint.
Send a PUT request to the `/acl/token` endpoint and specify a policy or templated policy in the request body to create a token linked to the templated policy. An ACL token linked to a policy with permissions to use the API endpoint is required. Refer to [ACL Token HTTP API](/consul/api-docs/acl/tokens) for additional information about using the API endpoint.

You can specify an admin partition and namespace when creating tokens in Consul Enterprise. The token is only valid in the specified scopes. The following example creates an ACL token that the service can use to register in the `ns1` namespace of partition `ptn1`:

```shell-session
$ curl --request PUT http://127.0.0.1:8500/v1/acl/token \
--header "X-Consul-Token: $CONSUL_HTTP_TOKEN" \
--data '{
"ServiceIdentities": [
"TemplatedPolicies": [
{
"ServiceName": "svc1"
"TemplateName": "builtin/service",
"TemplateVariables": {
"Name": "svc1"
}
}
],
"Namespace": "ns1",
Expand All @@ -125,7 +133,7 @@ $ curl --request PUT http://127.0.0.1:8500/v1/acl/token \

## Custom policy in Consul CE

When you are unable to link tokens to a service identity, you can define policies, register them with Consul, and link the policies to tokens that enable services to register into the Consul catalog.
When you are unable to link tokens to a templated policy, you can define policies, register them with Consul, and link the policies to tokens that enable services to register into the Consul catalog.

### Define a policy

Expand Down Expand Up @@ -261,7 +269,7 @@ $ curl --request PUT http://127.0.0.1:8500/v1/acl/token \

## Custom policy in Consul Enterprise <EnterpriseAlert inline/>

When you are unable to link tokens to a service identity, you can define policies, register them with Consul, and link the policies to tokens that enable services to register into the Consul catalog.
When you are unable to link tokens to a templated policy, you can define policies, register them with Consul, and link the policies to tokens that enable services to register into the Consul catalog.

### Define a policy

Expand Down
Loading

0 comments on commit fea35e6

Please sign in to comment.