Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lib: make sure only gRPC or ZMQ connects to SaaS #214

Open
wants to merge 2 commits into
base: node-v20.x-nsolid-v5.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
}
Loading