Access s3 in html file - amazon-web-services

I have an HTML file which contains screenshots of automation test run. These screenshots are stored in an s3 bucket. I will be attaching this HTML file to an email. Hence will like these screenshots to be rendered and be visible to anyone who opens the HTML report on their laptop.
The challenge
- I am behind a corporate firewall hence cannot allow public access to the s3 bucket
I can access the s3 bucket via IAM access from ec2-instance and will be uploading the screenshots to s3 using the same.
I am currently exploring the following options
Accessing S3 via a cloudfront url (not sure regarding the access control policies available via cloudfront). This option will require lots of back forth with IT hence would be a last resort
Embed javascript in the HTML file to access a hosted service on EC2. This service then fetches the objects from S3.

You could simply set the View only Public policy(refer bottom). That will allow anyone accessing and view the Images having correct URL.
Accessing S3 via a cloudfront url (not sure regarding the access control policies available via cloudfront). This option will require lots of back forth with IT hence would be a last resort
In my opinion this is not a correct solution. It is over engineering.
Embed javascript in the HTML file to access a hosted service on EC2. This service then fetches the objects from S3.
In my opinion this also unnecessary overhead, you may be dealing with.
Simple, solution will be--
Allow Public GET Only access. SO whomsoever having full path correct URL will be able to access, in your case it will be embeded HTML report that will have links of S3, sometthing like https://s3.amazonaws.com/bucket-name/somepath/somepath/../someimage.jpg
Create some complicated URL pattern, so that no one could guess it easily, hence no security breach.
Public access policy will look something like below.
{
"Version":"2012-10-17",
"Statement":[
{
"Sid":"AddPerm",
"Effect":"Allow",
"Principal": "*",
"Action":["s3:GetObject"],
"Resource":["arn:aws:s3:::examplebucket/*"]
} ] }

Related

Allowing S3 bucket to access itself

