From 0e6922a4a4382af2bad76c0aef750d8d06e764b4 Mon Sep 17 00:00:00 2001 From: Kevin Park Date: Tue, 9 Nov 2021 14:50:56 -0500 Subject: [PATCH] Cloudtrail `get_trail_status()` also accept ARN in the `Name` parameter (#4544) (#4547) --- moto/cloudtrail/models.py | 12 ++++++++++-- tests/test_cloudtrail/test_cloudtrail.py | 16 ++++++++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/moto/cloudtrail/models.py b/moto/cloudtrail/models.py index 7d27b8524..79602bb86 100644 --- a/moto/cloudtrail/models.py +++ b/moto/cloudtrail/models.py @@ -215,11 +215,19 @@ class CloudTrailBackend(BaseBackend): def get_trail_status(self, name): if len(name) < 3: raise TrailNameTooShort(actual_length=len(name)) - if name not in self.trails: + trail_name = next( + ( + trail.trail_name + for trail in self.trails.values() + if trail.trail_name == name or trail.arn == name + ), + None, + ) + if not trail_name: # This particular method returns the ARN as part of the error message arn = f"arn:aws:cloudtrail:{self.region_name}:{ACCOUNT_ID}:trail/{name}" raise TrailNotFoundException(name=arn) - trail = self.trails[name] + trail = self.trails[trail_name] return trail.status def describe_trails(self, include_shadow_trails): diff --git a/tests/test_cloudtrail/test_cloudtrail.py b/tests/test_cloudtrail/test_cloudtrail.py index f4392a12b..5a29ed874 100644 --- a/tests/test_cloudtrail/test_cloudtrail.py +++ b/tests/test_cloudtrail/test_cloudtrail.py @@ -251,6 +251,22 @@ def test_get_trail_status_inactive(): status.shouldnt.have.key("StartLoggingTime") +@mock_cloudtrail +@mock_s3 +def test_get_trail_status_arn_inactive(): + client = boto3.client("cloudtrail", region_name="us-east-1") + _, resp, _ = create_trail_simple() + status = client.get_trail_status(Name=resp["TrailARN"]) + status.should.have.key("IsLogging").equal(False) + status.should.have.key("LatestDeliveryAttemptTime").equal("") + status.should.have.key("LatestNotificationAttemptTime").equal("") + status.should.have.key("LatestNotificationAttemptSucceeded").equal("") + status.should.have.key("LatestDeliveryAttemptSucceeded").equal("") + status.should.have.key("TimeLoggingStarted").equal("") + status.should.have.key("TimeLoggingStopped").equal("") + status.shouldnt.have.key("StartLoggingTime") + + @mock_cloudtrail @mock_s3 def test_get_trail_status_after_starting():