Skip to content
Tutorials Center
My learning

Topics

Lesson 35 of 37 ยท Projects and Practice

Project: Build an ACL Rule Parser and Checker

Write a Python parser for Consul-style ACL rules and an authorizer that applies exact-over-prefix and longest-prefix matching, then lint policies for risky grants.

Consul's ACL rules look like a small language: a resource type, a name and an access level. In this project you write a parser for a simplified version of the rule syntax and an authorizer that answers "may this token do X to Y?", using the same precedence rules you learned: exact beats prefix, a longer prefix beats a shorter one, and deny is strongest across policies. Finally you add a linter.

This is a stand-in written for learning, not Consul's real parser (which supports full HCL and more resources). It runs in your browser.

Step 1: parse rules

We accept lines like these:

TEXT
service "web" { policy = "write" }
key_prefix "config/" { policy = "read" }
operator = "read"

Each line becomes a tuple (resource, segment, level). Single-valued resources like operator have an empty segment.

Python Step 1: the parser

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.

Two typos are caught: an unknown resource name would not be caught by this simple regex (servic matches the generic block pattern), but the invalid level banana is. Task for you at the end: validate resource names against a whitelist.

Step 2: the authorizer

Resolution works in three stages:

  1. Collect every rule whose resource family matches the request (service and service_prefix for a service lookup).
  2. Among them, pick the most specific: an exact rule beats any prefix rule; among prefixes, the longest wins.
  3. If nothing matches, the default policy decides.
Python Step 2: exact and longest-prefix matching

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.

Check each result against the rules to confirm it matches what Consul would do: api write is denied (only read from the empty prefix), and internal-db is denied despite the broad read, because a longer prefix rule is more specific.

Step 3: merge several policies

A token holds many policies. Rules from all of them are pooled together. The pooled set uses the same specificity ordering. When two rules have identical rank, the stronger deny wins and otherwise the higher access level.

Python Step 3: pooling policies for a token

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.

The first pool takes the higher level for the same rule (web write, config write); adding the blocker turns the config prefix into deny, even though other policies granted more.

Step 4: a linter

Finally, add automated review. These checks catch real-world mistakes:

Python Step 4: policy linter

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.

Extend the project

  1. Whitelist resources: reject unknown names such as servic.
  2. Identities: implement service_identity_rules("web") so tokens can include identities, and test that the generated rules allow what a sidecar needs.
  3. Explain mode: return which rule decided each request, like Consul's debugging output would, for support.
  4. Diff: compare two policies and print the access that changed.
  5. CI: wire the linter into a pipeline and fail on CRITICAL findings.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which rule decides service api write with rules service_prefix "" read and service "web" write?

    api matches only the empty prefix, which grants read, so write is denied.
  2. 2. How are an exact rule and a prefix rule ranked?

    Exact matches are more specific than any prefix.
  3. 3. What happens when two policies give the same resource different levels?

    Permissions are additive except for deny, which overrides.
  4. 4. Why lint for service_prefix "" write?

    Write on the empty prefix covers every service name, which is rarely intended.

Summary

  • A rule is (resource, segment, level); a tiny regex parser turns policy text into data and reports bad lines.
  • The authorizer collects matching rules, ranks them by specificity (exact over prefix, longer over shorter) and falls back to the default policy.
  • Pooling policies adds access except for deny; a linter catches dangerous grants before they ship.

Next: Project: An Intention Evaluator and Mesh Planner