-
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.
API Gateway deployment is configured for caching and cache is not enc…
…rypted. (#519) * #503 API Gateway stage is c onfigured for caching and cache is not encrypted. Only wan't to enforce encryption if cachig is enabled. * Update spec/custom_rules/ApiGatewayCacheEncryptedRule_spec.rb correct test message Co-authored-by: Kevin Formsma <kevin.formsma@gmail.com> * Update spec/custom_rules/ApiGatewayCacheEncryptedRule_spec.rb correct test message Co-authored-by: Kevin Formsma <kevin.formsma@gmail.com> * Update spec/custom_rules/ApiGatewayCacheEncryptedRule_spec.rb correct test message Co-authored-by: Kevin Formsma <kevin.formsma@gmail.com> Co-authored-by: Kevin Formsma <kevin.formsma@gmail.com>
- Loading branch information
Showing
5 changed files
with
117 additions
and
0 deletions.
There are no files selected for viewing
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,34 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'cfn-nag/violation' | ||
require_relative 'base' | ||
|
||
class ApiGatewayCacheEncryptedRule < BaseRule | ||
def rule_text | ||
'ApiGateway Deployment should have cache data encryption enabled when caching is enabled' \ | ||
' in StageDescription properties' | ||
end | ||
|
||
def rule_type | ||
Violation::WARNING | ||
end | ||
|
||
def rule_id | ||
'W87' | ||
end | ||
|
||
def audit_impl(cfn_model) | ||
violating_deployments = cfn_model.resources_by_type('AWS::ApiGateway::Deployment').select do |deployment| | ||
violating_deployment?(deployment) | ||
end | ||
|
||
violating_deployments.map(&:logical_resource_id) | ||
end | ||
|
||
private | ||
|
||
def violating_deployment?(deployment) | ||
!deployment.stageDescription.nil? && truthy?(deployment.stageDescription['CachingEnabled']) \ | ||
&& !truthy?(deployment.stageDescription['CacheDataEncrypted']) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
require 'spec_helper' | ||
require 'cfn-model' | ||
require 'cfn-nag/custom_rules/ApiGatewayCacheEncryptedRule' | ||
|
||
describe ApiGatewayCacheEncryptedRule do | ||
context 'Api Gateway has cache encryption enabled' do | ||
it 'returns no violating resources' do | ||
cfn_model = CfnParser.new.parse read_test_template('json/apigateway_cacheencrypted/apigateway_with_cache_encryption_enabled.json') | ||
|
||
actual_logical_resource_ids = ApiGatewayCacheEncryptedRule.new.audit_impl cfn_model | ||
expected_logical_resource_ids = %w[] | ||
|
||
expect(actual_logical_resource_ids).to eq expected_logical_resource_ids | ||
end | ||
end | ||
|
||
context 'Api Gateway has no cache configured' do | ||
it 'returns no violating resources' do | ||
cfn_model = CfnParser.new.parse read_test_template('json/apigateway_cacheencrypted/apigateway_with_no_cache_enabled_missing_stagedescription.json') | ||
|
||
actual_logical_resource_ids = ApiGatewayCacheEncryptedRule.new.audit_impl cfn_model | ||
expected_logical_resource_ids = %w[] | ||
|
||
expect(actual_logical_resource_ids).to eq expected_logical_resource_ids | ||
end | ||
end | ||
|
||
context 'Api Gateway with no cache encryption enabled' do | ||
it 'returns violating resource ids' do | ||
cfn_model = CfnParser.new.parse read_test_template('json/apigateway_cacheencrypted/apigateway_with_no_cache_encryption.json') | ||
|
||
actual_logical_resource_ids = ApiGatewayCacheEncryptedRule.new.audit_impl cfn_model | ||
expected_logical_resource_ids = %w[ApiGatewayWithCacheEncryptionDisabled ApiGatewayWithDefaultCacheEncryption] | ||
|
||
expect(actual_logical_resource_ids).to eq expected_logical_resource_ids | ||
end | ||
end | ||
|
||
end |
13 changes: 13 additions & 0 deletions
13
...st_templates/json/apigateway_cacheencrypted/apigateway_with_cache_encryption_enabled.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,13 @@ | ||
{ | ||
"Resources": { | ||
"ApiGatewayWithCacheEncryption": { | ||
"Type": "AWS::ApiGateway::Deployment", | ||
"Properties": { | ||
"StageDescription": { | ||
"CachingEnabled": "true", | ||
"CacheDataEncrypted": "true" | ||
} | ||
} | ||
} | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
.../apigateway_cacheencrypted/apigateway_with_no_cache_enabled_missing_stagedescription.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,10 @@ | ||
{ | ||
"Resources": { | ||
"ApiGatewayNoCacheEnabled": { | ||
"Type": "AWS::ApiGateway::Deployment", | ||
"Properties": { | ||
|
||
} | ||
} | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
spec/test_templates/json/apigateway_cacheencrypted/apigateway_with_no_cache_encryption.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,21 @@ | ||
{ | ||
"Resources": { | ||
"ApiGatewayWithCacheEncryptionDisabled": { | ||
"Type": "AWS::ApiGateway::Deployment", | ||
"Properties": { | ||
"StageDescription": { | ||
"CachingEnabled": "true", | ||
"CacheDataEncrypted": "false" | ||
} | ||
} | ||
}, | ||
"ApiGatewayWithDefaultCacheEncryption": { | ||
"Type": "AWS::ApiGateway::Deployment", | ||
"Properties": { | ||
"StageDescription": { | ||
"CachingEnabled": "true" | ||
} | ||
} | ||
} | ||
} | ||
} |