In today's security-first environments, password-based authentication is gradually being phased out in favor of more robust methods like key-based authentication. Our team recently moved to this model as part of our compliance and security hardening efforts. With around 500 users, manually generating SSH or encryption key pairs for each person wasn’t practical, so we automated the process using a simple Python script — and it’s been a game changer.
Instead of asking users to generate their own keys (which leads to inconsistent key strengths, file formats, and often misconfiguration), we decided to centrally generate encrypted RSA key pairs. The script leverages OpenSSL via the command line to create a 2048-bit RSA private key for each user, encrypts it with a passphrase, and then extracts the corresponding public key. We also output a CSV file listing all the generated key paths for easy reference and tracking.
The idea is simple: take a list of usernames, loop through each one, and generate a secure, encrypted RSA key pair. The private key is protected with a passphrase, and the public key is extracted using OpenSSL. Everything gets saved in an output folder, and we generate a CSV with the paths for easy reference.
Before you run the script, make sure you’ve got OpenSSL installed. On Windows, Git Bash includes it, so we just pointed the script to that executable. If you're on Linux or macOS, you’re probably already good to go.
Here’s what the script looks like:
You drop your usernames in a users.txt file, one per line. The script loops through them, spits out an encrypted .p8 private key and a .pub public key for each, and logs it all to a CSV. Super handy for onboarding or rotating keys across a large user base.
Couple of notes:
-
You can prompt for the passphrase using
getpass()if you’re running it manually. -
For automation (like in CI/CD), hardcoding the passphrase works, but make sure you’re storing it securely — don’t commit it to git or leave it lying around.
-
You’ll need to update the file paths before running. The placeholders are obvious.
This saved us hours of manual work and helped us enforce consistent key standards across the board. If you’ve got a big user base and need to move away from passwords, this little script might just make your life easier.
Comments
Post a Comment