Skip to content

Commit e4f3273

Browse files
committed
wip: tests for NodeConnectionManager general
[ci skip]
1 parent 15ea1fc commit e4f3273

13 files changed

+241
-99
lines changed

src/PolykeyAgent.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@ import type { CertManagerChangeData, Key } from './keys/types';
66
import type { RecoveryCode, PrivateKey } from './keys/types';
77
import type { PasswordMemLimit, PasswordOpsLimit } from './keys/types';
88
import type { Crypto as QUICCrypto } from '@matrixai/quic';
9+
import type * as quicEvents from '@matrixai/quic/dist/events';
910
import path from 'path';
1011
import process from 'process';
12+
import { webcrypto } from 'crypto';
1113
import Logger from '@matrixai/logger';
1214
import { DB } from '@matrixai/db';
1315
import { CreateDestroyStartStop } from '@matrixai/async-init/dist/CreateDestroyStartStop';
@@ -44,8 +46,6 @@ import * as workersUtils from './workers/utils';
4446
import TaskManager from './tasks/TaskManager';
4547
import { serverManifest as clientServerManifest } from './client/handlers';
4648
import { serverManifest as agentServerManifest } from './agent/handlers';
47-
import { webcrypto } from "crypto";
48-
import * as quicEvents from "@matrixai/quic/dist/events";
4949

