Sentinel for KV ACL Policy Enforcement

EnterpriseSentinel - 图1Enterprise

This feature requires Consul Enterprise(opens in new tab).

Consul 1.0 adds integration with 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.

Sentinel in Consul

Sentinel policies are applied during writes to the KV Store.

An optional sentinel field specifying code and enforcement level can be added to ACL policy definitions for Consul KV. The following policy ensures that the value written during a KV update must end with “dc1”.

Ensure values written during KV updates end in ‘dc1’

  1. key "datacenter_name" {
  2. policy = "write"
  3. sentinel {
  4. code = <<EOF
  5. import "strings"
  6. main = rule { strings.has_suffix(value, "dc1") }
  7. EOF
  8. enforcementlevel = "soft-mandatory"
  9. }
  10. }

If the enforcementlevel property is not set, it defaults to “hard-mandatory”.

Imports

Consul imports all the standard imports from Sentinel except http. All functions in these imports are available to be used in policies.

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

Variable NameTypeDescription
keystringKey being written
valuestringValue being written
flagsuint64Flags

Sentinel Examples

The following are two examples of ACL policies with Sentinel rules.

Required Key Suffix

Any values stored under the key ‘dc1’ end with ‘dev’

  1. key "dc1" {
  2. policy = "write"
  3. sentinel {
  4. code = <<EOF
  5. import "strings"
  6. main = rule { strings.has_suffix(value, "dev") }
  7. EOF
  8. }
  9. }

Restricted Update Time

The key ‘haproxy_version’ can only be updated during business hours

  1. key "haproxy_version" {
  2. policy = "write"
  3. sentinel {
  4. code = <<EOF
  5. import "time"
  6. main = rule { time.now.hour > 8 and time.now.hour < 17 }
  7. EOF
  8. }
  9. }