Ministack: Free, open-source local AWS emulator - 60+ services, Terraform compatible, real databases. Free forever. MIT licensed.
Python awsaws-emulatoraws-localaws-sdkdevtoolsdockerdynamodbec2emulatorlambdalocalstacklocalstack-alternativeministackmock-awsopen-sourcepythons3sqsterraform
feat(logs): add CloudWatch Logs delivery destination policy (#1838)
Fábio André Ramos Rodrigues7eea84af2c83
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 72e3e62f..cafd412e 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -9,6 +9,7 @@ Versioning follows [Semantic Versioning](https://semver.org/).### Added+- **CloudWatch Logs — delivery destination policy** — `PutDeliveryDestinationPolicy`, `GetDeliveryDestinationPolicy`, `DeleteDeliveryDestinationPolicy`. Required by an operator that authorizes `delivery.logs.amazonaws.com` to create deliveries to a `DeliveryDestination` (e.g. CloudFront access-log delivery to a log group) — `PutDeliveryDestinationPolicy` previously had no route, blocking the delivery from ever being created. Contributed by @fabio-andre-rodrigues.- **S3 — object annotations** — `PutObjectAnnotation`, `GetObjectAnnotation`, `ListObjectAnnotations` (prefix, `max-annotation-results`, continuation tokens) and `DeleteObjectAnnotation`: named UTF-8 payloads of 1 byte to 1 MiB, up to 1,000 per object version, with S3's naming rules (`InvalidAnnotationName`, `AnnotationNameTooLong`), `AnnotationLimitExceeded`, `UnsupportedMediaType` and `x-amz-object-if-match`. Each belongs to one object version: a new version starts with none, deleting a version deletes its annotations, a delete marker keeps them, and an unversioned overwrite or delete drops them; writing one changes neither the object's ETag nor its version. `CopyObject` carries them unless `x-amz-object-annotation-directive: EXCLUDE`, and recomputes their checksums when the copy names a new algorithm. Payload checksums are verified (`BadDigest`) and default to CRC64NVME; CRC32, CRC32C, CRC64NVME, SHA1, SHA256, SHA512, MD5 and XXHASH64 are computed, while XXHASH3 and XXHASH128 are refused rather than stored unverified. Annotations take the object's encryption and are refused on SSE-C objects, replicate with the object (`COMPLETED` / `REPLICA`), respect Object Lock on every write (governance retention needs `x-amz-bypass-governance-retention` to put, overwrite or delete one; compliance retention and a legal hold refuse all three, bypass or not, each as 403 `AccessDenied` naming retention or the legal hold, as observed on AWS), persist per version with `S3_PERSIST=1`, and send `s3:ObjectAnnotation:Put` / `s3:ObjectAnnotation:Delete` events with the record's `objectAnnotation` block; a `CopyObject` event reports `hasObjectAnnotation`. Previously a `PUT ?annotation` fell through to `PutObject` and replaced the object with the payload. The S3 Metadata annotation table (`UpdateBucketMetadataAnnotationTableConfiguration`) is not included: MiniStack has no S3 Metadata configuration for it to belong to. Checked against the S3 API Reference, the S3 User Guide's "Annotating your objects" page and the botocore model; the Object Lock behaviour was validated against AWS (ap-southeast-2).### Fixeddiff --git a/ministack/services/cloudwatch_logs.py b/ministack/services/cloudwatch_logs.pyindex 433d3e71..e7030a91 100644--- a/ministack/services/cloudwatch_logs.py+++ b/ministack/services/cloudwatch_logs.py@@ -17,6 +17,7 @@ Supports: CreateLogGroup, DeleteLogGroup, DescribeLogGroups,StartQuery, GetQueryResults, StopQuery,PutDeliverySource, GetDeliverySource, DeleteDeliverySource, DescribeDeliverySources,PutDeliveryDestination, GetDeliveryDestination, DeleteDeliveryDestination, DescribeDeliveryDestinations,+ PutDeliveryDestinationPolicy, GetDeliveryDestinationPolicy, DeleteDeliveryDestinationPolicy,CreateDelivery, GetDelivery, DeleteDelivery, DescribeDeliveries."""@@ -321,6 +322,9 @@ async def handle_request(method, path, headers, body, query_params):"GetDeliveryDestination": _get_delivery_destination,"DeleteDeliveryDestination": _delete_delivery_destination,"DescribeDeliveryDestinations": _describe_delivery_destinations,+ "PutDeliveryDestinationPolicy": _put_delivery_destination_policy,+ "GetDeliveryDestinationPolicy": _get_delivery_destination_policy,+ "DeleteDeliveryDestinationPolicy": _delete_delivery_destination_policy,"CreateDelivery": _create_delivery,"GetDelivery": _get_delivery,"DeleteDelivery": _delete_delivery,@@ -2315,6 +2319,47 @@ def _describe_delivery_destinations(data):return json_response({"deliveryDestinations": dests})+def _put_delivery_destination_policy(data):+ name = data.get("deliveryDestinationName")+ policy = data.get("deliveryDestinationPolicy")+ if not name or not policy:+ return error_response_json(+ "ValidationException",+ "deliveryDestinationName and deliveryDestinationPolicy are required.", 400,+ )+ dest = _delivery_destinations.get(name)+ if not dest:+ return error_response_json(+ "ResourceNotFoundException",+ f"Delivery destination does not exist: {name}", 400,+ )+ dest["policy"] = policy+ return json_response({"policy": {"deliveryDestinationPolicy": policy}})+++def _get_delivery_destination_policy(data):+ name = data.get("deliveryDestinationName")+ dest = _delivery_destinations.get(name)+ if not dest or not dest.get("policy"):+ return error_response_json(+ "ResourceNotFoundException",+ f"Delivery destination policy does not exist: {name}", 400,+ )+ return json_response({"policy": {"deliveryDestinationPolicy": dest["policy"]}})+++def _delete_delivery_destination_policy(data):+ name = data.get("deliveryDestinationName")+ dest = _delivery_destinations.get(name)+ if not dest or not dest.get("policy"):+ return error_response_json(+ "ResourceNotFoundException",+ f"Delivery destination policy does not exist: {name}", 400,+ )+ del dest["policy"]+ return json_response({})++def _create_delivery(data):source_name = data.get("deliverySourceName")dest_arn = data.get("deliveryDestinationArn")diff --git a/tests/test_cloudwatch_logs.py b/tests/test_cloudwatch_logs.pyindex b62d3f84..dcc97d6f 100644--- a/tests/test_cloudwatch_logs.py+++ b/tests/test_cloudwatch_logs.py@@ -1108,6 +1108,61 @@ def test_logs_delivery_destination_crud(logs):logs.delete_delivery_destination(name=dest_name)+def test_logs_delivery_destination_policy_crud(logs):+ """Put/Get/Delete round-trip for a delivery destination policy."""+ uid = _uuid_mod.uuid4().hex[:8]+ dest_name = f"intg-dest-{uid}"+ dest_resource_arn = f"arn:aws:logs:us-east-1:000000000000:log-group:/intg/delivery-{uid}:*"++ dest_resp = logs.put_delivery_destination(+ name=dest_name,+ deliveryDestinationConfiguration={+ "destinationResourceArn": dest_resource_arn,+ },+ )+ dest_arn = dest_resp["deliveryDestination"]["arn"]+ policy = json.dumps(+ {+ "Version": "2012-10-17",+ "Statement": [+ {+ "Sid": "AllowCWLDelivery",+ "Effect": "Allow",+ "Principal": {"Service": "delivery.logs.amazonaws.com"},+ "Action": "logs:CreateDelivery",+ "Resource": dest_arn,+ }+ ],+ }+ )++ put_resp = logs.put_delivery_destination_policy(+ deliveryDestinationName=dest_name,+ deliveryDestinationPolicy=policy,+ )+ assert put_resp["policy"]["deliveryDestinationPolicy"] == policy++ get_resp = logs.get_delivery_destination_policy(deliveryDestinationName=dest_name)+ assert get_resp["policy"]["deliveryDestinationPolicy"] == policy++ logs.delete_delivery_destination_policy(deliveryDestinationName=dest_name)+ with pytest.raises(ClientError) as exc:+ logs.get_delivery_destination_policy(deliveryDestinationName=dest_name)+ assert exc.value.response["Error"]["Code"] == "ResourceNotFoundException"++ logs.delete_delivery_destination(name=dest_name)+++def test_logs_delivery_destination_policy_requires_existing_destination(logs):+ """PutDeliveryDestinationPolicy on an unknown destination is ResourceNotFoundException."""+ with pytest.raises(ClientError) as exc:+ logs.put_delivery_destination_policy(+ deliveryDestinationName="does-not-exist",+ deliveryDestinationPolicy="{}",+ )+ assert exc.value.response["Error"]["Code"] == "ResourceNotFoundException"++def test_logs_delivery_create_binds_source_and_destination(logs):"""CreateDelivery wires a source to a destination and returns a delivery id/ARN."""uid = _uuid_mod.uuid4().hex[:8]