paulbsd-salt/states/_modules/pki.py

43 lines
985 B
Python
Raw Normal View History

2021-07-11 17:37:42 +02:00
#!/usr/bin/python3
# vim:syntax=python
2021-10-02 12:39:16 +02:00
import json
import base64
from urllib.request import urlopen
from urllib.request import Request
2021-07-11 17:37:42 +02:00
2021-10-02 12:39:16 +02:00
def get_file_content(checkfile=None):
2021-07-11 17:37:42 +02:00
try:
2021-10-02 12:39:16 +02:00
with open(checkfile, "r") as f:
ret = f.read()
except OSError:
return None
2021-07-11 17:37:42 +02:00
2021-10-02 12:39:16 +02:00
return ret
2021-07-11 17:37:42 +02:00
2021-10-02 12:39:16 +02:00
def write_file_content(content=None,
filename=None):
2021-07-11 17:37:42 +02:00
try:
2021-10-02 12:39:16 +02:00
with open(filename, "w") as f:
ret = f.write(content)
except OSError:
return None
2021-07-11 17:37:42 +02:00
return ret
def get_pki_cert(url="http://pki",
username=None,
password=None,
domains=None):
2021-10-02 12:39:16 +02:00
req = Request(method="GET",
url=f"{url}/domain/{domains}")
authstring = base64.b64encode(f"{username}:{password}".encode()).decode()
req.add_header("Authorization", f"Basic {authstring}")
res = urlopen(req)
resj = json.loads(res.read())
return resj["certificate"], resj["privatekey"]