Move licensing middleware to pro repo

This commit is contained in:
Rory Powell 2022-03-08 08:01:41 +00:00
parent 22c8f79aef
commit 995f7c4b58
9 changed files with 12 additions and 138 deletions

View File

@ -15,6 +15,8 @@ module.exports = {
auth: require("../auth"), auth: require("../auth"),
constants: require("../constants"), constants: require("../constants"),
migrations: require("../migrations"), migrations: require("../migrations"),
licensing: require("./licensing"),
errors: require("./errors"), errors: require("./errors"),
env: require("./environment"),
accounts: require("./cloud/accounts"),
tenancy: require("./tenancy"),
} }

View File

@ -1,35 +0,0 @@
const redis = require("./redis")
const env = require("../../environment")
const accounts = require("../../cloud/accounts")
const EXPIRY_SECONDS = 3600
const populateLicense = async tenantId => {
if (env.SELF_HOSTED) {
// get license key
} else {
return accounts.getLicense(tenantId)
}
}
exports.getLicense = async (tenantId, opts = { populateLicense: null }) => {
// try cache
const client = await redis.getClient()
let license = await client.get(tenantId)
if (!license) {
const populate = opts.populateLicense
? opts.populateLicense
: populateLicense
license = await populate(tenantId)
if (license) {
client.store(tenantId, license, EXPIRY_SECONDS)
}
}
return license
}
exports.invalidate = async tenantId => {
const client = await redis.getClient()
await client.delete(tenantId)
}

View File

@ -1,25 +0,0 @@
const Redis = require("../../redis")
const utils = require("../../redis/utils")
let client
const init = async () => {
client = await new Redis(utils.Databases.LICENSES).init()
}
const shutdown = async () => {
if (client) {
await client.finish()
}
}
process.on("exit", async () => {
await shutdown()
})
exports.getClient = async () => {
if (!client) {
await init()
}
return client
}

View File

@ -1,9 +0,0 @@
const middleware = require("./middleware")
const cache = require("./cache")
const usage = require("./usage")
module.exports = {
middleware,
cache,
usage,
}

View File

@ -1,17 +0,0 @@
const cache = require("../cache")
const buildLicensingMiddleware = opts => {
return async (ctx, next) => {
if (ctx.user) {
const tenantId = ctx.user.tenantId
const license = await cache.getLicense(tenantId, opts)
if (license) {
ctx.user.license = license
}
}
return next()
}
}
module.exports = buildLicensingMiddleware

View File

@ -1,41 +0,0 @@
const { getTenantId } = require("../../context")
const cache = require("../cache")
const env = require("../../environment")
const utils = require("../../utils")
const { UsageLimitError } = require("../../errors")
const UsageType = {
MONTHLY: "monthly",
STATIC: "static",
}
const StaticUsageLimits = {
MAX_DEVELOPERS: "maxDevelopers",
QUERY_TIMEOUT_SECONDS: "queryTimeoutSeconds",
}
// eslint-disable-next-line no-unused-vars
const MonthlyUsageLimits = {
MAX_QUERY_COUNT: "maxQueryCount",
}
exports.checkMaxDevelopers = async () => {
const developerCount = await utils.getBuildersCount()
await checkUsageLimit(
developerCount,
UsageType.STATIC,
StaticUsageLimits.MAX_DEVELOPERS
)
}
const checkUsageLimit = async (currentUsage, usageType, usageLimit) => {
const tenantId = getTenantId()
const license = await cache.getLicense(tenantId)
const limit = license.usageLimits[usageType][usageLimit]
if (currentUsage >= limit.value) {
throw new UsageLimitError(
`Licensed ${limit.name} has been exceeded. To upgrade, visit ${env.ACCOUNT_PORTAL_URL}/portal/plans`,
limit.name
)
}
}

View File

@ -20,7 +20,6 @@ const { hash } = require("./hashing")
const userCache = require("./cache/user") const userCache = require("./cache/user")
const env = require("./environment") const env = require("./environment")
const { getUserSessions, invalidateSessions } = require("./security/sessions") const { getUserSessions, invalidateSessions } = require("./security/sessions")
const { usage } = require("./licensing")
const APP_PREFIX = DocumentTypes.APP + SEPARATOR const APP_PREFIX = DocumentTypes.APP + SEPARATOR
@ -182,11 +181,11 @@ exports.saveUser = async (
hashPassword = true, hashPassword = true,
requirePassword = true requirePassword = true
) => { ) => {
// new user // // new user
// check license restrictions // // check license restrictions
if (!user._id && user.builder) { // if (!user._id && user.builder) {
await usage.checkMaxDevelopers() // await limits.checkMaxDevelopers()
} // }
if (!tenantId) { if (!tenantId) {
throw "No tenancy specified." throw "No tenancy specified."

View File

@ -11,7 +11,7 @@ const zlib = require("zlib")
const { mainRoutes, staticRoutes } = require("./routes") const { mainRoutes, staticRoutes } = require("./routes")
const pkg = require("../../package.json") const pkg = require("../../package.json")
const env = require("../environment") const env = require("../environment")
const { licensing } = require("@budibase/backend-core") const { middleware: licensing } = require("@budibase/pro")
const router = new Router() const router = new Router()
@ -55,7 +55,7 @@ router
.use(currentApp) .use(currentApp)
// this middleware will try to use the app ID to determine the tenancy // this middleware will try to use the app ID to determine the tenancy
.use(buildAppTenancyMiddleware()) .use(buildAppTenancyMiddleware())
.use(licensing.middleware()) .use(licensing())
.use(auditLog) .use(auditLog)
// error handling middleware // error handling middleware

View File

@ -8,7 +8,7 @@ const {
buildTenancyMiddleware, buildTenancyMiddleware,
buildCsrfMiddleware, buildCsrfMiddleware,
} = require("@budibase/backend-core/auth") } = require("@budibase/backend-core/auth")
const { licensing } = require("@budibase/backend-core") const { middleware: licensing } = require("@budibase/pro")
const { errors } = require("@budibase/backend-core") const { errors } = require("@budibase/backend-core")
const PUBLIC_ENDPOINTS = [ const PUBLIC_ENDPOINTS = [
@ -93,7 +93,7 @@ router
.use(buildAuthMiddleware(PUBLIC_ENDPOINTS)) .use(buildAuthMiddleware(PUBLIC_ENDPOINTS))
.use(buildTenancyMiddleware(PUBLIC_ENDPOINTS, NO_TENANCY_ENDPOINTS)) .use(buildTenancyMiddleware(PUBLIC_ENDPOINTS, NO_TENANCY_ENDPOINTS))
.use(buildCsrfMiddleware({ noCsrfPatterns: NO_CSRF_ENDPOINTS })) .use(buildCsrfMiddleware({ noCsrfPatterns: NO_CSRF_ENDPOINTS }))
.use(licensing.middleware()) .use(licensing())
// for now no public access is allowed to worker (bar health check) // for now no public access is allowed to worker (bar health check)
.use((ctx, next) => { .use((ctx, next) => {
if (ctx.publicEndpoint) { if (ctx.publicEndpoint) {