From 2cbfd4b0c60329b52d42dddd6e33aaaed5a6bbc5 Mon Sep 17 00:00:00 2001 From: mohamad khawam Date: Wed, 6 Mar 2024 13:04:24 -0500 Subject: [PATCH] Fixed updateDomainUser and added tests for it in db.spec.ts --- src/db/db.ts | 3 +- src/front/page/passwordScript.ts | 86 ++++---------------------------- test/db.spec.ts | 53 ++++++++++++++++++++ 3 files changed, 64 insertions(+), 78 deletions(-) diff --git a/src/db/db.ts b/src/db/db.ts index c96d2bb..b8c64ad 100644 --- a/src/db/db.ts +++ b/src/db/db.ts @@ -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; @@ -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++; diff --git a/src/front/page/passwordScript.ts b/src/front/page/passwordScript.ts index 377cbe1..2913a99 100644 --- a/src/front/page/passwordScript.ts +++ b/src/front/page/passwordScript.ts @@ -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"; @@ -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) { diff --git a/test/db.spec.ts b/test/db.spec.ts index 4041786..fb7115b 100644 --- a/test/db.spec.ts +++ b/test/db.spec.ts @@ -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");