From ad4a268a6977af345be17f2fe4bc68579fbe99ce Mon Sep 17 00:00:00 2001 From: Rory Powell Date: Tue, 8 Mar 2022 08:01:41 +0000 Subject: [PATCH] Move licensing middleware to pro repo --- packages/backend-core/src/index.js | 4 +- .../backend-core/src/licensing/cache/index.js | 35 ---------------- .../backend-core/src/licensing/cache/redis.js | 25 ----------- packages/backend-core/src/licensing/index.js | 9 ---- .../src/licensing/middleware/index.js | 17 -------- .../backend-core/src/licensing/usage/index.js | 41 ------------------- packages/backend-core/src/utils.js | 11 +++-- packages/server/src/api/index.js | 4 +- packages/worker/src/api/index.js | 4 +- 9 files changed, 12 insertions(+), 138 deletions(-) delete mode 100644 packages/backend-core/src/licensing/cache/index.js delete mode 100644 packages/backend-core/src/licensing/cache/redis.js delete mode 100644 packages/backend-core/src/licensing/index.js delete mode 100644 packages/backend-core/src/licensing/middleware/index.js delete mode 100644 packages/backend-core/src/licensing/usage/index.js diff --git a/packages/backend-core/src/index.js b/packages/backend-core/src/index.js index 8c453caf07..3d3b120d1b 100644 --- a/packages/backend-core/src/index.js +++ b/packages/backend-core/src/index.js @@ -15,6 +15,8 @@ module.exports = { auth: require("../auth"), constants: require("../constants"), migrations: require("../migrations"), - licensing: require("./licensing"), errors: require("./errors"), + env: require("./environment"), + accounts: require("./cloud/accounts"), + tenancy: require("./tenancy"), } diff --git a/packages/backend-core/src/licensing/cache/index.js b/packages/backend-core/src/licensing/cache/index.js deleted file mode 100644 index b1a0215852..0000000000 --- a/packages/backend-core/src/licensing/cache/index.js +++ /dev/null @@ -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) -} diff --git a/packages/backend-core/src/licensing/cache/redis.js b/packages/backend-core/src/licensing/cache/redis.js deleted file mode 100644 index a5115d0b45..0000000000 --- a/packages/backend-core/src/licensing/cache/redis.js +++ /dev/null @@ -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 -} diff --git a/packages/backend-core/src/licensing/index.js b/packages/backend-core/src/licensing/index.js deleted file mode 100644 index 90a88741e7..0000000000 --- a/packages/backend-core/src/licensing/index.js +++ /dev/null @@ -1,9 +0,0 @@ -const middleware = require("./middleware") -const cache = require("./cache") -const usage = require("./usage") - -module.exports = { - middleware, - cache, - usage, -} diff --git a/packages/backend-core/src/licensing/middleware/index.js b/packages/backend-core/src/licensing/middleware/index.js deleted file mode 100644 index 6d5ba42175..0000000000 --- a/packages/backend-core/src/licensing/middleware/index.js +++ /dev/null @@ -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 diff --git a/packages/backend-core/src/licensing/usage/index.js b/packages/backend-core/src/licensing/usage/index.js deleted file mode 100644 index 58e5aadd7f..0000000000 --- a/packages/backend-core/src/licensing/usage/index.js +++ /dev/null @@ -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 - ) - } -} diff --git a/packages/backend-core/src/utils.js b/packages/backend-core/src/utils.js index 3adfe4ade0..41d6d0de4a 100644 --- a/packages/backend-core/src/utils.js +++ b/packages/backend-core/src/utils.js @@ -20,7 +20,6 @@ const { hash } = require("./hashing") const userCache = require("./cache/user") const env = require("./environment") const { getUserSessions, invalidateSessions } = require("./security/sessions") -const { usage } = require("./licensing") const APP_PREFIX = DocumentTypes.APP + SEPARATOR @@ -182,11 +181,11 @@ exports.saveUser = async ( hashPassword = true, requirePassword = true ) => { - // new user - // check license restrictions - if (!user._id && user.builder) { - await usage.checkMaxDevelopers() - } + // // new user + // // check license restrictions + // if (!user._id && user.builder) { + // await limits.checkMaxDevelopers() + // } if (!tenantId) { throw "No tenancy specified." diff --git a/packages/server/src/api/index.js b/packages/server/src/api/index.js index 4318a7ea95..84b728bb4e 100644 --- a/packages/server/src/api/index.js +++ b/packages/server/src/api/index.js @@ -11,7 +11,7 @@ const zlib = require("zlib") const { mainRoutes, staticRoutes } = require("./routes") const pkg = require("../../package.json") const env = require("../environment") -const { licensing } = require("@budibase/backend-core") +const { middleware: licensing } = require("@budibase/pro") const router = new Router() @@ -55,7 +55,7 @@ router .use(currentApp) // this middleware will try to use the app ID to determine the tenancy .use(buildAppTenancyMiddleware()) - .use(licensing.middleware()) + .use(licensing()) .use(auditLog) // error handling middleware diff --git a/packages/worker/src/api/index.js b/packages/worker/src/api/index.js index 1a0d7e7971..e5634e5ed5 100644 --- a/packages/worker/src/api/index.js +++ b/packages/worker/src/api/index.js @@ -8,7 +8,7 @@ const { buildTenancyMiddleware, buildCsrfMiddleware, } = require("@budibase/backend-core/auth") -const { licensing } = require("@budibase/backend-core") +const { middleware: licensing } = require("@budibase/pro") const { errors } = require("@budibase/backend-core") const PUBLIC_ENDPOINTS = [ @@ -93,7 +93,7 @@ router .use(buildAuthMiddleware(PUBLIC_ENDPOINTS)) .use(buildTenancyMiddleware(PUBLIC_ENDPOINTS, NO_TENANCY_ENDPOINTS)) .use(buildCsrfMiddleware({ noCsrfPatterns: NO_CSRF_ENDPOINTS })) - .use(licensing.middleware()) + .use(licensing()) // for now no public access is allowed to worker (bar health check) .use((ctx, next) => { if (ctx.publicEndpoint) {