From 8718488aeef908385a312d1d0bd227cbc8fc4397 Mon Sep 17 00:00:00 2001 From: Martin McKeaveney Date: Mon, 16 Aug 2021 15:58:04 +0100 Subject: [PATCH 1/6] couchdb direct auth --- packages/auth/src/db/utils.js | 8 +++++++- packages/server/scripts/dev/manage.js | 2 +- packages/server/src/db/client.js | 4 ++++ packages/server/src/environment.js | 2 ++ packages/worker/scripts/dev/manage.js | 2 +- packages/worker/src/db/index.js | 4 ++++ packages/worker/src/environment.js | 3 +-- 7 files changed, 20 insertions(+), 5 deletions(-) diff --git a/packages/auth/src/db/utils.js b/packages/auth/src/db/utils.js index 1fc78f4182..e3a86f8ae8 100644 --- a/packages/auth/src/db/utils.js +++ b/packages/auth/src/db/utils.js @@ -168,7 +168,13 @@ exports.getAllDbs = async () => { if (env.isTest()) { return getCouch().allDbs() } - const response = await fetch(`${env.COUCH_DB_URL}/_all_dbs`) + const response = await fetch(`${env.COUCH_DB_URL}/_all_dbs`, { + method: "POST", + body: JSON.stringify({ + name: env.COUCH_DB_USERNAME, + password: env.COUCH_DB_PASSWORD, + }), + }) if (response.status === 200) { return response.json() } else { diff --git a/packages/server/scripts/dev/manage.js b/packages/server/scripts/dev/manage.js index ffd8c6b9e3..7e9d7f6a07 100644 --- a/packages/server/scripts/dev/manage.js +++ b/packages/server/scripts/dev/manage.js @@ -36,7 +36,7 @@ async function init() { const envFileJson = { PORT: 4001, MINIO_URL: "http://localhost:10000/", - COUCH_DB_URL: "http://budibase:budibase@localhost:10000/db/", + COUCH_DB_URL: "http://localhost:10000/db/", REDIS_URL: "localhost:6379", WORKER_URL: "http://localhost:4002", INTERNAL_API_KEY: "budibase", diff --git a/packages/server/src/db/client.js b/packages/server/src/db/client.js index d50d72b18d..887b287c84 100644 --- a/packages/server/src/db/client.js +++ b/packages/server/src/db/client.js @@ -12,6 +12,10 @@ PouchDB.adapter("writableStream", replicationStream.adapters.writableStream) let POUCH_DB_DEFAULTS = { prefix: COUCH_DB_URL, + auth: { + username: env.COUCH_DB_USERNAME, + password: env.COUCH_DB_PASSWORD, + } } if (env.isTest()) { diff --git a/packages/server/src/environment.js b/packages/server/src/environment.js index 52c680f65a..45c7673851 100644 --- a/packages/server/src/environment.js +++ b/packages/server/src/environment.js @@ -23,6 +23,8 @@ module.exports = { // important PORT: process.env.PORT, JWT_SECRET: process.env.JWT_SECRET, + COUCH_DB_USERNAME: process.env.COUCH_DB_USER, + COUCH_DB_PASSWORD: process.env.COUCH_DB_PASSWORD, COUCH_DB_URL: process.env.COUCH_DB_URL, MINIO_URL: process.env.MINIO_URL, WORKER_URL: process.env.WORKER_URL, diff --git a/packages/worker/scripts/dev/manage.js b/packages/worker/scripts/dev/manage.js index b9d28b6278..d7eb76fc74 100644 --- a/packages/worker/scripts/dev/manage.js +++ b/packages/worker/scripts/dev/manage.js @@ -17,7 +17,7 @@ async function init() { REDIS_URL: "localhost:6379", REDIS_PASSWORD: "budibase", MINIO_URL: "http://localhost:10000/", - COUCH_DB_URL: "http://budibase:budibase@localhost:10000/db/", + COUCH_DB_URL: "http://localhost:10000/db/", } let envFile = "" Object.keys(envFileJson).forEach(key => { diff --git a/packages/worker/src/db/index.js b/packages/worker/src/db/index.js index d6d035cff1..d09ddf0652 100644 --- a/packages/worker/src/db/index.js +++ b/packages/worker/src/db/index.js @@ -7,6 +7,10 @@ const COUCH_DB_URL = env.COUCH_DB_URL || "http://localhost:10000/db/" let POUCH_DB_DEFAULTS = { prefix: COUCH_DB_URL, + auth: { + username: env.COUCH_DB_USERNAME, + password: env.COUCH_DB_PASSWORD, + } } if (env.isTest()) { diff --git a/packages/worker/src/environment.js b/packages/worker/src/environment.js index 384230b9b3..e8666e6b87 100644 --- a/packages/worker/src/environment.js +++ b/packages/worker/src/environment.js @@ -23,7 +23,6 @@ module.exports = { MINIO_ACCESS_KEY: process.env.MINIO_ACCESS_KEY, MINIO_SECRET_KEY: process.env.MINIO_SECRET_KEY, MINIO_URL: process.env.MINIO_URL, - COUCH_DB_URL: process.env.COUCH_DB_URL, LOG_LEVEL: process.env.LOG_LEVEL, JWT_SECRET: process.env.JWT_SECRET, SALT_ROUNDS: process.env.SALT_ROUNDS, @@ -31,7 +30,7 @@ module.exports = { REDIS_PASSWORD: process.env.REDIS_PASSWORD, INTERNAL_API_KEY: process.env.INTERNAL_API_KEY, /* TODO: to remove - once deployment removed */ - COUCH_DB_USERNAME: process.env.COUCH_DB_USERNAME, + COUCH_DB_USERNAME: process.env.COUCH_DB_USER, COUCH_DB_PASSWORD: process.env.COUCH_DB_PASSWORD, _set(key, value) { process.env[key] = value From 884b36730b2b5f19b4d24d13c89b0323a6e19b8b Mon Sep 17 00:00:00 2001 From: Martin McKeaveney Date: Mon, 16 Aug 2021 16:38:00 +0100 Subject: [PATCH 2/6] utility helper for determining correct couchDB URL --- packages/auth/src/db/utils.js | 21 ++++++++++++------- packages/auth/src/environment.js | 2 ++ .../src/api/controllers/row/internalSearch.js | 3 ++- 3 files changed, 17 insertions(+), 9 deletions(-) diff --git a/packages/auth/src/db/utils.js b/packages/auth/src/db/utils.js index e3a86f8ae8..7c44664a68 100644 --- a/packages/auth/src/db/utils.js +++ b/packages/auth/src/db/utils.js @@ -159,6 +159,17 @@ exports.getDeployedAppID = appId => { return appId } +exports.getCouchUrl = () => { + // username and password already exist in URL + if (env.COUCH_DB_URL.includes("@")) { + return env.COUCH_DB_URL + } + + const [protocol, ...rest] = env.COUCH_DB_URL.split("://") + + return `${protocol}://${env.COUCH_DB_USERNAME}:${env.COUCH_DB_PASSWORD}@${rest}` +} + /** * if in production this will use the CouchDB _all_dbs call to retrieve a list of databases. If testing * when using Pouch it will use the pouchdb-all-dbs package. @@ -168,13 +179,7 @@ exports.getAllDbs = async () => { if (env.isTest()) { return getCouch().allDbs() } - const response = await fetch(`${env.COUCH_DB_URL}/_all_dbs`, { - method: "POST", - body: JSON.stringify({ - name: env.COUCH_DB_USERNAME, - password: env.COUCH_DB_PASSWORD, - }), - }) + const response = await fetch(`${exports.getCouchUrl()}/_all_dbs`) if (response.status === 200) { return response.json() } else { @@ -309,4 +314,4 @@ exports.Replication = Replication exports.getScopedConfig = getScopedConfig exports.generateConfigID = generateConfigID exports.getConfigParams = getConfigParams -exports.getScopedFullConfig = getScopedFullConfig +exports.getScopedFullConfig = getScopedFullConfig \ No newline at end of file diff --git a/packages/auth/src/environment.js b/packages/auth/src/environment.js index 355843d02d..e64a1c75f0 100644 --- a/packages/auth/src/environment.js +++ b/packages/auth/src/environment.js @@ -9,6 +9,8 @@ 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, + COUCH_DB_PASSWORD: process.env.COUCH_DB_PASSWORD, SALT_ROUNDS: process.env.SALT_ROUNDS, REDIS_URL: process.env.REDIS_URL, REDIS_PASSWORD: process.env.REDIS_PASSWORD, diff --git a/packages/server/src/api/controllers/row/internalSearch.js b/packages/server/src/api/controllers/row/internalSearch.js index c13091d6e8..20147a7857 100644 --- a/packages/server/src/api/controllers/row/internalSearch.js +++ b/packages/server/src/api/controllers/row/internalSearch.js @@ -1,6 +1,7 @@ const { SearchIndexes } = require("../../../db/utils") const env = require("../../../environment") const fetch = require("node-fetch") +const { getCouchUrl } = require("@budibase/auth/db") /** * Class to build lucene query URLs. @@ -233,7 +234,7 @@ class QueryBuilder { } async run() { - const url = `${env.COUCH_DB_URL}/${this.appId}/_design/database/_search/${SearchIndexes.ROWS}` + const url = `${getCouchUrl()}/${this.appId}/_design/database/_search/${SearchIndexes.ROWS}` const body = this.buildSearchBody() return await runQuery(url, body) } From 5f2fc47a168a26b5d151f8693c6af666ee91beb1 Mon Sep 17 00:00:00 2001 From: Martin McKeaveney Date: Mon, 16 Aug 2021 16:39:10 +0100 Subject: [PATCH 3/6] readd couchDB url to env --- packages/worker/src/environment.js | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/worker/src/environment.js b/packages/worker/src/environment.js index e8666e6b87..1fc68ccc71 100644 --- a/packages/worker/src/environment.js +++ b/packages/worker/src/environment.js @@ -23,6 +23,7 @@ module.exports = { MINIO_ACCESS_KEY: process.env.MINIO_ACCESS_KEY, MINIO_SECRET_KEY: process.env.MINIO_SECRET_KEY, MINIO_URL: process.env.MINIO_URL, + COUCH_DB_URL: process.env.COUCH_DB_URL, LOG_LEVEL: process.env.LOG_LEVEL, JWT_SECRET: process.env.JWT_SECRET, SALT_ROUNDS: process.env.SALT_ROUNDS, From 9ad19092af5c73833439848127b42afa90976008 Mon Sep 17 00:00:00 2001 From: Martin McKeaveney Date: Mon, 16 Aug 2021 16:44:46 +0100 Subject: [PATCH 4/6] match up env var in worker --- packages/worker/src/environment.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/worker/src/environment.js b/packages/worker/src/environment.js index 1fc68ccc71..384230b9b3 100644 --- a/packages/worker/src/environment.js +++ b/packages/worker/src/environment.js @@ -31,7 +31,7 @@ module.exports = { REDIS_PASSWORD: process.env.REDIS_PASSWORD, INTERNAL_API_KEY: process.env.INTERNAL_API_KEY, /* TODO: to remove - once deployment removed */ - COUCH_DB_USERNAME: process.env.COUCH_DB_USER, + COUCH_DB_USERNAME: process.env.COUCH_DB_USERNAME, COUCH_DB_PASSWORD: process.env.COUCH_DB_PASSWORD, _set(key, value) { process.env[key] = value From 3b20f5ba4d504f6cc67840b6a4f47c6d9f6e5a9a Mon Sep 17 00:00:00 2001 From: Martin McKeaveney Date: Mon, 16 Aug 2021 16:47:26 +0100 Subject: [PATCH 5/6] lint --- packages/server/src/api/controllers/row/internalSearch.js | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/server/src/api/controllers/row/internalSearch.js b/packages/server/src/api/controllers/row/internalSearch.js index 20147a7857..c5bd01b383 100644 --- a/packages/server/src/api/controllers/row/internalSearch.js +++ b/packages/server/src/api/controllers/row/internalSearch.js @@ -1,5 +1,4 @@ const { SearchIndexes } = require("../../../db/utils") -const env = require("../../../environment") const fetch = require("node-fetch") const { getCouchUrl } = require("@budibase/auth/db") From 16220dbf118eb47999288eb13a71b2c555de5eb0 Mon Sep 17 00:00:00 2001 From: Martin McKeaveney Date: Mon, 16 Aug 2021 17:17:48 +0100 Subject: [PATCH 6/6] lint :sparkles: --- packages/auth/src/db/utils.js | 2 +- packages/server/src/api/controllers/row/internalSearch.js | 4 +++- packages/server/src/db/client.js | 2 +- packages/worker/src/db/index.js | 2 +- 4 files changed, 6 insertions(+), 4 deletions(-) diff --git a/packages/auth/src/db/utils.js b/packages/auth/src/db/utils.js index 7c44664a68..2ac23d02a5 100644 --- a/packages/auth/src/db/utils.js +++ b/packages/auth/src/db/utils.js @@ -314,4 +314,4 @@ exports.Replication = Replication exports.getScopedConfig = getScopedConfig exports.generateConfigID = generateConfigID exports.getConfigParams = getConfigParams -exports.getScopedFullConfig = getScopedFullConfig \ No newline at end of file +exports.getScopedFullConfig = getScopedFullConfig diff --git a/packages/server/src/api/controllers/row/internalSearch.js b/packages/server/src/api/controllers/row/internalSearch.js index c5bd01b383..cc35da40ec 100644 --- a/packages/server/src/api/controllers/row/internalSearch.js +++ b/packages/server/src/api/controllers/row/internalSearch.js @@ -233,7 +233,9 @@ class QueryBuilder { } async run() { - const url = `${getCouchUrl()}/${this.appId}/_design/database/_search/${SearchIndexes.ROWS}` + const url = `${getCouchUrl()}/${this.appId}/_design/database/_search/${ + SearchIndexes.ROWS + }` const body = this.buildSearchBody() return await runQuery(url, body) } diff --git a/packages/server/src/db/client.js b/packages/server/src/db/client.js index 887b287c84..9a244fee17 100644 --- a/packages/server/src/db/client.js +++ b/packages/server/src/db/client.js @@ -15,7 +15,7 @@ let POUCH_DB_DEFAULTS = { auth: { username: env.COUCH_DB_USERNAME, password: env.COUCH_DB_PASSWORD, - } + }, } if (env.isTest()) { diff --git a/packages/worker/src/db/index.js b/packages/worker/src/db/index.js index d09ddf0652..c0676634f7 100644 --- a/packages/worker/src/db/index.js +++ b/packages/worker/src/db/index.js @@ -10,7 +10,7 @@ let POUCH_DB_DEFAULTS = { auth: { username: env.COUCH_DB_USERNAME, password: env.COUCH_DB_PASSWORD, - } + }, } if (env.isTest()) {