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,
|
|
|
|
url="http://pki",
|
|
|
|
username=None,
|
|
|
|
password=None,
|
|
|
|
domains=None,
|
|
|
|
certfile=None,
|
|
|
|
keyfile=None):
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
currentcert = __salt__['pki.get_file_content'](checkfile=certfile)
|
|
|
|
currentkey = __salt__['pki.get_file_content'](checkfile=keyfile)
|
|
|
|
|
|
|
|
newcert, newkey = __salt__['pki.get_pki_cert'](url=url,
|
|
|
|
username=username,
|
|
|
|
password=password,
|
|
|
|
domains=domain_concat)
|
|
|
|
|
|
|
|
if currentcert != newcert or currentkey != newkey:
|
|
|
|
wcert = __salt__['pki.write_file_content'](newcert, certfile)
|
|
|
|
wkey = __salt__['pki.write_file_content'](newkey, keyfile)
|
|
|
|
|
2021-07-18 10:02:15 +02:00
|
|
|
ret["changes"]["old"] = "\n".join([currentcert, currentkey])
|
|
|
|
ret["changes"]["new"] = "\n".join([newcert, newkey])
|
|
|
|
ret["changes"]["diff"] = get_diff(ret["changes"]["old"],
|
|
|
|
ret["changes"]["new"])
|
2021-07-11 17:37:42 +02:00
|
|
|
ret["comment"] = "Updated certificates and keys"
|
|
|
|
ret["result"] = all([wcert, wkey])
|
2021-07-18 10:02:15 +02:00
|
|
|
else:
|
|
|
|
ret["comment"] = "Config is good and not changed"
|
|
|
|
ret["result"] = True
|
2021-07-11 17:37:42 +02:00
|
|
|
|
|
|
|
return ret
|