paulbsd-salt/states/_states/pki.py

58 lines
2.0 KiB
Python
Raw Normal View History

2021-07-11 17:37:42 +02:00
#!/usr/bin/python3
2021-07-18 10:02:15 +02:00
from salt.utils.stringutils import get_diff
2021-07-11 17:37:42 +02:00
def fetched(name=None,
2021-09-25 17:17:29 +02:00
url="http://pki",
username=None,
password=None,
domains=None,
2022-10-01 20:10:31 +02:00
certfile=None,
2021-09-25 17:17:29 +02:00
fullcertfile=None,
keyfile=None):
2021-07-11 17:37:42 +02:00
ret = {
'name': name,
'changes': {},
'result': False,
2021-07-18 10:02:15 +02:00
'comment': 'Config is not up to date'
2021-07-11 17:37:42 +02:00
}
currentcert = None
currentkey = None
2021-07-18 10:02:15 +02:00
domain_concat = ",".join(domains)
2021-07-11 17:37:42 +02:00
2022-10-01 20:10:31 +02:00
currentcert = __salt__['pki.get_file_content'](checkfile=certfile)
2021-07-11 17:37:42 +02:00
currentkey = __salt__['pki.get_file_content'](checkfile=keyfile)
newcert, newkey = __salt__['pki.get_pki_cert'](url=url,
username=username,
password=password,
domains=domain_concat)
2022-10-01 20:10:31 +02:00
newfullcert = f"{newcert}\n\n{newkey}"
2021-07-11 17:37:42 +02:00
2021-10-02 12:39:16 +02:00
if all([newcert,newkey]):
if currentcert != newcert or currentkey != newkey:
2022-10-01 20:10:31 +02:00
wcert = __salt__['pki.write_file_content'](newcert, certfile)
2021-09-25 17:17:29 +02:00
wkey = __salt__['pki.write_file_content'](newkey, keyfile)
2022-10-01 20:10:31 +02:00
wfullcert = __salt__['pki.write_file_content'](newfullcert, fullcertfile)
2021-09-25 17:17:29 +02:00
2022-10-01 20:10:31 +02:00
if currentcert and currentkey:
ret["changes"]["old"] = "\n".join([currentcert, currentkey])
else:
ret["changes"]["old"] = ""
2021-09-25 17:17:29 +02:00
ret["changes"]["new"] = "\n".join([newcert, newkey])
ret["changes"]["diff"] = get_diff(ret["changes"]["old"],
ret["changes"]["new"])
ret["comment"] = "Updated certificates and keys"
2022-10-01 20:10:31 +02:00
ret["result"] = all([wcert, wkey, wfullcert])
2021-09-25 17:17:29 +02:00
else:
2021-10-02 12:39:16 +02:00
ret["comment"] = "Config is good and not changed"
ret["result"] = True
2021-07-18 10:02:15 +02:00
else:
2021-10-02 12:39:16 +02:00
ret["comment"] = "Error fetching in certificate / key length"
return ret
2021-07-11 17:37:42 +02:00
return ret