Skip to content

Commit 9103dc8

Browse files
committed
Read postgres connection details from a file
- Reading the DB connection details from a file. This supports passing the secret data from the operator in a more secure way, by volume mount instead of an env - The existing environment variables are still inspected, allowing overrides if needed. Signed-off-by: Danny Zaken <dannyzaken@gmail.com>
1 parent ec7011c commit 9103dc8

File tree

2 files changed

+37
-14
lines changed

2 files changed

+37
-14
lines changed

src/util/fs_utils.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,22 @@ async function disk_usage(root) {
134134
return { size, count };
135135
}
136136

137+
// try to read a file synchronously. If the file does not exist, return undefined
138+
// if the file exists but is not readable, throw an error
139+
// if the file exists and is readable, return the content
140+
function try_read_file_sync(file_name) {
141+
if (!file_name) return;
142+
try {
143+
return fs.readFileSync(file_name, 'utf8');
144+
} catch (err) {
145+
if (err.code === 'ENOENT' || err.code === 'ENOTDIR') {
146+
// file does not exist or is not a directory
147+
return;
148+
}
149+
throw err;
150+
}
151+
}
152+
137153

138154
// returns the first line in the file that contains the substring
139155
async function find_line_in_file(file_name, line_sub_string) {
@@ -356,3 +372,4 @@ exports.ignore_enoent = ignore_enoent;
356372
exports.PRIVATE_DIR_PERMISSIONS = PRIVATE_DIR_PERMISSIONS;
357373
exports.file_exists = file_exists;
358374
exports.file_not_exists = file_not_exists;
375+
exports.try_read_file_sync = try_read_file_sync;

src/util/postgres_client.js

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ const SensitiveString = require('./sensitive_string');
2828
const time_utils = require('./time_utils');
2929
const config = require('../../config');
3030
const ssl_utils = require('./ssl_utils');
31+
const fs_utils = require('./fs_utils');
3132

3233
const DB_CONNECT_ERROR_MESSAGE = 'Could not acquire client from DB connection pool';
3334
mongodb.Binary.prototype[util.inspect.custom] = function custom_inspect_binary() {
@@ -703,20 +704,20 @@ class PostgresTable {
703704
return _do_query(client || this.get_pool(), q, 0);
704705
}
705706

706-
/**
707+
/**
707708
* executeSQL takes a raw SQL query and params and runs it against
708709
* the database. If `query_name` is passed then it prepares a
709710
* statement on the first execution while the further executions
710711
* will re-utilize the prepared statement (pre-parsed).
711-
*
712+
*
712713
* @template T
713-
*
714-
* @param {string} query
715-
* @param {Array<any>} params
714+
*
715+
* @param {string} query
716+
* @param {Array<any>} params
716717
* @param {{
717718
* query_name?: string,
718719
* }} [options = {}]
719-
*
720+
*
720721
* @returns {Promise<import('pg').QueryResult<T>>}
721722
*/
722723
async executeSQL(query, params, options = {}) {
@@ -1496,22 +1497,27 @@ class PostgresClient extends EventEmitter {
14961497
}
14971498
};
14981499

1499-
const postgres_port = parseInt(process.env.POSTGRES_PORT || '5432', 10);
15001500

1501-
if (process.env.POSTGRES_CONNECTION_STRING) {
1501+
if (process.env.POSTGRES_CONNECTION_STRING_PATH) {
15021502
/** @type {import('pg').PoolConfig} */
15031503
this.new_pool_params = {
1504-
connectionString: process.env.POSTGRES_CONNECTION_STRING,
1504+
connectionString: fs.readFileSync(process.env.POSTGRES_CONNECTION_STRING_PATH, "utf8"),
15051505
...params,
15061506
};
15071507
} else {
1508+
// get the connection configuration. first from env, then from file, then default
1509+
const host = process.env.POSTGRES_HOST || fs_utils.try_read_file_sync(process.env.POSTGRES_HOST_PATH) || '127.0.0.1';
1510+
const user = process.env.POSTGRES_USER || fs_utils.try_read_file_sync(process.env.POSTGRES_USER_PATH) || 'postgres';
1511+
const password = process.env.POSTGRES_PASSWORD || fs_utils.try_read_file_sync(process.env.POSTGRES_PASSWORD_PATH) || 'noobaa';
1512+
const database = process.env.POSTGRES_DBNAME || fs_utils.try_read_file_sync(process.env.POSTGRES_DBNAME_PATH) || 'nbcore';
1513+
const port = parseInt(process.env.POSTGRES_PORT || fs_utils.try_read_file_sync(process.env.POSTGRES_PORT_PATH) || '5432', 10);
15081514
// TODO: This need to move to another function
15091515
this.new_pool_params = {
1510-
host: process.env.POSTGRES_HOST || '127.0.0.1',
1511-
user: process.env.POSTGRES_USER || 'postgres',
1512-
password: process.env.POSTGRES_PASSWORD || 'noobaa',
1513-
database: process.env.POSTGRES_DBNAME || 'nbcore',
1514-
port: postgres_port,
1516+
host,
1517+
user,
1518+
password,
1519+
database,
1520+
port,
15151521
...params,
15161522
};
15171523
}

0 commit comments

Comments
 (0)