AWS S3 IAM policy to limit to single sub folder - amazon-web-services

Scenario: I have a simple S3 bucket that multiple users will be uploading files to. Each user should be uploading to a specific folder and only that folder - no sub folders beyond that. Inside that folder, they can upload anything they want. I have an IAM policy that currently limits to that users folder, but allows them to specify sub folders, which I do not want.
Current IAM Policy JSON which limits to a top level folder:
{
"Version": "2012-10-17",
"Statement": [{
"Sid": "VisualEditor2",
"Effect": "Allow",
"Action": [
"s3:PutObject",
"s3:GetObjectTagging"
],
"Resource": "arn:aws:s3:::[MY_BUCKET]/[MY_FOLDER]/*"
}]
}
Proposed IAM Policy JSON which I expected to further limit PutObject only on the folder specified, but this doesn't seem to allow uploading of any object?:
{
"Version": "2012-10-17",
"Statement": [{
"Sid": "VisualEditor2",
"Effect": "Allow",
"Action": [
"s3:PutObject",
"s3:GetObjectTagging"
],
"Resource": "arn:aws:s3:::[MY_BUCKET]/[MY_FOLDER]/*",
"Condition": {
"StringEquals": {
"s3:prefix": [
"",
"[MY_FOLDER]/"
],
"s3:delimiter": [
"/"
]
}
}
}]
}
Expected Results
ALLOW arn:aws:s3:::[MY_BUCKET]/[MY_FOLDER]/[MY_FILE].csv
ALLOW arn:aws:s3:::[MY_BUCKET]/[MY_FOLDER]/[MY_FILE].parquet
ALLOW arn:aws:s3:::[MY_BUCKET]/[MY_FOLDER]/[MY_FILE].txt
DENY arn:aws:s3:::[MY_BUCKET]/[MY_FOLDER]/[MY_FOLDER1]/[MY FOLDER2]/[MY_FILE].txt
DENY arn:aws:s3:::[MY_BUCKET]/[MY_FOLDER]/[MY_FOLDER1]/[MY_FILE].txt
DENY arn:aws:s3:::[MY_BUCKET]/[MY_FOLDER]/[...N Folders]/[MY_FILE].txt

There are no 'folders' in Amazon S3 as there are in a file system. What you see as folders, actually are just prefixes to the filename. The way, Amazon S3 works, you can restrict access to specific prefixes, but can't restrict the creation of additional prefixes underneath.
However, you can implement your requirement by utilizing Amazon S3 Events and a Lambda function. The process could look like this:
1. User stores file in Amazon S3
2. Amazon S3 fires an event notification, containing metadata, including object key (which represents the filename) including prefixes (which represent the folders).
3. Lambda function triggered by the S3 Event and processes the metadata by:
checking the filename for subsequent slashes (/) after the allowed prefix (which indicates a 'subfolder')
deleting the created file if it contains subsequent slashes

Related

Restricting file types on amazon s3 bucket to specific folder with a policy

I am trying to create a bucket policy that allows the whole account access to upload objects, but limit a specific folder to a specific file ext, for example:
arn:aws:s3:::DOC-EXAMPLE-BUCKET/prefixname/*.jpg
However, after following the below example, it seems this is either only possible at a bucket level, or it restricts uploading specifically to the resource specified.
Example:
Restricting file types on amazon s3 bucket with a policy
My bucket policy looks like this:
{
"Version": "2012-10-17",
"Id": "Policy1464968545158",
"Statement": [
{
"Sid": "Stmt1464968483619",
"Effect": "Allow",
"Principal": {
"AWS": "arn:aws:iam::111111111111:root"
},
"Action": "s3:PutObject",
"Resource": [
"arn:aws:s3:::DOC-EXAMPLE-BUCKET/folder/*.csv"
]
},
{
"Sid": "Stmt1464968483619",
"Effect": "Deny",
"Principal": "*",
"Action": "s3:PutObject",
"NotResource": [
"arn:aws:s3:::DOC-EXAMPLE-BUCKET/folder/*.csv"
]
}
]
}
Now with the above policy, it limits only uploads to that specific prefix.
I have tried using this in conjunction with a IAM policy, but the DENY statement pretty much over rides it.
I have also tried to leave out the deny statement and then use an IAM policy to grant access to the bucket and the resources, but my IAM user is still able to upload all extension types to all the different prefixes.
Has anyone been able to get this working at an account level, and not a user level?

AWS S3 | Allow grantee to ONLY download objects from the bucket

It is possible to allow grantee to ONLY download objects from the AWS S3 bucket ? They should not be able to upload any documents in the bucket. If it is possible what bucket permissions should we assign to grantee ? I tried searching this over the web but couldn't find any relevant links.
Thanks in advance!
You can use following URL to generate the required policy.
https://awspolicygen.s3.amazonaws.com/policygen.html
Select Type of Policy : S3 Bucket Policy
Effect: Check Allow
Principal: * (Which means for every one)
Actions: GetObject
Amazon Resource Name (ARN): ARN should follow the following format: arn:aws:s3:::<bucket_name>/<key_name>.
Use a comma to separate multiple values.
Press Add statement and in step 3 you will be able to generate required policy.
Below is a sample policy.
{
"Id": "PolicyID",
"Version": "2012-10-17",
"Statement": [
{
"Sid": "Stmt1610823452352355758",
"Action": [
"s3:GetObject"
],
"Effect": "Allow",
"Resource": "arn:aws:s3:::sample-resource-name/*",
"Principal": "*"
}
]
}

Restrict file uploading to bucket root (pre-signed Form)

Is there a way to restrict the file upload via html forms (pre-signed POST) to the root folder?
The docs describes uploading into a specific folder with the starts-with condition.
But I can't see a way to restrict uploading to the bucket root (or any specific folder, without the ability to generate additional subfolders).
This bucket is only for uploading and any files that are accepted will move into another bucket. Because of that I don't want folders in there....
You will need an S3 Policy to deny actions where the resource contains a /. Add this to your S3 Bucket resource policy.
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "DenyObjectOthers",
"Effect": "Deny",
"Principal": "*",
"Action": [
"s3:*"
],
"Resource": [
"arn:aws:s3:::bucket/*/*"
]
}
]
}

