paulbsd-salt/states/_modules/pki.py

45 lines
1.1 KiB
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):
2024-04-20 15:59:23 +02:00
req = Request(method="POST",
url=f"{url}/cert",
headers={"Content-Type":"application/json"})
2021-10-02 12:39:16 +02:00
authstring = base64.b64encode(f"{username}:{password}".encode()).decode()
req.add_header("Authorization", f"Basic {authstring}")
2024-04-20 17:22:34 +02:00
jsondata = json.dumps({"domains":domains})
2024-04-20 16:26:51 +02:00
res = urlopen(req, jsondata.encode('utf-8'))
2021-10-02 12:39:16 +02:00
resj = json.loads(res.read())
return resj["certificate"], resj["privatekey"]