Skip to content

Commit

Permalink
Merge pull request #814 from MatrixAI/feature-unix-write
Browse files Browse the repository at this point in the history
Implement handler to wrap `fs.writeFile` functionality
  • Loading branch information
aryanjassal authored Sep 30, 2024
2 parents 387e2e3 + 8fb019a commit 5b78ce1
Show file tree
Hide file tree
Showing 8 changed files with 163 additions and 109 deletions.
6 changes: 3 additions & 3 deletions src/client/callers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ import vaultsPermissionUnset from './vaultsPermissionUnset';
import vaultsPull from './vaultsPull';
import vaultsRename from './vaultsRename';
import vaultsScan from './vaultsScan';
import vaultsSecretsEdit from './vaultsSecretsEdit';
import vaultsSecretsEnv from './vaultsSecretsEnv';
import vaultsSecretsGet from './vaultsSecretsGet';
import vaultsSecretsList from './vaultsSecretsList';
Expand All @@ -73,6 +72,7 @@ import vaultsSecretsNewDir from './vaultsSecretsNewDir';
import vaultsSecretsRename from './vaultsSecretsRename';
import vaultsSecretsRemove from './vaultsSecretsRemove';
import vaultsSecretsStat from './vaultsSecretsStat';
import vaultsSecretsWriteFile from './vaultsSecretsWriteFile';
import vaultsVersion from './vaultsVersion';

/**
Expand Down Expand Up @@ -144,7 +144,6 @@ const clientManifest = {
vaultsPull,
vaultsRename,
vaultsScan,
vaultsSecretsEdit,
vaultsSecretsEnv,
vaultsSecretsGet,
vaultsSecretsList,
Expand All @@ -154,6 +153,7 @@ const clientManifest = {
vaultsSecretsRename,
vaultsSecretsRemove,
vaultsSecretsStat,
vaultsSecretsWriteFile,
vaultsVersion,
};

Expand Down Expand Up @@ -224,7 +224,6 @@ export {
vaultsPull,
vaultsRename,
vaultsScan,
vaultsSecretsEdit,
vaultsSecretsEnv,
vaultsSecretsGet,
vaultsSecretsList,
Expand All @@ -234,5 +233,6 @@ export {
vaultsSecretsRename,
vaultsSecretsRemove,
vaultsSecretsStat,
vaultsSecretsWriteFile,
vaultsVersion,
};
12 changes: 0 additions & 12 deletions src/client/callers/vaultsSecretsEdit.ts

This file was deleted.

12 changes: 12 additions & 0 deletions src/client/callers/vaultsSecretsWriteFile.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import type { HandlerTypes } from '@matrixai/rpc';
import type VaultsSecretsWriteFile from '../handlers/VaultsSecretsWriteFile';
import { UnaryCaller } from '@matrixai/rpc';

type CallerTypes = HandlerTypes<VaultsSecretsWriteFile>;

const vaultsSecretsWriteFile = new UnaryCaller<
CallerTypes['input'],
CallerTypes['output']
>();

export default vaultsSecretsWriteFile;
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import * as vaultsUtils from '../../vaults/utils';
import * as vaultsErrors from '../../vaults/errors';
import * as vaultOps from '../../vaults/VaultOps';

class VaultsSecretsEdit extends UnaryHandler<
class VaultsSecretsWriteFile extends UnaryHandler<
{
vaultManager: VaultManager;
db: DB;
Expand All @@ -37,7 +37,7 @@ class VaultsSecretsEdit extends UnaryHandler<
await vaultManager.withVaults(
[vaultId],
async (vault) => {
await vaultOps.updateSecret(vault, input.secretName, secretContent);
await vaultOps.writeSecret(vault, input.secretName, secretContent);
},
tran,
);
Expand All @@ -48,4 +48,4 @@ class VaultsSecretsEdit extends UnaryHandler<
};
}

export default VaultsSecretsEdit;
export default VaultsSecretsWriteFile;
6 changes: 3 additions & 3 deletions src/client/handlers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ import VaultsPermissionUnset from './VaultsPermissionUnset';
import VaultsPull from './VaultsPull';
import VaultsRename from './VaultsRename';
import VaultsScan from './VaultsScan';
import VaultsSecretsEdit from './VaultsSecretsEdit';
import VaultsSecretsEnv from './VaultsSecretsEnv';
import VaultsSecretsGet from './VaultsSecretsGet';
import VaultsSecretsList from './VaultsSecretsList';
Expand All @@ -90,6 +89,7 @@ import VaultsSecretsNewDir from './VaultsSecretsNewDir';
import VaultsSecretsRename from './VaultsSecretsRename';
import VaultsSecretsRemove from './VaultsSecretsRemove';
import VaultsSecretsStat from './VaultsSecretsStat';
import VaultsSecretsWriteFile from './VaultsSecretsWriteFile';
import VaultsVersion from './VaultsVersion';

/**
Expand Down Expand Up @@ -184,7 +184,6 @@ const serverManifest = (container: {
vaultsPull: new VaultsPull(container),
vaultsRename: new VaultsRename(container),
vaultsScan: new VaultsScan(container),
vaultsSecretsEdit: new VaultsSecretsEdit(container),
vaultsSecretsEnv: new VaultsSecretsEnv(container),
vaultsSecretsGet: new VaultsSecretsGet(container),
vaultsSecretsList: new VaultsSecretsList(container),
Expand All @@ -194,6 +193,7 @@ const serverManifest = (container: {
vaultsSecretsRename: new VaultsSecretsRename(container),
vaultsSecretsRemove: new VaultsSecretsRemove(container),
vaultsSecretsStat: new VaultsSecretsStat(container),
vaultsSecretsWriteFile: new VaultsSecretsWriteFile(container),
vaultsVersion: new VaultsVersion(container),
};
};
Expand Down Expand Up @@ -266,7 +266,6 @@ export {
VaultsPull,
VaultsRename,
VaultsScan,
VaultsSecretsEdit,
VaultsSecretsEnv,
VaultsSecretsGet,
VaultsSecretsList,
Expand All @@ -276,5 +275,6 @@ export {
VaultsSecretsRename,
VaultsSecretsRemove,
VaultsSecretsStat,
VaultsSecretsWriteFile,
VaultsVersion,
};
16 changes: 16 additions & 0 deletions src/vaults/VaultOps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,21 @@ async function listSecrets(vault: Vault): Promise<string[]> {
});
}

/**
* Changes the contents of a secret. Creates a new file if it doesn't exist.
*/
async function writeSecret(
vault: Vault,
secretName: string,
content: Buffer | string,
logger?: Logger,
): Promise<void> {
await vault.writeF(async (efs) => {
await efs.writeFile(secretName, content);
});
logger?.info(`Wrote secret ${secretName} in vault ${vault.vaultId}`);
}

export {
addSecret,
updateSecret,
Expand All @@ -273,4 +288,5 @@ export {
mkdir,
addSecretDirectory,
listSecrets,
writeSecret,
};
Loading

0 comments on commit 5b78ce1

Please sign in to comment.