8000 AWS::WAFv2::WebACL.CustomResponseBodies and AWS::WAFv2::RuleGroup.CustomResponseBodies by jerry153fish · Pull Request #1899 · cloudtools/troposphere · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

AWS::WAFv2::WebACL.CustomResponseBodies and AWS::WAFv2::RuleGroup.CustomResponseBodies #1899

8000
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

Merged
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions tests/test_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
task_type,
tg_healthcheck_port,
waf_action_type,
wafv2_custom_body_response_content,
wafv2_custom_body_response_content_type
)


Expand Down Expand Up @@ -273,6 +275,20 @@ def test_waf_action_type(self):
with self.assertRaises(ValueError):
waf_action_type(s)

def test_wafv2_custom_body_response_content(self):
for s in ["{'hello': 'world'}", "<!DOCTYPE html><html><head><title>Test</title></head><body><h1>Test</h1><p>Test.</p></body></html>", "Health"]:
wafv2_custom_body_response_content(s)
for s in ["", "a"*10241]:
with self.assertRaises(ValueError):
wafv2_custom_body_response_content(s)

def test_wafv2_custom_body_response_content_type(self):
for s in ["APPLICATION_JSON", "TEXT_HTML", "TEXT_PLAIN"]:
wafv2_custom_body_response_content_type(s)
for s in ["", "APPLICATION", "HTML", "TEXT"]:
with self.assertRaises(ValueError):
wafv2_custom_body_response_content_type(s)


if __name__ == "__main__":
unittest.main()
20 changes: 20 additions & 0 deletions troposphere/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -658,3 +658,23 @@ def ecs_efs_encryption_status(status):
% (", ".join(valid_status))
)
return status


def wafv2_custom_body_response_content(content):
"""validate wafv2 custom body response content. Any character between 1 to 10240
"""
if not content:
raise ValueError("Content must not be empty")
if len(content) > 10240:
raise ValueError("Content maximum length must not exceed 10240")

return content


def wafv2_custom_body_response_content_type(content_type):
"""validate wafv2 custom response content type
"""
valid_types = ["APPLICATION_JSON", "TEXT_HTML", "TEXT_PLAIN"]
if content_type not in valid_types:
raise ValueError('ContentType must be one of: "%s"' % (", ".join(valid_types)))
return content_type
27 changes: 26 additions & 1 deletion troposphere/wafv2.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# See LICENSE file for full license.

from . import AWSObject, AWSProperty, Tags
from .validators import boolean, integer
from .validators import boolean, integer, wafv2_custom_body_response_content, wafv2_custom_body_response_content_type

VALID_TRANSFORMATION_TYPES = (
"CMD_LINE",
Expand Down Expand Up @@ -68,6 +68,22 @@ def validate_positional_constraint(positional_constraint):
return positional_constraint


def validate_custom_response_bodies(custom_response_bodies):
"""validate custom response bodies
"""
if not isinstance(custom_response_bodies, dict):
raise ValueError("CustomResponseBodies must be dict")

for k, v in custom_response_bodies.items():
if not isinstance(v, CustomResponseBody):
raise ValueError(
"value of %s must be type of CustomResponseBody"
% (k)
)

return custom_response_bodies


class ExcludedRule(AWSProperty):
props = {"Name": (str, False)}

Expand Down Expand Up @@ -354,6 +370,7 @@ class WebACL(AWSObject):
resource_type = "AWS::WAFv2::WebACL"

props = {
"CustomResponseBodies": (validate_custom_response_bodies, False),
"DefaultAction": (DefaultAction, False),
"Description": (str, False),
"Name": (str, False),
Expand Down Expand Up @@ -404,6 +421,7 @@ class RuleGroup(AWSObject):

props = {
"Capacity": (integer, False),
"CustomResponseBodies": (validate_custom_response_bodies, False),
"Description": (str, False),
"Name": (str, False),
"Rules": ([RuleGroupRule], False),
Expand All @@ -420,3 +438,10 @@ class WebACLAssociation(AWSObject):
"ResourceArn": (str, True),
"WebACLArn": (str, True),
}


class CustomResponseBody(AWSObject):
props = {
"Content": (wafv2_custom_body_response_content, True),
"ContentType": (wafv2_custom_body_response_content_type, True)
}
0