-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.py
More file actions
85 lines (73 loc) · 2.5 KB
/
Copy pathdatabase.py
File metadata and controls
85 lines (73 loc) · 2.5 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
import psycopg2
import os
from psycopg2.extensions import ISOLATION_LEVEL_AUTOCOMMIT
# Configuration
DB_CONFIG = {
"user": os.environ.get("POSTGRES_USER"),
"password": os.environ.get("POSTGRES_PASSWORD"),
"host": os.environ.get("POSTGRES_HOST"),
"port": os.environ.get("POSTGRES_PORT")
}
TARGET_DB = os.environ.get("TARGET_DB")
def init_db():
"""
1. Checks if the database 'siem_db' exists; creates it if not.
2. Connects to 'siem_db' and creates the required tables.
"""
conn = psycopg2.connect(dbname="postgres", **DB_CONFIG)
conn.set_isolation_level(ISOLATION_LEVEL_AUTOCOMMIT)
cur = conn.cursor()
# Check if DB exists
cur.execute(f"SELECT 1 FROM pg_catalog.pg_database WHERE datname = '{TARGET_DB}'")
exists = cur.fetchone()
if not exists:
print(f"Creating database {TARGET_DB}...")
cur.execute(f"CREATE DATABASE {TARGET_DB}")
else:
print(f"Database {TARGET_DB} already exists.")
cur.close()
conn.close()
# --- Step B: Create the Tables ---
# Now connect to the specific SIEM database
conn = psycopg2.connect(dbname=TARGET_DB, **DB_CONFIG)
cur = conn.cursor()
# Define the Schema (Using IF NOT EXISTS for safety)
# Remove the raw_content column. No need to store the raw content in the database as report is stored in S3.
table_schema = """
-- 1. Reports Table
CREATE TABLE IF NOT EXISTS reports (
report_id SERIAL PRIMARY KEY,
filename VARCHAR(255),
summary TEXT,
severity VARCHAR(50),
victim_sector VARCHAR(100),
timeline_start VARCHAR(100),
timeline_end VARCHAR(100),
raw_content TEXT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
-- 2. Indicators (IoCs)
CREATE TABLE IF NOT EXISTS iocs (
ioc_id SERIAL PRIMARY KEY,
report_id INT REFERENCES reports(report_id) ON DELETE CASCADE,
value VARCHAR(255),
type VARCHAR(50)
);
-- 3. TTPs (MITRE)
CREATE TABLE IF NOT EXISTS ttps (
ttp_id SERIAL PRIMARY KEY,
report_id INT REFERENCES reports(report_id) ON DELETE CASCADE,
technique_id VARCHAR(50),
technique_name VARCHAR(100)
);
"""
try:
cur.execute(table_schema)
conn.commit()
print("Schema initialized successfully (Tables created/verified).")
except Exception as e:
print(f"Error creating schema: {e}")
conn.rollback()
finally:
cur.close()
conn.close()