22import json
33from datetime import datetime
44
5+ from snowflake .connector .secret_detector import SecretDetector
6+
7+ # Domains to ignore (pip/installation traffic)
8+ IGNORE_DOMAINS = {
9+ "pypi.org" ,
10+ "files.pythonhosted.org" ,
11+ "example.com" , # Test domain from setup
12+ }
13+
514# Open CSV file for writing requests
615f = open ("test_requests.csv" , "w" , newline = "" , encoding = "utf-8" )
716writer = csv .writer (f )
2938def response (flow ):
3039 """Called when a response is received"""
3140 try :
41+ # Skip if domain should be ignored
42+ host = flow .request .pretty_host .lower ()
43+ if any (ignored_domain in host for ignored_domain in IGNORE_DOMAINS ):
44+ return
45+
3246 # Calculate duration
3347 duration_ms = (
3448 int ((flow .response .timestamp_end - flow .request .timestamp_start ) * 1000 )
@@ -40,16 +54,22 @@ def response(flow):
4054 request_size = len (flow .request .content ) if flow .request .content else 0
4155 response_size = len (flow .response .content ) if flow .response .content else 0
4256
43- # Convert headers to JSON strings (easier to parse later)
44- request_headers = json . dumps ( dict (flow .request .headers ) )
45- response_headers = json . dumps ( dict (flow .response .headers ) )
57+ # Convert headers to JSON strings and mask secrets
58+ request_headers_dict = dict (flow .request .headers )
59+ response_headers_dict = dict (flow .response .headers )
4660
47- # Extract key info
61+ request_headers = SecretDetector .mask_secrets (
62+ json .dumps (request_headers_dict )
63+ ).masked_text
64+ response_headers = SecretDetector .mask_secrets (
65+ json .dumps (response_headers_dict )
66+ ).masked_text
67+
68+ # Extract key info and mask sensitive data
4869 timestamp = datetime .now ().isoformat ()
4970 method = flow .request .method
50- url = flow .request .pretty_url
51- host = flow .request .pretty_host
52- path = flow .request .path
71+ url = SecretDetector .mask_secrets (flow .request .pretty_url ).masked_text
72+ path = SecretDetector .mask_secrets (flow .request .path ).masked_text
5373 status_code = flow .response .status_code
5474 reason = flow .response .reason
5575 content_type = flow .response .headers .get ("content-type" , "" )
@@ -76,25 +96,31 @@ def response(flow):
7696 f .flush () # Ensure it's written immediately
7797
7898 except Exception as e :
79- # Write error row
80- writer .writerow (
81- [
82- datetime .now ().isoformat (),
83- "ERROR" ,
84- str (e ),
85- "" ,
86- "" ,
87- "" ,
88- "" ,
89- "" ,
90- "" ,
91- "" ,
92- "" ,
93- "" ,
94- "" ,
95- ]
96- )
97- f .flush ()
99+ # Write error row (only for non-ignored domains)
100+ if "host" in locals ():
101+ host_check = locals ()["host" ]
102+ else :
103+ host_check = getattr (flow .request , "pretty_host" , "" ).lower ()
104+
105+ if not any (ignored_domain in host_check for ignored_domain in IGNORE_DOMAINS ):
106+ writer .writerow (
107+ [
108+ datetime .now ().isoformat (),
109+ "ERROR" ,
110+ SecretDetector .mask_secrets (str (e )).masked_text ,
111+ "" ,
112+ "" ,
113+ "" ,
114+ "" ,
115+ "" ,
116+ "" ,
117+ "" ,
118+ "" ,
119+ "" ,
120+ "" ,
121+ ]
122+ )
123+ f .flush ()
98124
99125
100126def done ():
0 commit comments