Where Secrets Leak
| Leak path | Example | Prevention |
|---|---|---|
| Printed in logs | echo $TOKEN, env, set -x, docker login -p | Use withCredentials, set +x, --password-stdin |
| Groovy string interpolation | sh "curl -H 'Authorization: ${TOKEN}'" puts the secret in the process list and log | Use single quotes so the shell reads the variable |
| Committed to Git | .env file, hard-coded key | Secret scanning (gitleaks, trufflehog), pre-commit hooks |
| Baked into images | ENV PASSWORD=..., copied key file | Build secrets (--secret), runtime injection |
| Pull request from a fork | A malicious Jenkinsfile reads credentials | Do not give secrets to untrusted code |
| Artifacts and test reports | Config files or core dumps archived | Review what archiveArtifacts collects |
**** in logs. If a secret is transformed (base64, URL-encoded, split in pieces), masking may not catch it. Treat masking as a safety net, not a plan.
Use HashiCorp Vault
A secrets manager gives central storage, audit, rotation and short-lived secrets. Jenkins asks Vault at build time instead of storing the secret.
- Use AppRole (or Kubernetes auth, or JWT/OIDC) for Jenkins, never a root token
- Policy: the Jenkins role can read only the paths it needs
- Use short TTL tokens and, where possible, dynamic secrets (Vault creates a database user that expires)
- Enable Vault's audit log: you can see who read which secret
Cloud Secret Managers and Short-Lived Access
| Platform | Options |
|---|---|
| AWS | Secrets Manager or SSM Parameter Store plugins; IAM roles for agents (no long-lived keys on disk); OIDC federation |
| Azure | Key Vault plugin; managed identity or workload identity |
| GCP | Secret Manager; workload identity federation |
| Kubernetes agents | Service account with IRSA / Workload Identity so a pod gets cloud permissions with no static key |
The best secret is the one that does not exist: give the build an identity (IAM role, workload identity) and let the cloud issue temporary credentials.
Script Security and the Groovy Sandbox
Pipeline Groovy can call almost any Java method, including ones that read files on the controller or run commands. The Script Security plugin protects Jenkins.
| Concept | Meaning |
|---|---|
| Groovy sandbox | Pipeline scripts run in a sandbox that allows only an approved list of methods |
| Script approval | A blocked method appears in Manage Jenkins → In-process Script Approval. An admin can approve it. Approve only what you understand |
| Trusted libraries | Global libraries from Manage Jenkins run without the sandbox. Treat their repository as admin-level code |
| Jenkinsfile from SCM | Runs in the sandbox by default |
new File, Runtime.exec, System.getenv, Jenkins.instance or reflection can read secrets or control Jenkins. If a pipeline needs them, move that logic into a trusted library step with narrow, reviewed behavior.
Trust Boundaries: Pull Requests
| Source of change | Risk | Safe handling |
|---|---|---|
| Branch in your repository by a team member | Low | Normal pipeline with credentials |
| Pull request from a fork (outside contributor) | High: they can change the Jenkinsfile | Build with no credentials, or only trust the Jenkinsfile from the target branch; approve before building |
| Third-party plugin or library | Supply-chain risk | Pin versions, review updates, use known sources |
- In the GitHub Branch Source plugin set Discover pull requests from forks → Trust: Contributors or Nobody
- Gate credentials by branch:
when { branch 'main' }around deploy stages - Use the Trusted Jenkinsfile option so PR builds use the base branch pipeline
Detect Secrets Early
- Add the same scan as a pre-commit hook so developers see it before pushing
- Scan the Git history at least once; old commits keep old secrets
If a Secret Leaks
- Revoke or rotate the secret now at the source. Deleting the log line is not enough
- Check the audit trail for use of the secret
- Remove it from Git history only after rotation (
git filter-repo), and expect forks to keep copies - Find the cause and fix it (interpolation, debug print, artifact)
- Document the incident and add a scan or check to prevent repeats
Practice Tasks
- Create a pipeline that prints a secret the wrong way (double quotes) and observe where it shows, then fix it
- Install a lab Vault, create an AppRole and read one secret from Jenkins
- Open In-process Script Approval and learn why a method was blocked
- Configure a multibranch job so fork PRs cannot use credentials
- Add gitleaks to a pipeline and make it fail on a fake key
Conclusion
Keep secrets out of code, images and logs: use credentials binding with single quotes, a secrets manager such as Vault with short-lived access, and cloud identities instead of static keys. Keep the Groovy sandbox on, approve scripts carefully, limit trusted libraries, and never expose credentials to fork pull requests.

