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