Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing Polykey and all tests for release #562

Merged
merged 9 commits into from
Oct 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 17 additions & 17 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
"@matrixai/timer": "^1.1.1",
"@matrixai/workers": "^1.3.7",
"@matrixai/ws": "^1.1.4",
"@matrixai/quic": "^0.1.3",
"@matrixai/quic": "^0.1.4",
"@matrixai/events": "^3.2.0",
"@peculiar/asn1-pkcs8": "^2.3.0",
"@peculiar/asn1-schema": "^2.3.0",
Expand Down
8 changes: 5 additions & 3 deletions src/PolykeyAgent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -609,7 +609,6 @@ class PolykeyAgent {
public async start({
password,
options = {},
workers,
fresh = false,
}: {
password: string;
Expand Down Expand Up @@ -701,10 +700,13 @@ class PolykeyAgent {
await this.notificationsManager.start({ fresh });
await this.sessionManager.start({ fresh });
await this.taskManager.startProcessing();
if (workers != null) {
if (optionsDefaulted.workers != null) {
this.workerManager = await workersUtils.createWorkerManager({
// 0 means max workers
cores: workers === 0 ? undefined : workers,
cores:
optionsDefaulted.workers === 0
? undefined
: optionsDefaulted.workers,
logger: this.logger.getChild(WorkerManager.name),
});
this.vaultManager.setWorkerManager(this.workerManager);
Expand Down
11 changes: 6 additions & 5 deletions src/keys/KeyRing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import type {
} from './types';
import type { NodeId } from '../ids/types';
import type { PolykeyWorkerManagerInterface } from '../workers/types';
import type { FileSystem } from '../types';
import type { FileSystem, ObjectEmpty } from '../types';
import path from 'path';
import Logger from '@matrixai/logger';
import {
Expand Down Expand Up @@ -55,7 +55,7 @@ class KeyRing {
}: {
keysPath: string;
password: string;
options: Partial<KeyRingOptions>;
options?: Partial<KeyRingOptions>;
workerManager?: PolykeyWorkerManagerInterface;
fs?: FileSystem;
logger?: Logger;
Expand All @@ -73,7 +73,8 @@ class KeyRing {
fs,
logger,
});
await keyRing.start({ password, fresh });
// Spreading defaulted options to start to provide the keys overrides
await keyRing.start({ password, fresh, ...optionsDefaulted });
logger.info(`Created ${this.name}`);
return keyRing;
}
Expand Down Expand Up @@ -135,8 +136,8 @@ class KeyRing {
options: {
password: string;
fresh?: boolean;
} & ( // eslint-disable-next-line @typescript-eslint/ban-types
| {}
} & (
| ObjectEmpty
| { recoveryCode: RecoveryCode }
| { privateKey: PrivateKey }
| { privateKeyPath: string }
Expand Down
5 changes: 0 additions & 5 deletions src/keys/utils/webcrypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,6 @@ import * as utils from '../../utils';
*/
const webcrypto = new peculiarWebcrypto.Crypto();

/**
* Monkey patches the global crypto object polyfill.
*/
globalThis.crypto = webcrypto;

/**
* Imports Ed25519 public `CryptoKey` from key buffer.
* If `publicKey` is already `CryptoKey`, then this just returns it.
Expand Down
2 changes: 1 addition & 1 deletion src/nodes/NodeConnectionManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1543,10 +1543,10 @@ class NodeConnectionManager {
},
ctx,
);
return true;
} catch {
return false;
}
return true;
}

/**
Expand Down
6 changes: 5 additions & 1 deletion src/nodes/NodeManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ import {
parseSignedClaim,
} from '../claims/utils';
import Token from '../tokens/Token';
import config from '../config';

const abortEphemeralTaskReason = Symbol('abort ephemeral task reason');
const abortSingletonTaskReason = Symbol('abort singleton task reason');
Expand Down Expand Up @@ -226,6 +227,8 @@ class NodeManager {
refreshBucketDelay = 3600000, // 1 hour in milliseconds
refreshBucketDelayJitter = 0.5, // Multiple of refreshBucketDelay to jitter by
retrySeedConnectionsDelay = 120000, // 2 minuets
connectionConnectTimeoutTime = config.defaultsSystem
.nodesConnectionConnectTimeoutTime,
logger,
}: {
db: DB;
Expand All @@ -238,7 +241,7 @@ class NodeManager {
refreshBucketDelay?: number;
refreshBucketDelayJitter?: number;
retrySeedConnectionsDelay?: number;
longTaskTimeout?: number;
connectionConnectTimeoutTime?: number;
logger?: Logger;
}) {
this.logger = logger ?? new Logger(this.constructor.name);
Expand All @@ -256,6 +259,7 @@ class NodeManager {
Math.min(refreshBucketDelayJitter, 1),
);
this.retrySeedConnectionsDelay = retrySeedConnectionsDelay;
this.connectionConnectTimeoutTime = connectionConnectTimeoutTime;
}

public async start() {
Expand Down
25 changes: 16 additions & 9 deletions tests/PolykeyAgent.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import * as errors from '@/errors';
import config from '@/config';
import { promise } from '@/utils/index';
import * as keysUtils from '@/keys/utils/index';
import * as keysEvents from '@/keys/events';

describe('PolykeyAgent', () => {
const password = 'password';
Expand Down Expand Up @@ -233,11 +234,13 @@ describe('PolykeyAgent', () => {
logger,
});
const prom = promise<CertManagerChangeData>();
pkAgent.events.on(
PolykeyAgent.eventSymbols.CertManager,
async (data: CertManagerChangeData) => {
pkAgent.certManager.addEventListener(
keysEvents.EventCertManagerCertChange.name,
async (evt: keysEvents.EventCertManagerCertChange) => {
const data = evt.detail;
prom.resolveP(data);
},
{ once: true },
);
await pkAgent.certManager.renewCertWithNewKeyPair(password);

Expand Down Expand Up @@ -266,11 +269,13 @@ describe('PolykeyAgent', () => {
logger,
});
const prom = promise<CertManagerChangeData>();
pkAgent.events.on(
PolykeyAgent.eventSymbols.CertManager,
async (data: CertManagerChangeData) => {
pkAgent.certManager.addEventListener(
keysEvents.EventCertManagerCertChange.name,
async (evt: keysEvents.EventCertManagerCertChange) => {
const data = evt.detail;
prom.resolveP(data);
},
{ once: true },
);
await pkAgent.certManager.resetCertWithNewKeyPair(password);

Expand Down Expand Up @@ -299,11 +304,13 @@ describe('PolykeyAgent', () => {
logger,
});
const prom = promise<CertManagerChangeData>();
pkAgent.events.on(
PolykeyAgent.eventSymbols.CertManager,
async (data: CertManagerChangeData) => {
pkAgent.certManager.addEventListener(
keysEvents.EventCertManagerCertChange.name,
async (evt: keysEvents.EventCertManagerCertChange) => {
const data = evt.detail;
prom.resolveP(data);
},
{ once: true },
);
await pkAgent.certManager.resetCertWithCurrentKeyPair();

Expand Down
10 changes: 6 additions & 4 deletions tests/PolykeyClient.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { PolykeyClient, PolykeyAgent } from '@';
import { Session } from '@/sessions';
import config from '@/config';
import * as keysUtils from '@/keys/utils/index';
import WebSocketClient from '@/websockets/WebSocketClient';
import { WebSocketClient } from '@matrixai/ws';

describe('PolykeyClient', () => {
const password = 'password';
Expand Down Expand Up @@ -54,13 +54,15 @@ describe('PolykeyClient', () => {
await session.writeToken('dummy' as SessionToken);
// Using fresh: true means that any token would be destroyed
const webSocketClient = await WebSocketClient.createWebSocketClient({
expectedNodeIds: [pkAgent.keyRing.getNodeId()],
config: {
verifyPeer: false,
},
host: pkAgent.clientServiceHost,
port: pkAgent.clientServicePort,
logger,
});
const pkClient = await PolykeyClient.createPolykeyClient({
streamFactory: (ctx) => webSocketClient.startConnection(ctx),
streamFactory: () => webSocketClient.connection.newStream(),
nodePath,
fs,
logger,
Expand All @@ -72,6 +74,6 @@ describe('PolykeyClient', () => {
expect(await session.readToken()).toBeDefined();
await pkClient.destroy();
expect(await session.readToken()).toBeUndefined();
await webSocketClient.destroy(true);
await webSocketClient.destroy({ force: true });
});
});
18 changes: 18 additions & 0 deletions tests/client/handlers/keys.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ import PolykeyAgent from '@/PolykeyAgent';
import { NodeManager } from '@/nodes';
import { publicKeyToJWK } from '@/keys/utils';
import ClientService from '@/client/ClientService';
import { sleep } from '@/utils';
import * as keysEvents from '@/keys/events';
import * as testsUtils from '../../utils';

describe('keysCertsChainGet', () => {
Expand Down Expand Up @@ -570,10 +572,18 @@ describe('keysKeyPairRenew', () => {
expect(mockedRefreshBuckets).toHaveBeenCalledTimes(0);
expect(fwdTLSConfig1).toEqual(expectedTLSConfig1);
expect(nodeId1.equals(nodeIdStatus1)).toBe(true);
const certChangeEventProm = testsUtils.promFromEvent(
pkAgent.certManager,
keysEvents.EventCertManagerCertChange,
);
// Run command
await rpcClient.methods.keysKeyPairRenew({
password: 'somepassphrase',
});
// Awaiting change to propagate
await certChangeEventProm.p;
// Wait some time after event for domains to update
await sleep(500);
const rootKeyPair2 = pkAgent.keyRing.keyPair;
const nodeId2 = pkAgent.keyRing.getNodeId();
// @ts-ignore - get protected property
Expand Down Expand Up @@ -690,10 +700,18 @@ describe('keysKeyPairReset', () => {
expect(mockedRefreshBuckets).not.toHaveBeenCalled();
expect(fwdTLSConfig1).toEqual(expectedTLSConfig1);
expect(nodeId1.equals(nodeIdStatus1)).toBe(true);
const certChangeEventProm = testsUtils.promFromEvent(
pkAgent.certManager,
keysEvents.EventCertManagerCertChange,
);
// Run command
await rpcClient.methods.keysKeyPairReset({
password: 'somepassphrase',
});
// Awaiting change to propagate
await certChangeEventProm.p;
// Wait some time after event for domains to update
await sleep(500);
const rootKeyPair2 = pkAgent.keyRing.keyPair;
const nodeId2 = pkAgent.keyRing.getNodeId();
// @ts-ignore - get protected property
Expand Down
2 changes: 1 addition & 1 deletion tests/discovery/Discovery.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,12 @@ describe('Discovery', () => {
keyRing = await KeyRing.createKeyRing({
password,
keysPath,
logger: logger.getChild('KeyRing'),
options: {
passwordOpsLimit: keysUtils.passwordOpsLimits.min,
passwordMemLimit: keysUtils.passwordMemLimits.min,
strictMemoryLock: false,
},
logger: logger.getChild('KeyRing'),
});
const dbPath = path.join(dataDir, 'db');
db = await DB.createDB({
Expand Down
8 changes: 5 additions & 3 deletions tests/identities/IdentitiesManager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -329,11 +329,13 @@ describe('IdentitiesManager', () => {
const keyRing = await KeyRing.createKeyRing({
password: 'password',
keysPath: path.join(dataDir, 'keys'),
options: {
strictMemoryLock: false,
passwordOpsLimit: keysUtils.passwordOpsLimits.min,
passwordMemLimit: keysUtils.passwordMemLimits.min,
},
logger,
fresh: true,
strictMemoryLock: false,
passwordOpsLimit: keysUtils.passwordOpsLimits.min,
passwordMemLimit: keysUtils.passwordMemLimits.min,
});
const sigchain = await Sigchain.createSigchain({
db,
Expand Down
10 changes: 6 additions & 4 deletions tests/keys/CertManager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,13 @@ describe(CertManager.name, () => {
keyRing = await KeyRing.createKeyRing({
keysPath,
password,
privateKey,
options: {
privateKey,
passwordOpsLimit: keysUtils.passwordOpsLimits.min,
passwordMemLimit: keysUtils.passwordMemLimits.min,
strictMemoryLock: false,
},
logger,
passwordOpsLimit: keysUtils.passwordOpsLimits.min,
passwordMemLimit: keysUtils.passwordMemLimits.min,
strictMemoryLock: false,
});
dbPath = `${dataDir}/db`;
db = await DB.createDB({
Expand Down
Loading