Skip to content

Commit

Permalink
Merge pull request #217 from MatrixAI/feature-eng-335-support-spaces-…
Browse files Browse the repository at this point in the history
…in-secret-paths

Add support for unix style paths for secret paths
  • Loading branch information
tegefaulkes authored Jun 28, 2024
2 parents f511693 + b1b5030 commit 1545cc5
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 3 deletions.
5 changes: 2 additions & 3 deletions src/utils/parsers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@ import * as gestaltsUtils from 'polykey/dist/gestalts/utils';
import * as networkUtils from 'polykey/dist/network/utils';
import * as nodesUtils from 'polykey/dist/nodes/utils';

const secretPathRegex =
/^([\w-]+)(?::)([\w\-\\\/\.\$]+)(?:=)?([a-zA-Z_][\w]+)?$/;
const secretPathRegex = /^([\w-]+)(?::)([^\0\\=]+)(?:=)?([a-zA-Z_][\w]+)?$/;
const secretPathEnvRegex =
/^([\w-]+)(?::)([\w\-\\\/\.\$]+)(?:=)?([a-zA-Z_]+[a-zA-Z0-9_]*)?$/;
/^([\w-]+)(?::)([^\0\\=]+)(?:=)?([a-zA-Z_]+[a-zA-Z0-9_]*)?$/;

/**
* Converts a validation parser to commander argument parser
Expand Down
40 changes: 40 additions & 0 deletions tests/secrets/create.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import type { VaultName } from 'polykey/dist/vaults/types';
import path from 'path';
import fs from 'fs';
import { test } from '@fast-check/jest';
import fc from 'fast-check';
import Logger, { LogLevel, StreamHandler } from '@matrixai/logger';
import PolykeyAgent from 'polykey/dist/PolykeyAgent';
import { vaultOps } from 'polykey/dist/vaults';
Expand Down Expand Up @@ -76,4 +78,42 @@ describe('commandCreateSecret', () => {
},
globalThis.defaultTimeout * 2,
);
const fileNameArb = fc.stringMatching(/^[^\0\\/=]$/);
test.prop([fileNameArb, fileNameArb], { numRuns: 10 })(
'secrets handle unix style paths for secrets',
async (directoryName, secretName) => {
await polykeyAgent.vaultManager.stop();
await polykeyAgent.vaultManager.start({ fresh: true });
const vaultName = 'Vault1' as VaultName;
const vaultId = await polykeyAgent.vaultManager.createVault(vaultName);
const secretPath = path.join(dataDir, 'secret');
await fs.promises.writeFile(secretPath, 'this is a secret');
const vaultsSecretPath = path.join(directoryName, secretName);

command = [
'secrets',
'create',
'-np',
dataDir,
secretPath,
`${vaultName}:${vaultsSecretPath}`,
];

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

await polykeyAgent.vaultManager.withVaults([vaultId], async (vault) => {
const list = await vaultOps.listSecrets(vault);
expect(list.sort()).toStrictEqual([vaultsSecretPath]);
expect(
(await vaultOps.getSecret(vault, vaultsSecretPath)).toString(),
).toStrictEqual('this is a secret');
});
},
);
});

0 comments on commit 1545cc5

Please sign in to comment.