Best way for client to access S3 storage file in a specific bucket

THanks in advance....
I would like to provide security for al my URLS of s3 bucket.IN my bucket around 10 files are there and each file is to be restricted to limited users.Dont want make my server as proxy for validation to access the files..please help me with best way to solve this.
Ex:
File to be accessed by only 3 users & FIle2 to be accessed by 5 users.but all files in same bucket.
It sounds like a perfect use-case for IAM. By using JSON syntax you can easily restrict access to AWS Resources (including S3 bucket, key).
Example:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"s3:GetObject"
],
"Principal": {
"AWS": ["arn:aws:iam::111122223333:user/Alice"
}
"Resource": [
"arn:aws:s3:::my-bucket/file1"
]
}
}

S3 IAM policy works in simulator, but not in real life

I have a client who I want to be able to upload files, but not navigate freely around my S3 bucket. I’ve created them an IAM user account, and applied the following policy:
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "Stmt1416387009000",
"Effect": "Allow",
"Action": [
"s3:ListAllMyBuckets"
],
"Resource": [
"arn:aws:s3:::*"
]
},
{
"Sid": "Stmt1416387127000",
"Effect": "Allow",
"Action": [
"s3:ListBucket"
],
"Resource": [
"arn:aws:s3:::progress"
]
},
{
"Sid": "Stmt1416387056000",
"Effect": "Allow",
"Action": [
"s3:AbortMultipartUpload",
"s3:PutObject"
],
"Resource": [
"arn:aws:s3:::progress/*"
]
}
]
}
There are three statements:
Ability to list all buckets (otherwise they can’t see anything in the S3 console when they log in)
Ability to list the contents of the progress bucket
Ability to put objects in the progress bucket
The user can log in to the AWS console with their username and password (and my custom account URL, i.e. https://account.signin.aws.amazon.com/console). They can go to the S3 section of the console, and see a list of all my buckets. However, if they click progress then they just get the following error message:
Sorry! You were denied access to do that.
I’ve checked with the IAM Policy Simulator whether the user has the ListBucket permission on the bucket’s ARN (arn:aws:s3:::progress) and the Policy Simulator says the user should be allowed.
I’ve logged out and in again as the target user in case policies are only refreshed on log out, but still no joy.
What have I done wrong? Have I missed something?
My guess is that when using the AWS console another call is made to get the bucket location before it can list the objects in that bucket, and the user doesn't have permission to make that call. You need to also give he account access to GetBucketLocation. Relevant text from the documentation
When you use the Amazon S3 console, note that when you click a bucket,
the console first sends the GET Bucket location request to find the
AWS region where the bucket is deployed. Then the console uses the
region-specific endpoint for the bucket to send the GET Bucket (List
Objects) request. As a result, if users are going to use the console,
you must grant permission for the s3:GetBucketLocation action as shown
in the following policy statement:
{
"Sid": "RequiredByS3Console",
"Action": ["s3:GetBucketLocation"],
"Effect": "Allow",
"Resource": ["arn:aws:s3:::*"]
}