Skip to content

Commit

Permalink
write certificate material to one JSON file and also read from that
Browse files Browse the repository at this point in the history
  • Loading branch information
Mastercuber committed Nov 6, 2023
1 parent 542cdaf commit 73dc5be
Showing 1 changed file with 25 additions and 14 deletions.
39 changes: 25 additions & 14 deletions src/_cert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,17 @@ export async function resolveCertificate(

if (typeof options === "object" && options.reuse) {
// Reuse previously autogenerated certificates if exists
const certExists = await pathExists(certificateDirectory + "cert.pem");
const keyExists = await pathExists(certificateDirectory + "cert-key.pem");
const certsExists = await pathExists(certificateDirectory + "certs.json");

if (certExists && keyExists) {
const cert = await fs.readFile(certificateDirectory + "cert.pem");
const key = await fs.readFile(certificateDirectory + "cert-key.pem");
if (certsExists) {
const certs = JSON.parse(
(await fs.readFile(certificateDirectory + "certs.json")).toString(
"utf8",
),
);
return {
cert: cert.toString("utf8"),
key: key.toString("utf8"),
cert: certs.cert + certs.caCert,
key: certs.key,
};
}
}

Check warning on line 75 in src/_cert.ts

View check run for this annotation

Codecov / codecov/patch

src/_cert.ts#L61-L75

Added lines #L61 - L75 were not covered by tests
Expand Down Expand Up @@ -98,7 +100,8 @@ export async function resolveCertificate(
cert: forge.pki.certificateToPem(_cert!),
};
} else {
const { cert } = await generateCertificates(options);
const { cert, ca } = await generateCertificates(options);
cert.cert += ca.cert;
https = cert;
}

Expand All @@ -124,8 +127,6 @@ async function generateCertificates(
const ca = await generateCACert(caOptions);

await mkdir(resolve(certificateDirectory), { recursive: true });
await writeFile(resolve(certificateDirectory + "ca.pem"), ca.cert);
await writeFile(resolve(certificateDirectory + "ca-key.pem"), ca.key);

const domains = Array.isArray(options.domains)
? options.domains
Expand All @@ -140,12 +141,22 @@ async function generateCertificates(
});

await writeFile(
resolve(certificateDirectory + "cert.pem"),
cert.cert + ca.cert,
resolve(certificateDirectory + "certs.json"),
JSON.stringify({
cert: cert.cert,
key: cert.key,
caCert: ca.cert,
caKey: ca.key,
}),
);
await writeFile(resolve(certificateDirectory + "cert-key.pem"), cert.key);

return { ca, cert };
return {
ca,
cert: {
cert: cert.cert,
key: cert.key,
},
};
}

async function resolveCert(options: HTTPSOptions): Promise<Certificate> {
Expand Down

0 comments on commit 73dc5be

Please sign in to comment.