paulbsd-salt/states/_modules/pki.py

46 lines
1.2 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,
2024-04-20 19:18:49 +02:00
domains=None,
provider=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 19:18:49 +02:00
jsondata = json.dumps({"domains":domains, "provider": provider})
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())
2024-04-20 17:24:47 +02:00
return resj[domains[0]]["certificate"], resj[domains[0]]["privatekey"]