Skip to content

Commit

Permalink
Improved log of certificate errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Netfloex committed May 12, 2022
1 parent 7a560c9 commit 3623c2a
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 11 deletions.
19 changes: 17 additions & 2 deletions src/lib/logMessages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { YAMLException } from "js-yaml";
import { ZodIssue } from "zod";

import { Log, Tag } from "@lib/logger";
import { InvalidSslReason } from "@utils/filterServersWithValidSslFiles";
import { formatAxiosError } from "@utils/formatAxiosError";
import { formatError } from "@utils/formatError";
import { gradientNumber } from "@utils/gradientNumber";
Expand Down Expand Up @@ -142,10 +143,24 @@ export const logMessages = defineLogList({
Tag.certbot,
chalk`{yellow The server {dim ${serverName}} has missing certificate files, requesting...}`
],
missingSSLFilesFinal: ({ serverName }: { serverName: string }) => [
certificateFailed: ({
serverName,
reason
}: {
serverName: string;
reason: InvalidSslReason;
}) => [
Log.error,
Tag.certbot,
chalk`{red The certificate files for {dim ${serverName}} could not be created, please see the error above.} ${
chalk`{red The certificate files for {dim ${serverName}} could not be ${
reason == "missing" ? "created" : "renewed"
}${
settings.disableCertbot
? " because certbot is disabled."
: !settings.certbotMail
? chalk` because {white {dim CERTBOT_MAIL}} is not set.`
: ". This might be because of an error stated above."
}} ${
settings.enableConfigMissingCerts
? chalk`This domain will still be {bold enabled}`
: "This domain is now disabled."
Expand Down
2 changes: 1 addition & 1 deletion src/tests/__snapshots__/log.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ exports[`Logger All log Messages 1`] = `
"[NCM] [ERROR] [CERTBOT] Certbot ran into an error:
",
"[NCM] [INFO] [CERTBOT] The server undefined has missing certificate files, requesting...",
"[NCM] [ERROR] [CERTBOT] The certificate files for undefined could not be created, please see the error above. This domain is now disabled.",
"[NCM] [ERROR] [CERTBOT] The certificate files for undefined could not be renewed because CERTBOT_MAIL is not set. This domain is now disabled.",
"[NCM] [INFO] [CERTBOT] The certificate for undefined is valid for -1 days",
"[NCM] [INFO] [CERTBOT] The certificate for undefined, expired 1 days ago",
"[NCM] [INFO] [CERTBOT] The certificate for undefined is using the Staging environment, renewing for production.",
Expand Down
36 changes: 28 additions & 8 deletions src/utils/filterServersWithValidSslFiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ import { sslFileFor, sslFilesFor } from "@utils/sslFilesFor";

import { SimpleServer } from "@models/ParsedConfig";

export type InvalidSslReason = "expired" | "staging" | "missing";

export interface InvalidSslServer {
server: SimpleServer;
reason: "expired" | "staging" | "missing";
reason: InvalidSslReason;
}

export interface FilteredServers {
Expand Down Expand Up @@ -46,8 +48,9 @@ export const filterServersWithValidSslFiles = async (
if (!last)
logger.missingSSLFiles({ serverName: server.server_name });
else {
logger.missingSSLFilesFinal({
serverName: server.server_name
logger.certificateFailed({
serverName: server.server_name,
reason: "missing"
});

/*
Expand Down Expand Up @@ -75,14 +78,31 @@ export const filterServersWithValidSslFiles = async (

if (days < 30) {
// Certificate expires in less than 30 days
logger.certificateExpiry({
serverName: server.server_name,
days
});
if (last)
logger.certificateFailed({
serverName: server.server_name,
reason: "expired"
});
else
logger.certificateExpiry({
serverName: server.server_name,
days
});

invalidSslServers.push({ server, reason: "expired" });
} else if (certificate.staging && settings.staging == false) {
console.log("Is Staging");
// It is a staging certificate *and* we are not using the staging environment

if (last)
logger.certificateFailed({
serverName: server.server_name,
reason: "staging"
});
else
logger.certificateStaging({
serverName: server.server_name
});

invalidSslServers.push({ server, reason: "staging" });
} else {
logger.certificateValid({
Expand Down

0 comments on commit 3623c2a

Please sign in to comment.