Skip to content

Commit

Permalink
add --bucket-name regex filter
Browse files Browse the repository at this point in the history
  • Loading branch information
badra001 committed Jul 17, 2026
1 parent e155231 commit 2377055
Showing 1 changed file with 20 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@
from collections import defaultdict

# --- VERSIONING ---
__version__ = "1.1.0"
__version__ = "1.2.0"

# Strict binary byte scaling dictionary
UNIT_CONVERSION_MAP = {
"B": {"divisor": 1, "label": "Bytes"},
"MB": {"divisor": 1024**2, "label": "MB"},
Expand All @@ -25,13 +24,13 @@ def find_latest_file(pattern):
return max(files, key=os.path.getctime) if files else None

def main():
parser = argparse.ArgumentParser(description="AWS S3 Fleet Storage Unit-Normalized Assessor Suite v1.1.0")
parser = argparse.ArgumentParser(description="AWS S3 Fleet Storage Filtered Assessor Suite v1.2.0")
parser.add_argument("--input", help="JSON file (default: latest audit_results.check_s3_buckets.*.json)")
parser.add_argument("--size-units", default="GB", choices=["B", "MB", "GB", "TB", "PB"],
help="Target size scale unit representation to output in reports (Default: GB)")
parser.add_argument("--bucket-name", help="Optional regular expression pattern to filter bucket names (e.g., '^prod-.*')")
args = parser.parse_args()

# Locate and resolve input data paths
input_file = args.input or find_latest_file("audit_results.check_s3_buckets.*.json")
if not input_file:
print("Error: No input file located."); sys.exit(1)
Expand All @@ -46,7 +45,15 @@ def main():
txt_filename = f"{os.path.splitext(input_file)[0]}.txt"
csv_filename = f"s3_global_inventory_{timestamp_suffix}.csv"

# Setup the requested unit variables
# Compile the bucket filter regex if provided
bucket_regex = None
if args.bucket_name:
try:
bucket_regex = re.compile(args.bucket_name)
except Exception as e:
print(f"Error: Invalid regular expression pattern supplied for --bucket-name: {e}")
sys.exit(1)

target_unit = args.size_units.upper()
scale_spec = UNIT_CONVERSION_MAP[target_unit]
divisor = scale_spec["divisor"]
Expand All @@ -72,6 +79,10 @@ def main():
max_alias_w = max(max_alias_w, len(str(alias)))

for b in buckets:
# --- NEW FEATURE: Apply bucket name regex filter block ---
if bucket_regex and not bucket_regex.search(b["name"]):
continue

total_buckets += 1
b_bytes = b.get("size_bytes", 0)
b_objects = b.get("object_count", 0)
Expand All @@ -92,7 +103,7 @@ def main():
b["versioning_status"], b["kms_key_id"], b["has_lifecycle"]
])

# Write Master Baseline Inventory CSV File
# Write Filtered Inventory CSV File
with open(csv_filename, "w", newline="", encoding="utf-8") as cf:
writer = csv.writer(cf)
writer.writerow(["Account ID", "Account Alias", "Bucket Name", "Region", "ARN", f"Size ({target_unit})", "Object Count", "Versioning Status", "KMS Key ID", "Has Lifecycle"])
Expand All @@ -108,6 +119,8 @@ def main():

# Section 1
tf.write("[SECTION 1: FLEET METRICS PROFILE OVERVIEW]\n")
if bucket_regex:
tf.write(f" [FILTER ACTIVE] Target Bucket Name Regex Match Pattern: '{args.bucket_name}'\n")
tf.write(f" ▶ Total Corporate S3 Buckets Managed : {total_buckets}\n")

scaled_global_size = total_global_bytes if target_unit == "B" else (total_global_bytes / divisor)
Expand Down Expand Up @@ -136,11 +149,9 @@ def main():
tf.write("[SECTION 4: GRANULAR BUCKET METADATA INVENTORY MATRIX]\n")
size_header_str = f"Size ({target_unit})"

# Build dynamic padding with right-aligned floating precision parameters
fmt_row_header = f" {{:<12}} | {{:<{{max_alias_w}}}} | {{:<{{max_bucket_w}}}} | {{:<12}} | {{:>18}} | {{:>12}} | {{:<10}} | {{:<13}}\n"
fmt_row_data = f" {{:<12}} | {{:<{{max_alias_w}}}} | {{:<{{max_bucket_w}}}} | {{:<12}} | {{:>18{',d' if target_unit == 'B' else ',.2f'}}} | {{:>12,}} | {{:<10}} | {{:<13}}\n"

# Inject column widths into format strings
f_header = fmt_row_header.format("Account ID", size_header_str, "Version", "Lifecycle", max_alias_w=max_alias_w, max_bucket_w=max_bucket_w)
tf.write(f_header.format("Account ID", "Account Alias", "Bucket Name", "Region", size_header_str, "Objects", "Version", "Lifecycle"))

Expand All @@ -150,7 +161,7 @@ def main():
f_data = fmt_row_data.format(max_alias_w=max_alias_w, max_bucket_w=max_bucket_w)
tf.write(f_data.format(r[0], r[1], r[2], r[3], scaled_bucket_size, r[6], r[7], r[9]))

print(f"[+] Output unit normalization target mapped successfully to: {target_unit}")
print(f"[+] Successfully evaluated total data subset across {total_buckets} matching buckets.")
print(f"[+] Multi-Account Analytical Dashboard Saved: {txt_filename}")
print(f"[+] Tabular Base Inventory Shared Asset Generated: {csv_filename}")

Expand Down

0 comments on commit 2377055

Please sign in to comment.