diff --git a/src/agent/service/echo.ts b/src/agent/service/echo.ts index 284869be4c..b99923bbbe 100644 --- a/src/agent/service/echo.ts +++ b/src/agent/service/echo.ts @@ -1,19 +1,13 @@ import type * as grpc from '@grpc/grpc-js'; import type { ConnectionInfoGet } from 'agent/types'; import * as utilsPB from '../../proto/js/polykey/v1/utils/utils_pb'; -import * as nodesUtils from '../../nodes/utils'; function echo({ connectionInfoGet }: { connectionInfoGet: ConnectionInfoGet }) { return async ( call: grpc.ServerUnaryCall, callback: grpc.sendUnaryData, ): Promise => { - const connectionInfo = connectionInfoGet(call)!; - console.log( - connectionInfo.egressPort, - connectionInfo.ingressPort, - nodesUtils.encodeNodeId(connectionInfo.nodeId), - ); + connectionInfoGet(call); const response = new utilsPB.EchoMessage(); response.setChallenge(call.request.getChallenge()); callback(null, response); diff --git a/tests/agent/GRPCClientAgent.test.ts b/tests/agent/GRPCClientAgent.test.ts index dc6635fdc3..b97a7216ba 100644 --- a/tests/agent/GRPCClientAgent.test.ts +++ b/tests/agent/GRPCClientAgent.test.ts @@ -1,5 +1,6 @@ import type { Host, Port, TLSConfig } from '@/network/types'; import type * as grpc from '@grpc/grpc-js'; +import type { NodeId } from '@/nodes/types'; import fs from 'fs'; import path from 'path'; import os from 'os'; @@ -21,8 +22,6 @@ import * as utilsPB from '@/proto/js/polykey/v1/utils/utils_pb'; import * as agentErrors from '@/agent/errors'; import * as keysUtils from '@/keys/utils'; import * as testAgentUtils from './utils'; -import * as testUtils from '../utils'; -import * as nodesUtils from '@/nodes/utils'; describe(GRPCClientAgent.name, () => { const host = '127.0.0.1' as Host; @@ -30,16 +29,11 @@ describe(GRPCClientAgent.name, () => { const logger = new Logger(`${GRPCClientAgent.name} test`, LogLevel.WARN, [ new StreamHandler(), ]); - let mockedGenerateKeyPair: jest.SpyInstance; let mockedGenerateDeterministicKeyPair: jest.SpyInstance; beforeAll(async () => { - const globalKeyPair = await testUtils.setupGlobalKeypair(); - mockedGenerateKeyPair = jest - .spyOn(keysUtils, 'generateKeyPair') - .mockResolvedValue(globalKeyPair); mockedGenerateDeterministicKeyPair = jest .spyOn(keysUtils, 'generateDeterministicKeyPair') - .mockResolvedValue(globalKeyPair); + .mockImplementation((bits, _) => keysUtils.generateKeyPair(bits)); }); afterAll(async () => { mockedGenerateDeterministicKeyPair.mockRestore(); @@ -221,78 +215,88 @@ describe(GRPCClientAgent.name, () => { expect(client.secured).toBeFalsy(); }); describe('With connection through proxies', () => { + const logger = new Logger(`${GRPCClientAgent.name} test`, LogLevel.WARN, [ + new StreamHandler(), + ]); + const localHost = '127.0.0.1' as Host; + let clientWithProxies1: GRPCClientAgent; let clientFwdProxy1: ForwardProxy; let clientKeyManager1: KeyManager; + let nodeId1: NodeId; let clientWithProxies2: GRPCClientAgent; let clientFwdProxy2: ForwardProxy; let clientKeyManager2: KeyManager; + let nodeId2: NodeId; beforeEach(async () => { - mockedGenerateDeterministicKeyPair - .mockClear() - .mockImplementation((bits, _) => { - return keysUtils.generateKeyPair(bits); - }); - + dataDir = await fs.promises.mkdtemp( + path.join(os.tmpdir(), 'polykey-test-'), + ); + // Setting up clients clientFwdProxy1 = new ForwardProxy({ authToken: 'auth', logger, }); - clientFwdProxy2 = new ForwardProxy({ - authToken: 'auth', - logger, - }); - clientKeyManager1 = await KeyManager.createKeyManager({ keysPath: path.join(dataDir, 'clientKeys1'), password: 'password', logger, }); + nodeId1 = clientKeyManager1.getNodeId(); + await clientFwdProxy1.start({ + tlsConfig: { + keyPrivatePem: clientKeyManager1.getRootKeyPairPem().privateKey, + certChainPem: await clientKeyManager1.getRootCertChainPem(), + }, + egressHost: localHost, + proxyHost: localHost, + }); + clientWithProxies1 = await GRPCClientAgent.createGRPCClientAgent({ + host: localHost, + nodeId: keyManager.getNodeId(), + port: revProxy.getIngressPort(), + proxyConfig: { + host: clientFwdProxy1.getProxyHost(), + port: clientFwdProxy1.getProxyPort(), + authToken: clientFwdProxy1.authToken, + }, + timeout: 5000, + logger, + }); + + clientFwdProxy2 = new ForwardProxy({ + authToken: 'auth', + logger, + }); clientKeyManager2 = await KeyManager.createKeyManager({ keysPath: path.join(dataDir, 'clientKeys2'), password: 'password', logger, }); - const clientTlsConfig1: TLSConfig = { - keyPrivatePem: clientKeyManager1.getRootKeyPairPem().privateKey, - certChainPem: await clientKeyManager1.getRootCertChainPem(), - }; - const clientTlsConfig2: TLSConfig = { - keyPrivatePem: clientKeyManager2.getRootKeyPairPem().privateKey, - certChainPem: await clientKeyManager2.getRootCertChainPem(), - }; - await clientFwdProxy1.start({ - tlsConfig: clientTlsConfig1, - egressHost: host, - proxyHost: host, - }); + nodeId2 = clientKeyManager2.getNodeId(); await clientFwdProxy2.start({ - tlsConfig: clientTlsConfig2, - egressHost: host, - proxyHost: host, - }); - clientWithProxies1 = await testAgentUtils.openTestAgentClient( - revProxy.getIngressPort(), - clientKeyManager1.getNodeId(), - { - host: clientFwdProxy1.getProxyHost(), - port: clientFwdProxy1.getProxyPort(), - authToken: clientFwdProxy1.authToken, + tlsConfig: { + keyPrivatePem: clientKeyManager2.getRootKeyPairPem().privateKey, + certChainPem: await clientKeyManager2.getRootCertChainPem(), }, - ); - clientWithProxies2 = await testAgentUtils.openTestAgentClient( - revProxy.getIngressPort(), - clientKeyManager2.getNodeId(), - { + egressHost: localHost, + proxyHost: localHost, + }); + clientWithProxies2 = await GRPCClientAgent.createGRPCClientAgent({ + host: '127.0.0.1' as Host, + logger, + nodeId: keyManager.getNodeId(), + port: revProxy.getIngressPort(), + proxyConfig: { host: clientFwdProxy2.getProxyHost(), port: clientFwdProxy2.getProxyPort(), authToken: clientFwdProxy2.authToken, }, - ); - console.log(nodesUtils.encodeNodeId(clientKeyManager1.getNodeId()), nodesUtils.encodeNodeId(clientKeyManager2.getNodeId())) - }); + timeout: 5000, + }); + }, 26000); afterEach(async () => { await testAgentUtils.closeTestAgentClient(clientWithProxies1); await clientFwdProxy1.stop(); @@ -300,8 +304,8 @@ describe(GRPCClientAgent.name, () => { await testAgentUtils.closeTestAgentClient(clientWithProxies2); await clientFwdProxy2.stop(); await clientKeyManager2.stop(); - }); - test('connectionInfoGetter is called and returns the expected information', async () => { + }, 25000); + test('connectionInfoGetter returns correct information for each connection', async () => { // We can't directly spy on the connectionInfoGetter result // but we can check that it called `getConnectionInfoByProxy` properly const getConnectionInfoByProxySpy = jest.spyOn( @@ -311,12 +315,19 @@ describe(GRPCClientAgent.name, () => { await clientWithProxies1.echo(new utilsPB.EchoMessage()); await clientWithProxies2.echo(new utilsPB.EchoMessage()); // It should've returned the expected information - const returnedInfo = getConnectionInfoByProxySpy.mock.results[0].value; - expect(returnedInfo.ingressPort).toEqual(revProxy.getIngressPort()); - expect(returnedInfo.ingressHost).toEqual(host); - expect(returnedInfo.egressPort).toEqual(clientFwdProxy1.getEgressPort()); - expect(returnedInfo.egressHost).toEqual(host); - expect(returnedInfo.nodeId).toStrictEqual(clientKeyManager1.getNodeId()); - }); + const returnedInfo1 = getConnectionInfoByProxySpy.mock.results[0].value; + expect(returnedInfo1.ingressPort).toEqual(revProxy.getIngressPort()); + expect(returnedInfo1.ingressHost).toEqual(localHost); + expect(returnedInfo1.egressPort).toEqual(clientFwdProxy1.getEgressPort()); + expect(returnedInfo1.egressHost).toEqual(localHost); + expect(returnedInfo1.nodeId).toStrictEqual(nodeId1); + // Checking second call + const returnedInfo2 = getConnectionInfoByProxySpy.mock.results[1].value; + expect(returnedInfo2.ingressPort).toEqual(revProxy.getIngressPort()); + expect(returnedInfo2.ingressHost).toEqual(localHost); + expect(returnedInfo2.egressPort).toEqual(clientFwdProxy2.getEgressPort()); + expect(returnedInfo2.egressHost).toEqual(localHost); + expect(returnedInfo2.nodeId).toStrictEqual(nodeId2); + }, 100000); }); });