audit-labs/audit-tools

A collection of scripts, queries, and other goodies you can use in an audit. audit automation compliance evidence scripts

tui/tests/test_aws_runner.py

v1.1.0
audit-tools/tui/tests/test_aws_runner.py history · blame · raw

116 lines · 3423 bytes

  1"""Tests for the TUI's AWS audit orchestration.
  2
  3Stub the boto3 session and the collectors, then verify run_audit's wiring:
  4each check is called with the config, per-check errors don't abort the run, a
  5failed session build is reported cleanly, and the CSV package is written.
  6"""
  7
  8import csv
  9import os
 10
 11import pytest
 12
 13from tui import aws_runner as r
 14
 15
 16@pytest.fixture
 17def fake_checks(monkeypatch):
 18    calls = {}
 19
 20    def users_fn(cfg):
 21        calls["users"] = cfg
 22        return [{"user": "alice"}, {"user": "bob"}]
 23
 24    def policy_fn(cfg):
 25        calls["policy"] = cfg
 26        return [{"minimum_length": 14}]
 27
 28    def boom_fn(cfg):
 29        raise RuntimeError("kaboom")
 30
 31    checks = [
 32        r.Check("users", "Users", users_fn, "users.csv"),
 33        r.Check("policy", "Policy", policy_fn, "policy.csv"),
 34        r.Check("boom", "Boom", boom_fn, "boom.csv"),
 35    ]
 36    monkeypatch.setattr(r, "CHECKS", checks)
 37
 38    # Replace build_cfg so no real boto3 session is created.
 39    monkeypatch.setattr(
 40        r.api,
 41        "build_cfg",
 42        lambda profile, region, account: {
 43            "session": "SESSION",
 44            "profile": profile,
 45            "region": region,
 46            "account": account,
 47        },
 48    )
 49    return calls
 50
 51
 52def run(tmp_path, keys, profile="", region="us-east-1", account=""):
 53    events = []
 54    sections = r.run_audit(
 55        r.AwsTarget(profile=profile, region=region, account=account),
 56        output_dir=str(tmp_path),
 57        selected_keys=keys,
 58        on_event=events.append,
 59    )
 60    return events, sections
 61
 62
 63def test_dispatch_and_files(tmp_path, fake_checks):
 64    calls = fake_checks
 65    run(tmp_path, ["users", "policy"], region="eu-west-1")
 66
 67    # Each collector received the config dict.
 68    assert calls["users"]["region"] == "eu-west-1"
 69    assert calls["policy"]["session"] == "SESSION"
 70
 71    for name in ("users.csv", "policy.csv", "summary.txt"):
 72        assert os.path.exists(tmp_path / name), name
 73
 74    with open(tmp_path / "users.csv", newline="") as f:
 75        assert len(list(csv.DictReader(f))) == 2
 76
 77
 78def test_failing_check_does_not_abort_run(tmp_path, fake_checks):
 79    events, sections = run(tmp_path, ["boom", "users"])
 80
 81    kinds = [(e.kind, e.label) for e in events]
 82    assert ("error", "Boom") in kinds
 83    assert ("done", "Users") in kinds
 84
 85    labels = dict(sections)
 86    assert labels["Boom"] == 0
 87    assert labels["Users"] == 2
 88
 89
 90def test_session_build_failure_is_reported(tmp_path, fake_checks, monkeypatch):
 91    def boom_cfg(profile, region, account):
 92        raise RuntimeError("ProfileNotFound")
 93
 94    monkeypatch.setattr(r.api, "build_cfg", boom_cfg)
 95
 96    events, sections = run(tmp_path, ["users"], profile="ghost")
 97
 98    kinds = [(e.kind, e.label) for e in events]
 99    assert ("error", "AWS session") in kinds
100    # Run still ends with a summary and writes the (empty) package.
101    summary = [e for e in events if e.kind == "summary"]
102    assert summary
103    assert summary[0].count == 0
104    assert sections == []
105    assert os.path.exists(tmp_path / "summary.txt")
106
107
108def test_subject_defaults_to_default(tmp_path, fake_checks):
109    run(tmp_path, ["users"], profile="")
110    # An empty profile is folder-named "default".
111    assert r.default_output_dir("./out", "") == r.default_output_dir("./out", "default")
112
113
114def test_sso_off_by_default():
115    assert "sso_assignments" not in r.DEFAULT_SELECTION
116    assert "iam_users" in r.DEFAULT_SELECTION