Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bucket Policy Configuration Enhancements #4096

Closed
san4d opened this issue Sep 19, 2024 · 1 comment
Closed

Bucket Policy Configuration Enhancements #4096

san4d opened this issue Sep 19, 2024 · 1 comment
Assignees

Comments

@san4d
Copy link
Contributor

san4d commented Sep 19, 2024

Problem

I'm working with an S3 bucket (sst.aws.Bucket) that I'd like to add server-side encryption to with a customer managed key. As part of that work, I want to require KMS encryption on all objects in the bucket based on this documentation from AWS.

The policy needs to end up like this:

{
   "Version":"2012-10-17",
   "Id":"PutObjectPolicy",
   "Statement":[{
         "Sid":"DenyObjectsThatAreNotSSEKMS",
         "Effect":"Deny",
         "Principal":"*",
         "Action":"s3:PutObject",
         "Resource":"arn:aws:s3:::amzn-s3-demo-bucket1/*",
         "Condition":{
            "Null":{
               "s3:x-amz-server-side-encryption-aws-kms-key-id":"true"
            }
         }
      }
   ]
}

Because the SST S3 creates a policy automatically and buckets cannot have more than one policy I tried to use transforms to adjust the policy. However, the policy transform does not have access to the bucket's ARN, which I need.

Ideas

  1. Update the transform to have reference to the bucket so I can interpolate the arn (like you do here).
  2. Add an optional policy statements field to BucketCorsArgs that can be pushed to the internal statements array before creating the policy document.
@fwang
Copy link
Contributor

fwang commented Oct 15, 2024

@san4d you have access to the bucket name via args passed into to transform.policy. We also recently added a helper function sst.aws.iamEdit to help w/ manipulating the IAM policy. You can

new sst.aws.Bucket("MyBucket", {
  transform: {
    policy: (args) => {
      args.policy = sst.aws.iamEdit(args.policy, (policy) => {
        policy.Statement.push({
          Effect: "Deny",
          Action: "s3:PutObject",
          Resource: $interpolate`arn:aws:s3:::${args.bucket}/*`,
          ...
        });
      });
    },
  },
});

Feel free to reopen is this doesn't work for you.

@fwang fwang closed this as completed Oct 15, 2024
@thdxr thdxr transferred this issue from sst/ion Oct 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants