I think storing credentials inside another *py file is your safest bet. Then just import it. Example would look like this
config.py
username = "xy"
password = "abcd"
main.py
import config
login(config.username, config.password)
Answer from kecer on Stack OverflowI think storing credentials inside another *py file is your safest bet. Then just import it. Example would look like this
config.py
username = "xy"
password = "abcd"
main.py
import config
login(config.username, config.password)
I was dealing exactly the same question and actually ended up with the same solution as kecer suggested. Since I need to use it in dozens of scripts, I've created own library. Let me share this solution with you.
credlib.py -- universal library to handle credentials
class credential:
def __init__(self, hostname, username, password):
self.hostname = hostname
self.username = username
self.password = password
mycredentials.py -- my local file to store all credentials
from credlib import credential
sys_prod = credential("srv01", "user", "pass")
sys_stg = credential("srv02", "user", "pass")
sys_db = credential("db01", "userdb", "passdb")
mysystemlib.py -- this is a general library to access my system (both new credential system and legacy is supported)
from credlib import credential
def system_login(*args): # this is new function definition
#def system_login(hostname, username, password): # this was previous function definition
if len(args) == 1 and isinstance(args[0], credential):
hostname = args[0].hostname
username = args[0].username
password = args[0].password
elif len(args) == 3:
hostname = args[0]
username = args[1]
password = args[2]
else:
raise ValueError('Invalid arguments')
do_login(hostname, username, password) # this is original system login call
main.py -- main script that combines credentials and system libs
from mycredentials import sys_stg, sys_db
import mysystemlib
...
mysystemlib.system_login(sys_stg)
Please note that the legacy hostname/username/password way still works so it does not affect old scripts:
mysystemlib.system_login("srv02", "user", "pass")
This has a lot benefits:
- same credential system across all our python scripts
- files with passwords are separated (files can have more strict permissions)
- files are not stored in our git repositories (excluded via
.gitignore) so that our python scripts/libs can be shared with others without exposing credentials (everyone defines their own credentials in their local files) - if a password needs to be changed, we do it at a single place only
Videos
What are the most secure methods for managing secrets in Python applications?
How do cloud secrets managers integrate with Python applications for secure secret retrieval?
What advanced security measures should be implemented when managing secrets in Python at scale?
The recent State of Secrets Sprawl report showed that 10 million (yes million) secrets like API keys, credential pairs and security certs were leaked in public GitHub repositories in 2022 and Python was by far the largest contributor to these.
The problem stems mostly from secrets being hardcoded directly into the source code. So this leads to the question, why are so many devs hardcoding secrets? The problem is a little more complicated with git because often a secret is hardcoded and removed without the dev realizing that the secret persists in the git history. But still, this is a big issue in the Python community.
Managing secrets can be really easy thanks to helpful Pypi packages like Python Dotenv which is my favorite for its simplicity and easy ability to manage secrets for multiple different environments like Dev and Prod. I'm curious about what others are using to manage secrets and why?
I thought I'd share some recent tutorials on managing secrets for anyone who may need a refresher on the topic. Please share more resources in the comments.
Managing Secrets in Python - Video
Managing Secrets in Python - Blog
Hello, I was wondering what's the most secure way to store secrets (API Keys, passwords, tokens, etc) in python. I know in powershell for example there's a built in module for doing so. As far as I know you store your secrets in a config.py file of sorts. But still all anyone has to do is just open that file and your passwords are visible in plain text... What are the recommended options for doing this?
Thanks!