Skip to content

Commit e7c3b53

Browse files
authored
chore(s3): updated documentation to provide better insights when using grant methods for the Bucket resource (#34733)
### Issue # (if applicable) Closes #34545 . ### Reason for this change Current behavior in some `grant` methods for the `Bucket` resource might cause confusion and be seen as a bug. ### Description of changes Added more details on the policy specifics, explaining why the current behavior is like that, and added additional resources if the implementer needs to restrict even more their permissions. ### Describe any new or updated permissions being added N/A ### Description of how you validated changes N/A. Just documentation being updated. ### Checklist - [x] My code adheres to the [CONTRIBUTING GUIDE](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) and [DESIGN GUIDELINES](https://github.com/aws/aws-cdk/blob/main/docs/DESIGN_GUIDELINES.md) ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 98f72f3 commit e7c3b53

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

packages/aws-cdk-lib/aws-s3/README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,22 @@ bucket.grantReadWrite(myLambda);
156156
Will give the Lambda's execution role permissions to read and write
157157
from the bucket.
158158

159+
### Understanding "grant" Methods
160+
161+
The S3 construct library provides several grant methods for the `Bucket` resource, but two of them have a special behavior. This two accept an `objectsKeyPattern` parameter to restrict granted permissions to specific resources:
162+
- `grantRead`
163+
- `grantReadWrite`
164+
165+
When examining the synthesized policy, you'll notice it includes both your specified object key patterns and the bucket itself.
166+
This is by design. Some permissions (like `s3:ListBucket`) apply at the bucket level, while others (like `s3:GetObject`) apply to specific objects.
167+
168+
Specifically, the [`s3:ListBucket` action operates on bucket resources](https://docs.aws.amazon.com/service-authorization/latest/reference/list_amazons3.html#amazons3-bucket)
169+
and requires the bucket ARN to work properly. This might be seen as a bug, giving the impression that more permissions were granted than the ones you intended, but the reality is that the policy does not ignore your `objectsKeyPattern` - object-specific actions like `s3:GetObject`
170+
will still be limited to the resources defined in your pattern.
171+
172+
If you need to restrict the `s3:ListBucket` action to specific paths, you can add a `Condition` to your policy that limits the `objectsKeyPattern` to specific folders. For more details and examples, see the [AWS documentation on bucket policies](https://docs.aws.amazon.com/AmazonS3/latest/userguide/example-bucket-policies.html#example-bucket-policies-folders).
173+
174+
159175
## AWS Foundational Security Best Practices
160176

161177
### Enforcing SSL

0 commit comments

Comments
 (0)