Skip to content

Commit

Permalink
WIP: Testing secret env in a subshell
Browse files Browse the repository at this point in the history
  • Loading branch information
Scott committed Nov 16, 2021
1 parent 0fa5f5c commit ae7bb01
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 35 deletions.
111 changes: 90 additions & 21 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@
"jose": "^3.12.3",
"level": "^7.0.0",
"lexicographic-integer": "^1.1.0",
"multiformats": "^9.4.8",
"nexpect": "^0.6.0",
"node-forge": "^0.10.0",
"pako": "^1.0.11",
"prompts": "^2.4.1",
Expand All @@ -102,7 +104,6 @@
"threads": "^1.6.5",
"ts-custom-error": "^3.2.0",
"utp-native": "^2.5.3",
"multiformats": "^9.4.8",
"uuid": "^8.3.0"
},
"devDependencies": {
Expand All @@ -112,6 +113,7 @@
"@types/google-protobuf": "^3.7.4",
"@types/jest": "^26.0.20",
"@types/level": "^6.0.0",
"@types/nexpect": "^0.4.31",
"@types/node": "^14.14.35",
"@types/node-forge": "^0.9.7",
"@types/pako": "^1.0.2",
Expand Down
3 changes: 0 additions & 3 deletions src/client/rpcVaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -695,9 +695,7 @@ const createVaultRPC = ({
const pattern = directoryMessage.getSecretDirectory();
const id = await utils.parseVaultInput(vaultMessage, vaultManager);
const vault = await vaultManager.openVault(id);
console.log(pattern);
const secretList = await vaultManager.glob(id, pattern);
console.log(secretList);
let secretMessage: secretsPB.Secret;
for (const secretName of secretList) {
const secretContent = await vaultOps.getSecret(vault, secretName);
Expand All @@ -708,7 +706,6 @@ const createVaultRPC = ({
}
await genWritable.next(null);
} catch (err) {
console.log(err);
await genWritable.throw(err);
}
},
Expand Down
14 changes: 7 additions & 7 deletions src/nodes/NodeManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -604,13 +604,13 @@ class NodeManager {
/**
* Retrieves all the vaults for a peers node
*/
@ready(new nodesErrors.ErrorNodeManagerNotStarted())
public async scanNodeVaults(nodeId: string): Promise<Array<string>> {
// Create a connection to another node
const connection = await this.getConnectionToNode(nodeId as NodeId);
// Scan the vaults of the node over the connection
return await connection.scanVaults();
}
// @ready(new nodesErrors.ErrorNodeManagerNotStarted())
// public async scanNodeVaults(nodeId: string): Promise<Array<string>> {
// // Create a connection to another node
// const connection = await this.getConnectionToNode(nodeId as NodeId);
// // Scan the vaults of the node over the connection
// return await connection.scanVaults();
// }

public async clearDB() {
await this.nodeGraph.clearDB();
Expand Down
27 changes: 24 additions & 3 deletions tests/bin/secret.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os from 'os';
import path from 'path';
import fs from 'fs';
import nexpect from 'nexpect';
import Logger, { LogLevel, StreamHandler } from '@matrixai/logger';
import { PolykeyAgent } from '@';
import * as utils from './utils';
Expand Down Expand Up @@ -64,7 +65,7 @@ describe('CLI secrets', () => {
});

describe('commandSecretEnv', () => {
test('should export globbed secrets', async () => {
test('should wrap globbed secrets', async () => {
const vaultName = 'Vault0' as VaultName;
const vault = await polykeyAgent.vaults.createVault(vaultName);

Expand All @@ -74,21 +75,22 @@ describe('CLI secrets', () => {
await vaultOps.addSecret(vault, 'TEST_VAR_3', 'test-3');
await vaultOps.addSecret(vault, 'dir1/dir2/dir3/TEST_VAR_4', 'test-4');

let message = 'export TEST VAR 1=test-1\nexport TEST_VAR_2=test-2\n';
let message = 'TEST VAR 1=test-1\nTEST_VAR_2=test-2\n';

command = [
'secrets',
'env',
'-np',
dataDir,
'-e',
'Vault0:dir1/dir2/*',
];

let result = await utils.pkWithStdio(command);
expect(result.code).toBe(0);
expect(result.stdout).toContain(message);

nexpect.spawn('echo', ['$TEST VAR 1']).expect('');

command = [
'secrets',
'env',
Expand All @@ -103,6 +105,25 @@ describe('CLI secrets', () => {
result = await utils.pkWithStdio(command);
expect(result.code).toBe(0);
expect(result.stdout).toContain(message);
nexpect.spawn('echo', ['$TEST_VAR_3']).expect('test-3');
nexpect.spawn('echo', ['$TEST VAR 1']).expect('test-1');
nexpect.spawn('echo', ['$TEST_VAR_2']).expect('test-2');
nexpect.spawn('echo', ['$TEST_VAR_4']).expect('test-4');
});
test('can export secrets to a bash subshell', async (done) => {
const vaultName = 'Vault000' as VaultName;
const vault = await polykeyAgent.vaults.createVault(vaultName);

await vaultOps.mkdir(vault, 'dir1/dir2/dir3', { recursive: true });
await vaultOps.addSecret(vault, 'dir1/dir2/TEST_VAR_2', 'test-2');

nexpect.spawn('npm', ['run', 'polykey', '--', 'secrets', 'env', '-np', dataDir, '-e', 'Vault000:**/*', 'bash'])
.sendline('echo $TEST_VAR_2')
.sendline('exit')
.run(function (_, stdout) {
expect(stdout).toContain('test-2');
done();
});
});
});

Expand Down

0 comments on commit ae7bb01

Please sign in to comment.