-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(dashmate): cleanup zerossl certs command (#2298)
- Loading branch information
Showing
8 changed files
with
174 additions
and
7 deletions.
There are no files selected for viewing
This file contains 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,58 @@ | ||
import { Listr } from 'listr2'; | ||
import { Flags } from '@oclif/core'; | ||
import ConfigBaseCommand from '../../oclif/command/ConfigBaseCommand.js'; | ||
import MuteOneLineError from '../../oclif/errors/MuteOneLineError.js'; | ||
|
||
export default class CleanupCommand extends ConfigBaseCommand { | ||
static description = `Cleanup Zero SSL certificate | ||
Cancel all drafted or pending validation certificates on ZeroSSL | ||
`; | ||
|
||
static flags = { | ||
...ConfigBaseCommand.flags, | ||
verbose: Flags.boolean({ char: 'v', description: 'use verbose mode for output', default: false }), | ||
}; | ||
|
||
/** | ||
* @param {Object} args | ||
* @param {Object} flags | ||
* @param {boolean} flags.verbose | ||
* @param {Config} config | ||
* @param {cleanupZeroSSLCertificatesTask} cleanupZeroSSLCertificatesTask | ||
* @return {Promise<void>} | ||
*/ | ||
async runWithDependencies( | ||
args, | ||
{ | ||
verbose: isVerbose, | ||
}, | ||
config, | ||
cleanupZeroSSLCertificatesTask, | ||
) { | ||
const tasks = new Listr( | ||
[ | ||
{ | ||
title: 'Cleanup ZeroSSL certificate', | ||
task: () => cleanupZeroSSLCertificatesTask(config), | ||
}, | ||
], | ||
{ | ||
renderer: isVerbose ? 'verbose' : 'default', | ||
rendererOptions: { | ||
showTimer: isVerbose, | ||
clearOutput: false, | ||
collapse: false, | ||
showSubtasks: true, | ||
removeEmptyLines: false, | ||
}, | ||
}, | ||
); | ||
|
||
try { | ||
await tasks.run(); | ||
} catch (e) { | ||
throw new MuteOneLineError(e); | ||
} | ||
} | ||
} |
This file contains 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 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 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
98 changes: 98 additions & 0 deletions
98
packages/dashmate/src/listr/tasks/ssl/zerossl/cleanupZeroSSLCertificatesTaskFactory.js
This file contains 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,98 @@ | ||
import chalk from 'chalk'; | ||
import { Listr } from 'listr2'; | ||
import { Observable } from 'rxjs'; | ||
import wait from '../../../../util/wait.js'; | ||
|
||
/** | ||
* @param {listCertificates} listCertificates | ||
* @param {cancelCertificate} cancelCertificate | ||
* @return {cleanupZeroSSLCertificatesTask} | ||
*/ | ||
export default function cleanupZeroSSLCertificatesTaskFactory( | ||
listCertificates, | ||
cancelCertificate, | ||
) { | ||
/** | ||
* @typedef {cleanupZeroSSLCertificatesTask} | ||
* @param {Config} config | ||
* @return {Listr} | ||
*/ | ||
function cleanupZeroSSLCertificatesTask(config) { | ||
const apiKey = config.get('platform.gateway.ssl.providerConfigs.zerossl.apiKey', true); | ||
|
||
return new Listr([ | ||
{ | ||
title: 'Collect drafted and pending validation certificates', | ||
// Skips the check if force flag is set | ||
task: async (ctx, task) => { | ||
ctx.certificates = []; | ||
|
||
let certificatesPerRequest = []; | ||
let page = 1; | ||
|
||
// Fetch all certificates in draft or pending validation status | ||
// with pagination | ||
do { | ||
certificatesPerRequest = await listCertificates(apiKey, ['draft', 'pending_validation'], page); | ||
|
||
ctx.certificates = ctx.certificates.concat(certificatesPerRequest); | ||
|
||
page += 1; | ||
|
||
// eslint-disable-next-line no-param-reassign | ||
task.output = `Found ${ctx.certificates.length} certificates`; | ||
} while (certificatesPerRequest.length === 1000); | ||
|
||
ctx.total = ctx.certificates.length; | ||
}, | ||
}, | ||
{ | ||
title: 'Cancel certificates', | ||
skip: (ctx) => ctx.certificates.length === 0, | ||
task: async (ctx, task) => { | ||
// eslint-disable-next-line no-param-reassign | ||
task.title = `Cancel ${ctx.certificates.length} certificates`; | ||
ctx.canceled = 0; | ||
ctx.errored = 0; | ||
return new Observable(async (observer) => { | ||
for (const certificate of ctx.certificates) { | ||
try { | ||
await cancelCertificate(apiKey, certificate.id); | ||
|
||
ctx.canceled += 1; | ||
} catch (e) { | ||
ctx.errored += 1; | ||
|
||
if (process.env.DEBUG) { | ||
// eslint-disable-next-line no-console | ||
console.warn(e); | ||
} | ||
} | ||
|
||
observer.next(chalk`{green ${ctx.canceled}} / {red ${ctx.errored}} / ${ctx.total}`); | ||
|
||
await wait(100); | ||
} | ||
|
||
if (ctx.errored > 0) { | ||
observer.error(new Error('Some certificates were not canceled. Please try again.')); | ||
} else { | ||
observer.complete(); | ||
} | ||
|
||
return this; | ||
}); | ||
}, | ||
options: { | ||
persistentOutput: true, | ||
}, | ||
}, | ||
], { | ||
rendererOptions: { | ||
showErrorMessage: true, | ||
}, | ||
}); | ||
} | ||
|
||
return cleanupZeroSSLCertificatesTask; | ||
} |
This file contains 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 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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
import requestApi from './requestApi'; | ||
import requestApi from './requestApi.js'; | ||
|
||
/** | ||
* Get ZeroSSL certificate | ||
|
This file contains 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