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

Session Bin Test Fixes #296

Merged
merged 29 commits into from
Dec 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
59fe0b3
Fixing tests/bin/sessions, removed nested describes and concurrent te…
tegefaulkes Nov 1, 2021
b7e5978
Adding typescript-cache-transpile for faster test execution with proc…
joshuakarp Dec 9, 2021
7789a48
Session fixes: correct authentication usage for CLI commands, excepti…
CMCDragonkai Dec 9, 2021
b170935
Integration FlowCountInterceptor to GRPCClient, enabling pkStdio
CMCDragonkai Dec 9, 2021
bdc2633
Ensure proper return for callback for `agentStop` and `sessionsUnlock…
CMCDragonkai Dec 10, 2021
8c530c0
Adding returns to all GRPC handlers
emmacasolin Dec 10, 2021
b0e4a82
`pk keys password`, `pk keys renew` and `pk keys reset` all take `--p…
CMCDragonkai Dec 12, 2021
720d345
`pk agent start` and `pk bootstrap` are also using `processNewPasswor…
CMCDragonkai Dec 12, 2021
76d4338
Added test for stopping agent while unauthenticated, fixed `pk agent …
CMCDragonkai Dec 12, 2021
a46d74e
Reading `PK_PASSWORD` should be in `processNewPassword`
CMCDragonkai Dec 12, 2021
a35ad99
Fixing password env
CMCDragonkai Dec 12, 2021
25d9654
sessions tests passing
emmacasolin Dec 13, 2021
bd3ca04
Close the testLockFile for `pkAgent` after unlocking
CMCDragonkai Dec 13, 2021
c7eaaa9
Fixing up `pk agent lock` tests
CMCDragonkai Dec 13, 2021
308c784
Fixing `pk agent lock` and `pk agent lockall` tests
CMCDragonkai Dec 13, 2021
130d3da
test fixes for keys + sessions
emmacasolin Dec 13, 2021
98b2916
Fixed tests/bin/agent concurrent testing
CMCDragonkai Dec 13, 2021
61604c3
nonces comment on sessions tests
emmacasolin Dec 13, 2021
42454b9
test fixes from status command implementation
emmacasolin Dec 13, 2021
17bba2a
Agent and sessions tests working
CMCDragonkai Dec 13, 2021
e0de24e
more meaningful naming
emmacasolin Dec 13, 2021
4ff1e88
replacing meta?: Metadata with auth
emmacasolin Dec 13, 2021
3dfa2cd
missed metas in vaults commands
emmacasolin Dec 13, 2021
63d6294
moving preprocessing outside of try blocks
emmacasolin Dec 13, 2021
35618f2
Moved `PolykeyClient.test.ts` out of `tests/client` to `tests`
CMCDragonkai Dec 13, 2021
b21d3bd
removing unnecessary writes to stdout
emmacasolin Dec 13, 2021
481a10f
Setup global root keypair for `tests/PolykeyClient.test.ts`
CMCDragonkai Dec 13, 2021
fd2236a
Linted
CMCDragonkai Dec 13, 2021
ea8493f
Added timeouts to bootstrapping
CMCDragonkai Dec 13, 2021
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
57 changes: 50 additions & 7 deletions package-lock.json

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

