From 3c39c618a1ab234c151d960ec16d12d0ccf6b69d Mon Sep 17 00:00:00 2001 From: Gao Sun Date: Wed, 15 Feb 2023 23:30:27 +0800 Subject: [PATCH] refactor: update per review --- .scripts/compare-database.js | 10 +++++++--- packages/core/src/env-set/index.ts | 4 ++-- packages/core/src/middleware/koa-spa-proxy.test.ts | 2 +- packages/core/src/middleware/koa-spa-proxy.ts | 5 +---- packages/core/src/tenants/Tenant.ts | 4 ++-- packages/core/src/tenants/utils.ts | 2 +- .../next-1676190092-migrate-admin-data.ts | 12 ++++++------ 7 files changed, 20 insertions(+), 19 deletions(-) diff --git a/.scripts/compare-database.js b/.scripts/compare-database.js index 83dd0e37b..63b44ae98 100644 --- a/.scripts/compare-database.js +++ b/.scripts/compare-database.js @@ -117,14 +117,18 @@ assert.deepStrictEqual(...manifests); const queryDatabaseData = async (database) => { const pool = new pg.Pool({ database, user: 'postgres', password: 'postgres' }); const result = await Promise.all(manifests[0].tables - // system configs are usually generated or time-relative, ignore for now - .filter(({ table_name }) => !['logto_configs', '_logto_configs', 'systems'].includes(table_name)) .map(async ({ table_name }) => { const { rows } = await pool.query(/* sql */`select * from ${table_name};`); // check config rows except the value column if (['logto_configs', '_logto_configs', 'systems'].includes(table_name)) { - return [table_name, omitArray(rows, 'value')]; + return [table_name, omitArray(rows, 'value').sort((a, b) => { + if (a.tenant_id === b.tenant_id) { + return a.key.localeCompare(b.key); + } + + return a.tenant_id.localeCompare(b.tenant_id); + })]; } return [table_name, omitArray( diff --git a/packages/core/src/env-set/index.ts b/packages/core/src/env-set/index.ts index 01268578f..c8155bb6b 100644 --- a/packages/core/src/env-set/index.ts +++ b/packages/core/src/env-set/index.ts @@ -19,14 +19,14 @@ import { throwNotLoadedError } from './throw-errors.js'; export enum UserApps { Api = 'api', Oidc = 'oidc', - Console = 'console', DemoApp = 'demo-app', - Welcome = 'welcome', } /** Apps (also paths) ONLY for the admin tenant. */ export enum AdminApps { Me = 'me', + Console = 'console', + Welcome = 'welcome', } const getTenantEndpoint = (id: string) => { diff --git a/packages/core/src/middleware/koa-spa-proxy.test.ts b/packages/core/src/middleware/koa-spa-proxy.test.ts index 800942fed..fdc824e35 100644 --- a/packages/core/src/middleware/koa-spa-proxy.test.ts +++ b/packages/core/src/middleware/koa-spa-proxy.test.ts @@ -35,7 +35,7 @@ describe('koaSpaProxy middleware', () => { const next = jest.fn(); - for (const app of Object.values(UserApps)) { + for (const app of Object.values(mountedApps)) { // eslint-disable-next-line @typescript-eslint/no-loop-func it(`${app} path should not call dev proxy`, async () => { const ctx = createContextWithRouteParameters({ diff --git a/packages/core/src/middleware/koa-spa-proxy.ts b/packages/core/src/middleware/koa-spa-proxy.ts index c76b6c58d..269a886b0 100644 --- a/packages/core/src/middleware/koa-spa-proxy.ts +++ b/packages/core/src/middleware/koa-spa-proxy.ts @@ -39,10 +39,7 @@ export default function koaSpaProxy app !== prefix && requestPath.startsWith(`/${app}`)) - ) { + if (!prefix && mountedApps.some((app) => app !== prefix && requestPath.startsWith(`/${app}`))) { return next(); } diff --git a/packages/core/src/tenants/Tenant.ts b/packages/core/src/tenants/Tenant.ts index a6627618b..66ada4c64 100644 --- a/packages/core/src/tenants/Tenant.ts +++ b/packages/core/src/tenants/Tenant.ts @@ -88,8 +88,8 @@ export default class Tenant implements TenantContext { app.use(koaConsoleRedirectProxy(queries)); app.use( mount( - '/' + UserApps.Console, - koaSpaProxy(mountedApps, UserApps.Console, 5002, UserApps.Console) + '/' + AdminApps.Console, + koaSpaProxy(mountedApps, AdminApps.Console, 5002, AdminApps.Console) ) ); } diff --git a/packages/core/src/tenants/utils.ts b/packages/core/src/tenants/utils.ts index fe87c6321..d6057e020 100644 --- a/packages/core/src/tenants/utils.ts +++ b/packages/core/src/tenants/utils.ts @@ -57,7 +57,7 @@ export const checkRowLevelSecurity = async (client: QueryClient) => { `Found following table(s) without RLS: ${rlsDisabled .map((row) => conditionalString(isKeyInObject(row, 'tablename') && String(row.tablename))) .join(', ')}\n\n` + - 'Did you forget to run `npm cli db multi-tenancy enable`?' + 'Did you forget to run `npm cli db alteration deploy`?' ); } }; diff --git a/packages/schemas/alterations/next-1676190092-migrate-admin-data.ts b/packages/schemas/alterations/next-1676190092-migrate-admin-data.ts index 579d7d6ab..bc6fa101f 100644 --- a/packages/schemas/alterations/next-1676190092-migrate-admin-data.ts +++ b/packages/schemas/alterations/next-1676190092-migrate-admin-data.ts @@ -47,6 +47,12 @@ const defaultTenantId = 'default'; const alteration: AlterationScript = { up: async (pool) => { + // Init admin OIDC configs + await updateConfigByKey(pool, adminTenantId, 'oidc.privateKeys', [ + await generateOidcPrivateKey(), + ]); + await updateConfigByKey(pool, adminTenantId, 'oidc.cookieKeys', [generateOidcCookieKey()]); + // Skipped tables: // applications_roles, applications, connectors, custom_phrases, logto_configs, // passcodes, resources, roles_scopes, roles, scopes, sign_in_experiences, @@ -134,12 +140,6 @@ const alteration: AlterationScript = { sql`,` )}; `); - - // Init admin OIDC configs - await updateConfigByKey(pool, adminTenantId, 'oidc.privateKeys', [ - await generateOidcPrivateKey(), - ]); - await updateConfigByKey(pool, adminTenantId, 'oidc.cookieKeys', [generateOidcCookieKey()]); }, down: async (pool) => { const { rows } = await pool.query<{ id: string }>(sql`select id from tenants;`);