Skip to content

Commit

Permalink
chore: adjust comments and input strings
Browse files Browse the repository at this point in the history
  • Loading branch information
helio-neto committed Feb 28, 2025
1 parent ffaa138 commit 65ba3f2
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 47 deletions.
14 changes: 9 additions & 5 deletions src/akamai/converter_rules_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
map_origin_type,
replace_variables,
map_operator,
map_variable,
behavior_key
)
from utils import sanitize_name
Expand Down Expand Up @@ -142,14 +143,15 @@ def assemble_request_rule(
Dict[str, Any]: Rule engine resource.
"""
phase = "request" if rule_name != "default" else "default"
rule_description = rule.get("comments", "").replace("\n", " ").replace("\r", " ").replace("\"", "'")
resource = {
"type": "azion_edge_application_rule_engine",
"name": sanitize_name(rule_name),
"attributes": {
"edge_application_id": f"azion_edge_application_main_setting.{main_setting_name}.edge_application.application_id",
"results": {
"name": "Default Rule" if phase == "default" else sanitize_name(rule_name),
"description": rule.get("comments", ""),
"description": rule_description,
"phase": phase,
"behaviors": request_behaviors
},
Expand Down Expand Up @@ -203,15 +205,16 @@ def assemble_response_rule(
break
else:
selected_criteria = azion_criteria.get("response_default")


rule_description = rule.get("comments", "").replace("\n", " ").replace("\r", " ").replace("\"", "'")
resource = {
"type": "azion_edge_application_rule_engine",
"name": name,
"attributes": {
"edge_application_id": f"azion_edge_application_main_setting.{main_setting_name}.edge_application.application_id",
"results": {
"name": name,
"description": rule.get("comments", ""),
"description": rule_description,
"phase": "response",
"behaviors": behaviors
},
Expand Down Expand Up @@ -391,7 +394,7 @@ def process_criteria(
"akamai_behavior": mapping.get("akamai_behavior",""),
}
if input_value is not None:
entry["input_value"] = input_value
entry["input_value"] = input_value.replace("\r", "")

# Append to the correct phase
if mapping.get("phase") == "response":
Expand Down Expand Up @@ -550,6 +553,7 @@ def behavior_capture_match_groups(
regex_value = replace_variables(options.get('regex')).replace('/', r'\/').replace('.', r'\\.')
random_number = random.randint(1000, 9999)
captured_array = options.get("variableName",f"var{random_number}")[:10]
subject = map_variable(options.get("variableValue"))
azion_behavior = {
"name": mapping["azion_behavior"],
"enabled": True,
Expand All @@ -559,7 +563,7 @@ def behavior_capture_match_groups(
),
"target": {
"captured_array": f'"{captured_array}"',
"subject": f'{replace_variables(options.get("variableValue"))}',
"subject": f'{subject}',
"regex": f"\"(.*)\\\\/{regex_value}\"",
}
}
Expand Down
86 changes: 44 additions & 42 deletions src/akamai/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,46 @@
from typing import Dict, Any, Optional, List
from utils import clean_and_parse_json, sanitize_name

operator_map = {
"EQUALS": "is_equal",
"EQUALS_ONE_OF": "is_equal",
"DOES_NOT_EQUAL": "is_not_equal",
"DOES_NOT_EQUAL_ONE_OF": "is_not_equal",
"MATCHES": "matches",
"MATCHES_ONE_OF": "matches",
"DOES_NOT_MATCH": "does_not_match",
"DOES_NOT_MATCH_ONE_OF": "does_not_match",
"STARTS_WITH": "starts_with",
"STARTS_WITH_ONE_OF": "starts_with",
"DOES_NOT_START_WITH": "does_not_start_with",
"EXISTS": "exists",
"DOES_NOT_EXIST": "does_not_exist",
"IS_ONE_OF": "is_equal",
"IS_NOT_ONE_OF": "is_not_equal"
}
OPERATOR_MAP = {
"EQUALS": "is_equal",
"EQUALS_ONE_OF": "is_equal",
"DOES_NOT_EQUAL": "is_not_equal",
"DOES_NOT_EQUAL_ONE_OF": "is_not_equal",
"MATCHES": "matches",
"MATCHES_ONE_OF": "matches",
"DOES_NOT_MATCH": "does_not_match",
"DOES_NOT_MATCH_ONE_OF": "does_not_match",
"STARTS_WITH": "starts_with",
"STARTS_WITH_ONE_OF": "starts_with",
"DOES_NOT_START_WITH": "does_not_start_with",
"EXISTS": "exists",
"DOES_NOT_EXIST": "does_not_exist",
"IS_ONE_OF": "is_equal",
"IS_NOT_ONE_OF": "is_not_equal"
}

# Context-aware mappings (direct mapping based on Akamai builtin variable)
AKAMAI_TO_AZION_MAP = {
"AK_PATH": "$${uri}",
"AK_CLIENT_IP": "$${remote_addr}",
"AK_ORIGINAL_URL": "$${request}",
"AK_SCHEME": "$${scheme}",
"AK_QUERY": "$${args}",
"AK_METHOD": "$${request_method}",
"AK_HOST": "$${host}",
"AK_TLS_VERSION": "$${tls_version}",
"AK_CLIENT_REAL_IP": "$${remote_addr}",
"AK_CLIENT_RTT": "$${rtt}",
"AK_CLIENT_USER_AGENT": "$${user_agent}",
"AK_CLIENT_ACCEPT_LANGUAGE": "$${http_accept_language}",
"AK_CLIENT_ACCEPT_ENCODING": "$${http_accept_encoding}",
"AK_CLIENT_ACCEPT_CHARSET": "$${http_accept_charset}",
"AK_CLIENT_COOKIE": "$${cookie_name}",
"AK_CLIENT_REFERER": "$${http_referer}",
"PMUSER_REDIR": "$${variable}",
"PMUSER_REDIR2": "$${variable}",
# Add more mappings as needed...
}

def get_main_setting_name(akamai_config: dict) -> str:
"""
Expand Down Expand Up @@ -153,32 +176,11 @@ def map_variable(value: str) -> str:
# Check if the variable has the 'builtin.' prefix and remove it if present
if value.startswith("{{builtin."):
value = value.replace("{{builtin.", "").replace("}}", "")

# Context-aware mappings (direct mapping based on Akamai builtin variable)
akamai_to_azion_map = {
"AK_PATH": "$${uri}",
"AK_CLIENT_IP": "$${remote_addr}",
"AK_ORIGINAL_URL": "$${request}",
"AK_SCHEME": "$${scheme}",
"AK_QUERY": "$${args}",
"AK_METHOD": "$${request_method}",
"AK_HOST": "$${host}",
"AK_TLS_VERSION": "$${tls_version}",
"AK_CLIENT_REAL_IP": "$${remote_addr}",
"AK_CLIENT_RTT": "$${rtt}",
"AK_CLIENT_USER_AGENT": "$${user_agent}",
"AK_CLIENT_ACCEPT_LANGUAGE": "$${http_accept_language}",
"AK_CLIENT_ACCEPT_ENCODING": "$${http_accept_encoding}",
"AK_CLIENT_ACCEPT_CHARSET": "$${http_accept_charset}",
"AK_CLIENT_COOKIE": "$${cookie_name}",
"AK_CLIENT_REFERER": "$${http_referer}",
"PMUSER_REDIR": "$${variable}",
"PMUSER_REDIR2": "$${variable}",
# Add more mappings as needed...
}
if value.startswith("{{user."):
value = value.replace("{{user.", "").replace("}}", "")

# Get the appropriate mapping for the variable or return the original value as a fallback
return akamai_to_azion_map.get(value, value)
return AKAMAI_TO_AZION_MAP.get(value, value)

def replace_variables(input_string: str) -> str:
"""
Expand Down Expand Up @@ -283,7 +285,7 @@ def map_operator(akamai_operator: str) -> str:
Returns:
str: Azion operator
"""
return operator_map.get(akamai_operator, "matches") # default to matches if unknown
return OPERATOR_MAP.get(akamai_operator, "matches") # default to matches if unknown

def is_positive_operator(operator: str) -> bool:
"""
Expand Down

0 comments on commit 65ba3f2

Please sign in to comment.