使用 IAM 政策條件進行精細定義存取控制 - Amazon EventBridge

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

使用 IAM 政策條件進行精細定義存取控制

若要授與許可,請使用 IAM 政策語言指定政策生效時間的條件。例如,您可以在特定日期之後套用政策。

政策中的條件由索引鍵/值對所組成。條件索引鍵名稱不區分大小寫。

若您在單一條件中指定多個索引鍵,EventBridge 必須符合所有條件和索引鍵,EventBridge 才能授與權限。若您針對單一索引鍵使用多個值指定單一條件,EventBridge 會在符合其中一個值時授與權限。

您可以在指定條件時使用預留位置或政策變數。如需詳細資訊,請參閱《IAM 使用者指南》https://docs.aws.amazon.com/IAM/latest/UserGuide/policyvariables.html 中的政策變數。如需使用 IAM 政策語言指定條件的詳細資訊,請參閱IAM 使用者指南中的條件

在預設情況下,IAM; 使用者和角色無法存取您帳戶中的事件。若要使用事件,使用者必須獲得 PutRule API 動作的授權。如果您允許 IAM 使用者或角色執行其政策的 events:PutRule 動作,則他們可以建立符合特定事件的規則。但是,為了使規則有用,使用者還必須具有 events:PutTargets 動作的權限,因為如果您希望規則執行的不僅僅是發佈 CloudWatch 指標,則還必須將目標新增至規則。

您可以在 IAM 使用者或角色的政策陳述式中提供條件,讓他們建立僅符合一組特定來源和事件類型的規則。若要授與特定來源和事件類型的存取權,請使用 events:sourceevents:detail-type 條件索引鍵。

類似地,您可以在 IAM 使用者或角色的政策陳述式中提供條件,讓他們在您的帳戶中建立來源。要授予對特定資源的訪問權限,請使用 events:TargetArn 條件鍵。

下列範例是一項政策,可讓使用者使用 PutRule API 動作的拒絕陳述式存取 EventBridge 中的 Amazon EC2 事件以外的所有事件。

{ "Version": "2012-10-17", "Statement": [ { "Sid": "DenyPutRuleForAllEC2Events", "Effect": "Deny", "Action": "events:PutRule", "Resource": "*", "Condition": { "StringEquals": { "events:source": "aws.ec2" } } } ] }

EventBridge 條件鍵

下表顯示您可以在 EventBridge 政策中使用的條件索引鍵以及鍵和值配對。

條件鍵 鍵值對 評估類型

aws:SourceAccount

aws:SourceArn 指定的規則存在的帳戶。

Account Id, Null

aws:SourceArn

傳送事件的規則的 ARN。

ARN, Null

events:creatorAccount

"events:creatorAccount":"creatorAccount"

若為建立帳戶,請針對建立規則的帳戶使用帳戶 ID。使用此條件可針對特定帳戶的規則授權 API 呼叫。

creatorAccount, Null

events:detail-type (事件:詳細資料類型)

"events:detail-type":"detail-type "

其中的 detail-type 是事件的 detail-type 欄位的文字字串,例如 "AWS API Call via CloudTrail""EC2 Instance State-change Notification"

Detail Type, Null

events: detail.eventTypeCode

"events:detail.eventTypeCode":"eventTypeCode"

對於 eventTypeCode,使用 detail.eventTypeCode 欄位的文字字串,例如 "AWS_ABUSE_DOS_REPORT"

eventTypeCode,Null

events: detail.service

"events:detail.service":"service"

對於服務,使用事件的 detail.service 欄位的文字字串,例如 "ABUSE"

服務,Null

events: detail.userIdentity.principalId

"events:detail.userIdentity.principalId":"principal-id"

對於 principal-id 使用 detail-type "AWS API Call via CloudTrail" 事件的 detail.userIdentity.principalId 欄位的文字字串,例如"AROAIDPPEZS35WEXAMPLE:AssumedRoleSessionName."

Principal Id,Null

events:eventBusInvocation

"events:eventBusInvocation":"boolean"

