From 7b461c03dd2c62879d44ff2408454db74602237c Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Mon, 9 May 2022 12:29:44 +0100 Subject: [PATCH 1/2] Fixing issue #5778 - making sure to only remove the first app_ element of the app ID. --- packages/backend-core/src/db/conversions.js | 30 ++++++++++----------- packages/backend-core/src/db/utils.js | 12 +++++++++ packages/server/package.json | 2 +- packages/server/src/db/utils.js | 26 +++--------------- packages/server/yarn.lock | 18 ++++++------- packages/worker/package.json | 2 +- packages/worker/yarn.lock | 18 ++++++------- 7 files changed, 51 insertions(+), 57 deletions(-) diff --git a/packages/backend-core/src/db/conversions.js b/packages/backend-core/src/db/conversions.js index 50d896322f..b820a643e8 100644 --- a/packages/backend-core/src/db/conversions.js +++ b/packages/backend-core/src/db/conversions.js @@ -23,24 +23,24 @@ exports.isDevApp = app => { } /** - * Convert a development app ID to a deployed app ID. + * Generates a development app ID from a real app ID. + * @returns {string} the dev app ID which can be used for dev database. */ -exports.getProdAppID = appId => { - // if dev, convert it - if (appId.startsWith(APP_DEV_PREFIX)) { - const id = appId.split(APP_DEV_PREFIX)[1] - return `${APP_PREFIX}${id}` - } - return appId +exports.getDevelopmentAppID = appId => { + // split to take off the app_ element, then join it together incase any other app_ exist + const split = appId.split(APP_PREFIX) + split.shift() + const rest = split.join(APP_PREFIX) + return `${APP_DEV_PREFIX}${rest}` } /** - * Convert a deployed app ID to a development app ID. + * Convert a development app ID to a deployed app ID. */ -exports.getDevelopmentAppID = appId => { - if (!appId.startsWith(APP_DEV_PREFIX)) { - const id = appId.split(APP_PREFIX)[1] - return `${APP_DEV_PREFIX}${id}` - } - return appId +exports.getProdAppID = appId => { + // split to take off the app_dev element, then join it together incase any other app_ exist + const split = appId.split(APP_DEV_PREFIX) + split.shift() + const rest = split.join(APP_DEV_PREFIX) + return `${APP_PREFIX}${rest}` } diff --git a/packages/backend-core/src/db/utils.js b/packages/backend-core/src/db/utils.js index 1e2ba846de..5f7bf794c2 100644 --- a/packages/backend-core/src/db/utils.js +++ b/packages/backend-core/src/db/utils.js @@ -43,6 +43,18 @@ exports.isDevAppID = isDevAppID exports.getDevelopmentAppID = getDevelopmentAppID exports.getProdAppID = getProdAppID +/** + * Generates a new app ID. + * @returns {string} The new app ID which the app doc can be stored under. + */ +exports.generateAppID = (tenantId = null) => { + let id = APP_PREFIX + if (tenantId) { + id += `${tenantId}${SEPARATOR}` + } + return `${id}${newid()}` +} + /** * If creating DB allDocs/query params with only a single top level ID this can be used, this * is usually the case as most of our docs are top level e.g. tables, automations, users and so on. diff --git a/packages/server/package.json b/packages/server/package.json index f4b0be65f4..efb1547d31 100644 --- a/packages/server/package.json +++ b/packages/server/package.json @@ -70,7 +70,7 @@ "@apidevtools/swagger-parser": "^10.0.3", "@budibase/backend-core": "^1.0.146", "@budibase/client": "^1.0.146", - "@budibase/pro": "1.0.146", + "@budibase/pro": "^1.0.145", "@budibase/string-templates": "^1.0.146", "@bull-board/api": "^3.7.0", "@bull-board/koa": "^3.7.0", diff --git a/packages/server/src/db/utils.js b/packages/server/src/db/utils.js index e40773bd0e..280596928b 100644 --- a/packages/server/src/db/utils.js +++ b/packages/server/src/db/utils.js @@ -9,6 +9,8 @@ const { StaticDatabases, isDevAppID, isProdAppID, + getDevelopmentAppID, + generateAppID, } = require("@budibase/backend-core/db") const UNICODE_MAX = "\ufff0" @@ -80,6 +82,8 @@ exports.UNICODE_MAX = UNICODE_MAX exports.SearchIndexes = SearchIndexes exports.AppStatus = AppStatus exports.BudibaseInternalDB = BudibaseInternalDB +exports.generateAppID = generateAppID +exports.generateDevAppID = getDevelopmentAppID exports.generateRoleID = generateRoleID exports.getRoleParams = getRoleParams @@ -243,28 +247,6 @@ exports.getLinkParams = (otherProps = {}) => { return getDocParams(DocumentTypes.LINK, null, otherProps) } -/** - * Generates a new app ID. - * @returns {string} The new app ID which the app doc can be stored under. - */ -exports.generateAppID = (tenantId = null) => { - let id = `${DocumentTypes.APP}${SEPARATOR}` - if (tenantId) { - id += `${tenantId}${SEPARATOR}` - } - return `${id}${newid()}` -} - -/** - * Generates a development app ID from a real app ID. - * @returns {string} the dev app ID which can be used for dev database. - */ -exports.generateDevAppID = appId => { - const prefix = `${DocumentTypes.APP}${SEPARATOR}` - const rest = appId.split(prefix)[1] - return `${DocumentTypes.APP_DEV}${SEPARATOR}${rest}` -} - /** * Generates a new layout ID. * @returns {string} The new layout ID which the layout doc can be stored under. diff --git a/packages/server/yarn.lock b/packages/server/yarn.lock index 0ee3d3bc78..84ffebfd3d 100644 --- a/packages/server/yarn.lock +++ b/packages/server/yarn.lock @@ -1014,10 +1014,10 @@ resolved "https://registry.yarnpkg.com/@bcoe/v8-coverage/-/v8-coverage-0.2.3.tgz#75a2e8b51cb758a7553d6804a5932d7aace75c39" integrity sha512-0hYQ8SB4Db5zvZB4axdMHGwEaQjkZzFjQiN9LVYvIFB2nSUHW9tYpxWriPrWDASIxiaXax83REcLxuSdnGPZtw== -"@budibase/backend-core@1.0.142": - version "1.0.142" - resolved "https://registry.yarnpkg.com/@budibase/backend-core/-/backend-core-1.0.142.tgz#cbfa426dbf064dff0ea5f9840baae595c0f5343c" - integrity sha512-gd52tOm4mytHa3OQQVcEiwnVCvq0pC0q3YN+5D1uoGt9WaAZoshB5AhXnUPZohvpo5j8isO07qXvF+5YffPVaw== +"@budibase/backend-core@1.0.145": + version "1.0.145" + resolved "https://registry.yarnpkg.com/@budibase/backend-core/-/backend-core-1.0.145.tgz#4b9deb9a7808f45e4239c4aae6193020533e2de9" + integrity sha512-UCQI9aQHRNskMH9AWqjjUfyPHOoO8fCW1iPjhOaijiFQ/8nr8TbfshQ5NKFXSCBdCUWIpuGO9664IeoL8LzWDA== dependencies: "@techpass/passport-openidconnect" "^0.3.0" aws-sdk "^2.901.0" @@ -1091,12 +1091,12 @@ svelte-flatpickr "^3.2.3" svelte-portal "^1.0.0" -"@budibase/pro@1.0.142": - version "1.0.142" - resolved "https://registry.yarnpkg.com/@budibase/pro/-/pro-1.0.142.tgz#17cd53d67ce80e0cce2179caee0c03e6a0d7084e" - integrity sha512-c1SaRoVik/AVn4zNNsDciFeIr+P3J6Sc8hsE5M3pb0Zj/NzSEHXTesxcBVLlD2PYlwnvWPSoMvdDJKrEFHvOYQ== +"@budibase/pro@^1.0.145": + version "1.0.145" + resolved "https://registry.yarnpkg.com/@budibase/pro/-/pro-1.0.145.tgz#447a26d21ca94a14bfaa9d9cef5b9a7b7a6e7c17" + integrity sha512-j5/EGNsMoxy6YxYr5/JLQ+hSehKQrSYrmSPBBzQjQnMMeFNMqHLNlYDsrayXxkEt0ix4WGJxFKTZUR6cLO3nYA== dependencies: - "@budibase/backend-core" "1.0.142" + "@budibase/backend-core" "1.0.145" node-fetch "^2.6.1" "@budibase/standard-components@^0.9.139": diff --git a/packages/worker/package.json b/packages/worker/package.json index 3216dcd952..672dc7769a 100644 --- a/packages/worker/package.json +++ b/packages/worker/package.json @@ -32,7 +32,7 @@ "license": "GPL-3.0", "dependencies": { "@budibase/backend-core": "^1.0.146", - "@budibase/pro": "1.0.146", + "@budibase/pro": "^1.0.145", "@budibase/string-templates": "^1.0.146", "@koa/router": "^8.0.0", "@sentry/node": "6.17.7", diff --git a/packages/worker/yarn.lock b/packages/worker/yarn.lock index c8a3b088bd..5f96a8265c 100644 --- a/packages/worker/yarn.lock +++ b/packages/worker/yarn.lock @@ -293,10 +293,10 @@ resolved "https://registry.yarnpkg.com/@bcoe/v8-coverage/-/v8-coverage-0.2.3.tgz#75a2e8b51cb758a7553d6804a5932d7aace75c39" integrity sha512-0hYQ8SB4Db5zvZB4axdMHGwEaQjkZzFjQiN9LVYvIFB2nSUHW9tYpxWriPrWDASIxiaXax83REcLxuSdnGPZtw== -"@budibase/backend-core@1.0.142": - version "1.0.142" - resolved "https://registry.yarnpkg.com/@budibase/backend-core/-/backend-core-1.0.142.tgz#cbfa426dbf064dff0ea5f9840baae595c0f5343c" - integrity sha512-gd52tOm4mytHa3OQQVcEiwnVCvq0pC0q3YN+5D1uoGt9WaAZoshB5AhXnUPZohvpo5j8isO07qXvF+5YffPVaw== +"@budibase/backend-core@1.0.145": + version "1.0.145" + resolved "https://registry.yarnpkg.com/@budibase/backend-core/-/backend-core-1.0.145.tgz#4b9deb9a7808f45e4239c4aae6193020533e2de9" + integrity sha512-UCQI9aQHRNskMH9AWqjjUfyPHOoO8fCW1iPjhOaijiFQ/8nr8TbfshQ5NKFXSCBdCUWIpuGO9664IeoL8LzWDA== dependencies: "@techpass/passport-openidconnect" "^0.3.0" aws-sdk "^2.901.0" @@ -321,12 +321,12 @@ uuid "^8.3.2" zlib "^1.0.5" -"@budibase/pro@1.0.142": - version "1.0.142" - resolved "https://registry.yarnpkg.com/@budibase/pro/-/pro-1.0.142.tgz#17cd53d67ce80e0cce2179caee0c03e6a0d7084e" - integrity sha512-c1SaRoVik/AVn4zNNsDciFeIr+P3J6Sc8hsE5M3pb0Zj/NzSEHXTesxcBVLlD2PYlwnvWPSoMvdDJKrEFHvOYQ== +"@budibase/pro@^1.0.145": + version "1.0.145" + resolved "https://registry.yarnpkg.com/@budibase/pro/-/pro-1.0.145.tgz#447a26d21ca94a14bfaa9d9cef5b9a7b7a6e7c17" + integrity sha512-j5/EGNsMoxy6YxYr5/JLQ+hSehKQrSYrmSPBBzQjQnMMeFNMqHLNlYDsrayXxkEt0ix4WGJxFKTZUR6cLO3nYA== dependencies: - "@budibase/backend-core" "1.0.142" + "@budibase/backend-core" "1.0.145" node-fetch "^2.6.1" "@cspotcode/source-map-consumer@0.8.0": From 2abfeb93563c23d6dcf564dd111b307617a971d1 Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Mon, 9 May 2022 13:28:01 +0100 Subject: [PATCH 2/2] Unit tests for the utility functions that have changed around app ID checks/generation./ --- packages/backend-core/src/db/conversions.js | 6 ++ .../backend-core/src/db/tests/utils.spec.js | 61 +++++++++++++++++++ 2 files changed, 67 insertions(+) create mode 100644 packages/backend-core/src/db/tests/utils.spec.js diff --git a/packages/backend-core/src/db/conversions.js b/packages/backend-core/src/db/conversions.js index b820a643e8..455cc712d8 100644 --- a/packages/backend-core/src/db/conversions.js +++ b/packages/backend-core/src/db/conversions.js @@ -27,6 +27,9 @@ exports.isDevApp = app => { * @returns {string} the dev app ID which can be used for dev database. */ exports.getDevelopmentAppID = appId => { + if (!appId || appId.startsWith(APP_DEV_PREFIX)) { + return appId + } // split to take off the app_ element, then join it together incase any other app_ exist const split = appId.split(APP_PREFIX) split.shift() @@ -38,6 +41,9 @@ exports.getDevelopmentAppID = appId => { * Convert a development app ID to a deployed app ID. */ exports.getProdAppID = appId => { + if (!appId || !appId.startsWith(APP_DEV_PREFIX)) { + return appId + } // split to take off the app_dev element, then join it together incase any other app_ exist const split = appId.split(APP_DEV_PREFIX) split.shift() diff --git a/packages/backend-core/src/db/tests/utils.spec.js b/packages/backend-core/src/db/tests/utils.spec.js new file mode 100644 index 0000000000..ebef670a81 --- /dev/null +++ b/packages/backend-core/src/db/tests/utils.spec.js @@ -0,0 +1,61 @@ +const { + generateAppID, + getDevelopmentAppID, + getProdAppID, + isDevAppID, + isProdAppID, +} = require("../utils") + +function getID() { + const appId = generateAppID() + const split = appId.split("_") + const uuid = split[split.length - 1] + const devAppId = `app_dev_${uuid}` + return { appId, devAppId, split, uuid } +} + +describe("app ID manipulation", () => { + it("should be able to generate a new app ID", () => { + expect(generateAppID().startsWith("app_")).toEqual(true) + }) + + it("should be able to convert a production app ID to development", () => { + const { appId, uuid } = getID() + expect(getDevelopmentAppID(appId)).toEqual(`app_dev_${uuid}`) + }) + + it("should be able to convert a development app ID to development", () => { + const { devAppId, uuid } = getID() + expect(getDevelopmentAppID(devAppId)).toEqual(`app_dev_${uuid}`) + }) + + it("should be able to convert a development ID to a production", () => { + const { devAppId, uuid } = getID() + expect(getProdAppID(devAppId)).toEqual(`app_${uuid}`) + }) + + it("should be able to convert a production ID to production", () => { + const { appId, uuid } = getID() + expect(getProdAppID(appId)).toEqual(`app_${uuid}`) + }) + + it("should be able to confirm dev app ID is development", () => { + const { devAppId } = getID() + expect(isDevAppID(devAppId)).toEqual(true) + }) + + it("should be able to confirm prod app ID is not development", () => { + const { appId } = getID() + expect(isDevAppID(appId)).toEqual(false) + }) + + it("should be able to confirm prod app ID is prod", () => { + const { appId } = getID() + expect(isProdAppID(appId)).toEqual(true) + }) + + it("should be able to confirm dev app ID is not prod", () => { + const { devAppId } = getID() + expect(isProdAppID(devAppId)).toEqual(false) + }) +}) \ No newline at end of file