Skip to content

fix: added support for generating S3 permissions using Bucket references #648

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 28 additions & 10 deletions lib/deploy/stepFunctions/compileIamRole.js
Original file line number Diff line number Diff line change
@@ -632,6 +632,24 @@ function getEventBridgeSchedulerPermissions(state) {
];
}

// Because the S3 Bucket parameter can be either a literal name or a reference to an existing S3 bucket we need to resolve
function resolveS3BucketReference(bucket, resource) {
if (isIntrinsic(bucket)) {
return {
'Fn::Sub': [
resource,
{ bucket },
],
};
}

return resource.replaceAll('${bucket}', bucket);
}

function resolveS3BucketReferences(bucket, resources) {
return resources.map((resource) => resolveS3BucketReference(bucket, resource));
}

function getS3ObjectPermissions(action, state) {
const bucket = state.Parameters.Bucket || '*';
const key = state.Parameters.Key || '*';
@@ -642,27 +660,27 @@ function getS3ObjectPermissions(action, state) {
return [
{
action: 's3:Get*',
resource: [
`arn:aws:s3:::${bucket}`,
`arn:aws:s3:::${bucket}/*`,
],
resource: resolveS3BucketReferences(bucket, [
`arn:aws:s3:::\${bucket}`,
Copy link
Contributor Author

@danrivett danrivett Apr 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I decided to keep the strings as template literals and just escape the ${bucket} so it wasn't interpolated here, but in the resolveS3BucketReferences() function.

This is because other affected strings below uses other variables such as ${prefix} and ${key} and so they needed to remain template literals to have those interpolated correctly.

So I decided for consistency I would keep them all as template literals and uniformly escape the ${bucket} references.

`arn:aws:s3:::\${bucket}/*`,
]),
},
{
action: 's3:List*',
resource: [
`arn:aws:s3:::${bucket}`,
`arn:aws:s3:::${bucket}/*`,
],
resource: resolveS3BucketReferences(bucket, [
`arn:aws:s3:::\${bucket}`,
`arn:aws:s3:::\${bucket}/*`,
]),
},
];
}

if (prefix) {
arn = `arn:aws:s3:::${bucket}/${prefix}/${key}`;
arn = resolveS3BucketReference(bucket, `arn:aws:s3:::\${bucket}/${prefix}/${key}`);
} else if (bucket === '*' && key === '*') {
arn = '*';
} else {
arn = `arn:aws:s3:::${bucket}/${key}`;
arn = resolveS3BucketReference(bucket, `arn:aws:s3:::\${bucket}/${key}`);
}

return [{