Securing Credentials and Environment Variables
Table of Contents
Section titled “Table of Contents”- Overview
- Threat Model
- Core Security Principles
- Credential Types and Risk Levels
- Environment Variable Hardening
- CI/CD Pipeline Patterns
- Jenkins Hardening Patterns
- GitHub Actions Hardening Patterns
- Kubernetes Runtime Patterns
- Secret Rotation and Revocation
- Logging, Telemetry, and Leak Detection
- Policy and Governance Controls
- Implementation Checklist
- Conclusion
Securing Credentials and Environment Variables
Section titled “Securing Credentials and Environment Variables”Overview
Section titled “Overview”Credentials and environment variables are common compromise paths in modern delivery systems. Most incidents are not caused by crypto weaknesses. They come from mis-scoped tokens, leaked logs, overprivileged CI jobs, and long-lived secrets.
This guide provides practical controls for CI/CD pipelines and runtime systems.
Threat Model
Section titled “Threat Model”Primary attack paths:
- Secrets committed to source control.
- Secrets exposed in build logs and artifacts.
- Stolen CI runner tokens with broad privileges.
- Lateral movement via shared environment variables.
- Compromised dependency or pipeline step exfiltrating secrets.
High-value targets:
- Cloud API keys and workload credentials.
- Artifact signing keys.
- Registry and deployment tokens.
- Database credentials.
Core Security Principles
Section titled “Core Security Principles”- Use short-lived credentials whenever possible.
- Scope credentials to least privilege and shortest lifetime.
- Inject credentials at runtime, never at build authoring time.
- Keep secrets out of source, logs, and build outputs.
- Make all sensitive operations auditable.
Credential Types and Risk Levels
Section titled “Credential Types and Risk Levels”- Static long-lived keys:
- Highest risk.
- Use only as temporary migration fallback.
- Rotating service tokens:
- Better than static keys.
- Still risky if broadly scoped.
- Dynamic credentials from secret brokers (Vault, cloud STS):
- Preferred.
- Time-bound and revocable.
- Federated identity (OIDC/workload identity):
- Strongest default for CI-to-cloud auth.
- Removes many stored secret cases.
Environment Variable Hardening
Section titled “Environment Variable Hardening”Environment variables are useful but easy to leak.
Controls:
- Do not place secrets in global job-level environment blocks.
- Inject secret env vars only in the narrowest stage/step scope.
- Disable command echo and shell tracing in sensitive steps.
- Mask high-risk patterns in logs.
- Separate non-sensitive config from secrets.
Bad pattern:
export AWS_SECRET_ACCESS_KEY="..."set -x./deploy.shSafer pattern:
set +xaws sts assume-role --role-arn "$ROLE_ARN" --role-session-name "ci" > /tmp/creds.jsonexport AWS_ACCESS_KEY_ID="$(jq -r .Credentials.AccessKeyId /tmp/creds.json)"export AWS_SECRET_ACCESS_KEY="$(jq -r .Credentials.SecretAccessKey /tmp/creds.json)"export AWS_SESSION_TOKEN="$(jq -r .Credentials.SessionToken /tmp/creds.json)"./deploy.shunset AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY AWS_SESSION_TOKENCI/CD Pipeline Patterns
Section titled “CI/CD Pipeline Patterns”Recommended pipeline control flow:
- Authenticate workload identity.
- Exchange for short-lived token.
- Fetch scoped secret from broker.
- Use secret in one isolated step.
- Clear environment and workspace sensitive files.
- Emit redacted audit metadata only.
Design rule:
- No secret should survive beyond the stage that needs it.
Jenkins Hardening Patterns
Section titled “Jenkins Hardening Patterns”- Use
withCredentialsfor short-lived scoped exposure. - Avoid storing secrets in global node environment configuration.
- Restrict pipeline script approvals and shared library update rights.
- Run agents as ephemeral workers, not long-lived mutable hosts.
- Deny outbound egress from jobs that do not require internet access.
Example:
pipeline { agent any stages { stage('Deploy') { steps { withCredentials([string(credentialsId: 'deploy-token', variable: 'DEPLOY_TOKEN')]) { sh ''' set +x ./deploy.sh ''' } } } }}GitHub Actions Hardening Patterns
Section titled “GitHub Actions Hardening Patterns”- Prefer OIDC federation to cloud providers over static repository secrets.
- Set
permissionsminimally per workflow/job. - Use environment protection rules for production credentials.
- Prevent untrusted forks from accessing protected secrets.
- Pin third-party actions by immutable commit SHA.
Example:
permissions: id-token: write contents: read packages: writeKubernetes Runtime Patterns
Section titled “Kubernetes Runtime Patterns”- Store secrets in dedicated secret manager integration where possible.
- Limit secret access with namespace and service account boundaries.
- Mount secrets only into pods that need them.
- Rotate secrets and trigger rolling restarts safely.
- Avoid passing high-value credentials via plain env vars for long-lived pods when file mounts or broker fetch is possible.
Secret Rotation and Revocation
Section titled “Secret Rotation and Revocation”Rotation policy:
- Human credentials: rapid expiration plus MFA.
- Machine credentials: automatic rotation on short schedule.
- Emergency revocation: immediate disable path with runbook.
Operational requirements:
- Track secret age and last-used timestamp.
- Alert on stale or unused privileged credentials.
- Validate rotation through synthetic pipeline tests.
Logging, Telemetry, and Leak Detection
Section titled “Logging, Telemetry, and Leak Detection”- Use log redaction filters in CI and runtime logs.
- Scan commits and artifacts for secret patterns.
- Alert on suspicious token use geography/time anomalies.
- Retain immutable audit logs for credential issuance and use.
- Correlate pipeline run IDs to cloud API calls for forensic traceability.
Policy and Governance Controls
Section titled “Policy and Governance Controls”- Policy as code gates on secret usage patterns.
- Enforce mandatory rotation intervals for high-privilege credentials.
- Block pipeline merges that introduce plaintext secrets.
- Require peer review for changes touching auth and secret handling.
- Periodically run break-glass tabletop exercises.
Implementation Checklist
Section titled “Implementation Checklist”- Remove static cloud keys from CI and move to OIDC or dynamic tokens.
- Scope all secret injection to stage-level, not pipeline-level.
- Disable shell tracing in secret-handling steps.
- Implement secret scanning in pre-commit and CI.
- Add automated rotation and immediate revocation runbooks.
- Audit all privileged token scopes and reduce blast radius.
- Verify no sensitive env vars are exported into artifacts.
Conclusion
Section titled “Conclusion”Credential security is mostly a lifecycle and blast-radius problem. If you adopt short-lived identity, scoped injection, strict logging hygiene, and enforceable policy gates, you can reduce compromise risk substantially without slowing delivery.