refactor: update authentication handling to use aiohttp.encode_basic_auth and adjust request options + fix other pytest warnings

This commit is contained in:
grossmj committed 2026-10-31 22:00:21 +07:00
1 parent 1608a65a01
commit ac65698eea
9 files changed
+50 -33

No files matched your search

+8 -6
View File
@@ -866,11 +866,13 @@ async def ws_console(
raise ControllerForbiddenError("Compute username is not set")
user = user.strip()
if user and password:
auth = aiohttp.BasicAuth(user, password.get_secret_value(), "utf-8")
auth_header = aiohttp.encode_basic_auth(user, password.get_secret_value(), encoding="utf-8")
else:
auth = aiohttp.BasicAuth(user, "")
auth_header = aiohttp.encode_basic_auth(user, "", encoding="utf-8")
ssl_context = Controller.instance().ssl_context()
async with HTTPClient.get_client().ws_connect(ws_console_compute_url, auth=auth, ssl_context=ssl_context) as ws:
async with HTTPClient.get_client().ws_connect(
ws_console_compute_url, headers={"Authorization": auth_header}, ssl_context=ssl_context
) as ws:
tasks = [
asyncio.ensure_future(ws_receive(ws)),
asyncio.ensure_future(ws_send(ws)),
@@ -964,12 +966,12 @@ async def vnc_console(
raise ControllerForbiddenError("Compute username is not set")
user = user.strip()
if user and password:
auth = aiohttp.BasicAuth(user, password.get_secret_value(), "utf-8")
auth_header = aiohttp.encode_basic_auth(user, password.get_secret_value(), encoding="utf-8")
else:
auth = aiohttp.BasicAuth(user, "")
auth_header = aiohttp.encode_basic_auth(user, "", encoding="utf-8")
ssl_context = Controller.instance().ssl_context()
async with HTTPClient.get_client().ws_connect(
vnc_console_compute_url, auth=auth, ssl_context=ssl_context
vnc_console_compute_url, headers={"Authorization": auth_header}, ssl_context=ssl_context
) as ws:
tasks = [
asyncio.ensure_future(vnc_receive(ws)),
+19 -7
View File
@@ -134,12 +134,14 @@ class Compute:
if password:
self._password = password
try:
self._auth = aiohttp.BasicAuth(self._user, self._password.get_secret_value(), "utf-8")
self._auth = aiohttp.encode_basic_auth(
self._user, self._password.get_secret_value(), encoding="utf-8"
)
except ValueError as e:
log.error(str(e))
else:
self._password = None
self._auth = aiohttp.BasicAuth(self._user, "")
self._auth = aiohttp.encode_basic_auth(self._user, "", encoding="utf-8")
def set_last_error(self, msg):
"""
@@ -353,7 +355,10 @@ class Compute:
"""
url = self._getUrl(f"/projects/{project.id}/files/{path}")
response = await self._session().request("GET", url, auth=self._auth)
request_options = {"auth": None}
if self._auth:
request_options["headers"] = {"Authorization": self._auth}
response = await self._session().request("GET", url, **request_options)
if response.status == 404:
raise ControllerNotFoundError(f"{path} not found on compute")
return response
@@ -368,7 +373,10 @@ class Compute:
"""
url = self._getUrl(f"/{image_type}/images/{image}")
response = await self._session().request("GET", url, auth=self._auth)
request_options = {"auth": None}
if self._auth:
request_options["headers"] = {"Authorization": self._auth}
response = await self._session().request("GET", url, **request_options)
if response.status == 404:
raise ControllerNotFoundError(f"{image} not found on compute")
return response
@@ -523,7 +531,10 @@ class Compute:
ws_url = self._getUrl("/notifications/ws")
try:
async with self._session().ws_connect(ws_url, auth=self._auth, heartbeat=10) as ws:
connection_options = {"heartbeat": 10}
if self._auth:
connection_options["headers"] = {"Authorization": self._auth}
async with self._session().ws_connect(ws_url, **connection_options) as ws:
log.info(f"Connected to compute '{self._id}' WebSocket '{ws_url}'")
async for response in ws:
if response.type == aiohttp.WSMsgType.TEXT:
@@ -598,6 +609,8 @@ class Compute:
async with asynctimeout(delay=timeout):
url = self._getUrl(path)
headers = {"content-type": "application/json"}
if self._auth:
headers["Authorization"] = self._auth
chunked = None
if data == {}:
data = None
@@ -622,13 +635,12 @@ class Compute:
else:
data = json.dumps(data).encode("utf-8")
try:
log.debug(f"Attempting request to compute: {method} {url} {headers}")
log.debug(f"Attempting request to compute: {method} {url}")
response = await self._session().request(
method,
url,
headers=headers,
data=data,
auth=self._auth,
params=params,
chunked=chunked,
timeout=timeout,
+9 -2
View File
@@ -59,11 +59,18 @@ class HTTPClient:
basic_auth = None
if user:
try:
basic_auth = aiohttp.BasicAuth(user, password.get_secret_value() if password else "", "utf-8")
basic_auth = aiohttp.encode_basic_auth(
user, password.get_secret_value() if password else "", encoding="utf-8"
)
except ValueError as e:
log.error(f"Basic authentication set-up error: {e}")
return client.request(method, url, auth=basic_auth, **kwargs)
headers = kwargs.pop("headers", None)
if basic_auth is not None:
headers = {key: value for key, value in (headers or {}).items() if key.lower() != "authorization"}
headers["Authorization"] = basic_auth
kwargs["headers"] = headers
return client.request(method, url, **kwargs)
@classmethod
def get(cls, path, **kwargs):