First commit

This commit is contained in:
giuliof 2021-09-04 12:56:09 +02:00
commit 6b073e87db
2 changed files with 46 additions and 0 deletions

43
main.py Normal file
View File

@ -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()

View File

@ -0,0 +1,3 @@
${wordpress.username}
${wordpress.password}
${unfair.password}