forked from Isomaniac/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcookie.txt
More file actions
228 lines (188 loc) · 8.65 KB
/
cookie.txt
File metadata and controls
228 lines (188 loc) · 8.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
"""
index.py - akamaivalidator with Enhanced UCAS (Verbose Logging)
Version: 2.0
Features:
- Execution modes (PRODUCTION, DEBUG, VERBOSE)
- Query logging (actual Athena queries)
- Response logging (full results)
- DynamoDB configuration
"""
import json
import os
import sys
from datetime import datetime
from logger import logger
from input import extract_lambda_input
from data import get_akamai_onboarding_compliance
from validate import validate_akamai
from output import build_result, write_result
# Import UCAS Debugger
from ucas_debugger import UCASDebugger
# Initialize UCAS with environment detection and validator name
is_local = os.getenv('AWS_EXECUTION_ENV') is None
ucas = UCASDebugger(
logger,
colored_output=is_local,
validator_name='akamaivalidator' # For DynamoDB config lookup
)
def handler(event, context):
"""
Akamai Validator Lambda with Enhanced UCAS debugging.
Supports execution modes:
- PRODUCTION: Minimal logging
- DEBUG: Standard UCAS output
- VERBOSE: Super detailed (queries, responses, raw data)
Toggle mode via DynamoDB (no code changes needed!)
"""
# Get actual Lambda request ID
request_id = getattr(context, 'aws_request_id', 'local') if context else 'local'
ucas.method_start("handler", request_id=request_id)
try:
# 1. Parse input
ucas.method_start("extract_lambda_input")
try:
identifier, control, validator = extract_lambda_input(event)
# VERBOSE: Log raw event data
ucas.log_raw_data("extract_lambda_input", event, "Raw Event")
ucas.method_stop("extract_lambda_input",
identifier=identifier,
control=control[:30] if len(control) > 30 else control)
except ValueError as exc:
ucas.method_stop("extract_lambda_input",
error_type=type(exc).__name__,
error_message=str(exc))
logger.error(f"Input parsing failed: {exc}")
ucas.method_stop("handler", final_status="ERROR", error_type="ValueError")
return {"error": str(exc), "status": "FAILED"}
# 2. Collect data from Athena
logger.info(f"Querying Akamai onboarding data for AppCI {identifier}")
ucas.method_start("get_akamai_onboarding_compliance", appci=identifier)
try:
# VERBOSE: Log the actual Athena query
# Note: You'll need to modify data.py to return both query and results
# For now, we'll construct the approximate query for logging
athena_query = f"""
SELECT *
FROM akamai_onboarding_shared_resources
WHERE appci = '{identifier}'
ORDER BY ingestion_date DESC
LIMIT 1
"""
ucas.log_query("get_akamai_onboarding_compliance", athena_query, "Athena SQL")
# Execute query
akamai_data = get_akamai_onboarding_compliance(identifier)
# VERBOSE: Log the actual response
ucas.log_response("get_akamai_onboarding_compliance", akamai_data, "Athena Results")
# VERBOSE: Log raw data structure
if akamai_data:
ucas.log_raw_data("get_akamai_onboarding_compliance", akamai_data[0], "First Record")
ucas.method_stop("get_akamai_onboarding_compliance",
record_count=len(akamai_data) if akamai_data else 0)
except Exception as e:
ucas.method_stop("get_akamai_onboarding_compliance",
error_type=type(e).__name__,
error_message=str(e))
raise
# 3. Validate
ucas.method_start("validate_akamai", validator_type=validator.get('type'))
try:
# VERBOSE: Log validation criteria
ucas.log_raw_data("validate_akamai", validator, "Validator Config")
passed = validate_akamai(validator, akamai_data)
# VERBOSE: Log validation details
validation_details = {
'criteria': validator.get('criteria', []),
'data_present': bool(akamai_data),
'result': 'PASS' if passed else 'FAIL'
}
ucas.log_raw_data("validate_akamai", validation_details, "Validation Details")
ucas.method_stop("validate_akamai", validation_result="PASS" if passed else "FAIL")
except Exception as e:
ucas.method_stop("validate_akamai",
error_type=type(e).__name__,
error_message=str(e))
raise
# 4. Build output
ucas.method_start("build_result", identifier=identifier)
try:
result = build_result(identifier, control, validator, passed)
# VERBOSE: Log result structure
ucas.log_raw_data("build_result", result, "Built Result")
ucas.method_stop("build_result", result_size=len(str(result)))
except Exception as e:
ucas.method_stop("build_result",
error_type=type(e).__name__,
error_message=str(e))
raise
# 5. Write to DynamoDB
ucas.method_start("write_result", identifier=identifier)
try:
write_result(identifier, control, validator, result)
# VERBOSE: Log DynamoDB write confirmation
ucas.log_raw_data("write_result",
{'table': 'eqa-validation-results-table', 'identifier': identifier},
"DynamoDB Write")
ucas.method_stop("write_result", status="success")
except Exception as e:
ucas.method_stop("write_result",
error_type=type(e).__name__,
error_message=str(e))
raise
# Success
logger.info(f"Validation complete — control: {control}, passed: {passed}")
ucas.method_stop("handler",
final_status="PASS" if passed else "FAIL",
validation_passed=passed,
result_size=len(str(result)))
return result
except Exception as e:
# Error handling
error_type = type(e).__name__
error_message = str(e)
# Log error with UCAS
ucas.print_error(e)
ucas.method_stop("handler",
final_status="ERROR",
error_type=error_type,
error_message=error_message)
# In Lambda: raise the error
# Locally: exit cleanly
if is_local:
sys.exit(1)
else:
raise
# ---------------------------------------------------------------------------
# Local testing — run directly: python index.py
# ---------------------------------------------------------------------------
if __name__ == "__main__":
# Mock context for local testing
class MockContext:
aws_request_id = "local"
function_name = "local"
test_event = {
"identifier": "BFM",
"control": "CDR-NSS3.4.2 - App01",
"validator": {
"type": "akamai",
"criteria": ["akamai"]
}
}
# Print header (only locally)
if is_local:
from ucas_debugger import Colors
print(f"\n{Colors.HEADER}{Colors.BOLD}{'='*70}{Colors.END}")
print(f"{Colors.HEADER}{Colors.BOLD} UCAS Debugger - akamaivalidator (Enhanced V2){Colors.END}")
print(f"{Colors.HEADER}{Colors.BOLD} {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}{Colors.END}")
print(f"{Colors.HEADER}{Colors.BOLD}{'='*70}{Colors.END}\n")
# Run handler
try:
result = handler(test_event, MockContext())
# Print summary (only locally)
if is_local:
status = "PASS" if result.get('passed') else "FAIL"
status_color = Colors.PASS if result.get('passed') else Colors.ERROR
print(f"\n{Colors.HEADER}{Colors.BOLD}{'='*70}{Colors.END}")
print(f"{status_color}{Colors.BOLD}{status}{Colors.END} | {test_event['identifier']} | {result['control']}")
print(f"{Colors.HEADER}{Colors.BOLD}{'='*70}{Colors.END}\n")
except SystemExit:
pass # Clean exit on error