Skip to content
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

Allow to set user and password using environment variables at runtime #426

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,6 @@ services:
environment:
# Tell Dockge where is your stacks directory
- DOCKGE_STACKS_DIR=/opt/stacks
# Usefull for interactive action within the container
stdin_open: true # docker run -i
tty: true # docker run -t
70 changes: 52 additions & 18 deletions extra/reset-password.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { DockgeServer } from "../backend/dockge-server";
import { log } from "../backend/log";
import { io } from "socket.io-client";
import { BaseRes } from "../common/util-common";
import { generatePasswordHash } from "../backend/password-hash";

console.log("== Dockge Reset Password Tool ==");

Expand All @@ -29,35 +30,67 @@ export const main = async () => {
}

try {
let user ;
// No need to actually reset the password for testing, just make sure no connection problem. It is ok for now.

if (!process.env.TEST_BACKEND) {
const user = await R.findOne("user");
if (! user) {
throw new Error("user not found, have you installed?");
user = await R.findOne("user");

if (! user ) {
if ( !process.env.USER ) {
throw new Error("user not found or provided, have you installed? Try to set USER and PASSWORD variables ...");
} else {
console.log("Trying to initialise user : " + process.env.USER);
user = R.dispense("user");
user.username = process.env.USER;
user.password = generatePasswordHash(process.env.PASSWORD);
await R.store(user);
console.log("User/Password set successfully");

// Reset all sessions by reset jwt secret
await server.initJWTSecret();
console.log("JWT reset successfully.");

// Disconnect all other socket clients of the user
await disconnectAllSocketClients(user.username, user.password);
console.log("You may have to restart");
exit;
}
}
}

console.log("Found user: " + user.username);
let password = "";
let confirmPassword = " ";

while (true) {
let password = await question("New Password: ");
let confirmPassword = await question("Confirm New Password: ");
while (true) {

if (password === confirmPassword) {
await User.resetPassword(user.id, password);
if (process.env.PASSWORD) {
console.log("Found password : " + process.env.PASSWORD) ;
password = process.env.PASSWORD ;
confirmPassword = process.env.PASSWORD ;
} else {
console.log("No found password: " ) ;
password = await question("New Password: ");
confirmPassword = await question("Confirm New Password: ");
}

// Reset all sessions by reset jwt secret
await server.initJWTSecret();
if (password === confirmPassword) {
await User.resetPassword(user.id, password);
console.log("Password reset successfully.");

console.log("Password reset successfully.");
// Reset all sessions by reset jwt secret
await server.initJWTSecret();

// Disconnect all other socket clients of the user
await disconnectAllSocketClients(user.username, password);
console.log("JWT reset successfully.");

break;
} else {
console.log("Passwords do not match, please try again.");
}
// Disconnect all other socket clients of the user
await disconnectAllSocketClients(user.username, password);

} else {
console.log("Passwords do not match, please try again.");
break;
}
break;
}
} catch (e) {
if (e instanceof Error) {
Expand Down Expand Up @@ -127,3 +160,4 @@ function disconnectAllSocketClients(username : string, password : string) : Prom
if (!process.env.TEST_BACKEND) {
main();
}

Loading