Compare commits

...

14 Commits

Author SHA1 Message Date
Richard Simpson 4edbc9a77a chore: fix step output syntax 2020-02-20 11:38:11 -06:00
Richard Simpson 5357098084 fix(tests): fix unit tests and improve e2e 2020-02-20 11:24:04 -06:00
dependabot-preview[bot] 3dfe7ff808 chore(deps): bump got from 10.5.7 to 10.6.0
Bumps [got](https://github.com/sindresorhus/got) from 10.5.7 to 10.6.0.
- [Release notes](https://github.com/sindresorhus/got/releases)
- [Commits](https://github.com/sindresorhus/got/compare/v10.5.7...v10.6.0)

Signed-off-by: dependabot-preview[bot] <support@dependabot.com>
2020-02-20 17:14:12 +00:00
dependabot-preview[bot] 1049321b7a chore(deps-dev): bump @types/jest from 24.0.18 to 25.1.3
Bumps [@types/jest](https://github.com/DefinitelyTyped/DefinitelyTyped/tree/HEAD/types/jest) from 24.0.18 to 25.1.3.
- [Release notes](https://github.com/DefinitelyTyped/DefinitelyTyped/releases)
- [Commits](https://github.com/DefinitelyTyped/DefinitelyTyped/commits/HEAD/types/jest)

Signed-off-by: dependabot-preview[bot] <support@dependabot.com>
2020-02-20 17:14:05 +00:00
Richard Simpson ec10b5e257 feat: add ability to retrieve secrets via ouputs 2020-02-20 11:13:47 -06:00
Richard Simpson 7d1d7d26ad fix(build): bump build file 2020-02-20 01:48:49 -06:00
dependabot-preview[bot] f9753d75ef chore(deps-dev): bump jest from 24.9.0 to 25.1.0
Bumps [jest](https://github.com/facebook/jest) from 24.9.0 to 25.1.0.
- [Release notes](https://github.com/facebook/jest/releases)
- [Changelog](https://github.com/facebook/jest/blob/master/CHANGELOG.md)
- [Commits](https://github.com/facebook/jest/compare/v24.9.0...v25.1.0)

Signed-off-by: dependabot-preview[bot] <support@dependabot.com>
2020-02-20 07:45:21 +00:00
Richard Simpson 11b4c03a67 fix(deps): fix comma issue in export variables 2020-02-20 01:43:53 -06:00
dependabot-preview[bot] bd26be00ff chore(deps): bump @actions/core from 1.1.1 to 1.2.2
Bumps [@actions/core](https://github.com/actions/toolkit/tree/HEAD/packages/core) from 1.1.1 to 1.2.2.
- [Release notes](https://github.com/actions/toolkit/releases)
- [Changelog](https://github.com/actions/toolkit/blob/master/packages/core/RELEASES.md)
- [Commits](https://github.com/actions/toolkit/commits/HEAD/packages/core)

Signed-off-by: dependabot-preview[bot] <support@dependabot.com>
2020-02-20 07:42:55 +00:00
dependabot-preview[bot] 452a071f5c chore(deps): bump got from 10.2.2 to 10.5.7
Bumps [got](https://github.com/sindresorhus/got) from 10.2.2 to 10.5.7.
- [Release notes](https://github.com/sindresorhus/got/releases)
- [Commits](https://github.com/sindresorhus/got/compare/v10.2.2...v10.5.7)

Signed-off-by: dependabot-preview[bot] <support@dependabot.com>
2020-02-20 07:41:41 +00:00
dependabot-preview[bot] a11e5a2e84 chore(deps-dev): bump @zeit/ncc from 0.20.5 to 0.21.1
Bumps [@zeit/ncc](https://github.com/zeit/ncc) from 0.20.5 to 0.21.1.
- [Release notes](https://github.com/zeit/ncc/releases)
- [Commits](https://github.com/zeit/ncc/compare/0.20.5...0.21.1)

Signed-off-by: dependabot-preview[bot] <support@dependabot.com>
2020-02-20 07:41:00 +00:00
dependabot-preview[bot] 7f552b0d14 chore(deps): [security] bump https-proxy-agent from 2.2.2 to 2.2.4
Bumps [https-proxy-agent](https://github.com/TooTallNate/node-https-proxy-agent) from 2.2.2 to 2.2.4. **This update includes security fixes.**
- [Release notes](https://github.com/TooTallNate/node-https-proxy-agent/releases)
- [Commits](https://github.com/TooTallNate/node-https-proxy-agent/compare/2.2.2...2.2.4)

Signed-off-by: dependabot-preview[bot] <support@dependabot.com>
2020-02-20 07:39:46 +00:00
Richard Simpson 20f954e024 chore: add place to buy me a coffee 2020-02-06 09:19:22 -06:00
Richard Simpson a884aa8e59 docs: clarify other engine support. 2020-02-05 16:37:16 -06:00
9 changed files with 5790 additions and 1778 deletions
+1
View File
@@ -0,0 +1 @@
ko_fi: richicoder
+3
View File
@@ -112,6 +112,7 @@ jobs:
VAULT_PORT: ${{ job.services.vault.ports[8200] }} VAULT_PORT: ${{ job.services.vault.ports[8200] }}
- name: use vault action (default K/V version 2) - name: use vault action (default K/V version 2)
uses: ./ uses: ./
id: kv-secrets
with: with:
url: http://localhost:${{ job.services.vault.ports[8200] }} url: http://localhost:${{ job.services.vault.ports[8200] }}
token: testtoken token: testtoken
@@ -140,6 +141,8 @@ jobs:
/cubbyhole/test zip | NAMED_CUBBYSECRET ; /cubbyhole/test zip | NAMED_CUBBYSECRET ;
- name: verify - name: verify
run: npm run test:e2e run: npm run test:e2e
env:
OTHER_SECRET_OUTPUT: ${{ steps.kv-secrets.outputs.otherSecret }}
publish: publish:
if: github.event_name == 'push' && contains(github.ref, 'master') if: github.event_name == 'push' && contains(github.ref, 'master')
+43 -6
View File
@@ -2,7 +2,7 @@
A helper action for easily pulling secrets from HashiCorp Vault™. A helper action for easily pulling secrets from HashiCorp Vault™.
By default, this action pulls from [Version 2](https://www.vaultproject.io/docs/secrets/kv/kv-v2/) of the K/V Engine. See examples below for how to [use v1](#using-kv-version-1) as well as non-K/V engines. By default, this action pulls from [Version 2](https://www.vaultproject.io/docs/secrets/kv/kv-v2/) of the K/V Engine. See examples below for how to [use v1](#using-kv-version-1) as well as [other non-K/V engines](#other-secret-engines).
## Example Usage ## Example Usage
@@ -52,7 +52,7 @@ The `secrets` parameter is a set of multiple secret requests separated by the `;
Each secret request is comprised of the `path` and the `key` of the desired secret, and optionally the desired Env Var output name. Each secret request is comprised of the `path` and the `key` of the desired secret, and optionally the desired Env Var output name.
```raw ```raw
{{ Secret Path }} {{ Secret Key }} | {{ Output Environment Variable Name }} {{ Secret Path }} {{ Secret Key }} | {{ Output Variable Name }}
``` ```
### Simple Key ### Simple Key
@@ -64,15 +64,28 @@ with:
secrets: ci npmToken secrets: ci npmToken
``` ```
`vault-action` will automatically normalize the given data key, and output: `vault-action` will automatically normalize the given secret selector key, and set the follow as environment variables for the following steps in the current job:
```bash ```bash
NPMTOKEN=somelongtoken NPMTOKEN=somelongtoken
``` ```
### Set Environment Variable Name You can also access the secret via ouputs:
However, if you want to set it to a specific environmental variable, say `NPM_TOKEN`, you could do this instead: ```yaml
steps:
# ...
- name: Import Secrets
id: secrets
# Import config...
- name: Sensitive Operation
run: "my-cli --token '${{ steps.secrets.outputs.npmToken }}'"
```
### Set Output Variable Name
However, if you want to set it to a specific name, say `NPM_TOKEN`, you could do this instead:
```yaml ```yaml
with: with:
@@ -85,6 +98,17 @@ With that, `vault-action` will now use your requested name and output:
NPM_TOKEN=somelongtoken NPM_TOKEN=somelongtoken
``` ```
```yaml
steps:
# ...
- name: Import Secrets
id: secrets
# Import config...
- name: Sensitive Operation
run: "my-cli --token '${{ steps.secrets.outputs.NPM_TOKEN }}'"
```
### Multiple Secrets ### Multiple Secrets
This action can take multi-line input, so say you had your AWS keys stored in a path and wanted to retrieve both of them. You can do: This action can take multi-line input, so say you had your AWS keys stored in a path and wanted to retrieve both of them. You can do:
@@ -147,7 +171,20 @@ with:
Resulting in: Resulting in:
```bash ```bash
FOO=bar MY_KEY=zap FOO=bar
MY_KEY=zap
```
```yaml
steps:
# ...
- name: Import Secrets
id: secrets
# Import config...
- name: Sensitive Operation
run: "my-cli --token '${{ steps.secrets.outputs.foo }}'"
- name: Another Sensitive Operation
run: "my-cli --token '${{ steps.secrets.outputs.MY_KEY }}'"
``` ```
Secrets pulled from the same `Secret Path` are cached by default. So if you, for example, are using the `aws` engine and retrieve a key, only a single key for a given path is returned. Secrets pulled from the same `Secret Path` are cached by default. So if you, for example, are using the `aws` engine and retrieve a key, only a single key for a given path is returned.
+38 -15
View File
@@ -1,4 +1,8 @@
// @ts-check
// @ts-ignore
const core = require('@actions/core'); const core = require('@actions/core');
// @ts-ignore
const command = require('@actions/core/lib/command'); const command = require('@actions/core/lib/command');
const got = require('got'); const got = require('got');
@@ -36,6 +40,7 @@ async function exportSecrets() {
options.headers["X-Vault-Namespace"] = vaultNamespace; options.headers["X-Vault-Namespace"] = vaultNamespace;
} }
/** @type {any} */
const result = await got.post(`${vaultUrl}/v1/auth/approle/login`, options); const result = await got.post(`${vaultUrl}/v1/auth/approle/login`, options);
if (result && result.body && result.body.auth && result.body.auth.client_token) { if (result && result.body && result.body.auth && result.body.auth.client_token) {
vaultToken = result.body.auth.client_token; vaultToken = result.body.auth.client_token;
@@ -64,7 +69,7 @@ async function exportSecrets() {
const responseCache = new Map(); const responseCache = new Map();
for (const secretRequest of secretRequests) { for (const secretRequest of secretRequests) {
const { secretPath, outputName, secretSelector, isJSONPath } = secretRequest; const { secretPath, outputVarName, envVarName, secretSelector, isJSONPath } = secretRequest;
const requestOptions = { const requestOptions = {
headers: { headers: {
'X-Vault-Token': vaultToken 'X-Vault-Token': vaultToken
@@ -98,13 +103,22 @@ async function exportSecrets() {
let dataDepth = isJSONPath === true ? 0 : kvRequest === false ? 1 : kvVersion; let dataDepth = isJSONPath === true ? 0 : kvRequest === false ? 1 : kvVersion;
const secretData = getResponseData(body, dataDepth); const secretData = getResponseData(body, dataDepth);
const value = selectData(secretData, secretSelector); const value = selectData(secretData, secretSelector, isJSONPath);
command.issue('add-mask', value); command.issue('add-mask', value);
core.exportVariable(outputName, `${value}`); core.exportVariable(envVarName, `${value}`);
core.debug(`${secretPath} => ${outputName}`); core.setOutput(outputVarName, `${value}`);
core.debug(`${secretPath} => outputs.${outputVarName} | env.${envVarName}`);
} }
}; };
/** @typedef {Object} SecretRequest
* @property {string} secretPath
* @property {string} envVarName
* @property {string} outputVarName
* @property {string} secretSelector
* @property {boolean} isJSONPath
*/
/** /**
* Parses a secrets input string into key paths and their resulting environment variable name. * Parses a secrets input string into key paths and their resulting environment variable name.
* @param {string} secretsInput * @param {string} secretsInput
@@ -116,18 +130,18 @@ function parseSecretsInput(secretsInput) {
.map(key => key.trim()) .map(key => key.trim())
.filter(key => key.length !== 0); .filter(key => key.length !== 0);
/** @type {{ secretPath: string; outputName: string; dataKey: string; }[]} */ /** @type {SecretRequest[]} */
const output = []; const output = [];
for (const secret of secrets) { for (const secret of secrets) {
let path = secret; let path = secret;
let outputName = null; let outputVarName = null;
const renameSigilIndex = secret.lastIndexOf('|'); const renameSigilIndex = secret.lastIndexOf('|');
if (renameSigilIndex > -1) { if (renameSigilIndex > -1) {
path = secret.substring(0, renameSigilIndex).trim(); path = secret.substring(0, renameSigilIndex).trim();
outputName = secret.substring(renameSigilIndex + 1).trim(); outputVarName = secret.substring(renameSigilIndex + 1).trim();
if (outputName.length < 1) { if (outputVarName.length < 1) {
throw Error(`You must provide a value when mapping a secret to a name. Input: "${secret}"`); throw Error(`You must provide a value when mapping a secret to a name. Input: "${secret}"`);
} }
} }
@@ -138,21 +152,29 @@ function parseSecretsInput(secretsInput) {
.filter(part => part.length !== 0); .filter(part => part.length !== 0);
if (pathParts.length !== 2) { if (pathParts.length !== 2) {
throw Error(`You must provide a valid path and key. Input: "${secret}"`) throw Error(`You must provide a valid path and key. Input: "${secret}"`);
} }
const [secretPath, secretSelector] = pathParts; const [secretPath, secretSelector] = pathParts;
// If we're not using a mapped name, normalize the key path into a variable name. const isJSONPath = secretSelector.includes('.');
if (!outputName) {
outputName = normalizeOutputKey(secretSelector); if (isJSONPath && !outputVarName) {
throw Error(`You must provide a name for the output key when using json selectors. Input: "${secret}"`);
}
let envVarName = outputVarName;
if (!outputVarName) {
outputVarName = secretSelector;
envVarName = normalizeOutputKey(outputVarName);
} }
output.push({ output.push({
secretPath, secretPath,
outputName, envVarName,
outputVarName,
secretSelector, secretSelector,
isJSONPath: secretSelector.startsWith('$') isJSONPath
}); });
} }
return output; return output;
@@ -161,7 +183,7 @@ function parseSecretsInput(secretsInput) {
/** /**
* Parses a JSON response and returns the secret data * Parses a JSON response and returns the secret data
* @param {string} responseBody * @param {string} responseBody
* @param {number} kvVersion * @param {number} dataLevel
*/ */
function getResponseData(responseBody, dataLevel) { function getResponseData(responseBody, dataLevel) {
let secretData = JSON.parse(responseBody); let secretData = JSON.parse(responseBody);
@@ -193,6 +215,7 @@ function normalizeOutputKey(dataKey) {
return dataKey.replace('/', '__').replace(/[^\w-]/, '').toUpperCase(); return dataKey.replace('/', '__').replace(/[^\w-]/, '').toUpperCase();
} }
// @ts-ignore
function parseBoolInput(input) { function parseBoolInput(input) {
if (input === null || input === undefined || input.trim() === '') { if (input === null || input === undefined || input.trim() === '') {
return null; return null;
+15 -6
View File
@@ -18,7 +18,8 @@ describe('parseSecretsInput', () => {
expect(output).toContainEqual({ expect(output).toContainEqual({
secretPath: 'test', secretPath: 'test',
secretSelector: 'key', secretSelector: 'key',
outputName: 'KEY', outputVarName: 'key',
envVarName: 'KEY',
isJSONPath: false isJSONPath: false
}); });
}); });
@@ -27,7 +28,8 @@ describe('parseSecretsInput', () => {
const output = parseSecretsInput('test key|testName'); const output = parseSecretsInput('test key|testName');
expect(output).toHaveLength(1); expect(output).toHaveLength(1);
expect(output[0]).toMatchObject({ expect(output[0]).toMatchObject({
outputName: 'testName', outputVarName: 'testName',
envVarName: 'testName',
}); });
}); });
@@ -58,10 +60,12 @@ describe('parseSecretsInput', () => {
expect(output).toHaveLength(2); expect(output).toHaveLength(2);
expect(output[0]).toMatchObject({ expect(output[0]).toMatchObject({
outputName: 'A', outputVarName: 'a',
envVarName: 'A',
}); });
expect(output[1]).toMatchObject({ expect(output[1]).toMatchObject({
outputName: 'secondName', outputVarName: 'secondName',
envVarName: 'secondName'
}); });
}); });
@@ -76,10 +80,12 @@ describe('parseSecretsInput', () => {
secretPath: 'first', secretPath: 'first',
}); });
expect(output[1]).toMatchObject({ expect(output[1]).toMatchObject({
outputName: 'B', outputVarName: 'b',
envVarName: 'B'
}); });
expect(output[2]).toMatchObject({ expect(output[2]).toMatchObject({
outputName: 'SOME_C', outputVarName: 'SOME_C',
envVarName: 'SOME_C',
}); });
}) })
}); });
@@ -172,6 +178,7 @@ describe('exportSecrets', () => {
await exportSecrets(); await exportSecrets();
expect(core.exportVariable).toBeCalledWith('KEY', '1'); expect(core.exportVariable).toBeCalledWith('KEY', '1');
expect(core.setOutput).toBeCalledWith('key', '1');
}); });
it('mapped secret retrieval', async () => { it('mapped secret retrieval', async () => {
@@ -183,6 +190,7 @@ describe('exportSecrets', () => {
await exportSecrets(); await exportSecrets();
expect(core.exportVariable).toBeCalledWith('TEST_NAME', '1'); expect(core.exportVariable).toBeCalledWith('TEST_NAME', '1');
expect(core.setOutput).toBeCalledWith('TEST_NAME', '1');
}); });
it('simple secret retrieval from K/V v1', async () => { it('simple secret retrieval from K/V v1', async () => {
@@ -197,5 +205,6 @@ describe('exportSecrets', () => {
await exportSecrets(); await exportSecrets();
expect(core.exportVariable).toBeCalledWith('KEY', '1'); expect(core.exportVariable).toBeCalledWith('KEY', '1');
expect(core.setOutput).toBeCalledWith('key', '1');
}); });
}); });
+1058 -314
View File
File diff suppressed because it is too large Load Diff
+1
View File
@@ -3,6 +3,7 @@ describe('e2e', () => {
expect(process.env.SECRET).toBe("SUPERSECRET"); expect(process.env.SECRET).toBe("SUPERSECRET");
expect(process.env.NAMED_SECRET).toBe("SUPERSECRET"); expect(process.env.NAMED_SECRET).toBe("SUPERSECRET");
expect(process.env.OTHERSECRET).toBe("OTHERSUPERSECRET"); expect(process.env.OTHERSECRET).toBe("OTHERSUPERSECRET");
expect(process.env.OTHER_SECRET_OUTPUT).toBe("OTHERSUPERSECRET");
expect(process.env.ALTSECRET).toBe("CUSTOMSECRET"); expect(process.env.ALTSECRET).toBe("CUSTOMSECRET");
expect(process.env.NAMED_ALTSECRET).toBe("CUSTOMSECRET"); expect(process.env.NAMED_ALTSECRET).toBe("CUSTOMSECRET");
expect(process.env.OTHERALTSECRET).toBe("OTHERCUSTOMSECRET"); expect(process.env.OTHERALTSECRET).toBe("OTHERCUSTOMSECRET");
+4627 -1433
View File
File diff suppressed because it is too large Load Diff
+4 -4
View File
@@ -38,13 +38,13 @@
}, },
"homepage": "https://github.com/RichiCoder1/vault-action#readme", "homepage": "https://github.com/RichiCoder1/vault-action#readme",
"dependencies": { "dependencies": {
"@actions/core": "^1.1.1", "@actions/core": "^1.2.2",
"got": "^10.2.2" "got": "^10.2.2"
}, },
"devDependencies": { "devDependencies": {
"@types/jest": "^24.0.18", "@types/jest": "^25.1.3",
"@zeit/ncc": "^0.20.5", "@zeit/ncc": "^0.21.1",
"jest": "^24.9.0", "jest": "^25.1.0",
"jest-when": "^2.7.0", "jest-when": "^2.7.0",
"semantic-release": "^15.13.24" "semantic-release": "^15.13.24"
} }