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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304import psycopg2
import json
import traceback
from data.access import connection
from utils.watch import logger
from psycopg2.pool import SimpleConnectionPool
# Set use_pooling to True to enable connection pooling
use_pooling = True
# Connection pool
pool = None
if use_pooling:
conn_params = connection().get_connection_params()
pool = SimpleConnectionPool(
minconn=1,
maxconn=10,
**conn_params
)
def connection_pooling():
return pool.getconn()
def release_pooling(conn):
pool.putconn(conn)
# Normal Insert
def execute_insert(query, params=None, fetchone=True):
# logger.debug(f"๐๏ธโ๏ธ Executing query: {query}")
# logger.debug(f"๐๏ธโ๏ธ Query parameters: {params}")
# Connect to the database
if use_pooling:
conn = connection_pooling()
else:
conn = connection()
conn.open()
logger.debug("๐๏ธโ๏ธ Database connection opened")
# Create a cursor
cur = conn.cursor() # Removed conn.
try:
# Execute the query
cur.execute(query, params)
conn.commit() # Removed conn.
logger.info("๐๏ธโ๏ธ๐ข Query executed and committed")
# Fetch the results if requested
result = None
if fetchone:
result = cur.fetchone() or () # return an empty tuple if None is returned
else:
result = cur.fetchall() or [] # return an empty list if None is returned
logger.debug(f'๐๏ธโ๏ธ Fetched results: {result}')
except Exception as e:
logger.error(f"๐๏ธโ๏ธ Error executing insert query: {e}\n{traceback.format_exc()}")
result = None
# Close the cursor and connection
cur.close()
if use_pooling:
release_pooling(conn)
else:
conn.close()
logger.debug("๐๏ธโ๏ธ Cursor and connection closed")
return result
# # # # # # # # # #
# Bulk Inserts
def execute_bulk_insert(query, params_list, fetchone=True):
# Connect to the database
if use_pooling:
conn = connection_pooling()
else:
conn = connection()
conn.open()
# Create a cursor
cur = conn.cursor()
try:
# Execute the query
with conn:
cur.executemany(query, params_list)
logger.info("๐๏ธโ๏ธ๐ข Query executed and committed")
# Fetch the results if requested
result = None
if fetchone:
result = cur.fetchone() or () # return an empty tuple if None is returned
else:
result = cur.fetchall() or [] # return an empty list if None is returned
logger.debug(f'๐๏ธโ๏ธ Fetched results: {result}')
except Exception as e:
logger.error(f"๐๏ธโ๏ธ Error executing bulk insert query: {e}\n{traceback.format_exc()}")
result = None
# Close the cursor and connection
cur.close()
if use_pooling:
release_pooling(conn)
else:
conn.close()
return result
#########################################################
## Queries
# Add Tech Apps
def record_tech_apps(name, description, icon, saas, website, pricing, scriptsrc, headers, cookies, dom, implies, cat_implies, js, requires, requires_cat, meta, cats):
logger.info(f'๐๏ธโ๏ธ Adding {name} to Tech Apps ')
query = """
INSERT INTO ref.tech_apps (
name,
description,
icon,
saas,
website,
pricing,
scriptsrc,
headers,
cookies,
dom,
implies,
cat_implies,
js,
requires,
requires_cat,
meta,
cats
)
VALUES ( %s, %s, %s, %s,
%s, %s, %s, %s,
%s, %s, %s, %s,
%s, %s, %s, %s, %s)
ON CONFLICT (name) DO UPDATE
SET description = excluded.description,
icon = excluded.icon,
saas = excluded.saas,
website = excluded.website,
pricing = excluded.pricing,
scriptsrc = excluded.scriptsrc,
headers = excluded.headers,
cookies = excluded.cookies,
dom = excluded.dom,
implies = excluded.implies,
cat_implies = excluded.cat_implies,
js = excluded.js,
requires = excluded.requires,
requires_cat = excluded.requires_cat,
meta = excluded.meta,
cats = excluded.cats
RETURNING id;
"""
try:
execute_insert(query, (name, description, icon, saas, website, pricing, scriptsrc, headers, cookies, dom, implies, cat_implies, js, requires, requires_cat, meta, cats))
# Log Success
logger.debug(f'๐๏ธ โ๏ธUPDATED: {name}')
return True
except Exception as e:
logger.error(f'๐๏ธ โ๏ธFailed to complete update: {name} - Error: {e}') # Display the error message
return False
def scan_axe_new_event(url_id, scanned_at, failure, axe_meta):
logger.debug(f'Creating new scan event for {url_id}...')
query = """
INSERT INTO events.scans_axe (
url_id,
scanned_at,
failure,
axe_meta
)
VALUES (
%s, %s, %s, %s
)
RETURNING id as scan_event_id;
"""
try:
result = execute_insert(query, (url_id, scanned_at, failure, axe_meta))
# Log the Yay!
scan_event_id = result[0]
logger.debug(f'๐๏ธ โ๏ธUPDATED: {url_id}')
return scan_event_id
except Exception as e:
logger.error(f'๐๏ธ โ๏ธFailed to complete update: {url_id} - Error: {e}')
# Display the error message
return False
# Add Axe Results
# Add Items
def insert_axe_items(scan_event_id, url_id, type, area, impact, tags):
logger.debug(f'Insert: Beginning to add items...')
query = """
INSERT INTO results.axe_items (
scan_event_id,
url_id,
type,
area,
impact,
tags
)
VALUES (
%s, %s, %s,
%s, %s, %s
)
RETURNING id as insert_id;
"""
result = execute_bulk_insert(query, [(scan_event_id, url_id, type, area, impact, tag) for tag in tags])
if result:
logger.info(f'Insert: New Item Added...')
return True
else:
logger.error(f'Insert: Problem adding item...')
return False
# Add Nodes
def insert_axe_nodes(scan_event_id, url_id, html, impact, target, data, failure_summary):
logger.debug(f'Insert: Beginning to add nodes...')
query = """
INSERT INTO results.axe_nodes (
scan_event_id,
url_id,
html,
impact,
target,
data,
failure_summary
)
VALUES (
%s, %s, %s, %s,
%s, %s, %s
)
RETURNING id as insert_id;
"""
result = execute_insert(query, (scan_event_id, url_id, html, impact, target, data, failure_summary))
if result and isinstance(result[0], int):
logger.info(f'Insert: New Node Added...')
return True
else:
logger.error(f'Insert: Problem adding node...')
return False
# Add Subnodes
def insert_axe_subnodes(scan_event_id, url_id, data, node_id, impact, message, node_type, related_nodes):
logger.debug(f'Insert: Beginning to add subnodes...')
query = """
INSERT INTO results.axe_subnodes (
scan_event_id,
url_id,
node_id,
data,
impact,
node_type,
message,
related_nodes
)
VALUES (
%s, %s, %s, %s,
%s, %s, %s, %s
)
RETURNING id as insert_id;
"""
result = execute_insert(query, (scan_event_id, url_id, node_id, data, impact, node_type, message, related_nodes))
if result and isinstance(result[0], int):
logger.info(f'Insert: New Subnode Added...')
return True
else:
logger.error(f'Insert: Problem adding subnode...')
return False
def add_tech_results(url_id, tech_apps):
logger.info(f'๐๏ธ โ๏ธ Adding Tech Results')
query = """
INSERT INTO results.tech_checks (
url_id,
techs
)
VALUES (
%s, %s
)
RETURNING url_id;
"""
try:
execute_insert(query, (url_id, json.dumps(tech_apps)))
logger.debug(f'๐๏ธ โ๏ธ UPDATED: {url_id}')
return True
except Exception as e: # Add 'Exception as e' to capture the exception details
logger.error(f'๐๏ธ โ๏ธ Failed to complete update: {url_id} - Error: {e}') # Display the error message
return False