對於布林值,當規則將事件傳送至另一個帳戶中事件匯流排的目標時,請使用 true。使用 PutEvents API 呼叫時使用 false。

eventBusInvocation, Null

events:ManagedBy

由 AWS 服務在內部使用。如果規則是由代表你的 AWS 服務所建立,則該值為建立規則之服務主體之名稱。

不適用於客戶政策。

events:source

"events:source":"source "

使用來源作為事件的來源欄位的文字字串,例如 "aws.ec2""aws.s3"。若要查看更多可能的來源值,請參閱 來自 AWS 服務的事件 中的範例事件。

Source,Null

events:TargetArn

"events:TargetArn":"target-arn "

對於Target arn,請使用規則的目標的 ARN,例如 "arn:aws:lambda:*:*:function:*"

ArrayOfARN, Null

如需 EventBridge 的範例政策陳述式,請參閱 管理對您 Amazon EventBridge 資源的存取許可

EventBridge 管道細節

EventBridge 管道不支援任何其他 IAM 政策條件鍵。

範例:使用 creatorAccount 條件

下列範例策略陳述式顯示如何使用策略中的 creatorAccount 條件,以便僅在指定為的帳號 creatorAccount 是建立規則的帳號時,才允許建立規則。

{ "Version": "2012-10-17", "Statement": [ { "Sid": "AllowPutRuleForOwnedRules", "Effect": "Allow", "Action": "events:PutRule", "Resource": "*", "Condition": { "StringEqualsIfExists": { "events:creatorAccount": "${aws:PrincipalAccount}" } } } ] }

範例:使用 eventBusInvocation 條件

eventBusInvocation 指示調用是否來自跨帳戶目標還是 PutEvents API 要求。如果調用來自包含跨帳戶目標的規則 (例如目標是另一個帳戶中的事件匯流排),則此值為 true。當調用來自 PutEvents API 請求的結果時,該值為 false。下列範例指出跨帳戶目標的調用。

{ "Version": "2012-10-17", "Statement": [ { "Sid": "AllowCrossAccountInvocationEventsOnly", "Effect": "Allow", "Action": "events:PutEvents", "Resource": "*", "Condition": { "BoolIfExists": { "events:eventBusInvocation": "true" } } } ] }

範例:限制存取特定資源

以下範例政策可連接至 IAM; 使用者。政策 A 允許所有事件的 PutRule API 動作,而政策 B 只有在所建立規則的事件模式符合 EC2 事件時,才允許 PutRule

政策 A:允許所有事件

{ "Version": "2012-10-17", "Statement": [ { "Sid": "AllowPutRuleForAllEvents", "Effect": "Allow", "Action": "events:PutRule", "Resource": "*" } ] }

政策 B:允許來自 EC2 的事件

{ "Version": "2012-10-17", "Statement": [ { "Sid": "AllowPutRuleForAllEC2Events", "Effect": "Allow", "Action": "events:PutRule", "Resource": "*", "Condition": { "StringEquals": { "events:source": "aws.ec2" } } } ] }

EventPatternPutRule 的必要引數。因此,如果具有政策 B 的使用者以類似以下事件的模式呼叫 PutRule

{ "source": [ "aws.ec2" ] }

則系統會建立規則,因為政策允許此特定來源,亦即 "aws.ec2"。不過,如果具有政策 B 的使用者以類似以下事件的模式呼叫 PutRule,則會拒絕建立規則,因為政策不允許此特定來源:也就是 "aws.s3"

{ "source": [ "aws.s3" ] }

實際上,擁有政策 B 的使用者僅允許建立符合源自於 Amazon EC2 事件的規則;因此,僅允許這些使用者存取來自 Amazon EC2 的事件。

請參閱下表以取得政策 A 和政策 B 的比較。

事件模式 政策 A 允許 政策 B 允許
{ "source": [ "aws.ec2" ] }

{ "source": [ "aws.ec2", "aws.s3" ] }

否 (不允許 aws.s3 Source)

{ "source": [ "aws.ec2" ], "detail-type": [ "EC2 Instance State-change Notification" ] }

