Skip to content
Tutorials Center
My learning

Topics

Lesson 22 of 37 ยท Service Mesh

Consul Intentions: Controlling Service Access

Control which services may talk to each other with Consul intentions: allow and deny rules, wildcards, precedence, default policy and L7 permissions.

Mutual TLS tells a sidecar who is calling. Intentions decide whether that caller is allowed. Without them, a mesh would encrypt everything yet still let any service reach any other. Intentions turn the mesh into a zero-trust network: you list the connections you want, and the rest are refused.

Consul and Envoy cannot run on this site, so the configuration is read-only. The runnable examples are Python stand-ins that implement the same precedence rules, so you can experiment with how allow and deny rules combine.

What an intention says

An intention has a source (the caller), a destination (the called service) and an action, allow or deny. The receiving sidecar enforces it by comparing the caller's certificate identity with the stored rules.

Current Consul manages intentions as a config entry of kind service-intentions, written for the destination:

HCL
Kind = "service-intentions"
Name = "api"

Sources = [
  {
    Name   = "web"
    Action = "allow"
  },
  {
    Name   = "batch"
    Action = "deny"
  }
]

Apply it with consul config write api-intentions.hcl. The older CLI is still available for quick changes:

Terminal
consul intention create -allow web api
consul intention create -deny batch api
consul intention list
consul intention check web api
consul intention match -destination api

consul intention check web api answers the practical question "would this connection be allowed?".

The default policy

If no intention matches a connection, the default applies. Two settings influence it:

  • With ACLs disabled, the default is allow all.
  • With ACLs enabled and acl.default_policy = "deny", the default is deny all.
  • Newer releases let you set it explicitly with default_intention_policy under acl (values allow or deny), or by a wildcard intention.

The safest production setup is default deny plus one explicit allow per real connection. A common bootstrap trick is a single wildcard intention:

HCL
Kind = "service-intentions"
Name = "*"

Sources = [
  { Name = "*", Action = "deny" }
]

Because it is the least specific rule, every other, more precise intention overrides it.

Precedence: the most specific rule wins

Several intentions can match one connection, for example * -> api (deny) and web -> api (allow). Consul orders them by precedence. A rule that names things exactly beats one that uses a wildcard, and the destination counts for more than the source. In open-source Consul the order is:

Rank Source Destination
1 (highest) exact exact
2 wildcard * exact
3 exact wildcard
4 (lowest) wildcard wildcard

Enterprise adds namespaces and admin partitions, which extend the table, but the principle is the same. The highest-precedence matching rule decides.

Python Stand-in: intention evaluator with precedence

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Notice batch -> metrics: both * -> metrics and batch -> * match. The rule with the exact destination ranks higher, so batch is allowed to reach metrics even though it is denied everywhere else. Always test overlapping wildcards before relying on them.

Layer 7 intentions

Plain intentions work at the connection level: the whole source is either allowed or not. If the destination's protocol is http or grpc (set via service-defaults), an intention can instead list permissions that look inside the request:

HCL
Kind = "service-intentions"
Name = "api"

Sources = [
  {
    Name = "web"
    Permissions = [
      {
        Action = "allow"
        HTTP {
          PathPrefix = "/v1/products"
          Methods    = ["GET"]
        }
      },
      {
        Action = "deny"
        HTTP {
          PathPrefix = "/"
        }
      }
    ]
  }
]

Permissions are evaluated in order and the first match wins, so the specific allow comes before the catch-all deny. An action (Action) and Permissions cannot be mixed in the same source. With L7 rules web may only GET product paths on api; everything else returns 403. These rules require a protocol other than tcp; for tcp services only whole-connection allow or deny is possible.

Python Stand-in: first-match L7 permissions

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Practical advice

  • Write intentions for the destination. The owner of api decides who may call it. Keep each service's entry in its own repository or Terraform module.
  • Start permissive in development, strict in production. Use consul intention check in CI to assert important connections.
  • Watch ACLs. Writing intentions needs service:write (or intentions rights) on the destination, otherwise teams could grant themselves access.
  • Gateways need intentions too. Ingress and terminating gateways are services with identities of their own.
  • Do not confuse intentions with firewalls. They apply only to traffic that goes through sidecars. Direct connections to an unprotected port are not covered.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What does an intention decide?

    An intention is an allow or deny rule between a source and a destination service identity.
  2. 2. Two intentions match a connection: * -> api (deny) and web -> api (allow). Which applies?

    Precedence prefers exact names over wildcards, so the web -> api allow wins.
  3. 3. Where does the config entry for web calling api belong?

    Service-intentions entries are named for the destination and list the sources in Sources.
  4. 4. What is required before an intention can match on an HTTP path?

    Only layer 7 protocols let the proxy inspect requests; tcp services get connection-level rules.
  5. 5. In an L7 intention, why does the specific allow come before the catch-all deny?

    Put specific permissions first; a catch-all placed first would block everything.

Summary

  • Intentions are allow/deny rules between services, enforced by the destination's sidecar using certificate identity.
  • Manage them as service-intentions config entries named for the destination; test with consul intention check.
  • The default policy applies when nothing matches; use default deny in production.
  • More specific rules beat wildcards; L7 permissions need an http or grpc protocol and are evaluated in order.

Next: Traffic Routing, Splitting and Resolution