-
Notifications
You must be signed in to change notification settings - Fork 54
feat(dashmate): add Let's Encrypt SSL provider support #3000
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
daf35c2
feat(dashmate): add Let's Encrypt SSL provider support
8b854ba
Version + Linting fixes
ef47a60
Config migration
76d31c9
Fix small errors from coderabbit
97555b0
Small fixes
e5f51a1
Using nullish coalescing
b59d867
A few more fixes
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
117 changes: 117 additions & 0 deletions
117
packages/dashmate/src/helper/scheduleRenewLetsEncryptCertificateFactory.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| import { CronJob } from 'cron'; | ||
| import path from 'path'; | ||
|
|
||
| import LegoCertificate from '../ssl/letsencrypt/LegoCertificate.js'; | ||
|
|
||
| /** | ||
| * @param {obtainLetsEncryptCertificateTask} obtainLetsEncryptCertificateTask | ||
| * @param {DockerCompose} dockerCompose | ||
| * @param {ConfigFileJsonRepository} configFileRepository | ||
| * @param {ConfigFile} configFile | ||
| * @param {writeConfigTemplates} writeConfigTemplates | ||
| * @param {HomeDir} homeDir | ||
| * @return {scheduleRenewLetsEncryptCertificate} | ||
| */ | ||
| export default function scheduleRenewLetsEncryptCertificateFactory( | ||
| obtainLetsEncryptCertificateTask, | ||
| dockerCompose, | ||
| configFileRepository, | ||
| configFile, | ||
| writeConfigTemplates, | ||
| homeDir, | ||
| ) { | ||
| /** | ||
| * @typedef scheduleRenewLetsEncryptCertificate | ||
| * @param {Config} config | ||
| * @return {Promise<void>} | ||
| */ | ||
| async function scheduleRenewLetsEncryptCertificate(config) { | ||
| const externalIp = config.get('externalIp'); | ||
| const legoDir = homeDir.joinPath(config.getName(), 'platform', 'gateway', 'lego'); | ||
| const certPath = path.join(legoDir, 'certificates', `${externalIp}.crt`); | ||
|
|
||
| let certificate; | ||
| try { | ||
| certificate = LegoCertificate.fromFile(certPath); | ||
| } catch (e) { | ||
| // eslint-disable-next-line no-console | ||
| console.error(`Failed to read Let's Encrypt certificate from ${certPath}: ${e.message}`); | ||
| // Schedule a check in 1 hour to see if certificate appears | ||
| const retryAt = new Date(Date.now() + 60 * 60 * 1000); | ||
|
|
||
| const retryJob = new CronJob(retryAt, async () => { | ||
| retryJob.stop(); | ||
| process.nextTick(() => scheduleRenewLetsEncryptCertificate(config)); | ||
| }); | ||
|
|
||
| retryJob.start(); | ||
| return; | ||
| } | ||
|
|
||
| let renewAt; | ||
| if (certificate.isExpiredInDays(LegoCertificate.EXPIRATION_LIMIT_DAYS)) { | ||
| // Obtain new certificate right away | ||
| renewAt = new Date(Date.now() + 3000); | ||
|
|
||
| // eslint-disable-next-line no-console | ||
| console.log(`Let's Encrypt certificate will expire in less than ${LegoCertificate.EXPIRATION_LIMIT_DAYS} days at ${certificate.expires}. Schedule to obtain it NOW.`); | ||
| } else { | ||
| // Schedule a new check close to expiration period | ||
| renewAt = new Date(certificate.expires); | ||
| renewAt.setDate(renewAt.getDate() - LegoCertificate.EXPIRATION_LIMIT_DAYS); | ||
|
|
||
| // eslint-disable-next-line no-console | ||
| console.log(`Let's Encrypt certificate will expire at ${certificate.expires}. Schedule to obtain at ${renewAt}.`); | ||
| } | ||
|
|
||
| let renewalSucceeded = false; | ||
|
|
||
| const job = new CronJob(renewAt, async () => { | ||
| try { | ||
| const tasks = obtainLetsEncryptCertificateTask(config); | ||
|
|
||
| await tasks.run({ | ||
| expirationDays: LegoCertificate.EXPIRATION_LIMIT_DAYS, | ||
| noRetry: true, | ||
| }); | ||
|
|
||
| // Write config files | ||
| configFileRepository.write(configFile); | ||
| writeConfigTemplates(config); | ||
|
|
||
| // Restart Gateway to catch up new SSL certificates | ||
| await dockerCompose.execCommand(config, 'gateway', 'kill -SIGHUP 1'); | ||
|
|
||
| // eslint-disable-next-line no-console | ||
| console.log("Let's Encrypt certificate renewed successfully"); | ||
|
|
||
| renewalSucceeded = true; | ||
| } catch (e) { | ||
| // eslint-disable-next-line no-console | ||
| console.error(`Failed to renew Let's Encrypt certificate: ${e.message}`); | ||
|
|
||
| renewalSucceeded = false; | ||
| } | ||
|
|
||
| job.stop(); | ||
| }, async () => { | ||
| // Schedule new cron task after completion | ||
| if (renewalSucceeded) { | ||
| // Success: reschedule immediately to read new cert expiry | ||
| process.nextTick(() => scheduleRenewLetsEncryptCertificate(config)); | ||
| } else { | ||
| // Failure: wait 1 hour before retrying to avoid tight loop | ||
| // eslint-disable-next-line no-console | ||
| console.log("Scheduling Let's Encrypt renewal retry in 1 hour"); | ||
|
|
||
| setTimeout(() => { | ||
| scheduleRenewLetsEncryptCertificate(config); | ||
| }, 60 * 60 * 1000); | ||
| } | ||
| }); | ||
|
|
||
| job.start(); | ||
| } | ||
|
|
||
| return scheduleRenewLetsEncryptCertificate; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.