diff --git a/states/_modules/dkron.py b/states/_modules/dkron.py index 7c7fdca..443a695 100644 --- a/states/_modules/dkron.py +++ b/states/_modules/dkron.py @@ -1,31 +1,28 @@ #!/usr/bin/python3 -import requests +import json +from urllib.request import urlopen, Request -def get_jobs(url="http://localhost:8898", verify=False): +def get_jobs(url="http://localhost:8898"): """get_jobs fetch jobs from dkron""" fullurl = f"{url}/v1/jobs" - ret = dict() - try: - req = requests.request("GET", fullurl, verify=verify) - except (requests.exceptions.RequestException) as exc: - raise f"Exception {exc} occured" - ret = req.json() - if req.status_code == 200: - return ret + req = Request(method="GET", url=fullurl) + res = urlopen(req) + dkron_ret = json.loads(res.read()) + if res.status == 200: + return dkron_ret return None -def set_jobs(url="http://localhost:8898", verify=False, job=None): +def set_jobs(url="http://localhost:8898", job=None): """set_jobs set jobs on dkron""" fullurl = f"{url}/v1/jobs" - ret = dict() - try: - req = requests.request("POST", fullurl, verify=verify, json=job) - except (requests.exceptions.RequestException) as exc: - raise f"Exception {exc} occured" - ret = req.json() - if req.status_code == 201: - return ret + data = json.dumps(job).encode('utf-8') + req = Request(method="POST", url=fullurl, data=data) + req.add_header('Content-Type', 'application/json') + res = urlopen(req) + dkron_ret = json.loads(res.read()) + if res.status == 201: + return dkron_ret return None diff --git a/states/_modules/pki.py b/states/_modules/pki.py index 2f40b65..acc34ec 100644 --- a/states/_modules/pki.py +++ b/states/_modules/pki.py @@ -1,31 +1,29 @@ #!/usr/bin/python3 # vim:syntax=python -import os -import requests +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): - ret = None - try: with open(filename, "w") as f: ret = f.write(content) - except Exception as err: - pass - - return - - -def get_file_content(checkfile=None): - ret = None - - try: - with open(checkfile, 'r') as f: - ret = f.read() - except FileNotFoundError as err: - pass + except OSError: + return None return ret @@ -34,16 +32,11 @@ def get_pki_cert(url="http://pki", username=None, password=None, domains=None): - ret = None + req = Request(method="GET", + url=f"{url}/domain/{domains}") + authstring = base64.b64encode(f"{username}:{password}".encode()).decode() + req.add_header("Authorization", f"Basic {authstring}") + res = urlopen(req) + resj = json.loads(res.read()) - try: - res = requests.request(method="GET", - url=f"{url}/domain/{domains}", - auth=(username, password)) - resj = res.json() - - return resj["certificate"], resj["privatekey"] - except Exception as err: - pass - - return None, None + return resj["certificate"], resj["privatekey"] diff --git a/states/_modules/syncthing.py b/states/_modules/syncthing.py index e2d45be..52fa272 100644 --- a/states/_modules/syncthing.py +++ b/states/_modules/syncthing.py @@ -1,7 +1,19 @@ #!/usr/bin/python3 +import ssl +import json import xml.etree.ElementTree as ET -import requests +from urllib.request import urlopen +from urllib.request import Request + + +def get_context(verify): + ctx = None + if not verify: + ctx = ssl.create_default_context() + ctx.check_hostname = False + ctx.verify_mode = ssl.CERT_NONE + return ctx def get_apikey(configfile="/root/.config/syncthing/config.xml"): @@ -18,16 +30,13 @@ def get_apikey(configfile="/root/.config/syncthing/config.xml"): def get_config(url, verify, apikey): fullurl = f"{url}/rest/system/config" - ret = dict() - try: - req = requests.request("get", - fullurl, - verify=verify, - headers={"X-API-Key": apikey}) - except (requests.exceptions.RequestException) as exc: - raise f"Exception {exc} occured" - ret = req.json() - if req.status_code == 200: + req = Request(method="GET", + url=fullurl) + req.add_header("X-API-Key", apikey) + res = urlopen(req, context=get_context(verify)) + + ret = json.loads(res.read()) + if res.status == 200: return ret return None @@ -35,15 +44,12 @@ def get_config(url, verify, apikey): def set_config(url, verify, apikey, config): fullurl = f"{url}/rest/system/config" - try: - req = requests.request("post", - fullurl, - verify=verify, - headers={"X-API-Key": apikey}, - json=config) - except (requests.exceptions.RequestException) as exc: - raise f"Exception {exc} occured" - if req.status_code == 200: + req = Request(method="POST", + url=fullurl, + data=json.dumps(config).encode()) + req.add_header("X-API-Key", apikey) + res = urlopen(req, context=get_context(verify)) + if res.status == 200: return True return None @@ -51,15 +57,12 @@ def set_config(url, verify, apikey, config): def insync(url, verify, apikey): fullurl = f"{url}/rest/system/config/insync" - try: - req = requests.request("get", - fullurl, - verify=verify, - headers={"X-API-Key": apikey}) - except (requests.exceptions.RequestException) as exc: - raise f"Exception {exc} occured" - ret = req.json() - if req.status_code == 200: + req = Request(method="GET", + url=fullurl) + req.add_header("X-API-Key", apikey) + res = urlopen(req, context=get_context(verify)) + ret = json.loads(res.read()) + if res.status == 200: return ret return None @@ -67,14 +70,11 @@ def insync(url, verify, apikey): def restart(url, verify, apikey): fullurl = f"{url}/rest/system/restart" - try: - req = requests.request("post", - fullurl, - verify=verify, - headers={"X-API-Key": apikey}) - except (requests.exceptions.RequestException) as exc: - raise f"Exception {exc} occured" - if req.status_code == 200: + req = Request(method="POST", + url=fullurl) + req.add_header("X-API-Key", apikey) + res = urlopen(req, context=get_context(verify)) + if res.status == 200: return {} return None diff --git a/states/_states/dkron.py b/states/_states/dkron.py index ef11852..2c254c4 100644 --- a/states/_states/dkron.py +++ b/states/_states/dkron.py @@ -11,7 +11,7 @@ def jobs(name, url="http://localhost:8080", verify=False, jobs_list=[]): 'comment': 'Config is up to date'} for job in jobs_list: - res = __salt__['dkron.set_jobs'](url, verify, job) + res = __salt__['dkron.set_jobs'](url, job) if res is not None: ret['changes'][job['name']] = res else: diff --git a/states/_states/pki.py b/states/_states/pki.py index 9f90f44..a8fb910 100644 --- a/states/_states/pki.py +++ b/states/_states/pki.py @@ -30,8 +30,8 @@ def fetched(name=None, password=password, domains=domain_concat) - if currentcert != newcert or currentkey != newkey: - if len(newcert) > 10 or len(newkey) > 10: + if all([newcert,newkey]): + if currentcert != newcert or currentkey != newkey: wcert = __salt__['pki.write_file_content'](newcert, fullcertfile) wkey = __salt__['pki.write_file_content'](newkey, keyfile) @@ -42,10 +42,10 @@ def fetched(name=None, ret["comment"] = "Updated certificates and keys" ret["result"] = all([wcert, wkey]) else: - ret["comment"] = "Error fetching in certificate / key length" - return ret + ret["comment"] = "Config is good and not changed" + ret["result"] = True else: - ret["comment"] = "Config is good and not changed" - ret["result"] = True + ret["comment"] = "Error fetching in certificate / key length" + return ret return ret