forked from KHenryAegis/VulnBot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb_utils.py
More file actions
120 lines (99 loc) · 3.61 KB
/
db_utils.py
File metadata and controls
120 lines (99 loc) · 3.61 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
#!/usr/bin/env python3
"""
Database utility script for VulnBot
Provides commands to initialize, connect, and manage the MySQL database
"""
import click
import subprocess
import sys
from pathlib import Path
try:
from config.config import Configs
from utils.session import create_tables, build_db_url
from utils.log_common import build_logger
except ImportError as e:
print(f"Import error: {e}")
print("Make sure you're running this from the VulnBot root directory")
sys.exit(1)
logger = build_logger()
@click.group(help="Database utilities for VulnBot")
def db():
pass
@db.command("init")
def init_db():
"""Initialize database tables"""
try:
create_tables()
logger.success("Database tables created successfully")
except Exception as e:
logger.error(f"Failed to create database tables: {e}")
sys.exit(1)
@db.command("connect")
def connect_db():
"""Connect to MySQL database using configured settings"""
mysql_config = Configs.db_config.mysql
if mysql_config.get('socket') and mysql_config['socket'] != "":
# Connect using Unix socket
cmd = [
"mysql",
f"--socket={mysql_config['socket']}",
f"--user={mysql_config['user']}",
f"--database={mysql_config['database']}"
]
if mysql_config.get('password') and mysql_config['password'] != "":
cmd.append(f"--password={mysql_config['password']}")
else:
# Connect using TCP
cmd = [
"mysql",
f"--host={mysql_config['host']}",
f"--port={mysql_config['port']}",
f"--user={mysql_config['user']}",
f"--database={mysql_config['database']}"
]
if mysql_config.get('password') and mysql_config['password'] != "":
cmd.append(f"--password={mysql_config['password']}")
logger.info(f"Connecting to database: {mysql_config['database']}")
try:
subprocess.run(cmd, check=True)
except subprocess.CalledProcessError as e:
logger.error(f"Failed to connect to database: {e}")
sys.exit(1)
except FileNotFoundError:
logger.error("MySQL client not found. Please install MySQL client tools.")
sys.exit(1)
@db.command("info")
def db_info():
"""Show database connection information"""
mysql_config = Configs.db_config.mysql
db_url = build_db_url()
logger.info("Database Configuration:")
logger.info(f" Database: {mysql_config['database']}")
logger.info(f" User: {mysql_config['user']}")
if mysql_config.get('socket') and mysql_config['socket'] != "":
logger.info(f" Connection Type: Unix Socket")
logger.info(f" Socket Path: {mysql_config['socket']}")
else:
logger.info(f" Connection Type: TCP")
logger.info(f" Host: {mysql_config['host']}")
logger.info(f" Port: {mysql_config['port']}")
if mysql_config.get('charset'):
logger.info(f" Charset: {mysql_config['charset']}")
logger.info(f" Connection URL: {db_url}")
@db.command("test")
def test_connection():
"""Test database connection"""
try:
from utils.session import engine
with engine.connect() as conn:
result = conn.execute("SELECT 1 as test")
if result.fetchone()[0] == 1:
logger.success("Database connection test successful")
else:
logger.error("Database connection test failed")
sys.exit(1)
except Exception as e:
logger.error(f"Database connection test failed: {e}")
sys.exit(1)
if __name__ == "__main__":
db()