2017-09-19 14:02:53 +00:00
---
2020-04-07 18:55:19 +00:00
layout: docs
page_title: Sentinel in Consul
2020-04-13 18:40:26 +00:00
sidebar_title: Sentinel
2020-04-07 18:55:19 +00:00
description: >-
Consul Enterprise uses Sentinel to augment the built-in ACL system to provide
advanced policy enforcement. Sentinel policies can currently execute on KV
modify and service registration.
2017-09-19 14:02:53 +00:00
---
# Sentinel Overview
2020-04-07 18:55:19 +00:00
2020-04-23 22:13:18 +00:00
<EnterpriseAlert />
2017-09-19 14:02:53 +00:00
2020-04-07 18:55:19 +00:00
Consul 1.0 adds integration with [Sentinel](https://hashicorp.com/sentinel) for policy enforcement.
Sentinel policies help extend the ACL system in Consul beyond the static "read", "write", and "deny"
policies to support full conditional logic and integration with external systems.
2017-09-19 14:02:53 +00:00
## Sentinel in Consul
2017-09-29 02:00:00 +00:00
Sentinel policies are applied during writes to the KV Store.
2020-04-09 23:46:54 +00:00
An optional `sentinel` field specifying code and enforcement level can be added to [ACL policy definitions](/docs/agent/acl-rules#sentinel-integration) for Consul KV. The following policy ensures that the value written during a KV update must end with "dc1".
2017-09-19 14:02:53 +00:00
```text
2019-03-13 17:47:25 +00:00
key "datacenter_name" {
policy = "write"
2017-09-19 14:02:53 +00:00
sentinel {
2018-11-08 22:28:40 +00:00
code = <<EOF
import "strings"
2019-03-13 17:47:25 +00:00
main = rule { strings.has_suffix(value, "dc1") }
2018-11-08 22:28:40 +00:00
EOF
2019-03-13 17:47:25 +00:00
enforcementlevel = "soft-mandatory"
2017-09-19 14:02:53 +00:00
}
2019-03-13 17:47:25 +00:00
}
2017-09-19 14:02:53 +00:00
```
If the `enforcementlevel` property is not set, it defaults to "hard-mandatory".
## Imports
2020-02-03 10:44:25 +00:00
Consul imports all the [standard imports](https://docs.hashicorp.com/sentinel/imports/) from Sentinel _except_ [`http`](https://docs.hashicorp.com/sentinel/imports/http/). All functions in these imports are available to be used in policies.
2017-09-19 14:02:53 +00:00
## Injected Variables
Consul passes some context as variables into Sentinel, which are available to use inside any policies you write.
#### Variables injected during KV store writes
2020-04-09 23:46:54 +00:00
| Variable Name | Type | Description |
| ------------- | -------- | ---------------------- |
| `key` | `string` | Key being written |
| `value` | `string` | Value being written |
| `flags` | `uint64` | [Flags](/api/kv#flags) |
2017-09-19 14:02:53 +00:00
2019-03-13 17:47:25 +00:00
## Sentinel Examples
2017-09-19 14:02:53 +00:00
2019-03-13 17:47:25 +00:00
The following are two examples of ACL policies with Sentinel rules.
2020-04-07 18:55:19 +00:00
### Required Key Suffix
2019-03-13 17:47:25 +00:00
Any values stored under the key prefix "dc1" must end with "dev"
2017-09-19 14:02:53 +00:00
```text
2019-03-13 17:47:25 +00:00
key "dc1" {
2017-10-13 19:15:08 +00:00
policy = "write"
sentinel {
2018-11-08 22:28:40 +00:00
code = <<EOF
import "strings"
2019-03-13 17:47:25 +00:00
main = rule { strings.has_suffix(value, "dev") }
2018-11-08 22:28:40 +00:00
EOF
2017-09-19 14:02:53 +00:00
}
}
```
2019-06-24 21:25:58 +00:00
### Restricted Update Time
2019-03-13 17:47:25 +00:00
The key "haproxy_version" can only be updated during business hours.
2017-09-19 14:02:53 +00:00
```text
2019-03-13 17:47:25 +00:00
key "haproxy_version" {
2017-10-13 19:15:08 +00:00
policy = "write"
sentinel {
2018-11-08 22:28:40 +00:00
code = <<EOF
import "time"
2019-06-06 20:31:54 +00:00
main = rule { time.now.hour > 8 and time.now.hour < 17 }
2018-11-08 22:28:40 +00:00
EOF
2017-09-19 14:02:53 +00:00
}
}
```