paulbsd-salt/states/_modules/pki.py
Paul Lecuq 014d7fba46
All checks were successful
continuous-integration/drone/push Build is passing
updated pki modules
2024-04-20 19:18:49 +02:00

46 lines
1.2 KiB
Python

#!/usr/bin/python3
# vim:syntax=python
import json
import base64
from urllib.request import urlopen
from urllib.request import Request
def get_file_content(checkfile=None):
try:
with open(checkfile, "r") as f:
ret = f.read()
except OSError:
return None
return ret
def write_file_content(content=None,
filename=None):
try:
with open(filename, "w") as f:
ret = f.write(content)
except OSError:
return None
return ret
def get_pki_cert(url="http://pki",
username=None,
password=None,
domains=None,
provider=None):
req = Request(method="POST",
url=f"{url}/cert",
headers={"Content-Type":"application/json"})
authstring = base64.b64encode(f"{username}:{password}".encode()).decode()
req.add_header("Authorization", f"Basic {authstring}")
jsondata = json.dumps({"domains":domains, "provider": provider})
res = urlopen(req, jsondata.encode('utf-8'))
resj = json.loads(res.read())
return resj[domains[0]]["certificate"], resj[domains[0]]["privatekey"]