Fixing issue with multi-tenancy and public apps, when the tenant isn't necessarily known, it can be found in the app ID, building a middleware to manage this.
This commit is contained in:
parent
ed2f4c78dc
commit
58aa32f1b0
|
@ -62,6 +62,23 @@ function getDocParams(docType, docId = null, otherProps = {}) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Given an app ID this will attempt to retrieve the tenant ID from it.
|
||||||
|
* @return {null|string} The tenant ID found within the app ID.
|
||||||
|
*/
|
||||||
|
exports.getTenantIDFromAppID = appId => {
|
||||||
|
const split = appId.split(SEPARATOR)
|
||||||
|
const hasDev = split[1] === DocumentTypes.DEV
|
||||||
|
if ((hasDev && split.length === 3) || (!hasDev && split.length === 2)) {
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
if (hasDev) {
|
||||||
|
return split[2]
|
||||||
|
} else {
|
||||||
|
return split[1]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Generates a new workspace ID.
|
* Generates a new workspace ID.
|
||||||
* @returns {string} The new workspace ID which the workspace doc can be stored under.
|
* @returns {string} The new workspace ID which the workspace doc can be stored under.
|
||||||
|
|
|
@ -11,6 +11,7 @@ const {
|
||||||
oidc,
|
oidc,
|
||||||
auditLog,
|
auditLog,
|
||||||
tenancy,
|
tenancy,
|
||||||
|
appTenancy,
|
||||||
} = require("./middleware")
|
} = require("./middleware")
|
||||||
const { setDB } = require("./db")
|
const { setDB } = require("./db")
|
||||||
const userCache = require("./cache/user")
|
const userCache = require("./cache/user")
|
||||||
|
@ -57,6 +58,7 @@ module.exports = {
|
||||||
oidc,
|
oidc,
|
||||||
jwt: require("jsonwebtoken"),
|
jwt: require("jsonwebtoken"),
|
||||||
buildTenancyMiddleware: tenancy,
|
buildTenancyMiddleware: tenancy,
|
||||||
|
buildAppTenancyMiddleware: appTenancy,
|
||||||
auditLog,
|
auditLog,
|
||||||
},
|
},
|
||||||
cache: {
|
cache: {
|
||||||
|
|
|
@ -0,0 +1,25 @@
|
||||||
|
const {
|
||||||
|
isMultiTenant,
|
||||||
|
updateTenantId,
|
||||||
|
isTenantIdSet,
|
||||||
|
DEFAULT_TENANT_ID,
|
||||||
|
} = require("../tenancy")
|
||||||
|
const ContextFactory = require("../tenancy/FunctionContext")
|
||||||
|
const { getTenantIDFromAppID } = require("../db/utils")
|
||||||
|
|
||||||
|
module.exports = () => {
|
||||||
|
return ContextFactory.getMiddleware(ctx => {
|
||||||
|
// if not in multi-tenancy mode make sure its default and exit
|
||||||
|
if (!isMultiTenant()) {
|
||||||
|
updateTenantId(DEFAULT_TENANT_ID)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
// if tenant ID already set no need to continue
|
||||||
|
if (isTenantIdSet()) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
const appId = ctx.appId ? ctx.appId : ctx.user ? ctx.user.appId : null
|
||||||
|
const tenantId = getTenantIDFromAppID(appId) || DEFAULT_TENANT_ID
|
||||||
|
updateTenantId(tenantId)
|
||||||
|
})
|
||||||
|
}
|
|
@ -5,6 +5,7 @@ const oidc = require("./passport/oidc")
|
||||||
const authenticated = require("./authenticated")
|
const authenticated = require("./authenticated")
|
||||||
const auditLog = require("./auditLog")
|
const auditLog = require("./auditLog")
|
||||||
const tenancy = require("./tenancy")
|
const tenancy = require("./tenancy")
|
||||||
|
const appTenancy = require("./appTenancy")
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
google,
|
google,
|
||||||
|
@ -14,4 +15,5 @@ module.exports = {
|
||||||
authenticated,
|
authenticated,
|
||||||
auditLog,
|
auditLog,
|
||||||
tenancy,
|
tenancy,
|
||||||
|
appTenancy,
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,7 +2,11 @@ const { setTenantId } = require("../tenancy")
|
||||||
const ContextFactory = require("../tenancy/FunctionContext")
|
const ContextFactory = require("../tenancy/FunctionContext")
|
||||||
const { buildMatcherRegex, matches } = require("./matchers")
|
const { buildMatcherRegex, matches } = require("./matchers")
|
||||||
|
|
||||||
module.exports = (allowQueryStringPatterns, noTenancyPatterns, opts = {}) => {
|
module.exports = (
|
||||||
|
allowQueryStringPatterns,
|
||||||
|
noTenancyPatterns,
|
||||||
|
opts = { noTenancyRequired: false }
|
||||||
|
) => {
|
||||||
const allowQsOptions = buildMatcherRegex(allowQueryStringPatterns)
|
const allowQsOptions = buildMatcherRegex(allowQueryStringPatterns)
|
||||||
const noTenancyOptions = buildMatcherRegex(noTenancyPatterns)
|
const noTenancyOptions = buildMatcherRegex(noTenancyPatterns)
|
||||||
|
|
||||||
|
|
|
@ -21,9 +21,7 @@ exports.doInTenant = (tenantId, task) => {
|
||||||
cls.setOnContext(TENANT_ID, tenantId)
|
cls.setOnContext(TENANT_ID, tenantId)
|
||||||
|
|
||||||
// invoke the task
|
// invoke the task
|
||||||
const result = task()
|
return task()
|
||||||
|
|
||||||
return result
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,10 @@
|
||||||
const Router = require("@koa/router")
|
const Router = require("@koa/router")
|
||||||
const { buildAuthMiddleware, auditLog, buildTenancyMiddleware } =
|
const {
|
||||||
require("@budibase/auth").auth
|
buildAuthMiddleware,
|
||||||
|
auditLog,
|
||||||
|
buildTenancyMiddleware,
|
||||||
|
buildAppTenancyMiddleware,
|
||||||
|
} = require("@budibase/auth").auth
|
||||||
const currentApp = require("../middleware/currentapp")
|
const currentApp = require("../middleware/currentapp")
|
||||||
const compress = require("koa-compress")
|
const compress = require("koa-compress")
|
||||||
const zlib = require("zlib")
|
const zlib = require("zlib")
|
||||||
|
@ -48,6 +52,8 @@ router
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
.use(currentApp)
|
.use(currentApp)
|
||||||
|
// this middleware will try to use the app ID to determine the tenancy
|
||||||
|
.use(buildAppTenancyMiddleware())
|
||||||
.use(auditLog)
|
.use(auditLog)
|
||||||
|
|
||||||
// error handling middleware
|
// error handling middleware
|
||||||
|
|
|
@ -19,6 +19,7 @@ const AppStatus = {
|
||||||
|
|
||||||
const DocumentTypes = {
|
const DocumentTypes = {
|
||||||
APP: CoreDocTypes.APP,
|
APP: CoreDocTypes.APP,
|
||||||
|
DEV: CoreDocTypes.DEV,
|
||||||
APP_DEV: CoreDocTypes.APP_DEV,
|
APP_DEV: CoreDocTypes.APP_DEV,
|
||||||
APP_METADATA: CoreDocTypes.APP_METADATA,
|
APP_METADATA: CoreDocTypes.APP_METADATA,
|
||||||
ROLE: CoreDocTypes.ROLE,
|
ROLE: CoreDocTypes.ROLE,
|
||||||
|
|
Loading…
Reference in New Issue