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

fix: update list bucket policies for s3 triggers #6497

Merged
merged 2 commits into from
Jan 29, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 2 additions & 2 deletions packages/amplify-category-storage/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,14 @@ async function getPermissionPolicies(context, resourceOpsMapping) {

if (providerPlugin) {
const providerController = require(`./provider-utils/${providerPlugin}`);
const { policy, attributes } = providerController.getPermissionPolicies(
const { policies, attributes } = providerController.getPermissionPolicies(
context,
service,
resourceName,
resourceOpsMapping[resourceName],
);

permissionPolicies.push(policy);
permissionPolicies.push(...policies);
resourceAttributes.push({ resourceName, attributes, category });
} else {
context.print.error(`Provider not configured for ${category}: ${resourceName}`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -937,7 +937,7 @@ async function addTrigger(context, resourceName, triggerFunction, adminTriggerFu
Statement: [
{
Effect: 'Allow',
Action: ['s3:PutObject', 's3:GetObject', 's3:ListBucket', 's3:DeleteObject'],
Action: ['s3:PutObject', 's3:GetObject', 's3:DeleteObject'],
Resource: [
{
'Fn::Join': [
Expand All @@ -953,6 +953,23 @@ async function addTrigger(context, resourceName, triggerFunction, adminTriggerFu
},
],
},
{
Effect: 'Allow',
Action: 's3:ListBucket',
Resource: [
{
'Fn::Join': [
'',
[
'arn:aws:s3:::',
{
Ref: 'S3Bucket',
},
],
],
},
],
},
],
},
},
Expand Down Expand Up @@ -1282,7 +1299,7 @@ function convertToCRUD(parameters, answers) {
}

export const getIAMPolicies = (resourceName, crudOptions) => {
let policy = {};
let policies = [];
let actions = new Set();

crudOptions.forEach(crudOption => {
Expand All @@ -1306,7 +1323,28 @@ export const getIAMPolicies = (resourceName, crudOptions) => {
});

actions = Array.from(actions);
policy = {
let listBucketPolicy = {};
if (actions.includes('s3:ListBucket')) {
listBucketPolicy = {
Effect: 'Allow',
Action: 's3:ListBucket',
Resource: [
{
'Fn::Join': [
'',
[
'arn:aws:s3:::',
{
Ref: `${category}${resourceName}BucketName`,
},
],
],
},
],
};
actions = actions.filter(action => action != 's3:ListBucket');
}
let policy = {
Effect: 'Allow',
Action: actions,
Resource: [
Expand All @@ -1324,10 +1362,11 @@ export const getIAMPolicies = (resourceName, crudOptions) => {
},
],
};

// push both policies
policies.push(policy, listBucketPolicy);
const attributes = ['BucketName'];

return { policy, attributes };
return { policies, attributes };
};

function getTriggersForLambdaConfiguration(protectionLevel, functionName) {
Expand Down