Skip to content

Commit 83f2e20

Browse files
authored
fix(dashmate): cannot read properties of undefined (reading 'expires') (#2164)
1 parent 2ea168a commit 83f2e20

File tree

3 files changed

+34
-8
lines changed

3 files changed

+34
-8
lines changed

packages/dashmate/src/doctor/analyse/analyseConfigFactory.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,10 @@ and revoke the previous certificate in the ZeroSSL dashboard`,
134134
description: chalk`ZeroSSL certificate is not valid.`,
135135
solution: chalk`Please run {bold.cyanBright dashmate ssl zerossl obtain} to get a new one.`,
136136
},
137+
[ERRORS.ZERO_SSL_API_ERROR]: {
138+
description: ssl?.data?.error?.message,
139+
solution: chalk`Please contact ZeroSSL support if needed.`,
140+
},
137141
}[ssl.error] ?? {};
138142

139143
if (description) {

packages/dashmate/src/listr/tasks/ssl/zerossl/obtainZeroSSLCertificateTaskFactory.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ export default function obtainZeroSSLCertificateTaskFactory(
6464
case ERRORS.CERTIFICATE_ID_IS_NOT_SET:
6565
// eslint-disable-next-line no-param-reassign
6666
task.output = 'Certificate is not configured yet, creating a new one';
67+
68+
// We need to create a new certificate
69+
ctx.certificate = null;
6770
break;
6871
case ERRORS.PRIVATE_KEY_IS_NOT_PRESENT:
6972
// If certificate exists but private key does not, then we can't set up TLS connection
@@ -85,6 +88,9 @@ export default function obtainZeroSSLCertificateTaskFactory(
8588
case ERRORS.CERTIFICATE_EXPIRES_SOON:
8689
// eslint-disable-next-line no-param-reassign
8790
task.output = `Certificate exists but expires in less than ${ctx.expirationDays} days at ${ctx.certificate.expires}. Obtain a new one`;
91+
92+
// We need to create a new certificate
93+
ctx.certificate = null;
8894
break;
8995
case ERRORS.CERTIFICATE_IS_NOT_VALIDATED:
9096
// eslint-disable-next-line no-param-reassign
@@ -93,7 +99,12 @@ export default function obtainZeroSSLCertificateTaskFactory(
9399
case ERRORS.CERTIFICATE_IS_NOT_VALID:
94100
// eslint-disable-next-line no-param-reassign
95101
task.output = 'Certificate is not valid. Create a new one';
102+
103+
// We need to create a new certificate
104+
ctx.certificate = null;
96105
break;
106+
case ERRORS.ZERO_SSL_API_ERROR:
107+
throw ctx.error;
97108
default:
98109
throw new Error(`Unknown error: ${error}`);
99110
}

packages/dashmate/src/ssl/zerossl/validateZeroSslCertificateFactory.js

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export const ERRORS = {
1111
CERTIFICATE_EXPIRES_SOON: 'CERTIFICATE_EXPIRES_SOON',
1212
CERTIFICATE_IS_NOT_VALIDATED: 'CERTIFICATE_IS_NOT_VALIDATED',
1313
CERTIFICATE_IS_NOT_VALID: 'CERTIFICATE_IS_NOT_VALID',
14+
ZERO_SSL_API_ERROR: 'ZERO_SSL_API_ERROR',
1415
};
1516

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

7071
// This function will throw an error if certificate with specified ID is not present
71-
const certificate = await getCertificate(data.apiKey, certificateId);
72+
try {
73+
data.certificate = await getCertificate(data.apiKey, certificateId);
74+
} catch (e) {
75+
if (e.code) {
76+
data.error = e;
7277

73-
data.isExpiresSoon = certificate.isExpiredInDays(expirationDays);
78+
return {
79+
error: ERRORS.ZERO_SSL_API_ERROR,
80+
data,
81+
};
82+
}
83+
84+
throw e;
85+
}
86+
87+
data.isExpiresSoon = data.certificate.isExpiredInDays(expirationDays);
7488

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

8498
// We need to make sure that external IP and certificate IP match
85-
if (certificate.common_name !== data.externalIp) {
99+
if (data.certificate.common_name !== data.externalIp) {
86100
return {
87101
error: ERRORS.EXTERNAL_IP_MISMATCH,
88102
data,
89103
};
90104
}
91105

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

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

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

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

130143
// Certificate is valid, so we might need only to download certificate bundle
131-
data.certificate = certificate;
132-
133144
return {
134145
data,
135146
};

0 commit comments

Comments
 (0)