refactor: update setup_database.sh for improved PostgreSQL configuration and user creation
All checks were successful
Build and Deploy MITM Webserver / build (push) Successful in 8s
All checks were successful
Build and Deploy MITM Webserver / build (push) Successful in 8s
This commit is contained in:
@@ -1,80 +1,72 @@
|
||||
#!/usr/bin/env bash
|
||||
#!/bin/bash
|
||||
set -e
|
||||
|
||||
DB_NAME="mitm_db"
|
||||
DB_USER="mitm_user"
|
||||
DB_PASS="mitm_pass"
|
||||
DB_PASS="mitm_password"
|
||||
LAN_SUBNET="192.168.178.0/24"
|
||||
|
||||
echo "[+] Installing PostgreSQL..."
|
||||
sudo apt-get update -y
|
||||
sudo apt-get install -y postgresql postgresql-contrib
|
||||
echo "[1] Installing PostgreSQL…"
|
||||
sudo apt update -y
|
||||
sudo apt install -y postgresql postgresql-contrib
|
||||
|
||||
echo "[+] Ensuring PostgreSQL is running..."
|
||||
sudo systemctl enable postgresql
|
||||
sudo systemctl start postgresql
|
||||
echo "[2] Configuring PostgreSQL to listen on all addresses…"
|
||||
PG_CONF="/etc/postgresql/$(ls /etc/postgresql)/main/postgresql.conf"
|
||||
sudo sed -i "s/^#listen_addresses =.*/listen_addresses = '*'/" "$PG_CONF"
|
||||
|
||||
echo "[+] Creating user and database (if missing)..."
|
||||
echo "[3] Updating pg_hba.conf for LAN + localhost access…"
|
||||
PG_HBA="/etc/postgresql/$(ls /etc/postgresql)/main/pg_hba.conf"
|
||||
|
||||
# Ensure localhost entry exists
|
||||
if ! grep -q "host *all *all *127.0.0.1/32" "$PG_HBA"; then
|
||||
echo "host all all 127.0.0.1/32 md5" | sudo tee -a "$PG_HBA"
|
||||
fi
|
||||
|
||||
# Ensure LAN entry exists
|
||||
if ! grep -q "$LAN_SUBNET" "$PG_HBA"; then
|
||||
echo "host all all $LAN_SUBNET md5" | sudo tee -a "$PG_HBA"
|
||||
fi
|
||||
|
||||
echo "[4] Restarting PostgreSQL…"
|
||||
sudo systemctl restart postgresql
|
||||
|
||||
echo "[5] Creating database and user (if not existing)…"
|
||||
sudo -u postgres psql <<EOF
|
||||
DO
|
||||
\$do\$
|
||||
DO \$\$
|
||||
BEGIN
|
||||
IF NOT EXISTS (SELECT FROM pg_catalog.pg_roles WHERE rolname = '${DB_USER}') THEN
|
||||
CREATE ROLE ${DB_USER} LOGIN PASSWORD '${DB_PASS}';
|
||||
END IF;
|
||||
IF NOT EXISTS (SELECT FROM pg_roles WHERE rolname = '$DB_USER') THEN
|
||||
CREATE USER $DB_USER WITH PASSWORD '$DB_PASS';
|
||||
END IF;
|
||||
|
||||
IF NOT EXISTS (SELECT FROM pg_database WHERE datname = '$DB_NAME') THEN
|
||||
CREATE DATABASE $DB_NAME OWNER $DB_USER;
|
||||
END IF;
|
||||
END
|
||||
\$do\$;
|
||||
|
||||
CREATE DATABASE ${DB_NAME} OWNER ${DB_USER}
|
||||
TEMPLATE template0
|
||||
ENCODING 'UTF8'
|
||||
LC_COLLATE='C'
|
||||
LC_CTYPE='C'
|
||||
CONNECTION LIMIT -1;
|
||||
|
||||
-- ignore errors if DB already exists
|
||||
ALTER DATABASE ${DB_NAME} OWNER TO ${DB_USER};
|
||||
\$\$;
|
||||
EOF
|
||||
|
||||
echo "[+] Creating tables (idempotent)..."
|
||||
sudo -u postgres psql -d ${DB_NAME} <<EOF
|
||||
|
||||
CREATE TABLE IF NOT EXISTS packets (
|
||||
echo "[6] Creating tables (idempotent)…"
|
||||
sudo -u postgres psql -d $DB_NAME <<EOF
|
||||
CREATE TABLE IF NOT EXISTS packet_log (
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
timestamp TIMESTAMPTZ DEFAULT NOW(),
|
||||
|
||||
direction VARCHAR(16) NOT NULL,
|
||||
iface VARCHAR(32) NOT NULL,
|
||||
|
||||
src_mac VARCHAR(17),
|
||||
dst_mac VARCHAR(17),
|
||||
|
||||
eth_type VARCHAR(8),
|
||||
|
||||
direction VARCHAR(16), -- ingress / egress
|
||||
interface VARCHAR(32),
|
||||
src_mac VARCHAR(32),
|
||||
dst_mac VARCHAR(32),
|
||||
eth_type VARCHAR(16),
|
||||
src_ip VARCHAR(64),
|
||||
dst_ip VARCHAR(64),
|
||||
ip_proto VARCHAR(16),
|
||||
|
||||
src_port INT,
|
||||
dst_port INT,
|
||||
|
||||
packet_len INT,
|
||||
packet_hex TEXT
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS nft_events (
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
timestamp TIMESTAMPTZ DEFAULT NOW(),
|
||||
packet_id BIGINT REFERENCES packets(id) ON DELETE CASCADE,
|
||||
|
||||
hook VARCHAR(64),
|
||||
ip_protocol VARCHAR(16),
|
||||
src_port INTEGER,
|
||||
dst_port INTEGER,
|
||||
packet_len INTEGER,
|
||||
nft_hook VARCHAR(32),
|
||||
nft_table VARCHAR(64),
|
||||
nft_chain VARCHAR(64),
|
||||
nft_rule VARCHAR(256),
|
||||
verdict VARCHAR(32)
|
||||
nft_verdict VARCHAR(32),
|
||||
raw_packet BYTEA
|
||||
);
|
||||
EOF
|
||||
|
||||
echo "[+] DONE!"
|
||||
echo "Database: ${DB_NAME}"
|
||||
echo "User: ${DB_USER}"
|
||||
echo "Password: ${DB_PASS}"
|
||||
echo "Done. PostgreSQL is ready for LAN + localhost connections."
|
||||
|
||||
Reference in New Issue
Block a user