paulbsd-salt/states/_modules/pki.py
Paul Lecuq ff6f65c614
All checks were successful
continuous-integration/drone/push Build is passing
updated pki modules
2024-04-20 17:24:47 +02:00

45 lines
1.1 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):
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})
res = urlopen(req, jsondata.encode('utf-8'))
resj = json.loads(res.read())
return resj[domains[0]]["certificate"], resj[domains[0]]["privatekey"]