Skip to content

Commit 5672632

Browse files
a-h-abdelsalamgreenbonebot
authored andcommitted
Refactor to resolve CodeQL alerts.
1 parent ddb4b5f commit 5672632

2 files changed

Lines changed: 30 additions & 32 deletions

File tree

gvm/protocols/http/openvasd/_client.py

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def create_openvasd_http_client(
3131
"""
3232
Create a `httpx.Client` configured for the OpenVASD HTTP API.
3333
34-
mTLS is used by default. Set `insecure_http=True` to use plain HTTP without SSL verification.
34+
mTLS is used by default. Set `insecure_http=True` to use plain HTTP.
3535
In this case, an API key can be used for authorization.
3636
3737
Args:
@@ -44,50 +44,50 @@ def create_openvasd_http_client(
4444
port: The port to connect to (default: 3000).
4545
4646
Behavior:
47-
- If `insecure_http=True`, `verify` is set to False (insecure), and HTTP is used instead of HTTPS.
47+
- If `insecure_http=True`, HTTP is used instead of HTTPS.
4848
An API key can be used for authorization.
49-
- If `insecure_http=False` (default), HTTPs with mTLS is used.
49+
- If `insecure_http=False` (default), HTTPS with mTLS is used.
5050
Both `server_ca_path` and `client_cert_paths` are required.
5151
5252
Raises:
5353
ValueError: If `insecure_http=False` and either
5454
`server_ca_path` or `client_cert_paths` is missing.
5555
"""
5656
headers = {}
57+
if api_key:
58+
headers["X-API-KEY"] = api_key
5759

58-
context: ssl.SSLContext | None = None
59-
protocol = "http" if insecure_http else "https"
60-
verify: bool | ssl.SSLContext = False if insecure_http else True
61-
62-
if not insecure_http:
63-
if not server_ca_path or not client_cert_paths:
64-
raise ValueError(
65-
"Both server_ca_path and client_cert_paths must be provided "
66-
"when insecure_http is False."
67-
)
68-
# Prepare mTLS SSL context
69-
context = ssl.create_default_context(
70-
ssl.Purpose.SERVER_AUTH, cafile=server_ca_path
60+
if insecure_http:
61+
return Client(
62+
base_url=f"http://{host_name}:{port}",
63+
headers=headers,
64+
http2=True,
65+
timeout=10.0,
7166
)
72-
if isinstance(client_cert_paths, tuple):
73-
context.load_cert_chain(
74-
certfile=client_cert_paths[0], keyfile=client_cert_paths[1]
75-
)
76-
else:
77-
context.load_cert_chain(certfile=client_cert_paths)
7867

79-
context.check_hostname = False
80-
context.verify_mode = ssl.CERT_REQUIRED
81-
verify = context
82-
if api_key:
83-
headers["X-API-KEY"] = api_key
68+
if not server_ca_path or not client_cert_paths:
69+
raise ValueError(
70+
"Both server_ca_path and client_cert_paths must be provided "
71+
"when insecure_http is False."
72+
)
73+
# Prepare mTLS SSL context
74+
context = ssl.create_default_context(
75+
ssl.Purpose.SERVER_AUTH, cafile=server_ca_path
76+
)
77+
if isinstance(client_cert_paths, tuple):
78+
context.load_cert_chain(
79+
certfile=client_cert_paths[0], keyfile=client_cert_paths[1]
80+
)
81+
else:
82+
context.load_cert_chain(certfile=client_cert_paths)
8483

85-
base_url = f"{protocol}://{host_name}:{port}"
84+
context.check_hostname = False
85+
context.verify_mode = ssl.CERT_REQUIRED
8686

8787
return Client(
88-
base_url=base_url,
88+
base_url=f"https://{host_name}:{port}",
8989
headers=headers,
90-
verify=verify,
90+
verify=context,
9191
http2=True,
9292
timeout=10.0,
9393
)

tests/protocols/http/openvasd/test_client.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ def test_init_without_tls_or_api_key(
1919
mock_httpx_client.assert_called_once()
2020
_, kwargs = mock_httpx_client.call_args
2121
self.assertEqual(kwargs["base_url"], "http://localhost:3000")
22-
self.assertFalse(kwargs["verify"])
2322
self.assertNotIn("X-API-KEY", kwargs["headers"])
2423
mock_ssl_ctx_factory.assert_not_called()
2524

@@ -34,7 +33,6 @@ def test_init_with_api_key_and_insecure_http(
3433
_, kwargs = mock_httpx_client.call_args
3534
self.assertEqual(kwargs["headers"]["X-API-KEY"], "secret")
3635
self.assertEqual(kwargs["base_url"], "http://localhost:3000")
37-
self.assertFalse(kwargs["verify"])
3836
mock_ssl_ctx_factory.assert_not_called()
3937

4038
@patch("gvm.protocols.http.openvasd._client.ssl.create_default_context")

0 commit comments

Comments
 (0)