Skip to content

Commit 9af18e9

Browse files
authored
fix(ns-api): gracefully handle error when checking for updates (#990)
1 parent 2c52422 commit 9af18e9

File tree

2 files changed

+26
-14
lines changed

2 files changed

+26
-14
lines changed

packages/ns-api/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ define Package/ns-api
2121
CATEGORY:=NethSecurity
2222
TITLE:=NethSecurity REST API
2323
URL:=https://github.com/NethServer/nethsecurity-controller/
24-
DEPENDS:=+python3-nethsec +python3-openssl +python3-urllib +python3-idna
24+
DEPENDS:=+python3-nethsec +python3-openssl +python3-urllib +python3-idna +python3-requests
2525
PKGARCH:=all
2626
endef
2727

packages/ns-api/files/ns.update

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,8 @@ import os
1111
import sys
1212
import json
1313
import time
14-
import urllib
1514
import subprocess
16-
import urllib.request
15+
import requests
1716
from nethsec import utils
1817
from urllib.parse import urlparse
1918
from euci import EUci
@@ -89,19 +88,32 @@ def install_package_updates():
8988
def check_system_update():
9089
e_uci = EUci()
9190
current_version = get_system_version()
92-
ret = {"currentVersion": f'NethSecurity {current_version}', "lastVersion": "", "scheduledAt": get_update_schedule()}
91+
data = {"currentVersion": f'NethSecurity {current_version}', "lastVersion": "", "scheduledAt": get_update_schedule()}
92+
url = e_uci.get('ns-plug', 'config', 'repository_url', default=None)
93+
if url is None:
94+
return utils.generic_error("repository_url_not_set")
95+
if e_uci.get('ns-plug', 'config', 'system_id', default=None) is None:
96+
url = f"{url}/{get_distfeed_channel()}"
9397
try:
94-
url = e_uci.get('ns-plug', 'config', 'repository_url')
95-
if (e_uci.get('ns-plug', 'config', 'system_id', default=None) is None):
96-
url = f"{url}/{get_distfeed_channel()}"
97-
98-
p = subprocess.run(["/usr/bin/curl", "-L", "-s", f"{url}/latest_release"], check=True, capture_output=True, text=True)
99-
version = p.stdout.rstrip()
98+
response = requests.get(f"{url}/latest_release", headers={"Accept": "application/json"}, timeout=5)
99+
response.raise_for_status()
100+
version = response.text.strip()
100101
if current_version != version:
101-
ret["lastVersion"] = f'NethSecurity {version}'
102-
except Exception as e:
103-
print(e, file=sys.stderr)
104-
return ret
102+
data["lastVersion"] = f'NethSecurity {version}'
103+
except requests.exceptions.ConnectionError:
104+
return utils.generic_error("connection_error")
105+
except requests.exceptions.RequestException as e:
106+
match e.response.status_code:
107+
case requests.codes.service_unavailable:
108+
return utils.generic_error("maintenance")
109+
case requests.codes.unauthorized:
110+
return utils.generic_error("unauthorized")
111+
case _:
112+
return utils.generic_error("server_error")
113+
except Exception:
114+
return utils.generic_error("generic_error")
115+
116+
return data
105117

106118
def schedule_system_update(timestamp):
107119
if timestamp < 0:

0 commit comments

Comments
 (0)