From d2f5a58f3bcb97acb34803d63fd4e750de57f81f Mon Sep 17 00:00:00 2001 From: Andras Bacsai Date: Sat, 9 Apr 2022 13:33:23 +0200 Subject: [PATCH] fix: Able to change postgres user password from ui --- src/lib/database/databases.ts | 14 ++++++++++---- .../databases/[id]/_Databases/_PostgreSQL.svelte | 6 +++--- src/routes/databases/[id]/index.json.ts | 4 ++-- 3 files changed, 15 insertions(+), 9 deletions(-) diff --git a/src/lib/database/databases.ts b/src/lib/database/databases.ts index 2179b5479..9132fc2b2 100644 --- a/src/lib/database/databases.ts +++ b/src/lib/database/databases.ts @@ -138,7 +138,7 @@ export async function stopDatabase(database) { return everStarted; } -export async function updatePasswordInDb(database, user, newPassword) { +export async function updatePasswordInDb(database, user, newPassword, isRoot) { const { id, type, @@ -157,9 +157,15 @@ export async function updatePasswordInDb(database, user, newPassword) { `DOCKER_HOST=${host} docker exec ${id} mysql -u ${rootUser} -p${rootUserPassword} -e \"ALTER USER '${user}'@'%' IDENTIFIED WITH caching_sha2_password BY '${newPassword}';\"` ); } else if (type === 'postgresql') { - await asyncExecShell( - `DOCKER_HOST=${host} docker exec ${id} psql postgresql://${dbUser}:${dbUserPassword}@${id}:5432/${defaultDatabase} -c "ALTER role ${user} WITH PASSWORD '${newPassword}'"` - ); + if (isRoot) { + await asyncExecShell( + `DOCKER_HOST=${host} docker exec ${id} psql postgresql://postgres:${rootUserPassword}@${id}:5432/${defaultDatabase} -c "ALTER role postgres WITH PASSWORD '${newPassword}'"` + ); + } else { + await asyncExecShell( + `DOCKER_HOST=${host} docker exec ${id} psql postgresql://${dbUser}:${dbUserPassword}@${id}:5432/${defaultDatabase} -c "ALTER role ${user} WITH PASSWORD '${newPassword}'"` + ); + } } else if (type === 'mongodb') { await asyncExecShell( `DOCKER_HOST=${host} docker exec ${id} mongo 'mongodb://${rootUser}:${rootUserPassword}@${id}:27017/admin?readPreference=primary&ssl=false' --eval "db.changeUserPassword('${user}','${newPassword}')"` diff --git a/src/routes/databases/[id]/_Databases/_PostgreSQL.svelte b/src/routes/databases/[id]/_Databases/_PostgreSQL.svelte index 72b01bcea..343ad74d6 100644 --- a/src/routes/databases/[id]/_Databases/_PostgreSQL.svelte +++ b/src/routes/databases/[id]/_Databases/_PostgreSQL.svelte @@ -26,12 +26,12 @@ >Root (postgres) User Password
diff --git a/src/routes/databases/[id]/index.json.ts b/src/routes/databases/[id]/index.json.ts index cdfb33d21..830deee10 100644 --- a/src/routes/databases/[id]/index.json.ts +++ b/src/routes/databases/[id]/index.json.ts @@ -68,9 +68,9 @@ export const post: RequestHandler = async (event) => { const database = await db.getDatabase({ id, teamId }); if (isRunning) { if (database.dbUserPassword !== dbUserPassword) { - await updatePasswordInDb(database, dbUser, dbUserPassword); + await updatePasswordInDb(database, dbUser, dbUserPassword, false); } else if (database.rootUserPassword !== rootUserPassword) { - await updatePasswordInDb(database, rootUser, rootUserPassword); + await updatePasswordInDb(database, rootUser, rootUserPassword, true); } } await db.updateDatabase({