CloudTrail Trail の Advanced Event Selectors で Management イベントを絞り込める Field は何か実機検証した
はじめに
「CloudTrail → CWL → Firehose + Lambda で S3 に保存する構成で、Lambda なしでも AwsConsoleSignIn イベントだけを選別できないか?」という疑問が湧いた。
Advanced Event Selectors を使えば Trail レベルでイベントを絞り込めるはずと思い、実機で試した。結論から言うと Management イベントに対して使える Field は想像よりはるかに少なかった。
検証環境のセットアップ
Advanced Event Selectors は既存の Trail に対して put-event-selectors で設定する。Trail がないと試せないため、まず最小構成で作成する。
ACCOUNT_ID=$(aws sts get-caller-identity --query Account --output text)
BUCKET="cloudtrail-test-tmp-${ACCOUNT_ID}"
# S3 バケット作成
aws s3api create-bucket \
--bucket "$BUCKET" \
--region ap-northeast-1 \
--create-bucket-configuration LocationConstraint=ap-northeast-1
# バケットポリシー(CloudTrail に書き込みを許可)
aws s3api put-bucket-policy --bucket "$BUCKET" --policy "{
\"Version\": \"2012-10-17\",
\"Statement\": [
{
\"Effect\": \"Allow\",
\"Principal\": {\"Service\": \"cloudtrail.amazonaws.com\"},
\"Action\": \"s3:GetBucketAcl\",
\"Resource\": \"arn:aws:s3:::${BUCKET}\"
},
{
\"Effect\": \"Allow\",
\"Principal\": {\"Service\": \"cloudtrail.amazonaws.com\"},
\"Action\": \"s3:PutObject\",
\"Resource\": \"arn:aws:s3:::${BUCKET}/AWSLogs/${ACCOUNT_ID}/*\",
\"Condition\": {
\"StringEquals\": {\"s3:x-amz-acl\": \"bucket-owner-full-control\"}
}
}
]
}"
# Trail 作成(ロギングは有効化しない)
aws cloudtrail create-trail \
--name test-selector-trail \
--s3-bucket-name "$BUCKET"
試験①: eventType = AwsConsoleSignIn
AwsConsoleSignIn イベントは eventType フィールドに "AwsConsoleSignIn" を持つ。これで絞り込めれば一番シンプルだ。
aws cloudtrail put-event-selectors \
--trail-name test-selector-trail \
--advanced-event-selectors '[{
"Name": "test-eventType",
"FieldSelectors": [
{"Field": "eventCategory", "Equals": ["Management"]},
{"Field": "eventType", "Equals": ["AwsConsoleSignIn"]}
]
}]'
結果:
An error occurred (InvalidEventSelectorsException):
The following field is not allowed when the eventCategory
field value equals Management: eventType.
❌ eventType は Management イベントでは使えない。
試験②: eventName = ConsoleLogin
AwsConsoleSignIn イベントの eventName は "ConsoleLogin"。eventType がダメなら eventName で絞り込めるか試した。
aws cloudtrail put-event-selectors \
--trail-name test-selector-trail \
--advanced-event-selectors '[{
"Name": "test-eventName",
"FieldSelectors": [
{"Field": "eventCategory", "Equals": ["Management"]},
{"Field": "eventName", "Equals": ["ConsoleLogin"]}
]
}]'
結果:
An error occurred (InvalidEventSelectorsException):
The following field is not allowed when the eventCategory
field value equals Management: eventName.
❌ eventName も Management イベントでは使えない。
試験③: eventSource = signin.amazonaws.com(Equals)
AwsConsoleSignIn イベントの eventSource は "signin.amazonaws.com"。Equals で絞り込めるか試した。
aws cloudtrail put-event-selectors \
--trail-name test-selector-trail \
--advanced-event-selectors '[{
"Name": "test-eventSource-eq",
"FieldSelectors": [
{"Field": "eventCategory", "Equals": ["Management"]},
{"Field": "eventSource", "Equals": ["signin.amazonaws.com"]}
]
}]'
結果:
An error occurred (InvalidEventSelectorsException):
The specified eventSource selectors are not supported
when eventCategory is set to "Equals": "Management".
❌ Equals での指定は拒否される。
試験④: eventSource = signin.amazonaws.com(NotEquals)
Equals がダメなら NotEquals(除外)ならどうか試した。
aws cloudtrail put-event-selectors \
--trail-name test-selector-trail \
--advanced-event-selectors '[{
"Name": "test-eventSource-neq",
"FieldSelectors": [
{"Field": "eventCategory", "Equals": ["Management"]},
{"Field": "eventSource", "NotEquals": ["signin.amazonaws.com"]}
]
}]'
結果:
An error occurred (InvalidEventSelectorsException):
The specified eventSource selectors are not supported
when eventCategory is set to "Equals": "Management".
❌ NotEquals でも同じエラー。signin.amazonaws.com は Equals・NotEquals どちらも弾かれる。
試験⑤: eventSource = kms.amazonaws.com(NotEquals)
AWS ドキュメントに「KMS と RDS Data API の除外のみ可能」と記載されていたため、正規ケースで動作確認した。
aws cloudtrail put-event-selectors \
--trail-name test-selector-trail \
--advanced-event-selectors '[{
"Name": "test-eventSource-kms",
"FieldSelectors": [
{"Field": "eventCategory", "Equals": ["Management"]},
{"Field": "eventSource", "NotEquals": ["kms.amazonaws.com"]}
]
}]'
結果:
{
"TrailARN": "arn:aws:cloudtrail:ap-northeast-1:xxxx:trail/test-selector-trail",
"AdvancedEventSelectors": [{
"Name": "test-eventSource-kms",
"FieldSelectors": [
{"Field": "eventCategory", "Equals": ["Management"]},
{"Field": "eventSource", "NotEquals": ["kms.amazonaws.com"]}
]
}]
}
✅ 成功。kms.amazonaws.com の除外だけは通る。
試験⑥: readOnly(成功ケース)
readOnly は唯一自由に使える絞り込みフィールド。書き込みイベントのみ・読み取りイベントのみに絞れる。
# 書き込みイベントのみ(ConsoleLogin は false)
aws cloudtrail put-event-selectors \
--trail-name test-selector-trail \
--advanced-event-selectors '[{
"Name": "WriteOnly",
"FieldSelectors": [
{"Field": "eventCategory", "Equals": ["Management"]},
{"Field": "readOnly", "Equals": ["false"]}
]
}]'
結果:
{
"AdvancedEventSelectors": [{
"Name": "WriteOnly",
"FieldSelectors": [
{"Field": "eventCategory", "Equals": ["Management"]},
{"Field": "readOnly", "Equals": ["false"]}
]
}]
}
✅ 成功。ただし readOnly: false(書き込み)には AwsConsoleSignIn 以外の多くのイベントも含まれるため、AwsConsoleSignIn だけの絞り込みには使えない。
クリーンアップ
aws cloudtrail delete-trail --name test-selector-trail
aws s3 rm "s3://${BUCKET}" --recursive
aws s3api delete-bucket --bucket "$BUCKET"
結果まとめ
Trail の Management イベントに対して put-event-selectors で使える Field の実機確認結果:
| Field | 操作 | 値 | 結果 |
|---|---|---|---|
eventCategory |
Equals | Management | ✅ 必須・成功 |
readOnly |
Equals | true / false | ✅ 成功(未掲載) |
eventType |
Equals | AwsConsoleSignIn | ❌ エラー |
eventName |
Equals | ConsoleLogin | ❌ エラー |
eventSource |
Equals | signin.amazonaws.com | ❌ エラー |
eventSource |
NotEquals | signin.amazonaws.com | ❌ エラー |
eventSource |
NotEquals | kms.amazonaws.com | ✅ 成功 |
eventSource |
NotEquals | rdsdata.amazonaws.com | ✅ 成功(ドキュメント記載) |
Management イベントで eventSource に指定できるのは kms.amazonaws.com と rdsdata.amazonaws.com の除外のみ。それ以外の値は Equals・NotEquals を問わずエラーになる。
なぜこの制約があるのか
AWS ドキュメント(AdvancedFieldSelector API Reference)に明記されている:
For management events for trails, this is an optional field that can be set to
NotEqualskms.amazonaws.comto exclude KMS management events, orNotEqualsrdsdata.amazonaws.comto exclude RDS management events.
Trail の Management イベントに対して細かいフィルタリングが許可されていない理由は推測になるが、Management イベントの監査ログとしての完全性を保つ設計思想が背景にあると考えられる。細かく絞り込みすぎると、意図せず重要なイベントを記録し損ねるリスクがある。
Trail と Event Data Store の違い
eventType・eventName・eventSource(自由な値)が使えるのは Event Data Store(CloudTrail Lake)のみ。
# Event Data Store では eventType で絞り込める
aws cloudtrail create-event-data-store \
--name signin-only-store \
--advanced-event-selectors '[{
"Name": "AwsConsoleSignIn only",
"FieldSelectors": [
{"Field": "eventCategory", "Equals": ["Management"]},
{"Field": "eventType", "Equals": ["AwsConsoleSignIn"]}
]
}]'
ただし Event Data Store の出力先は CloudTrail Lake(SQL クエリ用)であり、Trail のように S3 に直接 JSON 形式で書き出す用途には使えない。
結論
CloudTrail Trail の Advanced Event Selectors で AwsConsoleSignIn イベントだけを選別することはできない。
AwsConsoleSignIn と他のイベントを S3 の別フォルダに振り分けたい場合は、Trail から CloudWatch Logs に転送し、サブスクリプションフィルタ({ $.eventType = "AwsConsoleSignIn" })で振り分ける構成が必要になる。