{ "detail-type": [ "EC2 Instance State-change Notification" ] }

否 (必須指定來源)

範例:定義多個可個別用於事件模式的來源

以下政策允許 IAM 使用者或角色建立規則,其中的 EventPattern 來源為 Amazon EC2 或 Amazon ECS。

{ "Version": "2012-10-17", "Statement": [ { "Sid": "AllowPutRuleIfSourceIsEC2OrECS", "Effect": "Allow", "Action": "events:PutRule", "Resource": "*", "Condition": { "StringEquals": { "events:source": [ "aws.ec2", "aws.ecs" ] } } } ] }

下表展示了此政策允許或拒絕的事件模式範例。

事件模式 政策允許
{ "source": [ "aws.ec2" ] }

{ "source": [ "aws.ecs" ] }

{ "source": [ "aws.s3" ] }

{ "source": [ "aws.ec2", "aws.ecs" ] }

{ "detail-type": [ "AWS API Call via CloudTrail" ] }

範例:定義可用於事件模式的來源和 DetailType

以下政策僅允許來自 aws.ec2 來源且 DetailType 等於 EC2 instance state change notification 的事件。

{ "Version": "2012-10-17", "Statement": [ { "Sid": "AllowPutRuleIfSourceIsEC2AndDetailTypeIsInstanceStateChangeNotification", "Effect": "Allow", "Action": "events:PutRule", "Resource": "*", "Condition": { "StringEquals": { "events:source": "aws.ec2", "events:detail-type": "EC2 Instance State-change Notification" } } } ] }

下表展示了此政策允許或拒絕的事件模式範例。

事件模式 政策允許
{ "source": [ "aws.ec2" ] }

{ "source": [ "aws.ecs" ] }

{ "source": [ "aws.ec2" ], "detail-type": [ "EC2 Instance State-change Notification" ] }

{ "source": [ "aws.ec2" ], "detail-type": [ "EC2 Instance Health Failed" ] }

{ "detail-type": [ "EC2 Instance State-change Notification" ] }

範例:確定事件模式中的來源已定義

以下政策允許用戶僅以 EventPatterns (必須具有來源欄位) 建立規則。換言之,IAM 使用者或角色無法以未提供特定來源的 EventPattern 建立規則。

{ "Version": "2012-10-17", "Statement": [ { "Sid": "AllowPutRuleIfSourceIsSpecified", "Effect": "Allow", "Action": "events:PutRule", "Resource": "*", "Condition": { "Null": { "events:source": "false" } } } ] }

下表展示了此政策允許或拒絕的事件模式範例。

事件模式 政策允許
{ "source": [ "aws.ec2" ], "detail-type": [ "EC2 Instance State-change Notification" ] }

{ "source": [ "aws.ecs", "aws.ec2" ] }

{ "detail-type": [ "EC2 Instance State-change Notification" ] }

範例:在具有多個來源的事件模式中定義允許來源清單

以下政策允許以 EventPatterns (可擁有多個來源) 建立規則。事件模式中列出的每個來源都必須是條件中提供的清單成員。當使用 ForAllValues 條件時,請確定條件清單中至少有一個項目已經定義。

{ "Version": "2012-10-17", "Statement": [ { "Sid": "AllowPutRuleIfSourceIsSpecifiedAndIsEitherS3OrEC2OrBoth", "Effect": "Allow", "Action": "events:PutRule", "Resource": "*", "Condition": { "ForAllValues:StringEquals": { "events:source": [ "aws.ec2", "aws.s3" ] }, "Null": { "events:source": "false" } } } ] }

下表展示了此政策允許或拒絕的事件模式範例。

事件模式 政策允許
{ "source": [ "aws.ec2" ] }

{ "source": [ "aws.ec2", "aws.s3" ] }

{ "source": [ "aws.ec2", "aws.autoscaling" ] }

{ "detail-type": [ "EC2 Instance State-change Notification" ] }

範例:限制 PutRule 存取 detail.service

您可以將 IAM 使用者或角色限制為僅為 events:details.service 欄位中具有特定值的事件建立規則。events:details.service 的值並不一定是 AWS 服務的名稱。

