diff --git a/content/actions/deployment/security-hardening-your-deployments/configuring-openid-connect-in-hashicorp-vault.md b/content/actions/deployment/security-hardening-your-deployments/configuring-openid-connect-in-hashicorp-vault.md index 3b10f509b291..fbcb0c2c53ea 100644 --- a/content/actions/deployment/security-hardening-your-deployments/configuring-openid-connect-in-hashicorp-vault.md +++ b/content/actions/deployment/security-hardening-your-deployments/configuring-openid-connect-in-hashicorp-vault.md @@ -32,10 +32,53 @@ This guide gives an overview of how to configure HashiCorp Vault to trust {% dat To use OIDC with HashiCorp Vault, you will need to add a trust configuration for the {% data variables.product.prodname_dotcom %} OIDC provider. For more information, see the HashiCorp Vault [documentation](https://www.vaultproject.io/docs/auth/jwt). -Configure the vault to accept JSON Web Tokens (JWT) for authentication: -- For the `oidc_discovery_url`, use {% ifversion ghes %}`https://HOSTNAME/_services/token`{% else %}`https://token.actions.githubusercontent.com`{% endif %} -- For `bound_issuer`, use {% ifversion ghes %}`https://HOSTNAME/_services/token`{% else %}`https://token.actions.githubusercontent.com`{% endif %} -- Ensure that `bound_subject` is correctly defined for your security requirements. For more information, see ["Configuring the OIDC trust with the cloud"](/actions/deployment/security-hardening-your-deployments/about-security-hardening-with-openid-connect#configuring-the-oidc-trust-with-the-cloud) and [`hashicorp/vault-action`](https://github.com/hashicorp/vault-action). +To configure your Vault server to accept JSON Web Tokens (JWT) for authentication: + +1. Enable the JWT `auth` method, and use `write` to apply the configuration to your Vault. + For `oidc_discovery_url` and `bound_issuer` parameters, use {% ifversion ghes %}`https://HOSTNAME/_services/token`{% else %}`https://token.actions.githubusercontent.com`{% endif %}. These parameters allow the Vault server to verify the received JSON Web Tokens (JWT) during the authentication process. + + ```sh{:copy} + vault auth enable jwt + ``` + + ```sh{:copy} + vault write auth/jwt/config \ + bound_issuer="{% ifversion ghes %}https://HOSTNAME/_services/token{% else %}https://token.actions.githubusercontent.com{% endif %}" \ + oidc_discovery_url="{% ifversion ghes %}https://HOSTNAME/_services/token{% else %}https://token.actions.githubusercontent.com{% endif %}" + ``` +2. Configure a policy that only grants access to the specific paths your workflows will use to retrieve secrets. For more advanced policies, see the HashiCorp Vault [Policies documentation](https://www.vaultproject.io/docs/concepts/policies). + + ```sh{:copy} + vault policy write myproject-production - <`: Replace this with the URL of your HashiCorp Vault. +- ``: Replace this with the Namespace you've set in HashiCorp Vault. For example: `admin`. - ``: Replace this with the role you've set in the HashiCorp Vault trust relationship. -- ``: Replace this with the audience you've defined in the HashiCorp Vault trust relationship. -- ``: Replace this with the path to the secret you're retrieving from HashiCorp Vault. For example: `secret/data/ci npmToken`. +- ``: Replace this with the path to the secret you're retrieving from HashiCorp Vault. For example: `secret/data/production/ci npmToken`. ```yaml{:copy} jobs: - retrieve-secret: - steps: - - name: Retrieve secret from Vault - uses: hashicorp/vault-action@v2.4.0 - with: - url: - role: - method: jwt - jwtGithubAudience: - secrets: + retrieve-secret: + runs-on: ubuntu-latest + permissions: + id-token: write + contents: read + steps: + - name: Retrieve secret from Vault + uses: hashicorp/vault-action@v2.4.0 + with: + method: jwt + url: + namespace: + role: + secrets: - - name: Use secret from Vault - run: | - # This step has access to the secret retrieved above; see hashicorp/vault-action for more details. + - name: Use secret from Vault + run: | + # This step has access to the secret retrieved above; see hashicorp/vault-action for more details. ``` + +{% note %} + +**Note**: + +- If your Vault server is not accessible from the public network, consider using a self-hosted runner with other available Vault [auth methods](https://www.vaultproject.io/docs/auth). For more information, see "[About self-hosted runners](/actions/hosting-your-own-runners/about-self-hosted-runners)." +- `` must be set for a Vault Enterprise (including HCP Vault) deployment. For more information, see [Vault namespace](https://www.vaultproject.io/docs/enterprise/namespaces). + +{% endnote %} + +### Revoking the access token + +By default, the Vault server will automatically revoke access tokens when their TTL is expired, so you don't have to manually revoke the access tokens. However, if you do want to revoke access tokens immediately after your job has completed or failed, you can manually revoke the issued token using the [Vault API](https://www.vaultproject.io/api/auth/token#revoke-a-token-self). + +1. Set the `exportToken` option to `true` (default: `false`). This exports the issued Vault access token as an environment variable: `VAULT_TOKEN`. +2. Add a step to call the [Revoke a Token (Self)](https://www.vaultproject.io/api/auth/token#revoke-a-token-self) Vault API to revoke the access token. + +```yaml{:copy} +jobs: + retrieve-secret: + runs-on: ubuntu-latest + permissions: + id-token: write + contents: read + steps: + - name: Retrieve secret from Vault + uses: hashicorp/vault-action@v2.4.0 + with: + exportToken: true + method: jwt + url: + role: + secrets: + + - name: Use secret from Vault + run: | + # This step has access to the secret retrieved above; see hashicorp/vault-action for more details. + + - name: Revoke token + # This step always runs at the end regardless of the previous steps result + if: always() + run: | + curl -X POST -sv -H "X-Vault-Token: {% raw %}${{ env.VAULT_TOKEN }}{% endraw %}" \ + /v1/auth/token/revoke-self +``` \ No newline at end of file