Updates
This commit is contained in:
@@ -0,0 +1,93 @@
|
||||
import dsql from "@moduletrace/datasquirel";
|
||||
|
||||
type MysqlUser = {
|
||||
User: string;
|
||||
Host: string;
|
||||
};
|
||||
|
||||
export default async function initSQLCheckDsqlUsers() {
|
||||
const readOnlyUser = await dsql.utils.connDbHandler<MysqlUser[]>(
|
||||
global.INIT_SQL_ROOT_DB_CONN,
|
||||
`SELECT user,host FROM mysql.user WHERE user=? AND host=?`,
|
||||
[
|
||||
process.env.DSQL_DB_READ_ONLY_USERNAME,
|
||||
process.env.DSQL_DB_TARGET_IP_ADDRESS,
|
||||
]
|
||||
);
|
||||
|
||||
if (!readOnlyUser?.[0]?.User) {
|
||||
console.log(`Read Only User Does not Exit.`);
|
||||
|
||||
const createReadOnlyUser = await dsql.utils.connDbHandler(
|
||||
global.INIT_SQL_ROOT_DB_CONN,
|
||||
`CREATE USER IF NOT EXISTS \
|
||||
'${process.env.DSQL_DB_READ_ONLY_USERNAME}'@'${process.env.DSQL_DB_TARGET_IP_ADDRESS}' \
|
||||
IDENTIFIED BY '${process.env.DSQL_DB_READ_ONLY_PASSWORD}'`
|
||||
);
|
||||
} else {
|
||||
console.log("Read Only User Exists");
|
||||
|
||||
const grants = await dsql.utils.connDbHandler(
|
||||
global.INIT_SQL_ROOT_DB_CONN,
|
||||
`SHOW GRANTS FOR \
|
||||
'${process.env.DSQL_DB_READ_ONLY_USERNAME}'@'${process.env.DSQL_DB_TARGET_IP_ADDRESS}'`
|
||||
);
|
||||
|
||||
if (checkGrantsArrayForSSL(grants)) {
|
||||
const removeSSL = await dsql.utils.connDbHandler(
|
||||
global.INIT_SQL_ROOT_DB_CONN,
|
||||
`ALTER USER \
|
||||
'${process.env.DSQL_DB_READ_ONLY_USERNAME}'@'${process.env.DSQL_DB_TARGET_IP_ADDRESS}' \
|
||||
REQUIRE NONE`
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
const fullAccessUser = await dsql.utils.connDbHandler<MysqlUser[]>(
|
||||
global.INIT_SQL_ROOT_DB_CONN,
|
||||
`SELECT user,host FROM mysql.user WHERE user=? AND host=?`,
|
||||
[
|
||||
process.env.DSQL_DB_FULL_ACCESS_USERNAME,
|
||||
process.env.DSQL_DB_TARGET_IP_ADDRESS,
|
||||
]
|
||||
);
|
||||
|
||||
if (!fullAccessUser?.[0]?.User) {
|
||||
console.log(`Read Only User Does not Exit.`);
|
||||
|
||||
const createReadOnlyUser = await dsql.utils.connDbHandler(
|
||||
global.INIT_SQL_ROOT_DB_CONN,
|
||||
`CREATE USER IF NOT EXISTS \
|
||||
'${process.env.DSQL_DB_FULL_ACCESS_USERNAME}'@'${process.env.DSQL_DB_TARGET_IP_ADDRESS}' \
|
||||
IDENTIFIED BY '${process.env.DSQL_DB_FULL_ACCESS_PASSWORD}'`
|
||||
);
|
||||
} else {
|
||||
console.log("Full Access User Exists");
|
||||
|
||||
const grants = await dsql.utils.connDbHandler(
|
||||
global.INIT_SQL_ROOT_DB_CONN,
|
||||
`SHOW GRANTS FOR \
|
||||
'${process.env.DSQL_DB_FULL_ACCESS_USERNAME}'@'${process.env.DSQL_DB_TARGET_IP_ADDRESS}'`
|
||||
);
|
||||
|
||||
if (checkGrantsArrayForSSL(grants)) {
|
||||
const removeSSL = await dsql.utils.connDbHandler(
|
||||
global.INIT_SQL_ROOT_DB_CONN,
|
||||
`ALTER USER \
|
||||
'${process.env.DSQL_DB_FULL_ACCESS_USERNAME}'@'${process.env.DSQL_DB_TARGET_IP_ADDRESS}' \
|
||||
REQUIRE NONE`
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function checkGrantsArrayForSSL(array: any[]) {
|
||||
for (let i = 0; i < array.length; i++) {
|
||||
const element = array[i];
|
||||
const firstKey = Object.keys(element)[0];
|
||||
|
||||
if (element[firstKey].match(/require ssl/i)) return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
export default async function initSQLCheckEnvVariables() {
|
||||
if (!process.env.DSQL_DB_TARGET_IP_ADDRESS) {
|
||||
console.log("DSQL_DB_TARGET_IP_ADDRESS env is not set");
|
||||
process.exit(1);
|
||||
}
|
||||
if (!process.env.DSQL_DB_PASSWORD) {
|
||||
console.log("DSQL_DB_PASSWORD env is not set");
|
||||
process.exit(1);
|
||||
}
|
||||
if (!process.env.DSQL_DB_READ_ONLY_PASSWORD) {
|
||||
console.log("DSQL_DB_READ_ONLY_PASSWORD env is not set");
|
||||
process.exit(1);
|
||||
}
|
||||
if (!process.env.DSQL_DB_FULL_ACCESS_PASSWORD) {
|
||||
console.log("DSQL_DB_FULL_ACCESS_PASSWORD env is not set");
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
import dsql from "@moduletrace/datasquirel";
|
||||
|
||||
export default async function initSQLCreateDatabases() {
|
||||
const checkDatasquirelDatabase = await dsql.utils.connDbHandler<any[]>(
|
||||
global.INIT_SQL_MAIN_DB_CONN,
|
||||
"SHOW DATABASES LIKE 'datasquirel'"
|
||||
);
|
||||
|
||||
if (!checkDatasquirelDatabase?.[0]) {
|
||||
const createDsqlDatabase = await dsql.utils.connDbHandler(
|
||||
global.INIT_SQL_MAIN_DB_CONN,
|
||||
"CREATE DATABASE datasquirel"
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
import dsql from "@moduletrace/datasquirel";
|
||||
|
||||
type MysqlUser = {
|
||||
User: string;
|
||||
Host: string;
|
||||
};
|
||||
|
||||
export default async function initSQLHandleRootUsers() {
|
||||
if (process.env.NEXT_PUBLIC_DSQL_LOCAL) {
|
||||
console.log("Production Environment. Root user setup not required");
|
||||
return;
|
||||
}
|
||||
|
||||
const mainRootUser = await dsql.utils.connDbHandler<MysqlUser[]>(
|
||||
global.INIT_SQL_ROOT_DB_CONN,
|
||||
`SELECT user,host FROM mysql.user WHERE user='root' AND host=?`,
|
||||
[process.env.DSQL_DB_TARGET_IP_ADDRESS]
|
||||
);
|
||||
|
||||
if (!mainRootUser?.[0]?.User) {
|
||||
console.log(`Main Root User Does not Exit.`);
|
||||
|
||||
const createMainRootUser = await dsql.utils.connDbHandler<any[]>(
|
||||
global.INIT_SQL_ROOT_DB_CONN,
|
||||
[
|
||||
{
|
||||
query: `CREATE USER IF NOT EXISTS \
|
||||
'root'@'${process.env.DSQL_DB_TARGET_IP_ADDRESS}' \
|
||||
IDENTIFIED BY '${process.env.DSQL_DB_PASSWORD}'`,
|
||||
},
|
||||
{
|
||||
query: `GRANT ALL PRIVILEGES ON *.* TO 'root'@'${process.env.DSQL_DB_TARGET_IP_ADDRESS}' \
|
||||
WITH GRANT OPTION`,
|
||||
},
|
||||
{
|
||||
query: `FLUSH PRIVILEGES`,
|
||||
},
|
||||
]
|
||||
);
|
||||
|
||||
console.log("createMainRootUser", createMainRootUser);
|
||||
|
||||
const checkRootUser = await dsql.utils.connDbHandler<MysqlUser[]>(
|
||||
global.INIT_SQL_ROOT_DB_CONN,
|
||||
`SELECT user,host FROM mysql.user WHERE user='root' AND host='${process.env.DSQL_DB_TARGET_IP_ADDRESS}'`
|
||||
);
|
||||
|
||||
if (checkRootUser?.[0]?.User) {
|
||||
const dropWildcardRootUser = await dsql.utils.connDbHandler<
|
||||
MysqlUser[]
|
||||
>(global.INIT_SQL_ROOT_DB_CONN, "drop user 'root'@'%'");
|
||||
|
||||
console.log("dropWildcardRootUser", dropWildcardRootUser);
|
||||
}
|
||||
} else {
|
||||
console.log("Root User Exists");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user