aws s3 access denied when changing bucket policy for root user - amazon-web-services

I am logged in as a root user for aws account. A bucket has been created for static website hosting. I have also unchecked all the options on Public access Settings as you can see in this image below.
After that I tried to update the bucket policy to this from docs
{
"Version":"2012-10-17",
"Statement":[{
"Sid":"PublicReadGetObject",
"Effect":"Allow",
"Principal": "*",
"Action":["s3:GetObject"],
"Resource":["arn:aws:s3:::example-bucket/*"
]
}
]
}
But I keep getting Access denied Error. I don't get it what have I missed. I have tried following things.
I have found other SO posts this which tells to uncheck block
new public bucket policies option which I have already done but
why does it not work for me?
I destroyed the bucket and redid everything from scratch but same issue
persists.
I also created a new IAM user with roles to access everything. This also didn't solve the issue.
I can however manually change the s3 objects to public through Make Public option in s3 menu under s3 Overview tab. This has been solving my problem temporarily for now but I have to keep doing this every time I re upload the files.
So my Question is. Why do I keep getting access denied even for root user?

I followed your steps and it worked fine for me:
I created a new bucket
I clicked on the bucket, went into the Permissions tab and edited the Public access settings
I turned off the two settings under Manage public bucket policies for this bucket
I added a Bucket Policy by taking your policy (above) and changed the bucket name to match my bucket
I successfully saved the Bucket Policy
It then gave me a warning note:
This bucket has public access
You have provided public access to this bucket. We highly recommend that you never grant any kind of public access to your S3 bucket.
I cannot see why you would be receiving Access Denied. My only suggestion is to try it with a different browser just in case there's some strange issue.
You could also add the Bucket Policy via the the AWS CLI: put-bucket-policy — AWS CLI Command Reference
This also successfully added a Bucket Policy onto my bucket.

Related

Generate S3 Presigned URL in Cross-Account Bucket

I have 2 AWS accounts: prod and dev
In prod, I've provided cross-account access to a bucket, a, to the dev account via ACL set in the S3 console. I've granted all permissions to the dev account on this bucket.
At this point I can list, add, remove objects in the a bucket under the dev account credentials. I figured I should be able to create presigned URL's — however, the credentials I create are always AccessDenied in the page
Is there some permission I’ve not included, or something I’m misunderstanding?
I assumed that providing READ access to objects in this bucket from dev account would allow me to generate presigned URL’s that would allow my frontend app to download the files.
I assumed that providing WRITE access to objects in this bucket from dev account would allow me to generate presigned URL’s that would allow my frontend app to upload the files.
I’ve tried another approach as well, using this link to create STS credentials in dev to assume a role I’ve defined in prod that grants Full S3 access to bucket a. Similar results — full ability to list, download and add objects in the bucket but my presign URL is still showing a page that says AccessDenied... leading me to believe im just not granting the proper permission, but can't seem to find the docs to tell me which one.
Thanks in advance
EDIT
Policy I'm using
{
"Sid": "VisualEditor1",
"Effect": "Allow",
"Action": "s3:*",
"Resource": "arn:aws:s3:::a"
}

How do you allow granting public read access to objects uploaded to AWS S3?

