Skip to content

Commit e630109

Browse files
authored
Merge pull request #296 from MatrixAI/bin-test-fixes
Session Bin Test Fixes
2 parents 8804b28 + ea8493f commit e630109

102 files changed

Lines changed: 2143 additions & 2088 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

package-lock.json

Lines changed: 50 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@
6565
"lintfix": "eslint '{src,tests}/**/*.{js,ts}' --fix",
6666
"docs": "rm -r ./docs || true; typedoc --gitRevision master --tsconfig ./tsconfig.build.json --out ./docs src && touch ./docs/.nojekyll",
6767
"proto-generate": "scripts/proto-generate.sh",
68-
"polykey": "ts-node --require tsconfig-paths/register --transpile-only src/bin/polykey.ts"
68+
"polykey": "ts-node --require tsconfig-paths/register --compiler typescript-cached-transpile --transpile-only src/bin/polykey.ts"
6969
},
7070
"dependencies": {
7171
"@grpc/grpc-js": "1.3.7",
@@ -95,6 +95,7 @@
9595
"pako": "^1.0.11",
9696
"prompts": "^2.4.1",
9797
"readable-stream": "^3.6.0",
98+
"resource-counter": "^1.2.4",
9899
"threads": "^1.6.5",
99100
"ts-custom-error": "^3.2.0",
100101
"utp-native": "^2.5.3",
@@ -130,6 +131,7 @@
130131
"ts-node": "^10.4.0",
131132
"tsconfig-paths": "^3.9.0",
132133
"typedoc": "^0.21.5",
133-
"typescript": "^4.1.3"
134+
"typescript": "^4.1.3",
135+
"typescript-cached-transpile": "0.0.6"
134136
}
135137
}

