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

Use common prefix for log groups permissions at Lambdas' execution roles #6212

Merged
merged 1 commit into from
Jun 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
51 changes: 25 additions & 26 deletions lib/plugins/aws/package/lib/mergeIamTemplates.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -83,33 +83,32 @@ module.exports = {
} }
); );


this.serverless.service.getAllFunctions().forEach((functionName) => { const logGroupsPrefix = this.provider.naming
const functionObject = this.serverless.service.getFunction(functionName); .getLogGroupName(`${this.provider.serverless.service.service}-${this.provider.getStage()}`);


this.serverless.service.provider.compiledCloudFormationTemplate this.serverless.service.provider.compiledCloudFormationTemplate
.Resources[this.provider.naming.getRoleLogicalId()] .Resources[this.provider.naming.getRoleLogicalId()]
.Properties .Properties
.Policies[0] .Policies[0]
.PolicyDocument .PolicyDocument
.Statement[0] .Statement[0]
.Resource .Resource
.push({ .push({
'Fn::Sub': 'arn:${AWS::Partition}:logs:${AWS::Region}:${AWS::AccountId}' + 'Fn::Sub': 'arn:${AWS::Partition}:logs:${AWS::Region}:${AWS::AccountId}' +
`:log-group:${this.provider.naming.getLogGroupName(functionObject.name)}:*`, `:log-group:${logGroupsPrefix}*:*`,
}); });


this.serverless.service.provider.compiledCloudFormationTemplate this.serverless.service.provider.compiledCloudFormationTemplate
.Resources[this.provider.naming.getRoleLogicalId()] .Resources[this.provider.naming.getRoleLogicalId()]
.Properties .Properties
.Policies[0] .Policies[0]
.PolicyDocument .PolicyDocument
.Statement[1] .Statement[1]
.Resource .Resource
.push({ .push({
'Fn::Sub': 'arn:${AWS::Partition}:logs:${AWS::Region}:${AWS::AccountId}' + 'Fn::Sub': 'arn:${AWS::Partition}:logs:${AWS::Region}:${AWS::AccountId}' +
`:log-group:${this.provider.naming.getLogGroupName(functionObject.name)}:*:*`, `:log-group:${logGroupsPrefix}*:*:*`,
}); });
});


