Jenkins · Lesson 5 of 11 · 5 min read
Credentials and security
The credentials store
| Credential type | Example |
|---|---|
| Secret text | API token, Slack token |
| Username with password | Registry login, Nexus |
| SSH username with private key | Git over SSH, SSH agents |
| Secret file | kubeconfig, service account JSON |
| Certificate | PKCS#12 client cert |
| AWS credentials (plugin) | Access key pair — avoid; use roles |
Scopes: System (only Jenkins itself, e.g. connecting to agents), Global (available to jobs), and folder-level credentials visible only to jobs in that folder — the main way to isolate teams' secrets on a shared controller.
Credentials are encrypted on disk with keys in $JENKINS_HOME/secrets. Anyone who can run arbitrary code on the controller (admins, script console, unsandboxed Groovy) can decrypt them.
Enlarge diagram
Swipe sideways to see all of it, or tap to open it full screen.
Using credentials safely in pipelines
withCredentials([
usernamePassword(credentialsId: 'nexus', usernameVariable: 'NEXUS_USER', passwordVariable: 'NEXUS_PASS'),
string(credentialsId: 'sonar-token', variable: 'SONAR_TOKEN'),
file(credentialsId: 'staging-kubeconfig', variable: 'KUBECONFIG')
]) {
sh 'mvn -B deploy -s settings.xml' // settings.xml reads $NEXUS_USER/$NEXUS_PASS
sh 'kubectl get ns'
}
- Bound values are masked in console output (
****) — but masking is best-effort: base64-encoding or printing character by character defeats it, and anything written to files or sent elsewhere isn't masked. - Use single-quoted
shstrings so the shell, not Groovy, expands the secret. Groovy interpolation (sh "curl -u $USER:$PASS") puts the secret into the command string, can expose it in process listings, and Jenkins warns about it. - In declarative pipelines,
environment { TOKEN = credentials('id') }binds a credential for the whole pipeline or stage; for username/password credentials it also createsTOKEN_USRandTOKEN_PSW.
AWS access without static keys
The best answer: no long-lived AWS keys in Jenkins at all.
- Jenkins on EKS — the build pod's service account uses IRSA / EKS Pod Identity to assume a scoped IAM role.
- Jenkins on EC2 — the agent's instance profile, then
sts:AssumeRoleinto per-environment roles (withAWS(role: 'deploy-prod', ...)from the Pipeline: AWS Steps plugin). - OIDC — Jenkins can act as an OIDC identity provider (OpenID Connect Provider plugin) so builds exchange a short-lived token for an AWS role via
AssumeRoleWithWebIdentity, the same model GitHub Actions uses.
Enlarge diagram
Swipe sideways to see all of it, or tap to open it full screen.
Securing the controller
- Authentication: SSO via SAML/OIDC/LDAP/AD; no local accounts except a break-glass admin.
- Authorization: Role-based Strategy or Matrix with folder-level permissions; developers get build/read on their folders; very few admins. Never "Anyone can do anything" or "Logged-in users can do anything".
- No builds on the controller (0 executors).
- Script Security: pipelines run in the Groovy sandbox; unsafe method calls require admin script approval — approve narrowly. Restrict the Script Console (it's arbitrary code execution as the Jenkins user).
- Agent → controller security stays enabled so a compromised agent can't read controller files.
- CSRF protection on (API callers use an API token, which doesn't need a crumb, or a crumb with a session).
- Network: controller behind a load balancer with TLS, not exposed to the internet if possible; webhooks via an allow-listed path or relay.
- Updates: track LTS releases and security advisories; patch quickly.
- Approvals:
input(submitter: 'release-managers')to restrict who can promote to production; combine with folder permissions so others can't edit the job to remove it. - Audit: Audit Trail plugin, and pipeline/config changes go through Git (JCasC, Jenkinsfiles) so they're reviewed.
Enlarge diagram
Swipe sideways to see all of it, or tap to open it full screen.