1- import mysql . connector
1+ import psycopg2
22from hawk_scanner .internals import system
33import re
44from rich .console import Console
55from rich .table import Table
66
77console = Console ()
88
9- def connect_mysql (host , port , user , password , database ):
9+ def connect_postgresql (host , port , user , password , database ):
1010 try :
11- conn = mysql . connector .connect (
11+ conn = psycopg2 .connect (
1212 host = host ,
1313 port = port ,
1414 user = user ,
1515 password = password ,
1616 database = database
1717 )
18- if conn . is_connected () :
19- system .print_info (f"Connected to MySQL database at { host } " )
18+ if conn :
19+ system .print_info (f"Connected to PostgreSQL database at { host } " )
2020 return conn
21- else :
22- system .print_error (f"Failed to connect to MySQL database at { host } " )
2321 except Exception as e :
24- system .print_error (f"Failed to connect to MySQL database at { host } with error: { e } " )
22+ system .print_error (f"Failed to connect to PostgreSQL database at { host } with error: { e } " )
2523
2624def check_data_patterns (conn , patterns , profile_name , database_name ):
2725 cursor = conn .cursor ()
28- cursor .execute ("SHOW TABLES " )
26+ cursor .execute ("SELECT table_name FROM information_schema.tables WHERE table_schema = 'public' " )
2927 tables = [table [0 ] for table in cursor .fetchall ()]
3028
3129 table_count = 1
@@ -44,15 +42,15 @@ def check_data_patterns(conn, patterns, profile_name, database_name):
4442 if matches :
4543 for match in matches :
4644 results .append ({
47- 'host' : conn ._host ,
45+ 'host' : conn .dsn ,
4846 'database' : database_name ,
4947 'table' : table ,
5048 'column' : column ,
5149 'pattern_name' : match ['pattern_name' ],
5250 'matches' : match ['matches' ],
5351 'sample_text' : match ['sample_text' ],
5452 'profile' : profile_name ,
55- 'data_source' : 'mysql '
53+ 'data_source' : 'postgresql '
5654 })
5755
5856 data_count += 1
@@ -64,33 +62,33 @@ def check_data_patterns(conn, patterns, profile_name, database_name):
6462
6563def execute (args ):
6664 results = []
67- system .print_info (f"Running Checks for MySQL Sources" )
65+ system .print_info (f"Running Checks for PostgreSQL Sources" )
6866 connections = system .get_connection ()
6967
7068 if 'sources' in connections :
7169 sources_config = connections ['sources' ]
72- mysql_config = sources_config .get ('mysql ' )
70+ postgresql_config = sources_config .get ('postgresql ' )
7371
74- if mysql_config :
72+ if postgresql_config :
7573 patterns = system .get_fingerprint_file ()
7674
77- for key , config in mysql_config .items ():
75+ for key , config in postgresql_config .items ():
7876 host = config .get ('host' )
7977 user = config .get ('user' )
80- port = config .get ('port' , 3306 ) # default port
78+ port = config .get ('port' , 5432 ) # default port for PostgreSQL
8179 password = config .get ('password' )
8280 database = config .get ('database' )
8381
8482 if host and user and password and database :
85- system .print_info (f"Checking MySQL Profile { key } and database { database } " )
86- conn = connect_mysql (host , port , user , password , database )
83+ system .print_info (f"Checking PostgreSQL Profile { key } and database { database } " )
84+ conn = connect_postgresql (host , port , user , password , database )
8785 if conn :
88- results = check_data_patterns (conn , patterns , key , database )
86+ results + = check_data_patterns (conn , patterns , key , database )
8987 conn .close ()
9088 else :
91- system .print_error (f"Incomplete MySQL configuration for key: { key } " )
89+ system .print_error (f"Incomplete PostgreSQL configuration for key: { key } " )
9290 else :
93- system .print_error ("No MySQL connection details found in connection.yml" )
91+ system .print_error ("No PostgreSQL connection details found in connection.yml" )
9492 else :
9593 system .print_error ("No 'sources' section found in connection.yml" )
96- return results
94+ return results
0 commit comments