Skip to content

Commit edd2cce

Browse files
committed
test: add grpc tests testing TLS connections
Only grpc insecure connections were being used in the tests. Also, these new tests allow to cover for the untested `NSOLID_GRPC_CERTS` env variable. PR-URL: #406 Reviewed-By: Rafael Gonzaga <rafael.nunu@hotmail.com>
1 parent bf3a07c commit edd2cce

File tree

3 files changed

+68
-23
lines changed

3 files changed

+68
-23
lines changed

test/agents/test-grpc-basic.mjs

Lines changed: 34 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// Flags: --expose-internals
22
import { mustCall, mustSucceed } from '../common/index.mjs';
3+
import fixtures from '../common/fixtures.js';
34
import assert from 'node:assert';
45
import {
56
checkExitData,
@@ -13,17 +14,17 @@ const tests = [];
1314

1415
tests.push({
1516
name: 'should work if agent is killed with signal',
16-
test: async (getEnv) => {
17+
test: async (getEnv, isSecure) => {
1718
return new Promise((resolve) => {
18-
const grpcServer = new GRPCServer();
19+
const grpcServer = new GRPCServer({ tls: isSecure });
1920
grpcServer.start(mustSucceed(async (port) => {
2021
grpcServer.on('exit', mustCall((data) => {
2122
checkExitData(data.msg, data.metadata, agentId, { code: SIGTERM, error: null, profile: '' });
2223
grpcServer.close();
2324
resolve();
2425
}));
2526

26-
const env = getEnv(port);
27+
const env = getEnv(port, isSecure);
2728

2829
const opts = {
2930
stdio: ['inherit', 'inherit', 'inherit', 'ipc'],
@@ -39,17 +40,17 @@ tests.push({
3940

4041
tests.push({
4142
name: 'should work if agent exits gracefully without error',
42-
test: async (getEnv) => {
43+
test: async (getEnv, isSecure) => {
4344
return new Promise((resolve) => {
44-
const grpcServer = new GRPCServer();
45+
const grpcServer = new GRPCServer({ tls: isSecure });
4546
grpcServer.start(mustSucceed(async (port) => {
4647
grpcServer.on('exit', mustCall((data) => {
4748
checkExitData(data.msg, data.metadata, agentId, { code: 0, error: null, profile: '' });
4849
grpcServer.close();
4950
resolve();
5051
}));
5152

52-
const env = getEnv(port);
53+
const env = getEnv(port, isSecure);
5354

5455
const opts = {
5556
stdio: ['inherit', 'inherit', 'inherit', 'ipc'],
@@ -68,17 +69,17 @@ tests.push({
6869

6970
tests.push({
7071
name: 'should work if agent exits gracefully with error code',
71-
test: async (getEnv) => {
72+
test: async (getEnv, isSecure) => {
7273
return new Promise((resolve) => {
73-
const grpcServer = new GRPCServer();
74+
const grpcServer = new GRPCServer({ tls: isSecure });
7475
grpcServer.start(mustSucceed(async (port) => {
7576
grpcServer.on('exit', mustCall((data) => {
7677
checkExitData(data.msg, data.metadata, agentId, { code: 1, error: null, profile: '' });
7778
grpcServer.close();
7879
resolve();
7980
}));
8081

81-
const env = getEnv(port);
82+
const env = getEnv(port, isSecure);
8283

8384
const opts = {
8485
stdio: ['inherit', 'inherit', 'inherit', 'ipc'],
@@ -97,9 +98,9 @@ tests.push({
9798

9899
tests.push({
99100
name: 'should work if agent exits with exception',
100-
test: async (getEnv) => {
101+
test: async (getEnv, isSecure) => {
101102
return new Promise((resolve) => {
102-
const grpcServer = new GRPCServer();
103+
const grpcServer = new GRPCServer({ tls: isSecure });
103104
grpcServer.start(mustSucceed(async (port) => {
104105
grpcServer.on('exit', mustCall((data) => {
105106
const error = { message: 'Uncaught Error: error', stack: '' };
@@ -108,7 +109,7 @@ tests.push({
108109
resolve();
109110
}));
110111

111-
const env = getEnv(port);
112+
const env = getEnv(port, isSecure);
112113

113114
const opts = {
114115
stdio: ['inherit', 'inherit', 'inherit', 'ipc'],
@@ -127,28 +128,41 @@ tests.push({
127128

128129
const testConfigs = [
129130
{
130-
getEnv: (port) => {
131-
return {
131+
getEnv: (port, isSecure) => {
132+
const env = {
132133
NODE_DEBUG_NATIVE: 'nsolid_grpc_agent',
133-
NSOLID_GRPC_INSECURE: 1,
134134
NSOLID_GRPC: `localhost:${port}`,
135135
};
136+
if (!isSecure) {
137+
env.NSOLID_GRPC_INSECURE = 1;
138+
} else {
139+
env.NSOLID_GRPC_CERTS = fixtures.path('keys', 'selfsigned-no-keycertsign', 'cert.pem');
140+
}
141+
return env;
136142
},
137143
},
138144
{
139-
getEnv: (port) => {
140-
return {
145+
getEnv: (port, isSecure) => {
146+
const env = {
141147
NODE_DEBUG_NATIVE: 'nsolid_grpc_agent',
142-
NSOLID_GRPC_INSECURE: 1,
143148
NSOLID_SAAS: `aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbbbbtesting.localhost:${port}`,
144149
};
150+
if (!isSecure) {
151+
env.NSOLID_GRPC_INSECURE = 1;
152+
} else {
153+
env.NSOLID_GRPC_CERTS = fixtures.path('keys', 'selfsigned-no-keycertsign', 'cert.pem');
154+
}
155+
return env;
145156
},
146157
},
147158
];
148159

160+
const isSecureOpts = [false, true];
149161
for (const testConfig of testConfigs) {
150162
for (const { name, test } of tests) {
151-
console.log(`[basic] ${name}`);
152-
await test(testConfig.getEnv);
163+
for (const isSecure of isSecureOpts) {
164+
console.log(`[basic] ${name} ${isSecure ? 'secure' : 'insecure'}`);
165+
await test(testConfig.getEnv, isSecure);
166+
}
153167
}
154168
}

test/common/nsolid-grpc-agent/index.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,18 +69,24 @@ function checkResource(resource, agentId, config, metrics) {
6969

7070

7171
class GRPCServer extends EventEmitter {
72+
#opts;
7273
#server;
73-
constructor() {
74+
constructor(opts) {
7475
super();
7576
this.#server = null;
77+
this.#opts = opts || {};
7678
}
7779

7880
start(cb) {
7981
const args = [];
82+
if (this.#opts.tls) {
83+
args.push('--tls');
84+
}
85+
8086
const opts = {
8187
stdio: ['inherit', 'inherit', 'inherit', 'ipc'],
8288
};
83-
this.#server = fork(path.join(__dirname, 'server.mjs') + '', args, opts);
89+
this.#server = fork(path.join(__dirname, 'server.mjs'), args, opts);
8490
this.#server.on('message', (message) => {
8591
switch (message.type) {
8692
case 'exit':

test/common/nsolid-grpc-agent/server.mjs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,20 @@
11
import assert from 'node:assert';
22
import path from 'node:path';
3+
import { parseArgs } from 'node:util';
34
import grpc from '@grpc/grpc-js';
45
import protoLoader from '@grpc/proto-loader';
56

7+
import fixtures from '../fixtures.js';
8+
9+
const options = {
10+
tls: {
11+
type: 'boolean',
12+
default: false,
13+
},
14+
};
15+
16+
const args = parseArgs({ options });
17+
618
const logsServiceProtoPath = 'opentelemetry/proto/collector/logs/v1/logs_service.proto';
719
const metricsServiceProtoPath = 'opentelemetry/proto/collector/metrics/v1/metrics_service.proto';
820
const traceServiceProtoPath = 'opentelemetry/proto/collector/trace/v1/trace_service.proto';
@@ -210,7 +222,20 @@ async function startServer(cb) {
210222
},
211223
});
212224

213-
const credentials = grpc.ServerCredentials.createInsecure();
225+
let credentials;
226+
if (args.values.tls) {
227+
const key =
228+
fixtures.readKey(path.join('selfsigned-no-keycertsign', 'key.pem'));
229+
const cert =
230+
fixtures.readKey(path.join('selfsigned-no-keycertsign', 'cert.pem'));
231+
credentials = grpc.ServerCredentials.createSsl(null, [{
232+
cert_chain: cert,
233+
private_key: key,
234+
}], false); // False means no client-side authentication
235+
} else {
236+
credentials = grpc.ServerCredentials.createInsecure();
237+
}
238+
214239
return new Promise((resolve, reject) => {
215240
server.bindAsync('localhost:0', credentials, (err, port) => {
216241
server.start();

0 commit comments

Comments
 (0)