I just want my S3 bucket to be able to access itself. For example in my index.html there is a reference to a favicon, which resides in my s3 (the same actually) bucket. When i call the index.html, i get 403 HTTP ACCESS DENIED error.
If i put block all access off and i add a policy it works, but i do not want the Bucket to be public.
How am i able to invoke my website with my AWS user for example without making the site public (that is with having all internet access blocked)?
I just want my S3 bucket to be able to access itself.
no, the request always comes from the client
How am i able to invoke my website with my AWS user
For the site-level access control there is CloudFront with signed cookie. You will still need some logic (apigw+lambda? lambda on edge? other server?) to authenticate the user and sign the cookie.
You mention that "the websites in the bucket should be only be able to see by a few dedicated users, which i will create with IAM."
However, accessing Amazon S3 content with IAM credentials is not compatible with accessing objects via URLs in a web browser. IAM credentials can be used when making AWS API calls, but a different authentication method is required when accessing content via URLs. Authentication normally requires a back-end to perform the authentication steps, or you could use Amazon Cognito.
Without knowing how your bucket is set up and what permissions / access controls you have already deployed it is hard to give a definite answer.
Having said that it sounds like you simply need to walk through the proper steps for building an appropriate permission model. You have already explored part of this with the block all access and a policy, but there are also ACL's and permission specifics based on object ownership that need to be considered.
Ultimately AWS's documentation is going to do a better job than most to illustrate what to do and where to start:
https://docs.aws.amazon.com/AmazonS3/latest/userguide/WebsiteAccessPermissionsReqd.html
NOTE: if you share more information about how the bucket is configured and how your client side is accessing the website, I can edit the answer to give a more prescriptive solution (assuming the AWS docs don't get you all the way there)
UPDATE: After re-reading your question and comment on my answer, I think gusto2 and John's answers are pointing you in the right direction. What you are wanting to do is to authenticate users before they access the contents of the S3 bucket (which if I understand you right, is a s3 hosted static website). This means you need an authentication layer between the client and the bucket, which can be accomplished in a number of ways (lambda + cloudfront, or using an IdP like Cognito are certainly viable options). It would be a moot point for me to regurgitate exactly how to pull off something like this when there are a ton of accessible blog posts on the topic (search "Authenticate s3 static website").
HOWEVER I also want to point out that what you are wanting to accomplish is not possible in the way you are hoping to accomplish it (using IAM permission modeling to authenticate users against an s3 hosted static website). You can either authenticate users to your s3 website OR you can use IAM + S3 Permissions and ACL to set up AWS User and Role specific access to the contents of a bucket, but you can't use IAM users / roles as a method for authenticating client access to an S3 static website (not in any way I would imagine is simple or recommended at least...)

AWS 405 error on DELETE (Object) request in API Gateway

To start, I'll try and make sure to supply any information that might be needed, and really appreciate any help with this issue. I've been following basic AWS Tutorials for the past couple days to try to build a basic outline for a website idea, but found myself stuck when following this tutorial: https://docs.aws.amazon.com/apigateway/latest/developerguide/integrating-api-with-aws-services-s3.html
The goal with this is to enable my website to CRUD PDF files to an S3 bucket via API Gateway.
So far, I've followed the tutorial steps, set up the S3 Bucket, and attached the role (S3FullAccess) to the different APIs. The result is that, while other requests (GET/POST) seem to be working correctly, DELETE object results in a 405 method not allowed. I've looked around a bunch (been working on this particular issue for the past couple hours) and am at the point of:
Doubting it's the policy, since JSON shows {"Effect":"Allow", "Action": "s3:*", "Resource": " *"}
Doubting it's the S3 Bucket, as anything that looks like it could block access has been disabled
Wondering if Object ACL is the culprit, since the Grantee settings for my objects (S3 Console -> Bucket -> Object -> Permissions) shows that only the "Object owner" has permissions [Object: Read, Object ACL: Read/Write].
So now I'm trying to figure out if sending ACL configuration as part of the Gateway PUT request is the solution (and if so how). Additionally, I might be able to use a lambda function to reconfigure the object's ACL on the event trigger of a PUT request to S3, but that sounds like bad design for what's intended.
Additionally:
I'm not using Versioning, MFA, Encryption, or Object Lock
All "Block Public Access" settings are set to Off
No Bucket Policy is shown (since I'm using IAM)
AWS Regions are properly selected
Let me know if there's anything you need for additional info (such as screenshots of Gateway, IAM, or S3) and I'll update the post with them.
Thanks so much.

Prevent anyone from accessing my bucket unless a certain condition

I have an ec2 instance with a load balancer and cloudfront attached and I want to prevent my s3 bucket files from being viewed unless the files are being requested on my website. How would I be able to do this? I've tried "referrer" (which doesn't work sometimes, and apprently not the best option) and I've tried using the "source ip" condition which just doesn't work, I've put in my website ip, my vpc ip from my load balancer, etc, just doesn't work (unless there's another way I have to do it, I would appreciate it if anyone told me). I just want a bucket policty that has a condition like so:
"Condition": {
** person is on my website **
}
If anyone has any ideas, that would be nice, thanks.
I can immediately think of 2 options:
Make your bucket private and instead reverse-proxy the images through your own website.
Make your bucket use Query String Authentication and have your website generate a short-lived QSA token (5 minutes?) for each visitor.
If your content is being served from Amazon S3 or Amazon CloudFront, you can use pre-signed URLs to grant time-limited access to private content.
For example, let's say that you have a photo-sharing website and all photos are private by default. Access can be provided as follows:
Users authenticate to your application
The user then requests access to a private object, or your application wishes to generate an HTML page that includes a link to a private object (eg in an <img> tag).
The application checks whether the user is permitted to access the object. If they are, the application generates a pre-signed URL and provides it in the HTML page or as a link.
The user's browser then uses the URL to request the private object, which sends the request to CloudFront or S3
CloudFront or S3 then checks whether the pre-signed URL is correctly signed and is still within the validity period. If so, it provides access to the object. If not, it returns Access Denied.
For more information, see:
Amazon S3 pre-signed URLs
Using Amazon CloudFront Signed URLs

How to limit access to individual files in an Amazon s3 static website

Recently created a static website, hosted on s3 and I noticed when users check the source of the website, they can click links which allow them to access items such as images in a separate tab. Is there a way to allow the website to access the images but to limit users trying to access the source image.
A user simply opening assets in a tab should be fine, if you're simply trying to prevent the content from accessible other than on your domain you can use the Referer head to lock it down to only via your web site.
This can be done in S3 via a bucket policy similar to the one below.
{
"Version":"2012-10-17",
"Id":"http referer policy example",
"Statement":[
{
"Sid":"Allow get requests originating from www.example.com and example.com.",
"Effect":"Allow",
"Principal":"*",
"Action":["s3:GetObject","s3:GetObjectVersion"],
"Resource":"arn:aws:s3:::awsexamplebucket1/*",
"Condition":{
"StringLike":{"aws:Referer":["http://www.example.com/*","http://example.com/*"]}
}
}
]
}
You can always enhance this by using a CloudFront distribution combined with a AWS WAF to included a rule to block by referrer.
If you're trying to lock this content (whether someone needs to either login to see it or pay to get it) you have a couple of options.
You can create pre-signed URLs for your S3 objects and expose these in the HTML. This will be valid for a limited time (depending on the parameters passed into the generation).
You can use a CloudFront distribution with either signed cookies or signed URLs.
Sadly you can't limit ability to download your images without proper authentication. If your users can see images in their browsers, they can download them, as they've had been already downloaded.
But you can limit your users from directly going to your S3 bucket. For this you can front your S3 bucket with CloudFront (CF).
Specifically, you could setup Origin Access Identity in CF and make your website and all images accessible only through CF:
Restricting Access to Amazon S3 Content by Using an Origin Access Identity
Amazon S3 + Amazon CloudFront: A Match Made in the Cloud
How do I use CloudFront to serve a static website hosted on Amazon S3?

Need to make Amazon S3 bucket public from API

I've spent over a week on this issue, and Amazon does not provide any resources that can answer this for me. I have built a custom CMS that allows thousands of users to upload their own files. Those files need to be migrated to a CDN, as they are beginning to overwhelm the file system at near 50GB. I have already integrated with Amazon's S3 PHP SDK. My application must be able to do the following:
Create and remove buckets through the API, not the console. This is working.
Perform all CRUD operations on the files uploaded to the buckets, again explicitly through the console. Creating and removing files is working.
As part of CRUD, these files must then be readable via HTTP/HTTPS as they are required assets in the web application. These are all registering as 'Access Denied', due to the buckets not being public by default.
As I understand it, the point of a CDN is that Content can be Delivered via a Network. I need to understand how to make these files visible in a web application without the use of the console, as these buckets will be dynamically created by users and it's a game-breaker to require administration to update them manually.
I'd appreciate it if someone help me resolve this.
Objects in Amazon S3 are private by default. If you wish to make the objects public (meaning accessible to everyone in the world), there are two methods:
When uploading the objects, mark them as ACL=public-read. This Access Control List will make the object itself public. OR
Add a bucket policy to the bucket that will make the entire bucket (or, if desired, a portion of the bucket) public.
From Bucket Policy Examples - Amazon Simple Storage Service:
{
"Version":"2012-10-17",
"Statement":[
{
"Sid":"PublicRead",
"Effect":"Allow",
"Principal": "*",
"Action":["s3:GetObject"],
"Resource":["arn:aws:s3:::examplebucket/*"]
}
]
}
Such a policy can be added after bucket creation by using putBucketPolicy().
Also, please be aware that Amazon S3 Block Public Access is turned on by default on buckets to prevent exposing private content. The above method will require this block to be turned off. This can be done programmatically with putPublicAccessBlock() or deletePublicAccessBlock().