diff --git a/app.js b/app.js index d4f12dc..065522b 100644 --- a/app.js +++ b/app.js @@ -1,18 +1,4 @@ -const assert = require('assert'); -assert.ok(process.env.JAMBONES_MYSQL_HOST && - process.env.JAMBONES_MYSQL_USER && - process.env.JAMBONES_MYSQL_PASSWORD && - process.env.JAMBONES_MYSQL_DATABASE, 'missing JAMBONES_MYSQL_XXX env vars'); -if (process.env.JAMBONES_REDIS_SENTINELS) { - assert.ok(process.env.JAMBONES_REDIS_SENTINEL_MASTER_NAME, - 'missing JAMBONES_REDIS_SENTINEL_MASTER_NAME env var, JAMBONES_REDIS_SENTINEL_PASSWORD env var is optional'); -} else { - assert.ok(process.env.JAMBONES_REDIS_HOST, 'missing JAMBONES_REDIS_HOST env var'); -} -assert.ok(process.env.DRACHTIO_PORT || process.env.DRACHTIO_HOST, 'missing DRACHTIO_PORT env vars'); -assert.ok(process.env.DRACHTIO_SECRET, 'missing DRACHTIO_SECRET env var'); -assert.ok(process.env.JAMBONES_TIME_SERIES_HOST, 'missing JAMBONES_TIME_SERIES_HOST env var'); -assert.ok(process.env.JAMBONES_NETWORK_CIDR || process.env.K8S, 'missing JAMBONES_NETWORK_CIDR env var'); +require('./lib/config').validateEnvVars(); const Srf = require('drachtio-srf'); const srf = new Srf('sbc-inbound'); diff --git a/lib/config.js b/lib/config.js new file mode 100644 index 0000000..f2c8add --- /dev/null +++ b/lib/config.js @@ -0,0 +1,41 @@ +const assert = require('assert'); + +const requiredEnvVars = [ + 'JAMBONES_MYSQL_HOST', + 'JAMBONES_MYSQL_USER', + 'JAMBONES_MYSQL_PASSWORD', + 'JAMBONES_MYSQL_DATABASE', + 'DRACHTIO_SECRET', + 'JAMBONES_TIME_SERIES_HOST' +]; + +if (process.env.JAMBONES_REDIS_SENTINELS) { + requiredEnvVars.push('JAMBONES_REDIS_SENTINEL_MASTER_NAME'); +} else { + requiredEnvVars.push('JAMBONES_REDIS_HOST'); +} + +if (process.env.DRACHTIO_HOST || process.env.DRACHTIO_PORT) { + requiredEnvVars.push(process.env.DRACHTIO_HOST ? 'DRACHTIO_HOST' : 'DRACHTIO_PORT'); +} + +if (process.env.DRACHTIO_HOST && !process.env.K8S) { + assert.ok(process.env.JAMBONES_NETWORK_CIDR, + 'Missing JAMBONES_NETWORK_CIDR env var (required when DRACHTIO_HOST is set and not in K8S)'); +} else if (process.env.K8S && process.env.K8S_FEATURE_SERVER_SERVICE_NAME) { + requiredEnvVars.push('K8S'); + requiredEnvVars.push('K8S_FEATURE_SERVER_SERVICE_NAME'); +} else { + assert.ok(process.env.K8S && !process.env.K8S_FEATURE_SERVER_SERVICE_NAME, + 'when running in Kubernetes, an env var K8S_FEATURE_SERVER_SERVICE_NAME is required'); +} + +const validateEnvVars = () => { + requiredEnvVars.forEach((envVar) => { + assert.ok(process.env[envVar], `Missing ${envVar} env var`); + }); +}; + +module.exports = { + validateEnvVars +}; \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 2b504fa..0b54659 100644 --- a/package-lock.json +++ b/package-lock.json @@ -2685,9 +2685,10 @@ "dev": true }, "node_modules/cookie": { - "version": "0.6.0", - "resolved": "https://registry.npmjs.org/cookie/-/cookie-0.6.0.tgz", - "integrity": "sha512-U71cyTamuh1CRNCfpGY6to28lxvNwPG4Guz/EVjgf3Jmzv0vlDp1atT9eS5dDjMYHucpHbWns6Lwf3BKz6svdw==", + "version": "0.7.1", + "resolved": "https://registry.npmjs.org/cookie/-/cookie-0.7.1.tgz", + "integrity": "sha512-6DnInpx7SJ2AK3+CTUE/ZM0vWTUboZCegxhC2xiIydHR9jNuTAASBrfEpHhiGOZw/nX51bHt6YQl8jsGo4y/0w==", + "license": "MIT", "engines": { "node": ">= 0.6" } @@ -3424,16 +3425,17 @@ } }, "node_modules/express": { - "version": "4.21.0", - "resolved": "https://registry.npmjs.org/express/-/express-4.21.0.tgz", - "integrity": "sha512-VqcNGcj/Id5ZT1LZ/cfihi3ttTn+NJmkli2eZADigjq29qTlWi/hAQ43t/VLPq8+UX06FCEx3ByOYet6ZFblng==", + "version": "4.21.1", + "resolved": "https://registry.npmjs.org/express/-/express-4.21.1.tgz", + "integrity": "sha512-YSFlK1Ee0/GC8QaO91tHcDxJiE/X4FbpAyQWkxAvG6AXCuR65YzK8ua6D9hvi/TzUfZMpc+BwuM1IPw8fmQBiQ==", + "license": "MIT", "dependencies": { "accepts": "~1.3.8", "array-flatten": "1.1.1", "body-parser": "1.20.3", "content-disposition": "0.5.4", "content-type": "~1.0.4", - "cookie": "0.6.0", + "cookie": "0.7.1", "cookie-signature": "1.0.6", "debug": "2.6.9", "depd": "2.0.0", @@ -8880,9 +8882,9 @@ "dev": true }, "cookie": { - "version": "0.6.0", - "resolved": "https://registry.npmjs.org/cookie/-/cookie-0.6.0.tgz", - "integrity": "sha512-U71cyTamuh1CRNCfpGY6to28lxvNwPG4Guz/EVjgf3Jmzv0vlDp1atT9eS5dDjMYHucpHbWns6Lwf3BKz6svdw==" + "version": "0.7.1", + "resolved": "https://registry.npmjs.org/cookie/-/cookie-0.7.1.tgz", + "integrity": "sha512-6DnInpx7SJ2AK3+CTUE/ZM0vWTUboZCegxhC2xiIydHR9jNuTAASBrfEpHhiGOZw/nX51bHt6YQl8jsGo4y/0w==" }, "cookie-signature": { "version": "1.0.6", @@ -9435,16 +9437,16 @@ "integrity": "sha512-mQw+2fkQbALzQ7V0MY0IqdnXNOeTtP4r0lN9z7AAawCXgqea7bDii20AYrIBrFd/Hx0M2Ocz6S111CaFkUcb0Q==" }, "express": { - "version": "4.21.0", - "resolved": "https://registry.npmjs.org/express/-/express-4.21.0.tgz", - "integrity": "sha512-VqcNGcj/Id5ZT1LZ/cfihi3ttTn+NJmkli2eZADigjq29qTlWi/hAQ43t/VLPq8+UX06FCEx3ByOYet6ZFblng==", + "version": "4.21.1", + "resolved": "https://registry.npmjs.org/express/-/express-4.21.1.tgz", + "integrity": "sha512-YSFlK1Ee0/GC8QaO91tHcDxJiE/X4FbpAyQWkxAvG6AXCuR65YzK8ua6D9hvi/TzUfZMpc+BwuM1IPw8fmQBiQ==", "requires": { "accepts": "~1.3.8", "array-flatten": "1.1.1", "body-parser": "1.20.3", "content-disposition": "0.5.4", "content-type": "~1.0.4", - "cookie": "0.6.0", + "cookie": "0.7.1", "cookie-signature": "1.0.6", "debug": "2.6.9", "depd": "2.0.0",