krz/aws-summary-report

Automated AWS summary reports, straight to your inbox.

clone: git clone https://gitbay.org/krz/aws-summary-report.git

main: sections/s3.py · raw

 1# s3.py
 2import boto3
 3from tabulate import tabulate
 4
 5
 6def get_section(config):
 7    profile = config["aws"].get("profile")
 8    session = boto3.Session(profile_name=profile if profile else None)
 9    client = session.client("s3")
10
11    buckets = client.list_buckets()["Buckets"]
12    rows = []
13
14    for bucket in buckets:
15        name = bucket["Name"]
16        public = "Unknown"
17        encrypted = "No"
18
19        try:
20            acl = client.get_bucket_acl(Bucket=name)
21            public = any(
22                grant["Grantee"].get("URI", "").endswith("AllUsers")
23                for grant in acl["Grants"]
24            )
25        except Exception:
26            public = "Error"
27
28        try:
29            enc = client.get_bucket_encryption(Bucket=name)
30            rules = enc["ServerSideEncryptionConfiguration"]["Rules"]
31            if rules:
32                encrypted = "Yes"
33        except client.exceptions.ClientError:
34            encrypted = "No"
35
36        rows.append([name, "Yes" if public else "No", encrypted])
37
38    table = tabulate(
39        rows, headers=["Bucket", "Public", "Encrypted"], tablefmt="simple_grid"
40    )
41    lines = [
42        "S3 Bucket Access Summary:",
43        f"[https://{config['aws'].get('region')}.console.aws.amazon.com/s3/home]",
44        table,
45    ]
46
47    return "\n".join(lines)