Skip to content
KALANBLON
Show contents

Chapter 5 of 7 · 20 min

Access permissions and cost control

By the end of this chapter, you'll be able to

  • Explain the role of identity and access management (IAM)
  • Evaluate an access policy: deny by default, explicit deny wins
  • Apply the principle of least privilege
  • Spot unused resources and track costs per project

Identities and permissions

In a cloud account, every action goes through an identity and access management service (IAM). An identity can be a person, an application or a machine. Policies say which actions it may perform, on which resources. Almost all providers follow two rules:

  1. deny by default: whatever is not explicitly allowed is forbidden;
  2. an explicit deny overrides any allow.
from fnmatch import fnmatch

policy = [
    {"effect": "allow", "action": "storage:read", "resource": "photos/*"},
    {"effect": "allow", "action": "storage:write", "resource": "photos/students/*"},
    {"effect": "deny", "action": "storage:*", "resource": "photos/archives/*"},
]

def decision(action, resource):
    rules = [r for r in policy if fnmatch(action, r["action"]) and fnmatch(resource, r["resource"])]
    if any(r["effect"] == "deny" for r in rules):
        return "denied (explicit deny)"
    if any(r["effect"] == "allow" for r in rules):
        return "allowed"
    return "denied (by default)"

for action, resource in [("storage:read", "photos/class-3b.jpg"),
                         ("storage:write", "photos/students/awa.jpg"),
                         ("storage:read", "photos/archives/2020.jpg"),
                         ("storage:delete", "photos/class-3b.jpg")]:
    print(f"{action:<15} {resource:<26} → {decision(action, resource)}")
storage:read    photos/class-3b.jpg        → allowed
storage:write   photos/students/awa.jpg    → allowed
storage:read    photos/archives/2020.jpg   → denied (explicit deny)
storage:delete  photos/class-3b.jpg        → denied (by default)

fnmatch compares a name to a pattern with *, like the terminal's wildcards. The action names are simplified here, but the logic is that of real services.

With no matching rule at all, the answer would also have been a deny: deny by default.

Step 5 of 5
Text description of the animation
  1. An application asks to read an old photo.
  2. Reading photos/* is allowed, but a rule also targets photos/archives/*.
  3. An explicit deny matches: it wins, whatever the allows.
  4. Rule 1's allow is not even taken into account.
  5. With no matching rule at all, the answer would also have been a deny: deny by default.

Least privilege

Each identity only gets the permissions strictly needed for its task: the application that displays photos reads storage, it has no business deleting machines. If its key leaks, the damage stays limited. A few hygiene rules complete the principle:

  • no everyday use of the cloud's “root” account, and two-factor authentication for every human account;
  • temporary identities for applications (roles assigned to a machine or to the pipeline), rather than permanent keys copied into files;
  • no keys in code or in a Git repository: a published secret must be considered compromised, and revoked immediately.

Controlling costs

Pay-per-use has a flip side: whatever runs costs money, even if nobody uses it. Good habits:

  • budget alerts that warn when the month's spending goes over a threshold;
  • tags on every resource (project, environment, owner) to know who spends what;
  • hunting down idle and oversized resources: a machine used at 3% can often be replaced by a smaller one, or switched off at night.

Mini-lab: write the pipeline's policyPython 3.13

Output

Click “Run”: Python will load in your browser (about 10 MB the first time).

Your code runs in your browser, isolated from the page. · Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor

Your turn

Worked exercise

Worked exercise: hunting down waste

Here is an account's machine inventory, with their monthly cost and average CPU utilisation over 30 days. Spot the idle machines (under 5% utilisation) and compute the monthly saving if they are switched off, then display the cost per project of what remains.

machines = [
    {"name": "web-1", "project": "site", "cost": 60, "cpu": 0.45},
    {"name": "web-2", "project": "site", "cost": 60, "cpu": 0.41},
    {"name": "perf-test", "project": "site", "cost": 240, "cpu": 0.01},
    {"name": "analytics", "project": "data", "cost": 120, "cpu": 0.62},
    {"name": "trade-show-demo", "project": "sales", "cost": 90, "cpu": 0.02},
]
Hint 1
Split the list in two with a condition on cpu < 0.05.
Hint 2
A dictionary and get(project, 0) let you accumulate costs per project.
Show the solution and common mistakes
machines = [
    {"name": "web-1", "project": "site", "cost": 60, "cpu": 0.45},
    {"name": "web-2", "project": "site", "cost": 60, "cpu": 0.41},
    {"name": "perf-test", "project": "site", "cost": 240, "cpu": 0.01},
    {"name": "analytics", "project": "data", "cost": 120, "cpu": 0.62},
    {"name": "trade-show-demo", "project": "sales", "cost": 90, "cpu": 0.02},
]

idle = [m for m in machines if m["cpu"] < 0.05]
active = [m for m in machines if m["cpu"] >= 0.05]
saving = sum(m["cost"] for m in idle)
total = sum(m["cost"] for m in machines)

print("to switch off:", [m["name"] for m in idle])
print(f"saving: {saving} $/month out of {total} $ ({saving / total:.0%})")
per_project = {}
for m in active:
    per_project[m["project"]] = per_project.get(m["project"], 0) + m["cost"]
print("remaining cost per project:", per_project)
to switch off: ['perf-test', 'trade-show-demo']
saving: 330 $/month out of 570 $ (58%)
remaining cost per project: {'site': 120, 'data': 120}

More than half of the bill came from a forgotten performance test machine and a demo. Before switching off, check with their owner anyway (hence the value of the “owner” tag): a rarely used machine can have an occasional but important role.

Common mistakes:

  • Deleting straight away instead of switching off then checking: unsaved data on the machine would be lost.
  • Only looking at the CPU: a database machine can have a quiet CPU and very busy memory or disks.

Summary

  1. 1IAM decides who may perform which action on which resource, with deny by default.
  2. 2An explicit deny always wins over an allow.
  3. 3Least privilege: each identity only gets the permissions it strictly needs; never “*” on “*”.
  4. 4No keys in code: a published secret is compromised and must be revoked.
  5. 5Budget alerts, tags and hunting down idle resources keep the bill under control.

Your progress is saved on this device. Create a free account to keep your progress on all your devices.