Updates
This commit is contained in:
@@ -0,0 +1,24 @@
|
||||
import { $ } from "bun";
|
||||
import fs from "node:fs";
|
||||
import execute from "../(utils)/execute";
|
||||
|
||||
export default async function setupSSH() {
|
||||
console.log("Generating SSH keys ...");
|
||||
|
||||
const KEY_NAME = "dsql";
|
||||
const OUTPUT_DIR = "/ssh";
|
||||
const PASSPHRASE = "";
|
||||
|
||||
execute(`mkdir -p "${OUTPUT_DIR}"`);
|
||||
|
||||
const KEY_PATH = `${OUTPUT_DIR}/${KEY_NAME}`;
|
||||
|
||||
if (!fs.existsSync(KEY_PATH)) {
|
||||
console.log("Generating SSH keypair...");
|
||||
execute(
|
||||
`ssh-keygen -t rsa -b 4096 -f "${KEY_PATH}" -N "${PASSPHRASE}" -q`
|
||||
);
|
||||
}
|
||||
|
||||
console.log("SSH keys Setup Complete!");
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
import { $ } from "bun";
|
||||
import fs from "node:fs";
|
||||
import execute from "../(utils)/execute";
|
||||
|
||||
export default async function setupSSL() {
|
||||
console.log("Generating SSL Files ...");
|
||||
|
||||
const CA_CERT_FILE = "/ssl/ca-cert.pem";
|
||||
const CA_KEY_FILE = "/ssl/ca-key.pem";
|
||||
const SERVER_CERT_FILE = "/ssl/server-cert.pem";
|
||||
const SERVER_KEY_FILE = "/ssl/server-key.pem";
|
||||
|
||||
if (!fs.existsSync("/app/ssl")) {
|
||||
fs.mkdirSync("/app/ssl", { recursive: true });
|
||||
}
|
||||
|
||||
if (!fs.existsSync("/app/public/documents/ssl/")) {
|
||||
fs.mkdirSync("/app/public/documents/ssl/", { recursive: true });
|
||||
}
|
||||
|
||||
$.cwd("/ssl");
|
||||
|
||||
if (!fs.existsSync(CA_CERT_FILE) || !fs.existsSync(CA_KEY_FILE)) {
|
||||
console.log("Generating SSL Files ...");
|
||||
|
||||
execute(`rm -Rf /ssl/*`);
|
||||
execute(`openssl genrsa 2048 >ca-key.pem`, { cwd: "/ssl" });
|
||||
execute(
|
||||
`openssl req -new -x509 -nodes -days 365000 -key ca-key.pem -out ca-cert.pem -subj "/C=/ST=/L=/O=/CN=MariaDB admin"`,
|
||||
{ cwd: "/ssl" }
|
||||
);
|
||||
execute(
|
||||
`openssl req -newkey rsa:2048 -days 365000 -nodes -keyout server-key.pem -out server-req.pem -subj "/C=/ST=/L=/O=/CN=MariaDB server"`,
|
||||
{ cwd: "/ssl" }
|
||||
);
|
||||
execute(`openssl rsa -in server-key.pem -out server-key.pem`, {
|
||||
cwd: "/ssl",
|
||||
});
|
||||
execute(
|
||||
`openssl x509 -req -in server-req.pem -days 365000 -CA ca-cert.pem -CAkey ca-key.pem -set_serial 01 -out server-cert.pem`,
|
||||
{ cwd: "/ssl" }
|
||||
);
|
||||
execute(
|
||||
`openssl req -newkey rsa:2048 -days 365000 -nodes -keyout client-key.pem -out client-req.pem -subj "/C=/ST=/L=/O=/CN=MariaDB user"`,
|
||||
{ cwd: "/ssl" }
|
||||
);
|
||||
execute(`openssl rsa -in client-key.pem -out client-key.pem`, {
|
||||
cwd: "/ssl",
|
||||
});
|
||||
execute(
|
||||
`openssl x509 -req -in client-req.pem -days 365000 -CA ca-cert.pem -CAkey ca-key.pem -set_serial 01 -out client-cert.pem`,
|
||||
{ cwd: "/ssl" }
|
||||
);
|
||||
}
|
||||
|
||||
execute(`chmod 755 /ssl`);
|
||||
execute(`chmod 644 /ssl/\*.pem`);
|
||||
|
||||
execute(`rm -Rf /app/ssl/\*`);
|
||||
execute(`rm -Rf /app/public/documents/ssl/\*`);
|
||||
|
||||
execute(`cp /ssl/ca-cert.pem /app/ssl/`);
|
||||
// execute(`cp /ssl/client-key.pem /app/ssl/`);
|
||||
// execute(`cp /ssl/client-cert.pem /app/ssl/`);
|
||||
execute(`cp /ssl/ca-cert.pem /app/public/documents/ssl/`);
|
||||
// execute(`cp /ssl/client-key.pem /app/public/documents/ssl/`);
|
||||
// execute(`cp /ssl/client-cert.pem /app/public/documents/ssl/`);
|
||||
|
||||
const LOCAL_CONFIG_DIR = "/app/jsonData/dbSchemas/users";
|
||||
|
||||
if (!fs.existsSync(LOCAL_CONFIG_DIR)) {
|
||||
console.log("Creating Local Config Directory ...");
|
||||
fs.mkdirSync(LOCAL_CONFIG_DIR, { recursive: true });
|
||||
}
|
||||
|
||||
console.log("SSL Files Setup Complete!");
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
import { ExecOptions, execSync, ExecSyncOptions } from "child_process";
|
||||
|
||||
export default function execute(
|
||||
cmd: string,
|
||||
options?: ExecSyncOptions
|
||||
): string | undefined {
|
||||
try {
|
||||
const res = execSync(cmd, {
|
||||
encoding: "utf-8",
|
||||
...options,
|
||||
});
|
||||
|
||||
if (typeof res == "string") {
|
||||
return res.trim();
|
||||
} else {
|
||||
return undefined;
|
||||
}
|
||||
} catch (error) {
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
FROM oven/bun:debian
|
||||
|
||||
SHELL ["/bin/bash", "-c"]
|
||||
|
||||
RUN apt-get update
|
||||
RUN apt-get install -y openssl openssh-client
|
||||
|
||||
WORKDIR /app/docker/setup
|
||||
|
||||
ENTRYPOINT ["bun", "index.ts"]
|
||||
@@ -0,0 +1,7 @@
|
||||
import setupSSH from "./(functions)/setup-ssh";
|
||||
import setupSSL from "./(functions)/setup-ssl";
|
||||
|
||||
await setupSSL();
|
||||
await setupSSH();
|
||||
|
||||
process.exit(0);
|
||||
@@ -0,0 +1,77 @@
|
||||
#!/bin/bash
|
||||
|
||||
CA_CERT_FILE="/ssl/ca-cert.pem"
|
||||
CA_KEY_FILE="/ssl/ca-key.pem"
|
||||
SERVER_CERT_FILE="/ssl/server-cert.pem"
|
||||
SERVER_KEY_FILE="/ssl/server-key.pem"
|
||||
|
||||
if [ ! -d "/app/ssl" ]; then
|
||||
mkdir -p "/app/ssl"
|
||||
fi
|
||||
|
||||
if [ ! -d "/app/public/documents/ssl/" ]; then
|
||||
mkdir -p "/app/public/documents/ssl/"
|
||||
fi
|
||||
|
||||
if [ -f "$CA_CERT_FILE" ] && [ -f "$CA_KEY_FILE" ] && [ -f "$SERVER_CERT_FILE" ] && [ -f "$SERVER_KEY_FILE" ]; then
|
||||
echo "SSL Files Present. Moving Forward >>>"
|
||||
else
|
||||
echo "Generating SSL Files ..."
|
||||
rm -Rf /ssl/*
|
||||
cd /ssl
|
||||
openssl genrsa 2048 >ca-key.pem
|
||||
openssl req -new -x509 -nodes -days 365000 -key ca-key.pem -out ca-cert.pem -subj "/C=/ST=/L=/O=/CN=MariaDB admin"
|
||||
openssl req -newkey rsa:2048 -days 365000 -nodes -keyout server-key.pem -out server-req.pem -subj "/C=/ST=/L=/O=/CN=MariaDB server"
|
||||
openssl rsa -in server-key.pem -out server-key.pem
|
||||
openssl x509 -req -in server-req.pem -days 365000 -CA ca-cert.pem -CAkey ca-key.pem -set_serial 01 -out server-cert.pem
|
||||
openssl req -newkey rsa:2048 -days 365000 -nodes -keyout client-key.pem -out client-req.pem -subj "/C=/ST=/L=/O=/CN=MariaDB user"
|
||||
openssl rsa -in client-key.pem -out client-key.pem
|
||||
openssl x509 -req -in client-req.pem -days 365000 -CA ca-cert.pem -CAkey ca-key.pem -set_serial 01 -out client-cert.pem
|
||||
fi
|
||||
|
||||
chmod 755 /ssl
|
||||
chmod 644 /ssl/*.pem
|
||||
|
||||
rm -f /app/ssl/ca-cert.pem
|
||||
rm -Rf /app/ssl/*
|
||||
rm -Rf /app/public/documents/ssl/*
|
||||
|
||||
cp /ssl/ca-cert.pem /app/ssl/
|
||||
cp /ssl/ca-cert.pem /app/public/documents/ssl/
|
||||
|
||||
LOCAL_CONFIG_DIR="/app/jsonData/dbSchemas/users"
|
||||
if [ ! -d "$LOCAL_CONFIG_DIR" ]; then
|
||||
echo "Creating Local Config Directory ..."
|
||||
mkdir -p "$LOCAL_CONFIG_DIR"
|
||||
fi
|
||||
|
||||
##------------------
|
||||
# Create SSH keys
|
||||
##------------------
|
||||
echo "Generating SSH keys ..."
|
||||
|
||||
KEY_NAME="dsql"
|
||||
OUTPUT_DIR="/ssh"
|
||||
PASSPHRASE=""
|
||||
|
||||
mkdir -p "$OUTPUT_DIR"
|
||||
|
||||
KEY_PATH="$OUTPUT_DIR/$KEY_NAME"
|
||||
|
||||
if [ ! -f "$KEY_PATH" ]; then
|
||||
echo "Generating SSH keypair..."
|
||||
ssh-keygen -t rsa -b 4096 -f "$KEY_PATH" -N "$PASSPHRASE" -q
|
||||
|
||||
if [ $? -eq 0 ]; then
|
||||
echo "SSH keypair generated successfully!"
|
||||
echo "Private key: $KEY_PATH"
|
||||
echo "Public key: $KEY_PATH.pub"
|
||||
else
|
||||
echo "Error: Failed to generate SSH keypair."
|
||||
exit 1
|
||||
fi
|
||||
else
|
||||
echo "SSH keypair already exists. Skipping key generation."
|
||||
fi
|
||||
|
||||
exit 0
|
||||
Reference in New Issue
Block a user