Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Restrict access to S3 bucket by IP without affecting IAM credentials

I have the following use case on AWS:

  • A bucket which is used to "host" an internal-only web application built in AngularJS
  • A pool of servers in an auto scaling group which can modify the contents of the bucket

In order to ensure the bucket contents is only served internally, I've used the following bucket policy which restricts access to our static IP:

{
    "Statement": [
        {
            "Effect": "Deny",
            "Principal": {
                "AWS": "*"
            },
            "Action": "s3:*",
            "Resource": "arn:aws:s3:::mybucket.myhost.com/*",
            "Condition": {
                "NotIpAddress": {
                    "aws:SourceIp": [
                        "x.x.x.x/32"
                    ]
                }
            }
        }
    ]
}

This works perfectly in stopping anyone outside our office from accessing the bucket over HTTP. Perfect.

However, the servers in the auto scaling group (which have full permission on the bucket using IAM roles) cannot access the bucket. It seems the S3 bucket policy takes precedence. I cannot add their IPs to the bucket policy since it's a scaling group and their IPs will change on a regular basis.

I've tried a few solutions but had no joy:

  • Specifying a second statement with an "Allow" with my IAM role's ARN as the princple
  • Tried looking for a way to allow an EC2 security group full access but couldn't find the option
  • Tried looking for ways to make IAM roles have precedence over bucket policies

I feel like this should be simple but I'm getting frustrated now :(

like image 427
theandywaite Avatar asked Oct 13 '14 10:10

theandywaite


People also ask

How do you limit access to an S3 bucket by IP address?

To allow users to perform S3 actions on the bucket from the VPC endpoints or IP addresses, you must explicitly allow the user-level permissions. You can explicitly allow user-level permissions on either an AWS Identity and Access Management (IAM) policy or another statement in the bucket policy.

How do I restrict IP address in AWS?

To allow or block specific IP addresses for your EC2 instances, use a network Access Control List (ACL) or security group rules in your VPC. Network ACLs and security group rules act as firewalls allowing or blocking IP addresses from accessing your resources.


1 Answers

I realised all the objects in my bucket had public read ACL. I removed the public read ACL so objects defaulted to "deny". I was then able to use a policy like this to allow access to our office IP AND our auto scaling EC2 servers, which are identified by their IAM role:

{
    "Statement": [
        {
            "Effect": "Allow",
            "Principal": {
                "AWS": "*"
            },
            "Action": "s3:*",
            "Resource": "arn:aws:s3:::mybucket.myhost.com/*",
            "Condition": {
                "IpAddress": {
                    "aws:SourceIp": [
                        "x.x.x.x/32"
                    ]
                }
            }
        },
        {
            "Effect": "Allow",
            "Principal": {
                "AWS": "arn:aws:iam::xxxxxxxxxxxx:role/xxxxxx"
            },
            "Action": "s3:*",
            "Resource": "arn:aws:s3:::mybucket.myhost.com/*"
        }
    ]
}
like image 118
theandywaite Avatar answered Oct 27 '22 02:10

theandywaite