I have created a policy that allows access to a single S3 bucket in my account. I then created a group that has only this policy and a user that is part of that group.
The user can view, delete and upload files to the bucket, as expected. However, the user does not seem to be able to grant public read access to uploaded files.
When the Grant public read access to this object(s) option is selected, the upload fails.
The bucket is hosting a static website and I want to allow the frontend developer to upload files and make them public.
The policy for the user role is below:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"s3:*"
],
"Resource": "arn:aws:s3:::my-bucket"
},
{
"Effect": "Allow",
"Action": [
"s3:*"
],
"Resource": "arn:aws:s3:::my-bucket/*"
}
]
}
This is what happens when the IAM user tries to grant public access to the uploaded file:
The proxy error seems unrelated, but essentially the upload is stuck and nothing happens. If they don't select the Grant public access option, the upload goes through immediately (despite the proxy error showing up as well).
To reproduce your situation, I did the following:
Created a new Amazon S3 bucket with default settings (Block Public Access = On)
Created an IAM User (with no policies attached)
Created an IAM Group (with no policies attached)
Added the IAM User to the IAM Group
Attached your policy (from the Question) to the IAM Group (updating the bucket name) as an inline policy
Logged into the Amazon S3 management console as the new IAM User
At this point, the user received an Access Denied error because they were not permitted to list all Amazon S3 buckets. Thus, the console was not usable.
Instead, I ran this AWS CLI command:
aws s3 cp foo.txt s3://new-bucket/ --acl public-read
The result was:
An error occurred (AccessDenied) when calling the PutObject operation: Access Denied
However, the operation succeeded with:
aws s3 cp foo.txt s3://new-bucket/
This means that the --acl is the component that was denied.
I then went to Block Public Access for the bucket and turned OFF the option called "Block public access to buckets and objects granted through new access control lists (ACLs)". My settings were:
I then ran this command again:
aws s3 cp foo.txt s3://new-bucket/ --acl public-read
It worked!
To verify this, I went back into Block Public Access and turned ON all options (via the top checkbox). I re-ran the command and it was Access Denied again, confirming that the cause was the Block Public Access setting.
Bottom line: Turn off the first Block Public Access setting.
You can do it through AWS CLI Update object's ACL
Option 1:
object that's already stored on Amazon S3, you can run this command to update the ACL for public read access:
aws s3api put-object-acl --bucket <<S3 Bucket Name>> --key <<object>> --acl public-read
Option 2:
Run this command to grant full control of the object to the AWS account owner and read access to everyone else:
aws s3api put-object-acl --bucket <<S3 Bucket Name>> --key <<object>> --grant-full-control emailaddress=<<Accountowneremail#emaildomain.com>> --grant-read uri=http://acs.amazonaws.com/groups/global/AllUsers
I found that certain actions (like renaming an object) will fail when executed from the console (but will succeed from the CLI!) when ListAllMyBuckets is not granted for all s3 resources. Adding the following to the IAM policy resolved the issue:
{
"Sid": "AccessS3Console",
"Action": [
"s3:ListAllMyBuckets"
],
"Effect": "Allow",
"Resource": "arn:aws:s3:::*"
}
Some of the actions I tested that failed from the console but succeeded from CLI:
Renaming an object. The console displays "Error - Failed to rename the file to ". Workaround: deleting and re-uploading the object with a new name.
Uploading an object with "Grant public read access to this object(s)". The console's status bar shows that the operation is stuck in "in progress". Workaround: Uploading the object without granting public read access, and then right clicking on it and selecting "Make public".
I experienced these issues after following the instructions here
https://aws.amazon.com/premiumsupport/knowledge-center/s3-console-access-certain-bucket/ which describe how to restrict access to a single bucket (and preventing seeing the full list of buckets in the account). The post didn't mention the caveats.
To limit a user's Amazon S3 console access to only a certain bucket or
folder (prefix), change the following in the user's AWS Identity and
Access Management (IAM) permissions:
Remove permission to the s3:ListAllMyBuckets action.
Add permission to s3:ListBucket only for the bucket or folder that you want the user to access.
Note: To allow the user to upload and download objects from the bucket or folder, you must also include s3:PutObject and s3:GetObject.
Warning: After you change these permissions, the user gets an Access
Denied error when they access the main Amazon S3 console. The user
must access the bucket using a direct console link to the bucket or
folder.

Access Denied while Applying S3 Bucket Policy

Am new to AWS. i have created bucket for static hosting and generated my bucket policy as mentioned below
But when am trying to save, it gives Access Denied error.
And for your information am the owner of bucket and the root user.
First try this command,
aws s3 ls
This will tell you that you are able to access s3 services or not.
If it works than try to add that specific bucket in the resource section of your policy like below
"Resource": [
"BUCKET_NAME",
"BUCKET_NAME/*"
]
I hope this helps!

