Skip to content

Commit

Permalink
wip: PolykeyAgent is using the new ClientService
Browse files Browse the repository at this point in the history
  • Loading branch information
CMCDragonkai committed Oct 11, 2023
1 parent 493afa1 commit 6958b99
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 57 deletions.
86 changes: 37 additions & 49 deletions src/PolykeyAgent.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { DeepPartial, FileSystem, PromiseDeconstructed } from './types';
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';
Expand All @@ -11,7 +11,6 @@ import {
CreateDestroyStartStop,
ready,
} from '@matrixai/async-init/dist/CreateDestroyStartStop';
import * as clientUtilsMiddleware from './client/middleware';
import { WorkerManager } from './workers';
import KeyRing from './keys/KeyRing';
import CertManager from './keys/CertManager';
Expand All @@ -28,7 +27,9 @@ import Sigchain from './sigchain/Sigchain';
import Discovery from './discovery/Discovery';
import SessionManager from './sessions/SessionManager';
import IdentitiesManager from './identities/IdentitiesManager';
import { providers } from './identities';
import * as identityProviders from './identities/providers';
import TaskManager from './tasks/TaskManager';
import ClientService from './client/ClientService';
import config from './config';
import * as errors from './errors';
import * as events from './events';
Expand All @@ -37,10 +38,9 @@ import * as keysUtils from './keys/utils';
import * as keysEvents from './keys/events';
import * as nodesUtils from './nodes/utils';
import * as workersUtils from './workers/utils';
import TaskManager from './tasks/TaskManager';
import serverManifest from './client/handlers';
import * as clientMiddleware from './client/middleware';
import clientServerManifest from './client/handlers';
import agentServerManifest from './nodes/agent/handlers';
import ClientService from './client/ClientService';

