To dynamically inject values from a secret stored in HashiCorp Vault into an NGINX Ingress configuration, you'll generally need an intermediary process because NGINX Ingress does not directly support fetching secrets from Vault during runtime.
Here’s a more detailed step-by-step approach to achieve your goal, specifically replacing URL_FROM_VALUES with the actual value from the secret stored in Vault:
Steps to Retrieve Secrets from Vault and Update Ingress
-
Fetch Secrets from Vault: Use a Kubernetes Job or an external script to retrieve the secret you need and store it in a Kubernetes Secret or ConfigMap.
-
Update Ingress with the Retrieved Values: Create a process (could be a Kubernetes CronJob, or a CI/CD pipeline) that will read the secret and then update the Ingress resource with the retrieved values.
-
Example Implementation:
Here's a more structured breakdown of how to implement it:
Step 1: Kubernetes Job to Fetch Secrets from Vault
Create a Kubernetes Job to fetch the required secrets from Vault and store them in a Kubernetes Secret. Here's an example:
apiVersion: batch/v1
kind: Job
metadata:
name: fetch-vault-secret
spec:
template:
spec:
containers:
- name: vault-fetcher
image: hashicorp/vault:latest
command: ["sh", "-c", "vault kv get -field=your_field_name secret/path/to/secret | kubectl create secret generic your-secret-name --from-literal=url_value=$(cat) --dry-run=client -o yaml | kubectl apply -f -"]
env:
- name: VAULT_ADDR
value: "http://vault.default.svc.cluster.local:8200" # Point to your Vault address
- name: VAULT_TOKEN
valueFrom:
secretKeyRef:
name: vault-token
key: token
restartPolicy: Never
In this job, replace your_field_name with the field you are looking for and secret/path/to/secret with the path to your secret in Vault.
Step 2: Configure Your Ingress Resource
You would set up your Ingress using annotations to use the secret value, but you'll need to perform some automated updates. While you cannot use the secret directly in the configuration as-is, you can create a script (or another job) to fetch the secret and update the Ingress.
Here is an example of how you can reference the secret in an Ingress YAML, using a placeholder ($URL_FROM_SECRET) that you will replace with your update script:
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: my-ingress
annotations:
nginx.ingress.kubernetes.io/configuration-snippet: |
more_set_headers "X-Frame-Options: SAMEORIGIN";
more_set_headers "Content-Security-Policy: frame-src 'self' $URL_FROM_SECRET; object-src 'none'; img-src 'self' URL2_FROM_VALUES data:;";
spec:
rules:
- host: myapp.example.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: my-service
port:
number: 80
Step 3: Script to Update Ingress
Create a script that fetches the secret from Kubernetes and modifies the Ingress configuration:
#!/bin/bash
# Fetch the secret
URL_FROM_SECRET=$(kubectl get secret your-secret-name -o jsonpath='{.data.url_value}' | base64 --decode)
# Update the Ingress resource
kubectl patch ingress my-ingress --type='json' -p="[{\"op\": \"replace\", \"path\": \"/metadata/annotations/nginx.ingress.kubernetes.io/configuration-snippet\", \"value\": \"more_set_headers \\\"X-Frame-Options: SAMEORIGIN\\\"; more_set_headers \\\"Content-Security-Policy: frame-src 'self' ${URL_FROM_SECRET}; object-src 'none'; img-src 'self' URL2_FROM_VALUES data:\\\";}\"}]"
Execution
- Run the Kubernetes Job to fetch the secret.
- Execute the script to update the Ingress with the new value.
Note
-
Security: Make sure that your scripts and jobs handle sensitive information appropriately and ensure that you adhere to security best practices when managing sensitive data.
-
Automation: You may want to automate this process further, such as making the secret-fetching job and the Ingress updating job run periodically or triggered by changes in the Vault secret.
-
Error Handling: Add error handling in scripts to manage failures gracefully.
-
Testing: Always test the setup in a development environment before deploying it to production.
This sequence allows you to dynamically change the Ingress header values based on the secrets stored in Vault, adhering to the operationally best practices of Kubernetes and NGINX.