此政策條件可協助處理與安全性或濫用相關的 AWS Health 事件。透過此政策條件,您可以限制只有需要的使用者才能夠查看這些敏感提醒的存取權。

例如,以下政策允許僅為 events:details.service 值是 ABUSE 的事件建立規則。

{ "Version": "2012-10-17", "Statement": [ { "Sid": "AllowPutRuleEventsWithDetailServiceEC2", "Effect": "Allow", "Action": "events:PutRule", "Resource": "*", "Condition": { "StringEquals": { "events:detail.service": "ABUSE" } } } ] }

範例:限制 PutRule 存取 detail.eventTypeCode

您可以將 IAM 使用者或角色限制為僅為 events:details.eventTypeCode 欄位中具有特定值的事件建立規則。此政策條件可協助處理與安全性或濫用相關的 AWS Health 事件。透過此政策條件,您可以限制只有需要的使用者才能夠查看這些敏感提醒的存取權。

例如,以下政策允許僅為 events:details.eventTypeCode 值是 AWS_ABUSE_DOS_REPORT 的事件建立規則。

{ "Version": "2012-10-17", "Statement": [ { "Sid": "AllowPutRuleEventsWithDetailServiceEC2", "Effect": "Allow", "Action": "events:PutRule", "Resource": "*", "Condition": { "StringEquals": { "events:detail.eventTypeCode": "AWS_ABUSE_DOS_REPORT" } } } ] }

範例:確保僅允許來自某個特定 PrincipalId API 呼叫的 AWS CloudTrail 事件

所有 AWS CloudTrail 事件都有使用者 ID,該使用者在事件的 detail.userIdentity.principalId 路徑執行 API 呼叫 。利用 events:detail.userIdentity.principalId 條件鍵,您可以限制 IAM 使用者或角色僅能存取來自特定帳戶的 CloudTrail 事件。

"Version": "2012-10-17", "Statement": [ { "Sid": "AllowPutRuleOnlyForCloudTrailEventsWhereUserIsASpecificIAMUser", "Effect": "Allow", "Action": "events:PutRule", "Resource": "*", "Condition": { "StringEquals": { "events:detail-type": [ "AWS API Call via CloudTrail" ], "events:detail.userIdentity.principalId": [ "AIDAJ45Q7YFFAREXAMPLE" ] } } } ] }

下表展示了此政策允許或拒絕的事件模式範例。

事件模式 政策允許
{ "detail-type": [ "AWS API Call via CloudTrail" ] }

{ "detail-type": [ "AWS API Call via CloudTrail" ], "detail.userIdentity.principalId": [ "AIDAJ45Q7YFFAREXAMPLE" ] }

{ "detail-type": [ "AWS API Call via CloudTrail" ], "detail.userIdentity.principalId": [ "AROAIDPPEZS35WEXAMPLE:AssumedRoleSessionName" ] }

範例:限制存取目標

如果 IAM; 使用者或角色具有 events:PutTargets 許可,他們可以將相同帳戶中的任何目標新增至他們允許存取的規則。以下政策限制只能將目標新增至特定規則 (帳戶 123456789012 下的 MyRule)。

{ "Version": "2012-10-17", "Statement": [ { "Sid": "AllowPutTargetsOnASpecificRule", "Effect": "Allow", "Action": "events:PutTargets", "Resource": "arn:aws:events:us-east-1:123456789012:rule/MyRule" } ] }

若要限制哪些目標可以新增至規則,請使用 events:TargetArn 條件鍵。您可以將目標限制為只有 Lambda 函數,如下列範例。

{ "Version": "2012-10-17", "Statement": [ { "Sid": "AllowPutTargetsOnASpecificRuleAndOnlyLambdaFunctions", "Effect": "Allow", "Action": "events:PutTargets", "Resource": "arn:aws:events:us-east-1:123456789012:rule/MyRule", "Condition": { "ArnLike": { "events:TargetArn": "arn:aws:lambda:*:*:function:*" } } } ] }