paulbsd-salt/states/_modules/pki.py
Paul Lecuq 7abec3faa3
All checks were successful
continuous-integration/drone/push Build is passing
updated pki modules
2024-04-20 15:59:23 +02:00

44 lines
1.0 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}")
res = urlopen(req, json.dumps(domains))
resj = json.loads(res.read())
return resj["certificate"], resj["privatekey"]