-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
wip: created agent handlers and tests
[ci skip]
- Loading branch information
1 parent
d2eea49
commit ecf309c
Showing
9 changed files
with
365 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import type Logger from '@matrixai/logger'; | ||
import type { RPCRequestParams, RPCResponseResult } from '../types'; | ||
import type { DB } from '@matrixai/db'; | ||
import type SessionManager from '../../sessions/SessionManager'; | ||
import { UnaryHandler } from '../../RPC/handlers'; | ||
import { UnaryCaller } from '../../RPC/callers'; | ||
|
||
const agentLockAllCaller = new UnaryCaller< | ||
RPCRequestParams, | ||
RPCResponseResult | ||
>(); | ||
|
||
class AgentLockAllHandler extends UnaryHandler< | ||
{ | ||
sessionManager: SessionManager; | ||
db: DB; | ||
logger: Logger; | ||
}, | ||
RPCRequestParams, | ||
RPCResponseResult | ||
> { | ||
public async handle(): Promise<RPCResponseResult> { | ||
await this.container.db.withTransactionF((tran) => | ||
this.container.sessionManager.resetKey(tran), | ||
); | ||
return {}; | ||
} | ||
} | ||
|
||
export { agentLockAllCaller, AgentLockAllHandler }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import type Logger from '@matrixai/logger'; | ||
import type { RPCRequestParams, RPCResponseResult } from '../types'; | ||
import type PolykeyAgent from '../../PolykeyAgent'; | ||
import { running, status } from '@matrixai/async-init'; | ||
import { UnaryHandler } from '../../RPC/handlers'; | ||
import { UnaryCaller } from '../../RPC/callers'; | ||
|
||
const agentStopCaller = new UnaryCaller<RPCRequestParams, RPCResponseResult>(); | ||
|
||
class AgentStopHandler extends UnaryHandler< | ||
{ | ||
pkAgent: PolykeyAgent; | ||
logger: Logger; | ||
}, | ||
RPCRequestParams, | ||
RPCResponseResult | ||
> { | ||
public async handle(): Promise<RPCResponseResult> { | ||
const pkAgent = this.container.pkAgent; | ||
// If not running or in stopping status, then respond successfully | ||
if (!pkAgent[running] || pkAgent[status] === 'stopping') { | ||
return {}; | ||
} | ||
// Stop PK agent in the background, allow the RPC time to respond | ||
setTimeout(async () => { | ||
await pkAgent.stop(); | ||
}, 500); | ||
return {}; | ||
} | ||
} | ||
|
||
export { agentStopCaller, AgentStopHandler }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import type Logger from '@matrixai/logger'; | ||
import type SessionManager from '../../sessions/SessionManager'; | ||
import type KeyRing from '../../keys/KeyRing'; | ||
import type CertManager from '../../keys/CertManager'; | ||
import type PolykeyAgent from '../../PolykeyAgent'; | ||
import type { DB } from '@matrixai/db'; | ||
import { agentStatusCaller, AgentStatusHandler } from './agentStatus'; | ||
import { agentStopCaller, AgentStopHandler } from './agentStop'; | ||
import { agentUnlockCaller, AgentUnlockHandler } from './agentUnlock'; | ||
import { agentLockAllCaller, AgentLockAllHandler } from './agentLockAll'; | ||
|
||
const serverManifest = (container: { | ||
pkAgent: PolykeyAgent; | ||
keyRing: KeyRing; | ||
certManager: CertManager; | ||
db: DB; | ||
sessionManager: SessionManager; | ||
logger: Logger; | ||
}) => { | ||
// No type used here, it will override type inference | ||
return { | ||
agentLockAll: new AgentLockAllHandler(container), | ||
agentStatus: new AgentStatusHandler(container), | ||
agentStop: new AgentStopHandler(container), | ||
agentUnlock: new AgentUnlockHandler(container), | ||
}; | ||
}; | ||
|
||
// No type used here, it will override type inference | ||
const clientManifest = { | ||
agentLockAll: agentLockAllCaller, | ||
agentStatus: agentStatusCaller, | ||
agentStop: agentStopCaller, | ||
agentUnlock: agentUnlockCaller, | ||
}; | ||
|
||
export { serverManifest, clientManifest }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
import type { TLSConfig } from '@/network/types'; | ||
import fs from 'fs'; | ||
import path from 'path'; | ||
import os from 'os'; | ||
import Logger, { formatting, LogLevel, StreamHandler } from '@matrixai/logger'; | ||
import { DB } from '@matrixai/db'; | ||
import KeyRing from '@/keys/KeyRing'; | ||
import * as keysUtils from '@/keys/utils'; | ||
import RPCServer from '@/RPC/RPCServer'; | ||
import TaskManager from '@/tasks/TaskManager'; | ||
import { | ||
agentLockAllCaller, | ||
AgentLockAllHandler, | ||
} from '@/clientRPC/handlers/agentLockAll'; | ||
import RPCClient from '@/RPC/RPCClient'; | ||
import { SessionManager } from '@/sessions'; | ||
import ClientServer from '@/clientRPC/ClientServer'; | ||
import ClientClient from '@/clientRPC/ClientClient'; | ||
import * as testsUtils from '../../utils'; | ||
|
||
describe('agentLockAll', () => { | ||
const logger = new Logger('agentUnlock test', LogLevel.WARN, [ | ||
new StreamHandler( | ||
formatting.format`${formatting.level}:${formatting.keys}:${formatting.msg}`, | ||
), | ||
]); | ||
const password = 'helloWorld'; | ||
const host = '127.0.0.1'; | ||
let dataDir: string; | ||
let db: DB; | ||
let keyRing: KeyRing; | ||
let taskManager: TaskManager; | ||
let sessionManager: SessionManager; | ||
let clientClient: ClientClient; | ||
let clientServer: ClientServer; | ||
let tlsConfig: TLSConfig; | ||
|
||
beforeEach(async () => { | ||
dataDir = await fs.promises.mkdtemp( | ||
path.join(os.tmpdir(), 'polykey-test-'), | ||
); | ||
const keysPath = path.join(dataDir, 'keys'); | ||
const dbPath = path.join(dataDir, 'db'); | ||
db = await DB.createDB({ | ||
dbPath, | ||
logger, | ||
}); | ||
keyRing = await KeyRing.createKeyRing({ | ||
password, | ||
keysPath, | ||
logger, | ||
passwordOpsLimit: keysUtils.passwordOpsLimits.min, | ||
passwordMemLimit: keysUtils.passwordMemLimits.min, | ||
strictMemoryLock: false, | ||
}); | ||
taskManager = await TaskManager.createTaskManager({ db, logger }); | ||
sessionManager = await SessionManager.createSessionManager({ | ||
db, | ||
keyRing, | ||
logger, | ||
}); | ||
tlsConfig = await testsUtils.createTLSConfig(keyRing.keyPair); | ||
}); | ||
afterEach(async () => { | ||
await clientServer.stop(true); | ||
await clientClient.destroy(true); | ||
await taskManager.stop(); | ||
await keyRing.stop(); | ||
await db.stop(); | ||
await fs.promises.rm(dataDir, { | ||
force: true, | ||
recursive: true, | ||
}); | ||
}); | ||
test('Locks all current sessions', async () => { | ||
// Setup | ||
const rpcServer = await RPCServer.createRPCServer({ | ||
manifest: { | ||
agentLockAll: new AgentLockAllHandler({ | ||
db, | ||
sessionManager, | ||
logger, | ||
}), | ||
}, | ||
logger, | ||
}); | ||
clientServer = await ClientServer.createClientServer({ | ||
connectionCallback: (streamPair, connectionInfo) => | ||
rpcServer.handleStream(streamPair, connectionInfo), | ||
host, | ||
tlsConfig, | ||
logger: logger.getChild('server'), | ||
}); | ||
clientClient = await ClientClient.createClientClient({ | ||
expectedNodeIds: [keyRing.getNodeId()], | ||
host, | ||
logger: logger.getChild('client'), | ||
port: clientServer.port, | ||
}); | ||
const rpcClient = await RPCClient.createRPCClient({ | ||
manifest: { | ||
agentLockAll: agentLockAllCaller, | ||
}, | ||
streamPairCreateCallback: async () => clientClient.startConnection(), | ||
logger: logger.getChild('clientRPC'), | ||
}); | ||
|
||
// Doing the test | ||
const token = await sessionManager.createToken(); | ||
await rpcClient.methods.agentLockAll({}); | ||
expect(await sessionManager.verifyToken(token)).toBeFalsy(); | ||
}); | ||
}); |
Oops, something went wrong.