Skip to content

Commit

Permalink
lib: make sure only gRPC or ZMQ connects to SaaS
Browse files Browse the repository at this point in the history
PR-URL: #214
Reviewed-By: Trevor Norris <trev.norris@gmail.com>
PR-URL: #224
Reviewed-By: Juan José Arboleda <soyjuanarbol@gmail.com>
  • Loading branch information
santigimeno committed Nov 21, 2024
1 parent 531d712 commit ed40d73
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 17 deletions.
31 changes: 14 additions & 17 deletions lib/nsolid.js
Original file line number Diff line number Diff line change
Expand Up @@ -713,10 +713,11 @@ function updateConfig(config = {}) {
nsolidConfig.otlp = null;
}

if (nsolidConfig.grpc) {
// Make sure it's a valid URL otherwise it might crash
if (nsolidConfig.grpc && !nsolidConfig.saas) {
// Make sure it's a valid URL otherwise it might crash in the grpc++
// URLParser implementation
try {
new URL(nsolidConfig.grpc);
new URL(`http://${nsolidConfig.grpc}`);
} catch {
process._rawDebug(`Invalid grpc url: "${nsolidConfig.grpc}"`);
nsolidConfig.grpc = null;
Expand Down Expand Up @@ -768,12 +769,15 @@ function initializeConfig(nsolidConfig) {
pkgConfig.nsolid.pubkey ||
DEFAULT_PUBKEY;

// Only set saas if command wasn't already set
if (!nsolidConfig.command) {
nsolidConfig.saas =
process.env.NSOLID_SAAS ||
pkgConfig.nsolid.saas;
if (nsolidConfig.saas) {
// GRPC Agent configuration
nsolidConfig.grpc = process.env.NSOLID_GRPC || pkgConfig.nsolid.grpc;

nsolidConfig.saas = process.env.NSOLID_SAAS || pkgConfig.nsolid.saas;

if (nsolidConfig.saas) {
if (nsolidConfig.command) {
nsolidConfig.saas = undefined;
} else if (!nsolidConfig.grpc) {
nsolidConfig.command = parseSaasEnvVar(nsolidConfig.saas, 0);
}
}
Expand All @@ -783,10 +787,6 @@ function initializeConfig(nsolidConfig) {
envToBool(process.env.NSOLID_DISABLE_IPV6) ||
pkgConfig.nsolid.disableIpv6;

nsolidConfig.grpc =
process.env.NSOLID_GRPC ||
pkgConfig.nsolid.grpc;

// StatsD daemon address
nsolidConfig.statsd =
process.env.NSOLID_STATSD ||
Expand Down Expand Up @@ -897,9 +897,6 @@ function initializeConfig(nsolidConfig) {
}
}

// GRPC Agent configuration
nsolidConfig.grpc = process.env.NSOLID_GRPC || pkgConfig.nsolid.grpc;

// Make sure some hostname is always provided.
if (nsolidConfig.command && +nsolidConfig.command)
nsolidConfig.command = 'localhost:' + nsolidConfig.command;
Expand All @@ -909,7 +906,7 @@ function initializeConfig(nsolidConfig) {
nsolidConfig.bulk = 'localhost:' + nsolidConfig.bulk;
if (nsolidConfig.statsd && +nsolidConfig.statsd)
nsolidConfig.statsd = 'localhost:' + nsolidConfig.statsd;
if (nsolidConfig.grpc && +nsolidConfig.grpc)
if (!nsolidConfig.saas && nsolidConfig.grpc && +nsolidConfig.grpc)
nsolidConfig.grpc = 'localhost:' + nsolidConfig.grpc;

return nsolidConfig;
Expand Down
52 changes: 52 additions & 0 deletions test/parallel/test-nsolid-config-saas.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,56 @@ function execProc5() {
});
}

// NSOLID_COMMAND trumps NSOLID_SAAS, so the token should be ignored. It
// connects to both ZMQ and gRPC.
function execProc6() {
let output = '';
const proc = spawn(process.execPath,
[ __filename, 'child1' ],
{
env: {
NSOLID_COMMAND: PORT,
NSOLID_GRPC: PORT,
NSOLID_SAAS: saasToken
}
});
proc.stdout.on('data', (d) => {
output += d;
});

proc.on('close', (code) => {
assert.strictEqual(code, 0);
const config = JSON.parse(output);
assert.strictEqual(config.command, `localhost:${PORT}`);
assert.strictEqual(config.grpc, `localhost:${PORT}`);
assert.strictEqual(config.saas, undefined);
});
}

// If NSOLID_SAAS with NSOLID_GRPC, the token connects to SaaS using gRPC.
function execProc7() {
let output = '';
const proc = spawn(process.execPath,
[ __filename, 'child1' ],
{
env: {
NSOLID_SAAS: saasToken,
NSOLID_GRPC: PORT,
}
});
proc.stdout.on('data', (d) => {
output += d;
});

proc.on('close', (code) => {
assert.strictEqual(code, 0);
const config = JSON.parse(output);
assert.strictEqual(config.command, undefined);
assert.strictEqual(config.grpc, `${PORT}`);
assert.strictEqual(config.saas, saasToken);
});
}

switch (process.argv[2]) {
case 'child1':
process.stdout.write(JSON.stringify(nsolid.config));
Expand Down Expand Up @@ -157,4 +207,6 @@ switch (process.argv[2]) {
execProc3();
execProc4();
execProc5();
execProc6();
execProc7();
}

0 comments on commit ed40d73

Please sign in to comment.