From d09c497c12ac5e7797550f290e25ee1582743358 Mon Sep 17 00:00:00 2001 From: Andrew Rowley Date: Mon, 4 Dec 2023 14:08:05 +0000 Subject: [PATCH 1/5] Make it a bit more user friendly when you can't log in --- spinnman/spalloc/session.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/spinnman/spalloc/session.py b/spinnman/spalloc/session.py index 36aaad419..d4fd588fe 100644 --- a/spinnman/spalloc/session.py +++ b/spinnman/spalloc/session.py @@ -15,12 +15,13 @@ from logging import getLogger import re import requests +from json.decoder import JSONDecodeError from typing import Dict, Tuple, cast, Optional import websocket # type: ignore from spinn_utilities.log import FormatAdapter from spinn_utilities.typing.json import JsonObject -from .utils import clean_url from spinnman.exceptions import SpallocException +from .utils import clean_url logger = FormatAdapter(getLogger(__name__)) #: The name of the session cookie issued by Spring Security @@ -250,7 +251,18 @@ def renew(self) -> JsonObject: data=form, timeout=10) logger.debug("POST {} returned {}", self.__login_submit_url, r.status_code) - self._session_id = r.cookies[_SESSION_COOKIE] + try: + self._session_id = r.cookies[_SESSION_COOKIE] + except KeyError as e: + try: + json_error = r.json() + if 'message' in json_error: + error = json_error['message'] + else: + error = str(json_error) + except JSONDecodeError: + error = r.raw + raise SpallocException(f"Unable to login: {error}") from e # We don't need to follow that redirect # Step three: get the basic service data and new CSRF token From f356bd23630d538aa8990b4c43ff16078c8590e6 Mon Sep 17 00:00:00 2001 From: Andrew Rowley Date: Mon, 4 Dec 2023 14:28:12 +0000 Subject: [PATCH 2/5] Not sure what is really different! --- spinnman/spalloc/session.py | 1 - 1 file changed, 1 deletion(-) diff --git a/spinnman/spalloc/session.py b/spinnman/spalloc/session.py index d4fd588fe..ab3601705 100644 --- a/spinnman/spalloc/session.py +++ b/spinnman/spalloc/session.py @@ -263,7 +263,6 @@ def renew(self) -> JsonObject: except JSONDecodeError: error = r.raw raise SpallocException(f"Unable to login: {error}") from e - # We don't need to follow that redirect # Step three: get the basic service data and new CSRF token obj: JsonObject = self.get(self.__srv_base).json() From 65dc48117e72a9251b52a6daf76c10135331aa80 Mon Sep 17 00:00:00 2001 From: Andrew Rowley Date: Mon, 4 Dec 2023 14:30:57 +0000 Subject: [PATCH 3/5] Does this help? --- spinnman/spalloc/session.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spinnman/spalloc/session.py b/spinnman/spalloc/session.py index ab3601705..21fac6aba 100644 --- a/spinnman/spalloc/session.py +++ b/spinnman/spalloc/session.py @@ -262,7 +262,7 @@ def renew(self) -> JsonObject: error = str(json_error) except JSONDecodeError: error = r.raw - raise SpallocException(f"Unable to login: {error}") from e + raise KeyError(f"Unable to login: {error}") from e # Step three: get the basic service data and new CSRF token obj: JsonObject = self.get(self.__srv_base).json() From fd4acbd1761876c03c35196704ad2faccabba6d2 Mon Sep 17 00:00:00 2001 From: Andrew Rowley Date: Mon, 4 Dec 2023 14:41:51 +0000 Subject: [PATCH 4/5] Fixes? --- spinnman/spalloc/spalloc_client.py | 28 +++++++++++++--------------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/spinnman/spalloc/spalloc_client.py b/spinnman/spalloc/spalloc_client.py index f440de6f3..462cb0963 100644 --- a/spinnman/spalloc/spalloc_client.py +++ b/spinnman/spalloc/spalloc_client.py @@ -571,7 +571,7 @@ def __proxy_url(self) -> Optional[str]: except KeyError: return None - def __init_proxy(self) -> _ProxyReceiver: + def __init_proxy(self) -> Tuple[_ProxyReceiver, WebSocket]: if self.__proxy_handle is None or not self.__proxy_handle.connected: if self.__proxy_url is None: raise ValueError("no proxy available") @@ -579,39 +579,37 @@ def __init_proxy(self) -> _ProxyReceiver: self.__proxy_url, origin=get_hostname(self._url)) self.__proxy_thread = _ProxyReceiver(self.__proxy_handle) self.__proxy_ping = _ProxyPing(self.__proxy_handle) + assert self.__proxy_handle is not None assert self.__proxy_thread is not None - return self.__proxy_thread + return self.__proxy_thread, self.__proxy_handle @overrides(SpallocJob.connect_to_board) def connect_to_board( self, x: int, y: int, port: int = SCP_SCAMP_PORT) -> SpallocSCPConnection: - proxy = self.__init_proxy() - return _ProxiedSCAMPConnection( - self.__proxy_handle, proxy, int(x), int(y), int(port)) + proxy, ws = self.__init_proxy() + return _ProxiedSCAMPConnection(ws, proxy, int(x), int(y), int(port)) @overrides(SpallocJob.connect_for_booting) def connect_for_booting(self) -> SpallocBootConnection: - proxy = self.__init_proxy() - return _ProxiedBootConnection(self.__proxy_handle, proxy) + proxy, ws = self.__init_proxy() + return _ProxiedBootConnection(ws, proxy) @overrides(SpallocJob.open_eieio_connection) def open_eieio_connection(self, x: int, y: int) -> SpallocEIEIOConnection: - proxy = self.__init_proxy() + proxy, ws = self.__init_proxy() return _ProxiedEIEIOConnection( - self.__proxy_handle, proxy, int(x), int(y), SCP_SCAMP_PORT) + ws, proxy, int(x), int(y), SCP_SCAMP_PORT) @overrides(SpallocJob.open_eieio_listener_connection) def open_eieio_listener_connection(self) -> SpallocEIEIOListener: - proxy = self.__init_proxy() - return _ProxiedEIEIOListener( - self.__proxy_handle, proxy, self.get_connections()) + proxy, ws = self.__init_proxy() + return _ProxiedEIEIOListener(ws, proxy, self.get_connections()) @overrides(SpallocJob.open_udp_listener_connection) def open_udp_listener_connection(self) -> UDPConnection: - proxy = self.__init_proxy() - return _ProxiedUDPListener( - self.__proxy_handle, proxy, self.get_connections()) + proxy, ws = self.__init_proxy() + return _ProxiedUDPListener(ws, proxy, self.get_connections()) @overrides(SpallocJob.wait_for_state_change) def wait_for_state_change(self, old_state: SpallocState, From 6552ec1aae5070f71a79370d95dd26fa4eccfb47 Mon Sep 17 00:00:00 2001 From: Andrew Rowley Date: Mon, 4 Dec 2023 14:43:33 +0000 Subject: [PATCH 5/5] Hopefully this breaks nothing! --- spinnman/spalloc/session.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spinnman/spalloc/session.py b/spinnman/spalloc/session.py index 21fac6aba..ab3601705 100644 --- a/spinnman/spalloc/session.py +++ b/spinnman/spalloc/session.py @@ -262,7 +262,7 @@ def renew(self) -> JsonObject: error = str(json_error) except JSONDecodeError: error = r.raw - raise KeyError(f"Unable to login: {error}") from e + raise SpallocException(f"Unable to login: {error}") from e # Step three: get the basic service data and new CSRF token obj: JsonObject = self.get(self.__srv_base).json()