6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
"lintfix": "eslint '{src,tests}/**/*.{js,ts}' --fix",
"docs": "rm -r ./docs || true; typedoc --gitRevision master --tsconfig ./tsconfig.build.json --out ./docs src && touch ./docs/.nojekyll",
"proto-generate": "scripts/proto-generate.sh",
"polykey": "ts-node --require tsconfig-paths/register --transpile-only src/bin/polykey.ts"
"polykey": "ts-node --require tsconfig-paths/register --compiler typescript-cached-transpile --transpile-only src/bin/polykey.ts"
},
"dependencies": {
"@grpc/grpc-js": "1.3.7",
Expand Down Expand Up @@ -95,6 +95,7 @@
"pako": "^1.0.11",
"prompts": "^2.4.1",
"readable-stream": "^3.6.0",
"resource-counter": "^1.2.4",
"threads": "^1.6.5",
"ts-custom-error": "^3.2.0",
"utp-native": "^2.5.3",
Expand Down Expand Up @@ -130,6 +131,7 @@
"ts-node": "^10.4.0",
"tsconfig-paths": "^3.9.0",
"typedoc": "^0.21.5",
"typescript": "^4.1.3"
"typescript": "^4.1.3",
"typescript-cached-transpile": "0.0.6"
}
}
22 changes: 12 additions & 10 deletions src/agent/GRPCClientAgent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,17 @@ class GRPCClientAgent extends GRPCClient<AgentServiceClient> {
logger?: Logger;
}): Promise<GRPCClientAgent> {
logger.info(`Creating ${this.name}`);
const { client, serverCertChain } = await super.createClient({
clientConstructor: AgentServiceClient,
nodeId,
host,
port,
tlsConfig,
proxyConfig,
timeout,
logger,
});
const { client, serverCertChain, flowCountInterceptor } =
await super.createClient({
clientConstructor: AgentServiceClient,
nodeId,
host,
port,
tlsConfig,
proxyConfig,
timeout,
logger,
});
const grpcClientAgent = new GRPCClientAgent({
client,
nodeId,
Expand All @@ -56,6 +57,7 @@ class GRPCClientAgent extends GRPCClient<AgentServiceClient> {
tlsConfig,
proxyConfig,
serverCertChain,
flowCountInterceptor,
logger,
});
logger.info(`Created ${this.name}`);
Expand Down
2 changes: 1 addition & 1 deletion src/bin/agent/CommandLock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ class CommandLock extends CommandPolykey {
this.description('Lock the Client and Clear the Existing Token');
this.action(async (options) => {
const { default: Session } = await import('../../sessions/Session');
// Just delete the session token
const session = new Session({
sessionTokenPath: path.join(
options.nodePath,
Expand All @@ -18,6 +17,7 @@ class CommandLock extends CommandPolykey {
fs: this.fs,
logger: this.logger.getChild(Session.name),
});
// Destroy local session
await session.destroy();
});
}
Expand Down
29 changes: 21 additions & 8 deletions src/bin/agent/CommandLockAll.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import type PolykeyClient from '../../PolykeyClient';
import path from 'path';
import CommandPolykey from '../CommandPolykey';
import * as binUtils from '../utils';
import * as binOptions from '../utils/options';
import * as binProcessors from '../utils/processors';
import config from '../../config';

class CommandLockAll extends CommandPolykey {
constructor(...args: ConstructorParameters<typeof CommandPolykey>) {
Expand All @@ -14,7 +16,9 @@ class CommandLockAll extends CommandPolykey {
this.addOption(binOptions.clientPort);
this.action(async (options) => {
const { default: PolykeyClient } = await import('../../PolykeyClient');
const { default: Session } = await import('../../sessions/Session');
const utilsPB = await import('../../proto/js/polykey/v1/utils/utils_pb');

const clientOptions = await binProcessors.processClientOptions(
options.nodePath,
options.nodeId,
Expand All @@ -23,7 +27,19 @@ class CommandLockAll extends CommandPolykey {
this.fs,
this.logger.getChild(binProcessors.processClientOptions.name),
);
let pkClient: PolykeyClient | undefined;
const meta = await binProcessors.processAuthentication(
options.passwordFile,
this.fs,
);
const session = new Session({
sessionTokenPath: path.join(
options.nodePath,
config.defaults.tokenBase,
),
fs: this.fs,
logger: this.logger.getChild(Session.name),
});
let pkClient: PolykeyClient;
this.exitHandlers.handlers.push(async () => {
if (pkClient != null) await pkClient.stop();
});
Expand All @@ -35,18 +51,15 @@ class CommandLockAll extends CommandPolykey {
nodePath: options.nodePath,
logger: this.logger.getChild(PolykeyClient.name),
});
const meta = await binProcessors.processAuthentication(
options.passwordFile,
this.fs,
);
const grpcClient = pkClient.grpcClient;
const emptyMessage = new utilsPB.EmptyMessage();
await binUtils.retryAuthentication(
(auth) => grpcClient.sessionsLockAll(emptyMessage, auth),
(auth) => pkClient.grpcClient.sessionsLockAll(emptyMessage, auth),
meta,
);
// Destroy local session
await session.destroy();
} finally {
if (pkClient != null) await pkClient.stop();
if (pkClient! != null) await pkClient.stop();
}
});
}
Expand Down
37 changes: 30 additions & 7 deletions src/bin/agent/CommandStart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import CommandPolykey from '../CommandPolykey';
import * as binOptions from '../utils/options';
import * as binProcessors from '../utils/processors';
import * as binErrors from '../errors';
import { promise } from '../../utils';
import { promise, dirEmpty } from '../../utils';
import config from '../../config';

class CommandStart extends CommandPolykey {
Expand All @@ -33,12 +33,35 @@ class CommandStart extends CommandPolykey {
options.clientPort =
options.clientPort ?? config.defaults.networkConfig.clientPort;
const { default: PolykeyAgent } = await import('../../PolykeyAgent');
// Password is necessary
// If recovery code is supplied, then this is the new password
const password = await binProcessors.processPassword(
options.passwordFile,
this.fs,
);
let password: string | undefined;
if (options.fresh) {
// If fresh, then get a new password
password = await binProcessors.processNewPassword(
options.passwordFile,
this.fs,
);
} else if (options.recoveryCodeFile != null) {
// If recovery code is supplied, then this is the new password
password = await binProcessors.processNewPassword(
options.passwordFile,
this.fs,
);
} else if (await dirEmpty(this.fs, options.nodePath)) {
// If the node path is empty, get a new password
password = await binProcessors.processNewPassword(
options.passwordFile,
this.fs,
);
} else {
// Otherwise this is the existing password
// however, the code is capable of doing partial bootstrapping
// so it's possible that this is also a new password
// if the root key isn't setup
password = await binProcessors.processPassword(
options.passwordFile,
this.fs,
);
}
const recoveryCodeIn = await binProcessors.processRecoveryCode(
options.recoveryCodeFile,
this.fs,
Expand Down
8 changes: 4 additions & 4 deletions src/bin/agent/CommandStatus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ class CommandStatus extends CommandPolykey {
}),
);
} else {
const meta = await binProcessors.processAuthentication(
options.passwordFile,
this.fs,
);
let pkClient: PolykeyClient;
this.exitHandlers.handlers.push(async () => {
if (pkClient != null) await pkClient.stop();
Expand All @@ -51,10 +55,6 @@ class CommandStatus extends CommandPolykey {
nodePath: options.nodePath,
logger: this.logger.getChild(PolykeyClient.name),
});
const meta = await binProcessors.processAuthentication(
options.passwordFile,
this.fs,
);
const emptyMessage = new utilsPB.EmptyMessage();
response = await binUtils.retryAuthentication(
(auth) => pkClient.grpcClient.agentStatus(emptyMessage, auth),
Expand Down
11 changes: 5 additions & 6 deletions src/bin/agent/CommandStop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ class CommandStop extends CommandPolykey {
} else if (statusInfo?.status === 'STARTING') {
throw new binErrors.ErrorCLIStatusStarting();
}
const meta = await binProcessors.processAuthentication(
options.passwordFile,
this.fs,
);
// Either the statusInfo is undefined or LIVE
// Either way, the connection parameters now exist
let pkClient: PolykeyClient;
Expand All @@ -48,14 +52,9 @@ class CommandStop extends CommandPolykey {
port: clientStatus.clientPort!,
logger: this.logger.getChild(PolykeyClient.name),
});
const meta = await binProcessors.processAuthentication(
options.passwordFile,
this.fs,
);
const emptyMessage = new utilsPB.EmptyMessage();
const grpcClient = pkClient.grpcClient;
await binUtils.retryAuthentication(
(auth) => grpcClient.agentStop(emptyMessage, auth),
(auth) => pkClient.grpcClient.agentStop(emptyMessage, auth),
meta,
);
this.logger.info('Stopping Agent');
Expand Down
42 changes: 12 additions & 30 deletions src/bin/agent/CommandUnlock.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
import type { SessionToken } from '../../sessions/types';
import type { Metadata } from '@grpc/grpc-js';

import type PolykeyClient from '../../PolykeyClient';
import CommandPolykey from '../CommandPolykey';
import * as binUtils from '../utils';
Expand All @@ -17,10 +14,7 @@ class CommandUnlock extends CommandPolykey {
this.addOption(binOptions.clientPort);
this.action(async (options) => {
const { default: PolykeyClient } = await import('../../PolykeyClient');
const sessionsPB = await import(
'../../proto/js/polykey/v1/sessions/sessions_pb'
);

const utilsPB = await import('../../proto/js/polykey/v1/utils/utils_pb');
const clientOptions = await binProcessors.processClientOptions(
options.nodePath,
options.nodeId,
Expand All @@ -29,41 +23,29 @@ class CommandUnlock extends CommandPolykey {
this.fs,
this.logger.getChild(binProcessors.processClientOptions.name),
);

let pkClient: PolykeyClient | undefined;
const meta = await binProcessors.processAuthentication(
options.passwordFile,
this.fs,
);
let pkClient: PolykeyClient;
this.exitHandlers.handlers.push(async () => {
if (pkClient != null) await pkClient.stop();
});
try {
pkClient = await PolykeyClient.createPolykeyClient({
nodePath: options.nodePath,
nodeId: clientOptions.nodeId,
host: clientOptions.clientHost,
port: clientOptions.clientPort,
nodePath: options.nodePath,
logger: this.logger.getChild(PolykeyClient.name),
});

const password = await binProcessors.processPassword(
options.passwordFile,
this.fs,
const emptyMessage = new utilsPB.EmptyMessage();
await binUtils.retryAuthentication(
(auth) => pkClient.grpcClient.sessionsUnlock(emptyMessage, auth),
meta,
);
const grpcClient = pkClient.grpcClient;
const passwordMessage = new sessionsPB.Password();
passwordMessage.setPassword(password);
const responseMessage = await binUtils.retryAuthentication(
(metaRetried?: Metadata) => {
return metaRetried != null
? grpcClient.sessionsUnlock(passwordMessage, metaRetried)
: grpcClient.sessionsUnlock(passwordMessage);
},
);
const token: SessionToken = responseMessage.getToken() as SessionToken;

// Write token to file
await pkClient.session.writeToken(token);
process.stdout.write('Client session started');
} finally {
if (pkClient != null) await pkClient.stop();
if (pkClient! != null) await pkClient.stop();
}
});
}
Expand Down
Loading