From ef5c6a37b9ea0573d1aef9416e96e153def30591 Mon Sep 17 00:00:00 2001 From: Keyvan Chamani Date: Wed, 28 Aug 2024 17:42:27 -0400 Subject: [PATCH] UNTRACKED/k1ch/refactor-persona-db-layer (#117) * refactor: UNTRACKED/k1ch/refactor-persona-db-layer * release: Bump up version to v2.1.1 * minor: UNTRACKED/k1ch/introduce env var for knex pool --- database/knexfile.js | 8 +++++-- database/layer/admin-persona.js | 28 +++++++++++------------ database/layer/admin-personapermission.js | 20 ++++++++-------- database/layer/admin-personarole.js | 24 +++++++++---------- database/package-lock.json | 4 ++-- database/package.json | 2 +- server/.env.sample | 2 ++ server/package-lock.json | 6 ++--- server/package.json | 2 +- server/the-usher-openapi-spec.yaml | 2 +- 10 files changed, 49 insertions(+), 49 deletions(-) diff --git a/database/knexfile.js b/database/knexfile.js index c213af2..fee92be 100644 --- a/database/knexfile.js +++ b/database/knexfile.js @@ -6,6 +6,10 @@ module.exports = { searchPath: [env.PGSCHEMA, 'public'], migrations: { tableName: 'knex_migrations', - schemaName: env.PGSCHEMA - } + schemaName: env.PGSCHEMA, + }, + pool: { + min: process.env.KNEX_POOL_MIN || 1, + max: process.env.KNEX_POOL_MAX || 100, + }, } diff --git a/database/layer/admin-persona.js b/database/layer/admin-persona.js index 446dfa7..16e38f4 100644 --- a/database/layer/admin-persona.js +++ b/database/layer/admin-persona.js @@ -1,16 +1,14 @@ -const { PGPool } = require('./pg_pool') -const pool = new PGPool() const { usherDb } = require('./knex') const { pgErrorHandler } = require('../utils/pgErrorHandler') const insertPersona = async (tenantName, issClaim, subClaim, userContext) => { const sql = `INSERT INTO usher.personas (tenantkey, sub_claim, user_context) - SELECT key, $3, $4 + SELECT key, ?, ? FROM usher.tenants - WHERE name = $1 AND iss_claim = $2` - const sqlParams = [tenantName, issClaim, subClaim, userContext] + WHERE name = ? AND iss_claim = ?` + const sqlParams = [subClaim, userContext, tenantName, issClaim] try { - const results = await pool.query(sql, sqlParams) + const results = await usherDb.raw(sql, sqlParams) if (results.rowCount === 1) { return 'Insert successful' } else { @@ -18,7 +16,7 @@ const insertPersona = async (tenantName, issClaim, subClaim, userContext) => { return `Insert failed: ${errTenantDoesNotExist}` } } catch (error) { - if (error.message === 'duplicate key value violates unique constraint "personas_subclaim_userscope_tenantkey_uq"') { + if (error.message.includes('duplicate key value violates unique constraint "personas_subclaim_userscope_tenantkey_uq"')) { const errPersonaAlreadyExists = `A persona (sub_claim = ${subClaim}; user_context = ${userContext}) already exists on tenantname ${tenantName} iss_claim ${issClaim}` return `Insert failed: ${errPersonaAlreadyExists}` } @@ -28,11 +26,11 @@ const insertPersona = async (tenantName, issClaim, subClaim, userContext) => { const deletePersona = async (tenantName, issClaim, subClaim, userContext) => { const sql = `DELETE FROM usher.personas p - WHERE EXISTS (SELECT 1 FROM usher.tenants t WHERE t.KEY = p.tenantkey AND t.name = $1 and t.iss_claim = $2) - AND p.sub_claim = $3 AND p.user_context = $4` + WHERE EXISTS (SELECT 1 FROM usher.tenants t WHERE t.KEY = p.tenantkey AND t.name = ? and t.iss_claim = ?) + AND p.sub_claim = ? AND p.user_context = ?` const sqlParams = [tenantName, issClaim, subClaim, userContext] try { - const deleteResult = await pool.query(sql, sqlParams) + const deleteResult = await usherDb.raw(sql, sqlParams) if (deleteResult.rowCount === 1) { return 'Delete successful' } else { @@ -45,12 +43,12 @@ const deletePersona = async (tenantName, issClaim, subClaim, userContext) => { } const updatePersona = async (tenantName, issClaim, oldSubClaim, newSubClaim, oldUserContext, newUserContext) => { - const sql = `UPDATE usher.personas p SET sub_claim = $4, user_context = $6 - WHERE EXISTS (SELECT 1 FROM usher.tenants t WHERE t.KEY = p.tenantkey AND t.name = $1 and t.iss_claim = $2) - AND p.sub_claim = $3 AND p.user_context = $5` - const sqlParams = [tenantName, issClaim, oldSubClaim, newSubClaim, oldUserContext, newUserContext] + const sql = `UPDATE usher.personas p SET sub_claim = ?, user_context = ? + WHERE EXISTS (SELECT 1 FROM usher.tenants t WHERE t.KEY = p.tenantkey AND t.name = ? and t.iss_claim = ?) + AND p.sub_claim = ? AND p.user_context = ?` + const sqlParams = [newSubClaim, newUserContext, tenantName, issClaim, oldSubClaim, oldUserContext] try { - const updateResult = await pool.query(sql, sqlParams) + const updateResult = await usherDb.raw(sql, sqlParams) if (updateResult.rowCount === 1) { return 'Update successful' } else { diff --git a/database/layer/admin-personapermission.js b/database/layer/admin-personapermission.js index d22ab54..b7380f5 100644 --- a/database/layer/admin-personapermission.js +++ b/database/layer/admin-personapermission.js @@ -1,5 +1,3 @@ -const { PGPool } = require('./pg_pool') -const pool = new PGPool() const { usherDb } = require('./knex') const { pgErrorHandler } = require('../utils/pgErrorHandler') @@ -11,12 +9,12 @@ const insertPersonaPermissionByClientId = async (clientId, subClaim, permissionN INNER JOIN usher.tenantclients tc on (c.key = tc.clientkey) INNER JOIN usher.tenants t on (t.key = tc.tenantkey) INNER JOIN usher.personas prs on (prs.tenantkey = t.key) - WHERE c.client_id = $1 - AND prs.sub_claim = $2 - AND pm.name = $3` + WHERE c.client_id = ? + AND prs.sub_claim = ? + AND pm.name = ?` const sqlParams = [clientId, subClaim, permissionName] try { - const results = await pool.query(sql, sqlParams) + const results = await usherDb.raw(sql, sqlParams) if (results.rowCount === 1) { return 'Insert successful' } else { @@ -24,7 +22,7 @@ const insertPersonaPermissionByClientId = async (clientId, subClaim, permissionN return `Insert failed: ${errClientPersonaPermissionDoesNotExist}` } } catch (error) { - if (error.message === 'duplicate key value violates unique constraint "personapermissions_personakey_permissionkey_uq"') { + if (error.message.includes('duplicate key value violates unique constraint "personapermissions_personakey_permissionkey_uq"')) { const errClientPersonaPermissionAlreadyExists = `A persona permission client_id = ${clientId}; persona ${subClaim}; is already assigned to permission ${permissionName}.` return `Insert failed: ${errClientPersonaPermissionAlreadyExists}` } @@ -37,12 +35,12 @@ const deletePersonaPermissionByClientId = async (clientId, subClaim, permissionN WHERE EXISTS (SELECT c.key FROM usher.clients c - JOIN usher.permissions pm ON pm.clientkey = c.key WHERE c.client_id = $1 AND pm.name = $3) + JOIN usher.permissions pm ON pm.clientkey = c.key WHERE c.client_id = ? AND pm.name = ?) AND - EXISTS (SELECT prs.key FROM usher.personas prs WHERE prs.KEY = pp.personakey AND prs.sub_claim = $2)` - const sqlParams = [clientId, subClaim, permissionName] + EXISTS (SELECT prs.key FROM usher.personas prs WHERE prs.KEY = pp.personakey AND prs.sub_claim = ?)` + const sqlParams = [clientId, permissionName, subClaim] try { - const results = await pool.query(sql, sqlParams) + const results = await usherDb.raw(sql, sqlParams) if (results.rowCount === 1) { return 'Delete successful' } else { diff --git a/database/layer/admin-personarole.js b/database/layer/admin-personarole.js index e84e6ac..7a9ee35 100644 --- a/database/layer/admin-personarole.js +++ b/database/layer/admin-personarole.js @@ -1,5 +1,3 @@ -const { PGPool } = require('./pg_pool') -const pool = new PGPool() const { usherDb } = require('./knex') const { pgErrorHandler } = require('../utils/pgErrorHandler') @@ -7,11 +5,11 @@ const insertPersonaRole = async (tenantName, issClaim, subClaim, userContext, cl const sql = `INSERT INTO usher.personaroles (personakey, rolekey) SELECT p.KEY, r.KEY FROM usher.roles r JOIN usher.clients c ON (c.key = r.clientkey) inner join usher.tenantclients tc ON (c.key = tc.clientkey) inner JOIN usher.tenants t ON (t.key = tc.tenantkey) inner join usher.personas p on (p.tenantkey = t.key) - WHERE t.name = $1 AND t.iss_claim = $2 AND p.sub_claim = $3 AND p.user_context = $4 - AND c.client_id = $5 AND r.name = $6` + WHERE t.name = ? AND t.iss_claim = ? AND p.sub_claim = ? AND p.user_context = ? + AND c.client_id = ? AND r.name = ?` const sqlParams = [tenantName, issClaim, subClaim, userContext, clientId, rolename] try { - const results = await pool.query(sql, sqlParams) + const results = await usherDb.raw(sql, sqlParams) if (results.rowCount === 1) { return 'Insert successful' } else { @@ -19,7 +17,7 @@ const insertPersonaRole = async (tenantName, issClaim, subClaim, userContext, cl return `Insert failed: ${errPersonaRoleDoesNotExist}` } } catch (error) { - if (error.message === 'duplicate key value violates unique constraint "personaroles_personakey_rolekey_uq"') { + if (error.message.includes('duplicate key value violates unique constraint "personaroles_personakey_rolekey_uq"')) { const errPersonaRoleAlreadyExists = `A client role client_id = ${clientId} & rolename ${rolename} is already assigned to tenantname = ${tenantName} & iss_claim = ${issClaim} & sub_claim = ${subClaim} & user_context = ${userContext}` return `Insert failed: ${errPersonaRoleAlreadyExists}` } @@ -36,16 +34,16 @@ const deletePersonaRole = async (tenantName, issClaim, subClaim, userContext, cl JOIN usher.tenantclients tc ON (c.key = tc.clientkey) JOIN usher.tenants t ON (t.key = tc.tenantkey) JOIN usher.personas p ON (p.tenantkey = t.key) - WHERE t.name = $1 - AND t.iss_claim = $2 - AND p.sub_claim = $3 - AND p.user_context = $4 - AND c.client_id = $5 - AND r.name = $6 + WHERE t.name = ? + AND t.iss_claim = ? + AND p.sub_claim = ? + AND p.user_context = ? + AND c.client_id = ? + AND r.name = ? );` const sqlParams = [tenantName, issClaim, subClaim, userContext, clientId, rolename] try { - const results = await pool.query(sql, sqlParams) + const results = await usherDb.raw(sql, sqlParams) if (results.rowCount === 1) { return 'Delete successful' } else { diff --git a/database/package-lock.json b/database/package-lock.json index 8442a85..c5c517d 100644 --- a/database/package-lock.json +++ b/database/package-lock.json @@ -1,12 +1,12 @@ { "name": "@dmgt-tech/the-usher-server-database", - "version": "2.1.0", + "version": "2.1.1", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@dmgt-tech/the-usher-server-database", - "version": "2.1.0", + "version": "2.1.1", "license": "MIT", "dependencies": { "dotenv": "16.4.5", diff --git a/database/package.json b/database/package.json index b426acc..54deb32 100644 --- a/database/package.json +++ b/database/package.json @@ -1,6 +1,6 @@ { "name": "@dmgt-tech/the-usher-server-database", - "version": "2.1.0", + "version": "2.1.1", "description": "Database layer for TheUsher", "scripts": { "test": "mocha --exit", diff --git a/server/.env.sample b/server/.env.sample index 2ac0f89..7117d62 100644 --- a/server/.env.sample +++ b/server/.env.sample @@ -13,6 +13,8 @@ NODE_ENV=development PGURI=postgres://postgres:tehsecure@localhost:5432/postgres?sslmode=disable PGSCHEMA=usher +KNEX_POOL_MIN=1 +KNEX_POOL_MAX=100 # TOKEN LIFETIMES # Duration the access_token is valid: diff --git a/server/package-lock.json b/server/package-lock.json index c18fba9..6cebd6d 100644 --- a/server/package-lock.json +++ b/server/package-lock.json @@ -1,12 +1,12 @@ { "name": "@dmgt-tech/the-usher-server", - "version": "2.1.0", + "version": "2.1.1", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@dmgt-tech/the-usher-server", - "version": "2.1.0", + "version": "2.1.1", "license": "MIT", "dependencies": { "cors": "2.8.5", @@ -39,7 +39,7 @@ }, "../database": { "name": "@dmgt-tech/the-usher-server-database", - "version": "2.1.0", + "version": "2.1.1", "license": "MIT", "dependencies": { "dotenv": "16.4.5", diff --git a/server/package.json b/server/package.json index 7b21d41..99f4ea3 100644 --- a/server/package.json +++ b/server/package.json @@ -1,6 +1,6 @@ { "name": "@dmgt-tech/the-usher-server", - "version": "2.1.0", + "version": "2.1.1", "description": "The Usher Authorization Server", "engines": { "node": ">=18" diff --git a/server/the-usher-openapi-spec.yaml b/server/the-usher-openapi-spec.yaml index 8eb69ac..f2a0719 100644 --- a/server/the-usher-openapi-spec.yaml +++ b/server/the-usher-openapi-spec.yaml @@ -9,7 +9,7 @@ info: license: name: MIT url: https://opensource.org/licenses/MIT - version: 2.1.0 + version: 2.1.1 externalDocs: description: GitHub Repository url: https://github.com/DMGT-TECH/the-usher-server