Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve logging, clean code, fix vulnerabilities #61

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 32 additions & 28 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,40 @@
const {noopLogger, makePatternForConferenceScan} = require('./lib/utils');
const { noopLogger, makePatternForConferenceScan, buildSentinels } = require('./lib/utils');
const Redis = require('ioredis');
const fs = require('fs');

let JAMBONES_REDIS_CONFIGURATION = process.env.JAMBONES_REDIS_SENTINELS ? {
sentinels: process.env.JAMBONES_REDIS_SENTINELS.split(',').map((sentinel) => {
let host, port = 26379;
if (sentinel.includes(':')) {
const arr = sentinel.split(':');
host = arr[0];
port = parseInt(arr[1], 10);
} else {
host = sentinel;
}
return {host, port};
}),
name: process.env.JAMBONES_REDIS_SENTINEL_MASTER_NAME,
...(process.env.JAMBONES_REDIS_SENTINEL_PASSWORD && {
password: process.env.JAMBONES_REDIS_SENTINEL_PASSWORD
}),
...(process.env.JAMBONES_REDIS_SENTINEL_USERNAME && {
username: process.env.JAMBONES_REDIS_SENTINEL_USERNAME
}),
...(process.env.JAMBONES_REDIS_SENTINEL_SERVER_PASSWORD && {
sentinelPassword: process.env.JAMBONES_REDIS_SENTINEL_SERVER_PASSWORD
}),
} : {
let JAMBONES_REDIS_CONFIGURATION = {
host: process.env.JAMBONES_REDIS_HOST || 'localhost',
port: process.env.JAMBONES_REDIS_PORT || 6379
};

let mode = 'single';

if (process.env.JAMBONES_REDIS_SENTINELS) {
mode = 'high availability';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

where this mode variable is being used?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

line 62 - Successfully connected to Redis ${mode} mode

JAMBONES_REDIS_CONFIGURATION = {
name: process.env.JAMBONES_REDIS_SENTINEL_MASTER_NAME,
host: undefined,
port: undefined,
sentinels: buildSentinels(),
...(process.env.JAMBONES_REDIS_SENTINEL_USERNAME && {
username: process.env.JAMBONES_REDIS_SENTINEL_USERNAME
}),
...(process.env.JAMBONES_REDIS_SENTINEL_PASSWORD && {
password: process.env.JAMBONES_REDIS_SENTINEL_PASSWORD
}),
...(process.env.JAMBONES_REDIS_SENTINEL_SERVER_PASSWORD && {
sentinelPassword: process.env.JAMBONES_REDIS_SENTINEL_SERVER_PASSWORD
}),
}
}

if (process.env.ENABLE_JAMBONES_REDIS_TLS) {
JAMBONES_REDIS_CONFIGURATION = {
...JAMBONES_REDIS_CONFIGURATION,
tls: {
...(process.env.JAMBONES_REDIS_TLS_CA_FILE && {
ca: fs.readFileSync(process.env.JAMBONES_REDIS_TLS_CA_FILE)}),
ca: fs.readFileSync(process.env.JAMBONES_REDIS_TLS_CA_FILE)
}),
...(process.env.JAMBONES_REDIS_TLS_KEY_FILE && {
key: fs.readFileSync(process.env.JAMBONES_REDIS_TLS_KEY_FILE)
}),
Expand All @@ -47,17 +48,20 @@ if (process.env.ENABLE_JAMBONES_REDIS_TLS) {

module.exports = (opts, logger) => {
logger = logger || noopLogger;
const connectionOpts = (!!opts && Object.keys(opts).length > 0) ? {...opts} : JAMBONES_REDIS_CONFIGURATION;
const connectionOpts = (!!opts && Object.keys(opts).length > 0) ? { ...opts } : JAMBONES_REDIS_CONFIGURATION;

const client = new Redis(connectionOpts);
['ready', 'connect', 'reconnecting', 'error', 'end', 'warning']
.forEach((event) => {
client.on(event, (...args) => {
if ('error' === event) {
if (process.env.NODE_ENV === 'test' && args[0]?.code === 'ECONNREFUSED') return;
logger.error({...args}, '@jambonz/realtimedb-helpers - redis error');
logger.error({ ...args }, '@jambonz/realtimedb-helpers - redis error');
}
else if ('connect' === event) {
logger.info(`Successfully connected to Redis ${mode} mode`);
}
else logger.debug({args}, `redis event ${event}`);
else logger.debug({ args }, `redis event ${event}`);
});
});

Expand Down
19 changes: 18 additions & 1 deletion lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,22 @@ function filterNullsAndObjects(obj) {
debug(filtered, 'filtering done');
return filtered;
}

const buildSentinels = () => {
const sentinels = process.env.JAMBONES_REDIS_SENTINELS.split(',').map((sentinel) => {
let host, port = 26379;
if (sentinel.includes(':')) {
const [host, stringport] = sentinel.split(':');
host = host;
port = parseInt(stringport, 10);
} else {
host = sentinel;
}
return {host, port};
});
return sentinels;
}

module.exports = {
makeCallKey,
makeBasicAuthHeader,
Expand All @@ -61,5 +77,6 @@ module.exports = {
noopLogger,
filterNullsAndObjects,
CALL_SET: 'active-call-sids',
PURGE_CALLS_LOCK_KEY: 'purge-calls-lock'
PURGE_CALLS_LOCK_KEY: 'purge-calls-lock',
buildSentinels,
};
Loading