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): start a new trail with logging off (#2137)
iot-rocket1499df16aa8f
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 7a6bae8c..99f1a8f2 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -7,6 +7,9 @@ Versioning follows [Semantic Versioning](https://semver.org/).## [Unreleased]+### Fixed++- **CloudTrail — new trails start with logging off** — `CreateTrail` left a new trail logging, and `GetTrailStatus` reported a `StartLoggingTime` an hour back and a `LatestDeliveryTime` of now. A new trail now reports `IsLogging` false until `StartLogging`, and `GetTrailStatus` returns only the `StartLoggingTime` and `StopLoggingTime` of calls made (a restart keeps the last stop time), without `LatestDeliveryTime` or empty error strings. Contributed by @iot-rocket.## [1.5.26] — 2026-10-11### Fixeddiff --git a/ministack/services/cloudtrail.py b/ministack/services/cloudtrail.pyindex ea072369..e94bc57c 100644--- a/ministack/services/cloudtrail.py+++ b/ministack/services/cloudtrail.py@@ -462,7 +462,7 @@ def _create_trail(body: dict):"HasCustomEventSelectors": False,"HasInsightSelectors": False,"IsOrganizationTrail": body.get("IsOrganizationTrail", False),- "IsLogging": True,+ "IsLogging": False,}# CreateTrail must persist KmsKeyId so DescribeTrails/GetTrail echo it; otherwise a# CMK set at create is dropped and Terraform's aws_cloudtrail needs a second apply to@@ -553,18 +553,12 @@ def _get_trail_status(body: dict):return _err("CloudTrailARNInvalidException", str(exc))if trail is None:return _err("TrailNotFoundException", f"Unknown trail: {name!r}", 404)- now = int(time.time())- is_logging = bool(trail.get("IsLogging", True))- return _ok(- {- "IsLogging": is_logging,- "LatestDeliveryTime": now if is_logging else trail.get("_StoppedAt", now),- "StartLoggingTime": trail.get("_StartedAt", now - 3600),- "StopLoggingTime": trail.get("_StoppedAt") if not is_logging else None,- "LatestDeliveryError": "",- "LatestNotificationError": "",- }- )+ status = {+ "IsLogging": bool(trail.get("IsLogging", True)),+ "StartLoggingTime": trail.get("_StartedAt"),+ "StopLoggingTime": trail.get("_StoppedAt"),+ }+ return _ok({k: v for k, v in status.items() if v is not None})def _start_logging(body: dict):@@ -576,7 +570,6 @@ def _start_logging(body: dict):return errortrail["IsLogging"] = Truetrail["_StartedAt"] = int(time.time())- trail.pop("_StoppedAt", None)return _ok({})diff --git a/tests/test_cloudtrail.py b/tests/test_cloudtrail.pyindex c8bdc4c9..68687d8e 100644--- a/tests/test_cloudtrail.py+++ b/tests/test_cloudtrail.py@@ -161,7 +161,7 @@ def test_read_trail_by_arn_from_different_request_region(ct):assert get_resp["Trail"]["Name"] == namestatus_resp = west_ct.get_trail_status(Name=arn)- assert status_resp["IsLogging"] is True+ assert status_resp["IsLogging"] is Falsedesc_resp = west_ct.describe_trails(trailNameList=[arn])assert [trail["TrailARN"] for trail in desc_resp["trailList"]] == [arn]@@ -196,7 +196,7 @@ def test_multi_region_trail_is_shadow_visible_from_peer_region(ct):west_ct = _client("cloudtrail", region=WEST_REGION)assert west_ct.get_trail(Name=name)["Trail"]["HomeRegion"] == REGION- assert west_ct.get_trail_status(Name=name)["IsLogging"] is True+ assert west_ct.get_trail_status(Name=name)["IsLogging"] is Falseassert west_ct.describe_trails(trailNameList=[name])["trailList"][0]["TrailARN"] == arnassert any(t["TrailARN"] == arn for t in west_ct.describe_trails()["trailList"])assert any(t["TrailARN"] == arn for t in west_ct.list_trails()["Trails"])@@ -344,7 +344,21 @@ def test_get_trail_status(ct):name = f"trail-status-{_uid()}"ct.create_trail(Name=name, S3BucketName="bucket")resp = ct.get_trail_status(Name=name)- assert resp["IsLogging"] is True+ assert resp["IsLogging"] is False+++def test_new_trail_is_not_logging_until_started(ct):+ name = f"trail-new-{_uid()}"+ ct.create_trail(Name=name, S3BucketName="bucket")+ status = ct.get_trail_status(Name=name)+ status.pop("ResponseMetadata")+ assert status == {"IsLogging": False}++ ct.start_logging(Name=name)+ status = ct.get_trail_status(Name=name)+ status.pop("ResponseMetadata")+ assert status.keys() == {"IsLogging", "StartLoggingTime"}+ assert status["IsLogging"] is Truedef test_get_trail_status_not_found(ct):@@ -365,6 +379,7 @@ def test_start_stop_logging_flips_is_logging_state(ct):aws_cloudtrail data source asserts on this."""name = f"trail-state-{_uid()}"ct.create_trail(Name=name, S3BucketName="bucket")+ ct.start_logging(Name=name)assert ct.get_trail_status(Name=name)["IsLogging"] is Truect.stop_logging(Name=name)@@ -376,6 +391,20 @@ def test_start_stop_logging_flips_is_logging_state(ct):assert ct.get_trail_status(Name=name)["IsLogging"] is True+def test_restarted_trail_keeps_stop_logging_time(ct):+ name = f"trail-restart-{_uid()}"+ ct.create_trail(Name=name, S3BucketName="bucket")+ ct.start_logging(Name=name)+ ct.stop_logging(Name=name)+ stopped = ct.get_trail_status(Name=name)++ ct.start_logging(Name=name)+ status = ct.get_trail_status(Name=name)+ assert status["IsLogging"] is True+ assert status["StopLoggingTime"] == stopped["StopLoggingTime"]+ assert status["StartLoggingTime"] >= stopped["StartLoggingTime"]++def test_list_trails_returns_summaries(ct):name = f"trail-list-{_uid()}"ct.create_trail(Name=name, S3BucketName="bucket")