diff --git a/src/PolykeyAgent.ts b/src/PolykeyAgent.ts index 83fe9072e..997010d21 100644 --- a/src/PolykeyAgent.ts +++ b/src/PolykeyAgent.ts @@ -700,10 +700,10 @@ class PolykeyAgent { await this.notificationsManager?.stop(); await this.vaultManager?.stop(); await this.discovery?.stop(); - await this.taskManager?.stop(); await this.nodeGraph?.stop(); await this.nodeConnectionManager?.stop(); await this.nodeManager?.stop(); + await this.taskManager?.stop(); await this.proxy?.stop(); await this.grpcServerAgent?.stop(); await this.grpcServerClient?.stop(); diff --git a/src/nodes/NodeConnectionManager.ts b/src/nodes/NodeConnectionManager.ts index f5a67eeee..9861e2445 100644 --- a/src/nodes/NodeConnectionManager.ts +++ b/src/nodes/NodeConnectionManager.ts @@ -29,7 +29,7 @@ import GRPCClientAgent from '../agent/GRPCClientAgent'; import * as validationUtils from '../validation/utils'; import * as networkUtils from '../network/utils'; import * as nodesPB from '../proto/js/polykey/v1/nodes/nodes_pb'; -import { timerStart } from '../utils'; +import { timerStart, never } from '../utils'; type ConnectionAndTimer = { connection: NodeConnection; @@ -119,7 +119,8 @@ class NodeConnectionManager { this.nodeManager = nodeManager; // Adding seed nodes for (const nodeIdEncoded in this.seedNodes) { - const nodeId = nodesUtils.decodeNodeId(nodeIdEncoded)!; + const nodeId = nodesUtils.decodeNodeId(nodeIdEncoded); + if (nodeId == null) never(); await this.nodeManager.setNode( nodeId, this.seedNodes[nodeIdEncoded], @@ -224,7 +225,8 @@ class NodeConnectionManager { const [release, conn] = await acquire(); let caughtError; try { - return yield* g(conn!); + if (conn == null) never(); + return yield* g(conn); } catch (e) { caughtError = e; throw e; @@ -701,9 +703,11 @@ class NodeConnectionManager { */ @ready(new nodesErrors.ErrorNodeConnectionManagerNotRunning()) public getSeedNodes(): Array { - return Object.keys(this.seedNodes).map( - (nodeIdEncoded) => nodesUtils.decodeNodeId(nodeIdEncoded)!, - ); + return Object.keys(this.seedNodes).map((nodeIdEncoded) => { + const nodeId = nodesUtils.decodeNodeId(nodeIdEncoded); + if (nodeId == null) never(); + return nodeId; + }); } /** diff --git a/src/nodes/NodeManager.ts b/src/nodes/NodeManager.ts index 82d7eebff..7d313f62c 100644 --- a/src/nodes/NodeManager.ts +++ b/src/nodes/NodeManager.ts @@ -92,7 +92,13 @@ class NodeManager { host: Host, port: Port, ) => { - const nodeId = nodesUtils.decodeNodeId(nodeIdEncoded)!; + const nodeId = nodesUtils.decodeNodeId(nodeIdEncoded); + if (nodeId == null) { + this.logger.error( + `pingAndSetNodeHandler received invalid NodeId: ${nodeIdEncoded}`, + ); + never(); + } const host_ = await networkUtils.resolveHost(host); if ( await this.pingNode(nodeId, { host: host_, port }, { signal: ctx.signal }) @@ -569,7 +575,8 @@ class NodeManager { // We just add the new node anyway without checking the old one const oldNodeId = ( await this.nodeGraph.getOldestNode(bucketIndex, 1, tran) - ).pop()!; + ).pop(); + if (oldNodeId == null) never(); this.logger.debug( `Force was set, removing ${nodesUtils.encodeNodeId( oldNodeId, @@ -1013,10 +1020,13 @@ class NodeManager { } } // Refreshing every bucket above the closest node - let closestNodeInfo = closestNodes.pop()!; - if (this.keyManager.getNodeId().equals(closestNodeInfo[0])) { + let closestNodeInfo = closestNodes.pop(); + if ( + closestNodeInfo != null && + this.keyManager.getNodeId().equals(closestNodeInfo[0]) + ) { // Skip our nodeId if it exists - closestNodeInfo = closestNodes.pop()!; + closestNodeInfo = closestNodes.pop(); } let index = this.nodeGraph.nodeIdBits; if (closestNodeInfo != null) { diff --git a/tests/discovery/Discovery.test.ts b/tests/discovery/Discovery.test.ts index 3a5ebf34e..f99c45ee9 100644 --- a/tests/discovery/Discovery.test.ts +++ b/tests/discovery/Discovery.test.ts @@ -22,6 +22,7 @@ import * as nodesUtils from '@/nodes/utils'; import * as claimsUtils from '@/claims/utils'; import * as discoveryErrors from '@/discovery/errors'; import * as keysUtils from '@/keys/utils'; +import * as grpcUtils from '@/grpc/utils/index'; import * as testNodesUtils from '../nodes/utils'; import TestProvider from '../identities/TestProvider'; import { globalRootKeyPems } from '../fixtures/globalRootKeyPems'; @@ -59,6 +60,8 @@ describe('Discovery', () => { let nodeB: PolykeyAgent; let identityId: IdentityId; beforeEach(async () => { + // Sets the global GRPC logger to the logger + grpcUtils.setLogger(logger); dataDir = await fs.promises.mkdtemp( path.join(os.tmpdir(), 'polykey-test-'), );