src/agent/GRPCClientAgent.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,17 @@ class GRPCClientAgent extends GRPCClient<AgentServiceClient> {
3838
logger?: Logger;
3939
}): Promise<GRPCClientAgent> {
4040
logger.info(`Creating ${this.name}`);
41-
const { client, serverCertChain } = await super.createClient({
42-
clientConstructor: AgentServiceClient,
43-
nodeId,
44-
host,
45-
port,
46-
tlsConfig,
47-
proxyConfig,
48-
timeout,
49-
logger,
50-
});
41+
const { client, serverCertChain, flowCountInterceptor } =
42+
await super.createClient({
43+
clientConstructor: AgentServiceClient,
44+
nodeId,
45+
host,
46+
port,
47+
tlsConfig,
48+
proxyConfig,
49+
timeout,
50+
logger,
51+
});
5152
const grpcClientAgent = new GRPCClientAgent({
5253
client,
5354
nodeId,
@@ -56,6 +57,7 @@ class GRPCClientAgent extends GRPCClient<AgentServiceClient> {
5657
tlsConfig,
5758
proxyConfig,
5859
serverCertChain,
60+
flowCountInterceptor,
5961
logger,
6062
});
6163
logger.info(`Created ${this.name}`);

src/bin/agent/CommandLock.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ class CommandLock extends CommandPolykey {
99
this.description('Lock the Client and Clear the Existing Token');
1010
this.action(async (options) => {
1111
const { default: Session } = await import('../../sessions/Session');
12-
// Just delete the session token
1312
const session = new Session({
1413
sessionTokenPath: path.join(
1514
options.nodePath,
@@ -18,6 +17,7 @@ class CommandLock extends CommandPolykey {
1817
fs: this.fs,
1918
logger: this.logger.getChild(Session.name),
2019
});
20+
// Destroy local session
2121
await session.destroy();
2222
});
2323
}

src/bin/agent/CommandLockAll.ts

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import type PolykeyClient from '../../PolykeyClient';
2+
import path from 'path';
23
import CommandPolykey from '../CommandPolykey';
34
import * as binUtils from '../utils';
45
import * as binOptions from '../utils/options';
56
import * as binProcessors from '../utils/processors';
7+
import config from '../../config';
68

79
class CommandLockAll extends CommandPolykey {
810
constructor(...args: ConstructorParameters<typeof CommandPolykey>) {
@@ -14,7 +16,9 @@ class CommandLockAll extends CommandPolykey {
1416
this.addOption(binOptions.clientPort);
1517
this.action(async (options) => {
1618
const { default: PolykeyClient } = await import('../../PolykeyClient');
19+
const { default: Session } = await import('../../sessions/Session');
1720
const utilsPB = await import('../../proto/js/polykey/v1/utils/utils_pb');
21+
1822
const clientOptions = await binProcessors.processClientOptions(
1923
options.nodePath,
2024
options.nodeId,
@@ -23,7 +27,19 @@ class CommandLockAll extends CommandPolykey {
2327
this.fs,
2428
this.logger.getChild(binProcessors.processClientOptions.name),
2529
);
26-
let pkClient: PolykeyClient | undefined;
30+
const meta = await binProcessors.processAuthentication(
31+
options.passwordFile,
32+
this.fs,
33+
);
34+
const session = new Session({
35+
sessionTokenPath: path.join(
36+
options.nodePath,
37+
config.defaults.tokenBase,
38+
),
39+
fs: this.fs,
40+
logger: this.logger.getChild(Session.name),
41+
});
42+
let pkClient: PolykeyClient;
2743
this.exitHandlers.handlers.push(async () => {
2844
if (pkClient != null) await pkClient.stop();
2945
});
@@ -35,18 +51,15 @@ class CommandLockAll extends CommandPolykey {
3551
nodePath: options.nodePath,
3652
logger: this.logger.getChild(PolykeyClient.name),
3753
});
38-
const meta = await binProcessors.processAuthentication(
39-
options.passwordFile,
40-
this.fs,
41-
);
42-
const grpcClient = pkClient.grpcClient;
4354
const emptyMessage = new utilsPB.EmptyMessage();
4455
await binUtils.retryAuthentication(
45-
(auth) => grpcClient.sessionsLockAll(emptyMessage, auth),
56+
(auth) => pkClient.grpcClient.sessionsLockAll(emptyMessage, auth),
4657
meta,
4758
);
59+
// Destroy local session
60+
await session.destroy();
4861
} finally {
49-
if (pkClient != null) await pkClient.stop();
62+
if (pkClient! != null) await pkClient.stop();
5063
}
5164
});
5265
}

src/bin/agent/CommandStart.ts

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import CommandPolykey from '../CommandPolykey';
99
import * as binOptions from '../utils/options';
1010
import * as binProcessors from '../utils/processors';
1111
import * as binErrors from '../errors';
12-
import { promise } from '../../utils';
12+
import { promise, dirEmpty } from '../../utils';
1313
import config from '../../config';
1414

1515
class CommandStart extends CommandPolykey {
@@ -33,12 +33,35 @@ class CommandStart extends CommandPolykey {
3333
options.clientPort =
3434
options.clientPort ?? config.defaults.networkConfig.clientPort;
3535
const { default: PolykeyAgent } = await import('../../PolykeyAgent');
36-
// Password is necessary
37-
// If recovery code is supplied, then this is the new password
38-
const password = await binProcessors.processPassword(
39-
options.passwordFile,
40-
this.fs,
41-
);
36+
let password: string | undefined;
37+
if (options.fresh) {
38+
// If fresh, then get a new password
39+
password = await binProcessors.processNewPassword(
40+
options.passwordFile,
41+
this.fs,
42+
);
43+
} else if (options.recoveryCodeFile != null) {
44+
// If recovery code is supplied, then this is the new password
45+
password = await binProcessors.processNewPassword(
46+
options.passwordFile,
47+
this.fs,
48+
);
49+
} else if (await dirEmpty(this.fs, options.nodePath)) {
50+
// If the node path is empty, get a new password
51+
password = await binProcessors.processNewPassword(
52+
options.passwordFile,
53+
this.fs,
54+
);
55+
} else {
56+
// Otherwise this is the existing password
57+
// however, the code is capable of doing partial bootstrapping
58+
// so it's possible that this is also a new password
59+
// if the root key isn't setup
60+
password = await binProcessors.processPassword(
61+
options.passwordFile,
62+
this.fs,
63+
);
64+
}
4265
const recoveryCodeIn = await binProcessors.processRecoveryCode(
4366
options.recoveryCodeFile,
4467
this.fs,

src/bin/agent/CommandStatus.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ class CommandStatus extends CommandPolykey {
3838
}),
3939
);
4040
} else {
41+
const meta = await binProcessors.processAuthentication(
42+
options.passwordFile,
43+
this.fs,
44+
);
4145
let pkClient: PolykeyClient;
4246
this.exitHandlers.handlers.push(async () => {
4347
if (pkClient != null) await pkClient.stop();
@@ -51,10 +55,6 @@ class CommandStatus extends CommandPolykey {
5155
nodePath: options.nodePath,
5256
logger: this.logger.getChild(PolykeyClient.name),
5357
});
54-
const meta = await binProcessors.processAuthentication(
55-
options.passwordFile,
56-
this.fs,
57-
);
5858
const emptyMessage = new utilsPB.EmptyMessage();
5959
response = await binUtils.retryAuthentication(
6060
(auth) => pkClient.grpcClient.agentStatus(emptyMessage, auth),

src/bin/agent/CommandStop.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ class CommandStop extends CommandPolykey {
3434
} else if (statusInfo?.status === 'STARTING') {
3535
throw new binErrors.ErrorCLIStatusStarting();
3636
}
37+
const meta = await binProcessors.processAuthentication(
38+
options.passwordFile,
39+
this.fs,
40+
);
3741
// Either the statusInfo is undefined or LIVE
3842
// Either way, the connection parameters now exist
3943
let pkClient: PolykeyClient;
@@ -48,14 +52,9 @@ class CommandStop extends CommandPolykey {
4852
port: clientStatus.clientPort!,
4953
logger: this.logger.getChild(PolykeyClient.name),
5054
});
51-
const meta = await binProcessors.processAuthentication(
52-
options.passwordFile,
53-
this.fs,
54-
);
5555
const emptyMessage = new utilsPB.EmptyMessage();
56-
const grpcClient = pkClient.grpcClient;
5756
await binUtils.retryAuthentication(
58-
(auth) => grpcClient.agentStop(emptyMessage, auth),
57+
(auth) => pkClient.grpcClient.agentStop(emptyMessage, auth),
5958
meta,
6059
);
6160
this.logger.info('Stopping Agent');

src/bin/agent/CommandUnlock.ts

Lines changed: 12 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
import type { SessionToken } from '../../sessions/types';
2-
import type { Metadata } from '@grpc/grpc-js';
3-
41
import type PolykeyClient from '../../PolykeyClient';
52
import CommandPolykey from '../CommandPolykey';
63
import * as binUtils from '../utils';
@@ -17,10 +14,7 @@ class CommandUnlock extends CommandPolykey {
1714
this.addOption(binOptions.clientPort);
1815
this.action(async (options) => {
1916
const { default: PolykeyClient } = await import('../../PolykeyClient');
20-
const sessionsPB = await import(
21-
'../../proto/js/polykey/v1/sessions/sessions_pb'
22-
);
23-
17+
const utilsPB = await import('../../proto/js/polykey/v1/utils/utils_pb');
2418
const clientOptions = await binProcessors.processClientOptions(
2519
options.nodePath,
2620
options.nodeId,
@@ -29,41 +23,29 @@ class CommandUnlock extends CommandPolykey {
2923
this.fs,
3024
this.logger.getChild(binProcessors.processClientOptions.name),
3125
);
32-
33-
let pkClient: PolykeyClient | undefined;
26+
const meta = await binProcessors.processAuthentication(
27+
options.passwordFile,
28+
this.fs,
29+
);
30+
let pkClient: PolykeyClient;
3431
this.exitHandlers.handlers.push(async () => {
3532
if (pkClient != null) await pkClient.stop();
3633
});
3734
try {
3835
pkClient = await PolykeyClient.createPolykeyClient({
39-
nodePath: options.nodePath,
4036
nodeId: clientOptions.nodeId,
4137
host: clientOptions.clientHost,
4238
port: clientOptions.clientPort,
39+
nodePath: options.nodePath,
4340
logger: this.logger.getChild(PolykeyClient.name),
4441
});
45-
46-
const password = await binProcessors.processPassword(
47-
options.passwordFile,
48-
this.fs,
42+
const emptyMessage = new utilsPB.EmptyMessage();
43+
await binUtils.retryAuthentication(
44+
(auth) => pkClient.grpcClient.sessionsUnlock(emptyMessage, auth),
45+
meta,
4946
);
50-
const grpcClient = pkClient.grpcClient;
51-
const passwordMessage = new sessionsPB.Password();
52-
passwordMessage.setPassword(password);
53-
const responseMessage = await binUtils.retryAuthentication(
54-
(metaRetried?: Metadata) => {
55-
return metaRetried != null
56-
? grpcClient.sessionsUnlock(passwordMessage, metaRetried)
57-
: grpcClient.sessionsUnlock(passwordMessage);
58-
},
59-
);
60-
const token: SessionToken = responseMessage.getToken() as SessionToken;
61-
62-
// Write token to file
63-
await pkClient.session.writeToken(token);
64-
process.stdout.write('Client session started');
6547
} finally {
66-
if (pkClient != null) await pkClient.stop();
48+
if (pkClient! != null) await pkClient.stop();
6749
}
6850
});
6951
}

0 commit comments

Comments
 (0)