s3 存储桶主体末尾的通配符

tim*_*art 5 amazon-s3 terraform

我希望允许帐户中具有共享前缀的角色能够从 S3 存储桶中读取。例如,我们有多个名为RolePrefix1RolePrefix2等的角色,将来可能会创建更多这样的角色。我们希望账户中的所有角色RolePrefix都能够访问 S3 存储桶,而无需在将来更改策略文档。

我的存储桶策略文档的 terraform 如下:

data "aws_iam_policy_document" "bucket_policy_document" {
  statement {
    effect = "Allow"
    actions = ["s3:GetObject"]

    principals = {
      type = "AWS"
      identifiers = ["arn:aws:iam::111122223333:role/RolePrefix*"]
    }

    resources = ["${aws_s3_bucket.bucket.arn}/*"]
  }
}
Run Code Online (Sandbox Code Playgroud)

这给了我以下错误:

Error putting S3 policy: MalformedPolicy: Invalid principal in policy.

是否可以通过其他方式实现此功能?

Jam*_*ean 10

您不能在 IAM 主体字段中将通配符与 ARN 一起使用。您只能使用“*”。

https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_policies_elements_principal.html

“当您在 Principal 元素中指定用户时,不能使用通配符 (*) 来表示“所有用户”。委托人必须始终命名一个或多个特定用户。”

解决方法:保留 "Principal":{"AWS":"*"} 并基于 ARNLike 等创建条件,因为它们接受用户 arn 并在条件下使用通配符。例子:

https://aws.amazon.com/blogs/security/how-to-restrict-amazon-s3-bucket-access-to-a-specific-iam-role/

  • @yerzhan7 对于后代:`StringLike = { aws:PrincipalArn = [ "arn:aws:iam::*:role/<ROLE_NAME>" ] }` (5认同)
  • 重新格式化的示例: `"Condition": {"StringLike": { "aws:PrincipalArn": [ "arn:aws:iam::*:role/<ROLE_NAME>" ] }` (4认同)