class NukeKeypair: """Abstract key pair nuke in a class.""" def __init__(self, region_name=None) -> None: """Initialize key pair nuke.""" self.ec2 = AwsClient().connect("ec2", region_name) def nuke(self) -> None: """Keypair deleting function. Deleting all Keypair """ for keypair in self.list_keypair(): try: self.ec2.delete_key_pair(KeyName=keypair) print("Nuke Key Pair {0}".format(keypair)) except ClientError as exc: nuke_exceptions("keypair", keypair, exc) def list_keypair(self) -> Iterator[str]: """Keypair list function. List all keypair names :yield Iterator[str]: Key pair name """ response = self.ec2.describe_key_pairs() for keypair in response["KeyPairs"]: yield keypair["KeyName"]
class NukeNetworkAcl: """Abstract network acl nuke in a class.""" def __init__(self, region_name=None) -> None: """Initialize endpoint nuke.""" self.ec2 = AwsClient().connect("ec2", region_name) def nuke(self) -> None: """Network acl delete function.""" for net_acl in self.list_network_acls(): try: self.ec2.delete_network_acl(NetworkAclId=net_acl) print("Nuke ec2 network acl {0}".format(net_acl)) except ClientError as exc: nuke_exceptions("network acl", net_acl, exc) def list_network_acls(self) -> Iterator[str]: """Network acl list function. :yield Iterator[str]: Network acl Id """ response = self.ec2.describe_network_acls() for network_acl in response["NetworkAcls"]: yield network_acl["NetworkAclId"]
def __init__(self, region_name=None) -> None: """Initialize rds nuke.""" self.rds = AwsClient().connect("rds", region_name) try: self.rds.describe_db_clusters() except EndpointConnectionError: print("Rds resource is not available in this aws region") return
def __init__(self, region_name=None) -> None: """Initialize eip nuke.""" self.ec2 = AwsClient().connect("ec2", region_name) try: self.ec2.describe_addresses() except EndpointConnectionError: print("Eip resource is not available in this aws region") return
def __init__(self, region_name=None) -> None: """Initialize ecr nuke.""" self.ecr = AwsClient().connect("ecr", region_name) try: self.ecr.describe_repositories() except EndpointConnectionError: print("Ecr resource is not available in this aws region") return
def __init__(self, region_name=None) -> None: """Initialize eks nuke.""" self.eks = AwsClient().connect("eks", region_name) try: self.eks.list_clusters() except EndpointConnectionError: print("eks resource is not available in this aws region") return
def __init__(self, region_name=None) -> None: """Initialize efs nuke.""" self.efs = AwsClient().connect("efs", region_name) try: self.efs.describe_file_systems() except EndpointConnectionError: print("EFS resource is not available in this aws region") return
def __init__(self, region_name=None) -> None: """Initialize dlm nuke.""" self.dlm = AwsClient().connect("dlm", region_name) try: self.dlm.get_lifecycle_policies() except EndpointConnectionError: print("Dlm resource is not available in this aws region") return
def __init__(self, region_name=None) -> None: """Initialize glacier nuke.""" self.glacier = AwsClient().connect("glacier", region_name) try: self.glacier.list_vaults() except EndpointConnectionError: print("Glacier resource is not available in this aws region") return
class NukeS3: """Abstract s3 nuke in a class.""" def __init__(self, region_name=None) -> None: """Initialize s3 nuke.""" self.s3 = AwsClient().connect("s3", region_name) self.s3_resource = AwsResource().connect("s3", region_name) try: self.s3.list_buckets() except EndpointConnectionError: print("s3 resource is not available in this aws region") return def nuke(self, older_than_seconds: float) -> None: """S3 bucket deleting function. Deleting all s3 buckets with a timestamp greater than older_than_seconds. :param int older_than_seconds: The timestamp in seconds used from which the aws resource will be deleted """ for s3_bucket in self.list_buckets(older_than_seconds): try: # Delete all objects in bucket bucket = self.s3_resource.Bucket(s3_bucket) bucket.object_versions.delete() bucket.objects.delete() except ClientError as exc: nuke_exceptions("s3 object", s3_bucket, exc) try: # Delete bucket self.s3.delete_bucket(Bucket=s3_bucket) print("Nuke s3 bucket {0}".format(s3_bucket)) except ClientError as exc: nuke_exceptions("s3 bucket", s3_bucket, exc) def list_buckets(self, time_delete: float) -> Iterator[str]: """S3 bucket list function. List the names of all S3 buckets with a timestamp lower than time_delete. :param int time_delete: Timestamp in seconds used for filter S3 buckets :yield Iterator[str]: S3 buckets names """ response = self.s3.list_buckets() for bucket in response["Buckets"]: if bucket["CreationDate"].timestamp() < time_delete: yield bucket["Name"]
def __init__(self, region_name=None) -> None: """Initialize autoscaling nuke.""" self.asg = AwsClient().connect("autoscaling", region_name) try: self.asg.describe_auto_scaling_groups() except EndpointConnectionError: print("Autoscaling resource is not available in this aws region") return
def __init__(self, region_name=None) -> None: """Initialize elasticache nuke.""" self.elasticache = AwsClient().connect("elasticache", region_name) try: self.elasticache.describe_cache_clusters() except EndpointConnectionError: print("Elasticache resource is not available in this aws region") return
def __init__(self, region_name=None) -> None: """Initialize dynamodb nuke.""" self.dynamodb = AwsClient().connect("dynamodb", region_name) try: self.dynamodb.list_tables() except EndpointConnectionError: print("Dynamodb resource is not available in this aws region") return
def __init__(self, region_name=None) -> None: """Initialize s3 nuke.""" self.s3 = AwsClient().connect("s3", region_name) self.s3_resource = AwsResource().connect("s3", region_name) try: self.s3.list_buckets() except EndpointConnectionError: print("s3 resource is not available in this aws region") return
def __init__(self, region_name=None) -> None: """Initialize elb nuke.""" self.elb = AwsClient().connect("elb", region_name) self.elbv2 = AwsClient().connect("elbv2", region_name) try: self.elb.describe_load_balancers() self.elbv2.describe_load_balancers() except EndpointConnectionError: print("elb resource is not available in this aws region") return
def __init__(self, region_name=None) -> None: """Initialize elasticbeanstalk nuke.""" self.elasticbeanstalk = AwsClient().connect("elasticbeanstalk", region_name) try: self.elasticbeanstalk.describe_applications() except EndpointConnectionError: print( "elasticbeanstalk resource is not available in this aws region" ) return
class NukeGlacier: """Abstract glacier nuke in a class.""" def __init__(self, region_name=None) -> None: """Initialize glacier nuke.""" self.glacier = AwsClient().connect("glacier", region_name) try: self.glacier.list_vaults() except EndpointConnectionError: print("Glacier resource is not available in this aws region") return def nuke(self, older_than_seconds) -> None: """Glacier deleting function. Deleting all Glacier with a timestamp greater than older_than_seconds. :param int older_than_seconds: The timestamp in seconds used from which the aws resource will be deleted """ for vault in self.list_vaults(older_than_seconds): try: self.glacier.delete_vault(vaultName=vault) print("Nuke glacier vault {0}".format(vault)) except ClientError as exc: nuke_exceptions("glacier vault", vault, exc) def list_vaults(self, time_delete: float) -> Iterator[str]: """Glacier vault list function. List the names of all Glacier vaults with a timestamp lower than time_delete. :param int time_delete: Timestamp in seconds used for filter Glacier vaults :yield Iterator[str]: Glacier vaults names """ paginator = self.glacier.get_paginator("list_vaults") for page in paginator.paginate(): for vault in page["VaultList"]: creation_date = datetime.datetime.strptime( vault["CreationDate"], "%Y-%m-%dT%H:%M:%S.%fZ") if creation_date.timestamp() < time_delete: yield vault["VaultName"]
class NukeEcr: """Abstract ecr nuke in a class.""" def __init__(self, region_name=None) -> None: """Initialize ecr nuke.""" self.ecr = AwsClient().connect("ecr", region_name) try: self.ecr.describe_repositories() except EndpointConnectionError: print("Ecr resource is not available in this aws region") return def nuke(self, older_than_seconds: float) -> None: """Elastic Container Registry deleting function. Deleting all Elastic Container Registry with a timestamp greater than older_than_seconds. :param int older_than_seconds: The timestamp in seconds used from which the aws resource will be deleted """ for registry in self.list_registry(older_than_seconds): try: self.ecr.delete_repository(repositoryName=registry, force=True) print("Nuke ECR Registry{0}".format(registry)) except ClientError as exc: nuke_exceptions("ecr registry", registry, exc) def list_registry(self, time_delete: float) -> Iterator[str]: """Elastic Container Registry list function. List the names of all Elastic Container Registry with a timestamp lower than time_delete. :param int time_delete: Timestamp in seconds used for filter ECR :yield Iterator[str]: Elastic Container Registry names """ paginator = self.ecr.get_paginator("describe_repositories") for page in paginator.paginate(): for registry in page["repositories"]: if registry["createdAt"].timestamp() < time_delete: yield registry["repositoryName"]
class NukeEndpoint: """Abstract endpoint nuke in a class.""" def __init__(self, region_name=None) -> None: """Initialize endpoint nuke.""" self.ec2 = AwsClient().connect("ec2", region_name) try: self.ec2.describe_vpc_endpoints() except EndpointConnectionError: print("Ec2 endpoint resource is not available in this aws region") return def nuke(self, older_than_seconds: float) -> None: """Endpoint deleting function. Deleting all aws endpoint with a timestamp greater than older_than_seconds. :param int older_than_seconds: The timestamp in seconds used from which the aws resource will be deleted """ for endpoint in self.list_endpoints(older_than_seconds): try: self.ec2.delete_vpc_endpoints(VpcEndpointIds=[endpoint]) print("Nuke ec2 endpoint {0}".format(endpoint)) except ClientError as exc: nuke_exceptions("vpc endpoint", endpoint, exc) def list_endpoints(self, time_delete: float) -> Iterator[str]: """Aws enpoint list function. List IDs of all aws endpoints with a timestamp lower than time_delete. :param int time_delete: Timestamp in seconds used for filter aws endpoint :yield Iterator[str]: Elastic aws endpoint IDs """ response = self.ec2.describe_vpc_endpoints() for endpoint in response["VpcEndpoints"]: if endpoint["CreationTimestamp"].timestamp() < time_delete: yield endpoint["VpcEndpointId"]
class NukeEfs: """Abstract efs nuke in a class.""" def __init__(self, region_name=None) -> None: """Initialize efs nuke.""" self.efs = AwsClient().connect("efs", region_name) try: self.efs.describe_file_systems() except EndpointConnectionError: print("EFS resource is not available in this aws region") return def nuke(self, older_than_seconds: float) -> None: """EFS deleting function. Deleting all efs with a timestamp greater than older_than_seconds. :param int older_than_seconds: The timestamp in seconds used from which the aws resource will be deleted """ for efs_file_system in self.list_file_systems(older_than_seconds): try: self.efs.delete_file_system(FileSystemId=efs_file_system) print("Nuke EFS share {0}".format(efs_file_system)) except ClientError as exc: nuke_exceptions("efs filesystem", efs_file_system, exc) def list_file_systems(self, time_delete: float) -> Iterator[str]: """EFS list function. List IDS of all efs with a timestamp lower than time_delete. :param int time_delete: Timestamp in seconds used for filter efs :yield Iterator[str]: Rfs IDs """ paginator = self.efs.get_paginator("describe_file_systems") for page in paginator.paginate(): for filesystem in page["FileSystems"]: if filesystem["CreationTime"].timestamp() < time_delete: yield filesystem["FileSystemId"]
class NukeSecurityGroup: """Abstract security group nuke in a class.""" def __init__(self, region_name=None) -> None: """Initialize endpoint nuke.""" self.ec2 = AwsClient().connect("ec2", region_name) def nuke(self) -> None: """Security groups delete function.""" for sec_grp in self.list_security_groups(): try: self.revoke_all_rules_in_security_group(sec_grp) except ClientError as exc: nuke_exceptions("security group rule", sec_grp, exc) for sec_grp in self.list_security_groups(): try: self.ec2.delete_security_group(GroupId=sec_grp) print("Nuke ec2 security group {0}".format(sec_grp)) except ClientError as exc: nuke_exceptions("security group", sec_grp, exc) def revoke_all_rules_in_security_group(self, security_group_id) -> None: """Revoke all rules in specific security group. :param str security_group_id: The security group to apply this rule to. """ sg_desc = self.ec2.describe_security_groups( GroupIds=[security_group_id]) try: self.ec2.revoke_security_group_egress( GroupId=security_group_id, IpPermissions=sg_desc["SecurityGroups"][0]["IpPermissions"], ) self.ec2.revoke_security_group_ingress( GroupId=security_group_id, IpPermissions=sg_desc["SecurityGroups"][0] ["IpPermissionsEgress"], ) except ClientError as exc: nuke_exceptions("security group rule", security_group_id, exc) def list_security_groups(self) -> Iterator[str]: """Security groups list function. :yield Iterator[str]: Security group Id """ paginator = self.ec2.get_paginator("describe_security_groups") for page in paginator.paginate(): for security_group in page["SecurityGroups"]: yield security_group["GroupId"]
class NukeEmr: """Abstract emr nuke in a class.""" def __init__(self, region_name=None) -> None: """Initialize emr nuke.""" self.emr = AwsClient().connect("emr", region_name) def nuke(self, older_than_seconds: float) -> None: """Emr deleting function. Deleting all emr cluster resources with a timestamp greater than older_than_seconds. :param int older_than_seconds: The timestamp in seconds used from which the aws resource will be deleted """ for cluster_id in self.list_emr(older_than_seconds): try: self.emr.terminate_job_flows(JobFlowIds=[cluster_id]) print("Nuke emr cluster {0}".format(cluster_id)) except ClientError as exc: nuke_exceptions("emr cluster", cluster_id, exc) def list_emr(self, time_delete: float) -> Iterator[str]: """Emr volume list function. List the IDs of all emr clusters with a timestamp lower than time_delete. :param int time_delete: Timestamp in seconds used for filter ebs volumes :yield Iterator[str]: Emr cluster IDs """ paginator = self.emr.get_paginator("list_clusters") for page in paginator.paginate(): for cluster in page["Clusters"]: timeline = cluster["Status"]["Timeline"] if timeline["CreationDateTime"].timestamp() < time_delete: yield cluster["Id"]
class NukeKafka: """Abstract kafka nuke in a class.""" def __init__(self, region_name=None) -> None: """Initialize kafka nuke.""" self.kafka = AwsClient().connect("kafka", region_name) def nuke(self, older_than_seconds: float) -> None: """Kafka deleting function. Deleting all kafka cluster with a timestamp greater than older_than_seconds. :param int older_than_seconds: The timestamp in seconds used from which the aws resource will be deleted """ for cluster_arn in self.list_cluster(older_than_seconds): try: self.kafka.delete_cluster(ClusterArn=cluster_arn) print("Nuke kafka cluster {0}".format(cluster_arn)) except ClientError as exc: nuke_exceptions("kafka cluster", cluster_arn, exc) def list_cluster(self, time_delete: float) -> Iterator[str]: """Kafka cluster list function. List the IDs of all kafka clusters with a timestamp lower than time_delete. :param int time_delete: Timestamp in seconds used for filter ebs volumes :yield Iterator[str]: Kafka cluster arm """ paginator = self.kafka.get_paginator("list_clusters") for page in paginator.paginate(): for cluster in page["ClusterInfoList"]: if cluster["CreationTime"].timestamp() < time_delete: yield cluster["ClusterArn"]
class NukeEbs: """Abstract ebs nuke in a class.""" def __init__(self, region_name=None) -> None: """Initialize ebs nuke.""" self.ec2 = AwsClient().connect("ec2", region_name) def nuke(self, older_than_seconds: float) -> None: """Ebs deleting function. Deleting all ebs volumes resources with a timestamp greater than older_than_seconds. :param int older_than_seconds: The timestamp in seconds used from which the aws resource will be deleted """ for volume in self.list_ebs(older_than_seconds): try: self.ec2.delete_volume(VolumeId=volume) print("Nuke EBS Volume {0}".format(volume)) except ClientError as exc: nuke_exceptions("ebs volume", volume, exc) def list_ebs(self, time_delete: float) -> Iterator[str]: """Ebs volume list function. List the IDs of all ebs volumes with a timestamp lower than time_delete. :param int time_delete: Timestamp in seconds used for filter ebs volumes :yield Iterator[str]: Ebs volumes IDs """ paginator = self.ec2.get_paginator("describe_volumes") for page in paginator.paginate(): for volume in page["Volumes"]: if volume["CreateTime"].timestamp() < time_delete: yield volume["VolumeId"]
class NukeAmi: """Abstract ec2 ami in a class.""" def __init__(self, region_name=None) -> None: """Initialize ec2 ami.""" self.ec2 = AwsClient().connect("ec2", region_name) def nuke(self, older_than_seconds) -> None: """Ec2 ami deleting function. Deregister all ami present on the current aws account. :param int older_than_seconds: The timestamp in seconds used from which the aws resource will be deleted """ for ami_id in self.list_ami(older_than_seconds): try: self.ec2.deregister_image(ImageId=ami_id) print("Nuke ami {0}".format(ami_id)) except ClientError as exc: nuke_exceptions("ec2 ami", ami_id, exc) def list_ami(self, time_delete: float) -> Iterator[str]: """Ami volume list function. List the IDs of all AMI with a timestamp lower than time_delete. :param int time_delete: Timestamp in seconds used for filter AMI :yield Iterator[str]: AMI IDs """ amis_describe = self.ec2.describe_images(Owners=["self"]) for ami in amis_describe["Images"]: get_date_obj = parse(ami["CreationDate"]) date_obj = get_date_obj.replace(tzinfo=None).timestamp() if date_obj < time_delete: yield ami["ImageId"]
class NukeSnapshot: """Abstract ec2 snpashot in a class.""" def __init__(self, region_name=None) -> None: """Initialize snpashot nuke.""" self.ec2 = AwsClient().connect("ec2", region_name) def nuke(self, older_than_seconds: float) -> None: """Ec2 snapshot deleting function. Delete all snapshot present on the current aws account. :param int older_than_seconds: The timestamp in seconds used from which the aws resource will be deleted """ for snapshot_id in self.list_snapshot(older_than_seconds): try: self.ec2.delete_snapshot(SnapshotId=snapshot_id) print("Nuke snapshot {0}".format(snapshot_id)) except ClientError as exc: nuke_exceptions("ec2 snapshot", snapshot_id, exc) def list_snapshot(self, time_delete: float) -> Iterator[str]: """Snapshot list function. List the IDs of all snapshot owner with a timestamp lower than time_delete. :param int time_delete: Timestamp in seconds used for filter AMI :yield Iterator[str]: AMI IDs """ paginator = self.ec2.get_paginator("describe_snapshots") for page in paginator.paginate(OwnerIds=["self"]): for snapshot in page["Snapshots"]: if snapshot["StartTime"].timestamp() < time_delete: yield snapshot["SnapshotId"]
class NukeEip: """Abstract eip nuke in a class.""" def __init__(self, region_name=None) -> None: """Initialize eip nuke.""" self.ec2 = AwsClient().connect("ec2", region_name) try: self.ec2.describe_addresses() except EndpointConnectionError: print("Eip resource is not available in this aws region") return def nuke(self) -> None: """Eip deleting function. Delete all eip """ for eip in self.list_eips(): try: self.ec2.release_address(AllocationId=eip) print("Nuke elastic ip {0}".format(eip)) except ClientError as exc: nuke_exceptions("eip", eip, exc) def list_eips(self) -> Iterator[str]: """Eip list function. List all eip Id :yield Iterator[str]: Eip Id """ response = self.ec2.describe_addresses() for eip in response["Addresses"]: yield eip["AllocationId"]
class NukeDlm: """Abstract dlm nuke in a class.""" def __init__(self, region_name=None) -> None: """Initialize dlm nuke.""" self.dlm = AwsClient().connect("dlm", region_name) try: self.dlm.get_lifecycle_policies() except EndpointConnectionError: print("Dlm resource is not available in this aws region") return def nuke(self, older_than_seconds: float) -> None: """Dlm policies deleting function. Deleting all dlm policy resources with a timestamp greater than older_than_seconds. :param int older_than_seconds: The timestamp in seconds used from which the aws resource will be deleted """ for policy in self.list_policy(older_than_seconds): try: self.dlm.delete_lifecycle_policy(PolicyId=policy) print("Nuke dlm Lifecycle Policy {0}".format(policy)) except ClientError as exc: nuke_exceptions("dlm policy", policy, exc) def list_policy(self, time_delete: float) -> Iterator[str]: """Data Lifecycle Policies list function. Returns the IDs of all Data Lifecycle Policies with a timestamp lower than time_delete. :param int time_delete: Timestamp in seconds used for filter Data Lifecycle policies :yield Iterator[str]: Data Lifecycle policies IDs """ response = self.dlm.get_lifecycle_policies() for policy in response["Policies"]: detailed = self.dlm.get_lifecycle_policy( PolicyId=policy["PolicyId"]) if detailed["Policy"]["DateCreated"].timestamp() < time_delete: yield policy["PolicyId"]
class NukeEks: """Abstract eks nuke in a class.""" def __init__(self, region_name=None) -> None: """Initialize eks nuke.""" self.eks = AwsClient().connect("eks", region_name) try: self.eks.list_clusters() except EndpointConnectionError: print("eks resource is not available in this aws region") return def nuke(self, older_than_seconds: float) -> None: """EKS cluster deleting function. Deleting all EKS clusters with a timestamp greater than older_than_seconds. :param int older_than_seconds: The timestamp in seconds used from which the aws resource will be deleted """ for cluster in self.list_clusters(older_than_seconds): try: self.eks.delete_cluster(name=cluster) print("Nuke EKS Cluster{0}".format(cluster)) except ClientError as exc: nuke_exceptions("eks cluster", cluster, exc) def list_clusters(self, time_delete: float) -> Iterator[str]: """EKS cluster list function. List the names of all EKS clusters with a timestamp lower than time_delete. :param int time_delete: Timestamp in seconds used for filter EKS clusters :yield Iterator[str]: EKS cluster names """ response = self.eks.list_clusters() for kube in response["clusters"]: k8s = self.eks.describe_cluster(name=kube) if k8s["cluster"]["createdAt"].timestamp() < time_delete: yield kube
def __init__(self, region_name=None) -> None: """Initialize cloudwatch nuke.""" self.cloudwatch = AwsClient().connect("cloudwatch", region_name)