microgateway-config
Advanced tools
Comparing version 3.1.6 to 3.1.7
16
index.js
@@ -8,6 +8,8 @@ 'use strict'; | ||
const RedisClientLib = require('./lib/redisClient'); | ||
const EnvTagsReplacer = require('./lib/env-tags-replacer'); | ||
module.exports = function(){ | ||
var ioInstance = io(); | ||
var networkInstance = network(); | ||
let envTagsReplacerInstance = new EnvTagsReplacer(); | ||
var ioInstance = io(envTagsReplacerInstance); | ||
var networkInstance = network(ioInstance, envTagsReplacerInstance); | ||
return { | ||
@@ -55,2 +57,3 @@ get:function(options,cb){ | ||
networkInstance.setConsoleLogger(consoleLogger); | ||
envTagsReplacerInstance.setConsoleLogger(consoleLogger); | ||
}, | ||
@@ -63,4 +66,13 @@ getRedisClient:function(config, cb){ | ||
return new RedisClientLib(config, cb); | ||
}, | ||
replaceEnvTags:function(config,options){ | ||
/** | ||
* replaces the env tags in config by the env values | ||
* @param config object whose <E></E> tags to be replaced with env values. | ||
* @param options object which has below properties | ||
* disableLogs: boolean value, if 'true' console errors and debug logs will not be displayed. | ||
*/ | ||
return envTagsReplacerInstance.replaceEnvTags(config,options) | ||
} | ||
}; | ||
}(); |
'use strict'; | ||
var assert = require('assert'); | ||
var url = require('url'); | ||
module.exports.validate = function validate(configObject) { | ||
const EnvTagsReplacer = require('./env-tags-replacer'); | ||
const envTagsReplacer = new EnvTagsReplacer(); | ||
module.exports.validate = function validate(configObject, options={}) { | ||
assert(configObject, 'config is not defined'); | ||
let config = mergeEnvValues(JSON.parse(JSON.stringify(configObject))); | ||
let config = envTagsReplacer.replaceEnvTags(configObject); | ||
if (!process.env.EDGEMICRO_LOCAL && !process.env.EDGEMICRO_LOCAL_PROXY) { | ||
@@ -148,3 +152,6 @@ assert(config.edge_config, 'config.edge_config is not defined'); | ||
} | ||
if (config.edgemicro.hasOwnProperty('logTargetErrorsAs')) { | ||
const logLevel = ['error', 'warn', 'trace', 'info', 'debug']; | ||
assert(logLevel.includes(config.edgemicro.logTargetErrorsAs), 'invalid value for config.edgemicro.logTargetErrorsAs: ' + config.edgemicro.logTargetErrorsAs + ', valid values are error, warn, trace, info, debug'); | ||
} | ||
if (config.quota) { | ||
@@ -166,15 +173,20 @@ assert(config.quota.timeUnit === 'hour' || | ||
} | ||
if (config.analytics) { | ||
if (config.analytics.bufferSize) { | ||
assert(typeof config.analytics.bufferSize === 'number', 'config.analytics.bufferSize is not a number'); | ||
assert(+config.analytics.bufferSize > 0, 'config.analytics.bufferSize is invalid'); | ||
if (config.edgemicro.hasOwnProperty('enableAnalytics')) { | ||
assert(typeof config.edgemicro.enableAnalytics === 'boolean', 'config.edgemicro.enableAnalytics is not a boolean'); | ||
} | ||
if(!config.edgemicro.hasOwnProperty('enableAnalytics') || config.edgemicro.enableAnalytics === true || options.metrics === true){ | ||
if (config.analytics) { | ||
if (config.analytics.bufferSize) { | ||
assert(typeof config.analytics.bufferSize === 'number', 'config.analytics.bufferSize is not a number'); | ||
assert(+config.analytics.bufferSize > 0, 'config.analytics.bufferSize is invalid'); | ||
} | ||
if (config.analytics.flushInterval) { | ||
assert(typeof config.analytics.flushInterval === 'number', 'config.analytics.flushInterval is not a number'); | ||
assert(+config.analytics.flushInterval > 0, 'config.analytics.flushInterval is invalid'); | ||
} | ||
if (config.analytics.batchSize) { | ||
assert(typeof config.analytics.batchSize === 'number', 'config.analytics.batchSize is not a number'); | ||
assert(+config.analytics.batchSize > 0, 'config.analytics.batchSize is invalid'); | ||
} | ||
} | ||
if (config.analytics.flushInterval) { | ||
assert(typeof config.analytics.flushInterval === 'number', 'config.analytics.flushInterval is not a number'); | ||
assert(+config.analytics.flushInterval > 0, 'config.analytics.flushInterval is invalid'); | ||
} | ||
if (config.analytics.batchSize) { | ||
assert(typeof config.analytics.batchSize === 'number', 'config.analytics.batchSize is not a number'); | ||
assert(+config.analytics.batchSize > 0, 'config.analytics.batchSize is invalid'); | ||
} | ||
} | ||
@@ -247,23 +259,1 @@ if (config.spikearrest) { | ||
}; | ||
function mergeEnvValues(config){ | ||
try { | ||
let configStr = JSON.stringify(config); | ||
let envRegx = RegExp('<E>.+?<\/E>','g'); | ||
let envKeys = configStr.match(envRegx); | ||
if ( envKeys && envKeys.length > 0) { | ||
envKeys.forEach( key => { | ||
let envKey = key.replace('<E>','').replace("</E>",''); // remove env tags | ||
let value = process.env[envKey]; | ||
if ( value ) { | ||
configStr = configStr.replace(key,`${value}`) | ||
} | ||
}) | ||
config = JSON.parse(configStr); | ||
} | ||
} catch(err) { | ||
} | ||
return config; | ||
} | ||
@@ -7,7 +7,12 @@ 'use strict' | ||
const _ = require('lodash'); | ||
const EnvTagsReplacer = require('./env-tags-replacer'); | ||
let writeConsoleLog = function () {}; | ||
const CONSOLE_LOG_TAG_COMP = 'microgateway-config io'; | ||
const IO = function () { | ||
const IO = function (envTagsReplacer) { | ||
if ( envTagsReplacer ) { | ||
this.envTagsReplacer = envTagsReplacer; | ||
} else { | ||
this.envTagsReplacer = new EnvTagsReplacer(); | ||
} | ||
}; | ||
@@ -97,2 +102,5 @@ | ||
} | ||
if ( !options.keepEnvtags ) { | ||
content = this.envTagsReplacer.replaceEnvTags(content, options.envTagsReplacerOptions); | ||
} | ||
return content; | ||
@@ -148,2 +156,3 @@ }; | ||
writeConsoleLog = consoleLogger; | ||
this.envTagsReplacer.setConsoleLogger(writeConsoleLog); | ||
}; |
@@ -17,2 +17,3 @@ 'use strict'; | ||
const RedisClientLib = require('./redisClient'); | ||
const EnvTagsReplacer = require('./env-tags-replacer'); | ||
@@ -28,4 +29,9 @@ let writeConsoleLog = function () {}; | ||
const Loader = function(io) { | ||
const Loader = function(io, envTagsReplacer) { | ||
this.io = io || ioLib(); | ||
if ( envTagsReplacer ) { | ||
this.envTagsReplacer = envTagsReplacer; | ||
} else { | ||
this.envTagsReplacer = new EnvTagsReplacer(); | ||
} | ||
}; | ||
@@ -63,8 +69,8 @@ | ||
const config = this.io.loadSync({ | ||
source: options.source | ||
source: options.source, keepEnvtags: true | ||
}); | ||
let envReplacedConfig = this.envTagsReplacer.replaceEnvTags(config, { disableLogs: true }); | ||
//create default proxy if params were supplied. | ||
var proxies = { | ||
proxies: getDefaultProxy(config, options) | ||
proxies: getDefaultProxy(envReplacedConfig, options) // this function is just reading the config so passed envReplacedConfig | ||
}; | ||
@@ -83,3 +89,3 @@ | ||
default_config_validator.validate(config); | ||
default_config_validator.validate(config, globalOptions); | ||
@@ -110,8 +116,9 @@ const cache = _.merge({}, config, proxies); | ||
this.config = this.io.loadSync({ | ||
source: source | ||
source: source, keepEnvtags: true | ||
}); | ||
this.envReplacedConfig = this.envTagsReplacer.replaceEnvTags(this.config, { disableLogs: true }); | ||
//set default proxies | ||
this.config.proxies = getDefaultProxy(this.config, options); | ||
this.config.proxies = getDefaultProxy(this.envReplacedConfig, options); | ||
// | ||
loadConfiguration(this.config, keys, callback); | ||
loadConfiguration(this.config, keys, callback, this.envReplacedConfig); | ||
}); | ||
@@ -121,8 +128,9 @@ | ||
this.config = this.io.loadSync({ | ||
source: source | ||
source: source, keepEnvtags: true | ||
}); | ||
this.envReplacedConfig = this.envTagsReplacer.replaceEnvTags(this.config, { disableLogs: true }); | ||
//set default proxies | ||
this.config.proxies = getDefaultProxy(this.config, options); | ||
this.config.proxies = getDefaultProxy(this.envReplacedConfig, options); | ||
// | ||
loadConfiguration(this.config, keys, callback); | ||
loadConfiguration(this.config, keys, callback, this.envReplacedConfig); | ||
} | ||
@@ -143,7 +151,7 @@ } | ||
function loadConfiguration(thisconfig, keys, callback) { | ||
function loadConfiguration(thisconfig, keys, callback, envReplacedConfig) { | ||
default_config_validator.validate(thisconfig); | ||
default_config_validator.validate(thisconfig, globalOptions); | ||
const err = _validateUrls(thisconfig); | ||
const err = _validateUrls(envReplacedConfig); | ||
if (err) { | ||
@@ -154,6 +162,7 @@ return callback(err); | ||
const config = _setDefaults(thisconfig); | ||
envReplacedConfig = _setDefaults(envReplacedConfig); | ||
// initiate an immediate load, and setup retries if it fails | ||
_load(config, keys, function(err, proxies, products) { | ||
_load(envReplacedConfig, keys, function(err, proxies, products) { | ||
if (err) { | ||
@@ -200,4 +209,3 @@ return callback(err); | ||
const mergedConfig = _merge(config, _mapEdgeProxies(proxies), _mapEdgeProducts(products, config)); | ||
proxy_validator.validate(mergedConfig); | ||
_mergeKeys(mergedConfig, keys); // merge keys before sending to edge micro | ||
proxy_validator.validate(mergedConfig, globalOptions); | ||
callback(null, mergedConfig); | ||
@@ -224,3 +232,2 @@ } else { | ||
writeConsoleLog('info',{component: CONSOLE_LOG_TAG_COMP},'loaded cached config from', target); | ||
_mergeKeys(mergedConfig, keys); // merge keys before sending to edge micro | ||
callback(null, mergedConfig); | ||
@@ -233,3 +240,3 @@ } else { | ||
} | ||
}); | ||
}, config); | ||
} | ||
@@ -290,5 +297,5 @@ | ||
// sourceConfig is with env tags, config is env replaced. | ||
const processLoad = (config, keys, callback, useSynchronizer, redisClient, sourceConfig) => { | ||
const processLoad = (config, keys, callback, useSynchronizer, redisClient) => { | ||
const options = {}; | ||
@@ -529,13 +536,16 @@ | ||
} | ||
if (!config.oauth) | ||
config.oauth = {}; | ||
if (!config.apikeys) | ||
config.apikeys = {}; | ||
if (!config.oauthv2) | ||
config.oauthv2 = {}; | ||
/** | ||
* sourceConfig is used here because this is merged with data from edge and saved in the cache file. | ||
* We do not want the env replaced config to be saved on file because env values will come to file system. | ||
* */ | ||
if (!sourceConfig.oauth) | ||
sourceConfig.oauth = {}; | ||
if (!sourceConfig.apikeys) | ||
sourceConfig.apikeys = {}; | ||
if (!sourceConfig.oauthv2) | ||
sourceConfig.oauthv2 = {}; | ||
if (results.length > 1 && results[2]) { | ||
config.oauth.public_key = results[2]; // save key in oauth section | ||
config.apikeys.public_key = results[2]; //save key in oauthv2 section | ||
config.oauthv2.public_key = results[2]; | ||
sourceConfig.oauth.public_key = results[2]; // save key in oauth section | ||
sourceConfig.apikeys.public_key = results[2]; //save key in oauthv2 section | ||
sourceConfig.oauthv2.public_key = results[2]; | ||
} else { | ||
@@ -547,5 +557,5 @@ writeConsoleLog('warn',{component: CONSOLE_LOG_TAG_COMP},'failed to download jwt_public_key'); | ||
if (results.length > 1 && results[3]) { | ||
config.oauth.jwk_keys = results[3]; // save key in oauth section | ||
config.apikeys.jwk_keys = results[3]; | ||
config.oauthv2.jwk_keys = results[3]; | ||
sourceConfig.oauth.jwk_keys = results[3]; // save key in oauth section | ||
sourceConfig.apikeys.jwk_keys = results[3]; | ||
sourceConfig.oauthv2.jwk_keys = results[3]; | ||
} | ||
@@ -560,3 +570,3 @@ } catch (err) { | ||
} | ||
const _load = function(config, keys, callback) { | ||
const _load = function(config, keys, callback, sourceConfig) { // sourceConfig is with env tags, config is env replaced. | ||
const useSynchronizer = (config.edge_config.synchronizerMode === 1 || config.edge_config.synchronizerMode === 2) ? true : false; | ||
@@ -570,6 +580,6 @@ // connect to redis | ||
} | ||
processLoad(config, keys, callback, useSynchronizer,redisClient); | ||
processLoad(config, keys, callback, useSynchronizer, redisClient, sourceConfig); | ||
}); | ||
} else { | ||
processLoad(config, keys, callback, useSynchronizer); | ||
processLoad(config, keys, callback, useSynchronizer, null, sourceConfig); | ||
} | ||
@@ -613,27 +623,2 @@ | ||
/** | ||
* merge downloaded config with keys | ||
* @param mergedConfig | ||
* @param keys | ||
* @private | ||
*/ | ||
function _mergeKeys(mergedConfig, keys) { | ||
assert(keys.key, 'key is missing'); | ||
assert(keys.secret, 'secret is missing'); | ||
// copy keys to analytics section | ||
if (!mergedConfig.analytics) { | ||
mergedConfig.analytics = {}; | ||
} | ||
mergedConfig.analytics.key = keys.key; | ||
mergedConfig.analytics.secret = keys.secret; | ||
// copy keys to quota section | ||
if (mergedConfig.quota) { | ||
Object.keys(mergedConfig.quota).forEach(function(name) { | ||
const quota = mergedConfig.quota[name]; | ||
quota.key = keys.key; | ||
quota.secret = keys.secret; | ||
}); | ||
} | ||
} | ||
/** | ||
* | ||
@@ -684,2 +669,3 @@ * @param config | ||
mergedConfig['quota'] = products.product_to_quota; | ||
mergedConfig['product_to_scopes'] = products.product_to_scopes; | ||
if (mergedConfig['quota']) { | ||
@@ -759,2 +745,3 @@ let uri = ''; | ||
const product_to_api_resource = {}; | ||
const product_to_scopes = {}; | ||
assert(Array.isArray(products), 'products should be an array'); | ||
@@ -765,2 +752,3 @@ products.forEach(function(product) { | ||
product_to_api_resource[product.name] = product.apiResources; | ||
product_to_scopes[product.name] = product.scopes; | ||
product.proxies.forEach(function(proxy) { | ||
@@ -803,3 +791,4 @@ if (product_to_proxy[product.name]) { | ||
product_to_quota: product_to_quota, | ||
product_to_api_resource: product_to_api_resource | ||
product_to_api_resource: product_to_api_resource, | ||
product_to_scopes:product_to_scopes | ||
}; | ||
@@ -821,3 +810,2 @@ } | ||
default_config_validator.validate(config); | ||
@@ -966,2 +954,3 @@ const defaults = { | ||
writeConsoleLog = consoleLogger; | ||
this.envTagsReplacer.setConsoleLogger(writeConsoleLog); | ||
}; |
'use strict'; | ||
var assert = require('assert'); | ||
module.exports.validate = function validate(config) { | ||
assert(config, 'config is not defined'); | ||
const EnvTagsReplacer = require('./env-tags-replacer'); | ||
const envTagsReplacer = new EnvTagsReplacer(); | ||
module.exports.validate = function validate(configObject, options) { | ||
assert(configObject, 'config is not defined'); | ||
const config = envTagsReplacer.replaceEnvTags(configObject, { disableLogs: true }); | ||
assert(config.proxies, 'config.proxies is not defined'); | ||
@@ -16,11 +21,13 @@ config.proxies.forEach(function (proxy, index) { | ||
}); | ||
if (config.analytics) { | ||
assert(config.analytics.uri, 'config.analytics.uri is not defined'); | ||
assert(typeof config.analytics.uri === 'string', 'config.analytics.uri is not a string'); | ||
assert(config.analytics.proxy, 'config.analytics.proxy is not defined'); | ||
assert(config.analytics.proxy === 'dummy', 'config.analytics.proxy is not "dummy"'); | ||
assert(config.analytics.source, 'config.analytics.source is not defined'); | ||
assert(config.analytics.source === 'microgateway', 'config.analytics.source is not "microgateway"'); | ||
assert(config.analytics.proxy_revision, 'config.analytics.proxy_revision is not defined'); | ||
assert(typeof config.analytics.proxy_revision === 'number', 'config.analytics.proxy_revision is not a number'); | ||
if(!config.edgemicro.hasOwnProperty('enableAnalytics') || config.edgemicro.enableAnalytics === true || options.metrics === true){ | ||
if (config.analytics) { | ||
assert(config.analytics.uri, 'config.analytics.uri is not defined'); | ||
assert(typeof config.analytics.uri === 'string', 'config.analytics.uri is not a string'); | ||
assert(config.analytics.proxy, 'config.analytics.proxy is not defined'); | ||
assert(config.analytics.proxy === 'dummy', 'config.analytics.proxy is not "dummy"'); | ||
assert(config.analytics.source, 'config.analytics.source is not defined'); | ||
assert(config.analytics.source === 'microgateway', 'config.analytics.source is not "microgateway"'); | ||
assert(config.analytics.proxy_revision, 'config.analytics.proxy_revision is not defined'); | ||
assert(typeof config.analytics.proxy_revision === 'number', 'config.analytics.proxy_revision is not a number'); | ||
} | ||
} | ||
@@ -36,3 +43,3 @@ if (config.oauth) { | ||
} | ||
return config; | ||
return configObject; | ||
}; |
{ | ||
"name": "microgateway-config", | ||
"version": "3.1.6", | ||
"version": "3.1.7", | ||
"description": "Apigee Microgateway Handler", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
Sorry, the diff of this file is too big to display
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 7 instances in 1 package
328007
42
7080
69