AWS IAM Bool v/s BoolIfExists
AWS IAM Bool v/s BoolIfExists
我无法理解 AWS IAM 策略条件中 Bool
和 BoolIfExists
之间的区别。有人可以解释一下吗?
例如:
"Condition" : {"BoolIfExists" : {"aws:MutliFactorAuthPresent" : false}}
"Condition" : {"Bool" : {"aws:MutliFactorAuthPresent" : false}}
首先,不存在aws:MutliFactorAuthPresent
这样的条件。应该是aws:MultiFactorAuthPresent
。其次,aws:MultiFactorAuthPresent
是全局键,所以它一直存在。没有理由为它使用 IfExists
。
无论如何,您需要记住 IAM 条件键(全局键除外)是特定于资源的。例如,ec2:InstanceType
仅适用于 EC2 实例,而 ec2:VolumeSize
仅适用于 EBS 卷。
此外,某些 IAM 操作(例如 ec2:runinstance
)需要访问 多个资源,例如 ec2 实例和 ebs-volumes 等
因此,当您使用操作多个资源的操作制作 IAM 语句时,例如 ec2:runinstance
,并且您想要设置条件,您可能经常会出现不匹配的情况,因为并非所有条件键都适用于所有资源将被访问。
例如,条件键 ec2:VolumeSize
不适用于 EC2 实例,但适用于 EBS 卷。由于 following:
而失败
A key that is not present in the request is considered a mismatch.
因此您可以使用 ...IfExists condition operator 来确保在这种情况下不会出现故障。
一个example of such IAM statement是:
{
"Sid": "RunInstance",
"Effect": "Allow",
"Action": "ec2:RunInstances",
"Resource": "*",
"Condition": {
"StringLikeIfExists": {
"ec2:InstanceType": [
"t1.*",
"t2.*",
"m3.*"
]}}
}
如果没有 IfExists
,将会出现不匹配,因为 ec2:RunInstances
需要访问 EBS 卷,而 ec2:InstanceType
密钥不适用于这些卷。
来自https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_policies_condition-keys.html
推荐组合
我们建议您使用 BoolIfExists 运算符来检查请求是否使用 MFA 进行身份验证。
"Effect" : "Deny",
"Condition" : { "BoolIfExists" : { "aws:MultiFactorAuthPresent" : "false" } }
Deny、BoolIfExists 和 false 的组合拒绝未使用 MFA 进行身份验证的请求。具体来说,它拒绝来自不包含 MFA 的临时凭证的请求。它还拒绝使用 long-term 凭证发出的请求,例如 AWS CLI 或使用访问密钥发出的 AWS API 操作。 *IfExists 运算符检查是否存在 aws:MultiFactorAuthPresent 键以及它是否存在,如其存在所指示的那样。当您想要拒绝任何未使用 MFA 进行身份验证的请求时,请使用此选项。这更安全,但可以破坏任何使用访问密钥访问 AWS CLI 或 AWS API.
的代码或脚本
我无法理解 AWS IAM 策略条件中 Bool
和 BoolIfExists
之间的区别。有人可以解释一下吗?
例如:
"Condition" : {"BoolIfExists" : {"aws:MutliFactorAuthPresent" : false}}
"Condition" : {"Bool" : {"aws:MutliFactorAuthPresent" : false}}
首先,不存在aws:MutliFactorAuthPresent
这样的条件。应该是aws:MultiFactorAuthPresent
。其次,aws:MultiFactorAuthPresent
是全局键,所以它一直存在。没有理由为它使用 IfExists
。
无论如何,您需要记住 IAM 条件键(全局键除外)是特定于资源的。例如,ec2:InstanceType
仅适用于 EC2 实例,而 ec2:VolumeSize
仅适用于 EBS 卷。
此外,某些 IAM 操作(例如 ec2:runinstance
)需要访问 多个资源,例如 ec2 实例和 ebs-volumes 等
因此,当您使用操作多个资源的操作制作 IAM 语句时,例如 ec2:runinstance
,并且您想要设置条件,您可能经常会出现不匹配的情况,因为并非所有条件键都适用于所有资源将被访问。
例如,条件键 ec2:VolumeSize
不适用于 EC2 实例,但适用于 EBS 卷。由于 following:
A key that is not present in the request is considered a mismatch.
因此您可以使用 ...IfExists condition operator 来确保在这种情况下不会出现故障。
一个example of such IAM statement是:
{
"Sid": "RunInstance",
"Effect": "Allow",
"Action": "ec2:RunInstances",
"Resource": "*",
"Condition": {
"StringLikeIfExists": {
"ec2:InstanceType": [
"t1.*",
"t2.*",
"m3.*"
]}}
}
如果没有 IfExists
,将会出现不匹配,因为 ec2:RunInstances
需要访问 EBS 卷,而 ec2:InstanceType
密钥不适用于这些卷。
来自https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_policies_condition-keys.html
推荐组合
我们建议您使用 BoolIfExists 运算符来检查请求是否使用 MFA 进行身份验证。
"Effect" : "Deny",
"Condition" : { "BoolIfExists" : { "aws:MultiFactorAuthPresent" : "false" } }
Deny、BoolIfExists 和 false 的组合拒绝未使用 MFA 进行身份验证的请求。具体来说,它拒绝来自不包含 MFA 的临时凭证的请求。它还拒绝使用 long-term 凭证发出的请求,例如 AWS CLI 或使用访问密钥发出的 AWS API 操作。 *IfExists 运算符检查是否存在 aws:MultiFactorAuthPresent 键以及它是否存在,如其存在所指示的那样。当您想要拒绝任何未使用 MFA 进行身份验证的请求时,请使用此选项。这更安全,但可以破坏任何使用访问密钥访问 AWS CLI 或 AWS API.
的代码或脚本