Update getPlatformUrl usage to use a single interface

This commit is contained in:
Rory Powell 2022-05-17 09:52:38 +01:00
parent 0d9147be77
commit 99b83a4496
3 changed files with 192 additions and 68 deletions

View File

@ -1,61 +1,192 @@
require("../../tests/utilities/dbConfig");
const { const {
generateAppID, generateAppID,
getDevelopmentAppID, getDevelopmentAppID,
getProdAppID, getProdAppID,
isDevAppID, isDevAppID,
isProdAppID, isProdAppID,
getPlatformUrl,
getScopedConfig
} = require("../utils") } = require("../utils")
const tenancy = require("../../tenancy");
const { Configs, DEFAULT_TENANT_ID } = require("../../constants");
const env = require("../../environment")
function getID() { describe("utils", () => {
const appId = generateAppID() describe("app ID manipulation", () => {
const split = appId.split("_")
const uuid = split[split.length - 1] function getID() {
const devAppId = `app_dev_${uuid}` const appId = generateAppID()
return { appId, devAppId, split, uuid } const split = appId.split("_")
const uuid = split[split.length - 1]
const devAppId = `app_dev_${uuid}`
return { appId, devAppId, split, uuid }
}
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)
})
})
})
const DB_URL = "http://dburl.com"
const DEFAULT_URL = "http://localhost:10000"
const ENV_URL = "http://env.com"
const setDbPlatformUrl = async () => {
const db = tenancy.getGlobalDB()
db.put({
_id: "config_settings",
type: Configs.SETTINGS,
config: {
platformUrl: DB_URL
}
})
} }
describe("app ID manipulation", () => { const clearSettingsConfig = async () => {
it("should be able to generate a new app ID", () => { await tenancy.doInTenant(DEFAULT_TENANT_ID, async () => {
expect(generateAppID().startsWith("app_")).toEqual(true) const db = tenancy.getGlobalDB()
try {
const config = await db.get("config_settings")
await db.remove("config_settings", config._rev)
} catch (e) {
if (e.status !== 404) {
throw e
}
}
})
}
describe("getPlatformUrl", () => {
describe("self host", () => {
beforeEach(async () => {
env._set("SELF_HOST", 1)
await clearSettingsConfig()
})
it("gets the default url", async () => {
await tenancy.doInTenant(null, async () => {
const url = await getPlatformUrl()
expect(url).toBe(DEFAULT_URL)
})
})
it("gets the platform url from the environment", async () => {
await tenancy.doInTenant(null, async () => {
env._set("PLATFORM_URL", ENV_URL)
const url = await getPlatformUrl()
expect(url).toBe(ENV_URL)
})
})
it("gets the platform url from the database", async () => {
await tenancy.doInTenant(null, async () => {
await setDbPlatformUrl()
const url = await getPlatformUrl()
expect(url).toBe(DB_URL)
})
})
}) })
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", () => { describe("cloud", () => {
const { devAppId, uuid } = getID() const TENANT_AWARE_URL = "http://default.env.com"
expect(getDevelopmentAppID(devAppId)).toEqual(`app_dev_${uuid}`)
})
it("should be able to convert a development ID to a production", () => { beforeEach(async () => {
const { devAppId, uuid } = getID() env._set("SELF_HOSTED", 0)
expect(getProdAppID(devAppId)).toEqual(`app_${uuid}`) env._set("MULTI_TENANCY", 1)
}) env._set("PLATFORM_URL", ENV_URL)
await clearSettingsConfig()
})
it("should be able to convert a production ID to production", () => { it("gets the platform url from the environment without tenancy", async () => {
const { appId, uuid } = getID() await tenancy.doInTenant(DEFAULT_TENANT_ID, async () => {
expect(getProdAppID(appId)).toEqual(`app_${uuid}`) const url = await getPlatformUrl({ tenantAware: false })
}) expect(url).toBe(ENV_URL)
})
})
it("should be able to confirm dev app ID is development", () => { it("gets the platform url from the environment with tenancy", async () => {
const { devAppId } = getID() await tenancy.doInTenant(DEFAULT_TENANT_ID, async () => {
expect(isDevAppID(devAppId)).toEqual(true) const url = await getPlatformUrl()
}) expect(url).toBe(TENANT_AWARE_URL)
})
})
it("should be able to confirm prod app ID is not development", () => { it("never gets the platform url from the database", async () => {
const { appId } = getID() await tenancy.doInTenant(DEFAULT_TENANT_ID, async () => {
expect(isDevAppID(appId)).toEqual(false) await setDbPlatformUrl()
const url = await getPlatformUrl()
expect(url).toBe(TENANT_AWARE_URL)
})
})
}) })
})
it("should be able to confirm prod app ID is prod", () => { describe("getScopedConfig", () => {
const { appId } = getID() describe("settings config", () => {
expect(isProdAppID(appId)).toEqual(true)
})
it("should be able to confirm dev app ID is not prod", () => { beforeEach(async () => {
const { devAppId } = getID() await clearSettingsConfig()
expect(isProdAppID(devAppId)).toEqual(false) })
it("returns the platform url with an existing config", async () => {
await tenancy.doInTenant(DEFAULT_TENANT_ID, async () => {
await setDbPlatformUrl()
const db = tenancy.getGlobalDB()
const config = await getScopedConfig(db, { type: Configs.SETTINGS })
expect(config.platformUrl).toBe(DB_URL)
})
})
it("returns the platform url without an existing config", async () => {
await tenancy.doInTenant(DEFAULT_TENANT_ID, async () => {
const db = tenancy.getGlobalDB()
const config = await getScopedConfig(db, { type: Configs.SETTINGS })
expect(config.platformUrl).toBe(DEFAULT_URL)
})
})
}) })
}) })

