# FIRST attempt, kept for the write-up. Naive on purpose.
import json, sys
a = json.load(open(sys.argv[1])); n = 0
def f(sev, what): 
    global n; n += 1; print(f"[{sev}] {what}")
for b in a["s3_buckets"]:
    if b["Policy"] and any(s.get("Principal") == "*" for s in b["Policy"]["Statement"]): f("HIGH", f"s3 {b['Name']} public policy")
    if not b["Encryption"]: f("MED", f"s3 {b['Name']} no encryption")
for u in a["iam_users"]:
    for p in u["Policies"]:
        if any(s["Action"] == "*" for s in p["Document"]["Statement"]): f("HIGH", f"iam {u['UserName']} wildcard admin")
for r in a["iam_roles"]:
    if any(s["Principal"].get("AWS") == "*" for s in r["AssumeRolePolicyDocument"]["Statement"]): f("HIGH", f"role {r['RoleName']} trusts anyone")
for g in a["security_groups"]:
    for p in g["IpPermissions"]:
        if any(i["CidrIp"] == "0.0.0.0/0" for i in p["IpRanges"]): f("HIGH", f"sg {g['GroupName']} port {p['FromPort']} open to world")
for fn in a["lambda_functions"]:
    for k in fn["Environment"]["Variables"]:
        if any(w in k.upper() for w in ("SECRET", "PASSWORD", "KEY")): f("HIGH", f"lambda {fn['FunctionName']} secret-looking env var {k}")
for v in a["ebs_volumes"]:
    if not v["Encrypted"]: f("MED", f"ebs {v['VolumeId']} unencrypted")
for t in a["cloudtrail_trails"]:
    if not t["IsLogging"]: f("HIGH", f"trail {t['Name']} not logging")
print("total findings:", n)
