|
| 1 | +from datetime import datetime |
| 2 | +import boto3 |
| 3 | +from apps.aws.aws_models import ( |
| 4 | + IAMRole, |
| 5 | + IAMPolicy, |
| 6 | + IAMInlinePolicy, |
| 7 | + EC2Instance, |
| 8 | + S3Bucket, |
| 9 | + LambdaFunction, |
| 10 | + SQSQueue, |
| 11 | +) |
| 12 | + |
| 13 | + |
| 14 | +def populate_iam_roles_and_policies(region_name): |
| 15 | + # Initialize boto3 IAM client |
| 16 | + client = boto3.client("iam", region_name=region_name) |
| 17 | + |
| 18 | + # Get list of all IAM roles |
| 19 | + roles = client.list_roles() |
| 20 | + |
| 21 | + for role_data in roles["Roles"]: |
| 22 | + role, _ = IAMRole.objects.update_or_create( |
| 23 | + role_name=role_data["RoleName"], |
| 24 | + defaults={ |
| 25 | + "role_id": role_data["RoleId"], |
| 26 | + "arn": role_data["Arn"], |
| 27 | + "create_date": role_data["CreateDate"], |
| 28 | + }, |
| 29 | + ) |
| 30 | + |
| 31 | + # Fetch and attach managed policies to the role |
| 32 | + attached_policies = client.list_attached_role_policies(RoleName=role.role_name) |
| 33 | + for policy in attached_policies["AttachedPolicies"]: |
| 34 | + policy_details = client.get_policy(PolicyArn=policy["PolicyArn"]) |
| 35 | + policy_version = client.get_policy_version( |
| 36 | + PolicyArn=policy["PolicyArn"], |
| 37 | + VersionId=policy_details["Policy"]["DefaultVersionId"], |
| 38 | + ) |
| 39 | + iam_policy, _ = IAMPolicy.objects.update_or_create( |
| 40 | + policy_name=policy["PolicyName"], |
| 41 | + defaults={ |
| 42 | + "policy_id": policy_details["Policy"]["PolicyId"], |
| 43 | + "arn": policy_details["Policy"]["Arn"], |
| 44 | + "create_date": policy_details["Policy"]["CreateDate"], |
| 45 | + "policy_document": policy_version["PolicyVersion"]["Document"], |
| 46 | + }, |
| 47 | + ) |
| 48 | + # Only add the policy if it's attached to the current role |
| 49 | + role.policies.add(iam_policy) |
| 50 | + print( |
| 51 | + f"Attached policy: {iam_policy.policy_name} to role: {role.role_name}" |
| 52 | + ) |
| 53 | + |
| 54 | + print(f"Populated IAM roles and policies for region: {region_name}") |
| 55 | + |
| 56 | + |
| 57 | +def populate_iam_inline_policies(region_name): |
| 58 | + client = boto3.client("iam", region_name=region_name) |
| 59 | + |
| 60 | + # Iterate over all roles in the database |
| 61 | + for role in IAMRole.objects.all(): |
| 62 | + inline_policies = client.list_role_policies(RoleName=role.role_name) |
| 63 | + |
| 64 | + for policy_name in inline_policies["PolicyNames"]: |
| 65 | + policy = client.get_role_policy( |
| 66 | + RoleName=role.role_name, PolicyName=policy_name |
| 67 | + ) |
| 68 | + IAMInlinePolicy.objects.update_or_create( |
| 69 | + role=role, |
| 70 | + policy_name=policy_name, |
| 71 | + defaults={"policy_document": policy["PolicyDocument"]}, |
| 72 | + ) |
| 73 | + |
| 74 | + print(f"Populated IAM inline policies for region: {region_name}") |
| 75 | + |
| 76 | + |
| 77 | +def populate_ec2_instances(region_name): |
| 78 | + client = boto3.client("ec2", region_name=region_name) |
| 79 | + |
| 80 | + instances = client.describe_instances() |
| 81 | + |
| 82 | + for reservation in instances["Reservations"]: |
| 83 | + for instance_data in reservation["Instances"]: |
| 84 | + EC2Instance.objects.update_or_create( |
| 85 | + instance_id=instance_data["InstanceId"], |
| 86 | + defaults={ |
| 87 | + "name": next( |
| 88 | + ( |
| 89 | + tag["Value"] |
| 90 | + for tag in instance_data.get("Tags", []) |
| 91 | + if tag["Key"] == "Name" |
| 92 | + ), |
| 93 | + None, |
| 94 | + ), |
| 95 | + "instance_type": instance_data["InstanceType"], |
| 96 | + "region": region_name, |
| 97 | + "availability_zone": instance_data["Placement"]["AvailabilityZone"], |
| 98 | + "public_ip": instance_data.get("PublicIpAddress"), |
| 99 | + "private_ip": instance_data.get("PrivateIpAddress"), |
| 100 | + "state": instance_data["State"]["Name"], |
| 101 | + "launch_time": instance_data["LaunchTime"], |
| 102 | + "iam_role": instance_data.get("IamInstanceProfile", {}).get("Arn"), |
| 103 | + }, |
| 104 | + ) |
| 105 | + |
| 106 | + print(f"Populated EC2 instances for region: {region_name}") |
| 107 | + |
| 108 | + |
| 109 | +def populate_s3_buckets(region_name): |
| 110 | + client = boto3.client("s3", region_name=region_name) |
| 111 | + |
| 112 | + buckets = client.list_buckets() |
| 113 | + |
| 114 | + for bucket_data in buckets["Buckets"]: |
| 115 | + bucket_location = client.get_bucket_location(Bucket=bucket_data["Name"]) |
| 116 | + bucket_region = bucket_location["LocationConstraint"] or "us-east-1" |
| 117 | + if bucket_region == region_name: |
| 118 | + S3Bucket.objects.update_or_create( |
| 119 | + name=bucket_data["Name"], |
| 120 | + defaults={ |
| 121 | + "region": bucket_region, |
| 122 | + "creation_date": bucket_data["CreationDate"], |
| 123 | + }, |
| 124 | + ) |
| 125 | + |
| 126 | + print(f"Populated S3 buckets for region: {region_name}") |
| 127 | + |
| 128 | + |
| 129 | +def populate_lambda_functions(region_name): |
| 130 | + client = boto3.client("lambda", region_name=region_name) |
| 131 | + |
| 132 | + functions = client.list_functions() |
| 133 | + |
| 134 | + for function_data in functions["Functions"]: |
| 135 | + LambdaFunction.objects.update_or_create( |
| 136 | + function_name=function_data["FunctionName"], |
| 137 | + defaults={ |
| 138 | + "function_arn": function_data["FunctionArn"], |
| 139 | + "runtime": function_data["Runtime"], |
| 140 | + "handler": function_data["Handler"], |
| 141 | + "role": function_data["Role"], |
| 142 | + "code_size": function_data["CodeSize"], |
| 143 | + "description": function_data.get("Description", ""), |
| 144 | + "timeout": function_data["Timeout"], |
| 145 | + "memory_size": function_data["MemorySize"], |
| 146 | + "last_modified": function_data["LastModified"], |
| 147 | + "region": region_name, |
| 148 | + }, |
| 149 | + ) |
| 150 | + |
| 151 | + print(f"Populated Lambda functions for region: {region_name}") |
| 152 | + |
| 153 | + |
| 154 | +def populate_sqs_queues(region_name): |
| 155 | + client = boto3.client("sqs", region_name=region_name) |
| 156 | + |
| 157 | + queues = client.list_queues() |
| 158 | + |
| 159 | + if "QueueUrls" in queues: |
| 160 | + for queue_url in queues["QueueUrls"]: |
| 161 | + queue_attributes = client.get_queue_attributes( |
| 162 | + QueueUrl=queue_url, AttributeNames=["All"] |
| 163 | + )["Attributes"] |
| 164 | + |
| 165 | + # Convert Unix timestamp to datetime object |
| 166 | + created_timestamp = datetime.utcfromtimestamp( |
| 167 | + float(queue_attributes["CreatedTimestamp"]) |
| 168 | + ) |
| 169 | + |
| 170 | + SQSQueue.objects.update_or_create( |
| 171 | + queue_name=queue_attributes["QueueArn"].split(":")[-1], |
| 172 | + defaults={ |
| 173 | + "queue_url": queue_url, |
| 174 | + "region": region_name, |
| 175 | + "arn": queue_attributes["QueueArn"], |
| 176 | + "created_timestamp": created_timestamp, |
| 177 | + "visibility_timeout": queue_attributes["VisibilityTimeout"], |
| 178 | + "maximum_message_size": queue_attributes["MaximumMessageSize"], |
| 179 | + "message_retention_period": queue_attributes[ |
| 180 | + "MessageRetentionPeriod" |
| 181 | + ], |
| 182 | + }, |
| 183 | + ) |
| 184 | + |
| 185 | + print(f"Populated SQS queues for region: {region_name}") |
| 186 | + |
| 187 | + |
| 188 | +def populate_aws_resources(region_name): |
| 189 | + populate_iam_roles_and_policies(region_name) |
| 190 | + populate_iam_inline_policies(region_name) |
| 191 | + populate_ec2_instances(region_name) |
| 192 | + populate_s3_buckets(region_name) |
| 193 | + populate_lambda_functions(region_name) |
| 194 | + populate_sqs_queues(region_name) |
0 commit comments