Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wildcard at end of principal for s3 bucket

I want to allow roles within an account that have a shared prefix to be able to read from an S3 bucket. For example, we have a number of roles named RolePrefix1, RolePrefix2, etc, and may create more of these roles in the future. We want all roles in an account that begin with RolePrefix to be able to access the S3 bucket, without having to change the policy document in the future.

My terraform for bucket policy document is as below:

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}/*"]
  }
}

This gives me the following error:

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

Is it possible to achieve this functionality in another way?

like image 924
timleathart Avatar asked Jun 20 '19 03:06

timleathart


People also ask

What does principal AWS * mean?

Principal. A principal is a person or application that can make a request for an action or operation on an AWS resource. The principal is authenticated as the AWS account root user or an IAM entity to make requests to AWS. As a best practice, do not use your root user credentials for your daily work.

What is principal in S3 bucket policy?

Principal – The account or user who is allowed access to the actions and resources in the statement. In a bucket policy, the principal is the user, account, service, or other entity that is the recipient of this permission.

What are the 3 types of IAM principals?

Three types of Principals — root users, IAM users and Instance Principals. First IAM user is called the root user.

What is wildcard in AWS?

Some services do not let you specify actions for individual resources; instead, any actions that you list in the Action or NotAction element apply to all resources in that service. In these cases, you use the wildcard * in the Resource element. The following example refers to a specific Amazon SQS queue.


1 Answers

You cannot use wildcard along with the ARN in the IAM principal field. You're allowed to use just "*".

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

When you specify users in a Principal element, you cannot use a wildcard (*) to mean "all users". Principals must always name a specific user or users.

Workaround:

Keep "Principal":{"AWS":"*"} and create a condition based on ARNLike etc as they accept user ARN with wildcard in condition.

Example:

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

like image 166
James Dean Avatar answered Nov 16 '22 17:11

James Dean