@@ -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 )
0 commit comments