Adding script to generate htpassword

This commit is contained in:
Moritz Graf 2024-08-31 09:38:47 +02:00
parent 8404457c76
commit 4fb8dba34f
1 changed files with 33 additions and 0 deletions

33
k8s/htpasswd.py Executable file
View File

@ -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 <username> <password>")
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()