-
Notifications
You must be signed in to change notification settings - Fork 5
waf regex match and pattern sets #1
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
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 @@ | ||
| # package marker |
This file contains hidden or 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,56 @@ | ||
| import logging | ||
| from urllib.request import urlopen, Request, HTTPError, URLError | ||
| import json | ||
|
|
||
| logger = logging.getLogger() | ||
| logger.setLevel(logging.INFO) | ||
|
|
||
| class CustomResourceResponse: | ||
| def __init__(self, request_payload): | ||
| self.payload = request_payload | ||
| self.response = { | ||
| "StackId": request_payload["StackId"], | ||
| "RequestId": request_payload["RequestId"], | ||
| "LogicalResourceId": request_payload["LogicalResourceId"], | ||
| "Status": 'SUCCESS', | ||
| } | ||
|
|
||
|
|
||
| def respond_error(self, message): | ||
| self.response['Status'] = 'FAILED' | ||
| self.response['Reason'] = message | ||
| self.respond() | ||
|
|
||
| def respond(self, data): | ||
| event = self.payload | ||
| response = self.response | ||
| #### | ||
| #### copied from https://github.com/ryansb/cfn-wrapper-python/blob/master/cfn_resource.py | ||
| #### | ||
|
|
||
| if event.get("PhysicalResourceId", False): | ||
| response["PhysicalResourceId"] = event["PhysicalResourceId"] | ||
|
|
||
| logger.debug("Received %s request with event: %s" % | ||
| (event['RequestType'], json.dumps(event))) | ||
|
|
||
| serialized = json.dumps(response) | ||
| logger.info(f"Responding to {event['RequestType']} request with: {serialized}") | ||
| response["Data"] = data | ||
| req_data = serialized.encode('utf-8') | ||
|
|
||
| req = Request( | ||
| event['ResponseURL'], | ||
| data=req_data, | ||
| headers={'Content-Length': len(req_data), 'Content-Type': ''} | ||
| ) | ||
| req.get_method = lambda: 'PUT' | ||
|
|
||
| try: | ||
| urlopen(req) | ||
| logger.debug("Request to CFN API succeeded, nothing to do here") | ||
| except HTTPError as e: | ||
| logger.error("Callback to CFN API failed with status %d" % e.code) | ||
| logger.error("Response: %s" % e.reason) | ||
| except URLError as e: | ||
| logger.error("Failed to reach the server - %s" % e.reason) |
This file contains hidden or 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,50 @@ | ||
| import sys | ||
| import os | ||
| import re | ||
| import random | ||
|
|
||
| sys.path.append(f"{os.environ['LAMBDA_TASK_ROOT']}/lib") | ||
| sys.path.append(os.path.dirname(os.path.realpath(__file__))) | ||
|
|
||
| import cr_response | ||
| from logic import WafRegexLogic | ||
| import json | ||
|
|
||
| def lambda_handler(event, context): | ||
|
|
||
| print(f"Received event:{json.dumps(event)}") | ||
|
|
||
| lambda_response = cr_response.CustomResourceResponse(event) | ||
| cr_params = event['ResourceProperties'] | ||
| match_name = event['LogicalResourceId'] | ||
| waf_logic = WafRegexLogic(match_name , cr_params) | ||
| try: | ||
| # if create request, generate physical id, both for create/update copy files | ||
| if event['RequestType'] == 'Create': | ||
| print("Create request") | ||
| event['PhysicalResourceId'] = waf_logic.new_match_set() | ||
| data = { | ||
| "MatchID" : event['PhysicalResourceId'] | ||
| } | ||
| lambda_response.respond(data) | ||
|
|
||
| elif event['RequestType'] == 'Update': | ||
| print("Update request") | ||
| waf_logic.update_match_set(event['PhysicalResourceId']) | ||
| data = { | ||
| "MatchID" : event['PhysicalResourceId'] | ||
| } | ||
| lambda_response.respond(data) | ||
|
|
||
| elif event['RequestType'] == 'Delete': | ||
| print(event['PhysicalResourceId']) | ||
| waf_logic.remove_match_set(event['PhysicalResourceId']) | ||
| print("Delete request") | ||
| data = { } | ||
| lambda_response.respond(data) | ||
|
|
||
| except Exception as e: | ||
| message = str(e) | ||
| lambda_response.respond_error(message) | ||
|
|
||
| return 'OK' | ||
This file contains hidden or 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,159 @@ | ||
| import boto3 | ||
| import os | ||
| import glob | ||
| import logging | ||
| import pprint | ||
|
|
||
|
|
||
| logger = logging.getLogger() | ||
| logger.setLevel(logging.INFO) | ||
|
|
||
|
|
||
| class WafRegexLogic: | ||
|
|
||
| def __init__(self, name, resource_properties): | ||
| self.regex_patterns = resource_properties['RegexPatterns'] | ||
| self.match_type = resource_properties['Type'] | ||
| self.match_data = resource_properties['Data'] | ||
| self.transform = resource_properties['Transform'] | ||
| self.match_name = name | ||
| self.pattern_name = f"{name}-pattern" | ||
| self.client = boto3.client('waf-regional') | ||
|
|
||
| def new_pattern_set(self): | ||
| changeToken = self.client.get_change_token() | ||
| response_create_pattern_set = self.client.create_regex_pattern_set( | ||
| Name=self.pattern_name, | ||
| ChangeToken=changeToken['ChangeToken'] | ||
| ) | ||
| for pattern in self.regex_patterns: | ||
| self.insert_pattern_set(response_create_pattern_set['RegexPatternSet']['RegexPatternSetId'], pattern) | ||
| return response_create_pattern_set['RegexPatternSet']['RegexPatternSetId'] | ||
|
|
||
| def remove_pattern_set(self,pattern_set_id): | ||
| pattern_set_object = self.get_pattern_set(pattern_set_id) | ||
| for pattern_set_string in pattern_set_object['RegexPatternSet']['RegexPatternStrings']: | ||
| self.delete_pattern_set(pattern_set_id, pattern_set_string) | ||
| changeToken = self.client.get_change_token() | ||
| response = self.client.delete_regex_pattern_set( | ||
| RegexPatternSetId=pattern_set_id, | ||
| ChangeToken=changeToken['ChangeToken'] | ||
| ) | ||
|
|
||
| def get_pattern_set(self,pattern_set_id): | ||
| pattern_set_object = self.client.get_regex_pattern_set( | ||
| RegexPatternSetId=pattern_set_id | ||
| ) | ||
| return pattern_set_object | ||
|
|
||
|
|
||
| def update_pattern_set(self,pattern_set_id): | ||
| pattern_set_object = self.get_pattern_set(pattern_set_id) | ||
| #delete existing and add a new one | ||
| for pattern_set_string in pattern_set_object['RegexPatternSet']['RegexPatternStrings']: | ||
| self.delete_pattern_set(pattern_set_id, pattern_set_string) | ||
| for pattern in self.regex_patterns: | ||
| self.insert_pattern_set(pattern_set_id, pattern) | ||
|
|
||
|
|
||
| def insert_pattern_set(self,pattern_set_id, pattern_set_string): | ||
| changeToken = self.client.get_change_token() | ||
| update_regex_patternset = self.client.update_regex_pattern_set( | ||
| RegexPatternSetId=pattern_set_id, | ||
| Updates=[ | ||
| { | ||
| 'Action': 'INSERT', | ||
| 'RegexPatternString': pattern_set_string | ||
| }, | ||
| ], | ||
| ChangeToken=changeToken['ChangeToken'] | ||
| ) | ||
|
|
||
| def delete_pattern_set(self,pattern_set_id, pattern_set_string): | ||
| changeToken = self.client.get_change_token() | ||
| update_regex_patternset = self.client.update_regex_pattern_set( | ||
| RegexPatternSetId=pattern_set_id, | ||
| Updates=[ | ||
| { | ||
| 'Action': 'DELETE', | ||
| 'RegexPatternString': pattern_set_string | ||
| }, | ||
| ], | ||
| ChangeToken=changeToken['ChangeToken'] | ||
| ) | ||
|
|
||
| # | ||
| # Match Sets | ||
| # | ||
|
|
||
| def new_match_set(self): | ||
| changeToken = self.client.get_change_token() | ||
| #create match set | ||
| response_create_match_set = self.client.create_regex_match_set( | ||
| Name=self.match_name, | ||
| ChangeToken=changeToken['ChangeToken'] | ||
| ) | ||
| #create pattern set | ||
| pattern_set_id = self.new_pattern_set() | ||
| self.insert_match_set(response_create_match_set['RegexMatchSet']['RegexMatchSetId'], pattern_set_id) | ||
| return response_create_match_set['RegexMatchSet']['RegexMatchSetId'] | ||
|
|
||
| def insert_match_set(self, match_set_id, pattern_set_id): | ||
| changeToken = self.client.get_change_token() | ||
| update_regex_matchset = self.client.update_regex_match_set( | ||
| RegexMatchSetId=match_set_id, | ||
| Updates=[ | ||
| { | ||
| 'Action': 'INSERT', | ||
| 'RegexMatchTuple': { | ||
| 'FieldToMatch': { | ||
| 'Type': self.match_type, | ||
| 'Data': self.match_data | ||
| }, | ||
| 'TextTransformation': self.transform, | ||
| 'RegexPatternSetId': pattern_set_id | ||
| } | ||
| }, | ||
| ], | ||
| ChangeToken=changeToken['ChangeToken'] | ||
| ) | ||
|
|
||
| def update_match_set(self,match_set_id): | ||
| match_set_object = self.get_match_set(match_set_id) | ||
| for match_tuple in match_set_object['RegexMatchSet']['RegexMatchTuples']: | ||
| self.update_pattern_set(match_tuple['RegexPatternSetId']) | ||
| self.delete_match_set(match_set_id,match_tuple) | ||
| self.insert_match_set(match_set_id,match_tuple['RegexPatternSetId']) | ||
|
|
||
|
|
||
| def get_match_set(self,match_set_id): | ||
| match_set_object = self.client.get_regex_match_set( | ||
| RegexMatchSetId=match_set_id | ||
| ) | ||
| return match_set_object | ||
|
|
||
| def remove_match_set(self,match_set_id): | ||
| match_set_object = self.get_match_set(match_set_id) | ||
|
|
||
| for match_tuple in match_set_object['RegexMatchSet']['RegexMatchTuples']: | ||
| self.delete_match_set(match_set_id,match_tuple) | ||
| self.remove_pattern_set(match_tuple['RegexPatternSetId']) | ||
|
|
||
| changeToken = self.client.get_change_token() | ||
| response = self.client.delete_regex_match_set( | ||
| RegexMatchSetId=match_set_id, | ||
| ChangeToken=changeToken['ChangeToken'] | ||
| ) | ||
|
|
||
| def delete_match_set(self,match_set_id,match_tuple): | ||
| changeToken = self.client.get_change_token() | ||
| update_regex_matchset = self.client.update_regex_match_set( | ||
| RegexMatchSetId=match_set_id, | ||
| Updates=[ | ||
| { | ||
| 'Action': 'DELETE', | ||
| 'RegexMatchTuple': match_tuple | ||
| }, | ||
| ], | ||
| ChangeToken=changeToken['ChangeToken'] | ||
| ) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@rrakhmanto respond to CF on delete