-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
109 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
local-app/python-tools/cross-organization/generate_fms_payload.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| #!/usr/bin/env python | ||
| import json | ||
| import os | ||
| import sys | ||
|
|
||
| # --- VERSIONING --- | ||
| __version__ = "1.0.0" | ||
|
|
||
| def main(): | ||
| report_file = "sg_cluster_report.json" | ||
| if not os.path.exists(report_file): | ||
| print(f"Error: {report_file} not found. Run assess_security_groups.py first.") | ||
| sys.exit(1) | ||
|
|
||
| with open(report_file, 'r') as f: | ||
| clusters = json.load(f) | ||
|
|
||
| # Sort clusters by size to show the best candidates for FMS first | ||
| sorted_hashes = sorted(clusters.keys(), key=lambda x: len(clusters[x]), reverse=True) | ||
|
|
||
| print("-" * 80) | ||
| print(f"FMS POLICY PAYLOAD GENERATOR - v{__version__}") | ||
| print("-" * 80) | ||
| print(f"{'#':<3} | {'Hash ID':<15} | {'Instances':<10} | {'Example Name'}") | ||
|
|
||
| for i, h in enumerate(sorted_hashes[:10], 1): | ||
| example = clusters[h][0] | ||
| print(f"{i:<3} | {h[:12]:<15} | {len(clusters[h]):<10} | {example['name']}") | ||
|
|
||
| choice = input("\nSelect a Cluster Number to generate FMS payload: ") | ||
| try: | ||
| selected_hash = sorted_hashes[int(choice)-1] | ||
| # Load the raw SG data from the example path | ||
| example_path = clusters[selected_hash][0]['path'] | ||
| with open(example_path, 'r') as f: | ||
| sg_data = json.load(f) | ||
| except (ValueError, IndexError): | ||
| print("Invalid selection."); sys.exit(1) | ||
|
|
||
| # Construct the FMS ManagedServiceData JSON | ||
| # This matches the 'SECURITY_GROUPS_CONTENT_AUDIT' type requirement | ||
| fms_payload = { | ||
| "type": "SECURITY_GROUPS_CONTENT_AUDIT", | ||
| "securityGroups": [ | ||
| { | ||
| "id": "REPLACE_WITH_MASTER_SG_ID" | ||
| } | ||
| ], | ||
| "securityGroupAction": { | ||
| "type": "ALLOW_ONLY_MANAGED_RULES" | ||
| } | ||
| } | ||
|
|
||
| print("\n" + "="*80) | ||
| print("TERRAFORM FMS POLICY HCL SNIPPET") | ||
| print("="*80) | ||
| print(f""" | ||
| resource "aws_fms_policy" "remediated_policy" {{ | ||
| name = "FMS-Policy-{selected_hash[:8]}" | ||
| resource_type = "AWS::EC2::SecurityGroup" | ||
| remediation_enabled = false # Set to true after verifying audit results | ||
| security_service_policy_data {{ | ||
| type = "SECURITY_GROUPS_CONTENT_AUDIT" | ||
| managed_service_data = jsonencode({json.dumps(fms_payload, indent=2)}) | ||
| }} | ||
| include_map {{ | ||
| account = {json.dumps([inst['account'] for inst in clusters[selected_hash]])} | ||
| }} | ||
| }} | ||
| """) | ||
| print("="*80) | ||
| print("NOTE: You must create a 'Master SG' in your FMS Admin account with the") | ||
| print("rules found in the JSON audit, then replace REPLACE_WITH_MASTER_SG_ID.") | ||
|
|
||
| if __name__ == "__main__": | ||
| main() |