From af4c1b23de5a244f93c50a53311351d021e16488 Mon Sep 17 00:00:00 2001 From: Martin McKeaveney Date: Wed, 18 Aug 2021 11:23:28 +0100 Subject: [PATCH 1/2] simplify couchDB configuration --- packages/auth/src/db/utils.js | 6 ++++++ packages/auth/src/environment.js | 2 +- packages/server/src/db/client.js | 10 ++-------- packages/server/src/environment.js | 2 -- packages/worker/src/db/index.js | 10 ++-------- 5 files changed, 11 insertions(+), 19 deletions(-) diff --git a/packages/auth/src/db/utils.js b/packages/auth/src/db/utils.js index a7476b8e9c..dfefd54f97 100644 --- a/packages/auth/src/db/utils.js +++ b/packages/auth/src/db/utils.js @@ -166,6 +166,12 @@ exports.getCouchUrl = () => { const [protocol, ...rest] = env.COUCH_DB_URL.split("://") + if (!env.COUCH_DB_USERNAME || !env.COUCH_DB_PASSWORD) { + throw new Error( + "CouchDB configuration invalid. You must provide a fully qualified CouchDB url, or the COUCH_DB_USER and COUCH_DB_PASSWORD environment variables." + ) + } + return `${protocol}://${env.COUCH_DB_USERNAME}:${env.COUCH_DB_PASSWORD}@${rest}` } diff --git a/packages/auth/src/environment.js b/packages/auth/src/environment.js index 6afcb61a1a..4d1453837c 100644 --- a/packages/auth/src/environment.js +++ b/packages/auth/src/environment.js @@ -9,7 +9,7 @@ function isTest() { module.exports = { JWT_SECRET: process.env.JWT_SECRET, COUCH_DB_URL: process.env.COUCH_DB_URL, - COUCH_DB_USERNAME: process.env.COUCH_DB_USER || process.env.COUCH_DB_USERNAME, + COUCH_DB_USERNAME: process.env.COUCH_DB_USER, COUCH_DB_PASSWORD: process.env.COUCH_DB_PASSWORD, SALT_ROUNDS: process.env.SALT_ROUNDS, REDIS_URL: process.env.REDIS_URL, diff --git a/packages/server/src/db/client.js b/packages/server/src/db/client.js index dde857c462..a453ad8561 100644 --- a/packages/server/src/db/client.js +++ b/packages/server/src/db/client.js @@ -1,10 +1,11 @@ const PouchDB = require("pouchdb") +const { getCouchUrl } = require("@budibase/auth/db") const replicationStream = require("pouchdb-replication-stream") const allDbs = require("pouchdb-all-dbs") const find = require("pouchdb-find") const env = require("../environment") -const COUCH_DB_URL = env.COUCH_DB_URL || "http://localhost:10000/db/" +const COUCH_DB_URL = getCouchUrl() || "http://localhost:10000/db/" PouchDB.plugin(replicationStream.plugin) PouchDB.plugin(find) @@ -14,13 +15,6 @@ let POUCH_DB_DEFAULTS = { prefix: COUCH_DB_URL, } -if (env.COUCH_DB_USERNAME && env.COUCH_DB_PASSWORD) { - POUCH_DB_DEFAULTS.auth = { - username: env.COUCH_DB_USERNAME, - password: env.COUCH_DB_PASSWORD, - } -} - if (env.isTest()) { PouchDB.plugin(require("pouchdb-adapter-memory")) POUCH_DB_DEFAULTS = { diff --git a/packages/server/src/environment.js b/packages/server/src/environment.js index 4f02f0f302..9f69664ffb 100644 --- a/packages/server/src/environment.js +++ b/packages/server/src/environment.js @@ -24,8 +24,6 @@ module.exports = { PORT: process.env.PORT, JWT_SECRET: process.env.JWT_SECRET, COUCH_DB_URL: process.env.COUCH_DB_URL, - COUCH_DB_USERNAME: process.env.COUCH_DB_USER, - COUCH_DB_PASSWORD: process.env.COUCH_DB_PASSWORD, MINIO_URL: process.env.MINIO_URL, WORKER_URL: process.env.WORKER_URL, SELF_HOSTED: process.env.SELF_HOSTED, diff --git a/packages/worker/src/db/index.js b/packages/worker/src/db/index.js index b9e05a0e0e..632e19280d 100644 --- a/packages/worker/src/db/index.js +++ b/packages/worker/src/db/index.js @@ -1,21 +1,15 @@ const PouchDB = require("pouchdb") const allDbs = require("pouchdb-all-dbs") const env = require("../environment") +const { getCouchUrl } = require("@budibase/auth/db") // level option is purely for testing (development) -const COUCH_DB_URL = env.COUCH_DB_URL || "http://localhost:10000/db/" +const COUCH_DB_URL = getCouchUrl() || "http://localhost:10000/db/" let POUCH_DB_DEFAULTS = { prefix: COUCH_DB_URL, } -if (env.COUCH_DB_USERNAME && env.COUCH_DB_PASSWORD) { - POUCH_DB_DEFAULTS.auth = { - username: env.COUCH_DB_USERNAME, - password: env.COUCH_DB_PASSWORD, - } -} - if (env.isTest()) { PouchDB.plugin(require("pouchdb-adapter-memory")) POUCH_DB_DEFAULTS = { From 2a766a3011ba21031961801cd6a9934920313178 Mon Sep 17 00:00:00 2001 From: Martin McKeaveney Date: Wed, 18 Aug 2021 11:55:19 +0100 Subject: [PATCH 2/2] catch when no couchDB url passed --- packages/auth/src/db/utils.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/auth/src/db/utils.js b/packages/auth/src/db/utils.js index dfefd54f97..4cd29c9bc8 100644 --- a/packages/auth/src/db/utils.js +++ b/packages/auth/src/db/utils.js @@ -159,6 +159,8 @@ exports.getDeployedAppID = appId => { } exports.getCouchUrl = () => { + if (!env.COUCH_DB_URL) return + // username and password already exist in URL if (env.COUCH_DB_URL.includes("@")) { return env.COUCH_DB_URL