"""Walks through: private bucket -> bad policy -> exposure -> Block Public Access -> the trap. Offline model."""
import json
from bucket_sim import SimBucket, Denied, FLAGS

def load(p): return json.load(open(p))
def show(label, ok, why): print(f"  {label:<34} {'EXPOSED' if ok else 'blocked':<8} ({why})")

print("== STEP 1  fresh test bucket, nothing public")
b = SimBucket("lab-exposure-test")
b.put_object("reports/q3.csv"); b.put_object("reports/payroll.csv")
show("anonymous GET reports/q3.csv", *b.anon_get("reports/q3.csv"))

print("\n== STEP 2  attach a 'quick fix' policy: Principal *, s3:GetObject")
b.put_policy(load("policies/bad_get.json"))
show("anonymous GET reports/q3.csv", *b.anon_get("reports/q3.csv"))
ok, keys = b.anon_list()
print(f"  {'anonymous LIST bucket':<34} {'EXPOSED' if ok else 'blocked':<8} (GetObject only: attacker must already know key names)")

print("\n== STEP 3  swap in the 'AWS: *' policy that also allows ListBucket")
b.put_policy(load("policies/bad_aws_star_list.json"))
ok, keys = b.anon_list()
print(f"  {'anonymous LIST bucket':<34} {'EXPOSED' if ok else 'blocked':<8} keys visible: {', '.join(keys)}")

print("\n== STEP 4  the ACL route (separate bucket, public-read object ACL, no policy)")
a = SimBucket("lab-exposure-acl"); a.put_object("promo/flyer.pdf", acl="public-read")
show("anonymous GET promo/flyer.pdf", *a.anon_get("promo/flyer.pdf"))

print("\n== STEP 5  which single switch fixes what? (exposure already in place)")
print(f"  {'switch turned on':<24}{'old public policy':<20}{'old public ACL':<18}{'new public policy':<20}{'new public ACL'}")
matrix = []
for flag in FLAGS + ("ALL FOUR",):
    kw = {f: True for f in FLAGS} if flag == "ALL FOUR" else {flag: True}
    p = SimBucket("lab-exposure-test"); p.put_object("k"); p.put_policy(load("policies/bad_get.json")); p.set_bpa(**kw)
    old_policy = not p.anon_get("k")[0]
    c = SimBucket("lab-exposure-test"); c.put_object("k", acl="public-read"); c.set_bpa(**kw)
    old_acl = not c.anon_get("k")[0]
    n = SimBucket("lab-exposure-test", **kw)
    try: n.put_policy(load("policies/bad_get.json")); new_policy = False
    except Denied: new_policy = True
    n2 = SimBucket("lab-exposure-test", **kw)
    try: n2.put_object("k", acl="public-read"); new_acl = False
    except Denied: new_acl = True
    matrix.append((flag, old_policy, old_acl, new_policy, new_acl))
    y = lambda v: "stops it" if v else "-"
    print(f"  {flag:<24}{y(old_policy):<20}{y(old_acl):<18}{y(new_policy):<20}{y(new_acl)}")
json.dump(matrix, open("matrix.json", "w"))

print("\n== STEP 6  all four switches on, then try the bad policy again")
b.set_bpa(**{f: True for f in FLAGS})
show("anonymous GET reports/q3.csv", *b.anon_get("reports/q3.csv"))
try: b.put_policy(load("policies/bad_get.json"))
except Denied as e: print(f"  put bad policy again               {e}")

print("\n== STEP 7  the trap: the bad policy is STILL attached")
print(f"  policy still on bucket: {b.policy is not None}")
b.set_bpa(**{f: False for f in FLAGS})
show("someone toggles switches off", *b.anon_get("reports/q3.csv"))
b.set_bpa(**{f: True for f in FLAGS}); b.delete_policy()
show("policy deleted + switches on", *b.anon_get("reports/q3.csv"))
