Handling credentials in network automation: secrets managers, service accounts and least privilege
In short: Automation needs credentials to reach devices and APIs, which makes them valuable targets. Never store them in scripts or Git. Load them at run time from environment variables, encrypted files or, best, a secrets manager. Use dedicated automation accounts with only the permissions they need, rotate secrets regularly, and scan repositories for anything leaked.
The risk
A script with a hard-coded admin password, pushed to a shared Git repository, gives anyone with access the keys to the whole network. Leaked credentials in repositories are a very common cause of breaches.
Options, from basic to best
- Environment variables: set at run time and read with
os.environ. Better than hard-coding, but they can appear in process listings or shell history if set carelessly. - Encrypted files: Ansible Vault or Mozilla SOPS encrypt values in files that can be stored in Git; the decryption key is kept elsewhere.
- CI/CD secrets: pipeline platforms store secrets and inject them as masked variables during jobs.
- Secrets managers: HashiCorp Vault, AWS Secrets Manager, Azure Key Vault and similar tools provide access control, audit logs, rotation and short-lived credentials.
Prompting for a password
import getpass
import os
username = os.environ.get("NET_USER") or input("Username: ")
password = os.environ.get("NET_PASS") or getpass.getpass("Password: ")
For interactive scripts, getpass hides the password as it is typed.
Automation accounts and least privilege
- Create dedicated service accounts for automation, separate from personal accounts.
- Use TACACS+ or RADIUS authorisation to limit commands: a backup job only needs read access, a compliance check only needs show commands.
- Restrict where automation accounts can log in from (automation servers only).
- Log and monitor their use.
Rotation and leaks
- Rotate automation passwords and API tokens on a schedule and whenever staff leave.
- Prefer SSH keys or short-lived tokens where devices support them.
- Use secret scanning tools (for example gitleaks or built-in repository scanning) to catch credentials before they are pushed.
- If a secret leaks, rotate it immediately; deleting the commit is not enough because history and copies remain.
Real-world example: the public repository
An engineer shared a useful backup script on a public code site, forgetting that the device password was in the file. Automated scanners found it within hours. Fortunately, management access was restricted to internal networks. The team rotated the password, moved credentials into Vault and added secret scanning to every repository.
Practice
Why is deleting a commit with a leaked password not enough?
What advantages do secrets managers provide?
How can TACACS+ support least privilege for automation?
Key takeaways
- Never store credentials in scripts or plain text in Git.
- Use environment variables, encrypted files, CI secrets or a secrets manager.
- Give automation dedicated, least-privilege accounts with restricted access.
- Rotate secrets and scan repositories for leaks.
Original content written by Mohamed Fayas for FM TecHub. © 2026 FM TecHub. You're welcome to share a link to this page; please don't copy the text elsewhere without permission.