/**
* Optional configuration for `PolykeyAgent`.
Expand Down Expand Up @@ -181,7 +181,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 pkAgentProm: PromiseDeconstructed<PolykeyAgent> | undefined;

let status: Status | undefined;
let schema: Schema | undefined;
Expand Down Expand Up @@ -292,9 +291,9 @@ class PolykeyAgent {
fresh,
});
// Registering providers
const githubProvider = new providers.GithubProvider({
const githubProvider = new identityProviders.GithubProvider({
clientId: config.providers['github.com'].clientId,
logger: logger.getChild(providers.GithubProvider.name),
logger: logger.getChild(identityProviders.GithubProvider.name),
});
identitiesManager.registerProvider(githubProvider);
nodeGraph = await NodeGraph.createNodeGraph({
Expand Down Expand Up @@ -380,44 +379,20 @@ class PolykeyAgent {
if (optionsDefaulted.keys.recoveryCode != null) {
await sessionManager.resetKey();
}
pkAgentProm = utils.promise();
clientService = await ClientService.createClientService({
manifest: serverManifest({
acl: acl,
certManager: certManager,
db: db,
discovery: discovery,
fs: fs,
gestaltGraph: gestaltGraph,
identitiesManager: identitiesManager,
keyRing: keyRing,
logger: logger,
nodeConnectionManager: nodeConnectionManager,
nodeGraph: nodeGraph,
nodeManager: nodeManager,
notificationsManager: notificationsManager,
pkAgentProm: pkAgentProm.p,
sessionManager: sessionManager,
vaultManager: vaultManager,
}),
clientService = new ClientService({
tlsConfig,
options: {
middlewareFactory: clientUtilsMiddleware.middlewareServer(
sessionManager,
keyRing,
),
host: optionsDefaulted.clientServiceHost,
port: optionsDefaulted.clientServicePort,
keepAliveTimeoutTime: optionsDefaulted.client.keepAliveTimeoutTime,
keepAliveIntervalTime: optionsDefaulted.client.keepAliveIntervalTime,
rpcCallTimeoutTime: optionsDefaulted.rpc.callTimeoutTime,
rpcParserBufferSize: optionsDefaulted.rpc.parserBufferSize,
},
middlewareFactory: clientMiddleware.middlewareServer(
sessionManager,
keyRing,
),
keepAliveTimeoutTime: optionsDefaulted.client.keepAliveTimeoutTime,
keepAliveIntervalTime: optionsDefaulted.client.keepAliveIntervalTime,
rpcCallTimeoutTime: optionsDefaulted.rpc.callTimeoutTime,
rpcParserBufferSize: optionsDefaulted.rpc.parserBufferSize,
logger: logger.getChild(ClientService.name),
});
} catch (e) {
logger.warn(`Failed Creating ${this.name}`);
await clientService?.stop({ force: true });
await sessionManager?.stop();
await notificationsManager?.stop();
await vaultManager?.stop();
Expand Down Expand Up @@ -457,8 +432,6 @@ class PolykeyAgent {
fs,
logger,
});
pkAgentProm?.resolveP(pkAgent);

await pkAgent.start({
password,
options: {
Expand Down Expand Up @@ -670,10 +643,26 @@ class PolykeyAgent {
await this.identitiesManager.start({ fresh });
// Client server
await this.clientService.start({
options: {
host: optionsDefaulted.clientServiceHost,
port: optionsDefaulted.clientServicePort,
},
manifest: clientServerManifest({
polykeyAgent: this,
acl: this.acl,
certManager: this.certManager,
db: this.db,
discovery: this.discovery,
fs: this.fs,
gestaltGraph: this.gestaltGraph,
identitiesManager: this.identitiesManager,
keyRing: this.keyRing,
logger: this.logger,
nodeConnectionManager: this.nodeConnectionManager,
nodeGraph: this.nodeGraph,
nodeManager: this.nodeManager,
notificationsManager: this.notificationsManager,
sessionManager: this.sessionManager,
vaultManager: this.vaultManager,
}),
host: optionsDefaulted.clientServiceHost,
port: optionsDefaulted.clientServicePort,
});
await this.nodeManager.start();
await this.nodeConnectionManager.start({
Expand Down Expand Up @@ -824,7 +813,6 @@ class PolykeyAgent {
await this.vaultManager.destroy();
await this.discovery.destroy();
await this.nodeGraph.destroy();
await this.clientService.destroy();
await this.identitiesManager.destroy();
await this.gestaltGraph.destroy();
await this.acl.destroy();
Expand Down
16 changes: 8 additions & 8 deletions src/PolykeyClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ import path from 'path';
import Logger from '@matrixai/logger';
import { CreateDestroyStartStop } from '@matrixai/async-init/dist/CreateDestroyStartStop';
import { RPCClient } from '@matrixai/rpc';
import { middleware as rpcUtilsMiddleware } from '@matrixai/rpc';
import * as clientUtilsMiddleware from './client/middleware';
import { middleware as rpcMiddleware } from '@matrixai/rpc';
import * as clientMiddleware from './client/middleware';
import { Session } from './sessions';
import * as utils from './utils';
import * as errors from './errors';
import * as events from './events';
import config from './config';
import * as networkUtils from './network/utils';
import clientManifest from './client/callers';
import clientClientManifest from './client/callers';

/**
* This PolykeyClient would create a new PolykeyClient object that constructs
Expand Down Expand Up @@ -62,10 +62,10 @@ class PolykeyClient {
fresh,
});
const rpcClientClient = new RPCClient({
manifest: clientManifest,
manifest: clientClientManifest,
streamFactory,
middlewareFactory: rpcUtilsMiddleware.defaultClientMiddlewareWrapper(
clientUtilsMiddleware.middlewareClient(session),
middlewareFactory: rpcMiddleware.defaultClientMiddlewareWrapper(
clientMiddleware.middlewareClient(session),
parserBufferByteLimit,
),
toError: networkUtils.toError,
Expand All @@ -86,7 +86,7 @@ class PolykeyClient {

public readonly nodePath: string;
public readonly session: Session;
public readonly rpcClientClient: RPCClient<typeof clientManifest>;
public readonly rpcClientClient: RPCClient<typeof clientClientManifest>;

protected fs: FileSystem;
protected logger: Logger;
Expand All @@ -99,7 +99,7 @@ class PolykeyClient {
logger,
}: {
nodePath: string;
rpcClientClient: RPCClient<typeof clientManifest>;
rpcClientClient: RPCClient<typeof clientClientManifest>;
session: Session;
fs: FileSystem;
logger: Logger;
Expand Down

0 comments on commit 6958b99

Please sign in to comment.