diff --git a/plugins/aws/fix_plugin_aws/resource/ssm.py b/plugins/aws/fix_plugin_aws/resource/ssm.py index a9a4d1074f..38d7446545 100644 --- a/plugins/aws/fix_plugin_aws/resource/ssm.py +++ b/plugins/aws/fix_plugin_aws/resource/ssm.py @@ -360,7 +360,7 @@ class AwsSSMResourceCompliance(AwsResource, PhantomBaseResource): "ssm", "list-resource-compliance-summaries", "ResourceComplianceSummaryItems", - {"Filters": [{"Key": "Status", "Values": ["COMPLIANT"], "Type": "EQUAL"}]}, + {"Filters": [{"Key": "Status", "Values": ["NON_COMPLIANT"], "Type": "EQUAL"}]}, ) mapping: ClassVar[Dict[str, Bender]] = { "id": S("Id"), diff --git a/plugins/aws/test/resources/files/cloudwatch/get-metric-data__2024_05_01_12_00_00_00_00_numberofobjects_aws_s3_bucketname_bucket_1_storagetype_allstoragetypes_average_AWS_S3_NumberOfObjects_BucketName_bucket_1_StorageType_AllStorageTypes_86400_Average_Count_True_bucketsizebytes_a.json b/plugins/aws/test/resources/files/cloudwatch/get-metric-data__2020_05_30_17_45_30_numberofobjects_aws_s3_bucketname_bucket_1_storagetype_allstoragetypes_average_AWS_S3_NumberOfObjects_BucketName_bucket_1_StorageType_AllStorageTypes_86400_Average_Count_True_bucketsizebytes_aws_s3_.json similarity index 60% rename from plugins/aws/test/resources/files/cloudwatch/get-metric-data__2024_05_01_12_00_00_00_00_numberofobjects_aws_s3_bucketname_bucket_1_storagetype_allstoragetypes_average_AWS_S3_NumberOfObjects_BucketName_bucket_1_StorageType_AllStorageTypes_86400_Average_Count_True_bucketsizebytes_a.json rename to plugins/aws/test/resources/files/cloudwatch/get-metric-data__2020_05_30_17_45_30_numberofobjects_aws_s3_bucketname_bucket_1_storagetype_allstoragetypes_average_AWS_S3_NumberOfObjects_BucketName_bucket_1_StorageType_AllStorageTypes_86400_Average_Count_True_bucketsizebytes_aws_s3_.json index 11273b8167..956409f9a4 100644 --- a/plugins/aws/test/resources/files/cloudwatch/get-metric-data__2024_05_01_12_00_00_00_00_numberofobjects_aws_s3_bucketname_bucket_1_storagetype_allstoragetypes_average_AWS_S3_NumberOfObjects_BucketName_bucket_1_StorageType_AllStorageTypes_86400_Average_Count_True_bucketsizebytes_a.json +++ b/plugins/aws/test/resources/files/cloudwatch/get-metric-data__2020_05_30_17_45_30_numberofobjects_aws_s3_bucketname_bucket_1_storagetype_allstoragetypes_average_AWS_S3_NumberOfObjects_BucketName_bucket_1_StorageType_AllStorageTypes_86400_Average_Count_True_bucketsizebytes_aws_s3_.json @@ -3,24 +3,36 @@ { "Id": "bucketsizebytes_aws_s3_bucketname_bucket_1_storagetype_standardstorage_average", "Label": "BucketSizeBytes", - "Timestamps": [ "2024-04-30T12:50:00+00:00" ], - "Values": [ 1 ], + "Timestamps": [ + "2024-04-30T12:50:00+00:00" + ], + "Values": [ + 1 + ], "StatusCode": "Complete" }, { "Id": "bucketsizebytes_aws_s3_bucketname_bucket_1_storagetype_intelligenttieringstorage_average", "Label": "BucketSizeBytes", - "Timestamps": [ "2024-04-30T12:50:00+00:00" ], - "Values": [ 2 ], + "Timestamps": [ + "2024-04-30T12:50:00+00:00" + ], + "Values": [ + 2 + ], "StatusCode": "Complete" }, { "Id": "bucketsizebytes_aws_s3_bucketname_bucket_1_storagetype_standardiastorage_average", "Label": "BucketSizeBytes", - "Timestamps": [ "2024-04-30T12:50:00+00:00" ], - "Values": [ 3 ], + "Timestamps": [ + "2024-04-30T12:50:00+00:00" + ], + "Values": [ + 3 + ], "StatusCode": "Complete" } ], "Messages": [] -} +} \ No newline at end of file diff --git a/plugins/aws/test/resources/files/ssm/list-resource-compliance-summaries__Status_COMPLIANT_EQUAL.json b/plugins/aws/test/resources/files/ssm/list-resource-compliance-summaries__Status_NON_COMPLIANT_EQUAL.json similarity index 100% rename from plugins/aws/test/resources/files/ssm/list-resource-compliance-summaries__Status_COMPLIANT_EQUAL.json rename to plugins/aws/test/resources/files/ssm/list-resource-compliance-summaries__Status_NON_COMPLIANT_EQUAL.json diff --git a/plugins/aws/test/resources/s3_test.py b/plugins/aws/test/resources/s3_test.py index 645a0f4c47..fd9dd264f8 100644 --- a/plugins/aws/test/resources/s3_test.py +++ b/plugins/aws/test/resources/s3_test.py @@ -1,9 +1,14 @@ -from fixlib.graph import Graph -from test.resources import round_trip_for +from concurrent.futures import ThreadPoolExecutor +from datetime import datetime, timedelta from types import SimpleNamespace -from typing import cast, Any, Callable +from typing import cast, Any, Callable, List +from fix_plugin_aws.resource.base import AwsRegion, GraphBuilder +from fix_plugin_aws.resource.cloudwatch import update_resource_metrics, AwsCloudwatchMetricData, AwsCloudwatchQuery from fix_plugin_aws.aws_client import AwsClient from fix_plugin_aws.resource.s3 import AwsS3Bucket, AwsS3AccountSettings +from fixlib.threading import ExecutorQueue +from fixlib.graph import Graph +from test.resources import round_trip_for def test_buckets() -> None: @@ -62,14 +67,42 @@ def validate_delete_args(aws_service: str, fn: Callable[[Any], None]) -> Any: bucket.delete_resource(client, Graph()) -# TODO: fix 'RuntimeError: cannot schedule new futures after shutdown' -# def test_s3_usage_metrics(account_collector: AwsAccountCollector) -> None: -# bucket, builder = round_trip_for(AwsS3Bucket) -# builder.all_regions.update({"us-east-1": AwsRegion(id="us-east-1", name="us-east-1")}) -# account_collector.collect_usage_metrics(builder) -# bucket.complete_graph(builder, {}) -# assert bucket._resource_usage["standard_storage_bucket_size_bytes"]["avg"] == 1.0 -# assert bucket._resource_usage["intelligent_tiering_storage_bucket_size_bytes"]["avg"] == 2.0 -# assert bucket._resource_usage["standard_ia_storage_bucket_size_bytes"]["avg"] == 3.0 -# # This values is computed internally using the other values. If the number does not match, the logic is broken! -# assert bucket._resource_usage["bucket_size_bytes"]["avg"] == 6.0 +def test_s3_usage_metrics() -> None: + bucket, builder = round_trip_for(AwsS3Bucket, "bucket_lifecycle_policy") + builder.all_regions.update({"us-east-1": AwsRegion(id="us-east-1", name="us-east-1")}) + queries = bucket.collect_usage_metrics(builder) + lookup_map = {} + lookup_map[bucket.id] = bucket + + # simulates the `collect_usage_metrics` method found in `AwsAccountCollector`. + def collect_and_set_metrics(start_at: datetime, region: AwsRegion, queries: List[AwsCloudwatchQuery]) -> None: + with ThreadPoolExecutor(max_workers=1) as executor: + queue = ExecutorQueue(executor, tasks_per_key=lambda _: 1, name="test") + g_builder = GraphBuilder( + builder.graph, + builder.cloud, + builder.account, + region, + {region.id: region}, + builder.client, + queue, + builder.core_feedback, + last_run_started_at=builder.last_run_started_at, + ) + result = AwsCloudwatchMetricData.query_for_multiple( + g_builder, start_at, start_at + timedelta(hours=2), queries + ) + update_resource_metrics(lookup_map, result) + # compute bucket_size_bytes + for after_collect in builder.after_collect_actions: + after_collect() + + start = datetime(2020, 5, 30, 15, 45, 30) + + collect_and_set_metrics(start, AwsRegion(id="us-east-1", name="us-east-1"), queries) + + assert bucket._resource_usage["standard_storage_bucket_size_bytes"]["avg"] == 1.0 + assert bucket._resource_usage["intelligent_tiering_storage_bucket_size_bytes"]["avg"] == 2.0 + assert bucket._resource_usage["standard_ia_storage_bucket_size_bytes"]["avg"] == 3.0 + # This values is computed internally using the other values. If the number does not match, the logic is broken! + assert bucket._resource_usage["bucket_size_bytes"]["avg"] == 6.0