Files
floci/compatibility-tests/sdk-test-python/tests/test_config.py
T
Sam Fakhreddine 1b3417a7a3 feat(config): compliance evaluation loop, retention, pagination (#2642)
* feat(config): add compliance evaluation loop, retention, and pagination

Replace the hardcoded INSUFFICIENT_DATA compliance with real aggregation
over evaluations reported via PutEvaluations / PutExternalEvaluation:

- New actions (13): PutEvaluations, PutExternalEvaluation,
  DeleteEvaluationResults, DescribeComplianceByResource,
  GetComplianceDetailsByConfigRule, GetComplianceDetailsByResource,
  GetComplianceSummaryByConfigRule, GetComplianceSummaryByResourceType,
  DeleteConfigurationRecorder, DeleteDeliveryChannel,
  PutRetentionConfiguration, DescribeRetentionConfigurations,
  DeleteRetentionConfiguration
- PutConfigRule now round-trips the full AWS rule shape (Description,
  Scope, SourceDetails, InputParameters, MaximumExecutionFrequency,
  EvaluationModes) instead of silently dropping fields
- Evaluations persist to config-evaluations.json and are cascade-deleted
  with their rule; retention persists to config-retention.json
- NextToken/Limit pagination on describe/get operations with AWS page
  sizes and InvalidNextTokenException/InvalidLimitException validation
- DescribeConfigRules with an unknown name now fails with
  NoSuchConfigRuleException, matching AWS
- boto3 compatibility suite: tests/test_config.py

Since Floci performs no Lambda invocation, PutEvaluations accepts the
config rule name as the ResultToken; documented in the service doc's
deviations section.

* fix: honor Limit field in Config pagination requests

* fix(config): enforce the modeled rule enums and the ComplianceTypes filter shape

PutConfigRule now deserializes the whole modeled ConfigRule and stores it, so
Source.Owner, MaximumExecutionFrequency, ConfigRuleState and
EvaluationModes[].Mode were persisted and echoed back verbatim — a rule AWS
would have rejected with InvalidParameterValueException read back as a valid
one.

The ComplianceTypes filter on the four compliance reads was likewise unchecked:
an unmodeled value or a list longer than the shape's max of 3 simply matched
nothing, which a caller reads as an empty result rather than as a rejection.

* fix(config): validate the nested SourceDetails enums and Scope cardinality

The stored ConfigRule carries SourceDetails, whose EventSource (aws.config only),
MessageType and MaximumExecutionFrequency are all enum-typed, and a Scope whose
ComplianceResourceTypes list is capped at 100. All four were persisted and echoed
back by DescribeConfigRules verbatim, one level below the members the previous
commit checked.

* fix(config): reject a missing ConfigRuleName on DeleteConfigRule

store.remove(ruleName) throws NullPointerException on the backing
ConcurrentHashMap when ruleName is null (the handler passes null when
ConfigRuleName is absent from the request), producing a 500 instead of
the modeled NoSuchConfigRuleException. Guard on isBlank(ruleName)
first, matching the existing requireRule() idiom used by the sibling
evaluation operations for the same field.

* fix(config): keep the newest evaluation when PutEvaluations reorders

storeEvaluations() unconditionally overwrote the stored compliance
result for a resource/rule pair, so an older evaluation arriving after
a newer one (e.g. an out-of-order Lambda invocation) regressed
compliance details, aggregates, and summaries back to the stale value.
Compare against the stored OrderingTimestamp and skip the incoming
evaluation when it is older than what is already recorded.

* fix(config): make PutEvaluations atomic per resource/rule key

storeEvaluations() read the current evaluation with a plain get() and
wrote the new one with a separate put(), so two concurrent
PutEvaluations calls for the same resource could both read the old
value before either wrote, letting an older OrderingTimestamp win the
race regardless of arrival order. Replace the read-then-write with
ConcurrentHashMap.compute(), which is atomic per key.

Added a stress test exercising 16 concurrent writers per resource key
across 100 attempts; it passes reliably under the fix. The race window
in the prior get/put shape is narrow enough (a handful of nanoseconds
between the two calls) that it did not reproduce deterministically
under test scheduling even with that much contention -- the fix is
justified by the non-atomic read-then-write shape itself, not by a
forced failure.

* fix(config): enforce DescribeConfigRules' 25-name limit

ConfigRuleNames is modeled with Maximum 25 items (confirmed against
botocore's config service-2.json) but describeConfigRules had no size
check, so 26+ names were processed instead of rejected with
InvalidParameterValueException like every other array-size limit this
PR already enforces.

* fix(config): close the DeleteConfigRule/PutEvaluations deletion race

PutEvaluations (and PutExternalEvaluation, DeleteEvaluationResults)
checked a rule's existence via requireRule and then wrote to the
evaluations map as two separate steps. If DeleteConfigRule ran in
between -- removing the rule and its evaluation bucket -- the pending
PutEvaluations call would recreate the bucket via computeIfAbsent
after the delete, orphaning evaluations under the deleted rule's name.
A later rule created with that same name would then inherit the
deleted rule's compliance details and evaluation status.

Guard the check-then-act with a per-rule monitor (ruleKey(region,
ruleName)), matching the lockFor()-keyed-monitor idiom already used by
DocDbService for the identical class of race between a tag update and
a delete.

Added a stress test racing PutEvaluations against DeleteConfigRule
across 200 attempts, then recreating the rule under the same name and
asserting a clean (INSUFFICIENT_DATA) evaluation state. It passes
under the fix; as with the earlier PutEvaluations-ordering race, it
did not reproduce RED against the pre-fix code even under that much
contention -- the race window between the existence check and the map
write is too narrow to hit deterministically without injecting an
artificial delay into production code. The fix is justified by the
non-atomic check-then-act shape itself, confirmed by reading the code.
2026-08-28 11:58:39 -04:00

292 lines
13 KiB
Python

"""AWS Config integration tests."""
import datetime
import pytest
from botocore.exceptions import ClientError
LAMBDA_SOURCE = "arn:aws:lambda:us-east-1:000000000000:function:compliance-checker"
def _put_custom_rule(config_client, rule_name, **overrides):
"""Create a minimal CUSTOM_LAMBDA config rule."""
rule = {
"ConfigRuleName": rule_name,
"Source": {"Owner": "CUSTOM_LAMBDA", "SourceIdentifier": LAMBDA_SOURCE},
}
rule.update(overrides)
config_client.put_config_rule(ConfigRule=rule)
class TestConfigRuleLifecycle:
"""Test config rule CRUD with a full-shape round-trip."""
def test_put_describe_round_trip(self, config_client, unique_name):
"""Test PutConfigRule stores every field and DescribeConfigRules returns them."""
rule_name = f"rule-{unique_name}"
_put_custom_rule(
config_client,
rule_name,
Description="Checks bucket policies",
Scope={"ComplianceResourceTypes": ["AWS::S3::Bucket"], "TagKey": "env"},
Source={
"Owner": "CUSTOM_LAMBDA",
"SourceIdentifier": LAMBDA_SOURCE,
"SourceDetails": [
{"EventSource": "aws.config", "MessageType": "ConfigurationItemChangeNotification"}
],
},
InputParameters='{"maxPolicySize": 5}',
MaximumExecutionFrequency="Six_Hours",
)
try:
rules = config_client.describe_config_rules(ConfigRuleNames=[rule_name])["ConfigRules"]
assert len(rules) == 1
rule = rules[0]
assert rule["ConfigRuleName"] == rule_name
assert rule["ConfigRuleArn"].startswith("arn:aws:config:")
assert rule["ConfigRuleId"]
assert rule["ConfigRuleState"] == "ACTIVE"
assert rule["Description"] == "Checks bucket policies"
assert rule["Scope"]["ComplianceResourceTypes"] == ["AWS::S3::Bucket"]
assert rule["Scope"]["TagKey"] == "env"
assert rule["Source"]["Owner"] == "CUSTOM_LAMBDA"
assert rule["Source"]["SourceIdentifier"] == LAMBDA_SOURCE
assert rule["Source"]["SourceDetails"][0]["EventSource"] == "aws.config"
assert rule["InputParameters"] == '{"maxPolicySize": 5}'
assert rule["MaximumExecutionFrequency"] == "Six_Hours"
assert rule["EvaluationModes"] == [{"Mode": "DETECTIVE"}]
finally:
config_client.delete_config_rule(ConfigRuleName=rule_name)
def test_describe_unknown_rule_fails(self, config_client, unique_name):
"""Test DescribeConfigRules with an unknown name raises NoSuchConfigRuleException."""
with pytest.raises(ClientError) as exc:
config_client.describe_config_rules(ConfigRuleNames=[f"missing-{unique_name}"])
assert exc.value.response["Error"]["Code"] == "NoSuchConfigRuleException"
def test_delete_unknown_rule_fails(self, config_client, unique_name):
"""Test DeleteConfigRule with an unknown name raises NoSuchConfigRuleException."""
with pytest.raises(ClientError) as exc:
config_client.delete_config_rule(ConfigRuleName=f"missing-{unique_name}")
assert exc.value.response["Error"]["Code"] == "NoSuchConfigRuleException"
class TestComplianceLoop:
"""Test the evaluation-driven compliance loop."""
def test_evaluations_drive_compliance(self, config_client, unique_name):
"""Test PutEvaluations records results that compliance reads aggregate."""
rule_name = f"loop-{unique_name}"
resource_id = f"bucket-{unique_name}"
_put_custom_rule(config_client, rule_name)
try:
# TestMode validates but persists nothing
config_client.put_evaluations(
ResultToken=rule_name,
TestMode=True,
Evaluations=[
{
"ComplianceResourceType": "AWS::S3::Bucket",
"ComplianceResourceId": resource_id,
"ComplianceType": "NON_COMPLIANT",
"OrderingTimestamp": datetime.datetime(2024, 1, 1, tzinfo=datetime.timezone.utc),
}
],
)
compliance = config_client.describe_compliance_by_config_rule(
ConfigRuleNames=[rule_name]
)["ComplianceByConfigRules"][0]["Compliance"]
assert compliance["ComplianceType"] == "INSUFFICIENT_DATA"
# Real evaluations flip the rule to NON_COMPLIANT
response = config_client.put_evaluations(
ResultToken=rule_name,
Evaluations=[
{
"ComplianceResourceType": "AWS::S3::Bucket",
"ComplianceResourceId": resource_id,
"ComplianceType": "NON_COMPLIANT",
"Annotation": "Bucket policy too permissive",
"OrderingTimestamp": datetime.datetime(2024, 1, 1, tzinfo=datetime.timezone.utc),
}
],
)
assert response["FailedEvaluations"] == []
compliance = config_client.describe_compliance_by_config_rule(
ConfigRuleNames=[rule_name]
)["ComplianceByConfigRules"][0]["Compliance"]
assert compliance["ComplianceType"] == "NON_COMPLIANT"
assert compliance["ComplianceContributorCount"]["CappedCount"] == 1
assert compliance["ComplianceContributorCount"]["CapExceeded"] is False
details = config_client.get_compliance_details_by_config_rule(
ConfigRuleName=rule_name
)["EvaluationResults"]
assert len(details) == 1
qualifier = details[0]["EvaluationResultIdentifier"]["EvaluationResultQualifier"]
assert qualifier["ConfigRuleName"] == rule_name
assert qualifier["ResourceType"] == "AWS::S3::Bucket"
assert qualifier["ResourceId"] == resource_id
assert details[0]["ComplianceType"] == "NON_COMPLIANT"
assert details[0]["Annotation"] == "Bucket policy too permissive"
assert isinstance(details[0]["ResultRecordedTime"], datetime.datetime)
by_resource = config_client.get_compliance_details_by_resource(
ResourceType="AWS::S3::Bucket", ResourceId=resource_id
)["EvaluationResults"]
assert len(by_resource) == 1
assert (
by_resource[0]["EvaluationResultIdentifier"]["EvaluationResultQualifier"]["ConfigRuleName"]
== rule_name
)
resources = config_client.describe_compliance_by_resource(
ResourceType="AWS::S3::Bucket", ResourceId=resource_id
)["ComplianceByResources"]
assert len(resources) == 1
assert resources[0]["Compliance"]["ComplianceType"] == "NON_COMPLIANT"
summary = config_client.get_compliance_summary_by_config_rule()["ComplianceSummary"]
assert summary["NonCompliantResourceCount"]["CappedCount"] >= 1
assert isinstance(summary["ComplianceSummaryTimestamp"], datetime.datetime)
by_type = config_client.get_compliance_summary_by_resource_type(
ResourceTypes=["AWS::S3::Bucket"]
)["ComplianceSummariesByResourceType"]
assert len(by_type) == 1
assert by_type[0]["ResourceType"] == "AWS::S3::Bucket"
assert by_type[0]["ComplianceSummary"]["NonCompliantResourceCount"]["CappedCount"] >= 1
# DeleteEvaluationResults returns the rule to INSUFFICIENT_DATA
config_client.delete_evaluation_results(ConfigRuleName=rule_name)
compliance = config_client.describe_compliance_by_config_rule(
ConfigRuleNames=[rule_name]
)["ComplianceByConfigRules"][0]["Compliance"]
assert compliance["ComplianceType"] == "INSUFFICIENT_DATA"
finally:
config_client.delete_config_rule(ConfigRuleName=rule_name)
def test_put_external_evaluation(self, config_client, unique_name):
"""Test PutExternalEvaluation records a result for the named rule."""
rule_name = f"external-{unique_name}"
_put_custom_rule(config_client, rule_name)
try:
config_client.put_external_evaluation(
ConfigRuleName=rule_name,
ExternalEvaluation={
"ComplianceResourceType": "AWS::EC2::Instance",
"ComplianceResourceId": f"i-{unique_name}",
"ComplianceType": "COMPLIANT",
"OrderingTimestamp": datetime.datetime(2024, 1, 1, tzinfo=datetime.timezone.utc),
},
)
compliance = config_client.describe_compliance_by_config_rule(
ConfigRuleNames=[rule_name]
)["ComplianceByConfigRules"][0]["Compliance"]
assert compliance["ComplianceType"] == "COMPLIANT"
finally:
config_client.delete_config_rule(ConfigRuleName=rule_name)
def test_put_evaluations_error_cases(self, config_client, unique_name):
"""Test PutEvaluations validation errors surface AWS error codes."""
evaluation = {
"ComplianceResourceType": "AWS::S3::Bucket",
"ComplianceResourceId": f"bucket-{unique_name}",
"ComplianceType": "COMPLIANT",
"OrderingTimestamp": datetime.datetime(2024, 1, 1, tzinfo=datetime.timezone.utc),
}
with pytest.raises(ClientError) as exc:
config_client.put_evaluations(ResultToken="", Evaluations=[evaluation])
assert exc.value.response["Error"]["Code"] == "InvalidResultTokenException"
with pytest.raises(ClientError) as exc:
config_client.put_evaluations(
ResultToken=f"missing-{unique_name}", Evaluations=[evaluation]
)
assert exc.value.response["Error"]["Code"] == "NoSuchConfigRuleException"
class TestRecorderChannelLifecycle:
"""Test configuration recorder and delivery channel lifecycle."""
def test_recorder_and_channel_lifecycle(self, config_client):
"""Test recorder/channel setup, the guarded channel delete, and both deletes."""
config_client.put_configuration_recorder(
ConfigurationRecorder={
"name": "default",
"roleARN": "arn:aws:iam::000000000000:role/config-role",
"recordingGroup": {"allSupported": True},
}
)
try:
config_client.put_delivery_channel(
DeliveryChannel={"name": "default", "s3BucketName": "config-bucket"}
)
config_client.start_configuration_recorder(ConfigurationRecorderName="default")
with pytest.raises(ClientError) as exc:
config_client.delete_delivery_channel(DeliveryChannelName="default")
assert exc.value.response["Error"]["Code"] == "LastDeliveryChannelDeleteFailedException"
config_client.stop_configuration_recorder(ConfigurationRecorderName="default")
config_client.delete_delivery_channel(DeliveryChannelName="default")
with pytest.raises(ClientError) as exc:
config_client.delete_delivery_channel(DeliveryChannelName="default")
assert exc.value.response["Error"]["Code"] == "NoSuchDeliveryChannelException"
finally:
config_client.delete_configuration_recorder(ConfigurationRecorderName="default")
assert config_client.describe_configuration_recorders()["ConfigurationRecorders"] == []
class TestRetention:
"""Test retention configuration lifecycle."""
def test_retention_lifecycle(self, config_client):
"""Test put/describe/delete retention configuration."""
retention = config_client.put_retention_configuration(RetentionPeriodInDays=365)
assert retention["RetentionConfiguration"]["Name"] == "default"
assert retention["RetentionConfiguration"]["RetentionPeriodInDays"] == 365
try:
configurations = config_client.describe_retention_configurations()["RetentionConfigurations"]
assert len(configurations) == 1
assert configurations[0]["RetentionPeriodInDays"] == 365
finally:
config_client.delete_retention_configuration(RetentionConfigurationName="default")
assert config_client.describe_retention_configurations()["RetentionConfigurations"] == []
class TestPagination:
"""Test NextToken pagination through the boto3 paginator."""
def test_describe_config_rules_paginates(self, config_client, unique_name):
"""Test the boto3 paginator walks every page of DescribeConfigRules."""
rule_names = [f"pg-{unique_name}-{i:03d}" for i in range(30)]
for rule_name in rule_names:
_put_custom_rule(config_client, rule_name)
try:
first_page = config_client.describe_config_rules()
assert len(first_page["ConfigRules"]) == 25
assert first_page["NextToken"]
collected = []
paginator = config_client.get_paginator("describe_config_rules")
for page in paginator.paginate():
collected.extend(rule["ConfigRuleName"] for rule in page["ConfigRules"])
for rule_name in rule_names:
assert rule_name in collected
finally:
for rule_name in rule_names:
config_client.delete_config_rule(ConfigRuleName=rule_name)