From ea0e73fbcd1f13b4c0c72c732d9eead6f9af0e81 Mon Sep 17 00:00:00 2001 From: Jonathan Weth Date: Thu, 1 Jul 2021 19:01:48 +0200 Subject: Refactor ID system --- rwa/support/sessionservice/service.py | 156 +++++++++++++++++++++------------- rwa/support/sessionservice/session.py | 27 ++++-- test_client.py | 63 ++++++++++---- 3 files changed, 165 insertions(+), 81 deletions(-) diff --git a/rwa/support/sessionservice/service.py b/rwa/support/sessionservice/service.py index 471d599..9a843ae 100755 --- a/rwa/support/sessionservice/service.py +++ b/rwa/support/sessionservice/service.py @@ -30,6 +30,7 @@ import signal import time from threading import Thread from typing import Dict, Union +from uuid import uuid4 import click import dbus @@ -43,7 +44,7 @@ from gi.repository import GLib from .config import ALLOW_ONLY_ONE_SESSION, API_PATH, SUPPORTED_API_VERSIONS from .lock import is_locked, lock, unlock from .log import logging -from .session import Session +from .session import Session, combine from .trigger import TriggerServerThread @@ -76,9 +77,14 @@ class RWASupportSessionService(dbus.service.Object): self.sessions = {} self.settings = usersettings.Settings("org.ArcticaProject.RWASupportSessionService") - self.settings.add_setting("web_app_hosts", list, ["http://127.0.0.1:8000"]) + self.settings.add_setting("web_app_hosts", dict) self.settings.load_settings() + # Ensure default value for web app hosts settings + if not self.settings.web_app_hosts: + self.settings.web_app_hosts = {} + self.settings.save_settings() + super().__init__(name, "/RWASupportSessionService") logging.info("D-Bus service has been started.") @@ -98,9 +104,15 @@ class RWASupportSessionService(dbus.service.Object): Helper function: No D-Bus API. """ - hosts = self.settings.web_app_hosts + hosts = [ + self._build_host_dict(key, value) for key, value in self.settings.web_app_hosts.items() + ] return json.dumps(hosts) + def _build_host_dict(self, host_id: str, host: dict) -> dict: + """Include the host ID in the host dictionary.""" + return host | {"id": host_id} + @dbus.service.method("org.ArcticaProject.RWASupportSessionService", out_signature="s") def get_web_app_hosts(self) -> str: """Get all registered RWA.Support.WebApp hosts. @@ -111,7 +123,7 @@ class RWASupportSessionService(dbus.service.Object): :: - ["https://example.org", "http://127.0.0.1:8000"] + [{"id": , "https://example.org"}, {"id": , "http://127.0.0.1:8000"}] """ logging.info("D-Bus method call: %s()", "get_web_app_hosts") @@ -174,13 +186,13 @@ class RWASupportSessionService(dbus.service.Object): """Add a RWA.Support.WebApp host. :param host: Exact hostname of the RWA.Support.WebApp host (D-Bus string) - :return: All registered hosts as JSON array (D-Bus string) + :return: The registered host as JSOn object (D-Bus string) **Structure of returned JSON (success):** :: - ["https://example.org", "http://127.0.0.1:8000"] + {"id": , "http://127.0.0.1:8000"} **Structure of returned JSON (error):** @@ -215,56 +227,67 @@ class RWASupportSessionService(dbus.service.Object): logging.debug('Did not add "%s" to "web_app_hosts" in user_settings', host) return json.dumps(res) - self.settings.web_app_hosts.append(host) + host_id = str(uuid4()) + host_object = {"url": host} + + self.settings.web_app_hosts[host_id] = host_object self.settings.save_settings() + logging.debug('Added "%s" to "web_app_hosts" in user_settings', host) - return self._get_web_app_hosts() + return json.dumps(self._build_host_dict(host_id, host_object)) @dbus.service.method( "org.ArcticaProject.RWASupportSessionService", in_signature="i", out_signature="s" ) - def remove_web_app_host(self, host_idx: int) -> str: + def remove_web_app_host(self, host_id: str) -> str: """Remove a RWA.Support.WebApp host. - :param idx: Index of web app host (D-Bus integer) + :param host_id: ID of web app host (D-Bus string) :return: All registered hosts as JSON array (D-Bus string) **Structure of returned JSON:** :: - ["https://example.org", "http://127.0.0.1:8000"] + [{"id": , "https://example.org"}, {"id": , "http://127.0.0.1:8000"}] """ - logging.info("D-Bus method call: %s(%d)", "remove_web_app_host", host_idx) + logging.info("D-Bus method call: %s(%s)", "remove_web_app_host", host_id) - if host_idx >= 0 and host_idx < len(self.settings.web_app_hosts): - host = self.settings.web_app_hosts[host_idx] - del self.settings.web_app_hosts[host_idx] + if host_id in self.settings.web_app_hosts: + host_object = self.settings.web_app_hosts[host_id] + del self.settings.web_app_hosts[host_id] self.settings.save_settings() - logging.debug('Removed web_app_hosts[%d]="%s" in user settings', host_idx, host) + logging.debug('Removed web_app_hosts[%s]="%s" in user settings', host_id, host_object) else: logging.warning("Given host index is not valid!") logging.debug( - "Did not remove web_app_hosts[%d] (not existent!) in " "user settings", host_idx + "Did not remove web_app_hosts[%s] (not existent!) in " "user settings", host_id ) + return json.dumps({"status": "error", "type": "host_not_found"}) return self._get_web_app_hosts() @dbus.service.method( "org.ArcticaProject.RWASupportSessionService", in_signature="i", out_signature="s" ) - def start(self, host_idx: int) -> str: + def start(self, host_id: str) -> str: """Start a new remote session and register it in RWA.Support.WebApp. - :param host_idx: Index of web app host (D-Bus integer) + :param host_id: ID of web app host (D-Bus string) :return: Result as JSON (D-Bus string) **Structure of returned JSON (success):** :: - {"status": "success", "id": , "url": "", "pin": } + { + "status": "success", + "host_id": "", + "session_id": , + "url": "", + "pin": + } **Structure of returned JSON (error):** @@ -280,7 +303,7 @@ class RWASupportSessionService(dbus.service.Object): * ``permission_denied`` * ``unsupported_server`` """ - logging.info("D-Bus method call: %s(%d)", "start", host_idx) + logging.info("D-Bus method call: %s(%s)", "start", host_id) if ALLOW_ONLY_ONE_SESSION and len(self.sessions.values()) > 0: logging.warning( @@ -293,25 +316,26 @@ class RWASupportSessionService(dbus.service.Object): return response try: - host = self.settings.web_app_hosts[host_idx] - logging.debug('web_app_hosts[%d] is the following host: "%s"', host_idx, host) + host_object = self.settings.web_app_hosts[host_id] + host_object = self._build_host_dict(host_id, host_object) + logging.debug('web_app_hosts[%s] is the following host: "%s"', host_id, host_object) except IndexError: - logging.error("web_app_hosts[%d] does not exist!", host_idx) + logging.error("web_app_hosts[%s] does not exist!", host_id) response = json.dumps({"status": "error", "type": "host_not_found"}) logging.debug("The response to the D-Bus caller: '%s'", response) return response # Check host by doing a handshake - res = self._do_api_handshake(host) + res = self._do_api_handshake(host_object["url"]) if res["status"] == "error": return json.dumps(res) # Start session try: - session = Session(host, self.trigger_service.port, self.mockup_mode) + session = Session(host_object, self.trigger_service.port, self.mockup_mode) # Add session to sessions list - self.sessions[session.pid] = session + self.sessions[session.combined_id] = session # Start session update service self._ensure_update_service() @@ -335,23 +359,25 @@ class RWASupportSessionService(dbus.service.Object): return response @dbus.service.method( - "org.ArcticaProject.RWASupportSessionService", in_signature="i", out_signature="s" + "org.ArcticaProject.RWASupportSessionService", in_signature="si", out_signature="s" ) - def status(self, pid: int) -> str: + def status(self, host_id: str, session_id: int) -> str: """Return the status of a session. .. note:: This uses the last status version got by the update service in the background. - :param pid: (Process) ID of session (D-Bus integer) + :param host_id: Host ID (D-Bus string) + :param session_id: Session ID (D-Bus integer) + :return: Session status as JSON (D-Bus string) **Structure of returned JSON:** :: - {"id": , "status": } + {"host_id": "", "session_id": , "status": } **Possible status options:** @@ -362,56 +388,65 @@ class RWASupportSessionService(dbus.service.Object): ``dead`` There was a problem, so that the session is dead. ============ ====================== """ - logging.info("D-Bus method call: %s(%d)", "status", pid) - response = self._get_status(pid) + logging.info("D-Bus method call: %s(%s, %d)", "status", host_id, session_id) + response = self._get_status(host_id, session_id) logging.debug("The response to the D-Bus caller: '%s'", response) return response @dbus.service.method( - "org.ArcticaProject.RWASupportSessionService", in_signature="i", out_signature="s" + "org.ArcticaProject.RWASupportSessionService", in_signature="si", out_signature="s" ) - def refresh_status(self, pid: int) -> str: + def refresh_status(self, host_id: str, session_id: int) -> str: """Update status from WebApp before returning it here like :meth:`status`.""" - logging.info("D-Bus method call: %s(%d)", "refresh_status", pid) + logging.info("D-Bus method call: %s(%s, %d)", "refresh_status", host_id, session_id) - self._update_session(pid) - response = self._get_status(pid) + self._update_session(host_id, session_id) + response = self._get_status(host_id, session_id) logging.debug("The response to the D-Bus caller: '%s'", response) return response @dbus.service.method( - "org.ArcticaProject.RWASupportSessionService", in_signature="i", out_signature="s" + "org.ArcticaProject.RWASupportSessionService", in_signature="si", out_signature="s" ) - def stop(self, pid: int) -> str: + def stop(self, host_id: str, session_id: int) -> str: """Stop a remote session. - :param pid: (Process) ID of session (D-Bus integer) + :param host_id: Host ID (D-Bus string) + :param session_id: Session ID (D-Bus integer) :return: Session status as JSON (D-Bus string) **Structure of returned JSON:** :: - {"id": , "status": "stopped"} + {"host_id": "", "session_id": , "status": "stopped"} """ - logging.info("D-Bus method call: %s(%d)", "stop", pid) + logging.info("D-Bus method call: %s(%s, %d)", "stop", host_id, session_id) + combined_id = combine(host_id, session_id) try: - session = self.sessions[pid] + session = self.sessions[combined_id] except KeyError: - response = json.dumps({"pid": pid, "status": "stopped"}, sort_keys=True) + response = json.dumps( + {"host_id": host_id, "session_id": session_id, "status": "stopped"}, sort_keys=True + ) logging.debug("The response to the D-Bus caller: '%s'", response) return response session.stop() - response = json.dumps({"id": pid, "status": "stopped"}, sort_keys=True) + response = json.dumps( + {"host_id": host_id, "session_id": session_id, "status": "stopped"}, sort_keys=True + ) logging.debug("The response to the D-Bus caller: '%s'", response) return response - def _get_status(self, pid: int) -> str: + def _get_status(self, host_id: str, session_id: int) -> str: + combined_id = combine(host_id, session_id) try: - session = self.sessions[pid] + session = self.sessions[combined_id] except KeyError: - return json.dumps({"id": pid, "status": "dead"}, sort_keys=True) + return json.dumps( + {"host_id": host_id, "session_id": session_id, "status": "dead"}, sort_keys=True + ) return json.dumps(session.status) def _ensure_update_service(self): @@ -420,12 +455,13 @@ class RWASupportSessionService(dbus.service.Object): self.update_thread = Thread(target=self._update_sessions) self.update_thread.start() - def _update_session(self, pid: int): + def _update_session(self, host_id: str, session_id: int): """Update the status of a session.""" + combined_id = combine(host_id, session_id) try: - session = self.sessions[pid] + session = self.sessions[combined_id] except KeyError: - logging.info(f"Update status for session #{pid} …") + logging.info(f"Update status for session #{session_id} on host {host_id} …") logging.warning(" Session is dead.") return @@ -434,23 +470,23 @@ class RWASupportSessionService(dbus.service.Object): if running: pass elif session.status_text == "stopped" and session.pid in self.sessions: - logging.info(f"Update status for session #{pid} …") + logging.info(f"Update status for session #{session_id} on host {host_id} …") logging.warning(" Session is dead.") - del self.sessions[session.pid] + del self.sessions[combined_id] else: - logging.info(f"Update status for session #{pid} …") + logging.info(f"Update status for session #{session_id} on host {host_id} …") logging.warning(" VNC was stopped, so session is dead.") session.stop() - del self.sessions[session.pid] + del self.sessions[combined_id] def _update_sessions(self): """Go through all running sessions and update their status using ``_update_session``.""" logging.info("Started update service for sessions.") while len(self.sessions.values()) > 0: for session in list(self.sessions.values()): - self._update_session(session.pid) + self._update_session(session.host_id, session.session_id) time.sleep(2) @@ -466,7 +502,9 @@ class RWASupportSessionService(dbus.service.Object): for session in self.sessions.values(): if session.session_id == session_id: r = session.trigger(data, method) - logging.info(f"Session #{session.pid} matches the ID: {r}") + logging.info( + f"Session #{session.session_id} on host {session.host_id} matches the ID: {r}" + ) return r logging.warning(" No matching session found for this ID.") @@ -477,7 +515,7 @@ class RWASupportSessionService(dbus.service.Object): logging.info("Stop all sessions.") for session in list(self.sessions.values()): session.stop() - del self.sessions[session.pid] + del self.sessions[session.combined_id] def _stop_daemon(self): """Stop all sessions and this daemon.""" diff --git a/rwa/support/sessionservice/session.py b/rwa/support/sessionservice/session.py index aa97086..52f6471 100644 --- a/rwa/support/sessionservice/session.py +++ b/rwa/support/sessionservice/session.py @@ -51,6 +51,10 @@ def get_desktop_dir(): return output.strip().replace("\n", "") +def combine(host_id: str, session_id: int): + return f"{host_id}-{session_id}" + + class Session: #: Session is running STATUS_RUNNING = "running" @@ -58,14 +62,16 @@ class Session: #: Remote has joined the session STATUS_JOINED = "active" - def __init__(self, host: str, trigger_port: int, mockup_session: bool = False): - self.host = host - self.BASE_URL = self.host + API_PATH + def __init__(self, host_object: dict, trigger_port: int, mockup_session: bool = False): + self.host_object = host_object + self.host_url = self.host_object["url"] + self.host_id = self.host_object["id"] + self.BASE_URL = self.host_url + API_PATH self.REGISTER_URL = self.BASE_URL + "register/" self.STOP_URL = self.BASE_URL + "stop/" self.STATUS_URL = self.BASE_URL + "status/" self.MARK_JOB_AS_DONE_URL = self.BASE_URL + "jobs/mark_as_done/" - logging.info(f"Load API config: {self.host}") + logging.info(f"Load API config: {self.host_url}") self.trigger_token = secrets.token_urlsafe(20) self.trigger_port = trigger_port @@ -78,6 +84,10 @@ class Session: self._register_session() self.status_text = self.STATUS_RUNNING + @property + def combined_id(self): + return combine(self.host_id, self.session_id) + @property def pid(self) -> int: return self.vnc_pid @@ -296,8 +306,13 @@ class Session: @property def client_meta(self) -> Dict[str, Union[str, int]]: - return {"id": self.pid, "session_id": self.session_id, "url": self.web_url, "pin": self.pin} + return { + "host_id": self.host_id, + "session_id": self.session_id, + "url": self.web_url, + "pin": self.pin, + } @property def status(self) -> Dict[str, Union[str, int]]: - return {"id": self.pid, "status": self.status_text} + return {"host_id": self.host_id, "session_id": self.session_id, "status": self.status_text} diff --git a/test_client.py b/test_client.py index 72538c1..4a0f83a 100755 --- a/test_client.py +++ b/test_client.py @@ -43,38 +43,69 @@ def cli(): @cli.command() -@click.argument("host", type=int) -def start(host: int): +def get_web_app_hosts(): """Start a session on the RWA.Support.WebApp host with index HOST.""" - click.echo(f"Sending D-Bus request 'start': {host}") - response = req.start(host) + click.echo("Sending D-Bus request 'get_web_app_hosts'") + response = req.get_web_app_hosts() click.echo(f"Your response is: {response}") @cli.command() -@click.argument("pid", type=int) -def stop(pid: int): +@click.argument("host", type=str) +def add_web_app_host(host: str): + """Start a session on the RWA.Support.WebApp host with index HOST.""" + click.echo(f"Sending D-Bus request 'add_web_app_host': {host}") + response = req.add_web_app_host(host) + click.echo(f"Your response is: {response}") + + +@cli.command() +@click.argument("host", type=str) +def remove_web_app_host(host: str): + """Start a session on the RWA.Support.WebApp host with index HOST.""" + click.echo(f"Sending D-Bus request 'remove_web_app_host': {host}") + response = req.remove_web_app_host(host) + click.echo(f"Your response is: {response}") + + +@cli.command() +@click.argument("host_id", type=str) +def start(host_id: str): + """Start a session on the RWA.Support.WebApp host with index HOST.""" + click.echo(f"Sending D-Bus request 'start': {host_id}") + response = req.start(host_id) + click.echo(f"Your response is: {response}") + + +@cli.command() +@click.argument("host_id", type=str) +@click.argument("session_id", type=int) +def stop(host_id: str, session_id: int): """Stop the session with the pid PID.""" - click.echo(f"Sending D-Bus request 'stop' with PID {pid}") - response = req.stop(pid) + click.echo(f"Sending D-Bus request 'stop' with host ID {host_id} and session ID {session_id}") + response = req.stop(host_id, session_id) click.echo(f"Your response is: {response}") @cli.command() -@click.argument("pid", type=int) -def status(pid: int): +@click.argument("host_id", type=str) +@click.argument("session_id", type=int) +def status(host_id: str, session_id: int): """Get the status of the session with the pid PID.""" - click.echo(f"Sending D-Bus request 'status' with PID {pid}") - response = req.status(pid) + click.echo(f"Sending D-Bus request 'status' with host ID {host_id} and session ID {session_id}") + response = req.status(host_id, session_id) click.echo(f"Your response is: {response}") @cli.command() -@click.argument("pid", type=int) -def refresh_status(pid: int): +@click.argument("host_id", type=str) +@click.argument("session_id", type=int) +def refresh_status(host_id: str, session_id: int): """Refresh and get the status of the session with the pid PID.""" - click.echo(f"Sending D-Bus request 'refresh_status' with PID {pid}") - response = req.refresh_status(pid) + click.echo( + f"Sending D-Bus request 'refresh_status' with host ID {host_id} and session ID {session_id}" + ) + response = req.refresh_status(host_id, session_id) click.echo(f"Your response is: {response}") -- cgit v1.2.3 From b171ad5b512d0b17adc43ddde1a3e6fc976ff493 Mon Sep 17 00:00:00 2001 From: Daniel Teichmann Date: Thu, 1 Jul 2021 20:30:17 +0200 Subject: Update poetry.lock --- poetry.lock | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/poetry.lock b/poetry.lock index d2145f7..100aab3 100644 --- a/poetry.lock +++ b/poetry.lock @@ -60,7 +60,7 @@ python-versions = "*" [[package]] name = "asgiref" -version = "3.4.0" +version = "3.4.1" description = "ASGI specs, helper code, and adapters" category = "dev" optional = false @@ -236,7 +236,7 @@ python-versions = "*" [[package]] name = "django" -version = "3.2.4" +version = "3.2.5" description = "A high-level Python Web framework that encourages rapid development and clean, pragmatic design." category = "dev" optional = false @@ -1292,8 +1292,8 @@ argparse = [ {file = "argparse-1.4.0.tar.gz", hash = "sha256:62b089a55be1d8949cd2bc7e0df0bddb9e028faefc8c32038cc84862aefdd6e4"}, ] asgiref = [ - {file = "asgiref-3.4.0-py3-none-any.whl", hash = "sha256:d36fa91dd90e3aa3c81a6bd426ccc8fb20bd3d22b0cf14a12800289e9c3e2563"}, - {file = "asgiref-3.4.0.tar.gz", hash = "sha256:05914d0fa65a21711e732adc6572edad6c8da5f1435c3f0c060689ced5e85195"}, + {file = "asgiref-3.4.1-py3-none-any.whl", hash = "sha256:ffc141aa908e6f175673e7b1b3b7af4fdb0ecb738fc5c8b88f69f055c2415214"}, + {file = "asgiref-3.4.1.tar.gz", hash = "sha256:4ef1ab46b484e3c706329cedeff284a5d40824200638503f5768edb6de7d58e9"}, ] asn1crypto = [ {file = "asn1crypto-1.4.0-py2.py3-none-any.whl", hash = "sha256:4bcdf33c861c7d40bdcd74d8e4dd7661aac320fcdf40b9a3f95b4ee12fde2fa8"}, @@ -1405,8 +1405,8 @@ dj-database-url = [ {file = "dj_database_url-0.5.0-py2.py3-none-any.whl", hash = "sha256:851785365761ebe4994a921b433062309eb882fedd318e1b0fcecc607ed02da9"}, ] django = [ - {file = "Django-3.2.4-py3-none-any.whl", hash = "sha256:ea735cbbbb3b2fba6d4da4784a0043d84c67c92f1fdf15ad6db69900e792c10f"}, - {file = "Django-3.2.4.tar.gz", hash = "sha256:66c9d8db8cc6fe938a28b7887c1596e42d522e27618562517cc8929eb7e7f296"}, + {file = "Django-3.2.5-py3-none-any.whl", hash = "sha256:c58b5f19c5ae0afe6d75cbdd7df561e6eb929339985dbbda2565e1cabb19a62e"}, + {file = "Django-3.2.5.tar.gz", hash = "sha256:3da05fea54fdec2315b54a563d5b59f3b4e2b1e69c3a5841dda35019c01855cd"}, ] django-stubs = [ {file = "django-stubs-1.8.0.tar.gz", hash = "sha256:717967d7fee0a6af0746724a0be80d72831a982a40fa8f245a6a46f4cafd157b"}, -- cgit v1.2.3 From 4820353bed7a650c7a1c6ee0709484682232eddb Mon Sep 17 00:00:00 2001 From: Daniel Teichmann Date: Thu, 1 Jul 2021 20:31:22 +0200 Subject: Alter logging statements --- rwa/support/sessionservice/service.py | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/rwa/support/sessionservice/service.py b/rwa/support/sessionservice/service.py index ecc465c..cdf2a1d 100755 --- a/rwa/support/sessionservice/service.py +++ b/rwa/support/sessionservice/service.py @@ -161,22 +161,22 @@ class RWASupportSessionService(dbus.service.Object): r = requests.post(url) except requests.exceptions.ConnectionError: - logging.warning(" resulted in a connection error.") + logging.warning("Handshake resulted in a connection error.") return {"status": "error", "type": "connection"} if not r.ok: - logging.warning(" resulted in a connection error.") + logging.warning("Handshake resulted in a connection error.") return {"status": "error", "type": "connection"} if not r.json()["allowed"]: - logging.warning(" was not permitted.") + logging.warning("Handshake was not permitted.") return {"status": "error", "type": "permission_denied"} if r.json().get("api_version") not in SUPPORTED_API_VERSIONS: - logging.warning(" resulted in a incompatible API version.") + logging.warning("Handshake resulted in a incompatible API version.") return {"status": "error", "type": "unsupported_server"} - logging.info(" was successful.") + logging.info("Handshake was successful.") return {"status": "success", "type": "valid_host"} @@ -216,17 +216,26 @@ class RWASupportSessionService(dbus.service.Object): if not self._is_url(host): logging.warning("Given URL is not valid!") logging.debug('Did not add "%s" to "web_app_hosts" in user_settings', host) - return json.dumps({"status": "error", "type": "invalid_url"}) + + response = json.dumps({"status": "error", "type": "invalid_url"}) + logging.debug('The response to D-Bus caller: "%s"', response) + return response if host in self.settings.web_app_hosts: logging.warning("Given URL is already present!") logging.debug('Did not add "%s" to "web_app_hosts" in user_settings', host) - return json.dumps({"status": "error", "type": "duplicate"}) + + response = json.dumps({"status": "error", "type": "duplicate"}) + logging.debug('The response to D-Bus caller: "%s"', response) + return response res = self._do_api_handshake(host) if res["status"] == "error": logging.debug('Did not add "%s" to "web_app_hosts" in user_settings', host) - return json.dumps(res) + + response = json.dumps(res) + logging.debug('The response to D-Bus caller: "%s"', response) + return response host_id = str(uuid4()) host_object = {"url": host} @@ -234,7 +243,7 @@ class RWASupportSessionService(dbus.service.Object): self.settings.web_app_hosts[host_id] = host_object self.settings.save_settings() - logging.debug('Added "%s" to "web_app_hosts" in user_settings', host) + logging.info('Added "%s" to "web_app_hosts" in user_settings', host) response = json.dumps(self._build_host_dict(host_id, host_object)) logging.debug('The response to D-Bus caller: "%s"', response) @@ -261,11 +270,12 @@ class RWASupportSessionService(dbus.service.Object): host_object = self.settings.web_app_hosts[host_id] del self.settings.web_app_hosts[host_id] self.settings.save_settings() - logging.debug('Removed web_app_hosts[%s]="%s" in user settings', host_id, host_object) + logging.info('Removed web_app_hosts[%s]="%s" in user settings', host_id, host_object) else: logging.warning("Given host index is not valid!") logging.debug( - "Did not remove web_app_hosts[%s] (not existent!) in " "user settings", host_id + "Did not remove web_app_hosts[%s]" + "(not existent!) in " "user settings", host_id ) return json.dumps({"status": "error", "type": "host_not_found"}) -- cgit v1.2.3 From c9b8dab7aaf902682e268c079531ddbe20aad5b5 Mon Sep 17 00:00:00 2001 From: Daniel Teichmann Date: Thu, 1 Jul 2021 20:38:39 +0200 Subject: Fix bug when python version not ^3.9 --- rwa/support/sessionservice/service.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rwa/support/sessionservice/service.py b/rwa/support/sessionservice/service.py index cdf2a1d..55ec593 100755 --- a/rwa/support/sessionservice/service.py +++ b/rwa/support/sessionservice/service.py @@ -111,7 +111,7 @@ class RWASupportSessionService(dbus.service.Object): def _build_host_dict(self, host_id: str, host: dict) -> dict: """Include the host ID in the host dictionary.""" - return host | {"id": host_id} + return host.update({"id": host_id}) @dbus.service.method("org.ArcticaProject.RWASupportSessionService", out_signature="s") def get_web_app_hosts(self) -> str: -- cgit v1.2.3 From 88251a0f8c1501ed2211fdc39ee11bc839130f8c Mon Sep 17 00:00:00 2001 From: Daniel Teichmann Date: Thu, 1 Jul 2021 20:38:53 +0200 Subject: Fix KeyError not handled --- rwa/support/sessionservice/service.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rwa/support/sessionservice/service.py b/rwa/support/sessionservice/service.py index 55ec593..588e531 100755 --- a/rwa/support/sessionservice/service.py +++ b/rwa/support/sessionservice/service.py @@ -334,7 +334,7 @@ class RWASupportSessionService(dbus.service.Object): host_object = self.settings.web_app_hosts[host_id] host_object = self._build_host_dict(host_id, host_object) logging.debug('web_app_hosts[%s] is the following host: "%s"', host_id, host_object) - except IndexError: + except (KeyError, IndexError): logging.error("web_app_hosts[%s] does not exist!", host_id) response = json.dumps({"status": "error", "type": "host_not_found"}) logging.info("The response to the D-Bus caller: '%s'", response) -- cgit v1.2.3 From 9445811741b4e74348a822399d881f7e6d93275d Mon Sep 17 00:00:00 2001 From: Daniel Teichmann Date: Thu, 1 Jul 2021 21:25:15 +0200 Subject: Rename: host_id->host_uuid; Rename: host->host_url; Add debug logging statements --- rwa/support/sessionservice/service.py | 141 +++++++++++++++++----------------- rwa/support/sessionservice/session.py | 12 +-- 2 files changed, 78 insertions(+), 75 deletions(-) diff --git a/rwa/support/sessionservice/service.py b/rwa/support/sessionservice/service.py index 588e531..7bb45b2 100755 --- a/rwa/support/sessionservice/service.py +++ b/rwa/support/sessionservice/service.py @@ -104,14 +104,17 @@ class RWASupportSessionService(dbus.service.Object): Helper function: No D-Bus API. """ + + logging.debug("Raw web_app_hosts: %s", self.settings.web_app_hosts.items()) hosts = [ self._build_host_dict(key, value) for key, value in self.settings.web_app_hosts.items() ] return json.dumps(hosts) - def _build_host_dict(self, host_id: str, host: dict) -> dict: + def _build_host_dict(self, host_uuid: str, host: dict) -> dict: """Include the host ID in the host dictionary.""" - return host.update({"id": host_id}) + host.update({'uuid': host_uuid}) + return host @dbus.service.method("org.ArcticaProject.RWASupportSessionService", out_signature="s") def get_web_app_hosts(self) -> str: @@ -123,12 +126,12 @@ class RWASupportSessionService(dbus.service.Object): :: - [{"id": , "https://example.org"}, {"id": , "http://127.0.0.1:8000"}] + [{"id": , "https://example.org"}, {"id": , "http://127.0.0.1:8000"}] """ logging.info("D-Bus method call: %s()", "get_web_app_hosts") response = self._get_web_app_hosts() - logging.debug('The response to D-Bus caller: "%s"', response) + logging.info('The response to D-Bus caller: "%s"', response) return response def _do_api_handshake(self, host: str) -> Dict[str, str]: @@ -183,17 +186,17 @@ class RWASupportSessionService(dbus.service.Object): @dbus.service.method( "org.ArcticaProject.RWASupportSessionService", in_signature="s", out_signature="s" ) - def add_web_app_host(self, host: str) -> str: + def add_web_app_host(self, host_url: str) -> str: """Add a RWA.Support.WebApp host. - :param host: Exact hostname of the RWA.Support.WebApp host (D-Bus string) - :return: The registered host as JSOn object (D-Bus string) + :param host_url: Exact hostname of the RWA.Support.WebApp host (D-Bus string) + :return: The registered host as JSON object (D-Bus string) **Structure of returned JSON (success):** :: - {"id": , "http://127.0.0.1:8000"} + {"id": , "http://127.0.0.1:8000"} **Structure of returned JSON (error):** @@ -209,87 +212,87 @@ class RWASupportSessionService(dbus.service.Object): * ``invalid_url`` * ``duplicate`` """ - host = str(host).rstrip("/") + host_url = str(host_url).rstrip("/") - logging.info('D-Bus method call: %s("%s")', "add_web_app_host", host) + logging.info('D-Bus method call: %s("%s")', "add_web_app_host", host_url) - if not self._is_url(host): + if not self._is_url(host_url): logging.warning("Given URL is not valid!") - logging.debug('Did not add "%s" to "web_app_hosts" in user_settings', host) + logging.debug('Did not add "%s" to "web_app_hosts" in user_settings', host_url) response = json.dumps({"status": "error", "type": "invalid_url"}) - logging.debug('The response to D-Bus caller: "%s"', response) + logging.info('The response to D-Bus caller: "%s"', response) return response - if host in self.settings.web_app_hosts: + if host_url in self.settings.web_app_hosts: logging.warning("Given URL is already present!") - logging.debug('Did not add "%s" to "web_app_hosts" in user_settings', host) + logging.debug('Did not add "%s" to "web_app_hosts" in user_settings', host_url) response = json.dumps({"status": "error", "type": "duplicate"}) - logging.debug('The response to D-Bus caller: "%s"', response) + logging.info('The response to D-Bus caller: "%s"', response) return response - res = self._do_api_handshake(host) + res = self._do_api_handshake(host_url) if res["status"] == "error": - logging.debug('Did not add "%s" to "web_app_hosts" in user_settings', host) + logging.debug('Did not add "%s" to "web_app_hosts" in user_settings', host_url) response = json.dumps(res) - logging.debug('The response to D-Bus caller: "%s"', response) + logging.info('The response to D-Bus caller: "%s"', response) return response - host_id = str(uuid4()) - host_object = {"url": host} + host_uuid = str(uuid4()) + host_object = {"url": host_url} - self.settings.web_app_hosts[host_id] = host_object + self.settings.web_app_hosts[host_uuid] = host_object self.settings.save_settings() - logging.info('Added "%s" to "web_app_hosts" in user_settings', host) + logging.info('Added "%s" to "web_app_hosts" in user_settings', host_url) - response = json.dumps(self._build_host_dict(host_id, host_object)) - logging.debug('The response to D-Bus caller: "%s"', response) + response = json.dumps(self._build_host_dict(host_uuid, host_object)) + logging.info('The response to D-Bus caller: "%s"', response) return response @dbus.service.method( "org.ArcticaProject.RWASupportSessionService", in_signature="i", out_signature="s" ) - def remove_web_app_host(self, host_id: str) -> str: + def remove_web_app_host(self, host_uuid: str) -> str: """Remove a RWA.Support.WebApp host. - :param host_id: ID of web app host (D-Bus string) + :param host_uuid: ID of web app host (D-Bus string) :return: All registered hosts as JSON array (D-Bus string) **Structure of returned JSON:** :: - [{"id": , "https://example.org"}, {"id": , "http://127.0.0.1:8000"}] + [{"id": , "https://example.org"}, {"id": , "http://127.0.0.1:8000"}] """ - logging.info("D-Bus method call: %s(%s)", "remove_web_app_host", host_id) + logging.info("D-Bus method call: %s(%s)", "remove_web_app_host", host_uuid) - if host_id in self.settings.web_app_hosts: - host_object = self.settings.web_app_hosts[host_id] - del self.settings.web_app_hosts[host_id] + if host_uuid in self.settings.web_app_hosts: + host_object = self.settings.web_app_hosts[host_uuid] + del self.settings.web_app_hosts[host_uuid] self.settings.save_settings() - logging.info('Removed web_app_hosts[%s]="%s" in user settings', host_id, host_object) + logging.info('Removed web_app_hosts[%s]="%s" in user settings', host_uuid, host_object) else: logging.warning("Given host index is not valid!") logging.debug( "Did not remove web_app_hosts[%s]" - "(not existent!) in " "user settings", host_id + "(not existent!) in " "user settings", host_uuid ) return json.dumps({"status": "error", "type": "host_not_found"}) response = self._get_web_app_hosts() - logging.debug('The response to D-Bus caller: "%s"', response) + logging.info('The response to D-Bus caller: "%s"', response) return response @dbus.service.method( "org.ArcticaProject.RWASupportSessionService", in_signature="i", out_signature="s" ) - def start(self, host_id: str) -> str: + def start(self, host_uuid: str) -> str: """Start a new remote session and register it in RWA.Support.WebApp. - :param host_id: ID of web app host (D-Bus string) + :param host_uuid: ID of web app host (D-Bus string) :return: Result as JSON (D-Bus string) **Structure of returned JSON (success):** @@ -298,7 +301,7 @@ class RWASupportSessionService(dbus.service.Object): { "status": "success", - "host_id": "", + "host_uuid": "", "session_id": , "url": "", "pin": @@ -318,7 +321,7 @@ class RWASupportSessionService(dbus.service.Object): * ``permission_denied`` * ``unsupported_server`` """ - logging.info("D-Bus method call: %s(%s)", "start", host_id) + logging.info("D-Bus method call: %s(%s)", "start", host_uuid) if ALLOW_ONLY_ONE_SESSION and len(self.sessions.values()) > 0: logging.warning( @@ -331,11 +334,11 @@ class RWASupportSessionService(dbus.service.Object): return response try: - host_object = self.settings.web_app_hosts[host_id] - host_object = self._build_host_dict(host_id, host_object) - logging.debug('web_app_hosts[%s] is the following host: "%s"', host_id, host_object) + host_object = self.settings.web_app_hosts[host_uuid] + host_object = self._build_host_dict(host_uuid, host_object) + logging.debug('web_app_hosts[%s] is the following host: "%s"', host_uuid, host_object) except (KeyError, IndexError): - logging.error("web_app_hosts[%s] does not exist!", host_id) + logging.error("web_app_hosts[%s] does not exist!", host_uuid) response = json.dumps({"status": "error", "type": "host_not_found"}) logging.info("The response to the D-Bus caller: '%s'", response) return response @@ -376,14 +379,14 @@ class RWASupportSessionService(dbus.service.Object): @dbus.service.method( "org.ArcticaProject.RWASupportSessionService", in_signature="si", out_signature="s" ) - def status(self, host_id: str, session_id: int) -> str: + def status(self, host_uuid: str, session_id: int) -> str: """Return the status of a session. .. note:: This uses the last status version got by the update service in the background. - :param host_id: Host ID (D-Bus string) + :param host_uuid: Host ID (D-Bus string) :param session_id: Session ID (D-Bus integer) :return: Session status as JSON (D-Bus string) @@ -392,7 +395,7 @@ class RWASupportSessionService(dbus.service.Object): :: - {"host_id": "", "session_id": , "status": } + {"host_uuid": "", "session_id": , "status": } **Possible status options:** @@ -403,30 +406,30 @@ class RWASupportSessionService(dbus.service.Object): ``dead`` There was a problem, so that the session is dead. ============ ====================== """ - logging.info("D-Bus method call: %s(%s, %d)", "status", host_id, session_id) - response = self._get_status(host_id, session_id) + logging.info("D-Bus method call: %s(%s, %d)", "status", host_uuid, session_id) + response = self._get_status(host_uuid, session_id) logging.info("The response to the D-Bus caller: '%s'", response) return response @dbus.service.method( "org.ArcticaProject.RWASupportSessionService", in_signature="si", out_signature="s" ) - def refresh_status(self, host_id: str, session_id: int) -> str: + def refresh_status(self, host_uuid: str, session_id: int) -> str: """Update status from WebApp before returning it here like :meth:`status`.""" - logging.info("D-Bus method call: %s(%s, %d)", "refresh_status", host_id, session_id) + logging.info("D-Bus method call: %s(%s, %d)", "refresh_status", host_uuid, session_id) - self._update_session(host_id, session_id) - response = self._get_status(host_id, session_id) + self._update_session(host_uuid, session_id) + response = self._get_status(host_uuid, session_id) logging.info("The response to the D-Bus caller: '%s'", response) return response @dbus.service.method( "org.ArcticaProject.RWASupportSessionService", in_signature="si", out_signature="s" ) - def stop(self, host_id: str, session_id: int) -> str: + def stop(self, host_uuid: str, session_id: int) -> str: """Stop a remote session. - :param host_id: Host ID (D-Bus string) + :param host_uuid: Host ID (D-Bus string) :param session_id: Session ID (D-Bus integer) :return: Session status as JSON (D-Bus string) @@ -434,35 +437,35 @@ class RWASupportSessionService(dbus.service.Object): :: - {"host_id": "", "session_id": , "status": "stopped"} + {"host_uuid": "", "session_id": , "status": "stopped"} """ - logging.info("D-Bus method call: %s(%s, %d)", "stop", host_id, session_id) + logging.info("D-Bus method call: %s(%s, %d)", "stop", host_uuid, session_id) - combined_id = combine(host_id, session_id) + combined_id = combine(host_uuid, session_id) try: session = self.sessions[combined_id] except KeyError: logging.debug("D-Bus method stop(): sessions[%s] does not exist.", combined_id) response = json.dumps( - {"host_id": host_id, "session_id": session_id, "status": "stopped"}, sort_keys=True + {"host_uuid": host_uuid, "session_id": session_id, "status": "stopped"}, sort_keys=True ) logging.info("The response to the D-Bus caller: '%s'", response) return response session.stop() response = json.dumps( - {"host_id": host_id, "session_id": session_id, "status": "stopped"}, sort_keys=True + {"host_uuid": host_uuid, "session_id": session_id, "status": "stopped"}, sort_keys=True ) logging.info("The response to the D-Bus caller: '%s'", response) return response - def _get_status(self, host_id: str, session_id: int) -> str: - combined_id = combine(host_id, session_id) + def _get_status(self, host_uuid: str, session_id: int) -> str: + combined_id = combine(host_uuid, session_id) try: session = self.sessions[combined_id] except KeyError: logging.debug("_get_status(): self.sessions[%s] does not exist.", combined_id) return json.dumps( - {"host_id": host_id, "session_id": session_id, "status": "dead"}, sort_keys=True + {"host_uuid": host_uuid, "session_id": session_id, "status": "dead"}, sort_keys=True ) return json.dumps(session.status) @@ -472,13 +475,13 @@ class RWASupportSessionService(dbus.service.Object): self.update_thread = Thread(target=self._update_sessions) self.update_thread.start() - def _update_session(self, host_id: str, session_id: int): + def _update_session(self, host_uuid: str, session_id: int): """Update the status of a session.""" - combined_id = combine(host_id, session_id) + combined_id = combine(host_uuid, session_id) try: session = self.sessions[combined_id] except KeyError: - logging.info(f"Update status for session #{session_id} on host {host_id} …") + logging.info(f"Update status for session #{session_id} on host {host_uuid} …") logging.warning("Session %s is dead.", combined_id) return @@ -487,12 +490,12 @@ class RWASupportSessionService(dbus.service.Object): if running: pass elif session.status_text == "stopped" and session.pid in self.sessions: - logging.info(f"Update status for session #{session_id} on host {host_id} …") + logging.info(f"Update status for session #{session_id} on host {host_uuid} …") logging.warning("Session %s is dead.", combined_id) del self.sessions[combined_id] else: - logging.info(f"Update status for session #{session_id} on host {host_id} …") + logging.info(f"Update status for session #{session_id} on host {host_uuid} …") logging.warning("VNC was stopped, so session %s is dead.", session.combined_id) session.stop() @@ -503,7 +506,7 @@ class RWASupportSessionService(dbus.service.Object): logging.info("Started update service for sessions.") while len(self.sessions.values()) > 0: for session in list(self.sessions.values()): - self._update_session(session.host_id, session.session_id) + self._update_session(session.host_uuid, session.session_id) time.sleep(2) @@ -520,7 +523,7 @@ class RWASupportSessionService(dbus.service.Object): if session.session_id == session_id: r = session.trigger(data, method) logging.info( - f"Found matching session #{session.session_id} on host {session.host_id}: {r}" + f"Found matching session #{session.session_id} on host {session.host_uuid}: {r}" ) return r diff --git a/rwa/support/sessionservice/session.py b/rwa/support/sessionservice/session.py index 53bde2d..d3aa57c 100644 --- a/rwa/support/sessionservice/session.py +++ b/rwa/support/sessionservice/session.py @@ -51,8 +51,8 @@ def get_desktop_dir(): return output.strip().replace("\n", "") -def combine(host_id: str, session_id: int): - return f"{host_id}-{session_id}" +def combine(host_uuid: str, session_id: int): + return f"{host_uuid}-{session_id}" class Session: @@ -65,7 +65,7 @@ class Session: def __init__(self, host_object: dict, trigger_port: int, mockup_session: bool = False): self.host_object = host_object self.host_url = self.host_object["url"] - self.host_id = self.host_object["id"] + self.host_uuid = self.host_object["id"] self.BASE_URL = self.host_url + API_PATH self.REGISTER_URL = self.BASE_URL + "register/" self.STOP_URL = self.BASE_URL + "stop/" @@ -86,7 +86,7 @@ class Session: @property def combined_id(self): - return combine(self.host_id, self.session_id) + return combine(self.host_uuid, self.session_id) @property def pid(self) -> int: @@ -307,7 +307,7 @@ class Session: @property def client_meta(self) -> Dict[str, Union[str, int]]: return { - "host_id": self.host_id, + "host_uuid": self.host_uuid, "session_id": self.session_id, "url": self.web_url, "pin": self.pin, @@ -315,4 +315,4 @@ class Session: @property def status(self) -> Dict[str, Union[str, int]]: - return {"host_id": self.host_id, "session_id": self.session_id, "status": self.status_text} + return {"host_uuid": self.host_uuid, "session_id": self.session_id, "status": self.status_text} -- cgit v1.2.3 From 94cb2b582e2ae774d7e61237ea9c6ecde212a872 Mon Sep 17 00:00:00 2001 From: Daniel Teichmann Date: Thu, 1 Jul 2021 21:26:44 +0200 Subject: test_client.py: Update docstrings --- test_client.py | 48 ++++++++++++++++++++++++------------------------ 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/test_client.py b/test_client.py index 4a0f83a..bb54500 100755 --- a/test_client.py +++ b/test_client.py @@ -44,8 +44,8 @@ def cli(): @cli.command() def get_web_app_hosts(): - """Start a session on the RWA.Support.WebApp host with index HOST.""" - click.echo("Sending D-Bus request 'get_web_app_hosts'") + """Get all RWA.Support.WebApp hosts""" + click.echo("Sending D-Bus request 'get_web_app_hosts.'") response = req.get_web_app_hosts() click.echo(f"Your response is: {response}") @@ -53,7 +53,7 @@ def get_web_app_hosts(): @cli.command() @click.argument("host", type=str) def add_web_app_host(host: str): - """Start a session on the RWA.Support.WebApp host with index HOST.""" + """Add a RWA.Support.WebApp host. Requires """ click.echo(f"Sending D-Bus request 'add_web_app_host': {host}") response = req.add_web_app_host(host) click.echo(f"Your response is: {response}") @@ -62,50 +62,50 @@ def add_web_app_host(host: str): @cli.command() @click.argument("host", type=str) def remove_web_app_host(host: str): - """Start a session on the RWA.Support.WebApp host with index HOST.""" + """Remove a RWA.Support.WebApp host. Requires """ click.echo(f"Sending D-Bus request 'remove_web_app_host': {host}") response = req.remove_web_app_host(host) click.echo(f"Your response is: {response}") @cli.command() -@click.argument("host_id", type=str) -def start(host_id: str): - """Start a session on the RWA.Support.WebApp host with index HOST.""" - click.echo(f"Sending D-Bus request 'start': {host_id}") - response = req.start(host_id) +@click.argument("host_uuid", type=str) +def start(host_uuid: str): + """Start a session on the RWA.Support.WebApp host. Requires """ + click.echo(f"Sending D-Bus request 'start': {host_uuid}") + response = req.start(host_uuid) click.echo(f"Your response is: {response}") @cli.command() -@click.argument("host_id", type=str) +@click.argument("host_uuid", type=str) @click.argument("session_id", type=int) -def stop(host_id: str, session_id: int): - """Stop the session with the pid PID.""" - click.echo(f"Sending D-Bus request 'stop' with host ID {host_id} and session ID {session_id}") - response = req.stop(host_id, session_id) +def stop(host_uuid: str, session_id: int): + """Stop a session on the RWA.Support.WebApp host. Requires """ + click.echo(f"Sending D-Bus request 'stop' with host ID {host_uuid} and session ID {session_id}") + response = req.stop(host_uuid, session_id) click.echo(f"Your response is: {response}") @cli.command() -@click.argument("host_id", type=str) +@click.argument("host_uuid", type=str) @click.argument("session_id", type=int) -def status(host_id: str, session_id: int): - """Get the status of the session with the pid PID.""" - click.echo(f"Sending D-Bus request 'status' with host ID {host_id} and session ID {session_id}") - response = req.status(host_id, session_id) +def status(host_uuid: str, session_id: int): + """Get the status of a session. Requires """ + click.echo(f"Sending D-Bus request 'status' with host ID {host_uuid} and session ID {session_id}") + response = req.status(host_uuid, session_id) click.echo(f"Your response is: {response}") @cli.command() -@click.argument("host_id", type=str) +@click.argument("host_uuid", type=str) @click.argument("session_id", type=int) -def refresh_status(host_id: str, session_id: int): - """Refresh and get the status of the session with the pid PID.""" +def refresh_status(host_uuid: str, session_id: int): + """Refresh and then get the status of a session. Requires """ click.echo( - f"Sending D-Bus request 'refresh_status' with host ID {host_id} and session ID {session_id}" + f"Sending D-Bus request 'refresh_status' with host ID {host_uuid} and session ID {session_id}" ) - response = req.refresh_status(host_id, session_id) + response = req.refresh_status(host_uuid, session_id) click.echo(f"Your response is: {response}") -- cgit v1.2.3 From 2a354a70597dbbe23b203aa7f2693a9388b086d1 Mon Sep 17 00:00:00 2001 From: Daniel Teichmann Date: Thu, 1 Jul 2021 21:49:24 +0200 Subject: Fix duplicate url matching --- rwa/support/sessionservice/service.py | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/rwa/support/sessionservice/service.py b/rwa/support/sessionservice/service.py index 7bb45b2..95c014b 100755 --- a/rwa/support/sessionservice/service.py +++ b/rwa/support/sessionservice/service.py @@ -96,7 +96,7 @@ class RWASupportSessionService(dbus.service.Object): :return: Whether the string is an URL. """ valid = validators.url(url) - logging.debug(f"Is {url} an URL: {valid}") + logging.debug(f"Is '{url}' an URL: {valid}") return valid def _get_web_app_hosts(self) -> str: @@ -224,13 +224,20 @@ class RWASupportSessionService(dbus.service.Object): logging.info('The response to D-Bus caller: "%s"', response) return response - if host_url in self.settings.web_app_hosts: - logging.warning("Given URL is already present!") - logging.debug('Did not add "%s" to "web_app_hosts" in user_settings', host_url) - - response = json.dumps({"status": "error", "type": "duplicate"}) - logging.info('The response to D-Bus caller: "%s"', response) - return response + try: + for uuid, host in self.settings.web_app_hosts.items(): + if host_url in host['url']: + logging.warning("Given URL is already present!") + logging.debug('Did not add "%s" to "web_app_hosts" in user_settings', host_url) + + response = json.dumps({"status": "error", "type": "duplicate"}) + logging.info('The response to D-Bus caller: "%s"', response) + return response + except (KeyError, IndexError): + logging.warning( + 'Got an exception while trying to find given url ' + 'in already existing hosts!' + ) res = self._do_api_handshake(host_url) if res["status"] == "error": -- cgit v1.2.3 From 2048de4f21e5db17820bf245dcdc2d779cf3a259 Mon Sep 17 00:00:00 2001 From: Daniel Teichmann Date: Fri, 2 Jul 2021 20:40:47 +0200 Subject: Reflect API changes in docstrings --- rwa/support/sessionservice/service.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/rwa/support/sessionservice/service.py b/rwa/support/sessionservice/service.py index 95c014b..93ec919 100755 --- a/rwa/support/sessionservice/service.py +++ b/rwa/support/sessionservice/service.py @@ -126,7 +126,7 @@ class RWASupportSessionService(dbus.service.Object): :: - [{"id": , "https://example.org"}, {"id": , "http://127.0.0.1:8000"}] + [{"url": "https://example.org", "uuid": }, {"url": "http://127.0.0.1:8000", "uuid": }] """ logging.info("D-Bus method call: %s()", "get_web_app_hosts") @@ -196,7 +196,7 @@ class RWASupportSessionService(dbus.service.Object): :: - {"id": , "http://127.0.0.1:8000"} + {"status": "success", "host": {"url": "https://example.org", "uuid": }} **Structure of returned JSON (error):** @@ -255,7 +255,8 @@ class RWASupportSessionService(dbus.service.Object): logging.info('Added "%s" to "web_app_hosts" in user_settings', host_url) - response = json.dumps(self._build_host_dict(host_uuid, host_object)) + response = {"status": "success", "host": self._build_host_dict(host_uuid, host_object)} + response = json.dumps(response) logging.info('The response to D-Bus caller: "%s"', response) return response @@ -272,7 +273,7 @@ class RWASupportSessionService(dbus.service.Object): :: - [{"id": , "https://example.org"}, {"id": , "http://127.0.0.1:8000"}] + [{"url": "https://example.org", "uuid": }, {"url": "http://127.0.0.1:8000", "uuid": }] """ logging.info("D-Bus method call: %s(%s)", "remove_web_app_host", host_uuid) -- cgit v1.2.3 From 651d0b73df96fa467c1de207ac8055f4e0fd58a7 Mon Sep 17 00:00:00 2001 From: Daniel Teichmann Date: Fri, 2 Jul 2021 20:42:42 +0200 Subject: Fix URL duplication matching --- rwa/support/sessionservice/service.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rwa/support/sessionservice/service.py b/rwa/support/sessionservice/service.py index 93ec919..175ae17 100755 --- a/rwa/support/sessionservice/service.py +++ b/rwa/support/sessionservice/service.py @@ -226,7 +226,7 @@ class RWASupportSessionService(dbus.service.Object): try: for uuid, host in self.settings.web_app_hosts.items(): - if host_url in host['url']: + if host_url == host['url']: logging.warning("Given URL is already present!") logging.debug('Did not add "%s" to "web_app_hosts" in user_settings', host_url) -- cgit v1.2.3