if (this.serverless.service.provider.iamRoleStatements) { if (this.serverless.service.provider.iamRoleStatements) {
// add custom iam role statements // add custom iam role statements
Expand Down
93 changes: 5 additions & 88 deletions lib/plugins/aws/package/lib/mergeIamTemplates.test.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ describe('#mergeIamTemplates()', () => {
it('should merge the IamRoleLambdaExecution template into the CloudFormation template', it('should merge the IamRoleLambdaExecution template into the CloudFormation template',
() => awsPackage.mergeIamTemplates() () => awsPackage.mergeIamTemplates()
.then(() => { .then(() => {
const qualifiedFunction = awsPackage.serverless.service.getFunction(functionName).name; const canonicalFunctionsPrefix =
`${awsPackage.serverless.service.service}-${awsPackage.provider.getStage()}`;

expect(awsPackage.serverless.service.provider.compiledCloudFormationTemplate expect(awsPackage.serverless.service.provider.compiledCloudFormationTemplate
.Resources[awsPackage.provider.naming.getRoleLogicalId()] .Resources[awsPackage.provider.naming.getRoleLogicalId()]
).to.deep.equal({ ).to.deep.equal({
Expand Down Expand Up @@ -96,7 +98,7 @@ describe('#mergeIamTemplates()', () => {
Resource: [ Resource: [
{ {
'Fn::Sub': 'arn:${AWS::Partition}:logs:${AWS::Region}:${AWS::AccountId}:' 'Fn::Sub': 'arn:${AWS::Partition}:logs:${AWS::Region}:${AWS::AccountId}:'
+ `log-group:/aws/lambda/${qualifiedFunction}:*`, + `log-group:/aws/lambda/${canonicalFunctionsPrefix}*:*`,
}, },
], ],
}, },
Expand All @@ -108,7 +110,7 @@ describe('#mergeIamTemplates()', () => {
Resource: [ Resource: [
{ {
'Fn::Sub': 'arn:${AWS::Partition}:logs:${AWS::Region}:${AWS::AccountId}:' 'Fn::Sub': 'arn:${AWS::Partition}:logs:${AWS::Region}:${AWS::AccountId}:'
+ `log-group:/aws/lambda/${qualifiedFunction}:*:*`, + `log-group:/aws/lambda/${canonicalFunctionsPrefix}*:*:*`,
}, },
], ],
}, },
Expand Down Expand Up @@ -374,91 +376,6 @@ describe('#mergeIamTemplates()', () => {
}); });
}); });


it('should update IamRoleLambdaExecution with a logging resource for the function', () => {
const qualifiedFunction = awsPackage.serverless.service.getFunction(functionName).name;
return awsPackage.mergeIamTemplates().then(() => {
expect(awsPackage.serverless.service.provider.compiledCloudFormationTemplate
.Resources[awsPackage.provider.naming.getRoleLogicalId()]
.Properties
.Policies[0]
.PolicyDocument
.Statement[0]
.Resource
).to.deep.equal([
{
'Fn::Sub': 'arn:${AWS::Partition}:logs:${AWS::Region}:${AWS::AccountId}:'
+ `log-group:/aws/lambda/${qualifiedFunction}:*`,
},
]);
expect(awsPackage.serverless.service.provider.compiledCloudFormationTemplate
.Resources[awsPackage.provider.naming.getRoleLogicalId()]
.Properties
.Policies[0]
.PolicyDocument
.Statement[1]
.Resource
).to.deep.equal([
{
'Fn::Sub': 'arn:${AWS::Partition}:logs:${AWS::Region}:${AWS::AccountId}:'
+ `log-group:/aws/lambda/${qualifiedFunction}:*:*`,
},
]);
});
});

it('should update IamRoleLambdaExecution with each function\'s logging resources', () => {
awsPackage.serverless.service.functions = {
func0: {
handler: 'func.function.handler',
name: 'func0',
},
func1: {
handler: 'func.function.handler',
name: 'func1',
},
};
return awsPackage.mergeIamTemplates().then(() => {
expect(awsPackage.serverless.service.provider.compiledCloudFormationTemplate
.Resources[awsPackage.provider.naming.getRoleLogicalId()]
.Properties
.Policies[0]
.PolicyDocument
.Statement[0]
.Resource
).to.deep.equal(
[
{
'Fn::Sub': 'arn:${AWS::Partition}:logs:${AWS::Region}:${AWS::AccountId}:'
+ 'log-group:/aws/lambda/func0:*',
},
{
'Fn::Sub': 'arn:${AWS::Partition}:logs:${AWS::Region}:${AWS::AccountId}:'
+ 'log-group:/aws/lambda/func1:*',
},
]
);
expect(awsPackage.serverless.service.provider.compiledCloudFormationTemplate
.Resources[awsPackage.provider.naming.getRoleLogicalId()]
.Properties
.Policies[0]
.PolicyDocument
.Statement[1]
.Resource
).to.deep.equal(
[
{
'Fn::Sub': 'arn:${AWS::Partition}:logs:${AWS::Region}:${AWS::AccountId}:'
+ 'log-group:/aws/lambda/func0:*:*',
},
{
'Fn::Sub': 'arn:${AWS::Partition}:logs:${AWS::Region}:${AWS::AccountId}:'
+ 'log-group:/aws/lambda/func1:*:*',
},
]
);
});
});

it('should add default role if one of the functions has an ARN role', () => { it('should add default role if one of the functions has an ARN role', () => {
awsPackage.serverless.service.functions = { awsPackage.serverless.service.functions = {
func0: { func0: {
Expand Down