Skip to content
Tutorials Center
My learning

Topics

Lesson 27 of 37 ยท Wireless and Zero Trust

Building a Zero Trust Policy Engine

Build a small zero trust policy decision point in Python: score identity, device posture and risk signals, apply least-privilege rules and see why each request is allowed or denied.

The zero trust principles can sound abstract until you write the decision logic yourself. A policy engine takes a request (who, from which device, to what resource, in what context) and returns a decision (allow, deny, or allow with a condition such as "step up to MFA"), plus a reason that humans can audit.

In this lesson you will build one in Python in three steps: a simple rules engine, a risk-scoring layer, and a micro-segmentation check. It is a stand-in for products such as ZTNA gateways or cloud conditional-access services; it demonstrates the logic, not a real enforcement point.

The shape of a request

A request carries attributes from several sources:

Source Attributes
Identity provider user, role, group, whether MFA was done, how long ago
Device posture managed or not, OS patched, disk encrypted, endpoint agent healthy
Network and context country, time of day, new location, impossible-travel flag
Resource name, sensitivity (low, medium, high)

Decisions combine these. A pattern that works well is deny by default: nothing is allowed unless a rule says so, and a more specific deny always wins over a general allow.

Step 1: rules with a default deny

Python Attribute-based allow rules

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 request 5: the resource backup has no rule, so it is denied even for a DBA with every attribute green. That is default deny at work. It forces an owner to write an explicit rule before anything becomes reachable.

Step 2: add risk scoring and step-up

Hard rules are too blunt for context. A user with MFA who suddenly logs in from a new country at 03:00 on an unhealthy device should be treated differently from the same user at their desk. The engine below adds a risk score and three outcomes: allow, step-up (ask for stronger authentication), or deny.

Python Risk score with step-up authentication

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 same user is treated differently as conditions change, and every decision comes with a reason string. Reasons matter: in a real deployment they feed audit logs, help the help-desk explain a denial and let you debug policy.

Real engines (conditional access, ZTNA brokers) use more signals and often continuous re-evaluation. Scores and thresholds in this lesson are invented for teaching; tune yours against your own data and start in report-only mode.

Step 3: micro-segmentation as a flow allow-list

Zero trust also constrains machine to machine traffic. A micro-segmentation policy lists the exact flows that are permitted; everything else, especially lateral movement between servers, is denied. Here the policy is a table of (source group, destination group, port).

Python Allowed flows between workload groups

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.

Three flows are denied. web -> db skips the application tier, app -> internet has no egress rule (default deny), and db -> web:22 is the sort of lateral movement an attacker who owns the database would try. In practice teams first run these policies in monitor mode using flow logs from the cloud or host firewalls to learn what exists, then switch to enforcement. This is the safe path described in Zero Trust Principles.

Design lessons

  • Default deny, explicit allow. Absence of a rule means no access.
  • Specific beats general. Deny rules override allow rules.
  • Separate decision and enforcement. The engine decides; gateways and agents enforce.
  • Return reasons. Auditable decisions build trust in the system.
  • Re-evaluate. A session that was fine at 09:00 may not be at 09:30 after a device alert.
  • Fail safely. Decide what happens when the engine is unreachable (usually deny for sensitive resources, with a break-glass process).

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. A request matches no rule in a default-deny engine. What happens?

    Default deny means anything not explicitly allowed is refused.
  2. 2. What does a 'step-up' decision mean?

    Step-up asks the user to prove identity again (for example MFA) when risk is elevated.
  3. 3. Why is a web server connecting directly to the database a micro-segmentation violation in the example?

    The allow-list expresses the intended architecture; direct web-to-db skips the application layer and widens attack paths.
  4. 4. Why should a policy engine return a reason with each decision?

    Reasons make decisions reviewable by auditors and understandable to users and administrators.

Summary

  • A policy engine maps request attributes (identity, device, context, resource) to allow, step-up or deny, with a reason.
  • Default deny forces every access path to be explicitly approved.
  • Risk scores let context change the outcome; sensitive resources tolerate less risk.
  • Micro-segmentation applies the same allow-list idea to workload-to-workload flows and blocks lateral movement.
  • Start in monitor mode, log reasons, and re-evaluate continuously.

Next: The TLS Handshake and Certificates