From 4fb8dba34f2ecd1768a916c6087a2f5807b459b4 Mon Sep 17 00:00:00 2001 From: Moritz Graf Date: Sat, 31 Aug 2024 09:38:47 +0200 Subject: [PATCH] Adding script to generate htpassword --- k8s/htpasswd.py | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100755 k8s/htpasswd.py diff --git a/k8s/htpasswd.py b/k8s/htpasswd.py new file mode 100755 index 0000000..f1c37f0 --- /dev/null +++ b/k8s/htpasswd.py @@ -0,0 +1,33 @@ +import bcrypt +import sys + +def generate_htpasswd(username, password): + # Generate a salt + salt = bcrypt.gensalt() + + # Hash the password with the generated salt + hashed_password = bcrypt.hashpw(password.encode('utf-8'), salt) + + # Format the string for the .htpasswd + htpasswd_string = f"{username}:{hashed_password.decode('utf-8')}" + + return htpasswd_string + +def main(): + # Check if the correct number of arguments are provided + if len(sys.argv) != 3: + print("Usage: python script.py ") + sys.exit(1) + + # Get the username and password from the command-line arguments + username = sys.argv[1] + password = sys.argv[2] + + # Generate the htpasswd string + htpasswd_string = generate_htpasswd(username, password) + + # Print the htpasswd string to stdout + print(htpasswd_string) + +if __name__ == "__main__": + main()