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
fix(cloudtrail): return the default event selector for a new trail (#1977)
iot-rocket732624df78e2
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 881dbf1f..fa85cb97 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -9,6 +9,7 @@ Versioning follows [Semantic Versioning](https://semver.org/).### Fixed+- **CloudTrail — `GetEventSelectors` returns the default selector of a new trail** — a trail that never had `PutEventSelectors` answered an empty `EventSelectors` list instead of the default selector that logs all read and write management events. Contributed by @iot-rocket.- **STS — `GetAccessKeyInfo` answers the account that owns the key** — it returned the caller's account whatever `AccessKeyId` said; it now returns the owning account for keys the emulator knows (its root key, IAM access keys and STS sessions), the account encoded in any other `AKIA` or `ASIA` key id as AWS does, and a `ValidationError` for an id that encodes no account or breaks the parameter's length and pattern constraints. Contributed by @iot-rocket.- **STS — `GetAccessKeyInfo` is authorized like other actions** — under `AUTH=true` it was allowed like `GetCallerIdentity` and `GetSessionToken`, whatever the caller's policies said. It now needs an identity policy that allows it and no deny, and otherwise answers `AccessDenied`, as AWS does; the other two stay allowed. Contributed by @iot-rocket.- **API Gateway v2 (WebSocket API) — `$connect` with `AWS_IAM`** — an unsigned handshake is refused with 403 `Missing Authentication Token`, and under `AUTH=true` the caller needs `execute-api:Invoke` on `arn:aws:execute-api:<region>:<account>:<api-id>/<stage>/$connect`. Contributed by @iot-rocket.diff --git a/ministack/services/cloudtrail.py b/ministack/services/cloudtrail.pyindex ce3ffa86..825a3de6 100644--- a/ministack/services/cloudtrail.py+++ b/ministack/services/cloudtrail.py@@ -42,6 +42,10 @@ _trails = AccountRegionScopedDict() # trail_name -> trail_record, scop_event_selectors = AccountRegionScopedDict() # trail_name -> list[EventSelector], scoped to HomeRegion_trail_tags = AccountScopedDict() # trail_arn -> {tag_key: tag_value}+_DEFAULT_EVENT_SELECTORS = [+ {"ReadWriteType": "All", "IncludeManagementEvents": True, "DataResources": [], "ExcludeManagementEventSources": []}+]+_SCRUB_KEYS = frozenset({"secretaccesskey",@@ -680,7 +684,7 @@ def _get_event_selectors(body: dict):return _err("CloudTrailARNInvalidException", str(exc))if trail is None:return _err("TrailNotFoundException", f"Unknown trail: {raw!r}", 404)- selectors = _event_selectors.get_scoped(get_account_id(), home_region, name) or []+ selectors = _event_selectors.get_scoped(get_account_id(), home_region, name, _DEFAULT_EVENT_SELECTORS)return _ok({"TrailARN": trail.get("TrailARN", _trail_arn(name)),diff --git a/tests/test_cloudtrail.py b/tests/test_cloudtrail.pyindex 7408b469..93ac5a41 100644--- a/tests/test_cloudtrail.py+++ b/tests/test_cloudtrail.py@@ -23,6 +23,9 @@ from botocore.exceptions import ClientErrorENDPOINT = os.environ.get("MINISTACK_ENDPOINT", "http://localhost:4566")REGION = "us-east-1"WEST_REGION = "us-west-2"+DEFAULT_EVENT_SELECTORS = [+ {"ReadWriteType": "All", "IncludeManagementEvents": True, "DataResources": [], "ExcludeManagementEventSources": []}+]def _client(service, region=REGION):@@ -503,7 +506,7 @@ def test_put_event_selectors_rejects_foreign_region_trail_arn(ct):ct.put_event_selectors(TrailName=foreign_arn, EventSelectors=selectors)assert exc.value.response["Error"]["Code"] == "TrailNotFoundException"- assert ct.get_event_selectors(TrailName=name)["EventSelectors"] == []+ assert ct.get_event_selectors(TrailName=name)["EventSelectors"] == DEFAULT_EVENT_SELECTORSdef test_get_event_selectors_by_arn_from_different_request_region(ct):@@ -518,11 +521,13 @@ def test_get_event_selectors_by_arn_from_different_request_region(ct):assert resp["EventSelectors"] == selectors-def test_get_event_selectors_empty(ct):[email protected]("region", [REGION, WEST_REGION])+def test_get_event_selectors_defaults_to_all_management_events(ct, region):+ """A trail without PutEventSelectors reports the default selector, also from a peer region."""name = f"trail-nosel-{_uid()}"- ct.create_trail(Name=name, S3BucketName="bucket")- resp = ct.get_event_selectors(TrailName=name)- assert resp["EventSelectors"] == []+ ct.create_trail(Name=name, S3BucketName="bucket", IsMultiRegionTrail=True)+ resp = _client("cloudtrail", region=region).get_event_selectors(TrailName=name)+ assert resp["EventSelectors"] == DEFAULT_EVENT_SELECTORSdef test_add_list_remove_tags(ct):