-
Notifications
You must be signed in to change notification settings - Fork 212
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
LambdaFunctionReservedConcurrentExecutionsRule and fix end_end_test (#…
- Loading branch information
Showing
7 changed files
with
223 additions
and
5 deletions.
There are no files selected for viewing
27 changes: 27 additions & 0 deletions
27
lib/cfn-nag/custom_rules/LambdaFunctionReservedConcurrentExecutionsRule.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'cfn-nag/violation' | ||
require_relative 'base' | ||
|
||
class LambdaFunctionReservedConcurrentExecutionsRule < BaseRule | ||
def rule_text | ||
'Lambda functions should define ReservedConcurrentExecutions to reserve simultaneous executions' | ||
end | ||
|
||
def rule_type | ||
Violation::WARNING | ||
end | ||
|
||
def rule_id | ||
'W92' | ||
end | ||
|
||
def audit_impl(cfn_model) | ||
lambda_functions = cfn_model.resources_by_type('AWS::Lambda::Function') | ||
violating_lambda_functions = lambda_functions.select do |lambda_function| | ||
lambda_function.reservedConcurrentExecutions.nil? | ||
end | ||
|
||
violating_lambda_functions.map(&:logical_resource_id) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
spec/custom_rules/LambdaFunctionReservedConcurrentExecutionsRule_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'spec_helper' | ||
require 'cfn-model' | ||
require 'cfn-nag/custom_rules/LambdaFunctionReservedConcurrentExecutionsRule' | ||
|
||
describe LambdaFunctionReservedConcurrentExecutionsRule do | ||
|
||
describe 'AWS::Lambda::Function' do | ||
context 'when Lambda functions defines ReservedConcurrentExecutions to reserve simultaneous executions' do | ||
it 'does not return an offending logical resource id' do | ||
cfn_model = CfnParser.new.parse read_test_template('json/lambda_function/lambda_reserved_executions.json') | ||
actual_logical_resource_ids = LambdaFunctionReservedConcurrentExecutionsRule.new.audit_impl cfn_model | ||
|
||
expect(actual_logical_resource_ids).to eq [] | ||
end | ||
end | ||
end | ||
|
||
describe 'AWS::Lambda::Function' do | ||
context 'when Lambda functions does not define ReservedConcurrentExecutions to reserve simultaneous executions' do | ||
it 'does return an offending logical resource id' do | ||
cfn_model = CfnParser.new.parse read_test_template('json/lambda_function/lambda_not_reserved_executions.json') | ||
actual_logical_resource_ids = LambdaFunctionReservedConcurrentExecutionsRule.new.audit_impl cfn_model | ||
|
||
expect(actual_logical_resource_ids).to eq ["FunctionNotReserved"] | ||
end | ||
end | ||
end | ||
|
||
end |
76 changes: 76 additions & 0 deletions
76
spec/test_templates/json/lambda_function/lambda_not_reserved_executions.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
{ | ||
"Resources": { | ||
"FunctionNotReserved": { | ||
"Type": "AWS::Lambda::Function", | ||
"Properties": { | ||
"Role": { | ||
"Ref": "RoleWithLambdaVPCAccessManagedPolicy" | ||
}, | ||
"Handler": "index.handler", | ||
"Code": { | ||
"ZipFile": { | ||
"Fn::Join": [ | ||
"", | ||
[ | ||
"var response = require('cfn-response');", | ||
"exports.handler = function(event, context) {", | ||
"var responseData = {Value: event.ResourceProperties.List};", | ||
"responseData.Value.push(event.ResourceProperties.AppendedItem);", | ||
"response.send(event, context, response.SUCCESS, responseData);}" | ||
] | ||
] | ||
} | ||
}, | ||
"Runtime": "nodejs6.10", | ||
"VpcConfig": { | ||
"SecurityGroupIds": [ | ||
"secgroup" | ||
], | ||
"SubnetIds": [ | ||
"subnetid" | ||
] | ||
} | ||
} | ||
}, | ||
"RoleWithLambdaVPCAccessManagedPolicy": { | ||
"Type": "AWS::IAM::Role", | ||
"Properties": { | ||
"AssumeRolePolicyDocument": { | ||
"Version": "2012-10-17", | ||
"Statement": [ | ||
{ | ||
"Effect": "Allow", | ||
"Principal": { | ||
"Service": "lambda.amazonaws.com" | ||
}, | ||
"Action": [ | ||
"sts:AssumeRole" | ||
] | ||
} | ||
] | ||
}, | ||
"ManagedPolicyArns": [ | ||
"arn:aws:iam::aws:policy/service-role/AWSLambdaVPCAccessExecutionRole" | ||
], | ||
"Path": "/", | ||
"Policies": [ | ||
{ | ||
"PolicyName": "root", | ||
"PolicyDocument": { | ||
"Version": "2012-10-17", | ||
"Statement": [ | ||
{ | ||
"Effect": "Allow", | ||
"Action": [ | ||
"specific:ObscureAction" | ||
], | ||
"Resource": "*" | ||
} | ||
] | ||
} | ||
} | ||
] | ||
} | ||
} | ||
} | ||
} |
77 changes: 77 additions & 0 deletions
77
spec/test_templates/json/lambda_function/lambda_reserved_executions.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
{ | ||
"Resources": { | ||
"FunctionConcurrentReserverd": { | ||
"Type": "AWS::Lambda::Function", | ||
"Properties": { | ||
"ReservedConcurrentExecutions": "1", | ||
"Role": { | ||
"Ref": "RoleWithLambdaVPCAccessManagedPolicy" | ||
}, | ||
"Handler": "index.handler", | ||
"Code": { | ||
"ZipFile": { | ||
"Fn::Join": [ | ||
"", | ||
[ | ||
"var response = require('cfn-response');", | ||
"exports.handler = function(event, context) {", | ||
"var responseData = {Value: event.ResourceProperties.List};", | ||
"responseData.Value.push(event.ResourceProperties.AppendedItem);", | ||
"response.send(event, context, response.SUCCESS, responseData);}" | ||
] | ||
] | ||
} | ||
}, | ||
"Runtime": "nodejs6.10", | ||
"VpcConfig": { | ||
"SecurityGroupIds": [ | ||
"secgroup" | ||
], | ||
"SubnetIds": [ | ||
"subnetid" | ||
] | ||
} | ||
} | ||
}, | ||
"RoleWithLambdaVPCAccessManagedPolicy": { | ||
"Type": "AWS::IAM::Role", | ||
"Properties": { | ||
"AssumeRolePolicyDocument": { | ||
"Version": "2012-10-17", | ||
"Statement": [ | ||
{ | ||
"Effect": "Allow", | ||
"Principal": { | ||
"Service": "lambda.amazonaws.com" | ||
}, | ||
"Action": [ | ||
"sts:AssumeRole" | ||
] | ||
} | ||
] | ||
}, | ||
"ManagedPolicyArns": [ | ||
"arn:aws:iam::aws:policy/service-role/AWSLambdaVPCAccessExecutionRole" | ||
], | ||
"Path": "/", | ||
"Policies": [ | ||
{ | ||
"PolicyName": "root", | ||
"PolicyDocument": { | ||
"Version": "2012-10-17", | ||
"Statement": [ | ||
{ | ||
"Effect": "Allow", | ||
"Action": [ | ||
"specific:ObscureAction" | ||
], | ||
"Resource": "*" | ||
} | ||
] | ||
} | ||
} | ||
] | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters