Skip to content

Commit

Permalink
feat: encapsulates WebSocketClient into PolykeyClient
Browse files Browse the repository at this point in the history
* only `PolykeyAgentOptions` and `PolykeyClientOptions` should be used,
  all other subdomains should use flattened options
* updated all uses of subdomains to stop using `options`
* `PolykeyClient` encapsulates certificate verification behaviour
* `PolykeyClient` creation is now timed cancellable
* new utilities to deal with `NodeId` - `isNodeId`, `assertNodeId`, `generateNodeId`
  • Loading branch information
amydevs authored and CMCDragonkai committed Oct 21, 2023
1 parent 93cdef8 commit e57c36e
Show file tree
Hide file tree
Showing 44 changed files with 905 additions and 880 deletions.
88 changes: 57 additions & 31 deletions src/PolykeyAgent.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { DeepPartial, FileSystem } from './types';
import type { PolykeyWorkerManagerInterface } from './workers/types';
import type { TLSConfig } from './network/types';
import type { SeedNodes, NodesOptions } from './nodes/types';
import type { Key, KeysOptions } from './keys/types';
import type { SeedNodes } from './nodes/types';
import type { Key, PasswordOpsLimit, PasswordMemLimit } from './keys/types';
import path from 'path';
import process from 'process';
import Logger from '@matrixai/logger';
Expand Down Expand Up @@ -54,26 +54,30 @@ type PolykeyAgentOptions = {
seedNodes: SeedNodes;
workers: number;
ipv6Only: boolean;
keys: KeysOptions;
rpc: {
callTimeoutTime: number;
parserBufferSize: number;
keys: {
passwordOpsLimit: PasswordOpsLimit;
passwordMemLimit: PasswordMemLimit;
strictMemoryLock: boolean;
certDuration: number;
certRenewLeadTime: number;
recoveryCode: string;
};
client: {
connectTimeoutTime: number;
keepAliveTimeoutTime: number;
keepAliveIntervalTime: number;
rpcCallTimeoutTime: number;
rpcParserBufferSize: number;
};
nodes: {
connectionIdleTimeoutTime: number;
connectionFindConcurrencyLimit: number;
connectionConnectTimeoutTime: number;
connectionKeepAliveTimeoutTime: number;
connectionKeepAliveIntervalTime: number;
connectionHolePunchIntervalTime: number;
rpcCallTimeoutTime: number;
rpcParserBufferSize: number;
};
nodes: NodesOptions;
};

type PolykeyAgentStartOptions = {
clientServiceHost: string;
clientServicePort: number;
agentServiceHost: string;
agentServicePort: number;
ipv6Only: boolean;
workers: number;
};

interface PolykeyAgent extends CreateDestroyStartStop {}
Expand Down Expand Up @@ -113,9 +117,9 @@ class PolykeyAgent {
}: {
password: string;
options?: DeepPartial<PolykeyAgentOptions>;
fresh?: boolean;
fs?: FileSystem;
logger?: Logger;
fresh?: boolean;
}): Promise<PolykeyAgent> {
logger.info(`Creating ${this.name}`);
const umask = 0o077;
Expand All @@ -131,18 +135,18 @@ class PolykeyAgent {
workers: config.defaultsUser.workers,
ipv6Only: config.defaultsUser.ipv6Only,
keys: {
passwordOpsLimit: config.defaultsUser.passwordOpsLimit,
passwordMemLimit: config.defaultsUser.passwordMemLimit,
strictMemoryLock: config.defaultsUser.strictMemoryLock,
certDuration: config.defaultsUser.certDuration,
certRenewLeadTime: config.defaultsUser.certRenewLeadTime,
},
rpc: {
callTimeoutTime: config.defaultsSystem.rpcCallTimeoutTime,
parserBufferSize: config.defaultsSystem.rpcParserBufferSize,
},
client: {
connectTimoutTime: config.defaultsSystem.clientConnectTimeoutTime,
keepAliveTimeoutTime: config.defaultsSystem.clientKeepAliveTimeoutTime,
keepAliveIntervalTime:
config.defaultsSystem.clientKeepAliveIntervalTime,
rpcCallTimeoutTime: config.defaultsSystem.rpcCallTimeoutTime,
rpcParserBufferSize: config.defaultsSystem.rpcParserBufferSize,
},
nodes: {
connectionIdleTimeoutTime:
Expand All @@ -158,7 +162,7 @@ class PolykeyAgent {
connectionHolePunchIntervalTime:
config.defaultsSystem.nodesConnectionHolePunchIntervalTime,
},
});
}) as PolykeyAgentOptions;
// This can only happen if the caller didn't specify the node path and the
// automatic detection failed
if (optionsDefaulted.nodePath == null) {
Expand All @@ -181,7 +185,6 @@ class PolykeyAgent {
const dbPath = path.join(statePath, config.paths.dbBase);
const keysPath = path.join(statePath, config.paths.keysBase);
const vaultsPath = path.join(statePath, config.paths.vaultsBase);

let status: Status | undefined;
let schema: Schema | undefined;
let keyRing: KeyRing | undefined;
Expand Down Expand Up @@ -217,7 +220,9 @@ class PolykeyAgent {
});
keyRing = await KeyRing.createKeyRing({
keysPath,
options: optionsDefaulted.keys,
passwordOpsLimit: optionsDefaulted.keys.passwordOpsLimit,
passwordMemLimit: optionsDefaulted.keys.passwordMemLimit,
strictMemoryLock: optionsDefaulted.keys.strictMemoryLock,
fs,
fresh,
password,
Expand Down Expand Up @@ -256,7 +261,8 @@ class PolykeyAgent {
db,
keyRing,
taskManager,
options: optionsDefaulted.keys,
certDuration: optionsDefaulted.keys.certDuration,
certRenewLeadTime: optionsDefaulted.keys.certRenewLeadTime,
logger: logger.getChild(CertManager.name),
fresh,
});
Expand Down Expand Up @@ -310,7 +316,20 @@ class PolykeyAgent {
nodeGraph,
tlsConfig,
seedNodes: optionsDefaulted.seedNodes,
options: optionsDefaulted.nodes,
connectionFindConcurrencyLimit:
optionsDefaulted.nodes.connectionFindConcurrencyLimit,
connectionIdleTimeoutTime:
optionsDefaulted.nodes.connectionIdleTimeoutTime,
connectionConnectTimeoutTime:
optionsDefaulted.nodes.connectionConnectTimeoutTime,
connectionKeepAliveTimeoutTime:
optionsDefaulted.nodes.connectionKeepAliveTimeoutTime,
connectionKeepAliveIntervalTime:
optionsDefaulted.nodes.connectionKeepAliveIntervalTime,
connectionHolePunchIntervalTime:
optionsDefaulted.nodes.connectionHolePunchIntervalTime,
rpcParserBufferSize: optionsDefaulted.nodes.rpcParserBufferSize,
rpcCallTimeoutTime: optionsDefaulted.nodes.rpcCallTimeoutTime,
logger: logger.getChild(NodeConnectionManager.name),
});
nodeManager = new NodeManager({
Expand Down Expand Up @@ -387,8 +406,8 @@ class PolykeyAgent {
),
keepAliveTimeoutTime: optionsDefaulted.client.keepAliveTimeoutTime,
keepAliveIntervalTime: optionsDefaulted.client.keepAliveIntervalTime,
rpcCallTimeoutTime: optionsDefaulted.rpc.callTimeoutTime,
rpcParserBufferSize: optionsDefaulted.rpc.parserBufferSize,
rpcCallTimeoutTime: optionsDefaulted.client.rpcCallTimeoutTime,
rpcParserBufferSize: optionsDefaulted.client.rpcParserBufferSize,
logger: logger.getChild(ClientService.name),
});
} catch (e) {
Expand Down Expand Up @@ -585,7 +604,14 @@ class PolykeyAgent {
fresh = false,
}: {
password: string;
options?: Partial<PolykeyAgentStartOptions>;
options?: Partial<{
clientServiceHost: string;
clientServicePort: number;
agentServiceHost: string;
agentServicePort: number;
ipv6Only: boolean;
workers: number;
}>;
workers?: number;
fresh?: boolean;
}) {
Expand Down
Loading

0 comments on commit e57c36e

Please sign in to comment.