AWS S3, can not add bucket policy, get "Acess Denied" error

I have read everything I can read on the Internet or the official documents, but this error is so awkward.
I want to add following policy on to my bucket:
{ "Version":"2012-10-17", "Statement":[
{
"Sid":"AddPerm",
"Effect":"Allow",
"Principal": "*",
"Action":["s3:GetObject"],
"Resource":["arn:aws:s3:::mybucket/*"]
} ]
}
however, the only thing I can get is access denied:
I've tried to set this, but it did not work:
enter image description here
What's more, I tried to add bucket policy on my friend account, and it works, the only difference between these two accounts is that my account has an elastic beanstalk(I do not know whether this would affect the result), so please tell me what should I do?
Check what is the policy on that bucket and what is the policy the user under which you are logged in have. For being able to to change bucket policy you need the PutBucketPolicy permission. Keep in mind that Deny anywhere will block you from doing the specified action.
The fix would be adding s3:PutBucketPolicy to yourself and then you should be able to change the s3 bucket permissions. The user policy and the bucket policy on the same account should be "a union" of both policies, but it would be "an intersection" if you are accessing another account - but it seems that you are changing a bucket in your account, so you should be fine with that.
If you cannot change your user IAM policy or you accidentally put Deny on the bucket you will have to log in as the account root and try to fix that this way.
Also, make sure that making all objects in your bucket public is what you plan to do. You allow only GetObject, so if this should be some statically hosted content, this should be fine.

S3 - Revoking "full_control" permission from owned object

While writing S3 server implementation, ran into question I can't really find answer anywhere.
For example I'm the bucket owner, and as well owner of uploaded object.
In case I revoke "full_control" permission from object owner (myself), will I be able to access and modify that object?
What's the expected behaviour in following example:
s3cmd setacl --acl-grant full_control:ownerID s3://bucket/object
s3cmd setacl --acl-revoke full_control:ownerID s3://bucket/object
s3cmd setacl --acl-grant read:ownerID s3://bucket/object
Thanks
So there's the official answer from AWS support:
The short answer for that question would be yes, the bucket/object
owner has permission to read and update the bucket/object ACL,
provided that there is no bucket policy attached that explicitly
removes these permissions from the owner. For example, the following
policy would prevent the owner from doing anything on the bucket,
including changing the bucket's ACL:
{
"Id": "Policy1531126735810",
"Version": "2012-10-17",
"Statement": [
{
"Sid": "Example bucket policy",
"Action": "s3:*",
"Effect": "Deny",
"Resource": "arn:aws:s3:::<bucket>",
"Principal": "*"
}
]
}
However, as root (bucket owner) you'd still have permission to delete
that policy, which would then restore your permissions as bucket owner
to update the ACL.
By default, all S3 resources, buckets, objects and subresources, are
private; only the resource owner, which is the AWS account that
created it, can access the resource[1]. As the resource owner (AWS
account), you can optionally grant permission to other users by
attaching an access policy to the users.
Example: let's say you created an IAM user called -S3User1-, and gave
it permission to create buckets in S3 and update its ACLs. The user in
question then goes ahead and create a bucket and name it
"s3user1-bucket". After that, he goes further and remove List objects,
Write objects, Read bucket permission and Write bucket permissions
from the root account on the ACL section. At this point, if you log in
as root and attempt to read the objects in that bucket, an "Access
Denied" error will be thrown. However, as root you'll be able to go to
the "Permissions" section of the bucket and add these permissions
back.
These days it is recommended to use the official AWS Command-Line Interface (CLI) rather than s3cmd.
You should typically avoid using object-level permissions to control access. It is best to make them all "bucket-owner full control" and then use Bucket Policies to grant access to the bucket or a path.
If you wish to provide per-object access, it is recommended to use Amazon S3 pre-signed URLs, which give time-limited access to a private object. Once the time expires, the URL no longer works. Your application would be responsible for determining whether a user is permitted to access an object, and then generates the pre-signed URL (eg as a link or href on an HTML page).