From 60841d1b1f66251a15adb6c347959b9a3a96fcf6 Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Mon, 15 Mar 2021 20:43:08 +0000 Subject: [PATCH] Removing unused code that was causing some issues with API key lookup and validation in self hosted setups. --- packages/server/src/app.js | 8 +--- packages/server/src/selfhost/README.md | 7 --- packages/server/src/selfhost/index.js | 44 ------------------- .../server/src/utilities/security/apikey.js | 10 ++--- 4 files changed, 5 insertions(+), 64 deletions(-) delete mode 100644 packages/server/src/selfhost/README.md delete mode 100644 packages/server/src/selfhost/index.js diff --git a/packages/server/src/app.js b/packages/server/src/app.js index 15e996cfe6..30cdfe5fdb 100644 --- a/packages/server/src/app.js +++ b/packages/server/src/app.js @@ -9,7 +9,6 @@ const env = require("./environment") const eventEmitter = require("./events") const automations = require("./automations/index") const Sentry = require("@sentry/node") -const selfhost = require("./selfhost") const app = new Koa() @@ -66,11 +65,8 @@ module.exports = server.listen(env.PORT || 0, async () => { console.log(`Budibase running on ${JSON.stringify(server.address())}`) env._set("PORT", server.address().port) eventEmitter.emitPort(env.PORT) - automations.init() - // only init the self hosting DB info in the Pouch, not needed in self hosting prod - if (!env.CLOUD) { - await selfhost.init() - } + + await automations.init() }) process.on("uncaughtException", err => { diff --git a/packages/server/src/selfhost/README.md b/packages/server/src/selfhost/README.md deleted file mode 100644 index a02743a58c..0000000000 --- a/packages/server/src/selfhost/README.md +++ /dev/null @@ -1,7 +0,0 @@ -### Self hosting -This directory contains utilities that are needed for self hosted platforms to operate. -These will mostly be utilities, necessary to the operation of the server e.g. storing self -hosting specific options and attributes to CouchDB. - -All the internal operations should be exposed through the `index.js` so importing -the self host directory should give you everything you need. \ No newline at end of file diff --git a/packages/server/src/selfhost/index.js b/packages/server/src/selfhost/index.js deleted file mode 100644 index f77d1f0b6c..0000000000 --- a/packages/server/src/selfhost/index.js +++ /dev/null @@ -1,44 +0,0 @@ -const CouchDB = require("../db") -const env = require("../environment") -const newid = require("../db/newid") - -const SELF_HOST_DB = "self-host-db" -const SELF_HOST_DOC = "self-host-info" - -async function createSelfHostDB(db) { - await db.put({ - _id: "_design/database", - views: {}, - }) - const selfHostInfo = { - _id: SELF_HOST_DOC, - apiKeyId: newid(), - } - await db.put(selfHostInfo) - return selfHostInfo -} - -exports.init = async () => { - if (!env.SELF_HOSTED) { - return - } - const db = new CouchDB(SELF_HOST_DB) - try { - await db.get(SELF_HOST_DOC) - } catch (err) { - // failed to retrieve - if (err.status === 404) { - await createSelfHostDB(db) - } - } -} - -exports.getSelfHostInfo = async () => { - const db = new CouchDB(SELF_HOST_DB) - return db.get(SELF_HOST_DOC) -} - -exports.getSelfHostAPIKey = async () => { - const info = await exports.getSelfHostInfo() - return info ? info.apiKeyId : null -} diff --git a/packages/server/src/utilities/security/apikey.js b/packages/server/src/utilities/security/apikey.js index c8965cee43..3d5f428bb7 100644 --- a/packages/server/src/utilities/security/apikey.js +++ b/packages/server/src/utilities/security/apikey.js @@ -1,6 +1,5 @@ const { apiKeyTable } = require("../../db/dynamoClient") const env = require("../../environment") -const { getSelfHostAPIKey } = require("../../selfhost") /** * This file purely exists so that we can centralise all logic pertaining to API keys, as their usage differs @@ -8,16 +7,13 @@ const { getSelfHostAPIKey } = require("../../selfhost") */ exports.isAPIKeyValid = async apiKeyId => { - if (env.CLOUD && !env.SELF_HOSTED) { + if (!env.SELF_HOSTED) { let apiKeyInfo = await apiKeyTable.get({ primary: apiKeyId, }) return apiKeyInfo != null - } - if (env.SELF_HOSTED) { - const selfHostKey = await getSelfHostAPIKey() + } else { // if the api key supplied is correct then return structure similar - return apiKeyId === selfHostKey ? { pk: apiKeyId } : null + return apiKeyId === env.HOSTING_KEY } - return false }