Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[
{
"type": "enhancement",
"category": "CloudTrail",
"description": "Fix Log Prefix Formation with Multi-Account Organization Support in CloudTrail"
}
]
8 changes: 7 additions & 1 deletion src/CloudTrail/LogFileIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class LogFileIterator extends \IteratorIterator
{
// For internal use
const DEFAULT_TRAIL_NAME = 'Default';
const PREFIX_TEMPLATE = 'prefix/AWSLogs/account/CloudTrail/region/date/';
const PREFIX_TEMPLATE = 'prefix/AWSLogs/organization/account/CloudTrail/region/date/';

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might be a breaking change. Is there a way to tell if 'Enable for all accounts in my organization' is enabled first?

const PREFIX_WILDCARD = '*';

// Option names used internally or externally
Expand All @@ -30,6 +30,7 @@ class LogFileIterator extends \IteratorIterator
const END_DATE = 'end_date';
const ACCOUNT_ID = 'account_id';
const LOG_REGION = 'log_region';
const ORGANIZATION_ID = 'organization_id';

/** @var S3Client S3 client used to perform ListObjects operations */
private $s3Client;
Expand Down Expand Up @@ -163,6 +164,9 @@ private function buildListObjectsIterator(array $options)
'prefix' => isset($options[self::KEY_PREFIX])
? $options[self::KEY_PREFIX]
: null,
'organization' => isset($options[self::ORGANIZATION_ID])
? $options[self::ORGANIZATION_ID]
: null,
'account' => isset($options[self::ACCOUNT_ID])
? $options[self::ACCOUNT_ID]
: self::PREFIX_WILDCARD,
Expand All @@ -175,6 +179,8 @@ private function buildListObjectsIterator(array $options)
// Determine the longest key prefix that can be used to retrieve all
// of the relevant log files.
$candidatePrefix = ltrim(strtr(self::PREFIX_TEMPLATE, $parts), '/');
// Normalize the key prefix to remove double slashes
$candidatePrefix = preg_replace('#/{2,}#', '/', $candidatePrefix);
$logKeyPrefix = $candidatePrefix;
$index = strpos($candidatePrefix, self::PREFIX_WILDCARD);

Expand Down