From 2bc137baa552b27b993f3049f8e539e9ab7a5ff4 Mon Sep 17 00:00:00 2001 From: Pranav C Date: Wed, 24 May 2023 09:46:40 +0530 Subject: [PATCH 1/4] fix: add updgrader to migrate any project base config created with version 0.107.0 Signed-off-by: Pranav C --- .../nocodb/src/version-upgrader/NcUpgrader.ts | 2 + .../ncProjectConfigUpgrader.ts | 47 +++++++++++++++++++ 2 files changed, 49 insertions(+) create mode 100644 packages/nocodb/src/version-upgrader/ncProjectConfigUpgrader.ts diff --git a/packages/nocodb/src/version-upgrader/NcUpgrader.ts b/packages/nocodb/src/version-upgrader/NcUpgrader.ts index c2d351dc2f..951a93099f 100644 --- a/packages/nocodb/src/version-upgrader/NcUpgrader.ts +++ b/packages/nocodb/src/version-upgrader/NcUpgrader.ts @@ -13,6 +13,7 @@ import ncProjectUpgraderV2_0090000 from './ncProjectUpgraderV2_0090000'; import ncProjectEnvUpgrader0011045 from './ncProjectEnvUpgrader0011045'; import ncProjectEnvUpgrader from './ncProjectEnvUpgrader'; import ncHookUpgrader from './ncHookUpgrader'; +import ncProjectConfigUpgrader from './ncProjectConfigUpgrader'; import type { MetaService } from '../meta/meta.service'; import type { NcConfig } from '../interface/config'; @@ -48,6 +49,7 @@ export default class NcUpgrader { { name: '0105002', handler: ncStickyColumnUpgrader }, { name: '0105003', handler: ncFilterUpgrader_0105003 }, { name: '0105004', handler: ncHookUpgrader }, + { name: '0107004', handler: ncProjectConfigUpgrader }, ]; if (!(await ctx.ncMeta.knexConnection?.schema?.hasTable?.('nc_store'))) { return; diff --git a/packages/nocodb/src/version-upgrader/ncProjectConfigUpgrader.ts b/packages/nocodb/src/version-upgrader/ncProjectConfigUpgrader.ts new file mode 100644 index 0000000000..40c3d0a157 --- /dev/null +++ b/packages/nocodb/src/version-upgrader/ncProjectConfigUpgrader.ts @@ -0,0 +1,47 @@ +import CryptoJS from 'crypto-js'; +import { Base } from '../models'; +import { MetaTable } from '../utils/globals'; +import type { NcUpgraderCtx } from './NcUpgrader'; + +const TEMP_KEY = 'temporary-key'; + +// In version 0.107.0 we were used a temporary fallback secret key for JWT token encryption and project base config encryption. +// So any project created in version 0.107.0 won't be able to decrypt the project base config. +// So we need to update the project base config with the new secret key. +// Get all the project bases and update the project config with the new secret key. +export default async function ({ ncMeta }: NcUpgraderCtx) { + const actions = []; + + // Get all the project bases + const bases = await ncMeta.metaList2(null, null, MetaTable.BASES); + + // Update the base config with the new secret key if we could decrypt the base config with the fallback secret key + for (const base of bases) { + let config; + + // Try to decrypt the base config with the fallback secret key + // if we could decrypt the base config with the fallback secret key then we will update the base config with the new secret key + // otherwise we will skip the base config update since it is already encrypted with the new secret key + try { + config = JSON.parse( + CryptoJS.AES.decrypt(base.config, TEMP_KEY).toString(CryptoJS.enc.Utf8), + ); + } catch (e) { + continue; + } + + if (!config) { + continue; + } + + // Update the base config with the new secret key + actions.push( + Base.updateBase(base.id, { + id: base.id, + projectId: base.project_id, + config: base.config, + }), + ); + } + await Promise.all(actions); +} From 0eccc15074ce2ff6ea8b56550926ba26c8164985 Mon Sep 17 00:00:00 2001 From: Pranav C Date: Wed, 24 May 2023 10:13:37 +0530 Subject: [PATCH 2/4] fix: use decrypted config for update, update upgrader version, pass ncMeta reference Signed-off-by: Pranav C --- packages/nocodb/src/services/app-init.service.ts | 2 +- .../version-upgrader/ncProjectConfigUpgrader.ts | 14 +++++++++----- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/packages/nocodb/src/services/app-init.service.ts b/packages/nocodb/src/services/app-init.service.ts index b305425347..f08263fb38 100644 --- a/packages/nocodb/src/services/app-init.service.ts +++ b/packages/nocodb/src/services/app-init.service.ts @@ -35,7 +35,7 @@ export const appInitServiceProvider: Provider = { metaService: MetaService, eventEmitter: IEventEmitter, ) => { - process.env.NC_VERSION = '0105004'; + process.env.NC_VERSION = '0107004'; await NocoCache.init(); diff --git a/packages/nocodb/src/version-upgrader/ncProjectConfigUpgrader.ts b/packages/nocodb/src/version-upgrader/ncProjectConfigUpgrader.ts index 40c3d0a157..875672a272 100644 --- a/packages/nocodb/src/version-upgrader/ncProjectConfigUpgrader.ts +++ b/packages/nocodb/src/version-upgrader/ncProjectConfigUpgrader.ts @@ -36,11 +36,15 @@ export default async function ({ ncMeta }: NcUpgraderCtx) { // Update the base config with the new secret key actions.push( - Base.updateBase(base.id, { - id: base.id, - projectId: base.project_id, - config: base.config, - }), + Base.updateBase( + base.id, + { + id: base.id, + projectId: base.project_id, + config, + }, + ncMeta, + ), ); } await Promise.all(actions); From 49c2b8d4242f79a472688e06ba2320dd1048988e Mon Sep 17 00:00:00 2001 From: Pranav C Date: Wed, 24 May 2023 12:55:30 +0530 Subject: [PATCH 3/4] fix: provide an option to skip base reordering while updating Signed-off-by: Pranav C --- packages/nocodb/src/models/Base.ts | 4 +++- .../nocodb/src/version-upgrader/ncProjectConfigUpgrader.ts | 1 + 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/nocodb/src/models/Base.ts b/packages/nocodb/src/models/Base.ts index 2846b3b7b4..92f04e4d18 100644 --- a/packages/nocodb/src/models/Base.ts +++ b/packages/nocodb/src/models/Base.ts @@ -91,6 +91,7 @@ export default class Base implements BaseType { base: BaseType & { id: string; projectId: string; + skipReorder?: boolean; }, ncMeta = Noco.ncMeta, ) { @@ -144,7 +145,8 @@ export default class Base implements BaseType { // call before reorder to update cache const returnBase = await this.get(oldBase.id, ncMeta); - await this.reorderBases(base.projectId, returnBase.id, ncMeta); + if (!base.skipReorder) + await this.reorderBases(base.projectId, returnBase.id, ncMeta); return returnBase; } diff --git a/packages/nocodb/src/version-upgrader/ncProjectConfigUpgrader.ts b/packages/nocodb/src/version-upgrader/ncProjectConfigUpgrader.ts index 875672a272..c4c3855018 100644 --- a/packages/nocodb/src/version-upgrader/ncProjectConfigUpgrader.ts +++ b/packages/nocodb/src/version-upgrader/ncProjectConfigUpgrader.ts @@ -42,6 +42,7 @@ export default async function ({ ncMeta }: NcUpgraderCtx) { id: base.id, projectId: base.project_id, config, + skipReorder: true, }, ncMeta, ), From 305093e29d03d84423c4964c183b5b1ba15f9c64 Mon Sep 17 00:00:00 2001 From: Pranav C Date: Wed, 24 May 2023 13:14:56 +0530 Subject: [PATCH 4/4] fix: update meta config as well, which will be `null` Signed-off-by: Pranav C --- .../ncProjectConfigUpgrader.ts | 34 ++++++++----------- 1 file changed, 15 insertions(+), 19 deletions(-) diff --git a/packages/nocodb/src/version-upgrader/ncProjectConfigUpgrader.ts b/packages/nocodb/src/version-upgrader/ncProjectConfigUpgrader.ts index c4c3855018..35a7a759a2 100644 --- a/packages/nocodb/src/version-upgrader/ncProjectConfigUpgrader.ts +++ b/packages/nocodb/src/version-upgrader/ncProjectConfigUpgrader.ts @@ -26,27 +26,23 @@ export default async function ({ ncMeta }: NcUpgraderCtx) { config = JSON.parse( CryptoJS.AES.decrypt(base.config, TEMP_KEY).toString(CryptoJS.enc.Utf8), ); - } catch (e) { - continue; - } - if (!config) { - continue; + // Update the base config with the new secret key + actions.push( + Base.updateBase( + base.id, + { + id: base.id, + projectId: base.project_id, + config, + skipReorder: true, + }, + ncMeta, + ), + ); + } catch (e) { + // ignore the error } - - // Update the base config with the new secret key - actions.push( - Base.updateBase( - base.id, - { - id: base.id, - projectId: base.project_id, - config, - skipReorder: true, - }, - ncMeta, - ), - ); } await Promise.all(actions); }