39 lines
1.1 KiB
TypeScript
39 lines
1.1 KiB
TypeScript
import { execFileSync } from "node:child_process";
|
|
import { mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
|
|
import { tmpdir } from "node:os";
|
|
import path from "node:path";
|
|
export function certificateFixture(domain = "*.p.xn--876a.net") {
|
|
const dir = mkdtempSync(path.join(tmpdir(), "hy2-cert-fixture-"));
|
|
try {
|
|
writeFileSync(
|
|
path.join(dir, "openssl.cnf"),
|
|
`[req]\nprompt=no\ndistinguished_name=dn\nx509_extensions=ext\n[dn]\nCN=${domain}\n[ext]\nsubjectAltName=DNS:${domain}\n`,
|
|
);
|
|
execFileSync(
|
|
"openssl",
|
|
[
|
|
"req",
|
|
"-x509",
|
|
"-newkey",
|
|
"rsa:2048",
|
|
"-nodes",
|
|
"-keyout",
|
|
path.join(dir, "key"),
|
|
"-out",
|
|
path.join(dir, "crt"),
|
|
"-days",
|
|
"60",
|
|
"-config",
|
|
path.join(dir, "openssl.cnf"),
|
|
],
|
|
{ stdio: "ignore" },
|
|
);
|
|
return {
|
|
certificate: readFileSync(path.join(dir, "crt"), "utf8"),
|
|
privateKey: readFileSync(path.join(dir, "key"), "utf8"),
|
|
};
|
|
} finally {
|
|
rmSync(dir, { recursive: true, force: true });
|
|
}
|
|
}
|