From 8c944356d4a28410ce534b8ca66252f47bfc3d3b Mon Sep 17 00:00:00 2001 From: Adam Thornton Date: Fri, 2 Aug 2024 15:23:25 -0700 Subject: [PATCH] Rework debug logging. --- src/mobu/storage/nublado.py | 82 +++++++++++++++++++++++-------------- 1 file changed, 52 insertions(+), 30 deletions(-) diff --git a/src/mobu/storage/nublado.py b/src/mobu/storage/nublado.py index f4d16d61..12b78ecb 100644 --- a/src/mobu/storage/nublado.py +++ b/src/mobu/storage/nublado.py @@ -597,27 +597,38 @@ async def auth_to_hub(self) -> None: # redirection, because httpx doesn't do that if following redirects # automatically. while r.is_redirect: + self._logger.debug( + "Following hub redirect", + method=r.request.method, + original_url=r.request.url.copy_with( + query=None, fragment=None + ), + response_url=r.url.copy_with(query=None, fragment=None), + status_code=r.status_code, + ) xsrf = self._extract_xsrf(r) - if xsrf and xsrf != self._lab_xsrf: - if xsrf != self._hub_xsrf: - self._hub_xsrf = xsrf - self._logger.debug( - "Set _hub_xsrf", - url=r.url.copy_with(query=None, fragment=None), - status_code=r.status_code, - ) - next_url = urljoin(url, r.headers["Location"]) - r = await self._client.get(next_url, follow_redirects=False) - r.raise_for_status() - xsrf = self._extract_xsrf(r) - if xsrf and xsrf != self._lab_xsrf: - if xsrf != self._hub_xsrf: + if xsrf and xsrf != self._hub_xsrf: self._hub_xsrf = xsrf self._logger.debug( "Set _hub_xsrf", url=r.url.copy_with(query=None, fragment=None), status_code=r.status_code, ) + next_url = urljoin(url, r.headers["Location"]) + r = await self._client.get(next_url, follow_redirects=False) + r.raise_for_status() + xsrf = self._extract_xsrf(r) + if xsrf and xsrf == self._lab_xsrf: + self._logger.warning( + "_xsrf cookie for hub login matches lab _xsrf" + ) + if xsrf and xsrf != self._hub_xsrf: + self._hub_xsrf = xsrf + self._logger.debug( + "Set _hub_xsrf", + url=r.url.copy_with(query=None, fragment=None), + status_code=r.status_code, + ) elif not self._hub_xsrf: msg = "No _xsrf cookie set in login reply from JupyterHub" raise JupyterProtocolError(msg) @@ -647,29 +658,40 @@ async def auth_to_lab(self) -> None: url, headers=headers, follow_redirects=False ) while r.is_redirect: - xsrf = self._extract_xsrf(r) - if xsrf and xsrf != self._hub_xsrf: - if xsrf != self._lab_xsrf: - self._lab_xsrf = xsrf - self._logger.debug( - "Set _lab_xsrf", - url=r.url.copy_with(query=None, fragment=None), - status_code=r.status_code, - ) - next_url = urljoin(url, r.headers["Location"]) - r = await self._client.get( - next_url, headers=headers, follow_redirects=False + self._logger.debug( + "Following lab redirect", + method=r.request.method, + original_url=r.request.url.copy_with( + query=None, fragment=None + ), + response_url=r.url.copy_with(query=None, fragment=None), + status_code=r.status_code, ) - r.raise_for_status() - xsrf = self._extract_xsrf(r) - if xsrf and xsrf != self._hub_xsrf: - if xsrf != self._lab_xsrf: + xsrf = self._extract_xsrf(r) + if xsrf and xsrf != self._lab_xsrf: self._lab_xsrf = xsrf self._logger.debug( "Set _lab_xsrf", url=r.url.copy_with(query=None, fragment=None), status_code=r.status_code, ) + next_url = urljoin(url, r.headers["Location"]) + r = await self._client.get( + next_url, headers=headers, follow_redirects=False + ) + r.raise_for_status() + xsrf = self._extract_xsrf(r) + if xsrf and xsrf == self._hub_xsrf: + self._logger.warning( + "_xsrf cookie for lab login matches hub _xsrf" + ) + if xsrf and xsrf != self._lab_xsrf: + self._lab_xsrf = xsrf + self._logger.debug( + "Set _lab_xsrf", + url=r.url.copy_with(query=None, fragment=None), + status_code=r.status_code, + ) if not self._lab_xsrf: msg = "No _xsrf cookie set in login reply from lab" raise JupyterProtocolError(msg)