IAM 评估逻辑如何处理具有多个条件键的显式 Deny 策略?
2 分钟阅读
0
我想创建一个 AWS Identity and Access Management(IAM)显式 Deny 策略。此 Deny 策略必须限制创建 Amazon Elastic Compute Cloud(Amazon EC2)实例和 Amazon Elastic Block Store(Amazon EBS)卷。
简短描述
使用 IAM 策略标签,限制启动具有 Allow 带上 StringLike 或 Deny 带上 StringNotLike 字符串条件操作符的 EC2 实例和 EBS 卷。
有关更多信息,请参阅如何使用 IAM policy 标签来限定 EC2 实例或 EBS 卷的创建方式?
解决方法
要限制 EC2 实例和 EBS 卷的创建,请使用以下使用 Deny 带上 StringNotLike 的 IAM 策略示例。
**注意:**最佳实践是使用 Deny 带上 StringNotLike 来防止意外访问权限。
如果您的策略有多个条件操作符或将多个键附加到单个条件操作符,则会使用 AND 逻辑对这些条件进行评估。使用 Deny 多个标签值时,每个 RequestTag 键必须使用单独的语句才能获得相同的 AND 逻辑。
**注意:**使用 Deny 策略时,如果在一个条件下设置所有 RequestTag 键值,则可能无法按预期工作。这是因为除非所有条件均已满足,否则该操作是允许的。所有条件都满足后,该操作将被拒绝。
以下标签是必须的:
- cost_center 标签必须具有一个非空值。
- EC2 实例应该有一个名为 Production 的标签键。
- identifier 标签必须是任意五个字符的组合。
- env 标签值必须是 sandbox、dev 或 prod。
策略示例:
{ "Version": "2012-10-17", "Statement": [ { "Sid": "AllowToDescribeAll", "Effect": "Allow", "Action": [ "ec2:Describe*" ], "Resource": "*" }, { "Sid": "AllowRunInstances", "Effect": "Allow", "Action": "ec2:RunInstances", "Resource": [ "arn:aws:ec2:*::image/*", "arn:aws:ec2:*::snapshot/*", "arn:aws:ec2:*:*:subnet/*", "arn:aws:ec2:*:*:network-interface/*", "arn:aws:ec2:*:*:security-group/*", "arn:aws:ec2:*:*:key-pair/*" ] }, { "Sid": "AllowRunInstancesWithRestrictions1", "Effect": "Deny", "Action": [ "ec2:CreateVolume", "ec2:RunInstances" ], "Resource": [ "arn:aws:ec2:*:*:volume/*", "arn:aws:ec2:*:*:instance/*" ], "Condition": { "StringNotLike": { "aws:RequestTag/cost_center": "?*" } } }, { "Sid": "AllowRunInstancesWithRestrictions2", "Effect": "Deny", "Action": [ "ec2:CreateVolume", "ec2:RunInstances" ], "Resource": [ "arn:aws:ec2:*:*:volume/*", "arn:aws:ec2:*:*:instance/*" ], "Condition": { "ForAllValues:StringNotLike": { "aws:TagKeys": "Production" } } }, { "Sid": "AllowRunInstancesWithRestrictions3", "Effect": "Deny", "Action": [ "ec2:CreateVolume", "ec2:RunInstances" ], "Resource": [ "arn:aws:ec2:*:*:volume/*", "arn:aws:ec2:*:*:instance/*" ], "Condition": { "StringNotLike": { "aws:RequestTag/identifier": "?????" } } }, { "Sid": "AllowRunInstancesWithRestrictions4", "Effect": "Deny", "Action": [ "ec2:CreateVolume", "ec2:RunInstances" ], "Resource": [ "arn:aws:ec2:*:*:volume/*", "arn:aws:ec2:*:*:instance/*" ], "Condition": { "StringNotLike": { "aws:RequestTag/env": [ "sandbox", "dev", "prod" ] } } }, { "Sid": "AllowRunInstances1", "Effect": "Allow", "Action": [ "ec2:CreateVolume", "ec2:RunInstances" ], "Resource": [ "arn:aws:ec2:*:*:volume/*", "arn:aws:ec2:*:*:instance/*" ] }, { "Sid": "AllowCreateTagsOnRunInstance", "Effect": "Allow", "Action": "ec2:CreateTags", "Resource": "*", "Condition": { "StringEquals": { "ec2:CreateAction": "RunInstances" } } } ] }
请注意以下带有强制要求的值:
- aws:TagKeys 值会强制要求 Production 值要区分大小写。
- ????? 值会强制要求使用任意五个值的组合。前后空格将被忽略。
- ?* 值强制要求在值字段中至少要有一个字符,这样对应的 EC2 实例就无法在空标签值的情况下启动。
相关信息
没有评论
相关内容
- AWS 官方已更新 2 个月前
- AWS 官方已更新 1 年前
- AWS 官方已更新 4 个月前