-
Notifications
You must be signed in to change notification settings - Fork 83
feat(cli)!: Add "certificate generate" command #1560
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
8 commits
Select commit
Hold shift + click to select a range
3d6b1ce
feat(cli)!: Add "certificate generate" command
matz3 a7b4753
refactor: Apply code review feedback
matz3 24a4dfb
fix(server): Prevent hang after certificate generation
matz3 40c3e1e
refactor: Rename dev-cert to UI5 CLI
matz3 75dfdc5
docs: Apply suggestions from code review
matz3 6fb8958
fix: Use valid certificate common name / update outdated test assertions
matz3 fc546c4
Revert "fix(server): Prevent hang after certificate generation"
matz3 1d01fe4
fix(cli): Prevent hang after certificate generation
matz3 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,104 @@ | ||||||
| import chalk from "chalk"; | ||||||
| import process from "node:process"; | ||||||
| import baseMiddleware from "../middlewares/base.js"; | ||||||
| import {getUi5DataDirOrDefault, resolveServerCertificatePaths, formatPath} from "../../dataDir.js"; | ||||||
| import {exists} from "../../utils/fsHelper.js"; | ||||||
|
|
||||||
| const certificateCommand = { | ||||||
| command: "certificate", | ||||||
| describe: "Manage the UI5 CLI server certificate", | ||||||
| middlewares: [baseMiddleware], | ||||||
| }; | ||||||
|
|
||||||
| certificateCommand.builder = function(cli) { | ||||||
| return cli | ||||||
| .demandCommand(1, "Command required. Available command is 'generate'") | ||||||
| .command("generate", "Generate a self-signed server certificate and install it into the trust store", { | ||||||
| handler: handleGenerate, | ||||||
| builder: function(yargs) { | ||||||
| return yargs | ||||||
| .option("key", { | ||||||
| describe: "Path the private key is written to", | ||||||
| defaultDescription: "~/.ui5/server/server.key", | ||||||
| type: "string" | ||||||
| }) | ||||||
| .option("cert", { | ||||||
| describe: "Path the certificate is written to", | ||||||
| defaultDescription: "~/.ui5/server/server.crt", | ||||||
| type: "string" | ||||||
| }) | ||||||
| .option("force", { | ||||||
| alias: "f", | ||||||
| describe: "Generate a new certificate even if one already exists at the target path", | ||||||
| default: false, | ||||||
| type: "boolean" | ||||||
| }) | ||||||
| .example("$0 certificate generate", | ||||||
| "Generate a server certificate in the default UI5 data directory") | ||||||
| .example("$0 certificate generate --force", | ||||||
| "Regenerate the server certificate, overwriting an existing one") | ||||||
| .example("UI5_DATA_DIR=/custom/path $0 certificate generate", | ||||||
| "Generate a server certificate in a non-default UI5 data directory"); | ||||||
| }, | ||||||
| middlewares: [baseMiddleware], | ||||||
| }); | ||||||
| }; | ||||||
|
|
||||||
| async function handleGenerate(argv) { | ||||||
| const ui5DataDir = await getUi5DataDirOrDefault({cwd: process.cwd()}); | ||||||
| const {keyPath, certPath} = resolveServerCertificatePaths(ui5DataDir, { | ||||||
| keyPath: argv.key, | ||||||
| certPath: argv.cert, | ||||||
| }); | ||||||
|
|
||||||
| if (!argv.force) { | ||||||
| let keyExists; | ||||||
| let certExists; | ||||||
| try { | ||||||
| [keyExists, certExists] = await Promise.all([exists(keyPath), exists(certPath)]); | ||||||
| } catch (err) { | ||||||
| throw new Error( | ||||||
| `Failed to check for an existing server certificate at ${formatPath(keyPath)} ` + | ||||||
| `and ${formatPath(certPath)}: ${err.message}`, {cause: err}); | ||||||
| } | ||||||
| // Only a complete pair counts as "already existing". A partial state (just the key or just the | ||||||
| // certificate) is a broken pair that the user cannot otherwise repair without --force, so fall | ||||||
| // through to regeneration, which overwrites any leftover file. | ||||||
| if (keyExists && certExists) { | ||||||
| process.stderr.write( | ||||||
| `A server certificate already exists at the target location:\n` + | ||||||
| ` Private key: ${chalk.bold(formatPath(keyPath))}\n` + | ||||||
| ` Certificate: ${chalk.bold(formatPath(certPath))}\n\n` + | ||||||
| `Use ${chalk.bold("--force")} to generate a new certificate and overwrite the existing one.\n` | ||||||
| ); | ||||||
| return; | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| // Inform the user before triggering the trust-store installation, which requires elevated | ||||||
| // privileges and therefore prompts for the root password (or shows a confirmation dialog on Windows). | ||||||
| if (process.platform === "win32") { | ||||||
| process.stderr.write("Please press allow in the opened dialog to confirm importing the newly created " + | ||||||
| "SSL certificate into the operating system and browsers.\n"); | ||||||
| } else { | ||||||
| process.stderr.write("Please enter your root password to allow importing the newly created " + | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| "SSL certificate into the operating system and browsers.\n"); | ||||||
| } | ||||||
|
|
||||||
| const {generateSslCertificate} = await import("@ui5/server/internal/sslUtil"); | ||||||
| await generateSslCertificate(keyPath, certPath); | ||||||
|
|
||||||
| process.stderr.write( | ||||||
| `\nServer certificate written:\n` + | ||||||
| ` Private key: ${chalk.bold(formatPath(keyPath))}\n` + | ||||||
| ` Certificate: ${chalk.bold(formatPath(certPath))}\n` | ||||||
| ); | ||||||
|
|
||||||
| // devcert-sanscache leaves handles open that keep the event loop alive: it resumes stdin to wait | ||||||
| // for the user to confirm the browser import without pausing it again, and its Firefox flow starts | ||||||
| // an HTTP server that is never closed. The latter runs unconditionally on Windows, so the process | ||||||
| // would otherwise hang here on every run. All work is done at this point, so exit explicitly. | ||||||
| process.exit(0); | ||||||
| } | ||||||
|
|
||||||
| export default certificateCommand; | ||||||
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.