Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for unix style paths for secret paths #217

Merged
merged 1 commit into from
Jun 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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');
});
},
);
});