Skip to content

Commit

Permalink
chore: added ctx to most of secrets commands
Browse files Browse the repository at this point in the history
  • Loading branch information
aryanjassal committed Dec 12, 2024
1 parent d518114 commit b596934
Show file tree
Hide file tree
Showing 12 changed files with 87 additions and 26 deletions.
16 changes: 9 additions & 7 deletions src/client/handlers/VaultsPull.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,15 @@ class VaultsPull extends UnaryHandler<
const nodeId = input.nodeIdEncoded
? ids.parseNodeId(input.nodeIdEncoded)
: undefined;
await vaultManager.pullVault({
vaultId: vaultId,
pullNodeId: nodeId,
pullVaultNameOrId: pullVaultId,
ctx: ctx,
tran: tran,
});
await vaultManager.pullVault(
{
vaultId: vaultId,
pullNodeId: nodeId,
pullVaultNameOrId: pullVaultId,
tran: tran,
},
ctx,
);
});
return { type: 'success', success: true };
};
Expand Down
10 changes: 8 additions & 2 deletions src/client/handlers/VaultsSecretsCat.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import type { ContextTimed } from '@matrixai/contexts';
import type { DB } from '@matrixai/db';
import type { JSONValue } from '@matrixai/rpc';
import type {
ClientRPCRequestParams,
ClientRPCResponseResult,
Expand Down Expand Up @@ -26,6 +28,9 @@ class VaultsSecretsCat extends DuplexHandler<
input: AsyncIterableIterator<
ClientRPCRequestParams<SecretIdentifierMessage>
>,
_cancel: (reason?: any) => void,
_meta: Record<string, JSONValue>,
ctx: ContextTimed,
): AsyncGenerator<ClientRPCResponseResult<ContentOrErrorMessage>> {
const { db, vaultManager }: { db: DB; vaultManager: VaultManager } =
this.container;
Expand All @@ -34,8 +39,8 @@ class VaultsSecretsCat extends DuplexHandler<
> {
// As we need to preserve the order of parameters, we need to loop over
// them individually, as grouping them would make them go out of order.
for await (const secretIdentiferMessage of input) {
const { nameOrId, secretName } = secretIdentiferMessage;
for await (const secretIdentifierMessage of input) {
const { nameOrId, secretName } = secretIdentifierMessage;
const vaultIdFromName = await vaultManager.getVaultId(nameOrId, tran);
const vaultId = vaultIdFromName ?? vaultsUtils.decodeVaultId(nameOrId);
if (vaultId == null) throw new vaultsErrors.ErrorVaultsVaultUndefined();
Expand Down Expand Up @@ -63,6 +68,7 @@ class VaultsSecretsCat extends DuplexHandler<
throw e;
}
},
ctx,
tran,
);
}
Expand Down
17 changes: 10 additions & 7 deletions src/client/handlers/VaultsSecretsEnv.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import type { ContextTimed } from '@matrixai/contexts';
import type { DB } from '@matrixai/db';
import type { JSONValue } from '@matrixai/rpc';
import type {
ClientRPCRequestParams,
ClientRPCResponseResult,
Expand All @@ -22,24 +24,24 @@ class VaultsSecretsEnv extends DuplexHandler<
input: AsyncIterableIterator<
ClientRPCRequestParams<SecretIdentifierMessage>
>,
_cancel,
_meta,
ctx,
_cancel: (reason?: any) => void,
_meta: Record<string, JSONValue>,
ctx: ContextTimed,
): AsyncGenerator<ClientRPCResponseResult<SecretContentMessage>> {
if (ctx.signal.aborted) throw ctx.signal.reason;
const { db, vaultManager }: { db: DB; vaultManager: VaultManager } =
this.container;

return yield* db.withTransactionG(async function* (tran): AsyncGenerator<
ClientRPCResponseResult<SecretContentMessage>
> {
if (ctx.signal.aborted) throw ctx.signal.reason;
ctx.signal.throwIfAborted();
for await (const secretIdentifierMessage of input) {
const { nameOrId, secretName } = secretIdentifierMessage;
const vaultIdFromName = await vaultManager.getVaultId(nameOrId, tran);
const vaultId = vaultIdFromName ?? vaultsUtils.decodeVaultId(nameOrId);
if (vaultId == null) {
throw new vaultsErrors.ErrorVaultsVaultUndefined();
throw new vaultsErrors.ErrorVaultsVaultUndefined(
`Vault "${nameOrId}" does not exist`,
);
}
const secrets = await vaultManager.withVaults(
[vaultId],
Expand Down Expand Up @@ -72,6 +74,7 @@ class VaultsSecretsEnv extends DuplexHandler<
return results;
});
},
ctx,
tran,
);
for (const { filePath, value } of secrets) {
Expand Down
12 changes: 10 additions & 2 deletions src/client/handlers/VaultsSecretsMkdir.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import type { ContextTimed } from '@matrixai/contexts';
import type { DB } from '@matrixai/db';
import type { JSONValue } from '@matrixai/rpc';
import type {
ClientRPCRequestParams,
ClientRPCResponseResult,
Expand All @@ -22,6 +24,9 @@ class VaultsSecretsMkdir extends DuplexHandler<
> {
public handle = async function* (
input: AsyncIterableIterator<ClientRPCRequestParams<SecretDirMessage>>,
_cancel: (reason?: any) => void,
_meta: Record<string, JSONValue>,
ctx: ContextTimed,
): AsyncGenerator<ClientRPCResponseResult<SuccessOrErrorMessage>> {
const { db, vaultManager }: { db: DB; vaultManager: VaultManager } =
this.container;
Expand All @@ -38,10 +43,12 @@ class VaultsSecretsMkdir extends DuplexHandler<
const vaultId =
vaultIdFromName ?? vaultsUtils.decodeVaultId(nameOrId);
if (vaultId == null) {
throw new vaultsErrors.ErrorVaultsVaultUndefined();
throw new vaultsErrors.ErrorVaultsVaultUndefined(
`Vault "${nameOrId}" does not exist`,
);
}
// Write directories. This doesn't need to be grouped by vault names,
// as no commit is created for empty directories anyways. The
// as no commit is created for empty directories anyway. The
// vaultOps.mkdir() method also returns an object of type
// SuccessOrErrorMessage. As such, we can return the result without
// doing any type conversion or extra processing.
Expand All @@ -68,6 +75,7 @@ class VaultsSecretsMkdir extends DuplexHandler<
}
}
},
ctx,
tran,
);
}
Expand Down
10 changes: 9 additions & 1 deletion src/client/handlers/VaultsSecretsNew.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import type { ContextTimed } from '@matrixai/contexts';
import type { DB } from '@matrixai/db';
import type { JSONValue } from '@matrixai/rpc';
import type {
ClientRPCRequestParams,
ClientRPCResponseResult,
Expand All @@ -21,6 +23,9 @@ class VaultsSecretsNew extends UnaryHandler<
> {
public handle = async (
input: ClientRPCRequestParams<SecretContentMessage>,
_cancel: (reason?: any) => void,
_meta: Record<string, JSONValue>,
ctx: ContextTimed,
): Promise<ClientRPCResponseResult<SuccessMessage>> => {
const { db, vaultManager }: { db: DB; vaultManager: VaultManager } =
this.container;
Expand All @@ -32,14 +37,17 @@ class VaultsSecretsNew extends UnaryHandler<
const vaultId =
vaultIdFromName ?? vaultsUtils.decodeVaultId(input.nameOrId);
if (vaultId == null) {
throw new vaultsErrors.ErrorVaultsVaultUndefined();
throw new vaultsErrors.ErrorVaultsVaultUndefined(
`Vault "${input.nameOrId}" does not exist`,
);
}
const content = Buffer.from(input.secretContent, 'binary');
await vaultManager.withVaults(
[vaultId],
async (vault) => {
await vaultOps.addSecret(vault, input.secretName, content);
},
ctx,
tran,
);
});
Expand Down
6 changes: 6 additions & 0 deletions src/client/handlers/VaultsSecretsNewDir.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import type { FileSystem } from 'types';
import type { ContextTimed } from '@matrixai/contexts';
import type { DB } from '@matrixai/db';
import type { JSONValue } from '@matrixai/rpc';
import type {
ClientRPCRequestParams,
ClientRPCResponseResult,
Expand All @@ -23,6 +25,9 @@ class VaultsSecretsNewDir extends UnaryHandler<
> {
public handle = async (
input: ClientRPCRequestParams<SecretDirMessage>,
_cancel: (reason?: any) => void,
_meta: Record<string, JSONValue>,
ctx: ContextTimed,
): Promise<ClientRPCResponseResult<SuccessMessage>> => {
const {
db,
Expand All @@ -44,6 +49,7 @@ class VaultsSecretsNewDir extends UnaryHandler<
async (vault) => {
await vaultOps.addSecretDirectory(vault, input.dirName, fs);
},
ctx,
tran,
);
});
Expand Down
4 changes: 2 additions & 2 deletions src/client/handlers/VaultsSecretsRemove.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { ContextCancellable } from '@matrixai/contexts';
import type { ContextTimed } from '@matrixai/contexts';
import type { DB } from '@matrixai/db';
import type { ResourceAcquire } from '@matrixai/resources';
import type { JSONValue } from '@matrixai/rpc';
Expand Down Expand Up @@ -35,7 +35,7 @@ class VaultsSecretsRemove extends DuplexHandler<
>,
_cancel: (reason?: any) => void,
_meta: Record<string, JSONValue>,
ctx: ContextCancellable,
ctx: ContextTimed,
): AsyncGenerator<ClientRPCResponseResult<SuccessOrErrorMessage>> {
const { db, vaultManager }: { db: DB; vaultManager: VaultManager } =
this.container;
Expand Down
10 changes: 9 additions & 1 deletion src/client/handlers/VaultsSecretsRename.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import type { ContextTimed } from '@matrixai/contexts';
import type { DB } from '@matrixai/db';
import type { JSONValue } from '@matrixai/rpc';
import type {
ClientRPCRequestParams,
ClientRPCResponseResult,
Expand All @@ -21,6 +23,9 @@ class VaultsSecretsRename extends UnaryHandler<
> {
public handle = async (
input: ClientRPCRequestParams<SecretRenameMessage>,
_cancel: (reason?: any) => void,
_meta: Record<string, JSONValue>,
ctx: ContextTimed,
): Promise<ClientRPCResponseResult<SuccessMessage>> => {
const { db, vaultManager }: { db: DB; vaultManager: VaultManager } =
this.container;
Expand All @@ -32,7 +37,9 @@ class VaultsSecretsRename extends UnaryHandler<
const vaultId =
vaultIdFromName ?? vaultsUtils.decodeVaultId(input.nameOrId);
if (vaultId == null) {
throw new vaultsErrors.ErrorVaultsVaultUndefined();
throw new vaultsErrors.ErrorVaultsVaultUndefined(
`Vault "${input.nameOrId}" does not exist`,
);
}
await vaultManager.withVaults(
[vaultId],
Expand All @@ -43,6 +50,7 @@ class VaultsSecretsRename extends UnaryHandler<
input.newSecretName,
);
},
ctx,
tran,
);
});
Expand Down
10 changes: 9 additions & 1 deletion src/client/handlers/VaultsSecretsStat.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import type { ContextTimed } from '@matrixai/contexts';
import type { DB } from '@matrixai/db';
import type { JSONValue } from '@matrixai/rpc';
import type {
ClientRPCRequestParams,
ClientRPCResponseResult,
Expand All @@ -21,6 +23,9 @@ class VaultsSecretsStat extends UnaryHandler<
> {
public handle = async (
input: ClientRPCRequestParams<SecretIdentifierMessage>,
_cancel: (reason?: any) => void,
_meta: Record<string, JSONValue>,
ctx: ContextTimed,
): Promise<ClientRPCResponseResult<SecretStatMessage>> => {
const { db, vaultManager }: { db: DB; vaultManager: VaultManager } =
this.container;
Expand All @@ -32,14 +37,17 @@ class VaultsSecretsStat extends UnaryHandler<
const vaultId =
vaultIdFromName ?? vaultsUtils.decodeVaultId(input.nameOrId);
if (vaultId == null) {
throw new vaultsErrors.ErrorVaultsVaultUndefined();
throw new vaultsErrors.ErrorVaultsVaultUndefined(
`Vault "${input.nameOrId}" does not exist`,
);
}
const secretName = input.secretName;
const stat = await vaultManager.withVaults(
[vaultId],
async (vault) => {
return await vaultOps.statSecret(vault, secretName);
},
ctx,
tran,
);
return {
Expand Down
6 changes: 6 additions & 0 deletions src/client/handlers/VaultsSecretsWriteFile.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import type { ContextTimed } from '@matrixai/contexts';
import type { DB } from '@matrixai/db';
import type { JSONValue } from '@matrixai/rpc';
import type {
ClientRPCRequestParams,
ClientRPCResponseResult,
Expand All @@ -21,6 +23,9 @@ class VaultsSecretsWriteFile extends UnaryHandler<
> {
public handle = async (
input: ClientRPCRequestParams<SecretContentMessage>,
_cancel: (reason?: any) => void,
_meta: Record<string, JSONValue>,
ctx: ContextTimed,
): Promise<ClientRPCResponseResult<SuccessMessage>> => {
const { db, vaultManager }: { db: DB; vaultManager: VaultManager } =
this.container;
Expand All @@ -40,6 +45,7 @@ class VaultsSecretsWriteFile extends UnaryHandler<
async (vault) => {
await vaultOps.writeSecret(vault, input.secretName, secretContent);
},
ctx,
tran,
);
});
Expand Down
6 changes: 6 additions & 0 deletions src/client/handlers/VaultsVersion.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import type { ContextTimed } from '@matrixai/contexts';
import type { DB } from '@matrixai/db';
import type { JSONValue } from '@matrixai/rpc';
import type {
ClientRPCRequestParams,
ClientRPCResponseResult,
Expand All @@ -20,6 +22,9 @@ class VaultsVersion extends UnaryHandler<
> {
public handle = async (
input: ClientRPCRequestParams<VaultsVersionMessage>,
_cancel: (reason?: any) => void,
_meta: Record<string, JSONValue>,
ctx: ContextTimed,
): Promise<ClientRPCResponseResult<VaultsLatestVersionMessage>> => {
const { db, vaultManager }: { db: DB; vaultManager: VaultManager } =
this.container;
Expand All @@ -42,6 +47,7 @@ class VaultsVersion extends UnaryHandler<
const currentVersionId = (await vault.log(versionId, 0))[0]?.commitId;
return [latestOid, currentVersionId];
},
ctx,
tran,
);
// Checking if latest version ID
Expand Down
6 changes: 3 additions & 3 deletions src/vaults/VaultManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -850,9 +850,9 @@ class VaultManager {
const vault = await this.getVault(vaultId, tran, ctx);
await vault.pullVault({
nodeManager: this.nodeManager,
pullNodeId,
pullVaultNameOrId,
tran,
pullNodeId: pullNodeId,
pullVaultNameOrId: pullVaultNameOrId,
tran: tran,
});
},
);
Expand Down

0 comments on commit b596934

Please sign in to comment.