Audit device access-control policy
Evaluate role and zone rules against device requests, expose an over-broad grant and test a least-privilege correction.

Shield Shelly
The Authentication guide leads learners through access-control models, identity management and authorization.
Predict the reading, then compare it with the measurement.
Python 3 in your browser (JupyterLite)
Python · no installOpen the notebook in your browser and run each Python cell; no install or account is needed.
Three ways to run: use JupyterLite here with no install; run main.py locally from the downloadable lab folder; or open the same notebook in Google Colab.
Steps
Step 1
- Do
- Run the first notebook cell in the editor and inspect the synthetic policy table and request count.
- You will see
Run the cell. You should see:
- Synthetic classroom policy and requests; no live identity service
- P1: admin * * -> allow
- P2: technician plant-a read -> allow
- P3: technician plant-a configure -> allow
- P4: viewer * read -> allow
- requests=7; unmatched requests default to deny
- STEP 1 policy fixture fixed
- Why it matters
- The small table separates role, zone and action so each grant can be reviewed.
Code for step 1#!/usr/bin/env python3 """Synthetic RBAC/ABAC policy audit with first matching rule and default deny.""" rules = [ ("P1", "admin", "*", "*", "allow"), ("P2", "technician", "plant-a", "read", "allow"), ("P3", "technician", "plant-a", "configure", "allow"), ("P4", "viewer", "*", "read", "allow"), # intentionally broad ] requests = [ ("R1", "admin", "plant-b", "configure"), ("R2", "technician", "plant-a", "read"), ("R3", "technician", "plant-b", "configure"), ("R4", "viewer", "plant-a", "read"), ("R5", "viewer", "plant-b", "read"), ("R6", "viewer", "plant-a", "configure"), ("R7", "guest", "plant-a", "read"), ] print("Synthetic classroom policy and requests; no live identity service") for rid, role, zone, action, effect in rules: print(f"{rid}: {role:10} {zone:7} {action:9} -> {effect}") print(f"requests={len(requests)}; unmatched requests default to deny") print("STEP 1 policy fixture fixed")
allow" loading="lazy">Step 1 · Python 3 in your browser (JupyterLite); numbered callout added to a real capture. Step 2
- Do
- Run the second cell in the editor and inspect each decision with its matching rule in the output table.
- You will see
Run the cell. You should see:
- request role zone action decision matching-rule
- R1 admin plant-b configure allow P1
- R2 technician plant-a read allow P2
- R3 technician plant-b configure deny default-deny
- R4 viewer plant-a read allow P4
- R5 viewer plant-b read allow P4
- R6 viewer plant-a configure deny default-deny
- R7 guest plant-a read deny default-deny
- STEP 2 first-match decisions evaluated
- Why it matters
- A matching-rule column makes default deny and explicit grants distinguishable.
Code for step 2def evaluate(request, policy): rid, role, zone, action = request for rule_id, need_role, need_zone, need_action, effect in policy: if (need_role in (role, "*") and need_zone in (zone, "*") and need_action in (action, "*")): return effect, rule_id return "deny", "default-deny" print("request role zone action decision matching-rule") for request in requests: decision, rule = evaluate(request, rules) print(f"{request[0]:7} {request[1]:10} {request[2]:7} {request[3]:9} {decision:8} {rule}") print("STEP 2 first-match decisions evaluated")
Step 2 · Python 3 in your browser (JupyterLite); numbered callout added to a real capture. Step 3
- Do
- Run the third cell in the editor and inspect the cross-zone viewer request in the audit readout.
- You will see
Run the cell. You should see:
- Audit request=R5 role=viewer zone=plant-b action=read
- Decision=allow; matching rule=P4
- Expected least-privilege boundary: viewer reads only plant-a
- Finding: P4 wildcard zone grants plant-b read to every viewer
- P1 admin wildcard is intentional in this classroom policy
- STEP 3 over-broad rule identified
- Why it matters
- A wildcard zone silently crosses the intended viewer boundary.
Code for step 3cross_zone = requests[4] decision, match = evaluate(cross_zone, rules) print(f"Audit request={cross_zone[0]} role={cross_zone[1]} zone={cross_zone[2]} action={cross_zone[3]}") print(f"Observed decision={decision}; matching rule={match}") print("Expected least-privilege boundary: viewer reads only plant-a") print("Finding: P4 wildcard zone grants plant-b read to every viewer") print("P1 admin wildcard is intentional in this classroom policy") print("STEP 3 over-broad rule identified")
Step 3 · Python 3 in your browser (JupyterLite); numbered callout added to a real capture. Step 4
- Do
- Run the fourth cell in the editor and inspect before-and-after decisions after narrowing P4.
- You will see
Run the cell. You should see:
- Patch: P4 zone '*' -> 'plant-a'; other rules retained
- request before/rule after/rule
- R1 allow/P1 allow/P1
- R2 allow/P2 allow/P2
- R3 deny /default-deny deny /default-deny
- R4 allow/P4 allow/P4
- R5 allow/P4 deny /default-deny
- R6 deny /default-deny deny /default-deny
- R7 deny /default-deny deny /default-deny
- STEP 4 policy narrowed and replayed
- Why it matters
- Replay shows whether the fix blocks the unwanted request while preserving legitimate access.
Code for step 4fixed_rules = [(*rule[:2], "plant-a", *rule[3:]) if rule[0] == "P4" else rule for rule in rules] print("Patch: P4 zone '*' -> 'plant-a'; other rules retained") print("request before/rule after/rule") for request in requests: before = evaluate(request, rules) after = evaluate(request, fixed_rules) print(f"{request[0]:7} {before[0]:5}/{before[1]:12} {after[0]:5}/{after[1]}") print("STEP 4 policy narrowed and replayed")
'plant-a'; other rules retained; request before/rule after/rule" loading="lazy">Step 4 · Python 3 in your browser (JupyterLite); numbered callout added to a real capture. Step 5
- Do
- Run the final cell in the editor and inspect the post-fix allow/deny totals and assertions.
- You will see
Run the cell. You should see:
- Post-fix: allow=3, deny=4
- R5 cross-zone viewer read: deny/default-deny
- R2 plant-a technician read: allow/P2
- R7 unknown role: deny/default-deny
- Seven expected decisions: validated
- This table does not establish enforcement by a real device or service.
- STEP 5 policy invariants validated
- Why it matters
- Expected decisions make the classroom policy auditable after any future change.
Code for step 5expected = {"R1":"allow", "R2":"allow", "R3":"deny", "R4":"allow", "R5":"deny", "R6":"deny", "R7":"deny"} for request in requests: assert evaluate(request, fixed_rules)[0] == expected[request[0]] print(f"Post-fix: allow={sum(x == 'allow' for x in expected.values())}, deny={sum(x == 'deny' for x in expected.values())}") print("R5 cross-zone viewer read: deny/default-deny") print("R2 plant-a technician read: allow/P2") print("R7 unknown role: deny/default-deny") print("Seven expected decisions: validated") print("This table does not establish enforcement by a real device or service.") print("STEP 5 policy invariants validated")
Step 5 · Python 3 in your browser (JupyterLite); numbered callout added to a real capture.
Chapter checks
These questions refer to the chapter’s examples. Use the return links to review their answers.
A new technician account is created in an IoT platform. Following least privilege, what should its initial access be?
Return to the chapter’s knowledge checkAn AWS IoT-style policy contains a broad statement that Allows publishing to all device topics and a separate statement that explicitly Denies publishing to another tenant's topics. A device tries to publish to another tenant's topic. What happens?
Return to the chapter’s knowledge check
Caution
Before you start
Read this before you start.
This synthetic policy audit does not establish enforcement by a real device or service.
Technical details
- Version tested
- Version tested: Python 3.12.7 / Pyodide 0.27.6 in JupyterLite 0.6.4; Chromium 148.0.7778.96; synthetic classroom fixtures. Date: 2026-10-08.
https://iotclass.org/labs/device-policy-audit.html