Skip to content

Commit

Permalink
Merge pull request #193 from MatrixAI/feature-eng-152-secrets-edit-co…
Browse files Browse the repository at this point in the history
…mmand-race-condtion

Fix race condition with `secrets edit` command
  • Loading branch information
tegefaulkes authored Jun 7, 2024
2 parents b83c0fe + 0deae29 commit 10c0ae3
Show file tree
Hide file tree
Showing 22 changed files with 226 additions and 393 deletions.
10 changes: 6 additions & 4 deletions src/secrets/CommandEdit.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import type PolykeyClient from 'polykey/dist/PolykeyClient';
import fs from 'fs';
import path from 'path';
import * as errors from '../errors';
import CommandPolykey from '../CommandPolykey';
import * as binUtils from '../utils';
Expand Down Expand Up @@ -62,10 +64,10 @@ class CommandEdit extends CommandPolykey {
meta,
);
const secretContent = response.secretContent;
// Linux
const tmpDir = `${os.tmpdir}/pksecret`;
await this.fs.promises.mkdir(tmpDir);
const tmpFile = `${tmpDir}/pkSecretFile`;
const tmpDir = await fs.promises.mkdtemp(
path.join(os.tmpdir(), 'polykey-'),
);
const tmpFile = path.join(tmpDir, 'pksecret');
await this.fs.promises.writeFile(tmpFile, secretContent);
execSync(`$EDITOR \"${tmpFile}\"`, { stdio: 'inherit' });
let content: Buffer;
Expand Down
15 changes: 3 additions & 12 deletions tests/secrets/create.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,12 @@ describe('commandCreateSecret', () => {
const logger = new Logger('CLI Test', LogLevel.WARN, [new StreamHandler()]);
let dataDir: string;
let polykeyAgent: PolykeyAgent;
let passwordFile: string;
let command: Array<string>;

beforeEach(async () => {
dataDir = await fs.promises.mkdtemp(
path.join(globalThis.tmpDir, 'polykey-test-'),
);
passwordFile = path.join(dataDir, 'passwordFile');
await fs.promises.writeFile(passwordFile, 'password');
polykeyAgent = await PolykeyAgent.createPolykeyAgent({
password,
options: {
Expand All @@ -35,14 +32,6 @@ describe('commandCreateSecret', () => {
},
logger: logger,
});
// Authorize session
await testUtils.pkStdio(
['agent', 'unlock', '-np', dataDir, '--password-file', passwordFile],
{
env: {},
cwd: dataDir,
},
);
});
afterEach(async () => {
await polykeyAgent.stop();
Expand Down Expand Up @@ -70,7 +59,9 @@ describe('commandCreateSecret', () => {
];

const result = await testUtils.pkStdio([...command], {
env: {},
env: {
PK_PASSWORD: password,
},
cwd: dataDir,
});
expect(result.exitCode).toBe(0);
Expand Down
15 changes: 3 additions & 12 deletions tests/secrets/delete.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,12 @@ describe('commandDeleteSecret', () => {
const logger = new Logger('CLI Test', LogLevel.WARN, [new StreamHandler()]);
let dataDir: string;
let polykeyAgent: PolykeyAgent;
let passwordFile: string;
let command: Array<string>;

beforeEach(async () => {
dataDir = await fs.promises.mkdtemp(
path.join(globalThis.tmpDir, 'polykey-test-'),
);
passwordFile = path.join(dataDir, 'passwordFile');
await fs.promises.writeFile(passwordFile, 'password');
polykeyAgent = await PolykeyAgent.createPolykeyAgent({
password,
options: {
Expand All @@ -35,14 +32,6 @@ describe('commandDeleteSecret', () => {
},
logger: logger,
});
// Authorize session
await testUtils.pkStdio(
['agent', 'unlock', '-np', dataDir, '--password-file', passwordFile],
{
env: {},
cwd: dataDir,
},
);
});
afterEach(async () => {
await polykeyAgent.stop();
Expand All @@ -65,7 +54,9 @@ describe('commandDeleteSecret', () => {
command = ['secrets', 'delete', '-np', dataDir, `${vaultName}:MySecret`];

const result = await testUtils.pkStdio([...command], {
env: {},
env: {
PK_PASSWORD: password,
},
cwd: dataDir,
});
expect(result.exitCode).toBe(0);
Expand Down
Loading

0 comments on commit 10c0ae3

Please sign in to comment.