Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ns-api): gracefully handle error when checking for updates #990

Merged
merged 3 commits into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/ns-api/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ define Package/ns-api
CATEGORY:=NethSecurity
TITLE:=NethSecurity REST API
URL:=https://github.com/NethServer/nethsecurity-controller/
DEPENDS:=+python3-nethsec +python3-openssl +python3-urllib +python3-idna
DEPENDS:=+python3-nethsec +python3-openssl +python3-urllib +python3-idna +python3-requests
PKGARCH:=all
endef

Expand Down
36 changes: 23 additions & 13 deletions packages/ns-api/files/ns.update
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@ import os
import sys
import json
import time
import urllib
import subprocess
import urllib.request
import requests
from nethsec import utils
from urllib.parse import urlparse
from euci import EUci
Expand Down Expand Up @@ -89,19 +88,30 @@ def install_package_updates():
def check_system_update():
e_uci = EUci()
current_version = get_system_version()
ret = {"currentVersion": f'NethSecurity {current_version}', "lastVersion": "", "scheduledAt": get_update_schedule()}
data = {"currentVersion": f'NethSecurity {current_version}', "lastVersion": "", "scheduledAt": get_update_schedule()}
url = e_uci.get('ns-plug', 'config', 'repository_url')
Tbaile marked this conversation as resolved.
Show resolved Hide resolved
if e_uci.get('ns-plug', 'config', 'system_id', default=None) is None:
url = f"{url}/{get_distfeed_channel()}"
try:
url = e_uci.get('ns-plug', 'config', 'repository_url')
if (e_uci.get('ns-plug', 'config', 'system_id', default=None) is None):
url = f"{url}/{get_distfeed_channel()}"

p = subprocess.run(["/usr/bin/curl", "-L", "-s", f"{url}/latest_release"], check=True, capture_output=True, text=True)
version = p.stdout.rstrip()
response = requests.get(f"{url}/latest_release", headers={"Accept": "application/json"}, timeout=5)
response.raise_for_status()
version = response.text.strip()
if current_version != version:
ret["lastVersion"] = f'NethSecurity {version}'
except Exception as e:
print(e, file=sys.stderr)
return ret
data["lastVersion"] = f'NethSecurity {version}'
except requests.exceptions.ConnectionError:
return utils.generic_error("connection_error")
except requests.exceptions.RequestException as e:
match e.response.status_code:
case requests.codes.service_unavailable:
return utils.generic_error("maintenance")
case requests.codes.unauthorized:
return utils.generic_error("unauthorized")
case _:
return utils.generic_error("server_error")
except Exception:
return utils.generic_error("generic_error")

return data

def schedule_system_update(timestamp):
if timestamp < 0:
Expand Down
Loading