5050
// TODO: clean this up
5151
type NetworkConfig = {
@@ -568,7 +568,7 @@ class PolykeyAgent {
568568
},
569569
crypto: quicCrypto,
570570
logger: logger.getChild(QUICServer.name + 'Agent'),
571-
// keepaliveIntervalTime: 0, TODO
571+
// KeepaliveIntervalTime: 0, TODO
572572
// maxReadableStreamBytes: 0,
573573
// maxWritableStreamBytes: 0,
574574
socket: quicSocket,
@@ -890,7 +890,9 @@ class PolykeyAgent {
890890
ipv6Only: false, // TODO: hardcode this?
891891
});
892892
// Setting up stream handling
893-
const handleStream = async (event: quicEvents.QUICConnectionStreamEvent) => {
893+
const handleStream = async (
894+
event: quicEvents.QUICConnectionStreamEvent,
895+
) => {
894896
// Streams are handled via the RPCServer.
895897
const stream = event.detail;
896898
this.logger.info('!!!!Handling new stream!!!!!');
@@ -916,7 +918,10 @@ class PolykeyAgent {
916918
this.quicServerAgent.addEventListener(
917919
'stop',
918920
() => {
919-
this.quicServerAgent.removeEventListener('connection', handleConnection);
921+
this.quicServerAgent.removeEventListener(
922+
'connection',
923+
handleConnection,
924+
);
920925
},
921926
{ once: true },
922927
);

src/agent/handlers/nodesCrossSignClaim.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { AgentClaimMessage } from "./types";
1+
import type { AgentClaimMessage } from './types';
22
import type { AgentRPCRequestParams, AgentRPCResponseResult } from '../types';
33
import type { NodeId } from '../../ids';
44
import type ACL from '../../acl/ACL';
@@ -21,7 +21,7 @@ class NodesCrossSignClaimHandler extends DuplexHandler<
2121
_,
2222
meta,
2323
): AsyncGenerator<AgentRPCResponseResult<AgentClaimMessage>> {
24-
const { acl, nodeManager} = this.container;
24+
const { acl, nodeManager } = this.container;
2525
// TODO: get remote info from metadata. dependent on js-quic meta types
2626
const requestingNodeId: NodeId | undefined = nodesUtils.decodeNodeId(
2727
meta?.remoteNodeId,

src/agent/handlers/nodesHolePunchMessageSend.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ class NodesHolePunchMessageSendHandler extends UnaryHandler<
5353
sourceId: input.srcIdEncoded,
5454
},
5555
);
56+
// FIXME: Need a utility for getting the NodeId from the connection info.
57+
// We only have the remote certificates if that. Should throw if certs are missing but in practice this should
58+
// never happen given the custom verification logic checks this.
5659
const connectionInfo = meta;
5760
const srcNodeId = nodesUtils.encodeNodeId(connectionInfo!.remoteNodeId);
5861
// Firstly, check if this node is the desired node
@@ -67,11 +70,9 @@ class NodesHolePunchMessageSendHandler extends UnaryHandler<
6770
`Received signaling message to target ${input.srcIdEncoded}@${host}:${port}`,
6871
);
6972
// Ignore failure
70-
try {
71-
await nodeConnectionManager.holePunchReverse(host, port);
72-
} catch {
73-
// Do nothing
74-
}
73+
await nodeConnectionManager
74+
.holePunchReverse(host, port)
75+
.catch(() => {});
7576
} else {
7677
logger.error(
7778
'Received signaling message, target information was missing, skipping reverse hole punch',

src/client/handlers/agentStatus.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import type { ClientRPCRequestParams, ClientRPCResponseResult } from '../types';
22
import type { StatusResultMessage } from './types';
33
import type PolykeyAgent from '../../PolykeyAgent';
4+
import type { Host, Port } from '../../network/types';
45
import * as nodesUtils from '../../nodes/utils';
56
import * as keysUtils from '../../keys/utils';
67
import { UnaryHandler } from '../../rpc/handlers';
7-
import { Host, Port } from "@/network/types";
88

99
class AgentStatusHandler extends UnaryHandler<
1010
{

src/nodes/NodeConnectionManager.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import type NodeManager from './NodeManager';
1515
import type { PromiseCancellable } from '@matrixai/async-cancellable';
1616
import type { MultiLockRequest } from '@matrixai/async-locks/dist/types';
1717
import type { QUICClientConfig } from './types';
18+
import type { HolePunchRelayMessage } from '../agent/handlers/types';
1819
import { withF } from '@matrixai/resources';
1920
import Logger from '@matrixai/logger';
2021
import { ready, StartStop } from '@matrixai/async-init/dist/StartStop';
@@ -29,7 +30,6 @@ import * as validationUtils from '../validation/utils';
2930
import * as networkUtils from '../network/utils';
3031
import { never } from '../utils';
3132
import { clientManifest as agentClientManifest } from '../agent/handlers/clientManifest';
32-
import { HolePunchRelayMessage } from "../agent/handlers/types";
3333

3434
// TODO: check all locking and add cancellation for it.
3535

@@ -89,7 +89,7 @@ class NodeConnectionManager {
8989
string,
9090
{ lastAttempt: number; delay: number }
9191
> = new Map();
92-
protected backoffDefault: number = 300; // 5 min
92+
protected backoffDefault: number = 1000 * 60 * 5; // 5 min
9393
protected backoffMultiplier: number = 2; // Doubles every failure
9494
protected quicClientConfig: QUICClientConfig;
9595

@@ -1011,7 +1011,7 @@ class NodeConnectionManager {
10111011
public async establishMultiConnection(
10121012
nodeIds: Array<NodeId>,
10131013
addresses: Array<NodeAddress>,
1014-
connectionTimeout: number = 2000, // TODO: apply or remove
1014+
_connectionTimeout: number = 2000, // TODO: apply or remove
10151015
limit: number | undefined,
10161016
@context ctx: ContextTimed,
10171017
): Promise<Array<NodeId>> {

src/nodes/NodeManager.ts

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,23 @@ import type {
1212
} from './types';
1313
import type {
1414
Claim,
15-
ClaimId, ClaimIdEncoded,
15+
ClaimId,
16+
ClaimIdEncoded,
1617
SignedClaim,
17-
} from "../claims/types";
18+
} from '../claims/types';
1819
import type TaskManager from '../tasks/TaskManager';
1920
import type GestaltGraph from '../gestalts/GestaltGraph';
2021
import type { TaskHandler, TaskHandlerId, Task } from '../tasks/types';
2122
import type { ContextTimed } from '@matrixai/contexts';
2223
import type { PromiseCancellable } from '@matrixai/async-cancellable';
2324
import type { Host, Port } from '../network/types';
24-
import type {
25-
SignedTokenEncoded,
26-
TokenHeaderSignatureEncoded,
27-
TokenPayloadEncoded
28-
} from "../tokens/types";
25+
import type { SignedTokenEncoded } from '../tokens/types';
2926
import type { ClaimLinkNode } from '../claims/payloads/index';
30-
import type { ServerDuplexStream } from '@grpc/grpc-js';
31-
import type { AsyncGeneratorDuplexStream } from '../grpc/types';
27+
import type { AgentClaimMessage } from '../agent/handlers/types';
28+
import type {
29+
AgentRPCRequestParams,
30+
AgentRPCResponseResult,
31+
} from '../agent/types';
3232
import Logger from '@matrixai/logger';
3333
import { StartStop, ready } from '@matrixai/async-init/dist/StartStop';
3434
import { Semaphore, Lock } from '@matrixai/async-locks';
@@ -37,19 +37,17 @@ import { Timer } from '@matrixai/timer';
3737
import { timedCancellable, context } from '@matrixai/contexts/dist/decorators';
3838
import * as nodesErrors from './errors';
3939
import * as nodesUtils from './utils';
40+
import * as claimsUtils from '../claims/utils';
4041
import * as tasksErrors from '../tasks/errors';
4142
import * as claimsErrors from '../claims/errors';
4243
import * as keysUtils from '../keys/utils';
43-
import { never, promise } from "../utils/utils";
44+
import { never, promise } from '../utils/utils';
4445
import {
4546
decodeClaimId,
4647
encodeClaimId,
4748
parseSignedClaim,
4849
} from '../claims/utils';
4950
import Token from '../tokens/Token';
50-
import { AgentRPCRequestParams, AgentRPCResponseResult } from "@/agent/types";
51-
import { AgentClaimMessage } from "../agent/handlers/types";
52-
import * as claimsUtils from "@/claims/utils";
5351

5452
const abortEphemeralTaskReason = Symbol('abort ephemeral task reason');
5553
const abortSingletonTaskReason = Symbol('abort singleton task reason');
@@ -366,7 +364,8 @@ class NodeManager {
366364
const claims: Record<ClaimId, SignedClaim> = {};
367365
const client = connection.getClient();
368366
for await (const agentClaim of await client.methods.nodesChainDataGet({
369-
claimIdEncoded: claimId != null ? encodeClaimId(claimId) : "" as ClaimIdEncoded,
367+
claimIdEncoded:
368+
claimId != null ? encodeClaimId(claimId) : ('' as ClaimIdEncoded),
370369
})) {
371370
if (ctx.signal.aborted) throw ctx.signal.reason;
372371
// Need to re-construct each claim
@@ -448,7 +447,9 @@ class NodeManager {
448447
}
449448
const receivedClaim = readStatus.value;
450449
// We need to re-construct the token from the message
451-
const signedClaim = parseSignedClaim(receivedClaim.signedTokenEncoded);
450+
const signedClaim = parseSignedClaim(
451+
receivedClaim.signedTokenEncoded,
452+
);
452453
fullySignedToken = Token.fromSigned(signedClaim);
453454
// Check that the signatures are correct
454455
const targetNodePublicKey =
@@ -469,7 +470,9 @@ class NodeManager {
469470
}
470471
const receivedClaimRemote = readStatus2.value;
471472
// We need to re-construct the token from the message
472-
const signedClaimRemote = parseSignedClaim(receivedClaimRemote.signedTokenEncoded);
473+
const signedClaimRemote = parseSignedClaim(
474+
receivedClaimRemote.signedTokenEncoded,
475+
);
473476
// This is a singly signed claim,
474477
// we want to verify it before signing and sending back
475478
const signedTokenRemote = Token.fromSigned(signedClaimRemote);
@@ -478,8 +481,9 @@ class NodeManager {
478481
}
479482
signedTokenRemote.signWithPrivateKey(this.keyRing.keyPair);
480483
// 4. X <- responds with double signing the X signed claim <- Y
481-
const agentClaimedMessageRemote =
482-
claimsUtils.generateSignedClaim(signedTokenRemote.toSigned());
484+
const agentClaimedMessageRemote = claimsUtils.generateSignedClaim(
485+
signedTokenRemote.toSigned(),
486+
);
483487
await writer.write({
484488
signedTokenEncoded: agentClaimedMessageRemote,
485489
});
@@ -589,7 +593,7 @@ class NodeManager {
589593
);
590594
yield {
591595
signedTokenEncoded: await halfSignedClaimProm.p,
592-
}
596+
};
593597
const [, claim] = await claimProm;
594598
// With the claim created we want to add it to the gestalt graph
595599
const issNodeInfo = {

src/notifications/NotificationsManager.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import {
1414
import { utils as idUtils } from '@matrixai/id';
1515
import * as notificationsUtils from './utils';
1616
import * as notificationsErrors from './errors';
17-
import * as notificationsPB from '../proto/js/polykey/v1/notifications/notifications_pb';
1817
import * as nodesUtils from '../nodes/utils';
1918
import { never } from '../utils/utils';
2019

src/vaults/VaultInternal.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ import type {
1515
import type KeyRing from '../keys/KeyRing';
1616
import type { NodeId, NodeIdEncoded } from '../ids/types';
1717
import type NodeConnectionManager from '../nodes/NodeConnectionManager';
18-
import type { POJO } from '../types';
18+
import type RPCClient from '../rpc/RPCClient';
19+
import type { clientManifest as agentClientManifest } from '../agent/handlers/clientManifest';
1920
import path from 'path';
2021
import git from 'isomorphic-git';
21-
import * as grpc from '@grpc/grpc-js';
2222
import Logger from '@matrixai/logger';
2323
import {
2424
CreateDestroyStartStop,
@@ -30,11 +30,7 @@ import * as vaultsErrors from './errors';
3030
import * as vaultsUtils from './utils';
3131
import { tagLast } from './types';
3232
import * as nodesUtils from '../nodes/utils';
33-
import * as validationUtils from '../validation/utils';
34-
import * as vaultsPB from '../proto/js/polykey/v1/vaults/vaults_pb';
3533
import { never } from '../utils/utils';
36-
import RPCClient from "../rpc/RPCClient";
37-
import { clientManifest as agentClientManifest} from '../agent/handlers/clientManifest';
3834

3935
type RemoteInfo = {
4036
remoteNode: NodeIdEncoded;
@@ -765,7 +761,7 @@ class VaultInternal {
765761
_vaultAction: VaultAction,
766762
): Promise<any[]> {
767763
throw Error('TMP IMP');
768-
// const vaultNameOrId_ = typeof vaultNameOrId === 'string' ?
764+
// Const vaultNameOrId_ = typeof vaultNameOrId === 'string' ?
769765
// vaultNameOrId :
770766
// vaultsUtils.encodeVaultId(vaultNameOrId);
771767
// const response = client.methods.vaultsGitInfoGet({

src/vaults/VaultManager.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ import * as nodesUtils from '../nodes/utils';
4141
import * as keysUtils from '../keys/utils';
4242
import config from '../config';
4343
import { mkdirExists } from '../utils/utils';
44-
import * as utilsPB from '../proto/js/polykey/v1/utils/utils_pb';
4544

4645
/**
4746
* Object map pattern for each vault
@@ -866,8 +865,7 @@ class VaultManager {
866865
for await (const vault of genReadable) {
867866
const vaultName = vault.vaultName;
868867
const vaultIdEncoded = vault.vaultIdEncoded;
869-
const vaultPermissions =
870-
vault.vaultPermissions;
868+
const vaultPermissions = vault.vaultPermissions;
871869
yield { vaultName, vaultIdEncoded, vaultPermissions };
872870
}
873871
},

tests/nodes/NodeConnection.test.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import type { Host, Port, TLSConfig } from '@/network/types';
22
import type * as quicEvents from '@matrixai/quic/dist/events';
33
import type { NodeId, NodeIdEncoded } from '@/ids';
44
import type { Host as QUICHost, Crypto as QUICCrypto } from '@matrixai/quic';
5-
import { webcrypto } from 'crypto';
65
import { QUICServer, QUICSocket } from '@matrixai/quic';
76
import Logger, { formatting, LogLevel, StreamHandler } from '@matrixai/logger';
87
import { errors as quicErrors } from '@matrixai/quic';
@@ -13,8 +12,8 @@ import RPCServer from '@/rpc/RPCServer';
1312
import * as nodesErrors from '@/nodes/errors';
1413
import NodeConnection from '@/nodes/NodeConnection';
1514
import { promise } from '@/utils';
16-
import * as tlsUtils from '../utils/tls';
1715
import * as testNodesUtils from './utils';
16+
import * as tlsUtils from '../utils/tls';
1817

1918
describe(`${NodeConnection.name}`, () => {
2019
const logger = new Logger(`${NodeConnection.name} test`, LogLevel.WARN, [

0 commit comments

Comments
 (0)