Every automation needs to prove who it is. A deployment pipeline needs cloud access, a nightly export needs database credentials, and an integration needs an API key for the partner system. Those credentials are secrets, and automation tends to accumulate them in the worst possible places: hard-coded in scripts, pasted into environment files, stored as long-lived CI variables, or echoed into logs during debugging.
This guide sets out a practical approach to secrets management for automation scripts and pipelines. It draws on the OWASP Secrets Management Cheat Sheet, cloud provider documentation and MITRE's weakness catalogue, and is written for teams that run real business processes on scripts, schedulers and pipelines rather than for a single application.
Why automation is a secrets problem
Human users sign in, use multi-factor authentication and leave. Automation holds its access permanently, runs unattended and is often copied from one script to the next. MITRE classifies embedding credentials in code or configuration as CWE-798: Use of Hard-coded Credentials, and notes that when the same credential is shared across installations it can enable widespread attacks. In automation, the equivalent risk is one credential reused by many scripts: compromise any one of them and you have compromised all of them.
There is also a reliability angle. Credentials expire, get rotated or get revoked. If nobody knows which scripts use a given secret, a routine rotation becomes an outage. Good secrets management is therefore as much about business reliability as it is about security.
Step 1: Find out where your secrets live today
Before changing anything, build an inventory. Look in:
- Source repositories, including history, not just the current branch.
.envfiles, configuration files and deployment manifests.- CI/CD variables and pipeline definitions.
- Scheduler configurations (cron entries, task schedulers, workflow tools).
- Container images and virtual machine templates.
- Log files, error trackers and chat channels where output gets pasted.
For each secret, record what it grants access to, which automations use it, who owns it and when it was last rotated. Secrets with no known owner or consumer are candidates for revocation.
Step 2: Prefer no secret at all
The best secret is one you never have to store. Many platforms now let automation authenticate with a short-lived identity instead of a long-lived key. GitHub's documentation on OpenID Connect for GitHub Actions describes the model: you don't need to duplicate cloud credentials as long-lived GitHub secrets, because the cloud provider issues a short-lived access token that is only valid for a single job and then expires. Trust conditions on the cloud side control which repositories and workflows may request that token.
The same principle applies elsewhere: cloud workloads can use attached roles or managed identities; services inside a platform can use platform-issued identities. Whenever a script currently holds a static cloud key, ask whether it could use a federated or workload identity instead.
Step 3: Centralise what remains in a secrets manager
Some secrets cannot be eliminated, such as a partner's API key or a legacy database password. OWASP recommends standardising and centralising secrets management, while accepting that different environments may reasonably use different tools (for example, a cloud provider's secret store for cloud-native teams and a separate system for private infrastructure). What matters is that each secret has one authoritative home.
Practical rules for automation:
- Fetch at runtime. Scripts retrieve secrets from the manager when they run, rather than having them baked into images, repositories or scheduler configuration.
- Never write secrets to disk unless a tool requires a file, and then use a temporary file with restrictive permissions that is deleted afterwards.
- Keep CI/CD secrets scoped. OWASP describes three acceptable patterns for pipelines: storing secrets in the CI/CD tool with rotation and restricted visibility, pulling them from a dedicated secrets manager, or having deployed services fetch their own secrets so the pipeline never sees them.
Step 4: One identity per automation, with least privilege
Shared "automation" accounts are convenient and dangerous. Give each workflow its own identity and grant only the permissions it needs: read-only where it only reads, write access only to the specific tables, buckets or endpoints it modifies. OWASP emphasises fine-grained access control on each secret, so that people and processes can reach only the secrets required for their role.
Separate identities also make audit logs meaningful. When every script uses the same key, a log entry tells you nothing about which process acted.
Step 5: Design rotation in from the start
OWASP recommends regular rotation of machine secrets to limit the window in which a stolen credential is useful, and describes a lifecycle of creation, rotation, revocation and expiration. (It also notes that for user passwords, NIST guidance favours changing them when compromise is suspected rather than on a fixed schedule; machine credentials are a different case.)
Rotation fails when the consumers of a secret are not ready for it to change. AWS Secrets Manager's documentation on rotation strategies illustrates the two classic approaches for database credentials:
- Single user — one set of credentials is updated in place. AWS describes this as the simplest strategy and appropriate for most use cases, with a short window in which calls using the old credentials might be denied; it recommends mitigating that with an appropriate retry strategy.
- Alternating users — two users exist and rotation alternates between them, so an application that retrieves the secret during rotation still receives a valid set of credentials. AWS positions this for applications that require high availability.
Whichever you choose, write automation that re-reads the secret when authentication fails and retries once or twice with a delay, rather than caching a credential for the lifetime of the process. That single design choice turns rotation from a coordinated change into a non-event.
Step 6: Use dynamic, short-lived credentials where you can
OWASP recommends dynamic secrets wherever possible: credentials generated on demand for a single session or deployment and expiring afterwards, which reduces the value of any single leaked credential. Many secrets managers can issue temporary database or cloud credentials this way. For high-risk automations, such as anything that can move money or delete data, the extra setup is usually justified.
Step 7: Keep secrets out of logs and output
Automation leaks secrets in ways humans rarely would. Common causes:
- Printing full command lines or connection strings in debug output.
- Tools that echo URLs containing embedded credentials (for example,
protocol://user:password@host). - Exceptions that include request headers or configuration objects.
- Pasting job output into tickets or chat when troubleshooting.
Mitigations: enable secret masking in your CI/CD tool, avoid passing secrets as command-line arguments (they can be visible to other processes and captured in shell history), redact known secret patterns in your logging library, and review what your scripts print on failure, not just on success.
Step 8: Detect leaks early
OWASP recommends integrating secrets detection early in development with pre-commit hooks and IDE plugins, and names tools such as Yelp's detect-secrets as examples. Add the same scanning to your CI pipeline so that anything missed locally is caught before merge, and scan existing repositories once as part of the inventory in Step 1. OWASP also suggests standardising test secrets across the organisation so that scanners can ignore them and false positives stay manageable.
Step 9: Audit access
Your secrets manager should record who or what accessed each secret, from where and when, along with rotation events and failed access attempts. OWASP describes auditing as an essential part of secrets management. Review those logs for secrets accessed by identities that should not need them; this is often how orphaned or over-shared credentials are discovered.
When a secret leaks: a short response playbook
OWASP's guidance on exposed secrets comes down to a clear order of operations, which is worth writing into your continuity runbook so nobody has to improvise:
- Revoke the exposed credential immediately.
- Rotate to a new credential, ideally through automation that updates every consumer.
- Remove the secret from code, history and logs where it appeared.
- Review the access history for the credential to understand what it may have been used for, and document the incident.
Order matters. Rewriting repository history does not make a secret safe again, because it may already have been copied. Revocation comes first.
Checklist for automation secrets
- Every secret is inventoried with an owner and a list of consumers.
- Static cloud keys are replaced with federated or workload identities where supported.
- Remaining secrets live in a secrets manager and are fetched at runtime.
- Each automation has its own least-privilege identity.
- Rotation is scheduled and consumers re-read secrets on authentication failure.
- Logs and job output are masked and reviewed for leaks.
- Pre-commit and CI scanning are in place.
- Access to secrets is logged and periodically reviewed.
- A leak response procedure exists and has been rehearsed.
Secrets handling is one part of making automation dependable. Retry logic is another: a rotated credential or a transient failure should not cause duplicate side effects, which we cover in designing idempotent, retry-safe integrations. To discuss your own environment, see our security and automation solutions.
Frequently asked questions
Are environment variables a safe place for secrets?
They are better than hard-coding, but they are not a secrets manager. Environment variables can be exposed through debug output, crash reports and process inspection. Treat them as a delivery mechanism at most, populated at runtime from a secrets manager.
How often should automation credentials be rotated?
There is no universal interval. OWASP recommends regular rotation on a defined schedule to limit exposure; the right frequency depends on what the credential grants and how easily consumers can pick up a new value. Short-lived, dynamically issued credentials reduce the need for scheduled rotation.
What is the first thing to fix?
Hard-coded credentials in source repositories and long-lived cloud keys in CI/CD variables. Both are common, high-impact and usually straightforward to replace.
Need Help Hardening Your Automation?
Talk to our team about continuity, security and reliability for the workflows your business depends on.
Contact Us →