Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/dashmate/src/doctor/analyse/analyseConfigFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,10 @@ and revoke the previous certificate in the ZeroSSL dashboard`,
description: chalk`ZeroSSL certificate is not valid.`,
solution: chalk`Please run {bold.cyanBright dashmate ssl zerossl obtain} to get a new one.`,
},
[ERRORS.ZERO_SSL_API_ERROR]: {
description: ssl?.data?.error?.message,
solution: chalk`Please contact ZeroSSL support if needed.`,
},
}[ssl.error] ?? {};

if (description) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ export default function obtainZeroSSLCertificateTaskFactory(
case ERRORS.CERTIFICATE_ID_IS_NOT_SET:
// eslint-disable-next-line no-param-reassign
task.output = 'Certificate is not configured yet, creating a new one';

// We need to create a new certificate
ctx.certificate = null;
break;
case ERRORS.PRIVATE_KEY_IS_NOT_PRESENT:
// If certificate exists but private key does not, then we can't set up TLS connection
Expand All @@ -85,6 +88,9 @@ export default function obtainZeroSSLCertificateTaskFactory(
case ERRORS.CERTIFICATE_EXPIRES_SOON:
// eslint-disable-next-line no-param-reassign
task.output = `Certificate exists but expires in less than ${ctx.expirationDays} days at ${ctx.certificate.expires}. Obtain a new one`;

// We need to create a new certificate
ctx.certificate = null;
break;
case ERRORS.CERTIFICATE_IS_NOT_VALIDATED:
// eslint-disable-next-line no-param-reassign
Expand All @@ -93,7 +99,12 @@ export default function obtainZeroSSLCertificateTaskFactory(
case ERRORS.CERTIFICATE_IS_NOT_VALID:
// eslint-disable-next-line no-param-reassign
task.output = 'Certificate is not valid. Create a new one';

// We need to create a new certificate
ctx.certificate = null;
break;
case ERRORS.ZERO_SSL_API_ERROR:
throw ctx.error;
default:
throw new Error(`Unknown error: ${error}`);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export const ERRORS = {
CERTIFICATE_EXPIRES_SOON: 'CERTIFICATE_EXPIRES_SOON',
CERTIFICATE_IS_NOT_VALIDATED: 'CERTIFICATE_IS_NOT_VALIDATED',
CERTIFICATE_IS_NOT_VALID: 'CERTIFICATE_IS_NOT_VALID',
ZERO_SSL_API_ERROR: 'ZERO_SSL_API_ERROR',
};

/**
Expand Down Expand Up @@ -68,9 +69,22 @@ export default function validateZeroSslCertificateFactory(homeDir, getCertificat
data.isBundleFilePresent = fs.existsSync(data.bundleFilePath);

// This function will throw an error if certificate with specified ID is not present
const certificate = await getCertificate(data.apiKey, certificateId);
try {
data.certificate = await getCertificate(data.apiKey, certificateId);
} catch (e) {
if (e.code) {
data.error = e;

data.isExpiresSoon = certificate.isExpiredInDays(expirationDays);
return {
error: ERRORS.ZERO_SSL_API_ERROR,
data,
};
}

throw e;
}

data.isExpiresSoon = data.certificate.isExpiredInDays(expirationDays);

// If certificate exists but private key does not, then we can't setup TLS connection
// In this case we need to regenerate a certificate or put back this private key
Expand All @@ -82,17 +96,16 @@ export default function validateZeroSslCertificateFactory(homeDir, getCertificat
}

// We need to make sure that external IP and certificate IP match
if (certificate.common_name !== data.externalIp) {
if (data.certificate.common_name !== data.externalIp) {
return {
error: ERRORS.EXTERNAL_IP_MISMATCH,
data,
};
}

if (['pending_validation', 'draft'].includes(certificate.status)) {
if (['pending_validation', 'draft'].includes(data.certificate.status)) {
// Certificate is already created, so we just need to pass validation
// and download certificate file
data.certificate = certificate;

// We need to download new certificate bundle
data.isBundleFilePresent = false;
Expand All @@ -103,7 +116,7 @@ export default function validateZeroSslCertificateFactory(homeDir, getCertificat
};
}

if (certificate.status !== 'issued' || data.isExpiresSoon) {
if (data.certificate.status !== 'issued' || data.isExpiresSoon) {
// Certificate is going to expire soon, or current certificate is not valid
// we need to obtain a new one

Expand All @@ -128,8 +141,6 @@ export default function validateZeroSslCertificateFactory(homeDir, getCertificat
}

// Certificate is valid, so we might need only to download certificate bundle
data.certificate = certificate;

return {
data,
};
Expand Down
Loading