Compare commits

..

13 Commits

Author SHA1 Message Date
JM Faircloth 0b1f582bde simplify more 2023-07-05 16:01:13 -05:00
JM Faircloth 32afcc7f20 remove unused var assignment 2023-07-05 14:33:14 -05:00
JM Faircloth d12e95ba40 remove comment 2023-07-05 13:21:29 -05:00
JM Faircloth 4ef647191c final cleanup 2023-07-05 12:58:41 -05:00
JM Faircloth 1a47f33407 fix test string format 2023-07-05 12:55:58 -05:00
JM Faircloth b239ef37f5 update test check 2023-07-05 12:52:39 -05:00
JM Faircloth ccc7cef6eb fix test string format 2023-07-05 12:49:58 -05:00
JM Faircloth 8e836c6e8e fix ordering of build steps 2023-07-05 12:45:57 -05:00
JM Faircloth a2cae737a3 add debug 2023-07-05 12:43:02 -05:00
JM Faircloth b536ec9ec6 add test cases 2023-07-05 12:39:34 -05:00
JM Faircloth e5605de996 fix lint and pass token to build 2023-07-05 12:30:19 -05:00
JM Faircloth 788264dddd add more tests 2023-07-05 12:25:38 -05:00
JM Faircloth a24b038252 fix secrets stored in JSON format 2023-07-03 15:57:28 -05:00
17 changed files with 178 additions and 610 deletions
+7 -15
View File
@@ -3,29 +3,21 @@ name: Bug report
about: Create a report to help us improve about: Create a report to help us improve
title: "[BUG] " title: "[BUG] "
labels: bug labels: bug
assignees: RichiCoder1
--- ---
## Vault server version **Describe the bug**
v0.0.0
## vault-action version
v0.0.0
## Describe the bug
A clear and concise description of what the bug is. A clear and concise description of what the bug is.
## To Reproduce **To Reproduce**
The yaml of the `vault-action` step, with any sensitive information masked or removed. The yaml of the `vault-action` step, with any sensitive information masked or removed.
## Expected behavior **Expected behavior**
A clear and concise description of what you expected to happen. A clear and concise description of what you expected to happen.
## Log Output **Log Output**
For the most verbose logs, add a secret called For the most verbose logs, [add a secret called `ACTIONS_STEP_DEBUG` with the value `true`](https://github.com/actions/toolkit/blob/main/docs/action-debugging.md). Then, re-run the workflow if possible and post the *raw logs* for the step here with any sensitive information masked or removed.
[`ACTIONS_STEP_DEBUG`](https://github.com/actions/toolkit/blob/main/docs/action-debugging.md)
with the value `true`. Then, re-run the workflow if possible and post the *raw
logs* for the step here with any sensitive information masked or removed.
## Additional context **Additional context**
Add any other context about the problem here. Add any other context about the problem here.
+5 -4
View File
@@ -3,17 +3,18 @@ name: Feature request
about: Suggest an idea for this project about: Suggest an idea for this project
title: "[FEAT] " title: "[FEAT] "
labels: enhancement labels: enhancement
assignees: RichiCoder1
--- ---
## Is your feature request related to a problem? Please describe. **Is your feature request related to a problem? Please describe.**
A clear and concise description of what the problem is. Ex. I'm always frustrated when [...] A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]
## Describe the solution you'd like **Describe the solution you'd like**
A clear and concise description of what you want to happen. A clear and concise description of what you want to happen.
## Describe alternatives you've considered **Describe alternatives you've considered**
A clear and concise description of any alternative solutions or features you've considered. A clear and concise description of any alternative solutions or features you've considered.
## Additional context **Additional context**
Add any other context or screenshots about the feature request here. Add any other context or screenshots about the feature request here.
-21
View File
@@ -1,21 +0,0 @@
### Description
<!--- Description of the change. For example: This PR updates ABC resource so that we can XYZ --->
<!--- If your PR fully resolves and should automatically close the linked issue, use Closes. Otherwise, use Relates --->
Relates OR Closes #0000
### Checklist
- [ ] Added [CHANGELOG](https://github.com/hashicorp/vault-action/blob/master/CHANGELOG.md) entry (only for user-facing changes)
- [ ] Did not commit changes to `dist/index.js` (This is only done for releases by vault-action maintainers)
### Community Note
* Please vote on this pull request by adding a 👍
[reaction](https://blog.github.com/2016-03-10-add-reactions-to-pull-requests-issues-and-comments/)
to the original pull request comment to help the community and maintainers
prioritize this request
* Please do not leave "+1" comments, they generate extra noise for pull request
followers and do not help prioritize the request
+1 -24
View File
@@ -1,29 +1,6 @@
## Unreleased ## Unreleased
## 2.7.5 (January 30, 2024) * Add changes here
Improvements:
* Bump node runtime from node16 to node20 [GH-509](https://github.com/hashicorp/vault-action/pull/509)
* Bump got from 11.8.5 to 11.8.6 [GH-492](https://github.com/hashicorp/vault-action/pull/492)
## 2.7.4 (October 26, 2023)
Features:
* Add ability to specify a wildcard for the key name to get all keys in the path [GH-488](https://github.com/hashicorp/vault-action/pull/488)
## 2.7.3 (July 13, 2023)
Bugs:
* Revert to the handling of secrets in JSON format since v2.1.2 [GH-478](https://github.com/hashicorp/vault-action/pull/478)
## 2.7.2 (July 6, 2023)
Bugs:
* Fix a regression that broke support for secrets in JSON format [GH-473](https://github.com/hashicorp/vault-action/pull/473)
## 2.7.1 (July 3, 2023) ## 2.7.1 (July 3, 2023)
+29 -192
View File
@@ -8,9 +8,6 @@
A helper action for easily pulling secrets from HashiCorp Vault™. A helper action for easily pulling secrets from HashiCorp Vault™.
Note: The Vault Github Action is a read-only action, and in general
is not meant to modify Vaults state.
<!-- TOC --> <!-- TOC -->
- [Vault GitHub Action](#vault-github-action) - [Vault GitHub Action](#vault-github-action)
@@ -25,7 +22,6 @@ is not meant to modify Vaults state.
- [Userpass](#userpass) - [Userpass](#userpass)
- [Ldap](#ldap) - [Ldap](#ldap)
- [Other Auth Methods](#other-auth-methods) - [Other Auth Methods](#other-auth-methods)
- [Custom Path](#custom-path-name)
- [Key Syntax](#key-syntax) - [Key Syntax](#key-syntax)
- [Simple Key](#simple-key) - [Simple Key](#simple-key)
- [Set Output Variable Name](#set-output-variable-name) - [Set Output Variable Name](#set-output-variable-name)
@@ -80,7 +76,7 @@ For example, a common pattern is to save all the secrets in a JSON file:
- name: Step following 'Import Secrets' - name: Step following 'Import Secrets'
run: | run: |
touch secrets.json touch secrets.json
echo '${{ toJson(steps.import-secrets.outputs) }}' >> secrets.json echo "${{ toJson(steps.import-secrets.outputs) }}" >> secrets.json
# ... # ...
``` ```
@@ -301,20 +297,6 @@ with:
If any other method is specified and you provide an `authPayload`, the action will If any other method is specified and you provide an `authPayload`, the action will
attempt to `POST` to `auth/${method}/login` with the provided payload and parse out the client token. attempt to `POST` to `auth/${method}/login` with the provided payload and parse out the client token.
### Custom Path Name
Auth methods at custom path names can be configured using the [`path`](#path) parameter
```yaml
with:
url: https://vault.mycompany.com:8200
caCertificate: ${{ secrets.VAULT_CA_CERT }}
path: my-custom-path
method: userpass
username: ${{ secrets.VAULT_USERNAME }}
password: ${{ secrets.VAULT_PASSWORD }}
```
## Key Syntax ## Key Syntax
The `secrets` parameter is a set of multiple secret requests separated by the `;` character. The `secrets` parameter is a set of multiple secret requests separated by the `;` character.
@@ -391,13 +373,6 @@ with:
secret/data/ci/aws accessKey | AWS_ACCESS_KEY_ID ; secret/data/ci/aws accessKey | AWS_ACCESS_KEY_ID ;
secret/data/ci/aws secretKey | AWS_SECRET_ACCESS_KEY secret/data/ci/aws secretKey | AWS_SECRET_ACCESS_KEY
``` ```
You can specify a wildcard * for the key name to get all keys in the path. If you provide an output name with the wildcard, the name will be prepended to the key name:
```yaml
with:
secrets: |
secret/data/ci/aws * | MYAPP_ ;
```
## Other Secret Engines ## Other Secret Engines
@@ -482,172 +457,34 @@ steps:
Here are all the inputs available through `with`: Here are all the inputs available through `with`:
### `url` | Input | Description | Default | Required |
| ------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------- | ------- | -------- |
**Type: `string`**\ | `url` | The URL for the vault endpoint | | ✔ |
**Required** | `secrets` | A semicolon-separated list of secrets to retrieve. These will automatically be converted to environmental variable keys. See README for more details | | |
| `namespace` | The Vault namespace from which to query secrets. Vault Enterprise only, unset by default | | |
The URL for the Vault endpoint. | `method` | The method to use to authenticate with Vault. | `token` | |
| `role` | Vault role for specified auth method | | |
### `secrets` | `path` | Custom vault path, if the auth method was enabled at a different path | | |
| `token` | The Vault Token to be used to authenticate with Vault | | |
**Type: `string`** | `roleId` | The Role Id for App Role authentication | | |
| `secretId` | The Secret Id for App Role authentication | | |
A semicolon-separated list of secrets to retrieve. These will automatically be | `githubToken` | The Github Token to be used to authenticate with Vault | | |
converted to environmental variable keys. See [Key Syntax](#key-syntax) for | `jwtPrivateKey` | Base64 encoded Private key to sign JWT | | |
more details. | `jwtKeyPassword` | Password for key stored in jwtPrivateKey (if needed) | | |
| `jwtGithubAudience` | Identifies the recipient ("aud" claim) that the JWT is intended for |`sigstore`| |
### `namespace` | `jwtTtl` | Time in seconds, after which token expires | | 3600 |
| `kubernetesTokenPath` | The path to the service-account secret with the jwt token for kubernetes based authentication |`/var/run/secrets/kubernetes.io/serviceaccount/token` | |
**Type: `string`** | `username` | The username of the user to log in to Vault as. Available to both Userpass and LDAP auth methods | | |
| `password` | The password of the user to log in to Vault as. Available to both Userpass and LDAP auth methods | | |
The Vault namespace from which to query secrets. Vault Enterprise only, unset by default. | `authPayload` | The JSON payload to be sent to Vault when using a custom authentication method. | | |
| `extraHeaders` | A string of newline separated extra headers to include on every request. | | |
### `method` | `exportEnv` | Whether or not export secrets as environment variables. | `true` | |
| `exportToken` | Whether or not export Vault token as environment variables (i.e VAULT_TOKEN). | `false` | |
**Type: `string`**\ | `outputToken` | Whether or not to set the `vault_token` output to contain the Vault token after authentication. | `false` | |
**Default: `token`** | `caCertificate` | Base64 encoded CA certificate the server certificate was signed with. | | |
| `clientCertificate` | Base64 encoded client certificate the action uses to authenticate with Vault when mTLS is enabled. | | |
The method to use to authenticate with Vault. | `clientKey` | Base64 encoded client key the action uses to authenticate with Vault when mTLS is enabled. | | |
| `tlsSkipVerify` | When set to true, disables verification of server certificates when testing the action. | `false` | |
### `role`
**Type: `string`**
Vault role for the specified auth method.
### `path`
**Type: `string`**
The Vault path for the auth method.
### `token`
**Type: `string`**
The Vault token to be used to authenticate with Vault.
### `roleId`
**Type: `string`**
The role ID for App Role authentication.
### `secretId`
**Type: `string`**
The secret ID for App Role authentication.
### `githubToken`
**Type: `string`**
The Github Token to be used to authenticate with Vault.
### `jwtPrivateKey`
**Type: `string`**
Base64 encoded private key to sign the JWT.
### `jwtKeyPassword`
**Type: `string`**
Password for key stored in `jwtPrivateKey` (if needed).
### `jwtGithubAudience`
**Type: `string`**\
**Default: `sigstore`**
Identifies the recipient ("aud" claim) that the JWT is intended for.
### `jwtTtl`
**Type: `string`**\
**Default: `3600`**
Time in seconds, after which token expires.
### `kubernetesTokenPath`
**Type: `string`**\
**Default: `/var/run/secrets/kubernetes.io/serviceaccount/token`**
The path to the service-account secret with the jwt token for kubernetes based authentication.
### `username`
**Type: `string`**
The username of the user to log in to Vault as. Available to both Userpass and LDAP auth methods.
### `password`
**Type: `string`**
The password of the user to log in to Vault as. Available to both Userpass and LDAP auth methods.
### `authPayload`
**Type: `string`**
The JSON payload to be sent to Vault when using a custom authentication method.
### `extraHeaders`
**Type: `string`**
A string of newline separated extra headers to include on every request.
### `exportEnv`
**Type: `string`**\
**Default: `true`**
Whether or not to export secrets as environment variables.
### `exportToken`
**Type: `string`**\
**Default: `false`**
Whether or not export Vault token as environment variables (i.e VAULT_TOKEN).
### `outputToken`
**Type: `string`**\
**Default: `false`**
Whether or not to set the `vault_token` output to contain the Vault token after authentication.
### `caCertificate`
**Type: `string`**
Base64 encoded CA certificate the server certificate was signed with. Defaults to CAs provided by Mozilla.
### `clientCertificate`
**Type: `string`**
Base64 encoded client certificate the action uses to authenticate with Vault when mTLS is enabled.
### `clientKey`
**Type: `string`**
Base64 encoded client key the action uses to authenticate with Vault when mTLS is enabled.
### `tlsSkipVerify`
**Type: `string`**\
**Default: `false`**
When set to true, disables verification of server certificates when testing the action.
## Masking - Hiding Secrets from Logs ## Masking - Hiding Secrets from Logs
+7 -7
View File
@@ -1,4 +1,4 @@
name: 'HashiCorp Vault' name: 'Vault Secrets'
description: 'A Github Action that allows you to consume HashiCorp Vault™ secrets as secure environment variables' description: 'A Github Action that allows you to consume HashiCorp Vault™ secrets as secure environment variables'
inputs: inputs:
url: url:
@@ -18,16 +18,16 @@ inputs:
description: 'Vault role for specified auth method' description: 'Vault role for specified auth method'
required: false required: false
path: path:
description: 'The Vault path for the auth method.' description: 'Custom Vault path, if the auth method was mounted at a different path'
required: false required: false
token: token:
description: 'The Vault token to be used to authenticate with Vault' description: 'The Vault Token to be used to authenticate with Vault'
required: false required: false
roleId: roleId:
description: 'The role ID for App Role authentication' description: 'The Role Id for App Role authentication'
required: false required: false
secretId: secretId:
description: 'The secret ID for App Role authentication' description: 'The Secret Id for App Role authentication'
required: false required: false
githubToken: githubToken:
description: 'The Github Token to be used to authenticate with Vault' description: 'The Github Token to be used to authenticate with Vault'
@@ -61,7 +61,7 @@ inputs:
default: 'false' default: 'false'
required: false required: false
caCertificate: caCertificate:
description: 'Base64 encoded CA certificate the server certificate was signed with. Defaults to CAs provided by Mozilla.' description: 'Base64 encoded CA certificate to verify the Vault server certificate.'
required: false required: false
clientCertificate: clientCertificate:
description: 'Base64 encoded client certificate for mTLS communication with the Vault server.' description: 'Base64 encoded client certificate for mTLS communication with the Vault server.'
@@ -90,7 +90,7 @@ inputs:
description: 'The encoding type of the secret to decode. If not specified, the secret will not be decoded. Supported values: base64, hex, utf8' description: 'The encoding type of the secret to decode. If not specified, the secret will not be decoded. Supported values: base64, hex, utf8'
required: false required: false
runs: runs:
using: 'node20' using: 'node16'
main: 'dist/index.js' main: 'dist/index.js'
branding: branding:
icon: 'unlock' icon: 'unlock'
+36 -134
View File
@@ -3702,7 +3702,6 @@ function asPromise(normalizedOptions) {
request._beforeError(new types_1.HTTPError(response)); request._beforeError(new types_1.HTTPError(response));
return; return;
} }
request.destroy();
resolve(request.options.resolveBodyOnly ? response.body : response); resolve(request.options.resolveBodyOnly ? response.body : response);
}); });
const onError = (error) => { const onError = (error) => {
@@ -18517,9 +18516,6 @@ const core = __nccwpck_require__(2186);
const command = __nccwpck_require__(7351); const command = __nccwpck_require__(7351);
const got = (__nccwpck_require__(3061)["default"]); const got = (__nccwpck_require__(3061)["default"]);
const jsonata = __nccwpck_require__(4245); const jsonata = __nccwpck_require__(4245);
const { normalizeOutputKey } = __nccwpck_require__(1608);
const { WILDCARD } = __nccwpck_require__(4438);
const { auth: { retrieveToken }, secrets: { getSecrets } } = __nccwpck_require__(4351); const { auth: { retrieveToken }, secrets: { getSecrets } } = __nccwpck_require__(4351);
const AUTH_METHODS = ['approle', 'token', 'github', 'jwt', 'kubernetes', 'ldap', 'userpass']; const AUTH_METHODS = ['approle', 'token', 'github', 'jwt', 'kubernetes', 'ldap', 'userpass'];
@@ -18688,7 +18684,7 @@ function parseSecretsInput(secretsInput) {
const selectorAst = jsonata(selectorQuoted).ast(); const selectorAst = jsonata(selectorQuoted).ast();
const selector = selectorQuoted.replace(new RegExp('"', 'g'), ''); const selector = selectorQuoted.replace(new RegExp('"', 'g'), '');
if (selector !== WILDCARD && (selectorAst.type !== "path" || selectorAst.steps[0].stages) && selectorAst.type !== "string" && !outputVarName) { if ((selectorAst.type !== "path" || selectorAst.steps[0].stages) && selectorAst.type !== "string" && !outputVarName) {
throw Error(`You must provide a name for the output key when using json selectors. Input: "${secret}"`); throw Error(`You must provide a name for the output key when using json selectors. Input: "${secret}"`);
} }
@@ -18708,6 +18704,20 @@ function parseSecretsInput(secretsInput) {
return output; return output;
} }
/**
* Replaces any dot chars to __ and removes non-ascii charts
* @param {string} dataKey
* @param {boolean=} isEnvVar
*/
function normalizeOutputKey(dataKey, isEnvVar = false) {
let outputKey = dataKey
.replace('.', '__').replace(new RegExp('-', 'g'), '').replace(/[^\p{L}\p{N}_-]/gu, '');
if (isEnvVar) {
outputKey = outputKey.toUpperCase();
}
return outputKey;
}
/** /**
* @param {string} inputKey * @param {string} inputKey
* @param {any} inputOptions * @param {any} inputOptions
@@ -18736,11 +18746,11 @@ function parseHeadersInput(inputKey, inputOptions) {
module.exports = { module.exports = {
exportSecrets, exportSecrets,
parseSecretsInput, parseSecretsInput,
parseHeadersInput, normalizeOutputKey,
parseHeadersInput
}; };
/***/ }), /***/ }),
/***/ 4915: /***/ 4915:
@@ -18907,17 +18917,6 @@ module.exports = {
}; };
/***/ }),
/***/ 4438:
/***/ ((module) => {
const WILDCARD = '*';
module.exports = {
WILDCARD
};
/***/ }), /***/ }),
/***/ 4351: /***/ 4351:
@@ -18937,8 +18936,8 @@ module.exports = {
/***/ ((module, __unused_webpack_exports, __nccwpck_require__) => { /***/ ((module, __unused_webpack_exports, __nccwpck_require__) => {
const jsonata = __nccwpck_require__(4245); const jsonata = __nccwpck_require__(4245);
const { WILDCARD } = __nccwpck_require__(4438);
const { normalizeOutputKey } = __nccwpck_require__(1608);
/** /**
* @typedef {Object} SecretRequest * @typedef {Object} SecretRequest
* @property {string} path * @property {string} path
@@ -18961,8 +18960,7 @@ const { normalizeOutputKey } = __nccwpck_require__(1608);
*/ */
async function getSecrets(secretRequests, client) { async function getSecrets(secretRequests, client) {
const responseCache = new Map(); const responseCache = new Map();
let results = []; const results = [];
for (const secretRequest of secretRequests) { for (const secretRequest of secretRequests) {
let { path, selector } = secretRequest; let { path, selector } = secretRequest;
@@ -18985,65 +18983,33 @@ async function getSecrets(secretRequests, client) {
throw error throw error
} }
} }
if (!selector.match(/.*[\.].*/)) {
body = JSON.parse(body); selector = '"' + selector + '"'
}
if (selector == WILDCARD) { selector = "data." + selector
let keys = body.data; body = JSON.parse(body)
if (body.data["data"] != undefined) { if (body.data["data"] != undefined) {
keys = keys.data; selector = "data." + selector
}
for (let key in keys) {
let newRequest = Object.assign({},secretRequest);
newRequest.selector = key;
if (secretRequest.selector === secretRequest.outputVarName) {
newRequest.outputVarName = key;
newRequest.envVarName = key;
}
else {
newRequest.outputVarName = secretRequest.outputVarName+key;
newRequest.envVarName = secretRequest.envVarName+key;
}
newRequest.outputVarName = normalizeOutputKey(newRequest.outputVarName);
newRequest.envVarName = normalizeOutputKey(newRequest.envVarName,true);
selector = key;
results = await selectAndAppendResults(
selector,
body,
cachedResponse,
newRequest,
results
);
}
} }
else {
results = await selectAndAppendResults(
selector,
body,
cachedResponse,
secretRequest,
results
);
}
}
const value = await selectData(body, selector);
results.push({
request: secretRequest,
value,
cachedResponse
});
}
return results; return results;
} }
/** /**
* Uses a Jsonata selector retrieve a bit of data from the result * Uses a Jsonata selector retrieve a bit of data from the result
* @param {object} data * @param {object} data
* @param {string} selector * @param {string} selector
*/ */
async function selectData(data, selector) { async function selectData(data, selector) {
const ata = jsonata(selector); const ata = jsonata(selector);
let result = JSON.stringify(await ata.evaluate(data)); let result = JSON.stringify(await ata.evaluate(data));
// Compat for custom engines // Compat for custom engines
if (!result && ((ata.ast().type === "path" && ata.ast()['steps'].length === 1) || ata.ast().type === "string") && selector !== 'data' && 'data' in data) { if (!result && ((ata.ast().type === "path" && ata.ast()['steps'].length === 1) || ata.ast().type === "string") && selector !== 'data' && 'data' in data) {
result = JSON.stringify(await jsonata(`data.${selector}`).evaluate(data)); result = JSON.stringify(await jsonata(`data.${selector}`).evaluate(data));
@@ -19057,75 +19023,11 @@ async function selectData(data, selector) {
return result; return result;
} }
/**
* Uses selectData with the selector to get the value and then appends it to the
* results. Returns a new array with all of the results.
* @param {string} selector
* @param {object} body
* @param {object} cachedResponse
* @param {TRequest} secretRequest
* @param {SecretResponse<TRequest>[]} results
* @return {Promise<SecretResponse<TRequest>[]>}
*/
const selectAndAppendResults = async (
selector,
body,
cachedResponse,
secretRequest,
results
) => {
if (!selector.match(/.*[\.].*/)) {
selector = '"' + selector + '"';
}
selector = "data." + selector;
if (body.data["data"] != undefined) {
selector = "data." + selector;
}
const value = await selectData(body, selector);
return [
...results,
{
request: secretRequest,
value,
cachedResponse,
},
];
};
module.exports = { module.exports = {
getSecrets, getSecrets,
selectData selectData
} }
/***/ }),
/***/ 1608:
/***/ ((module) => {
/**
* Replaces any dot chars to __ and removes non-ascii charts
* @param {string} dataKey
* @param {boolean=} isEnvVar
*/
function normalizeOutputKey(dataKey, isEnvVar = false) {
let outputKey = dataKey
.replace(".", "__")
.replace(new RegExp("-", "g"), "")
.replace(/[^\p{L}\p{N}_-]/gu, "");
if (isEnvVar) {
outputKey = outputKey.toUpperCase();
}
return outputKey;
}
module.exports = {
normalizeOutputKey
};
/***/ }), /***/ }),
/***/ 9491: /***/ 9491:
@@ -171,26 +171,6 @@ describe('integration', () => {
expect(core.exportVariable).toBeCalledWith('OTHERSECRETDASH', 'OTHERSUPERSECRET'); expect(core.exportVariable).toBeCalledWith('OTHERSECRETDASH', 'OTHERSUPERSECRET');
}); });
it('get wildcard secrets', async () => {
mockInput(`secret/data/test * ;`);
await exportSecrets();
expect(core.exportVariable).toBeCalledTimes(1);
expect(core.exportVariable).toBeCalledWith('SECRET', 'SUPERSECRET');
});
it('get wildcard secrets with name prefix', async () => {
mockInput(`secret/data/test * | GROUP_ ;`);
await exportSecrets();
expect(core.exportVariable).toBeCalledTimes(1);
expect(core.exportVariable).toBeCalledWith('GROUP_SECRET', 'SUPERSECRET');
});
it('leading slash kvv2', async () => { it('leading slash kvv2', async () => {
mockInput('/secret/data/foobar fookv2'); mockInput('/secret/data/foobar fookv2');
@@ -215,34 +195,6 @@ describe('integration', () => {
expect(core.exportVariable).toBeCalledWith('OTHERSECRETDASH', 'OTHERCUSTOMSECRET'); expect(core.exportVariable).toBeCalledWith('OTHERSECRETDASH', 'OTHERCUSTOMSECRET');
}); });
it('get K/V v1 wildcard secrets', async () => {
mockInput(`secret-kv1/test * ;`);
await exportSecrets();
expect(core.exportVariable).toBeCalledTimes(1);
expect(core.exportVariable).toBeCalledWith('SECRET', 'CUSTOMSECRET');
});
it('get K/V v1 wildcard secrets with name prefix', async () => {
mockInput(`secret-kv1/test * | GROUP_ ;`);
await exportSecrets();
expect(core.exportVariable).toBeCalledTimes(1);
expect(core.exportVariable).toBeCalledWith('GROUP_SECRET', 'CUSTOMSECRET');
});
it('get wildcard nested secret from K/V v1', async () => {
mockInput('secret-kv1/nested/test *');
await exportSecrets();
expect(core.exportVariable).toBeCalledWith('OTHERSECRETDASH', 'OTHERCUSTOMSECRET');
});
it('leading slash kvv1', async () => { it('leading slash kvv1', async () => {
mockInput('/secret-kv1/foobar fookv1'); mockInput('/secret-kv1/foobar fookv1');
@@ -273,17 +225,6 @@ describe('integration', () => {
expect(core.exportVariable).toBeCalledWith('FOO', 'bar'); expect(core.exportVariable).toBeCalledWith('FOO', 'bar');
}); });
it('wildcard supports cubbyhole', async () => {
mockInput('/cubbyhole/test *');
await exportSecrets();
expect(core.exportVariable).toBeCalledTimes(2);
expect(core.exportVariable).toBeCalledWith('FOO', 'bar');
expect(core.exportVariable).toBeCalledWith('ZIP', 'zap');
});
it('caches responses', async () => { it('caches responses', async () => {
mockInput(` mockInput(`
/cubbyhole/test foo ; /cubbyhole/test foo ;
+4 -1
View File
@@ -12,6 +12,9 @@ describe('e2e', () => {
expect(process.env.SUBSEQUENT_TEST_SECRET).toBe("SUBSEQUENT_TEST_SECRET"); expect(process.env.SUBSEQUENT_TEST_SECRET).toBe("SUBSEQUENT_TEST_SECRET");
expect(process.env.JSONSTRING).toBe('{"x":1,"y":"qux"}'); expect(process.env.JSONSTRING).toBe('{"x":1,"y":"qux"}');
expect(process.env.JSONSTRINGMULTILINE).toBe('{"x": 1, "y": "q\\nux"}'); expect(process.env.JSONSTRINGMULTILINE).toBe('{"x": 1, "y": "q\\nux"}');
expect(process.env.JSONDATA).toBe('{"x":1,"y":"qux"}');
let result = JSON.stringify('{"x":1,"y":"qux"}');
result = result.substring(1, result.length - 1);
expect(process.env.JSONDATA).toBe(result);
}); });
}); });
@@ -72,22 +72,6 @@ describe('integration', () => {
expect(core.exportVariable).toBeCalledWith('TEST_KEY', 'SUPERSECRET_IN_NAMESPACE'); expect(core.exportVariable).toBeCalledWith('TEST_KEY', 'SUPERSECRET_IN_NAMESPACE');
}); });
it('get wildcard secrets', async () => {
mockInput('secret/data/test *');
await exportSecrets();
expect(core.exportVariable).toBeCalledWith('SECRET', 'SUPERSECRET_IN_NAMESPACE');
});
it('get wildcard secrets with name prefix', async () => {
mockInput('secret/data/test * | GROUP_');
await exportSecrets();
expect(core.exportVariable).toBeCalledWith('GROUP_SECRET', 'SUPERSECRET_IN_NAMESPACE');
});
it('get nested secret', async () => { it('get nested secret', async () => {
mockInput('secret/data/nested/test otherSecret'); mockInput('secret/data/nested/test otherSecret');
@@ -119,22 +103,6 @@ describe('integration', () => {
expect(core.exportVariable).toBeCalledWith('SECRET', 'CUSTOMSECRET_IN_NAMESPACE'); expect(core.exportVariable).toBeCalledWith('SECRET', 'CUSTOMSECRET_IN_NAMESPACE');
}); });
it('get wildcard secrets from K/V v1', async () => {
mockInput('my-secret/test *');
await exportSecrets();
expect(core.exportVariable).toBeCalledWith('SECRET', 'CUSTOMSECRET_IN_NAMESPACE');
});
it('get wildcard secrets from K/V v1 with name prefix', async () => {
mockInput('my-secret/test * | GROUP_');
await exportSecrets();
expect(core.exportVariable).toBeCalledWith('GROUP_SECRET', 'CUSTOMSECRET_IN_NAMESPACE');
});
it('get nested secret from K/V v1', async () => { it('get nested secret from K/V v1', async () => {
mockInput('my-secret/nested/test otherSecret'); mockInput('my-secret/nested/test otherSecret');
+7 -7
View File
@@ -9,7 +9,7 @@
"version": "0.1.0", "version": "0.1.0",
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"got": "^11.8.6", "got": "^11.8.5",
"jsonata": "^2.0.3", "jsonata": "^2.0.3",
"jsrsasign": "^10.8.6" "jsrsasign": "^10.8.6"
}, },
@@ -2150,9 +2150,9 @@
} }
}, },
"node_modules/got": { "node_modules/got": {
"version": "11.8.6", "version": "11.8.5",
"resolved": "https://registry.npmjs.org/got/-/got-11.8.6.tgz", "resolved": "https://registry.npmjs.org/got/-/got-11.8.5.tgz",
"integrity": "sha512-6tfZ91bOr7bOXnK7PRDCGBLa1H4U080YHNaAQ2KsMGlLEzRbk44nsZF2E1IeRc3vtJHPVbKCYgdFbaGO2ljd8g==", "integrity": "sha512-o0Je4NvQObAuZPHLFoRSkdG2lTgtcynqymzg2Vupdx6PorhaT5MCbIyXG6d4D94kk8ZG57QeosgdiqfJWhEhlQ==",
"dependencies": { "dependencies": {
"@sindresorhus/is": "^4.0.0", "@sindresorhus/is": "^4.0.0",
"@szmarczak/http-timer": "^4.0.5", "@szmarczak/http-timer": "^4.0.5",
@@ -5975,9 +5975,9 @@
"dev": true "dev": true
}, },
"got": { "got": {
"version": "11.8.6", "version": "11.8.5",
"resolved": "https://registry.npmjs.org/got/-/got-11.8.6.tgz", "resolved": "https://registry.npmjs.org/got/-/got-11.8.5.tgz",
"integrity": "sha512-6tfZ91bOr7bOXnK7PRDCGBLa1H4U080YHNaAQ2KsMGlLEzRbk44nsZF2E1IeRc3vtJHPVbKCYgdFbaGO2ljd8g==", "integrity": "sha512-o0Je4NvQObAuZPHLFoRSkdG2lTgtcynqymzg2Vupdx6PorhaT5MCbIyXG6d4D94kk8ZG57QeosgdiqfJWhEhlQ==",
"requires": { "requires": {
"@sindresorhus/is": "^4.0.0", "@sindresorhus/is": "^4.0.0",
"@szmarczak/http-timer": "^4.0.5", "@szmarczak/http-timer": "^4.0.5",
+1 -1
View File
@@ -34,7 +34,7 @@
}, },
"homepage": "https://github.com/hashicorp/vault-action#readme", "homepage": "https://github.com/hashicorp/vault-action#readme",
"dependencies": { "dependencies": {
"got": "^11.8.6", "got": "^11.8.5",
"jsonata": "^2.0.3", "jsonata": "^2.0.3",
"jsrsasign": "^10.8.6" "jsrsasign": "^10.8.6"
}, },
+17 -6
View File
@@ -3,9 +3,6 @@ const core = require('@actions/core');
const command = require('@actions/core/lib/command'); const command = require('@actions/core/lib/command');
const got = require('got').default; const got = require('got').default;
const jsonata = require('jsonata'); const jsonata = require('jsonata');
const { normalizeOutputKey } = require('./utils');
const { WILDCARD } = require('./constants');
const { auth: { retrieveToken }, secrets: { getSecrets } } = require('./index'); const { auth: { retrieveToken }, secrets: { getSecrets } } = require('./index');
const AUTH_METHODS = ['approle', 'token', 'github', 'jwt', 'kubernetes', 'ldap', 'userpass']; const AUTH_METHODS = ['approle', 'token', 'github', 'jwt', 'kubernetes', 'ldap', 'userpass'];
@@ -174,7 +171,7 @@ function parseSecretsInput(secretsInput) {
const selectorAst = jsonata(selectorQuoted).ast(); const selectorAst = jsonata(selectorQuoted).ast();
const selector = selectorQuoted.replace(new RegExp('"', 'g'), ''); const selector = selectorQuoted.replace(new RegExp('"', 'g'), '');
if (selector !== WILDCARD && (selectorAst.type !== "path" || selectorAst.steps[0].stages) && selectorAst.type !== "string" && !outputVarName) { if ((selectorAst.type !== "path" || selectorAst.steps[0].stages) && selectorAst.type !== "string" && !outputVarName) {
throw Error(`You must provide a name for the output key when using json selectors. Input: "${secret}"`); throw Error(`You must provide a name for the output key when using json selectors. Input: "${secret}"`);
} }
@@ -194,6 +191,20 @@ function parseSecretsInput(secretsInput) {
return output; return output;
} }
/**
* Replaces any dot chars to __ and removes non-ascii charts
* @param {string} dataKey
* @param {boolean=} isEnvVar
*/
function normalizeOutputKey(dataKey, isEnvVar = false) {
let outputKey = dataKey
.replace('.', '__').replace(new RegExp('-', 'g'), '').replace(/[^\p{L}\p{N}_-]/gu, '');
if (isEnvVar) {
outputKey = outputKey.toUpperCase();
}
return outputKey;
}
/** /**
* @param {string} inputKey * @param {string} inputKey
* @param {any} inputOptions * @param {any} inputOptions
@@ -222,6 +233,6 @@ function parseHeadersInput(inputKey, inputOptions) {
module.exports = { module.exports = {
exportSecrets, exportSecrets,
parseSecretsInput, parseSecretsInput,
parseHeadersInput, normalizeOutputKey,
parseHeadersInput
}; };
+4 -1
View File
@@ -223,7 +223,10 @@ describe('exportSecrets', () => {
it('JSON data secret retrieval', async () => { it('JSON data secret retrieval', async () => {
const jsonData = {"x":1,"y":2}; const jsonData = {"x":1,"y":2};
let result = JSON.stringify(jsonData); // for secrets stored in Vault as pure JSON, we call stringify twice
// and remove the added surrounding quotes
let result = JSON.stringify(JSON.stringify(jsonData));
result = result.substring(1, result.length - 1);
mockInput('test key'); mockInput('test key');
mockVaultData({ mockVaultData({
-5
View File
@@ -1,5 +0,0 @@
const WILDCARD = '*';
module.exports = {
WILDCARD
};
+60 -82
View File
@@ -1,6 +1,6 @@
const jsonata = require("jsonata"); const jsonata = require("jsonata");
const { WILDCARD } = require("./constants");
const { normalizeOutputKey } = require("./utils");
/** /**
* @typedef {Object} SecretRequest * @typedef {Object} SecretRequest
* @property {string} path * @property {string} path
@@ -23,8 +23,7 @@ const { normalizeOutputKey } = require("./utils");
*/ */
async function getSecrets(secretRequests, client) { async function getSecrets(secretRequests, client) {
const responseCache = new Map(); const responseCache = new Map();
let results = []; const results = [];
for (const secretRequest of secretRequests) { for (const secretRequest of secretRequests) {
let { path, selector } = secretRequest; let { path, selector } = secretRequest;
@@ -47,53 +46,22 @@ async function getSecrets(secretRequests, client) {
throw error throw error
} }
} }
if (!selector.match(/.*[\.].*/)) {
body = JSON.parse(body); selector = '"' + selector + '"'
}
if (selector == WILDCARD) { selector = "data." + selector
let keys = body.data; body = JSON.parse(body)
if (body.data["data"] != undefined) { if (body.data["data"] != undefined) {
keys = keys.data; selector = "data." + selector
}
for (let key in keys) {
let newRequest = Object.assign({},secretRequest);
newRequest.selector = key;
if (secretRequest.selector === secretRequest.outputVarName) {
newRequest.outputVarName = key;
newRequest.envVarName = key;
}
else {
newRequest.outputVarName = secretRequest.outputVarName+key;
newRequest.envVarName = secretRequest.envVarName+key;
}
newRequest.outputVarName = normalizeOutputKey(newRequest.outputVarName);
newRequest.envVarName = normalizeOutputKey(newRequest.envVarName,true);
selector = key;
results = await selectAndAppendResults(
selector,
body,
cachedResponse,
newRequest,
results
);
}
} }
else {
results = await selectAndAppendResults(
selector,
body,
cachedResponse,
secretRequest,
results
);
}
}
const value = await selectData(body, selector);
results.push({
request: secretRequest,
value,
cachedResponse
});
}
return results; return results;
} }
@@ -104,7 +72,13 @@ async function getSecrets(secretRequests, client) {
*/ */
async function selectData(data, selector) { async function selectData(data, selector) {
const ata = jsonata(selector); const ata = jsonata(selector);
let result = JSON.stringify(await ata.evaluate(data)); let d = await ata.evaluate(data);
console.log(selector);
// If we have a Javascript Object, then this data was stored in Vault as
// pure JSON (not a JSON string). We will capture that before we stringify it.
const storedAsJSONData = isObject(d);
result = JSON.stringify(d);
// Compat for custom engines // Compat for custom engines
if (!result && ((ata.ast().type === "path" && ata.ast()['steps'].length === 1) || ata.ast().type === "string") && selector !== 'data' && 'data' in data) { if (!result && ((ata.ast().type === "path" && ata.ast()['steps'].length === 1) || ata.ast().type === "string") && selector !== 'data' && 'data' in data) {
@@ -114,47 +88,51 @@ async function selectData(data, selector) {
} }
if (result.startsWith(`"`)) { if (result.startsWith(`"`)) {
// we need to strip the beginning and ending quotes otherwise it will
// always successfully parse as a JSON string
// result = result.substring(1, result.length - 1);
// if (!isJSONString(result)) {
// // add the quotes back so we can parse it into a Javascript object
// // to allow support for multi-line secrets. See https://github.com/hashicorp/vault-action/issues/160
// result = `"${result}"`
console.log(" =>>> PARSING")
result = JSON.parse(result); result = JSON.parse(result);
// }
} else {
console.log('does not start with quote')
// Support secrets stored in Vault as pure JSON.
// See https://github.com/hashicorp/vault-action/issues/194 and https://github.com/hashicorp/vault-action/pull/173
result = JSON.stringify(result);
result = result.substring(1, result.length - 1);
} }
return result; return result;
} }
/** /**
* Uses selectData with the selector to get the value and then appends it to the * isOjbect returns true if target is a Javascript object
* results. Returns a new array with all of the results. * @param {Type} target
* @param {string} selector
* @param {object} body
* @param {object} cachedResponse
* @param {TRequest} secretRequest
* @param {SecretResponse<TRequest>[]} results
* @return {Promise<SecretResponse<TRequest>[]>}
*/ */
const selectAndAppendResults = async ( function isObject(target) {
selector, return typeof target === 'object' && target !== null;
body, }
cachedResponse,
secretRequest,
results
) => {
if (!selector.match(/.*[\.].*/)) {
selector = '"' + selector + '"';
}
selector = "data." + selector;
if (body.data["data"] != undefined) { /**
selector = "data." + selector; * isJSONString returns true if target parses as a valid JSON string
} * @param {Type} target
*/
function isJSONString(target) {
if (typeof target !== "string"){
return false;
}
const value = await selectData(body, selector); try {
return [ JSON.parse(target);
...results, } catch (e) {
{ return false;
request: secretRequest, }
value,
cachedResponse, return true;
}, }
];
};
module.exports = { module.exports = {
getSecrets, getSecrets,
-19
View File
@@ -1,19 +0,0 @@
/**
* Replaces any dot chars to __ and removes non-ascii charts
* @param {string} dataKey
* @param {boolean=} isEnvVar
*/
function normalizeOutputKey(dataKey, isEnvVar = false) {
let outputKey = dataKey
.replace(".", "__")
.replace(new RegExp("-", "g"), "")
.replace(/[^\p{L}\p{N}_-]/gu, "");
if (isEnvVar) {
outputKey = outputKey.toUpperCase();
}
return outputKey;
}
module.exports = {
normalizeOutputKey
};