From 6b073e87dbc825070e74300ce27acf67c618b189 Mon Sep 17 00:00:00 2001 From: giuliof Date: Sat, 4 Sep 2021 12:56:09 +0200 Subject: [PATCH] First commit --- main.py | 43 +++++++++++++++++++++++++++++++++ yml/docker-compose.template.yml | 3 +++ 2 files changed, 46 insertions(+) create mode 100644 main.py create mode 100644 yml/docker-compose.template.yml diff --git a/main.py b/main.py new file mode 100644 index 0000000..446e422 --- /dev/null +++ b/main.py @@ -0,0 +1,43 @@ +# chiedi la password di sblocco +import os +import re +import getpass +from pykeepass import PyKeePass + +# Generator: finds all "docker-compose.template.yml" in subdirectories from rootdir +def getDockerComposeFiles(rootdir = '.'): + for subdir, dirs, files in os.walk(rootdir): + for file in files: + if file == "docker-compose.template.yml": + yield os.path.join(subdir, file) + +def main(): + # Dictionary with all services secrets + subDict = {} + + print("Unlock password database: ") + dbpass = getpass.getpass(prompt='Password: ', stream=None) + + # Populate dictionary + with PyKeePass('secrets.kdbx', password=dbpass) as kp: + for key in kp.entries: + service = key.title + + subDict[service+".username"] = key.username + subDict[service+".password"] = key.password + + + # Scan all yml files for placeholders in form of ${service.username} and ${service.password} and apply + for filename in getDockerComposeFiles('yml'): + with open(filename, 'r') as f: + try: + applied = re.sub('\$\{(.*?)\}', lambda x: subDict[x.group(1)], f.read()) + except KeyError as e: + print(f"Could not patch {filename}, key {e.args[0]} not found") + continue + filename=filename.replace(".template", "") + print("writing to " + filename) + with open(filename, 'w') as f: + f.write(applied) + +main() \ No newline at end of file diff --git a/yml/docker-compose.template.yml b/yml/docker-compose.template.yml new file mode 100644 index 0000000..7b84adf --- /dev/null +++ b/yml/docker-compose.template.yml @@ -0,0 +1,3 @@ +${wordpress.username} +${wordpress.password} +${unfair.password}