104 lines
3.9 KiB
JavaScript
104 lines
3.9 KiB
JavaScript
|
// @ts-check
|
||
|
|
||
|
////////////////////////////////////////
|
||
|
////////////////////////////////////////
|
||
|
////////////////////////////////////////
|
||
|
|
||
|
require("dotenv").config({ path: "./../.env" });
|
||
|
const generator = require("generate-password");
|
||
|
const noDatabaseDbHandler = require("./utils/noDatabaseDbHandler");
|
||
|
const dbHandler = require("./utils/dbHandler");
|
||
|
const encrypt = require("../functions/dsql/encrypt");
|
||
|
|
||
|
/** ****************************************************************************** */
|
||
|
/** ****************************************************************************** */
|
||
|
/** ****************************************************************************** */
|
||
|
/** ****************************************************************************** */
|
||
|
/** ****************************************************************************** */
|
||
|
/** ****************************************************************************** */
|
||
|
|
||
|
/**
|
||
|
* Create database from Schema Function
|
||
|
* ==============================================================================
|
||
|
* @param {object} params - Single object params
|
||
|
* @param {number|string|null} params.userId - User ID or null
|
||
|
*/
|
||
|
async function resetSQLCredentials() {
|
||
|
/**
|
||
|
* @description Users
|
||
|
* @type {*[] | null}
|
||
|
*/ // @ts-ignore
|
||
|
const users = await dbHandler({
|
||
|
query: `SELECT * FROM users`,
|
||
|
});
|
||
|
|
||
|
if (!users) {
|
||
|
process.exit();
|
||
|
}
|
||
|
|
||
|
for (let i = 0; i < users.length; i++) {
|
||
|
const user = users[i];
|
||
|
|
||
|
if (!user) continue;
|
||
|
|
||
|
const defaultMariadbUserHost = process.env.DSQL_DB_HOST || "127.0.0.1";
|
||
|
|
||
|
try {
|
||
|
const username = `dsql_user_${user.id}`;
|
||
|
const password = generator.generate({
|
||
|
length: 16,
|
||
|
numbers: true,
|
||
|
symbols: true,
|
||
|
uppercase: true,
|
||
|
exclude: "*#.'`\"",
|
||
|
});
|
||
|
const encryptedPassword = encrypt({ data: password });
|
||
|
|
||
|
await noDatabaseDbHandler(`DROP USER IF EXISTS '${username}'@'%'`);
|
||
|
await noDatabaseDbHandler(
|
||
|
`DROP USER IF EXISTS '${username}'@'${defaultMariadbUserHost}'`
|
||
|
);
|
||
|
|
||
|
await noDatabaseDbHandler(
|
||
|
`CREATE USER IF NOT EXISTS '${username}'@'${defaultMariadbUserHost}' IDENTIFIED BY '${password}' REQUIRE SSL`
|
||
|
);
|
||
|
|
||
|
await noDatabaseDbHandler(
|
||
|
`GRANT ALL PRIVILEGES ON \`datasquirel_user_${user.id}_%\`.* TO '${username}'@'${defaultMariadbUserHost}'`
|
||
|
);
|
||
|
|
||
|
await noDatabaseDbHandler(`FLUSH PRIVILEGES`);
|
||
|
|
||
|
const updateUser = await dbHandler({
|
||
|
query: `UPDATE users SET mariadb_user = ?, mariadb_host = ?, mariadb_pass = ? WHERE id = ?`,
|
||
|
values: [
|
||
|
username,
|
||
|
defaultMariadbUserHost,
|
||
|
encryptedPassword,
|
||
|
user.id,
|
||
|
],
|
||
|
});
|
||
|
|
||
|
console.log(
|
||
|
`User ${user.id}: ${user.first_name} ${user.last_name} SQL credentials successfully added.`
|
||
|
);
|
||
|
} catch (/** @type {any} */ error) {
|
||
|
console.log(`Error in adding SQL user =>`, error.message);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
process.exit();
|
||
|
|
||
|
////////////////////////////////////////
|
||
|
////////////////////////////////////////
|
||
|
////////////////////////////////////////
|
||
|
}
|
||
|
|
||
|
/** ****************************************************************************** */
|
||
|
/** ****************************************************************************** */
|
||
|
/** ****************************************************************************** */
|
||
|
/** ****************************************************************************** */
|
||
|
/** ****************************************************************************** */
|
||
|
|
||
|
resetSQLCredentials();
|