mirror of
https://github.com/hashicorp/vault-action.git
synced 2026-07-29 18:03:14 +03:00
Compare commits
28 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 61dd38f491 | |||
| 168f9696e8 | |||
| c502100fbe | |||
| b8c90c7243 | |||
| d5a69ceca1 | |||
| f3e4110f8d | |||
| 8c00f7e4fd | |||
| ab957d2aec | |||
| 0451f06f9f | |||
| b6210c5a51 | |||
| 09740f679f | |||
| 72c7a899ca | |||
| 0cf3bd6a39 | |||
| 0723fd7348 | |||
| 0405c26bc9 | |||
| 2eea93c25e | |||
| 4d9cf786d6 | |||
| 575042000a | |||
| cc8ad16bec | |||
| cd1614aee3 | |||
| f3a53220d9 | |||
| 3526e1be65 | |||
| f60544fbda | |||
| 9e8f22534f | |||
| 8417c61f8a | |||
| 0cca23248c | |||
| 8dad53ed10 | |||
| 937d792a86 |
@@ -74,6 +74,8 @@ jobs:
|
|||||||
|
|
||||||
- name: Run docker-compose
|
- name: Run docker-compose
|
||||||
run: docker-compose up -d vault-enterprise
|
run: docker-compose up -d vault-enterprise
|
||||||
|
env:
|
||||||
|
VAULT_LICENSE_CI: ${{ secrets.VAULT_LICENSE_CI }}
|
||||||
|
|
||||||
- uses: actions/setup-node@v1
|
- uses: actions/setup-node@v1
|
||||||
with:
|
with:
|
||||||
@@ -93,7 +95,7 @@ jobs:
|
|||||||
- name: NPM Build
|
- name: NPM Build
|
||||||
run: npm run build
|
run: npm run build
|
||||||
|
|
||||||
- name: NPM Run test:intergration:enterprise
|
- name: NPM Run test:integration:enterprise
|
||||||
run: npm run test:integration:enterprise
|
run: npm run test:integration:enterprise
|
||||||
env:
|
env:
|
||||||
VAULT_HOST: localhost
|
VAULT_HOST: localhost
|
||||||
|
|||||||
@@ -1,5 +1,31 @@
|
|||||||
## Unreleased
|
## Unreleased
|
||||||
|
|
||||||
|
## 2.4.0 (October 21st, 2021)
|
||||||
|
|
||||||
|
Features:
|
||||||
|
* GitHub provided JWT auth is now supported [GH-257](https://github.com/hashicorp/vault-action/pull/257)
|
||||||
|
|
||||||
|
## 2.3.1 (August 23rd, 2021)
|
||||||
|
|
||||||
|
Improvements:
|
||||||
|
* bump normalize-url from 4.5.0 to 4.5.1 [GH-227](https://github.com/hashicorp/vault-action/pull/227)
|
||||||
|
* bump path-parse from 1.0.6 to 1.0.7 [GH-239](https://github.com/hashicorp/vault-action/pull/239)
|
||||||
|
|
||||||
|
## 2.3.0 (June 23rd, 2021)
|
||||||
|
|
||||||
|
Features:
|
||||||
|
* K8s auth method is now supported [GH-218](https://github.com/hashicorp/vault-action/pull/218)
|
||||||
|
* Custom auth method mount points is configurable [GH-218](https://github.com/hashicorp/vault-action/pull/218)
|
||||||
|
|
||||||
|
## 2.2.0 (May 6th, 2021)
|
||||||
|
|
||||||
|
Security:
|
||||||
|
* multi-line secrets are now properly masked in logs [GH-208](https://github.com/hashicorp/vault-action/pull/208)
|
||||||
|
[CVE-2021-32074](https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2021-32074)
|
||||||
|
|
||||||
|
Features:
|
||||||
|
* JWT auth method is now supported [GH-188](https://github.com/hashicorp/vault-action/pull/188)
|
||||||
|
|
||||||
## 2.1.2 (January 21st, 2021)
|
## 2.1.2 (January 21st, 2021)
|
||||||
|
|
||||||
Bugs:
|
Bugs:
|
||||||
|
|||||||
@@ -10,13 +10,13 @@ A helper action for easily pulling secrets from HashiCorp Vault™.
|
|||||||
|
|
||||||
<!-- TOC -->
|
<!-- TOC -->
|
||||||
|
|
||||||
|
- [Vault GitHub Action](#vault-github-action)
|
||||||
- [Example Usage](#example-usage)
|
- [Example Usage](#example-usage)
|
||||||
- [Authentication method](#authentication-method)
|
- [Authentication method](#authentication-method)
|
||||||
- [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)
|
||||||
- [Multiple Secrets](#multiple-secrets)
|
- [Multiple Secrets](#multiple-secrets)
|
||||||
- [Nested Secrets](#nested-secrets)
|
|
||||||
- [Other Secret Engines](#other-secret-engines)
|
- [Other Secret Engines](#other-secret-engines)
|
||||||
- [Adding Extra Headers](#adding-extra-headers)
|
- [Adding Extra Headers](#adding-extra-headers)
|
||||||
- [Vault Enterprise Features](#vault-enterprise-features)
|
- [Vault Enterprise Features](#vault-enterprise-features)
|
||||||
@@ -36,7 +36,7 @@ jobs:
|
|||||||
steps:
|
steps:
|
||||||
# ...
|
# ...
|
||||||
- name: Import Secrets
|
- name: Import Secrets
|
||||||
uses: hashicorp/vault-action@v2.1.2
|
uses: hashicorp/vault-action@v2.3.1
|
||||||
with:
|
with:
|
||||||
url: https://vault.mycompany.com:8200
|
url: https://vault.mycompany.com:8200
|
||||||
token: ${{ secrets.VaultToken }}
|
token: ${{ secrets.VaultToken }}
|
||||||
@@ -71,14 +71,62 @@ with:
|
|||||||
caCertificate: ${{ secrets.VAULTCA }}
|
caCertificate: ${{ secrets.VAULTCA }}
|
||||||
```
|
```
|
||||||
- **github**: you must provide the github token as `githubToken`
|
- **github**: you must provide the github token as `githubToken`
|
||||||
|
|
||||||
|
**Notice: [Vault GitHub authentication](https://www.vaultproject.io/docs/auth/github)
|
||||||
|
requires `read:org` permissions for authentication. The auto-generated `GITHUB_TOKEN`
|
||||||
|
created for projects does not have these permissions and GitHub does not allow this
|
||||||
|
token's permissions to be modified. A new GitHub Token secret must be created with
|
||||||
|
`read:org` permissions to use this authentication method.**
|
||||||
|
|
||||||
```yaml
|
```yaml
|
||||||
...
|
...
|
||||||
with:
|
with:
|
||||||
url: https://vault.mycompany.com:8200
|
url: https://vault.mycompany.com:8200
|
||||||
method: github
|
method: github
|
||||||
githubToken: ${{ secrets.GITHUB_TOKEN }}
|
githubToken: ${{ secrets.MY_GITHUB_TOKEN }}
|
||||||
caCertificate: ${{ secrets.VAULTCA }}
|
caCertificate: ${{ secrets.VAULTCA }}
|
||||||
```
|
```
|
||||||
|
- **jwt**: (Github OIDC) you must provide a `role` parameter, additionally you can pass `jwtGithubAudience` parameter.
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
...
|
||||||
|
with:
|
||||||
|
url: https://vault.mycompany.com:8200
|
||||||
|
method: jwt
|
||||||
|
role: github-action
|
||||||
|
```
|
||||||
|
|
||||||
|
**Notice:** For Github provided OIDC token to work, the workflow should have `id-token: write` & `contents: read` specified in the `permissions` section of the workflow
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
...
|
||||||
|
permissions:
|
||||||
|
id-token: write
|
||||||
|
contents: read
|
||||||
|
...
|
||||||
|
```
|
||||||
|
|
||||||
|
- **jwt**: you must provide a `role` & `jwtPrivateKey` parameters, additionally you can pass `jwtKeyPassword` & `jwtTtl` parameters
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
...
|
||||||
|
with:
|
||||||
|
url: https://vault.mycompany.com:8200
|
||||||
|
method: jwt
|
||||||
|
role: github-action
|
||||||
|
jwtPrivateKey: ${{ secrets.JWT_PRIVATE_KEY }}
|
||||||
|
jwtKeyPassword: ${{ secrets.JWT_KEY_PASS }}
|
||||||
|
jwtTtl: 3600 # 1 hour, default value
|
||||||
|
```
|
||||||
|
|
||||||
|
- **kubernetes**: you must provide the `role` paramaters. You can optionally override the `kubernetesTokenPath` paramater for custom mounted serviceAccounts. Consider [kubernetes auth](https://www.vaultproject.io/docs/auth/kubernetes) when using self-hosted runners on Kubernetes:
|
||||||
|
```yaml
|
||||||
|
...
|
||||||
|
with:
|
||||||
|
url: https://vault.mycompany.com:8200
|
||||||
|
method: kubernetes
|
||||||
|
role: ${{ secrets.KUBE_ROLE }}
|
||||||
|
```
|
||||||
|
|
||||||
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.
|
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.
|
||||||
|
|
||||||
@@ -243,10 +291,17 @@ Here are all the inputs available through `with`:
|
|||||||
| `secrets` | A semicolon-separated list of secrets to retrieve. These will automatically be converted to environmental variable keys. See README for more details | | ✔ |
|
| `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 | | |
|
| `namespace` | The Vault namespace from which to query secrets. Vault Enterprise only, unset by default | | |
|
||||||
| `method` | The method to use to authenticate with Vault. | `token` | |
|
| `method` | The method to use to authenticate with Vault. | `token` | |
|
||||||
|
| `role` | Vault role for specified auth method | | |
|
||||||
|
| `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 | | |
|
| `token` | The Vault Token to be used to authenticate with Vault | | |
|
||||||
| `roleId` | The Role Id for App Role authentication | | |
|
| `roleId` | The Role Id for App Role authentication | | |
|
||||||
| `secretId` | The Secret Id for App Role authentication | | |
|
| `secretId` | The Secret Id for App Role authentication | | |
|
||||||
| `githubToken` | The Github Token to be used to authenticate with Vault | | |
|
| `githubToken` | The Github Token to be used to authenticate with Vault | | |
|
||||||
|
| `jwtPrivateKey` | Base64 encoded Private key to sign JWT | | |
|
||||||
|
| `jwtKeyPassword` | Password for key stored in jwtPrivateKey (if needed) | | |
|
||||||
|
| `jwtGithubAudience` | Identifies the recipient ("aud" claim) that the JWT is intended for |`sigstore`| |
|
||||||
|
| `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` | |
|
||||||
| `authPayload` | The JSON payload to be sent to Vault when using a custom authentication method. | | |
|
| `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. | | |
|
| `extraHeaders` | A string of newline separated extra headers to include on every request. | | |
|
||||||
| `exportEnv` | Whether or not export secrets as environment variables. | `true` | |
|
| `exportEnv` | Whether or not export secrets as environment variables. | `true` | |
|
||||||
|
|||||||
+24
-1
@@ -14,6 +14,12 @@ inputs:
|
|||||||
description: 'The method to use to authenticate with Vault.'
|
description: 'The method to use to authenticate with Vault.'
|
||||||
default: 'token'
|
default: 'token'
|
||||||
required: false
|
required: false
|
||||||
|
role:
|
||||||
|
description: 'Vault role for specified auth method'
|
||||||
|
required: false
|
||||||
|
path:
|
||||||
|
description: 'Custom Vault path, if the auth method was mounted at a different path'
|
||||||
|
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
|
||||||
@@ -26,6 +32,10 @@ inputs:
|
|||||||
githubToken:
|
githubToken:
|
||||||
description: 'The Github Token to be used to authenticate with Vault'
|
description: 'The Github Token to be used to authenticate with Vault'
|
||||||
required: false
|
required: false
|
||||||
|
kubernetesTokenPath:
|
||||||
|
description: 'The path to the Kubernetes service account secret'
|
||||||
|
required: false
|
||||||
|
default: '/var/run/secrets/kubernetes.io/serviceaccount/token'
|
||||||
authPayload:
|
authPayload:
|
||||||
description: 'The JSON payload to be sent to Vault when using a custom authentication method.'
|
description: 'The JSON payload to be sent to Vault when using a custom authentication method.'
|
||||||
required: false
|
required: false
|
||||||
@@ -52,7 +62,20 @@ inputs:
|
|||||||
tlsSkipVerify:
|
tlsSkipVerify:
|
||||||
description: 'When set to true, disables verification of the Vault server certificate. Setting this to true in production is not recommended.'
|
description: 'When set to true, disables verification of the Vault server certificate. Setting this to true in production is not recommended.'
|
||||||
required: false
|
required: false
|
||||||
default: "false"
|
default: 'false'
|
||||||
|
jwtPrivateKey:
|
||||||
|
description: 'Base64 encoded Private key to sign JWT'
|
||||||
|
required: false
|
||||||
|
jwtKeyPassword:
|
||||||
|
description: 'Password for key stored in jwtPrivateKey (if needed)'
|
||||||
|
required: false
|
||||||
|
jwtGithubAudience:
|
||||||
|
description: 'Identifies the recipient ("aud" claim) that the JWT is intended for'
|
||||||
|
required: false
|
||||||
|
jwtTtl:
|
||||||
|
description: 'Time in seconds, after which token expires'
|
||||||
|
required: false
|
||||||
|
default: 3600
|
||||||
runs:
|
runs:
|
||||||
using: 'node12'
|
using: 'node12'
|
||||||
main: 'dist/index.js'
|
main: 'dist/index.js'
|
||||||
|
|||||||
Vendored
+1666
-41
File diff suppressed because one or more lines are too long
@@ -12,6 +12,7 @@ services:
|
|||||||
image: hashicorp/vault-enterprise:latest
|
image: hashicorp/vault-enterprise:latest
|
||||||
environment:
|
environment:
|
||||||
VAULT_DEV_ROOT_TOKEN_ID: testtoken
|
VAULT_DEV_ROOT_TOKEN_ID: testtoken
|
||||||
|
VAULT_LICENSE: ${VAULT_LICENSE_CI}
|
||||||
ports:
|
ports:
|
||||||
- 8200:8200
|
- 8200:8200
|
||||||
privileged: true
|
privileged: true
|
||||||
|
|||||||
@@ -0,0 +1,238 @@
|
|||||||
|
jest.mock('@actions/core');
|
||||||
|
jest.mock('@actions/core/lib/command');
|
||||||
|
const core = require('@actions/core');
|
||||||
|
const rsasign = require('jsrsasign');
|
||||||
|
const {
|
||||||
|
privateRsaKey,
|
||||||
|
privateRsaKeyBase64,
|
||||||
|
publicRsaKey
|
||||||
|
} = require('./rsa_keys');
|
||||||
|
|
||||||
|
const got = require('got');
|
||||||
|
const { when } = require('jest-when');
|
||||||
|
|
||||||
|
const { exportSecrets } = require('../../src/action');
|
||||||
|
|
||||||
|
const vaultUrl = `http://${process.env.VAULT_HOST || 'localhost'}:${process.env.VAULT_PORT || '8200'}`;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns Github OIDC response mock
|
||||||
|
* @param {string} aud Audience claim
|
||||||
|
* @returns {string}
|
||||||
|
*/
|
||||||
|
function mockGithubOIDCResponse(aud= "https://github.com/hashicorp/vault-action") {
|
||||||
|
const alg = 'RS256';
|
||||||
|
const header = { alg: alg, typ: 'JWT' };
|
||||||
|
const now = rsasign.KJUR.jws.IntDate.getNow();
|
||||||
|
const payload = {
|
||||||
|
jti: "unique-id",
|
||||||
|
sub: "repo:hashicorp/vault-action:ref:refs/heads/master",
|
||||||
|
aud,
|
||||||
|
ref: "refs/heads/master",
|
||||||
|
sha: "commit-sha",
|
||||||
|
repository: "hashicorp/vault-action",
|
||||||
|
repository_owner: "hashicorp",
|
||||||
|
run_id: "1",
|
||||||
|
run_number: "1",
|
||||||
|
run_attempt: "1",
|
||||||
|
actor: "github-username",
|
||||||
|
workflow: "Workflow Name",
|
||||||
|
head_ref: "",
|
||||||
|
base_ref: "",
|
||||||
|
event_name: "push",
|
||||||
|
ref_type: "branch",
|
||||||
|
job_workflow_ref: "hashicorp/vault-action/.github/workflows/workflow.yml@refs/heads/master",
|
||||||
|
iss: 'vault-action',
|
||||||
|
iat: now,
|
||||||
|
nbf: now,
|
||||||
|
exp: now + 3600,
|
||||||
|
};
|
||||||
|
const decryptedKey = rsasign.KEYUTIL.getKey(privateRsaKey);
|
||||||
|
return rsasign.KJUR.jws.JWS.sign(alg, JSON.stringify(header), JSON.stringify(payload), decryptedKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
describe('jwt auth', () => {
|
||||||
|
beforeAll(async () => {
|
||||||
|
// Verify Connection
|
||||||
|
await got(`${vaultUrl}/v1/secret/config`, {
|
||||||
|
headers: {
|
||||||
|
'X-Vault-Token': 'testtoken',
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
try {
|
||||||
|
await got(`${vaultUrl}/v1/sys/auth/jwt`, {
|
||||||
|
method: 'POST',
|
||||||
|
headers: {
|
||||||
|
'X-Vault-Token': 'testtoken',
|
||||||
|
},
|
||||||
|
json: {
|
||||||
|
type: 'jwt'
|
||||||
|
}
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
const {response} = error;
|
||||||
|
if (response.statusCode === 400 && response.body.includes("path is already in use")) {
|
||||||
|
// Auth method might already be enabled from previous test runs
|
||||||
|
} else {
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
await got(`${vaultUrl}/v1/sys/policy/reader`, {
|
||||||
|
method: 'PUT',
|
||||||
|
headers: {
|
||||||
|
'X-Vault-Token': 'testtoken',
|
||||||
|
},
|
||||||
|
json: {
|
||||||
|
policy: `
|
||||||
|
path "*" {
|
||||||
|
capabilities = ["read"]
|
||||||
|
}
|
||||||
|
`
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
await got(`${vaultUrl}/v1/auth/jwt/config`, {
|
||||||
|
method: 'POST',
|
||||||
|
headers: {
|
||||||
|
'X-Vault-Token': 'testtoken',
|
||||||
|
},
|
||||||
|
json: {
|
||||||
|
jwt_validation_pubkeys: publicRsaKey
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
await got(`${vaultUrl}/v1/auth/jwt/role/default`, {
|
||||||
|
method: 'POST',
|
||||||
|
headers: {
|
||||||
|
'X-Vault-Token': 'testtoken',
|
||||||
|
},
|
||||||
|
json: {
|
||||||
|
role_type: 'jwt',
|
||||||
|
bound_audiences: null,
|
||||||
|
bound_claims: {
|
||||||
|
iss: 'vault-action'
|
||||||
|
},
|
||||||
|
user_claim: 'iss',
|
||||||
|
policies: ['reader']
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
await got(`${vaultUrl}/v1/secret/data/test`, {
|
||||||
|
method: 'POST',
|
||||||
|
headers: {
|
||||||
|
'X-Vault-Token': 'testtoken',
|
||||||
|
},
|
||||||
|
json: {
|
||||||
|
data: {
|
||||||
|
secret: 'SUPERSECRET',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('authenticate with private key', () => {
|
||||||
|
beforeEach(() => {
|
||||||
|
jest.resetAllMocks();
|
||||||
|
|
||||||
|
when(core.getInput)
|
||||||
|
.calledWith('url')
|
||||||
|
.mockReturnValueOnce(`${vaultUrl}`);
|
||||||
|
|
||||||
|
when(core.getInput)
|
||||||
|
.calledWith('method')
|
||||||
|
.mockReturnValueOnce('jwt');
|
||||||
|
|
||||||
|
when(core.getInput)
|
||||||
|
.calledWith('jwtPrivateKey')
|
||||||
|
.mockReturnValueOnce(privateRsaKeyBase64);
|
||||||
|
|
||||||
|
when(core.getInput)
|
||||||
|
.calledWith('role')
|
||||||
|
.mockReturnValueOnce('default');
|
||||||
|
|
||||||
|
when(core.getInput)
|
||||||
|
.calledWith('secrets')
|
||||||
|
.mockReturnValueOnce('secret/data/test secret');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('successfully authenticates', async () => {
|
||||||
|
await exportSecrets();
|
||||||
|
expect(core.exportVariable).toBeCalledWith('SECRET', 'SUPERSECRET');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('authenticate with Github OIDC', () => {
|
||||||
|
beforeAll(async () => {
|
||||||
|
await got(`${vaultUrl}/v1/auth/jwt/role/default-sigstore`, {
|
||||||
|
method: 'POST',
|
||||||
|
headers: {
|
||||||
|
'X-Vault-Token': 'testtoken',
|
||||||
|
},
|
||||||
|
json: {
|
||||||
|
role_type: 'jwt',
|
||||||
|
bound_audiences: null,
|
||||||
|
bound_claims: {
|
||||||
|
iss: 'vault-action',
|
||||||
|
aud: 'sigstore',
|
||||||
|
},
|
||||||
|
user_claim: 'iss',
|
||||||
|
policies: ['reader']
|
||||||
|
}
|
||||||
|
});
|
||||||
|
})
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
jest.resetAllMocks();
|
||||||
|
|
||||||
|
when(core.getInput)
|
||||||
|
.calledWith('url')
|
||||||
|
.mockReturnValueOnce(`${vaultUrl}`);
|
||||||
|
|
||||||
|
when(core.getInput)
|
||||||
|
.calledWith('method')
|
||||||
|
.mockReturnValueOnce('jwt');
|
||||||
|
|
||||||
|
when(core.getInput)
|
||||||
|
.calledWith('jwtPrivateKey')
|
||||||
|
.mockReturnValueOnce('');
|
||||||
|
|
||||||
|
when(core.getInput)
|
||||||
|
.calledWith('role')
|
||||||
|
.mockReturnValueOnce('default');
|
||||||
|
|
||||||
|
when(core.getInput)
|
||||||
|
.calledWith('secrets')
|
||||||
|
.mockReturnValueOnce('secret/data/test secret');
|
||||||
|
|
||||||
|
when(core.getIDToken)
|
||||||
|
.calledWith()
|
||||||
|
.mockReturnValueOnce(mockGithubOIDCResponse());
|
||||||
|
});
|
||||||
|
|
||||||
|
it('successfully authenticates', async () => {
|
||||||
|
await exportSecrets();
|
||||||
|
expect(core.exportVariable).toBeCalledWith('SECRET', 'SUPERSECRET');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('successfully authenticates with `jwtGithubAudience` set to `sigstore`', async () => {
|
||||||
|
when(core.getInput)
|
||||||
|
.calledWith('role')
|
||||||
|
.mockReturnValueOnce('default-sigstore');
|
||||||
|
|
||||||
|
when(core.getInput)
|
||||||
|
.calledWith('jwtGithubAudience')
|
||||||
|
.mockReturnValueOnce('sigstore');
|
||||||
|
|
||||||
|
when(core.getIDToken)
|
||||||
|
.calledWith()
|
||||||
|
.mockReturnValueOnce(mockGithubOIDCResponse('sigstore'));
|
||||||
|
|
||||||
|
await exportSecrets();
|
||||||
|
expect(core.exportVariable).toBeCalledWith('SECRET', 'SUPERSECRET');
|
||||||
|
})
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
||||||
@@ -0,0 +1,78 @@
|
|||||||
|
const privateRsaKey = `
|
||||||
|
-----BEGIN RSA PRIVATE KEY-----
|
||||||
|
MIIJKAIBAAKCAgEAwavSHLLo7bUSuKX2EKu3YStrNTGdmhku7sAFaeDyi9His1oo
|
||||||
|
t+wzajWp1rqHaGVk4b5o5z6D7Xhm0zYPhpTTvEd3NyONc9sjVd7sf9rQaBY3QusP
|
||||||
|
YAdF6j0ydTJGnTeG9N2zhHjdMLR+3F39F9Ry6vddS9w3ibjVERucQtpqGhy5TIWh
|
||||||
|
ttk3gN3A7dk972+WCQeKUCC6wU6PvAEUPflThQc1hSHldpjVUHQlZkQXl/XHWBzZ
|
||||||
|
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hw34I5dpcqrYP4DErpKdYA3nsjx9rNQg0I4Zo7yVCRoiqRHa97GVQ=
|
||||||
|
-----END RSA PRIVATE KEY-----
|
||||||
|
`;
|
||||||
|
|
||||||
|
const privateRsaKeyBase64 = Buffer.from(privateRsaKey).toString('base64');
|
||||||
|
|
||||||
|
const publicRsaKey = `
|
||||||
|
-----BEGIN PUBLIC KEY-----
|
||||||
|
MIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEAwavSHLLo7bUSuKX2EKu3
|
||||||
|
YStrNTGdmhku7sAFaeDyi9His1oot+wzajWp1rqHaGVk4b5o5z6D7Xhm0zYPhpTT
|
||||||
|
vEd3NyONc9sjVd7sf9rQaBY3QusPYAdF6j0ydTJGnTeG9N2zhHjdMLR+3F39F9Ry
|
||||||
|
6vddS9w3ibjVERucQtpqGhy5TIWhttk3gN3A7dk972+WCQeKUCC6wU6PvAEUPflT
|
||||||
|
hQc1hSHldpjVUHQlZkQXl/XHWBzZESwMgiQcAmVL3lkvxmgIYscqWzf8cTHogNrP
|
||||||
|
ml9Il89N+2XYcEfXgWLOyzGhQggigN9DRHDDE1UWT1foHnmeXAIZPCveKZc/Jp5S
|
||||||
|
ASIxaZ+r73mWjVt19GSvEsqtejDRmMC3jrYCdISrCVsHkbRQq7/yLCi5sJG9p2gD
|
||||||
|
91jCgaBy2tw22nWN/KoHm9p/mhzWmlWwVJrFTNiP0qccyoa5h1a6WBPt0oCKQk3I
|
||||||
|
AMV5HhVw4DhPx8gvh7qwOZk9vXXAFyA8bWhOVIFIJ8i8Gq25y7/bukyzqsEPkQ2m
|
||||||
|
KgbQnh3VOUCSugJFodPC/Jf7VQK1yRpUZH2v7r9cBjHVnUnePGac3Zns7/iRYBvK
|
||||||
|
5cIFUg+X48VXIMKIvs4EA8ee8VacVh8tyR6EuP42BU5fGQlvLC+ZKT165maQv/Vl
|
||||||
|
f52E9W2iFNt3sxB0KFtOkbkCAwEAAQ==
|
||||||
|
-----END PUBLIC KEY-----
|
||||||
|
`;
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
privateRsaKey,
|
||||||
|
privateRsaKeyBase64,
|
||||||
|
publicRsaKey
|
||||||
|
};
|
||||||
Generated
+19677
-119
File diff suppressed because it is too large
Load Diff
+3
-2
@@ -45,13 +45,14 @@
|
|||||||
"homepage": "https://github.com/hashicorp/vault-action#readme",
|
"homepage": "https://github.com/hashicorp/vault-action#readme",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"got": "^11.5.1",
|
"got": "^11.5.1",
|
||||||
"jsonata": "^1.8.2"
|
"jsonata": "^1.8.2",
|
||||||
|
"jsrsasign": "^10.1.10"
|
||||||
},
|
},
|
||||||
"peerDependencies": {
|
"peerDependencies": {
|
||||||
"@actions/core": ">=1 <2"
|
"@actions/core": ">=1 <2"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@actions/core": "^1.2.3",
|
"@actions/core": "^1.6.0",
|
||||||
"@types/got": "^9.6.11",
|
"@types/got": "^9.6.11",
|
||||||
"@types/jest": "^26.0.13",
|
"@types/jest": "^26.0.13",
|
||||||
"@zeit/ncc": "^0.22.3",
|
"@zeit/ncc": "^0.22.3",
|
||||||
|
|||||||
+6
-2
@@ -5,7 +5,7 @@ const got = require('got').default;
|
|||||||
const jsonata = require('jsonata');
|
const jsonata = require('jsonata');
|
||||||
const { auth: { retrieveToken }, secrets: { getSecrets } } = require('./index');
|
const { auth: { retrieveToken }, secrets: { getSecrets } } = require('./index');
|
||||||
|
|
||||||
const AUTH_METHODS = ['approle', 'token', 'github'];
|
const AUTH_METHODS = ['approle', 'token', 'github', 'jwt', 'kubernetes'];
|
||||||
|
|
||||||
async function exportSecrets() {
|
async function exportSecrets() {
|
||||||
const vaultUrl = core.getInput('url', { required: true });
|
const vaultUrl = core.getInput('url', { required: true });
|
||||||
@@ -78,7 +78,11 @@ async function exportSecrets() {
|
|||||||
if (cachedResponse) {
|
if (cachedResponse) {
|
||||||
core.debug('ℹ using cached response');
|
core.debug('ℹ using cached response');
|
||||||
}
|
}
|
||||||
command.issue('add-mask', value);
|
for (const line of value.replace(/\r/g, '').split('\n')) {
|
||||||
|
if (line.length > 0) {
|
||||||
|
command.issue('add-mask', line);
|
||||||
|
}
|
||||||
|
}
|
||||||
if (exportEnv) {
|
if (exportEnv) {
|
||||||
core.exportVariable(request.envVarName, `${value}`);
|
core.exportVariable(request.envVarName, `${value}`);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ jest.mock('got');
|
|||||||
jest.mock('@actions/core');
|
jest.mock('@actions/core');
|
||||||
jest.mock('@actions/core/lib/command');
|
jest.mock('@actions/core/lib/command');
|
||||||
|
|
||||||
|
const command = require('@actions/core/lib/command');
|
||||||
const core = require('@actions/core');
|
const core = require('@actions/core');
|
||||||
const got = require('got');
|
const got = require('got');
|
||||||
const {
|
const {
|
||||||
@@ -294,4 +295,40 @@ describe('exportSecrets', () => {
|
|||||||
expect(core.exportVariable).toBeCalledWith('KEY', '1');
|
expect(core.exportVariable).toBeCalledWith('KEY', '1');
|
||||||
expect(core.setOutput).toBeCalledWith('key', '1');
|
expect(core.setOutput).toBeCalledWith('key', '1');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('single-line secret gets masked', async () => {
|
||||||
|
mockInput('test key');
|
||||||
|
mockVaultData({
|
||||||
|
key: 'secret'
|
||||||
|
});
|
||||||
|
mockExportToken("false")
|
||||||
|
|
||||||
|
await exportSecrets();
|
||||||
|
|
||||||
|
expect(command.issue).toBeCalledTimes(1);
|
||||||
|
|
||||||
|
expect(command.issue).toBeCalledWith('add-mask', 'secret');
|
||||||
|
expect(core.setOutput).toBeCalledWith('key', 'secret');
|
||||||
|
})
|
||||||
|
|
||||||
|
it('multi-line secret gets masked for each line', async () => {
|
||||||
|
const multiLineString = `a multi-line string
|
||||||
|
|
||||||
|
with blank lines
|
||||||
|
|
||||||
|
`
|
||||||
|
mockInput('test key');
|
||||||
|
mockVaultData({
|
||||||
|
key: multiLineString
|
||||||
|
});
|
||||||
|
mockExportToken("false")
|
||||||
|
|
||||||
|
await exportSecrets();
|
||||||
|
|
||||||
|
expect(command.issue).toBeCalledTimes(2); // 1 for each non-empty line.
|
||||||
|
|
||||||
|
expect(command.issue).toBeCalledWith('add-mask', 'a multi-line string');
|
||||||
|
expect(command.issue).toBeCalledWith('add-mask', 'with blank lines');
|
||||||
|
expect(core.setOutput).toBeCalledWith('key', multiLineString);
|
||||||
|
})
|
||||||
});
|
});
|
||||||
|
|||||||
+66
-6
@@ -1,22 +1,55 @@
|
|||||||
// @ts-check
|
// @ts-check
|
||||||
const core = require('@actions/core');
|
const core = require('@actions/core');
|
||||||
|
const rsasign = require('jsrsasign');
|
||||||
|
const fs = require('fs');
|
||||||
|
|
||||||
|
const defaultKubernetesTokenPath = '/var/run/secrets/kubernetes.io/serviceaccount/token'
|
||||||
/***
|
/***
|
||||||
* Authenticate with Vault and retrieve a Vault token that can be used for requests.
|
* Authenticate with Vault and retrieve a Vault token that can be used for requests.
|
||||||
* @param {string} method
|
* @param {string} method
|
||||||
* @param {import('got').Got} client
|
* @param {import('got').Got} client
|
||||||
*/
|
*/
|
||||||
async function retrieveToken(method, client) {
|
async function retrieveToken(method, client) {
|
||||||
|
const path = core.getInput('path', { required: false }) || method;
|
||||||
|
|
||||||
switch (method) {
|
switch (method) {
|
||||||
case 'approle': {
|
case 'approle': {
|
||||||
const vaultRoleId = core.getInput('roleId', { required: true });
|
const vaultRoleId = core.getInput('roleId', { required: true });
|
||||||
const vaultSecretId = core.getInput('secretId', { required: true });
|
const vaultSecretId = core.getInput('secretId', { required: true });
|
||||||
return await getClientToken(client, method, { role_id: vaultRoleId, secret_id: vaultSecretId });
|
return await getClientToken(client, method, path, { role_id: vaultRoleId, secret_id: vaultSecretId });
|
||||||
}
|
}
|
||||||
case 'github': {
|
case 'github': {
|
||||||
const githubToken = core.getInput('githubToken', { required: true });
|
const githubToken = core.getInput('githubToken', { required: true });
|
||||||
return await getClientToken(client, method, { token: githubToken });
|
return await getClientToken(client, method, path, { token: githubToken });
|
||||||
}
|
}
|
||||||
|
case 'jwt': {
|
||||||
|
/** @type {string} */
|
||||||
|
let jwt;
|
||||||
|
const role = core.getInput('role', { required: true });
|
||||||
|
const privateKeyRaw = core.getInput('jwtPrivateKey', { required: false });
|
||||||
|
const privateKey = Buffer.from(privateKeyRaw, 'base64').toString();
|
||||||
|
const keyPassword = core.getInput('jwtKeyPassword', { required: false });
|
||||||
|
const tokenTtl = core.getInput('jwtTtl', { required: false }) || '3600'; // 1 hour
|
||||||
|
const githubAudience = core.getInput('jwtGithubAudience', { required: false });
|
||||||
|
|
||||||
|
if (!privateKey) {
|
||||||
|
jwt = await core.getIDToken(githubAudience)
|
||||||
|
} else {
|
||||||
|
jwt = generateJwt(privateKey, keyPassword, Number(tokenTtl));
|
||||||
|
}
|
||||||
|
|
||||||
|
return await getClientToken(client, method, path, { jwt: jwt, role: role });
|
||||||
|
}
|
||||||
|
case 'kubernetes': {
|
||||||
|
const role = core.getInput('role', { required: true })
|
||||||
|
const tokenPath = core.getInput('kubernetesTokenPath', { required: false }) || defaultKubernetesTokenPath
|
||||||
|
const data = fs.readFileSync(tokenPath, 'utf8')
|
||||||
|
if (!(role && data) && data != "") {
|
||||||
|
throw new Error("Role Name must be set and a kubernetes token must set")
|
||||||
|
}
|
||||||
|
return await getClientToken(client, method, path, { jwt: data, role: role })
|
||||||
|
}
|
||||||
|
|
||||||
default: {
|
default: {
|
||||||
if (!method || method === 'token') {
|
if (!method || method === 'token') {
|
||||||
return core.getInput('token', { required: true });
|
return core.getInput('token', { required: true });
|
||||||
@@ -26,19 +59,46 @@ async function retrieveToken(method, client) {
|
|||||||
if (!payload) {
|
if (!payload) {
|
||||||
throw Error('When using a custom authentication method, you must provide the payload');
|
throw Error('When using a custom authentication method, you must provide the payload');
|
||||||
}
|
}
|
||||||
return await getClientToken(client, method, JSON.parse(payload.trim()));
|
return await getClientToken(client, method, path, JSON.parse(payload.trim()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/***
|
||||||
|
* Generates signed Json Web Token with specified private key and ttl
|
||||||
|
* @param {string} privateKey
|
||||||
|
* @param {string} keyPassword
|
||||||
|
* @param {number} ttl
|
||||||
|
*/
|
||||||
|
function generateJwt(privateKey, keyPassword, ttl) {
|
||||||
|
const alg = 'RS256';
|
||||||
|
const header = { alg: alg, typ: 'JWT' };
|
||||||
|
const now = rsasign.KJUR.jws.IntDate.getNow();
|
||||||
|
const payload = {
|
||||||
|
iss: 'vault-action',
|
||||||
|
iat: now,
|
||||||
|
nbf: now,
|
||||||
|
exp: now + ttl,
|
||||||
|
event: process.env.GITHUB_EVENT_NAME,
|
||||||
|
workflow: process.env.GITHUB_WORKFLOW,
|
||||||
|
sha: process.env.GITHUB_SHA,
|
||||||
|
actor: process.env.GITHUB_ACTOR,
|
||||||
|
repository: process.env.GITHUB_REPOSITORY,
|
||||||
|
ref: process.env.GITHUB_REF
|
||||||
|
};
|
||||||
|
const decryptedKey = rsasign.KEYUTIL.getKey(privateKey, keyPassword);
|
||||||
|
return rsasign.KJUR.jws.JWS.sign(alg, JSON.stringify(header), JSON.stringify(payload), decryptedKey);
|
||||||
|
}
|
||||||
|
|
||||||
/***
|
/***
|
||||||
* Call the appropriate login endpoint and parse out the token in the response.
|
* Call the appropriate login endpoint and parse out the token in the response.
|
||||||
* @param {import('got').Got} client
|
* @param {import('got').Got} client
|
||||||
* @param {string} method
|
* @param {string} method
|
||||||
|
* @param {string} path
|
||||||
* @param {any} payload
|
* @param {any} payload
|
||||||
*/
|
*/
|
||||||
async function getClientToken(client, method, payload) {
|
async function getClientToken(client, method, path, payload) {
|
||||||
/** @type {'json'} */
|
/** @type {'json'} */
|
||||||
const responseType = 'json';
|
const responseType = 'json';
|
||||||
var options = {
|
var options = {
|
||||||
@@ -46,10 +106,10 @@ async function getClientToken(client, method, payload) {
|
|||||||
responseType,
|
responseType,
|
||||||
};
|
};
|
||||||
|
|
||||||
core.debug(`Retrieving Vault Token from v1/auth/${method}/login endpoint`);
|
core.debug(`Retrieving Vault Token from v1/auth/${path}/login endpoint`);
|
||||||
|
|
||||||
/** @type {import('got').Response<VaultLoginResponse>} */
|
/** @type {import('got').Response<VaultLoginResponse>} */
|
||||||
const response = await client.post(`v1/auth/${method}/login`, options);
|
const response = await client.post(`v1/auth/${path}/login`, options);
|
||||||
if (response && response.body && response.body.auth && response.body.auth.client_token) {
|
if (response && response.body && response.body.auth && response.body.auth.client_token) {
|
||||||
core.debug('✔ Vault Token successfully retrieved');
|
core.debug('✔ Vault Token successfully retrieved');
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,83 @@
|
|||||||
|
jest.mock('got');
|
||||||
|
jest.mock('@actions/core');
|
||||||
|
jest.mock('@actions/core/lib/command');
|
||||||
|
jest.mock("fs")
|
||||||
|
|
||||||
|
const core = require('@actions/core');
|
||||||
|
const got = require('got');
|
||||||
|
const fs = require("fs")
|
||||||
|
const { when } = require('jest-when');
|
||||||
|
|
||||||
|
|
||||||
|
const {
|
||||||
|
retrieveToken
|
||||||
|
} = require('./auth');
|
||||||
|
|
||||||
|
|
||||||
|
function mockInput(name, key) {
|
||||||
|
when(core.getInput)
|
||||||
|
.calledWith(name)
|
||||||
|
.mockReturnValueOnce(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
function mockApiResponse() {
|
||||||
|
const response = { body: { auth: { client_token: testToken, renewable: true, policies: [], accessor: "accessor" } } }
|
||||||
|
got.post = jest.fn()
|
||||||
|
got.post.mockReturnValue(response)
|
||||||
|
}
|
||||||
|
const testToken = "testoken";
|
||||||
|
|
||||||
|
describe("test retrival for token", () => {
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
jest.resetAllMocks();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("test retrival with approle", async () => {
|
||||||
|
const method = 'approle'
|
||||||
|
mockApiResponse()
|
||||||
|
const testRoleId = "testRoleId"
|
||||||
|
const testSecretId = "testSecretId"
|
||||||
|
mockInput("roleId", testRoleId)
|
||||||
|
mockInput("secretId", testSecretId)
|
||||||
|
const token = await retrieveToken(method, got)
|
||||||
|
expect(token).toEqual(testToken)
|
||||||
|
const payload = got.post.mock.calls[0][1].json
|
||||||
|
expect(payload).toEqual({ role_id: testRoleId, secret_id: testSecretId })
|
||||||
|
const url = got.post.mock.calls[0][0]
|
||||||
|
expect(url).toContain('approle')
|
||||||
|
})
|
||||||
|
|
||||||
|
it("test retrival with github token", async () => {
|
||||||
|
const method = 'github'
|
||||||
|
mockApiResponse()
|
||||||
|
const githubToken = "githubtoken"
|
||||||
|
mockInput("githubToken", githubToken)
|
||||||
|
const token = await retrieveToken(method, got)
|
||||||
|
expect(token).toEqual(testToken)
|
||||||
|
const payload = got.post.mock.calls[0][1].json
|
||||||
|
expect(payload).toEqual({ token: githubToken })
|
||||||
|
const url = got.post.mock.calls[0][0]
|
||||||
|
expect(url).toContain('github')
|
||||||
|
})
|
||||||
|
|
||||||
|
it("test retrival with kubernetes", async () => {
|
||||||
|
const method = 'kubernetes'
|
||||||
|
const jwtToken = "someJwtToken"
|
||||||
|
const testRole = "testRole"
|
||||||
|
const testTokenPath = "testTokenPath"
|
||||||
|
const testPath = 'differentK8sPath'
|
||||||
|
mockApiResponse()
|
||||||
|
mockInput("kubernetesTokenPath", testTokenPath)
|
||||||
|
mockInput("role", testRole)
|
||||||
|
mockInput("path", testPath)
|
||||||
|
fs.readFileSync = jest.fn()
|
||||||
|
fs.readFileSync.mockReturnValueOnce(jwtToken)
|
||||||
|
const token = await retrieveToken(method, got)
|
||||||
|
expect(token).toEqual(testToken)
|
||||||
|
const payload = got.post.mock.calls[0][1].json
|
||||||
|
expect(payload).toEqual({ jwt: jwtToken, role: testRole })
|
||||||
|
const url = got.post.mock.calls[0][0]
|
||||||
|
expect(url).toContain('differentK8sPath')
|
||||||
|
})
|
||||||
|
})
|
||||||
Reference in New Issue
Block a user