From b3a45ca5ddd372228a7a917b976e9a2bdcade70c Mon Sep 17 00:00:00 2001 From: Aidan Gilmore Date: Fri, 10 Mar 2023 12:46:03 -0500 Subject: [PATCH] Updated configuration to support docker secrets --- docs/Options.md | 3 ++- src/utils/config.js | 44 ++++++++++++++++++++++++++++++++------------ 2 files changed, 34 insertions(+), 13 deletions(-) diff --git a/docs/Options.md b/docs/Options.md index 4b0bcd5e..b6f1270c 100644 --- a/docs/Options.md +++ b/docs/Options.md @@ -1,6 +1,7 @@ # Options -The following environment variables are used by Ackee. You can also create a [`.env` file](https://www.npmjs.com/package/dotenv) in the root of the project to store all variables in one file. +The following environment variables are used by Ackee. You can also create a [`.env` file](https://www.npmjs.com/package/dotenv) in the root of the project to store all variables in one file. All environment +variables also support a `_FILE` suffix, which will fetch the value from the specified file path. - [Database](#database) - [Port](#port) diff --git a/src/utils/config.js b/src/utils/config.js index 3ff3eeb6..15a6c64c 100644 --- a/src/utils/config.js +++ b/src/utils/config.js @@ -1,5 +1,6 @@ 'use static' +const fs = require('fs') const { day } = require('./times') // Must be a function or object that loads and returns the env variables at runtime. @@ -7,18 +8,37 @@ const { day } = require('./times') module.exports = new Proxy({}, { get: function(target, prop) { const data = { - ttl: process.env.ACKEE_TTL || day, - port: process.env.ACKEE_PORT || process.env.PORT || 3000, - dbUrl: process.env.ACKEE_MONGODB || process.env.MONGODB_URI, - allowOrigin: process.env.ACKEE_ALLOW_ORIGIN, - autoOrigin: process.env.ACKEE_AUTO_ORIGIN === 'true', - username: process.env.ACKEE_USERNAME, - password: process.env.ACKEE_PASSWORD, - isDemoMode: process.env.ACKEE_DEMO === 'true', - isDevelopmentMode: process.env.NODE_ENV === 'development', - isPreBuildMode: process.env.BUILD_ENV === 'pre', + ttl: configValueFromEnv('ACKEE_TTL') || day, + port: configValueFromEnv('ACKEE_PORT', 'PORT') || 3000, + dbUrl: configValueFromEnv('ACKEE_MONGODB', 'MONGODB_URI'), + allowOrigin: configValueFromEnv('ACKEE_ALLOW_ORIGIN'), + autoOrigin: configValueFromEnv('ACKEE_AUTO_ORIGIN') === 'true', + username: configValueFromEnv('ACKEE_USERNAME'), + password: configValueFromEnv('ACKEE_PASSWORD'), + isDemoMode: configValueFromEnv('ACKEE_DEMO') === 'true', + isDevelopmentMode: configValueFromEnv('NODE_ENV') === 'development', + isPreBuildMode: configValueFromEnv('BUILD_ENV') === 'pre', } - return data[prop] }, -}) \ No newline at end of file +}, +) + +// Retrieves value of the environment variable, if it exists. We also check for +// another enviroment variable with the same name as the but with the suffix +// _FILE. If it exists, we read the value from the file and return it. This is +// useful for Docker secrets. +const configValueFromEnv = (...envVariable) => { + for (const env of envVariable) { + if (process.env[env]) { + return process.env[env] + } + + const envFile = process.env[`${ env }_FILE`] + if (envFile) { + return fs.readFileSync(envFile, 'utf8') + } + } + + return +} \ No newline at end of file