1- import psycopg2
1+ import pymysql
22from hawk_scanner .internals import system
3- import re
43from rich .console import Console
5- from rich .table import Table
64
75console = Console ()
86
9- def connect_postgresql (host , port , user , password , database ):
7+ def connect_mysql (host , port , user , password , database ):
108 try :
11- conn = psycopg2 .connect (
9+ conn = pymysql .connect (
1210 host = host ,
1311 port = port ,
1412 user = user ,
1513 password = password ,
1614 database = database
1715 )
1816 if conn :
19- system .print_info (f"Connected to PostgreSQL database at { host } " )
17+ system .print_info (f"Connected to MySQL database at { host } " )
2018 return conn
2119 except Exception as e :
22- system .print_error (f"Failed to connect to PostgreSQL database at { host } with error: { e } " )
20+ system .print_error (f"Failed to connect to MySQL database at { host } with error: { e } " )
2321
24- def check_data_patterns (conn , patterns , profile_name , database_name ):
22+ def check_data_patterns (conn , patterns , profile_name , database_name , limit_start = 0 , limit_end = 500 , tables = None ):
2523 cursor = conn .cursor ()
26- cursor .execute ("SELECT table_name FROM information_schema.tables WHERE table_schema = 'public'" )
24+
25+ # Get the list of tables to scan
26+ cursor .execute ("SHOW TABLES" )
2727 tables = [table [0 ] for table in cursor .fetchall ()]
28+ tables_to_scan = tables or [] # Use all tables if tables[] is blank or not provided
2829
2930 table_count = 1
3031
3132 results = []
32- for table in tables :
33- cursor .execute (f"SELECT * FROM { table } " )
33+ for table in tables_to_scan :
34+ cursor .execute (f"SELECT * FROM { table } LIMIT { limit_end } OFFSET { limit_start } " )
3435 columns = [column [0 ] for column in cursor .description ]
3536
3637 data_count = 1
@@ -42,15 +43,15 @@ def check_data_patterns(conn, patterns, profile_name, database_name):
4243 if matches :
4344 for match in matches :
4445 results .append ({
45- 'host' : conn .dsn ,
46+ 'host' : conn .get_host_info () ,
4647 'database' : database_name ,
4748 'table' : table ,
4849 'column' : column ,
4950 'pattern_name' : match ['pattern_name' ],
5051 'matches' : match ['matches' ],
5152 'sample_text' : match ['sample_text' ],
5253 'profile' : profile_name ,
53- 'data_source' : 'postgresql '
54+ 'data_source' : 'mysql '
5455 })
5556
5657 data_count += 1
@@ -62,33 +63,40 @@ def check_data_patterns(conn, patterns, profile_name, database_name):
6263
6364def execute (args ):
6465 results = []
65- system .print_info (f"Running Checks for PostgreSQL Sources" )
66+ system .print_info (f"Running Checks for MySQL Sources" )
6667 connections = system .get_connection ()
6768
6869 if 'sources' in connections :
6970 sources_config = connections ['sources' ]
70- postgresql_config = sources_config .get ('postgresql ' )
71+ mysql_config = sources_config .get ('mysql ' )
7172
72- if postgresql_config :
73+ if mysql_config :
7374 patterns = system .get_fingerprint_file ()
7475
75- for key , config in postgresql_config .items ():
76+ for key , config in mysql_config .items ():
7677 host = config .get ('host' )
7778 user = config .get ('user' )
78- port = config .get ('port' , 5432 ) # default port for PostgreSQL
79+ port = config .get ('port' , 3306 ) # default port for MySQL
7980 password = config .get ('password' )
8081 database = config .get ('database' )
82+ limit_start = config .get ('limit_start' , 0 )
83+ limit_end = config .get ('limit_end' , 500 )
84+ tables = config .get ('tables' , [])
8185
8286 if host and user and password and database :
83- system .print_info (f"Checking PostgreSQL Profile { key } and database { database } " )
84- conn = connect_postgresql (host , port , user , password , database )
87+ system .print_info (f"Checking MySQL Profile { key } and database { database } " )
88+ conn = connect_mysql (host , port , user , password , database )
8589 if conn :
86- results += check_data_patterns (conn , patterns , key , database )
90+ results += check_data_patterns (conn , patterns , key , database , limit_start = limit_start , limit_end = limit_end , tables = tables )
8791 conn .close ()
8892 else :
89- system .print_error (f"Incomplete PostgreSQL configuration for key: { key } " )
93+ system .print_error (f"Incomplete MySQL configuration for key: { key } " )
9094 else :
91- system .print_error ("No PostgreSQL connection details found in connection.yml" )
95+ system .print_error ("No MySQL connection details found in connection.yml" )
9296 else :
9397 system .print_error ("No 'sources' section found in connection.yml" )
94- return results
98+ return results
99+
100+ # Example usage
101+ if __name__ == "__main__" :
102+ execute (None )
0 commit comments