Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bucket policy that respects pre-signed URLs OR IP Address deny?

I would like to be able to restrict access to files in a S3 bucket in multiple ways. This is due to the fact that the files stored can be accessed in different manners. We do this because we have TBs of files, so we don't want to duplicate the bucket.

One access method is through tokenized CDN delivery which uses the S3 bucket as a source. So that the files may be pulled, I've set the permissions for the files to allow download for everybody. Using a bucket policy, I can restrict IP addresses which can get the files in the bucket. So I've restricted them to the CDN IP block and anyone outside those IP addresses can't grab the file.

The other is access method is by direct downloads using our store system which generates S3 time expiring pre-signed URLS.

Since the CDN pull effectively needs the files to be publicly readable, is there a way to:

  1. Check first for a valid pre-signed URL and serve the file if the request is valid

  2. If not valid, fall back to the IP address restriction to prevent further access?

I've got a working IP restriction bucket policy working, but that stomps out the pre-signed access...removing the bucket policy fixes the pre-signed access but then the files are public.

like image 411
johnmontfx Avatar asked Mar 11 '16 20:03

johnmontfx


People also ask

Does S3 bucket need to be public for Presigned URL?

All objects and buckets are private by default. However, you can use a presigned URL to optionally share objects or allow your customers/users to upload objects to buckets without AWS security credentials or permissions.

Why should you use S3 Presigned URLs?

The main purpose of presigned URLs is to grant a user temporary access to an S3 object. However, presigned URLs can be used to grant permission to perform additional operations on S3 buckets and objects.

What is the difference between S3 ACL and bucket policy?

The biggest advantage of using ACL is that you can control the access level of not only buckets but also of an object using it. Whereas IAM or Bucket Policies can only be attached to buckets but not to objects in the bucket, Bucket ACLs can be assigned to buckets as well as objects in it.


1 Answers

Objects in Amazon S3 are private by default. Access then can be granted via any of these methods:

  • Per-object ACLs (mostly for granting public access)
  • Bucket Policy with rules to define what API calls are permitted in which circumstances (eg only from a given IP address range)
  • IAM Policy -- similar to Bucket Policy, but can be applied to specific Users or Groups
  • A Pre-signed URL that grants time-limited access to an object

When attempting to access content in Amazon S3, as long as any of the above permit access, then access is granted. It is not possible to deny access via a different method -- for example, if access is granted via a pre-signed URL, then a Bucket Policy cannot cause that access to be denied.

Therefore, the system automatically does what you wish... If the pre-signed URL is valid, then access is granted. If the IP address comes from the desired range, then access is granted. It should work correctly.

It is very strange that you say the IP restriction "stomps out the pre-signed access" -- that should not be possible.

like image 88
John Rotenstein Avatar answered Sep 19 '22 12:09

John Rotenstein