Skip to content

Commit abc421b

Browse files
authored
patch: Add option to disable ssl.VERIFY_X509_STRICT (#350)
Since Python 3.13, ssl.VERIFY_X509_STRICT is enabled by default. Old Kubernetes clusters might have certificates that do not (yet) comply with that. Add a (custom) configuration option to disable this flag.
1 parent 1504929 commit abc421b

6 files changed

+53
-0
lines changed

kubernetes_asyncio/client/configuration.py

+8
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,14 @@ def __init__(self, host=None,
177177
Set this to false to skip verifying SSL certificate when calling API
178178
from https server.
179179
"""
180+
self.disable_strict_ssl_verification = False
181+
"""Set to true, to accept certificates violate X509 strict certificate
182+
verification requirements, like missing the following extensions:
183+
- X509v3 Subject Key Identifier
184+
- X509v3 Authority Key Identifier
185+
- X509v3 Subject Alternative Name
186+
(It is implemented by removing ssl.VERIFY_X509_STRICT from SSLContext.verify_flags)
187+
"""
180188
self.ssl_ca_cert = ssl_ca_cert
181189
"""Set this to customize the certificate file to verify the peer.
182190
"""

kubernetes_asyncio/client/rest.py

+2
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ def __init__(self, configuration, pools_size=4, maxsize=None):
6161
if not configuration.verify_ssl:
6262
ssl_context.check_hostname = False
6363
ssl_context.verify_mode = ssl.CERT_NONE
64+
if configuration.disable_strict_ssl_verification:
65+
ssl_context.verify_flags &= ~ssl.VERIFY_X509_STRICT
6466

6567
connector = aiohttp.TCPConnector(
6668
limit=maxsize,

kubernetes_asyncio/client/test_rest.py

+8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import asyncio
2+
import ssl
23
import unittest
34
from unittest.mock import AsyncMock
45
import aiohttp
@@ -30,3 +31,10 @@ async def test_rest_request_timeout(self):
3031
timeout=expected_timeout_arg,
3132
headers={"Content-Type": "application/json"}
3233
)
34+
35+
async def test_disable_ssl_verification(self):
36+
configuration = Configuration()
37+
configuration.disable_strict_ssl_verification = True
38+
rest_api = RESTClientObject(configuration=configuration)
39+
ssl_context = rest_api.pool_manager._connector._ssl
40+
self.assertEqual(ssl_context.verify_flags & ssl.VERIFY_X509_STRICT, 0)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
diff --git a/kubernetes_asyncio/client/configuration.py b/kubernetes_asyncio/client/configuration.py
2+
index d0dd9f9e..facc9173 100644
3+
--- a/kubernetes_asyncio/client/configuration.py
4+
+++ b/kubernetes_asyncio/client/configuration.py
5+
@@ -177,6 +177,14 @@ conf = client.Configuration(
6+
Set this to false to skip verifying SSL certificate when calling API
7+
from https server.
8+
"""
9+
+ self.disable_strict_ssl_verification = False
10+
+ """Set to true, to accept certificates violate X509 strict certificate
11+
+ verification requirements, like missing the following extensions:
12+
+ - X509v3 Subject Key Identifier
13+
+ - X509v3 Authority Key Identifier
14+
+ - X509v3 Subject Alternative Name
15+
+ (It is implemented by removing ssl.VERIFY_X509_STRICT from SSLContext.verify_flags)
16+
+ """
17+
self.ssl_ca_cert = ssl_ca_cert
18+
"""Set this to customize the certificate file to verify the peer.
19+
"""
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
diff --git a/kubernetes_asyncio/client/rest.py b/kubernetes_asyncio/client/rest.py
2+
index eca41107..ee30e26a 100644
3+
--- a/kubernetes_asyncio/client/rest.py
4+
+++ b/kubernetes_asyncio/client/rest.py
5+
@@ -61,6 +61,8 @@ class RESTClientObject(object):
6+
if not configuration.verify_ssl:
7+
ssl_context.check_hostname = False
8+
ssl_context.verify_mode = ssl.CERT_NONE
9+
+ if configuration.disable_strict_ssl_verification:
10+
+ ssl_context.verify_flags &= ~ssl.VERIFY_X509_STRICT
11+
12+
connector = aiohttp.TCPConnector(
13+
limit=maxsize,

scripts/update-client.sh

+3
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ patch "${CLIENT_ROOT}/client/rest.py" "${SCRIPT_ROOT}/rest_client_patch_read_buf
7373
echo ">>> fix generated rest client and configuration to support customer server hostname TLS verification..."
7474
patch "${CLIENT_ROOT}/client/rest.py" "${SCRIPT_ROOT}/rest_client_server_hostname_patch.diff"
7575
patch "${CLIENT_ROOT}/client/configuration.py" "${SCRIPT_ROOT}/client_configuration_tls_server_name_patch.diff"
76+
echo ">>> fix generated rest client and configuration to support disabling strict TLS verification..."
77+
patch "${CLIENT_ROOT}/client/rest.py" "${SCRIPT_ROOT}/rest_client_disable_ssl_strict_verification_patch.diff"
78+
patch "${CLIENT_ROOT}/client/configuration.py" "${SCRIPT_ROOT}/client_configuration_disable_ssl_strict_verification_patch.diff"
7679
echo ">>> fix generated rest client by handling timeout correctly..."
7780
patch -R "${CLIENT_ROOT}/client/rest.py" "${SCRIPT_ROOT}/rest_client_timeout.diff"
7881

0 commit comments

Comments
 (0)