From b2da82453a96165269dc3987392a8c80a01b00ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9rgio=20Salgado?= Date: Fri, 22 Apr 2022 19:20:56 +0100 Subject: [PATCH] fix: production env file path --- bin/convert-txs.js | 2 +- bin/lamassu-configure-frontcamera | 2 +- bin/lamassu-eth-recovery | 2 +- bin/lamassu-migrate | 2 +- bin/lamassu-mnemonic | 2 +- bin/lamassu-ofac-update-sources | 2 +- bin/lamassu-operator | 2 +- bin/lamassu-register | 2 +- bin/lamassu-update-to-mnemonic | 2 +- bin/migrate-config.js | 2 +- bin/old-lamassu-register | 2 +- dev/recreate-seeds.js | 2 +- lib/app.js | 2 +- lib/migrate-options.js | 60 ++++----------------------- lib/new-admin/admin-server.js | 2 +- lib/new-admin/graphql-dev-insecure.js | 2 +- lib/options-loader.js | 2 +- tools/build-prod-env.js | 2 +- tools/set-env-var.js | 5 ++- 19 files changed, 27 insertions(+), 72 deletions(-) diff --git a/bin/convert-txs.js b/bin/convert-txs.js index 71c81f95..658381ef 100755 --- a/bin/convert-txs.js +++ b/bin/convert-txs.js @@ -1,7 +1,7 @@ #!/usr/bin/env node const path = require('path') -require('dotenv').config({ path: path.resolve(__dirname, '../.env') }) +require('dotenv').config({ path: process.env.NODE_ENV === 'production' ? path.resolve(os.homedir(), '.lamassu', '.env') : path.resolve(__dirname, '../.env') }) var pgp = require('pg-promise')() diff --git a/bin/lamassu-configure-frontcamera b/bin/lamassu-configure-frontcamera index 31780f3e..910db546 100755 --- a/bin/lamassu-configure-frontcamera +++ b/bin/lamassu-configure-frontcamera @@ -3,7 +3,7 @@ 'use strict' const path = require('path') -require('dotenv').config({ path: path.resolve(__dirname, '../.env') }) +require('dotenv').config({ path: process.env.NODE_ENV === 'production' ? path.resolve(os.homedir(), '.lamassu', '.env') : path.resolve(__dirname, '../.env') }) const setEnvVariable = require('../tools/set-env-var') diff --git a/bin/lamassu-eth-recovery b/bin/lamassu-eth-recovery index 1d9c71cd..aeedde67 100644 --- a/bin/lamassu-eth-recovery +++ b/bin/lamassu-eth-recovery @@ -1,7 +1,7 @@ #!/usr/bin/env node const path = require('path') -require('dotenv').config({ path: path.resolve(__dirname, '../.env') }) +require('dotenv').config({ path: process.env.NODE_ENV === 'production' ? path.resolve(os.homedir(), '.lamassu', '.env') : path.resolve(__dirname, '../.env') }) const hdkey = require('ethereumjs-wallet/hdkey') const hkdf = require('futoin-hkdf') const db = require('../lib/db') diff --git a/bin/lamassu-migrate b/bin/lamassu-migrate index e09c1fdf..286a6842 100755 --- a/bin/lamassu-migrate +++ b/bin/lamassu-migrate @@ -3,7 +3,7 @@ const FileStore = require('migrate/lib/file-store') const _ = require('lodash/fp') const path = require('path') -require('dotenv').config({ path: path.resolve(__dirname, '../.env') }) +require('dotenv').config({ path: process.env.NODE_ENV === 'production' ? path.resolve(os.homedir(), '.lamassu', '.env') : path.resolve(__dirname, '../.env') }) const db = require('../lib/db') const migrate = require('../lib/migrate') diff --git a/bin/lamassu-mnemonic b/bin/lamassu-mnemonic index fa841b08..3e655ef2 100755 --- a/bin/lamassu-mnemonic +++ b/bin/lamassu-mnemonic @@ -2,7 +2,7 @@ const fs = require('fs') const path = require('path') -require('dotenv').config({ path: path.resolve(__dirname, '../.env') }) +require('dotenv').config({ path: process.env.NODE_ENV === 'production' ? path.resolve(os.homedir(), '.lamassu', '.env') : path.resolve(__dirname, '../.env') }) const MNEMONIC_PATH = process.env.MNEMONIC_PATH diff --git a/bin/lamassu-ofac-update-sources b/bin/lamassu-ofac-update-sources index 0a7f9528..786fd35c 100755 --- a/bin/lamassu-ofac-update-sources +++ b/bin/lamassu-ofac-update-sources @@ -5,7 +5,7 @@ const setEnvVariable = require('../tools/set-env-var') const path = require('path') -require('dotenv').config({ path: path.resolve(__dirname, '../.env') }) +require('dotenv').config({ path: process.env.NODE_ENV === 'production' ? path.resolve(os.homedir(), '.lamassu', '.env') : path.resolve(__dirname, '../.env') }) if (!process.env.OFAC_SOURCES_NAMES && !process.env.OFAC_SOURCES_URLS) { setEnvVariable('OFAC_SOURCES_NAMES', 'sdn_advanced,cons_advanced') diff --git a/bin/lamassu-operator b/bin/lamassu-operator index 8249ee64..bedf170c 100644 --- a/bin/lamassu-operator +++ b/bin/lamassu-operator @@ -4,7 +4,7 @@ const fs = require('fs') const hkdf = require('futoin-hkdf') const path = require('path') -require('dotenv').config({ path: path.resolve(__dirname, '../.env') }) +require('dotenv').config({ path: process.env.NODE_ENV === 'production' ? path.resolve(os.homedir(), '.lamassu', '.env') : path.resolve(__dirname, '../.env') }) const mnemonicHelpers = require('../lib/mnemonic-helpers') diff --git a/bin/lamassu-register b/bin/lamassu-register index 66af20ef..8196b6dc 100755 --- a/bin/lamassu-register +++ b/bin/lamassu-register @@ -1,7 +1,7 @@ #!/usr/bin/env node const path = require('path') -require('dotenv').config({ path: path.resolve(__dirname, '../.env') }) +require('dotenv').config({ path: process.env.NODE_ENV === 'production' ? path.resolve(os.homedir(), '.lamassu', '.env') : path.resolve(__dirname, '../.env') }) const { asyncLocalStorage, defaultStore } = require('../lib/async-storage') const userManagement = require('../lib/new-admin/graphql/modules/userManagement') const authErrors = require('../lib/new-admin/graphql/errors/authentication') diff --git a/bin/lamassu-update-to-mnemonic b/bin/lamassu-update-to-mnemonic index ed14222e..3da8cfe3 100755 --- a/bin/lamassu-update-to-mnemonic +++ b/bin/lamassu-update-to-mnemonic @@ -9,7 +9,7 @@ const mnemonicHelpers = require('../lib/mnemonic-helpers') const setEnvVariable = require('../tools/set-env-var') const path = require('path') -require('dotenv').config({ path: path.resolve(__dirname, '../.env') }) +require('dotenv').config({ path: process.env.NODE_ENV === 'production' ? path.resolve(os.homedir(), '.lamassu', '.env') : path.resolve(__dirname, '../.env') }) if (!process.env.MNEMONIC_PATH && process.env.SEED_PATH) { const seed = fs.readFileSync(process.env.SEED_PATH, 'utf8').trim() diff --git a/bin/migrate-config.js b/bin/migrate-config.js index 759e5e31..5d486ef8 100644 --- a/bin/migrate-config.js +++ b/bin/migrate-config.js @@ -5,7 +5,7 @@ const pgp = require('pg-promise')() const path = require('path') -require('dotenv').config({ path: path.resolve(__dirname, '../.env') }) +require('dotenv').config({ path: process.env.NODE_ENV === 'production' ? path.resolve(os.homedir(), '.lamassu', '.env') : path.resolve(__dirname, '../.env') }) const { PSQL_URL } = require('../lib/constants') diff --git a/bin/old-lamassu-register b/bin/old-lamassu-register index e6e2313a..3da5a9f4 100755 --- a/bin/old-lamassu-register +++ b/bin/old-lamassu-register @@ -1,7 +1,7 @@ #!/usr/bin/env node const path = require('path') -require('dotenv').config({ path: path.resolve(__dirname, '../.env') }) +require('dotenv').config({ path: process.env.NODE_ENV === 'production' ? path.resolve(os.homedir(), '.lamassu', '.env') : path.resolve(__dirname, '../.env') }) const login = require('../lib/admin/login') diff --git a/dev/recreate-seeds.js b/dev/recreate-seeds.js index b4027093..bd542df1 100644 --- a/dev/recreate-seeds.js +++ b/dev/recreate-seeds.js @@ -8,7 +8,7 @@ const os = require('os') const bip39 = require('bip39') const setEnvVariable = require('../tools/set-env-var') -require('dotenv').config({ path: path.resolve(__dirname, '../.env') }) +require('dotenv').config({ path: process.env.NODE_ENV === 'production' ? path.resolve(os.homedir(), '.lamassu', '.env') : path.resolve(__dirname, '../.env') }) if (process.env.MNEMONIC_PATH && !process.env.SEED_PATH) { const mnemonic = fs.readFileSync(process.env.MNEMONIC_PATH, 'utf8') diff --git a/lib/app.js b/lib/app.js index 449da86a..f910164c 100644 --- a/lib/app.js +++ b/lib/app.js @@ -4,7 +4,7 @@ const http = require('http') const https = require('https') const argv = require('minimist')(process.argv.slice(2)) -require('dotenv').config({ path: path.resolve(__dirname, '../.env') }) +require('dotenv').config({ path: process.env.NODE_ENV === 'production' ? path.resolve(os.homedir(), '.lamassu', '.env') : path.resolve(__dirname, '../.env') }) const { asyncLocalStorage, defaultStore } = require('./async-storage') const routes = require('./routes') diff --git a/lib/migrate-options.js b/lib/migrate-options.js index 921b8b1b..8c79fa51 100644 --- a/lib/migrate-options.js +++ b/lib/migrate-options.js @@ -1,7 +1,9 @@ const _ = require('lodash/fp') const fs = require('fs') +const os = require('os') const makeDir = require('make-dir') const path = require('path') +const cp = require('child_process') const load = require('./options-loader') const logger = require('./logger') @@ -53,61 +55,13 @@ function updateOptionBasepath (result, optionName) { } async function run () { - // load defaults - const defaultOpts = require('../lamassu-default') - // load current opts - const options = load() - const currentOpts = options.opts + const options = load().opts + const shouldMigrate = !fs.existsSync(process.env.NODE_ENV === 'production' ? path.resolve(os.homedir(), '.lamassu', '.env') : path.resolve(__dirname, '../.env')) - // check if there are new options to add - let result = _.mergeAll([defaultOpts, currentOpts]) - - // get all the options - // that ends with "Path" suffix - logger.info(`Detected lamassu-server basepath: ${currentBasePath}`) - _.each(_.wrap(updateOptionBasepath, result), - [ - 'seedPath', - 'caPath', - 'certPath', - 'keyPath', - 'lamassuCaPath' - ]) - - const shouldMigrate = !_.isEqual(result, currentOpts) || _.has('lamassuServerPath', result) - - // write the resulting lamassu.json + // write the resulting .env if (shouldMigrate) { - // remove old lamassuServerPath config - result = _.omit('lamassuServerPath', result) - - // find keys for which values - // have been changed - const differentValue = _.wrap(_.filter, key => !_.isEqual(result[key], currentOpts[key])) - - // output affected options - const newOpts = _.pick(_.union( - // find change keys - differentValue(_.keys(result)), - // find new opts - _.difference(_.keys(result), _.keys(currentOpts)) - ), result) - logger.info('Updating options', newOpts) - - // store new lamassu.json file - fs.writeFileSync(options.path, JSON.stringify(result, null, ' ')) + const postgresPw = new RegExp(':(\\w*)@').exec(options.postgresql)[1] + cp.spawnSync('node', ['tools/build-prod-env.js', '--db-password', postgresPw, '--hostname', options.hostname], { cwd: currentBasePath, encoding: 'utf-8' }) } - - // get all the new options - // that ends with "Dir" suffix - mapKeyValuesDeep((v, k) => { - if (_.endsWith('Dir', k)) { - const path = _.attempt(() => makeDir.sync(v)) - - if (_.isError(path)) { - logger.error(`while creating folder ${v}`, path) - } - } - }, result) } diff --git a/lib/new-admin/admin-server.js b/lib/new-admin/admin-server.js index b6ec0221..7d633c08 100644 --- a/lib/new-admin/admin-server.js +++ b/lib/new-admin/admin-server.js @@ -12,7 +12,7 @@ const { graphqlUploadExpress } = require('graphql-upload') const { ApolloServer } = require('apollo-server-express') const _ = require('lodash/fp') -require('dotenv').config({ path: path.resolve(__dirname, '../../.env') }) +require('dotenv').config({ path: process.env.NODE_ENV === 'production' ? path.resolve(os.homedir(), '.lamassu', '.env') : path.resolve(__dirname, '../.env') }) const { asyncLocalStorage, defaultStore } = require('../async-storage') const logger = require('../logger') diff --git a/lib/new-admin/graphql-dev-insecure.js b/lib/new-admin/graphql-dev-insecure.js index 8d673d54..c8468208 100644 --- a/lib/new-admin/graphql-dev-insecure.js +++ b/lib/new-admin/graphql-dev-insecure.js @@ -2,7 +2,7 @@ const express = require('express') const path = require('path') const { ApolloServer } = require('apollo-server-express') -require('dotenv').config({ path: path.resolve(__dirname, '../../.env') }) +require('dotenv').config({ path: process.env.NODE_ENV === 'production' ? path.resolve(os.homedir(), '.lamassu', '.env') : path.resolve(__dirname, '../.env') }) const { typeDefs, resolvers } = require('./graphql/schema') const logger = require('../logger') diff --git a/lib/options-loader.js b/lib/options-loader.js index 4f05ee5a..5c81fd66 100644 --- a/lib/options-loader.js +++ b/lib/options-loader.js @@ -4,7 +4,7 @@ const os = require('os') const argv = require('minimist')(process.argv.slice(2)) const _ = require('lodash/fp') -require('dotenv').config({ path: path.resolve(__dirname, '../.env') }) +require('dotenv').config({ path: process.env.NODE_ENV === 'production' ? path.resolve(os.homedir(), '.lamassu', '.env') : path.resolve(__dirname, '../.env') }) const DATABASE = process.env.LAMASSU_DB ?? 'PROD' const dbMapping = psqlConf => ({ diff --git a/tools/build-prod-env.js b/tools/build-prod-env.js index ba277224..99e7c2be 100644 --- a/tools/build-prod-env.js +++ b/tools/build-prod-env.js @@ -13,7 +13,7 @@ if (!_.isEqual(_.intersection(_.keys(argv), requiredParams), requiredParams)) { process.exit(2) } -fs.copyFileSync(path.resolve(__dirname, '../.sample.env'), path.resolve(__dirname, '../.env')) +fs.copyFileSync(path.resolve(__dirname, '../.sample.env'), process.env.NODE_ENV === 'production' ? path.resolve(os.homedir(), '.lamassu', '.env') : path.resolve(__dirname, '../.env')) setEnvVariable('POSTGRES_USER', 'lamassu_pg') setEnvVariable('POSTGRES_PASSWORD', `${argv['db-password']}`) diff --git a/tools/set-env-var.js b/tools/set-env-var.js index 1b82cff0..fbe0086b 100644 --- a/tools/set-env-var.js +++ b/tools/set-env-var.js @@ -3,7 +3,8 @@ const os = require('os') const path = require('path') const setEnvVariable = (key, value) => { - const ENV_VARIABLES = fs.readFileSync(path.resolve(__dirname, '../.env'), 'utf-8').split(os.EOL) + const ENV_PATH = process.env.NODE_ENV === 'production' ? path.resolve(os.homedir(), '.lamassu', '.env') : path.resolve(__dirname, '../.env') + const ENV_VARIABLES = fs.readFileSync(ENV_PATH, 'utf-8').split(os.EOL) const target = ENV_VARIABLES.indexOf(ENV_VARIABLES.find(line => line.match(new RegExp(`^${key}=`)))) if (target < 0) { @@ -18,7 +19,7 @@ const setEnvVariable = (key, value) => { ENV_VARIABLES.splice(target, 1, `${key}=${value}`) } - fs.writeFileSync(path.resolve(__dirname, '../.env'), ENV_VARIABLES.join(os.EOL)) + fs.writeFileSync(ENV_PATH, ENV_VARIABLES.join(os.EOL)) } module.exports = setEnvVariable