forked from mtusi2026/mtusi2026lab7
144 lines
3.7 KiB
TypeScript
144 lines
3.7 KiB
TypeScript
import { readFile } from "node:fs/promises";
|
|
import { resolve } from "node:path";
|
|
import {
|
|
local,
|
|
root,
|
|
run,
|
|
privateWrite,
|
|
compose,
|
|
requireThat,
|
|
} from "../scripts/runtime";
|
|
import { containerID, containerIP } from "../scripts/runtime-inspect";
|
|
import { tlsProbe, expectDenied } from "./transport";
|
|
import { images } from "../scripts/topology";
|
|
export async function postgresTransport() {
|
|
const ip = await containerIP("postgres");
|
|
const positive = await tlsProbe(ip, 5432, "postgres", "worker", "");
|
|
requireThat(
|
|
positive.authorized && positive.protocol?.startsWith("TLSv1."),
|
|
"PG_TLS_POSITIVE_FAILED",
|
|
);
|
|
const wrongName = await expectDenied(
|
|
() => tlsProbe(ip, 5432, "invalid.example", "worker", ""),
|
|
"trust",
|
|
);
|
|
const badCA = await expectDenied(
|
|
() => tlsProbe(ip, 5432, "postgres", "worker", "", "foreign-lb"),
|
|
"trust",
|
|
);
|
|
const envFile = resolve(local, "operator", "pg-worker.env");
|
|
await privateWrite(
|
|
envFile,
|
|
`PGHOST=127.0.0.1\nPGPORT=15432\nPGDATABASE=messenger\nPGUSER=worker\nPGSSLMODE=disable\nPGGSSENCMODE=disable\n`,
|
|
);
|
|
const gate = await containerID("worker-gateway");
|
|
const result = await run([
|
|
"docker",
|
|
"run",
|
|
"--pull=never",
|
|
"--rm",
|
|
"--user",
|
|
`${process.getuid?.()}:${process.getgid?.()}`,
|
|
"--network",
|
|
"container:" + gate,
|
|
"--env-file",
|
|
envFile,
|
|
"--entrypoint",
|
|
"psql",
|
|
images.postgres,
|
|
"-X",
|
|
"-A",
|
|
"-t",
|
|
"-v",
|
|
"ON_ERROR_STOP=1",
|
|
"-c",
|
|
"SELECT json_build_object('role',current_user,'tls',ssl,'protocol',version,'identity',client_dn)::text FROM pg_stat_ssl WHERE pid=pg_backend_pid()",
|
|
]);
|
|
const query = JSON.parse(result.out);
|
|
requireThat(
|
|
query.role === "worker" &&
|
|
query.tls === true &&
|
|
/^TLSv1\.[23]$/.test(query.protocol) &&
|
|
["CN=worker", "/CN=worker"].includes(query.identity),
|
|
"PG_REAL_QUERY_NOT_MTLS",
|
|
);
|
|
const impersonation = await run(
|
|
[
|
|
"docker",
|
|
"run",
|
|
"--pull=never",
|
|
"--rm",
|
|
"--user",
|
|
`${process.getuid?.()}:${process.getgid?.()}`,
|
|
"--network",
|
|
"container:" + gate,
|
|
"--env-file",
|
|
envFile,
|
|
"--env",
|
|
"PGUSER=api",
|
|
"--entrypoint",
|
|
"psql",
|
|
images.postgres,
|
|
"-X",
|
|
"-A",
|
|
"-t",
|
|
"-c",
|
|
"SELECT 1",
|
|
],
|
|
{ allowFailure: true },
|
|
);
|
|
requireThat(
|
|
impersonation.code !== 0 &&
|
|
/certificate authentication failed/.test(impersonation.err),
|
|
"PG_CERT_ROLE_IMPERSONATION_NOT_DENIED",
|
|
);
|
|
// Assert actual database-side client cert enforcement, not merely a successful TLS handshake.
|
|
const rejected = await run(
|
|
[
|
|
"docker",
|
|
"run",
|
|
"--pull=never",
|
|
"--rm",
|
|
"--user",
|
|
`${process.getuid?.()}:${process.getgid?.()}`,
|
|
"--network",
|
|
"container:" + gate,
|
|
"--env-file",
|
|
envFile,
|
|
"--env",
|
|
"PGHOST=" + ip,
|
|
"--env",
|
|
"PGPORT=5432",
|
|
"--env",
|
|
"PGSSLMODE=disable",
|
|
"--entrypoint",
|
|
"psql",
|
|
images.postgres,
|
|
"-X",
|
|
"-A",
|
|
"-t",
|
|
"-c",
|
|
"SELECT 1",
|
|
],
|
|
{ allowFailure: true },
|
|
);
|
|
requireThat(
|
|
rejected.code !== 0 && /pg_hba.conf rejects connection/.test(rejected.err),
|
|
"PG_PLAINTEXT_NOT_DENIED",
|
|
);
|
|
const evidence = {
|
|
service: "PostgreSQL18.6",
|
|
direct_tls: "HAProxy ALPN postgresql, verifyhost postgres",
|
|
actual_query: query,
|
|
wrong_hostname: wrongName,
|
|
wrong_ca: badCA,
|
|
plaintext: "pg_hba.conf rejects connection",
|
|
different_database_role: "certificate authentication failed",
|
|
};
|
|
await Bun.write(
|
|
resolve(root, "evidence/security/postgres-transport.json"),
|
|
JSON.stringify(evidence, null, 2) + "\n",
|
|
);
|
|
console.log("PASS_POSTGRES_REAL_QUERY_MTLS_AND_NEGATIVE_TRUST");
|
|
}
|