Skip to content

Commit

Permalink
Fixed updateDomainUser and added tests for it in db.spec.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
dominusmars committed Mar 6, 2024
1 parent fb99703 commit 2cbfd4b
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 78 deletions.
3 changes: 2 additions & 1 deletion src/db/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -738,6 +738,7 @@ class DataBase {
}

private async updateDomainUser(username: string, domain: string, passwordHash: string, skip_id: string) {
if (!domain || domain.length <= 1) return;
for await (let id of this.users.keys()) {
try {
if (skip_id == id) continue;
Expand Down Expand Up @@ -783,7 +784,7 @@ class DataBase {

await this.users.put(user.user_id, user);

if (user.domain != "" || user.domain != undefined) {
if (user.domain && user.domain.length >= 1) {
await this.updateDomainUser(user.username, user.domain, user.password, user.user_id);
}
this.changes++;
Expand Down
86 changes: 9 additions & 77 deletions src/front/page/passwordScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,76 +121,6 @@ async function runScript(debug?: boolean) {

let results = await runScriptOn(computers, seed, debug);

// //Generate values
// const amountOfPasswords = computers.reduce((value, computer) => value + computer.users.length, 0);

// const passwordsArray = debug ? Array.from({ length: amountOfPasswords }, () => TEST_PASSWORD()) : generatePasses(amountOfPasswords, seed);

// const passwords = computers.map((computer) => {
// let target_passwords = [];
// for (let i = 0; i < computer.users.length; i++) {
// let password = passwordsArray.pop();
// if (!password) throw new Error("Not enough passwords");
// target_passwords.push(password);
// }
// return target_passwords;
// });

// log(
// debug
// ? `Running DEBUG script on ${computers.length} computers ${amountOfPasswords} users`
// : `Running script on ${computers.length} computers ${amountOfPasswords} ${amountOfPasswords} users`
// );
// logger.log(
// debug
// ? `Running DEBUG script on ${computers.length} computers ${amountOfPasswords} users`
// : `Running script on ${computers.length} computers ${amountOfPasswords} users`
// );

// let bar = new Bar(amountOfPasswords);
// var then = new Date();

// let success = 0;
// let fails = 0;
// const promises = computers.map(async (target, i) => {
// const target_passwords = passwords[i];
// let results: (string | boolean)[] = [];
// for await (const [index, user] of target.users.entries()) {
// let result: string | boolean = false;
// try {
// let password = target_passwords[index];
// if (!password) throw new Error("Unable to find password to change to");
// const passwordResult = await changePasswordOf(target, user, password);
// if (typeof passwordResult == "string" || passwordResult.error) {
// throw new Error(typeof passwordResult == "string" ? passwordResult : passwordResult.error ? passwordResult.error : "");
// }
// let wrote = await runningDB.writeUserResult(user.user_id, passwordResult);

// bar.done(`${user.username} ${user.ipaddress} ${user.hostname}`);
// success++;
// if (!wrote) throw new Error("Unable to write user password");
// result = `Success ${user.username} ${user.ipaddress} ${user.hostname}`;
// } catch (error) {
// let passwordTried = target_passwords[index];
// if (passwordTried) await runningDB.writeUserFailedPassword(user.user_id, passwordTried);
// bar.done(`Errored: ${user.username} ${user.ipaddress} ${user.hostname}`);
// result = `Errored: ${user.username} ${user.ipaddress} ${user.hostname} ` + (error as Error).message;
// fails++;
// }
// results.push(result);
// }

// return results;
// });

// var results = await Promise.allSettled(promises);
// var now = new Date();

// var lapse_time = now.getTime() - then.getTime();
// logger.log(`Successfully changed passwords on ${success} of ${amountOfPasswords} in ${lapse_time} ms`, "info");

// console.log(`Successfully changed passwords on ${success} of ${amountOfPasswords} in ${lapse_time} ms`.green);

const runningLog = results.reduce((prev, value) => {
if (!value) return prev;
if (typeof value == "boolean") return prev + "UNKNOWN\n";
Expand Down Expand Up @@ -307,13 +237,15 @@ async function runScriptNetworkSelect(debug?: boolean) {

log(`Selected ${computers.length} targets on ${networks.join(" ")}`);

const { seed } = await inquirer.prompt([
{
name: "seed",
type: "input",
message: "Please enter a seed",
},
]);
const { seed } = debug
? { seed: "" }
: await inquirer.prompt([
{
name: "seed",
type: "input",
message: "Please enter a seed",
},
]);

//Hold Original Log for later
console.log = function (...args) {
Expand Down
53 changes: 53 additions & 0 deletions test/db.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,59 @@ describe("DataBase", () => {
assert.ok(!result, "able to edit user which doesnt exit");
});
});
describe("writeUserResult", () => {
it("should be able to change user password and update failed passwords", async () => {
const ip = "192.168.2.4";
const os = "linux";
const hostname = "myhost";
const domain = "example.com";
const username = "username";
const password = "password";
await db.addTargetAndUser(hostname, ip, username, password, os, domain);
const user = await db.getUser("192.168.2.4", username);
if (!user) throw new Error("Unable to get user after insert");
await db.writeUserResult(user.user_id, { error: false, password: "password123", ssh: true });
const user_after = await db.getUser("192.168.2.4", username);
if (!user_after) throw new Error("Unable to get user after password change");

assert.equal(user_after.password, "password123", "user password not changed");
assert.ok(user_after.oldPasswords.includes("password"), "password was not included in old passwords");
});
it("should change password of users on a domain", async () => {
const ip = "192.168.2.5";
const os = "linux";
const hostname = "myhost";
const domain = "example.com";
const username = "username";
const password = "password";
await db.addTargetAndUser(hostname, ip, username, password, os, domain);
const user = await db.getUser("192.168.2.4", username);
if (!user) throw new Error("Unable to get user after insert");
await db.writeUserResult(user.user_id, { error: false, password: "password1234", ssh: true });
const user_after = await db.getUser("192.168.2.5", username);
if (!user_after) throw new Error("Unable to get user after password change");
assert.equal(user_after.password, "password1234", "user password not changed");
assert.ok(user_after.oldPasswords.includes("password"), "password was not included in old passwords");
});
it("shouldn't change password of nondomain user", async () => {
const ip = "192.168.2.6";
const os = "linux";
const hostname = "myhost";
const domain = "";
const username = "username";
const password = "password";
await db.addTargetAndUser(hostname, ip, username, password, os, domain);
await db.addTargetAndUser(hostname, "192.168.2.7", "username", "password", "linux", "");

const user = await db.getUser("192.168.2.6", username);
if (!user) throw new Error("Unable to get user after insert");
await db.writeUserResult(user.user_id, { error: false, password: "password1234", ssh: true });
const user_after = await db.getUser("192.168.2.7", username);
if (!user_after) throw new Error("Unable to get user after password change");
assert.equal(user_after.password, "password", "user password not changed");
assert.ok(!user_after.oldPasswords.includes("password"), "password was included in old passwords");
});
});
describe("editComputer", () => {
it("should be able to change domain and os", async () => {
let computer = await db.getComputer("192.168.1.3");
Expand Down

0 comments on commit 2cbfd4b

Please sign in to comment.