Commit 679f73c4 authored by Robin Fischer's avatar Robin Fischer

Add first working version of automatic authentication

parent 4a983eab
#!/usr/bin/python3
import sys
import os
import requests
import re
import argparse
import subprocess
import time
def log(text):
print(text, file=sys.stderr)
def shell(command, targetDir):
p = subprocess.Popen(command, stdout=subprocess.PIPE, shell=True, cwd=targetDir)
(output, err) = p.communicate()
p_status = p.wait()
print("Command exit status/return code : ", p_status)
decodedOutput = output.decode('utf-8')
print("Output: " + decodedOutput)
return decodedOutput
parser = argparse.ArgumentParser(description="Generate and await http certbot validation")
parser.add_argument("templatePath", help="The template file used to create the html pages to validate")
parser.add_argument("targetDir", help="The directory where the html pages are saved to")
args = parser.parse_args()
templatePath = args.templatePath
targetDir = args.targetDir
validation = os.environ["CERTBOT_VALIDATION"]
token = os.environ["CERTBOT_TOKEN"]
domain = os.environ["CERTBOT_DOMAIN"]
gitToken = os.environ["TOKEN"]
fileName = domain + ".html"
log("Validation " + validation)
log("Token " + token)
log("Domain " + domain)
def writeValidationFile():
with open(templatePath, "r") as file :
filedata = file.read()
# Replace the target string
filedata = filedata.replace("%TOKEN%", token)
filedata = filedata.replace("%VALIDATION%", validation)
# Write the file out again
outPath = os.path.join(targetDir, fileName)
with open(outPath, 'w') as file:
file.write(filedata)
# Push changes to origin
def gitPush():
originUrl = shell("git remote get-url origin", targetDir)
writeOriginUrl = re.sub("gitlab-ci-token:.*?@", "gitlab-ci-token:" + gitToken + "@", originUrl)
print("Replaced for path: " + writeOriginUrl)
shell("git remote add tokenOrigin " + writeOriginUrl, targetDir)
shell("git checkout master", targetDir)
shell("git add " + fileName, targetDir)
shell("git config user.email gitlab-runner@baron.com", targetDir)
shell("git config user.name baron", targetDir)
shell("git commit -m 'Add domain validation for " + domain + "'", targetDir)
shell("git push tokenOrigin", targetDir)
# Wait for webpage to update
def waitForPageChange():
doesSiteExist = False
url = "http://" + domain + "/.well-known/acme-challenge/" + token
print("url: " + url)
while not doesSiteExist:
r = requests.get(url)
responseText = r.text.strip()
print("R: " + responseText + ", V: " + validation)
if responseText == validation:
doesSiteExist = True
else:
print("Page not found yet. Waiting...")
time.sleep(30)
print("Passed validation")
writeValidationFile()
gitPush()
waitForPageChange()
#!/bin/bash
echo "Got authenticator hook ${CERTBOT_VALIDATION}" >&2
#!/bin/bash
echo "Got cleanup"
---
layout: null
permalink: "/.well-known/acme-challenge/%TOKEN%/index.html"
---
%VALIDATION%
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment