2024-06-14 11:09:53 +00:00
|
|
|
import LIMIT_MAX from "Common/Types/Database/LimitMax";
|
|
|
|
import { EVERY_FIFTEEN_MINUTE, EVERY_MINUTE } from "Common/Utils/CronTime";
|
2024-08-07 21:50:32 +00:00
|
|
|
import { IsDevelopment } from "Common/Server/EnvironmentConfig";
|
|
|
|
import AcmeCertificateService from "Common/Server/Services/AcmeCertificateService";
|
|
|
|
import BasicCron from "Common/Server/Utils/BasicCron";
|
|
|
|
import LocalFile from "Common/Server/Utils/LocalFile";
|
2024-04-30 12:22:06 +00:00
|
|
|
// @ts-ignore
|
2024-08-07 21:50:32 +00:00
|
|
|
import logger from "Common/Server/Utils/Logger";
|
2024-08-05 19:00:31 +00:00
|
|
|
import AcmeCertificate from "Common/Models/DatabaseModels/AcmeCertificate";
|
2024-04-30 12:22:06 +00:00
|
|
|
|
|
|
|
export default class Jobs {
|
2024-06-14 11:09:53 +00:00
|
|
|
public static init(): void {
|
|
|
|
BasicCron({
|
|
|
|
jobName: "StatusPageCerts:WriteAcmeCertsToDisk",
|
|
|
|
options: {
|
|
|
|
schedule: IsDevelopment ? EVERY_MINUTE : EVERY_FIFTEEN_MINUTE,
|
|
|
|
runOnStartup: true,
|
|
|
|
},
|
|
|
|
runFunction: async () => {
|
|
|
|
// Fetch all domains where certs are added to greenlock.
|
2024-04-30 12:22:06 +00:00
|
|
|
|
2024-06-14 11:09:53 +00:00
|
|
|
const certs: Array<AcmeCertificate> =
|
|
|
|
await AcmeCertificateService.findBy({
|
|
|
|
query: {},
|
|
|
|
select: {
|
|
|
|
domain: true,
|
|
|
|
certificate: true,
|
|
|
|
certificateKey: true,
|
|
|
|
},
|
|
|
|
limit: LIMIT_MAX,
|
|
|
|
skip: 0,
|
|
|
|
props: {
|
|
|
|
isRoot: true,
|
|
|
|
},
|
|
|
|
});
|
2024-04-30 12:22:06 +00:00
|
|
|
|
2024-06-14 11:09:53 +00:00
|
|
|
for (const cert of certs) {
|
|
|
|
try {
|
|
|
|
await LocalFile.makeDirectory("/etc/nginx/certs/StatusPageCerts");
|
|
|
|
} catch (err) {
|
|
|
|
// directory already exists, ignore.
|
|
|
|
logger.error("Create directory err");
|
|
|
|
logger.error(err);
|
|
|
|
}
|
2024-04-30 12:22:06 +00:00
|
|
|
|
2024-06-14 11:09:53 +00:00
|
|
|
// Write to disk.
|
|
|
|
await LocalFile.write(
|
|
|
|
`/etc/nginx/certs/StatusPageCerts/${cert.domain}.crt`,
|
|
|
|
cert.certificate?.toString() || "",
|
|
|
|
);
|
2024-04-30 12:22:06 +00:00
|
|
|
|
2024-06-14 11:09:53 +00:00
|
|
|
await LocalFile.write(
|
|
|
|
`/etc/nginx/certs/StatusPageCerts/${cert.domain}.key`,
|
|
|
|
cert.certificateKey?.toString() || "",
|
|
|
|
);
|
2024-10-09 18:31:47 +00:00
|
|
|
|
|
|
|
logger.debug(`Wrote custom certs to disk for domain: ${cert.domain}`);
|
2024-06-14 11:09:53 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
2024-04-30 12:22:06 +00:00
|
|
|
}
|