Skip to content

Commit 10cf617

Browse files
committed
Rename versioned module and drop Client class
The client class just creates a httpx.Client instance. Therefore it should be a function. Rename the module and remove the v from the name to be consistent with other module names.
1 parent 0aea412 commit 10cf617

File tree

4 files changed

+76
-81
lines changed

4 files changed

+76
-81
lines changed

gvm/protocols/http/openvasd/_client.py

Lines changed: 55 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -3,79 +3,78 @@
33
# SPDX-License-Identifier: GPL-3.0-or-later
44

55
"""
6-
Client wrapper for initializing a connection to the openvasd HTTP API using optional mTLS authentication.
6+
http client for initializing a connection to the openvasd HTTP API using optional mTLS authentication.
77
"""
88

99
import ssl
10+
from os import PathLike
1011
from typing import Optional, Tuple, Union
1112

1213
from httpx import Client
1314

15+
StrOrPathLike = Union[str, PathLike[str]]
1416

15-
class OpenvasdClient:
17+
18+
def create_openvasd_http_client(
19+
host_name: str,
20+
*,
21+
api_key: Optional[str] = None,
22+
server_ca_path: Optional[StrOrPathLike] = None,
23+
client_cert_paths: Optional[
24+
Union[StrOrPathLike, Tuple[StrOrPathLike, StrOrPathLike]]
25+
] = None,
26+
port: int = 3000,
27+
) -> Client:
1628
"""
17-
The client wrapper around `httpx.Client` configured for mTLS-secured access or API KEY
29+
Create a `httpx.Client` configured for mTLS-secured or API KEY access
1830
to an openvasd HTTP API instance.
19-
"""
20-
21-
def __init__(
22-
self,
23-
host_name: str,
24-
*,
25-
api_key: Optional[str] = None,
26-
server_ca_path: Optional[str] = None,
27-
client_cert_paths: Optional[Union[str, Tuple[str, str]]] = None,
28-
port: int = 3000,
29-
):
30-
"""
31-
Initialize the OpenVASD HTTP client with optional mTLS and API key.
3231
33-
Args:
34-
host_name: Hostname or IP of the OpenVASD server (e.g., "localhost").
35-
api_key: Optional API key used for authentication via HTTP headers.
36-
server_ca_path: Path to the server's CA certificate (for verifying the server).
37-
client_cert_paths: Path to the client certificate (str) or a tuple of
38-
(cert_path, key_path) for mTLS authentication.
39-
port: The port to connect to (default: 3000).
32+
Args:
33+
host_name: Hostname or IP of the OpenVASD server (e.g., "localhost").
34+
api_key: Optional API key used for authentication via HTTP headers.
35+
server_ca_path: Path to the server's CA certificate (for verifying the server).
36+
client_cert_paths: Path to the client certificate (str) or a tuple of
37+
(cert_path, key_path) for mTLS authentication.
38+
port: The port to connect to (default: 3000).
4039
41-
Behavior:
42-
- If both `server_ca_path` and `client_cert_paths` are set, an mTLS connection
43-
is established using an SSLContext.
44-
- If not, `verify` is set to False (insecure), and HTTP is used instead of HTTPS.
45-
HTTP connection needs api_key for authorization.
46-
"""
47-
headers = {}
40+
Behavior:
41+
- If both `server_ca_path` and `client_cert_paths` are set, an mTLS connection
42+
is established using an SSLContext.
43+
- If not, `verify` is set to False (insecure), and HTTP is used instead of HTTPS.
44+
HTTP connection needs api_key for authorization.
45+
"""
46+
headers = {}
4847

49-
context: Optional[ssl.SSLContext] = None
48+
context: Optional[ssl.SSLContext] = None
5049

51-
# Prepare mTLS SSL context if needed
52-
if client_cert_paths and server_ca_path:
53-
context = ssl.create_default_context(
54-
ssl.Purpose.SERVER_AUTH, cafile=server_ca_path
50+
# Prepare mTLS SSL context if needed
51+
if client_cert_paths and server_ca_path:
52+
context = ssl.create_default_context(
53+
ssl.Purpose.SERVER_AUTH, cafile=server_ca_path
54+
)
55+
if isinstance(client_cert_paths, tuple):
56+
context.load_cert_chain(
57+
certfile=client_cert_paths[0], keyfile=client_cert_paths[1]
5558
)
56-
if isinstance(client_cert_paths, tuple):
57-
context.load_cert_chain(
58-
certfile=client_cert_paths[0], keyfile=client_cert_paths[1]
59-
)
60-
else:
61-
context.load_cert_chain(certfile=client_cert_paths)
59+
else:
60+
context.load_cert_chain(certfile=client_cert_paths)
6261

63-
context.check_hostname = False
64-
context.verify_mode = ssl.CERT_REQUIRED
62+
context.check_hostname = False
63+
context.verify_mode = ssl.CERT_REQUIRED
6564

66-
# Set verify based on context presence
67-
verify: Union[bool, ssl.SSLContext] = context if context else False
65+
# Set verify based on context presence
66+
verify: Union[bool, ssl.SSLContext] = context if context else False
6867

69-
if api_key:
70-
headers["X-API-KEY"] = api_key
68+
if api_key:
69+
headers["X-API-KEY"] = api_key
7170

72-
protocol = "https" if context else "http"
73-
base_url = f"{protocol}://{host_name}:{port}"
71+
protocol = "https" if context else "http"
72+
base_url = f"{protocol}://{host_name}:{port}"
7473

75-
self.client = Client(
76-
base_url=base_url,
77-
headers=headers,
78-
verify=verify,
79-
http2=True,
80-
timeout=10.0,
81-
)
74+
return Client(
75+
base_url=base_url,
76+
headers=headers,
77+
verify=verify,
78+
http2=True,
79+
timeout=10.0,
80+
)

gvm/protocols/http/openvasd/openvasdv1.py renamed to gvm/protocols/http/openvasd/openvasd1.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
from typing import Optional, Tuple, Union
1313

14-
from ._client import OpenvasdClient
14+
from ._client import StrOrPathLike, create_openvasd_http_client
1515
from .health import HealthAPI
1616
from .metadata import MetadataAPI
1717
from .notus import NotusAPI
@@ -35,8 +35,10 @@ def __init__(
3535
port: int = 3000,
3636
*,
3737
api_key: Optional[str] = None,
38-
server_ca_path: Optional[str] = None,
39-
client_cert_paths: Optional[Union[str, Tuple[str, str]]] = None,
38+
server_ca_path: Optional[StrOrPathLike] = None,
39+
client_cert_paths: Optional[
40+
Union[StrOrPathLike, Tuple[StrOrPathLike, StrOrPathLike]]
41+
] = None,
4042
):
4143
"""
4244
Initialize the OpenvasdHttpApiV1 entry point.
@@ -52,13 +54,13 @@ def __init__(
5254
- Sets up an underlying `httpx.Client` using `OpenvasdClient`.
5355
- Initializes sub-API modules with the shared client instance.
5456
"""
55-
self._client = OpenvasdClient(
57+
self._client = create_openvasd_http_client(
5658
host_name=host_name,
5759
port=port,
5860
api_key=api_key,
5961
server_ca_path=server_ca_path,
6062
client_cert_paths=client_cert_paths,
61-
).client
63+
)
6264

6365
# Sub-API modules
6466
self.health = HealthAPI(self._client)

tests/protocols/http/openvasd/test_client.py

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,26 @@
66
import unittest
77
from unittest.mock import MagicMock, patch
88

9-
from gvm.protocols.http.openvasd._client import OpenvasdClient
9+
from gvm.protocols.http.openvasd._client import create_openvasd_http_client
1010

1111

1212
class TestOpenvasdClient(unittest.TestCase):
13-
1413
@patch("gvm.protocols.http.openvasd._client.Client")
1514
def test_init_without_tls_or_api_key(self, mock_httpx_client):
16-
client = OpenvasdClient("localhost")
15+
create_openvasd_http_client("localhost")
1716
mock_httpx_client.assert_called_once()
18-
args, kwargs = mock_httpx_client.call_args
17+
_, kwargs = mock_httpx_client.call_args
1918
self.assertEqual(kwargs["base_url"], "http://localhost:3000")
2019
self.assertFalse(kwargs["verify"])
2120
self.assertNotIn("X-API-KEY", kwargs["headers"])
22-
self.assertEqual(client.client, mock_httpx_client())
2321

2422
@patch("gvm.protocols.http.openvasd._client.Client")
2523
def test_init_with_api_key_only(self, mock_httpx_client):
26-
client = OpenvasdClient("localhost", api_key="secret")
27-
args, kwargs = mock_httpx_client.call_args
24+
create_openvasd_http_client("localhost", api_key="secret")
25+
_, kwargs = mock_httpx_client.call_args
2826
self.assertEqual(kwargs["headers"]["X-API-KEY"], "secret")
2927
self.assertEqual(kwargs["base_url"], "http://localhost:3000")
3028
self.assertFalse(kwargs["verify"])
31-
self.assertEqual(client.client, mock_httpx_client())
3229

3330
@patch("gvm.protocols.http.openvasd._client.ssl.create_default_context")
3431
@patch("gvm.protocols.http.openvasd._client.Client")
@@ -38,7 +35,7 @@ def test_init_with_mtls_tuple(
3835
mock_context = MagicMock(spec=ssl.SSLContext)
3936
mock_ssl_ctx_factory.return_value = mock_context
4037

41-
OpenvasdClient(
38+
create_openvasd_http_client(
4239
"localhost",
4340
server_ca_path="/path/ca.pem",
4441
client_cert_paths=("/path/cert.pem", "/path/key.pem"),
@@ -51,7 +48,7 @@ def test_init_with_mtls_tuple(
5148
certfile="/path/cert.pem", keyfile="/path/key.pem"
5249
)
5350
mock_httpx_client.assert_called_once()
54-
args, kwargs = mock_httpx_client.call_args
51+
_, kwargs = mock_httpx_client.call_args
5552
self.assertEqual(kwargs["base_url"], "https://localhost:3000")
5653
self.assertEqual(kwargs["verify"], mock_context)
5754

@@ -63,7 +60,7 @@ def test_init_with_mtls_single_cert(
6360
mock_context = MagicMock(spec=ssl.SSLContext)
6461
mock_ssl_ctx_factory.return_value = mock_context
6562

66-
OpenvasdClient(
63+
create_openvasd_http_client(
6764
"localhost",
6865
server_ca_path="/path/ca.pem",
6966
client_cert_paths="/path/client.pem",
@@ -72,6 +69,6 @@ def test_init_with_mtls_single_cert(
7269
mock_context.load_cert_chain.assert_called_once_with(
7370
certfile="/path/client.pem"
7471
)
75-
args, kwargs = mock_httpx_client.call_args
72+
_, kwargs = mock_httpx_client.call_args
7673
self.assertEqual(kwargs["base_url"], "https://localhost:3000")
7774
self.assertEqual(kwargs["verify"], mock_context)

tests/protocols/http/openvasd/test_openvasd1.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,14 @@
55
import unittest
66
from unittest.mock import MagicMock, patch
77

8-
from gvm.protocols.http.openvasd.openvasdv1 import OpenvasdHttpApiV1
8+
from gvm.protocols.http.openvasd.openvasd1 import OpenvasdHttpApiV1
99

1010

1111
class TestOpenvasdHttpApiV1(unittest.TestCase):
12-
13-
@patch("gvm.protocols.http.openvasd.openvasdv1.OpenvasdClient")
14-
def test_initializes_all_sub_apis(self, mock_openvasd_client_class):
12+
@patch("gvm.protocols.http.openvasd.openvasd1.create_openvasd_http_client")
13+
def test_initializes_all_sub_apis(self, mock_crate_openvasd_client):
1514
mock_httpx_client = MagicMock()
16-
mock_openvasd_client_instance = MagicMock()
17-
mock_openvasd_client_instance.client = mock_httpx_client
18-
mock_openvasd_client_class.return_value = mock_openvasd_client_instance
15+
mock_crate_openvasd_client.return_value = mock_httpx_client
1916

2017
api = OpenvasdHttpApiV1(
2118
host_name="localhost",
@@ -25,7 +22,7 @@ def test_initializes_all_sub_apis(self, mock_openvasd_client_class):
2522
client_cert_paths=("/path/to/client.pem", "/path/to/key.pem"),
2623
)
2724

28-
mock_openvasd_client_class.assert_called_once_with(
25+
mock_crate_openvasd_client.assert_called_once_with(
2926
host_name="localhost",
3027
port=3000,
3128
api_key="test-key",

0 commit comments

Comments
 (0)