View File

@ -9,7 +9,7 @@ const {
APP_PREFIX, APP_PREFIX,
APP_DEV, APP_DEV,
} = require("./constants") } = require("./constants")
const { getTenantId, getGlobalDBName } = require("../tenancy") const { getTenantId, getGlobalDBName, getGlobalDB } = require("../tenancy")
const fetch = require("node-fetch") const fetch = require("node-fetch")
const { doWithDB, allDbs } = require("./index") const { doWithDB, allDbs } = require("./index")
const { getCouchInfo } = require("./pouch") const { getCouchInfo } = require("./pouch")
@ -392,9 +392,7 @@ const getScopedFullConfig = async function (db, { type, user, workspace }) {
// always provide the platform URL // always provide the platform URL
if (type === Configs.SETTINGS) { if (type === Configs.SETTINGS) {
if (scopedConfig && scopedConfig.doc) { if (scopedConfig && scopedConfig.doc) {
scopedConfig.doc.config.platformUrl = await getPlatformUrl( scopedConfig.doc.config.platformUrl = await getPlatformUrl()
scopedConfig.doc.config
)
} else { } else {
scopedConfig = { scopedConfig = {
doc: { doc: {
@ -409,19 +407,30 @@ const getScopedFullConfig = async function (db, { type, user, workspace }) {
return scopedConfig && scopedConfig.doc return scopedConfig && scopedConfig.doc
} }
const getPlatformUrl = async settings => { const getPlatformUrl = async (opts = { tenantAware: true }) => {
let platformUrl = env.PLATFORM_URL || "http://localhost:10000" let platformUrl = env.PLATFORM_URL || "http://localhost:10000"
if (!env.SELF_HOSTED && env.MULTI_TENANCY) { if (!env.SELF_HOSTED && env.MULTI_TENANCY && opts.tenantAware) {
// cloud and multi tenant - add the tenant to the default platform url // cloud and multi tenant - add the tenant to the default platform url
const tenantId = getTenantId() const tenantId = getTenantId()
if (!platformUrl.includes("localhost:")) { if (!platformUrl.includes("localhost:")) {
platformUrl = platformUrl.replace("://", `://${tenantId}.`) platformUrl = platformUrl.replace("://", `://${tenantId}.`)
} }
} else { } else if (env.SELF_HOSTED) {
const db = getGlobalDB()
// get the doc directly instead of with getScopedConfig to prevent loop
let settings
try {
settings = await db.get(generateConfigID({ type: Configs.SETTINGS }))
} catch (e) {
if (e.status !== 404) {
throw e
}
}
// self hosted - check for platform url override // self hosted - check for platform url override
if (settings && settings.platformUrl) { if (settings && settings.config && settings.config.platformUrl) {
platformUrl = settings.platformUrl platformUrl = settings.config.platformUrl
} }
} }

View File

@ -1,7 +1,7 @@
const google = require("../google") const google = require("../google")
const { Cookies, Configs } = require("../../../constants") const { Cookies, Configs } = require("../../../constants")
const { clearCookie, getCookie } = require("../../../utils") const { clearCookie, getCookie } = require("../../../utils")
const { getScopedConfig } = require("../../../db/utils") const { getScopedConfig, getPlatformUrl } = require("../../../db/utils")
const { doWithDB } = require("../../../db") const { doWithDB } = require("../../../db")
const environment = require("../../../environment") const environment = require("../../../environment")
const { getGlobalDB } = require("../../../tenancy") const { getGlobalDB } = require("../../../tenancy")
@ -21,26 +21,10 @@ async function fetchGoogleCreds() {
) )
} }
async function getPlatformUrl() {
let platformUrl = environment.PLATFORM_URL || "http://localhost:10000"
const db = getGlobalDB()
const settings = await getScopedConfig(db, {
type: Configs.SETTINGS,
})
// self hosted - check for platform url override
if (settings && settings.platformUrl) {
platformUrl = settings.platformUrl
}
return platformUrl
}
async function preAuth(passport, ctx, next) { async function preAuth(passport, ctx, next) {
// get the relevant config // get the relevant config
const googleConfig = await fetchGoogleCreds() const googleConfig = await fetchGoogleCreds()
const platformUrl = await getPlatformUrl() const platformUrl = await getPlatformUrl({ tenantAware: false })
let callbackUrl = `${platformUrl}/api/global/auth/datasource/google/callback` let callbackUrl = `${platformUrl}/api/global/auth/datasource/google/callback`
const strategy = await google.strategyFactory(googleConfig, callbackUrl) const strategy = await google.strategyFactory(googleConfig, callbackUrl)
@ -59,7 +43,7 @@ async function preAuth(passport, ctx, next) {
async function postAuth(passport, ctx, next) { async function postAuth(passport, ctx, next) {
// get the relevant config // get the relevant config
const config = await fetchGoogleCreds() const config = await fetchGoogleCreds()
const platformUrl = await getPlatformUrl() const platformUrl = await getPlatformUrl({ tenantAware: false })
let callbackUrl = `${platformUrl}/api/global/auth/datasource/google/callback` let callbackUrl = `${platformUrl}/api/global/auth/datasource/google/callback`
const strategy = await google.strategyFactory( const strategy = await google.strategyFactory(