fix(cloudtrail): get_trail_status must work with MultiRegion trails (#6687)

This commit is contained in:
Pepe Fagoaga 2023-08-18 10:14:19 +02:00 committed by GitHub
parent 30c5d435a6
commit a36cd89780
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 38 additions and 5 deletions

View File

@ -300,21 +300,22 @@ class CloudTrailBackend(BaseBackend):
def get_trail_status(self, name: str) -> TrailStatus:
if len(name) < 3:
raise TrailNameTooShort(actual_length=len(name))
trail_name = next(
all_trails = self.describe_trails(include_shadow_trails=True)
trail = next(
(
trail.trail_name
for trail in self.trails.values()
trail
for trail in all_trails
if trail.trail_name == name or trail.arn == name
),
None,
)
if not trail_name:
if not trail:
# This particular method returns the ARN as part of the error message
arn = (
f"arn:aws:cloudtrail:{self.region_name}:{self.account_id}:trail/{name}"
)
raise TrailNotFoundException(account_id=self.account_id, name=arn)
trail = self.trails[trail_name]
return trail.status
def describe_trails(self, include_shadow_trails: bool) -> Iterable[Trail]:

View File

@ -319,6 +319,38 @@ def test_get_trail_status_after_starting_and_stopping():
assert "TimeLoggingStopped" in status # .equal("2021-10-13T15:03:21Z")
@mock_cloudtrail
@mock_s3
@mock_sns
def test_get_trail_status_multi_region_not_from_the_home_region():
# CloudTrail client
client_us_east_1 = boto3.client("cloudtrail", region_name="us-east-1")
# Create Trail
_, _, _, trail_name_us_east_1 = create_trail_advanced()
# Start Logging
_ = client_us_east_1.start_logging(Name=trail_name_us_east_1)
# Check Trails in the Home Region us-east-1
trails_us_east_1 = client_us_east_1.describe_trails()["trailList"]
trail_arn_us_east_1 = trails_us_east_1[0]["TrailARN"]
assert len(trails_us_east_1) == 1
# Get Trail status in the Home Region us-east-1
trail_status_us_east_1 = client_us_east_1.get_trail_status(Name=trail_arn_us_east_1)
assert trail_status_us_east_1["IsLogging"]
# Check Trails in another region eu-west-1 for a MultiRegion trail
client_eu_west_1 = boto3.client("cloudtrail", region_name="eu-west-1")
trails_eu_west_1 = client_eu_west_1.describe_trails()["trailList"]
assert len(trails_eu_west_1) == 1
# Get Trail status in another region eu-west-1 for a MultiRegion trail
trail_status_us_east_1 = client_eu_west_1.get_trail_status(Name=trail_arn_us_east_1)
assert trail_status_us_east_1["IsLogging"]
@mock_cloudtrail
@mock_s3
@mock_sns