02 Account Coverage
Query
// REVIEW ONLY workbook query: 📡 1.1 Expected vs. Observed AWS Account & Region Coverage
let Start = {TimeRange:start};
let End = {TimeRange:end};
let AccountFilter = trim(" ", base64_decode_tostring("{AwsAccount:base64}"));
let UseAllAccounts = AccountFilter in ("", "*");
let SafeCloudTrail = union isfuzzy=true
(datatable(TimeGenerated:datetime, AwsEventId:string, EventSource:string, EventName:string, AWSRegion:string,
RecipientAccountId:string, UserIdentityAccountId:string, UserIdentityType:string, UserIdentityArn:string,
UserIdentityAccessKeyId:string, SourceIpAddress:string, UserAgent:string,
ErrorCode:string, ErrorMessage:string, RequestParameters:string, ResponseElements:string, Resources:string,
ManagementEvent:bool, ReadOnly:bool, SessionMfaAuthenticated:string)[]),
(AWSCloudTrail
| where TimeGenerated between (Start .. End)
| project TimeGenerated, AwsEventId, EventSource, EventName, AWSRegion, RecipientAccountId,
UserIdentityAccountId, UserIdentityType, UserIdentityArn,
UserIdentityAccessKeyId, SourceIpAddress, UserAgent, ErrorCode, ErrorMessage,
RequestParameters, ResponseElements, Resources, ManagementEvent, ReadOnly, SessionMfaAuthenticated);
let Expected = union isfuzzy=true
(datatable(SearchKey:string)[]),
(_GetWatchlist('AWS_AccountCoverage'))
| where isnotempty(tostring(column_ifexists("SearchKey", "")))
| project AWSAccountId=tostring(column_ifexists("AWSAccountId", "")),
ExpectedRegions=todynamic(tostring(column_ifexists("ExpectedRegionsJson", "[]"))),
CloudTrailExpected=tostring(column_ifexists("CloudTrailExpected", "")),
S3DataEventsExpected=tostring(column_ifexists("S3DataEventsExpected", "")),
BedrockDataEventsExpected=tostring(column_ifexists("BedrockDataEventsExpected", "")),
LastReviewedUtc=todatetime(column_ifexists("LastReviewedUtc", "")),
CoverageEvidence=tostring(column_ifexists("CoverageEvidence", ""))
| where isnotempty(AWSAccountId) and (UseAllAccounts or AWSAccountId == AccountFilter);
let Observed = SafeCloudTrail
| where UseAllAccounts or RecipientAccountId == AccountFilter or UserIdentityAccountId == AccountFilter
| summarize LastEvent=max(TimeGenerated),
FreshnessMin=round(datetime_diff('second', now(), max(TimeGenerated)) / 60.0, 1),
Events=count(),
DataEvents=countif(ManagementEvent == false),
ObservedRegions=make_set(AWSRegion, 100),
ObservedSources=make_set(EventSource, 100)
by AWSAccountId=RecipientAccountId;
Expected
| join kind=fullouter Observed on AWSAccountId
| extend Account=coalesce(iff(isnotempty(AWSAccountId), AWSAccountId, ""), AWSAccountId1)
| where isnotempty(Account)
| extend InventoryState=iff(isempty(CoverageEvidence), "🟡 Unregistered in AWS_AccountCoverage", "🟢 Declared in AWS_AccountCoverage"),
ObservationState=iff(isnull(LastEvent), "🔴 BLIND SPOT: No CloudTrail events in window", "🟢 ACTIVE: CloudTrail events observed"),
RegionsNotObserved=set_difference(iff(isnotnull(array_length(ExpectedRegions)), ExpectedRegions, dynamic([])), ObservedRegions)
| project Account, ObservationState, InventoryState, LastEvent, FreshnessMin, Events, DataEvents,
ExpectedRegions, ObservedRegions, RegionsNotObserved, ObservedSources,
CloudTrailExpected, S3DataEventsExpected, BedrockDataEventsExpected, LastReviewedUtc
| order by Account asc
| take 5000Explanation
This KQL query is designed to analyze and compare expected versus observed AWS account and region coverage based on CloudTrail events. Here's a simplified breakdown of what the query does:
-
Time Range and Account Filtering:
- It sets a time range (
StartandEnd) and filters for a specific AWS account if provided (AccountFilter). If no specific account is provided, it considers all accounts (UseAllAccounts).
- It sets a time range (
-
Data Collection:
- SafeCloudTrail: It gathers CloudTrail event data within the specified time range, focusing on various event details like event source, region, account IDs, etc.
- Expected: It retrieves expected AWS account and region coverage information from a watchlist named 'AWS_AccountCoverage'. This includes expected regions, CloudTrail expectations, and other metadata.
-
Observed Data:
- It processes the CloudTrail data to summarize the last event time, event count, data event count, and observed regions and sources for each AWS account.
-
Comparison and Analysis:
- The query performs a full outer join between the expected and observed data based on AWS account IDs.
- It extends the results with additional information:
- InventoryState: Indicates if the account is registered in the 'AWS_AccountCoverage' watchlist.
- ObservationState: Shows if CloudTrail events were observed in the specified time window.
- RegionsNotObserved: Lists expected regions that were not observed in the CloudTrail data.
-
Output:
- The final output includes details like account ID, observation state, inventory state, last event time, freshness of data, event counts, expected and observed regions, and other metadata.
- The results are ordered by account ID and limited to 5000 entries.
In summary, this query helps identify discrepancies between expected and actual AWS account and region coverage by analyzing CloudTrail events, highlighting any blind spots or unregistered accounts.