Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Log Prefix Formation with Multi-Account Organization Support in C… #2815

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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/';
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