Adding a way to reset the admin password via CLI without any user interaction (#3912)

* feat(cli): Allow unattended password reset via CLI

This commit adds a way to reset the admin password via CLI without any
user interaction (unattended operation).

It adds an optional `new_password` CLI argument that, when present is
used instead of prompting the user for password and password
confirmation.

It also makes sure the user is informed the password could leak into
it's shell history (it's up to him to do some cleaning if
needed/wanted).

* Change to dash style

* Add dry-run

* Fix number password issue

---------

Co-authored-by: Louis Lam <louislam@users.noreply.github.com>
pull/4169/head
Duvergier Claude 12 months ago committed by GitHub
parent 81b84a3c53
commit 478403ef63
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -12,6 +12,10 @@ const rl = readline.createInterface({
}); });
const main = async () => { const main = async () => {
if ("dry-run" in args) {
console.log("Dry run mode, no changes will be made.");
}
console.log("Connecting the database"); console.log("Connecting the database");
Database.initDataDir(args); Database.initDataDir(args);
await Database.connect(false, false, true); await Database.connect(false, false, true);
@ -27,15 +31,26 @@ const main = async () => {
console.log("Found user: " + user.username); console.log("Found user: " + user.username);
while (true) { while (true) {
let password = await question("New Password: "); let password;
let confirmPassword = await question("Confirm New Password: "); let confirmPassword;
// When called with "--new-password" argument for unattended modification (e.g. npm run reset-password -- --new_password=secret)
if ("new-password" in args) {
console.log("Using password from argument");
console.warn("\x1b[31m%s\x1b[0m", "Warning: the password might be stored, in plain text, in your shell's history");
password = confirmPassword = args["new-password"] + "";
} else {
password = await question("New Password: ");
confirmPassword = await question("Confirm New Password: ");
}
if (password === confirmPassword) { if (password === confirmPassword) {
if (!("dry-run" in args)) {
await User.resetPassword(user.id, password); await User.resetPassword(user.id, password);
// Reset all sessions by reset jwt secret // Reset all sessions by reset jwt secret
await initJWTSecret(); await initJWTSecret();
}
break; break;
} else { } else {
console.log("Passwords do not match, please try again."); console.log("Passwords do not match, please try again.");

Loading…
Cancel
Save