Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const edgeconfig = require('microgateway-config');
const gateway = require('microgateway-core');
const reloadCluster = require('./reload-cluster');
const JsonSocket = require('../../third_party/json-socket/json-socket');
const configLocations = require('../../config/locations');
const isWin = /^win/.test(process.platform);
const ipcPath = configLocations.getIPCFilePath();
const pidPath = configLocations.getPIDFilePath();
const defaultPollInterval = 600;
const uuid = require('uuid/v1');
const debug = require('debug')('microgateway');
const jsdiff = require('diff');
const _ = require('lodash');
//const os = require('os');
const writeConsoleLog = require('microgateway-core').Logging.writeConsoleLog;
edgeconfig.setConsoleLogger(writeConsoleLog);
const Gateway = function() {};
const CONSOLE_LOG_TAG_COMP = 'microgateway gateway';
module.exports = function() {
return new Gateway();
};
// initializeMicroGatewayLogging
// All logging is initialized here.
// For logging to happend xalling initializeMicroGatewayLogging is required at some point early on in
// the flow of configuration
function initializeMicroGatewayLogging(config,options) {
// gateway from require
gateway.Logging.init(config,options);
targetFile = configLocations.getSourceFile(options.org, options.env);
const cache = configLocations.getCachePath(options.org, options.env);
if (fs.existsSync(cache)) {
fs.unlinkSync(cache);
//writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP}, 'deleted ' + cache);
}
const targetPath = configLocations.getSourcePath(options.org, options.env);
if (fs.existsSync(targetPath)) {
fs.unlinkSync(targetPath);
//writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP}, 'deleted ' + targetPath);
}
var configFileDirectory = options.configDir || configLocations.homeDir;
//writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP}, 'init config');
edgeconfig.init({
source: configLocations.getDefaultPath(options.configDir),
targetDir: configFileDirectory,
targetFile: targetFile,
overwrite: true
}, function (/* err, configPath */) {
options.deployed = false;
deployAuth.checkDeployedProxies(options, (err, options) => {
if (err) {
writeConsoleLog('error',{component: CONSOLE_LOG_TAG_COMP}, err);
if ( cb ) { cb(err) } else process.exit(1);
return;
}
configureEdgemicroWithCreds(options, (err) => {
if (err) {
writeConsoleLog('error',{component: CONSOLE_LOG_TAG_COMP}, err);
if ( cb ) { cb(err) } else process.exit(1);
async.series(tasks, function (err, results) {
if (err) {
return cb(err);
}
assert(targetFile, 'must have an assigned target file')
// writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP}, 'updating agent configuration');
if (err) {
return cb(err)
}
agentConfigPath = configLocations.getSourcePath(options.org, options.env, options.configDir);
const agentConfig = edgeconfig.load({ source: agentConfigPath });
addEnvVars(agentConfig);
if (options.deployed === false) {
agentConfig['edge_config']['jwt_public_key'] = (options.url ? options.url+"/edgemicro-auth/publicKey" : results[0]); // get deploy results
agentConfig['edge_config'].bootstrap = results[2].bootstrap; // get genkeys results
} else {
agentConfig['edge_config']['jwt_public_key'] = authUri + '/publicKey';
agentConfig['edge_config'].bootstrap = results[1].bootstrap;
}
var publicKeyUri = agentConfig['edge_config']['jwt_public_key'];
if (publicKeyUri) {
agentConfig['edge_config']['products'] = publicKeyUri.replace('publicKey', 'products');
if (!agentConfig.hasOwnProperty('oauth') || agentConfig['oauth'] === null) {
function(err, results) {
if (err) {
return cb(err);
}
const agentConfigPath = sourcePath;
const agentConfig = that.config = edgeconfig.load({
source: agentConfigPath
});
if (options.internaldeployed === false && options.deployed === false) {
agentConfig['edge_config']['jwt_public_key'] = results[2]; // get deploy results
agentConfig['edge_config'].bootstrap = results[4]; // get genkeys results
} else if (options.internaldeployed === true && options.internaldeployed === false) {
agentConfig['edge_config']['jwt_public_key'] = results[0];
agentConfig['edge_config'].bootstrap = results[2];
} else {
agentConfig['edge_config']['jwt_public_key'] = that.authUri + '/publicKey';
agentConfig['edge_config'].bootstrap = results[1];
}
var publicKeyUri = agentConfig['edge_config']['jwt_public_key'];
if (publicKeyUri) {
}
const targetPath = configLocations.getSourcePath(options.org, options.env);
if (fs.existsSync(targetPath)) {
fs.unlinkSync(targetPath);
writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},'deleted ' + targetPath);
}
options.proxyName = this.name = 'edgemicro-auth';
this.basePath = '/edgemicro-auth';
this.managementUri = options.mgmtUrl;
this.runtimeUrl = options.runtimeUrl;
this.virtualHosts = options.virtualHosts || 'default';
const config = edgeconfig.load({
source: configLocations.getDefaultPath(options.configDir)
});
this.config = config;
this.authUri = config.edge_config.authUri = this.runtimeUrl + this.basePath;
this.config.edge_config.managementUri = this.managementUri;
this.baseUri = this.runtimeUrl + '/edgemicro/%s/organization/%s/environment/%s';
this.vaultName = config.edge_config.vaultName;
this.config.edge_config.baseUri = this.baseUri;
this.deployment = deploymentFx(config.edge_config, this.virtualHosts);
// first: runtimeUri, second: credential, third: org, fourth: env
this.credentialUrl = util.format(this.baseUri, 'credential', options.org, options.env);
this.regionUrl = util.format(this.baseUri, 'region', options.org, options.env);
this.bootstrapUrl = util.format(this.baseUri, 'bootstrap', options.org, options.env);
this.cert = cert(this.config);
this.sourcePath = configLocations.getSourcePath(options.org, options.env);
var bootstrapUri = agentConfig['edge_config']['bootstrap'];
if (bootstrapUri) {
if (!agentConfig.hasOwnProperty('analytics') || agentConfig['analytics'] === null) {
agentConfig['analytics'] = {};
}
agentConfig['analytics']['uri'] = bootstrapUri.replace('bootstrap', 'axpublisher');
agentConfig['analytics']['bufferSize'] = BUFFERSIZE;
agentConfig['analytics']['batchSize'] = BATCHSIZE;
agentConfig['analytics']['flushInterval'] = FLUSHINTERVAL;
}
writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP});
writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP}, 'saving configuration information to:', agentConfigPath);
edgeconfig.save(agentConfig, agentConfigPath); // if it didn't throw, save succeeded
writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP});
if (options.deployed === true) {
writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP}, 'vault info:\n', results[0]);
} else {
writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP}, 'vault info:\n', results[1]);
}
writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP});
writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},keySecretMessage);
const key = results[2] ? results[2].key : results[1].key;
const secret = results[2] ? results[2].secret : results[1].secret;
assert(key, 'must have a key');
assert(secret, 'must have a secret');
writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP}, ' key:', key);
writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP}, ' secret:', secret);
edgeconfig.get({ source: sourcePath, keys: keys }, function (err, config) {
edgeconfig.save(config, cachePath);
agentLib({ keys: keys, target: cachePath }, (err, agent, config) => {
if (err) {
return printError(err);
}
downloadedConfig = config;
async.series(tasks, function ( /*asyncErr, res */ ) {
writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},'verification complete');
agent.close(process.exit); // close and stop agent
});
})
});
edgeconfig.get(opts, (err, newConfig) => {
if(validator(newConfig) === false && !err) {
err = {};
}
if (err) {
// failed to check new config. so try to check again after pollInterval
writeConsoleLog('error',{component: CONSOLE_LOG_TAG_COMP},'Failed to check for change in Config. Will retry after ' + pollInterval + ' seconds');
setTimeout(() => {
reloadOnConfigChange(oldConfig, cache, opts);
}, pollInterval * 1000);
} else {
pollInterval = config.edgemicro.config_change_poll_interval ? config.edgemicro.config_change_poll_interval : pollInterval;
var isConfigChanged = hasConfigChanged(oldConfig, newConfig);
if (isConfigChanged) {
writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},'Configuration change detected. Saving new config and Initiating reload');
edgeconfig.save(newConfig, cache);
clientSocket.sendMessage({
command: 'reload'
});
}
setTimeout(() => {
reloadOnConfigChange(newConfig, cache, opts);
}, pollInterval * 1000);
}
});
};
const util = require('util');
const url = require('url');
const request = require('request');
const debug = require('debug')('edgemicro');
const async = require('async');
const crypto = require('crypto');
const _ = require('lodash');
const parser = new(require('xml2js')).Parser();
const builder = new(require('xml2js')).Builder();
const assert = require('assert');
const cert = require('./cert-lib');
const edgeconfig = require('microgateway-config');
const configLocations = require('../../config/locations');
const deploymentFx = require('./deploy-auth');
const writeConsoleLog = require('microgateway-core').Logging.writeConsoleLog;
edgeconfig.setConsoleLogger(writeConsoleLog);
const CONSOLE_LOG_TAG_COMP = 'microgateway private';
const DEFAULT_HOSTS = 'default,secure';
const Private = function() {};
module.exports = function() {
return new Private();
};
// begins edgemicro configuration process
Private.prototype.configureEdgemicro = function(options, cb) {
if (!fs.existsSync(configLocations.getDefaultPath(options.configDir))) {
writeConsoleLog('error',{component: CONSOLE_LOG_TAG_COMP},"Missing %s, Please run 'edgemicro init'", configLocations.getDefaultPath(options.configDir))
return cb("Please call edgemicro init first")
return cb(new Error('analytics synthetic - got 401 Unauthorized. Invalid key/secret credentials.'));
} else if (res.statusCode !== 200) {
writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},'verifying analytics with payload: FAIL');
return cb(new Error('analytics synthetic - got code: ' + res.statusCode));
} else {
writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},'verifying analytics with payload: OK');
return cb();
}
})
.write(JSON.stringify(payload));
}
];
const cachePath = configLocations.getCachePath(options.org, options.env);
edgeconfig.get({ source: sourcePath, keys: keys }, function (err, config) {
edgeconfig.save(config, cachePath);
agentLib({ keys: keys, target: cachePath }, (err, agent, config) => {
if (err) {
return printError(err);
}
downloadedConfig = config;
async.series(tasks, function ( /*asyncErr, res */ ) {
writeConsoleLog('log',{component: CONSOLE_LOG_TAG_COMP},'verification complete');
agent.close(process.exit); // close and stop agent
});
})
});
}
function printError(err) {