Skip to content

Commit 4fe0fb1

Browse files
committed
wip: fixing up PolykeyAgent usage
[ci skip]
1 parent 2eef18b commit 4fe0fb1

16 files changed

+416
-313
lines changed

src/PolykeyAgent.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,9 @@ class PolykeyAgent {
267267
keyRing ??
268268
(await KeyRing.createKeyRing({
269269
keysPath,
270+
recoveryCode: optionsDefaulted.keys.recoveryCode,
271+
privateKey: optionsDefaulted.keys.privateKey,
272+
privateKeyPath: optionsDefaulted.keys.privateKeyPath,
270273
passwordOpsLimit: optionsDefaulted.keys.passwordOpsLimit,
271274
passwordMemLimit: optionsDefaulted.keys.passwordMemLimit,
272275
strictMemoryLock: optionsDefaulted.keys.strictMemoryLock,
@@ -701,14 +704,16 @@ class PolykeyAgent {
701704
this.fs = fs;
702705
}
703706

707+
// TODO: add getters for runtime service information?
708+
704709
public async start({
705710
password,
706-
options,
711+
options = {},
707712
workers,
708713
fresh = false,
709714
}: {
710715
password: string;
711-
options: Partial<PolykeyAgentStartOptions>;
716+
options?: Partial<PolykeyAgentStartOptions>;
712717
workers?: number;
713718
fresh?: boolean;
714719
}) {

src/bootstrap/utils.ts

Lines changed: 44 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import type { PasswordMemLimit, PasswordOpsLimit } from '../keys/types';
44
import path from 'path';
55
import Logger from '@matrixai/logger';
66
import { DB } from '@matrixai/db';
7+
import { CertManager } from '@/keys';
78
import * as bootstrapErrors from './errors';
89
import TaskManager from '../tasks/TaskManager';
910
import { IdentitiesManager } from '../identities';
@@ -43,8 +44,6 @@ async function bootstrapState({
4344
// Required parameters
4445
password,
4546
// Optional configuration
46-
// nodePath = config.defaults.nodePath,
47-
// keyRingConfig = {},
4847
options = {},
4948
fresh = false,
5049
// Optional dependencies
@@ -53,30 +52,37 @@ async function bootstrapState({
5352
}: {
5453
password: string;
5554
options?: DeepPartial<BootstrapOptions>;
56-
// NodePath?: string;
57-
// keyRingConfig?: {
58-
// recoveryCode?: RecoveryCode;
59-
// privateKey?: PrivateKey;
60-
// privateKeyPath?: string;
61-
// passwordOpsLimit?: PasswordOpsLimit;
62-
// passwordMemLimit?: PasswordMemLimit;
63-
// };
6455
fresh?: boolean;
6556
fs?: FileSystem;
6657
logger?: Logger;
6758
}): Promise<RecoveryCode | undefined> {
6859
const umask = 0o077;
6960
logger.info(`Setting umask to ${umask.toString(8).padStart(3, '0')}`);
7061
process.umask(umask);
71-
logger.info(`Setting node path to ${nodePath}`);
72-
if (nodePath == null) {
62+
const optionsDefaulted = utils.mergeObjects(options, {
63+
nodePath: config.defaultsUser.nodePath,
64+
keys: {
65+
certDuration: config.defaultsUser.certDuration,
66+
},
67+
});
68+
logger.info(`Setting node path to ${optionsDefaulted.nodePath}`);
69+
if (optionsDefaulted.nodePath == null) {
7370
throw new errors.ErrorUtilsNodePath();
7471
}
75-
await mkdirExists(fs, nodePath);
72+
await mkdirExists(fs, optionsDefaulted.nodePath);
7673
// Setup node path and sub paths
77-
const statusPath = path.join(nodePath, config.paths.statusBase);
78-
const statusLockPath = path.join(nodePath, config.paths.statusLockBase);
79-
const statePath = path.join(nodePath, config.paths.stateBase);
74+
const statusPath = path.join(
75+
optionsDefaulted.nodePath,
76+
config.paths.statusBase,
77+
);
78+
const statusLockPath = path.join(
79+
optionsDefaulted.nodePath,
80+
config.paths.statusLockBase,
81+
);
82+
const statePath = path.join(
83+
optionsDefaulted.nodePath,
84+
config.paths.stateBase,
85+
);
8086
const dbPath = path.join(statePath, config.paths.dbBase);
8187
const keysPath = path.join(statePath, config.paths.keysBase);
8288
const vaultsPath = path.join(statePath, config.paths.vaultsBase);
@@ -90,7 +96,7 @@ async function bootstrapState({
9096
await status.start({ pid: process.pid });
9197
if (!fresh) {
9298
// Check the if number of directory entries is greater than 1 due to status.json and status.lock
93-
if ((await fs.promises.readdir(nodePath)).length > 2) {
99+
if ((await fs.promises.readdir(optionsDefaulted.nodePath)).length > 2) {
94100
throw new bootstrapErrors.ErrorBootstrapExistingState();
95101
}
96102
}
@@ -110,7 +116,12 @@ async function bootstrapState({
110116
fs,
111117
logger: logger.getChild(KeyRing.name),
112118
fresh,
113-
...keyRingConfig,
119+
recoveryCode: optionsDefaulted.recoveryCode,
120+
privateKey: optionsDefaulted.privateKey,
121+
privateKeyPath: optionsDefaulted.privateKeyPath,
122+
passwordOpsLimit: optionsDefaulted.passwordOpsLimit,
123+
passwordMemLimit: optionsDefaulted.passwordMemLimit,
124+
strictMemoryLock: optionsDefaulted.strictMemoryLock,
114125
});
115126
const db = await DB.createDB({
116127
dbPath,
@@ -135,6 +146,19 @@ async function bootstrapState({
135146
},
136147
fresh,
137148
});
149+
const taskManager = await TaskManager.createTaskManager({
150+
db,
151+
logger,
152+
lazy: true,
153+
});
154+
const certManager = await CertManager.createCertManager({
155+
keyRing,
156+
db,
157+
taskManager,
158+
fresh,
159+
logger,
160+
certDuration: optionsDefaulted.certDuration,
161+
});
138162
const sigchain = await Sigchain.createSigchain({
139163
db,
140164
keyRing,
@@ -166,11 +190,7 @@ async function bootstrapState({
166190
keyRing,
167191
logger: logger.getChild(NodeGraph.name),
168192
});
169-
const taskManager = await TaskManager.createTaskManager({
170-
db,
171-
logger,
172-
lazy: true,
173-
});
193+
174194
const nodeManager = new NodeManager({
175195
db,
176196
keyRing,
@@ -217,6 +237,7 @@ async function bootstrapState({
217237
await gestaltGraph.stop();
218238
await acl.stop();
219239
await sigchain.stop();
240+
await certManager.stop();
220241
await taskManager.stop();
221242
await db.stop();
222243
await keyRing.stop();

src/client/handlers/agentStatus.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ class AgentStatusHandler extends UnaryHandler<
2020
nodeIdEncoded: nodesUtils.encodeNodeId(pkAgent.keyRing.getNodeId()),
2121
clientHost: pkAgent.webSocketServerClient.getHost(),
2222
clientPort: pkAgent.webSocketServerClient.getPort(),
23-
agentHost: pkAgent.quicSocket.host,
24-
agentPort: pkAgent.quicSocket.port,
23+
agentHost: pkAgent.nodeConnectionManager.host,
24+
agentPort: pkAgent.nodeConnectionManager.port,
2525
publicKeyJwk: keysUtils.publicKeyToJWK(pkAgent.keyRing.keyPair.publicKey),
2626
certChainPEM: await pkAgent.certManager.getCertPEMsChainPEM(),
2727
};

tests/PolykeyAgent.test.ts

Lines changed: 92 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import * as keysUtils from '@/keys/utils/index';
1414

1515
describe('PolykeyAgent', () => {
1616
const password = 'password';
17+
const localhost = '127.0.0.1';
1718
const logger = new Logger('PolykeyAgent Test', LogLevel.WARN, [
1819
new StreamHandler(),
1920
]);
@@ -33,13 +34,17 @@ describe('PolykeyAgent', () => {
3334
const nodePath = path.join(dataDir, 'polykey');
3435
const pkAgent = await PolykeyAgent.createPolykeyAgent({
3536
password,
36-
nodePath,
37-
logger,
38-
keyRingConfig: {
39-
passwordOpsLimit: keysUtils.passwordOpsLimits.min,
40-
passwordMemLimit: keysUtils.passwordMemLimits.min,
41-
strictMemoryLock: false,
37+
options: {
38+
nodePath,
39+
agentServiceHost: localhost,
40+
clientServiceHost: localhost,
41+
keys: {
42+
passwordOpsLimit: keysUtils.passwordOpsLimits.min,
43+
passwordMemLimit: keysUtils.passwordMemLimits.min,
44+
strictMemoryLock: false,
45+
},
4246
},
47+
logger,
4348
});
4449
await expect(pkAgent.destroy(password)).rejects.toThrow(
4550
errors.ErrorPolykeyAgentRunning,
@@ -56,14 +61,18 @@ describe('PolykeyAgent', () => {
5661
const nodePath = `${dataDir}/polykey`;
5762
const pkAgent = await PolykeyAgent.createPolykeyAgent({
5863
password,
59-
nodePath,
60-
logger,
61-
keyRingConfig: {
62-
passwordOpsLimit: keysUtils.passwordOpsLimits.min,
63-
passwordMemLimit: keysUtils.passwordMemLimits.min,
64-
strictMemoryLock: false,
64+
options: {
65+
nodePath,
66+
workers: 0,
67+
agentServiceHost: localhost,
68+
clientServiceHost: localhost,
69+
keys: {
70+
passwordOpsLimit: keysUtils.passwordOpsLimits.min,
71+
passwordMemLimit: keysUtils.passwordMemLimits.min,
72+
strictMemoryLock: false,
73+
},
6574
},
66-
workers: 0,
75+
logger,
6776
});
6877
let nodePathContents = await fs.promises.readdir(nodePath);
6978
expect(nodePathContents).toContain(config.paths.statusBase);
@@ -96,13 +105,17 @@ describe('PolykeyAgent', () => {
96105
const statusLockPath = path.join(nodePath, config.paths.statusLockBase);
97106
const pkAgent = await PolykeyAgent.createPolykeyAgent({
98107
password,
99-
nodePath,
100-
logger,
101-
keyRingConfig: {
102-
passwordOpsLimit: keysUtils.passwordOpsLimits.min,
103-
passwordMemLimit: keysUtils.passwordMemLimits.min,
104-
strictMemoryLock: false,
108+
options: {
109+
nodePath,
110+
agentServiceHost: localhost,
111+
clientServiceHost: localhost,
112+
keys: {
113+
passwordOpsLimit: keysUtils.passwordOpsLimits.min,
114+
passwordMemLimit: keysUtils.passwordMemLimits.min,
115+
strictMemoryLock: false,
116+
},
105117
},
118+
logger,
106119
});
107120
const status = new Status({
108121
statusPath,
@@ -131,13 +144,17 @@ describe('PolykeyAgent', () => {
131144
});
132145
const pkAgent = await PolykeyAgent.createPolykeyAgent({
133146
password,
134-
nodePath,
135-
logger,
136-
keyRingConfig: {
137-
passwordOpsLimit: keysUtils.passwordOpsLimits.min,
138-
passwordMemLimit: keysUtils.passwordMemLimits.min,
139-
strictMemoryLock: false,
147+
options: {
148+
nodePath,
149+
agentServiceHost: localhost,
150+
clientServiceHost: localhost,
151+
keys: {
152+
passwordOpsLimit: keysUtils.passwordOpsLimits.min,
153+
passwordMemLimit: keysUtils.passwordMemLimits.min,
154+
strictMemoryLock: false,
155+
},
140156
},
157+
logger,
141158
});
142159
expect(await schema.readVersion()).toBe(config.stateVersion);
143160
await pkAgent.stop();
@@ -158,13 +175,17 @@ describe('PolykeyAgent', () => {
158175
await expect(
159176
PolykeyAgent.createPolykeyAgent({
160177
password,
161-
nodePath,
162-
logger,
163-
keyRingConfig: {
164-
passwordOpsLimit: keysUtils.passwordOpsLimits.min,
165-
passwordMemLimit: keysUtils.passwordMemLimits.min,
166-
strictMemoryLock: false,
178+
options: {
179+
nodePath,
180+
agentServiceHost: localhost,
181+
clientServiceHost: localhost,
182+
keys: {
183+
passwordOpsLimit: keysUtils.passwordOpsLimits.min,
184+
passwordMemLimit: keysUtils.passwordMemLimits.min,
185+
strictMemoryLock: false,
186+
},
167187
},
188+
logger,
168189
}),
169190
).rejects.toThrow(errors.ErrorSchemaVersionTooNew);
170191
// The 0 version will always be too old
@@ -179,13 +200,17 @@ describe('PolykeyAgent', () => {
179200
await expect(
180201
PolykeyAgent.createPolykeyAgent({
181202
password,
182-
nodePath,
183-
logger,
184-
keyRingConfig: {
185-
passwordOpsLimit: keysUtils.passwordOpsLimits.min,
186-
passwordMemLimit: keysUtils.passwordMemLimits.min,
187-
strictMemoryLock: false,
203+
options: {
204+
nodePath,
205+
agentServiceHost: localhost,
206+
clientServiceHost: localhost,
207+
keys: {
208+
passwordOpsLimit: keysUtils.passwordOpsLimits.min,
209+
passwordMemLimit: keysUtils.passwordMemLimits.min,
210+
strictMemoryLock: false,
211+
},
188212
},
213+
logger,
189214
}),
190215
).rejects.toThrow(errors.ErrorSchemaVersionTooOld);
191216
});
@@ -195,13 +220,17 @@ describe('PolykeyAgent', () => {
195220
try {
196221
pkAgent = await PolykeyAgent.createPolykeyAgent({
197222
password,
198-
nodePath,
199-
logger,
200-
keyRingConfig: {
201-
passwordOpsLimit: keysUtils.passwordOpsLimits.min,
202-
passwordMemLimit: keysUtils.passwordMemLimits.min,
203-
strictMemoryLock: false,
223+
options: {
224+
nodePath,
225+
agentServiceHost: localhost,
226+
clientServiceHost: localhost,
227+
keys: {
228+
passwordOpsLimit: keysUtils.passwordOpsLimits.min,
229+
passwordMemLimit: keysUtils.passwordMemLimits.min,
230+
strictMemoryLock: false,
231+
},
204232
},
233+
logger,
205234
});
206235
const prom = promise<CertManagerChangeData>();
207236
pkAgent.events.on(
@@ -224,13 +253,17 @@ describe('PolykeyAgent', () => {
224253
try {
225254
pkAgent = await PolykeyAgent.createPolykeyAgent({
226255
password,
227-
nodePath,
228-
logger,
229-
keyRingConfig: {
230-
passwordOpsLimit: keysUtils.passwordOpsLimits.min,
231-
passwordMemLimit: keysUtils.passwordMemLimits.min,
232-
strictMemoryLock: false,
256+
options: {
257+
nodePath,
258+
agentServiceHost: localhost,
259+
clientServiceHost: localhost,
260+
keys: {
261+
passwordOpsLimit: keysUtils.passwordOpsLimits.min,
262+
passwordMemLimit: keysUtils.passwordMemLimits.min,
263+
strictMemoryLock: false,
264+
},
233265
},
266+
logger,
234267
});
235268
const prom = promise<CertManagerChangeData>();
236269
pkAgent.events.on(
@@ -253,13 +286,17 @@ describe('PolykeyAgent', () => {
253286
try {
254287
pkAgent = await PolykeyAgent.createPolykeyAgent({
255288
password,
256-
nodePath,
257-
logger,
258-
keyRingConfig: {
259-
passwordOpsLimit: keysUtils.passwordOpsLimits.min,
260-
passwordMemLimit: keysUtils.passwordMemLimits.min,
261-
strictMemoryLock: false,
289+
options: {
290+
nodePath,
291+
agentServiceHost: localhost,
292+
clientServiceHost: localhost,
293+
keys: {
294+
passwordOpsLimit: keysUtils.passwordOpsLimits.min,
295+
passwordMemLimit: keysUtils.passwordMemLimits.min,
296+
strictMemoryLock: false,
297+
},
262298
},
299+
logger,
263300
});
264301
const prom = promise<CertManagerChangeData>();
265302
pkAgent.events.on(

0 commit comments

Comments
 (0)