From eeff713b29f7886ccbffbf6f071cc07710526b4d Mon Sep 17 00:00:00 2001 From: Santiago Gimeno Date: Mon, 11 Nov 2024 15:10:01 +0100 Subject: [PATCH] lib: make sure only gRPC or ZMQ connects to SaaS --- lib/nsolid.js | 31 +++++++------- test/parallel/test-nsolid-config-saas.js | 52 ++++++++++++++++++++++++ 2 files changed, 66 insertions(+), 17 deletions(-) diff --git a/lib/nsolid.js b/lib/nsolid.js index 984d7f8ba8..52b434d434 100644 --- a/lib/nsolid.js +++ b/lib/nsolid.js @@ -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; @@ -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); } } @@ -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 || @@ -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; @@ -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; diff --git a/test/parallel/test-nsolid-config-saas.js b/test/parallel/test-nsolid-config-saas.js index 0aa7903b3a..4626ac2241 100644 --- a/test/parallel/test-nsolid-config-saas.js +++ b/test/parallel/test-nsolid-config-saas.js @@ -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)); @@ -157,4 +207,6 @@ switch (process.argv[2]) { execProc3(); execProc4(); execProc5(); + execProc6(); + execProc7(); }