Skip to content

Commit 0b2cac9

Browse files
committed
dont set ssl cert if it is dns
Signed-off-by: Javan Lacerda <javanlacerda@google.com>
1 parent b888207 commit 0b2cac9

2 files changed

Lines changed: 104 additions & 1 deletion

File tree

src/clusterfuzz/_internal/k8s/service.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
"""Kubernetes batch client."""
1515
import base64
1616
import collections
17+
import ipaddress
1718
import os
1819
import typing
1920
import uuid
@@ -206,7 +207,16 @@ def _load_gke_credentials(self):
206207

207208
configuration = k8s_client.Configuration()
208209
configuration.host = f'https://{endpoint}'
209-
configuration.ssl_ca_cert = ca_cert_path
210+
211+
try:
212+
ipaddress.ip_address(endpoint)
213+
configuration.ssl_ca_cert = ca_cert_path
214+
except ValueError:
215+
# If the endpoint is a hostname, we assume it's using a public CA or
216+
# the system trust store should be used.
217+
logs.info(f'Endpoint {endpoint} is a hostname. '
218+
'Skipping custom CA configuration.')
219+
210220
configuration.verify_ssl = True
211221

212222
def get_token(creds):
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
# Copyright 2026 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
"""Tests for KubernetesService credential loading."""
15+
16+
import os
17+
import unittest
18+
from unittest import mock
19+
20+
from clusterfuzz._internal.k8s import service
21+
from clusterfuzz._internal.tests.test_libs import helpers
22+
from clusterfuzz._internal.tests.test_libs import test_utils
23+
24+
25+
class KubernetesCredentialsTest(unittest.TestCase):
26+
"""Tests for KubernetesService credential loading."""
27+
28+
def setUp(self):
29+
helpers.patch(self, [
30+
'clusterfuzz._internal.system.environment.get_value',
31+
'google.auth.default',
32+
'googleapiclient.discovery.build',
33+
'kubernetes.client.Configuration',
34+
'kubernetes.config.load_kube_config',
35+
])
36+
self.mock.get_value.return_value = 'test-project'
37+
creds = mock.Mock()
38+
creds.token = 'test-token'
39+
self.mock.default.return_value = (creds, 'test-project')
40+
41+
self.mock_discovery = self.mock.build.return_value
42+
self.mock_clusters = self.mock_discovery.projects.return_value.locations.return_value.clusters.return_value
43+
44+
self.mock_config_instance = self.mock.Configuration.return_value
45+
46+
os.environ['BOT_DIR'] = '/tmp'
47+
48+
def test_load_gke_credentials_ip_endpoint(self):
49+
"""Test loading credentials with an IP endpoint (should set ssl_ca_cert)."""
50+
self.mock_clusters.list.return_value.execute.return_value = {
51+
'clusters': [{
52+
'name': 'clusterfuzz-cronjobs-gke',
53+
'endpoint': '1.2.3.4',
54+
'masterAuth': {
55+
'clusterCaCertificate': 'dGVzdA==' # base64 "test"
56+
}
57+
}]
58+
}
59+
60+
# Bypass __init__ logic to call _load_gke_credentials directly
61+
with mock.patch.object(service.KubernetesService, '__init__', return_value=None):
62+
kube_service = service.KubernetesService()
63+
64+
kube_service._load_gke_credentials()
65+
66+
self.assertEqual(self.mock_config_instance.host, 'https://1.2.3.4')
67+
self.assertIsNotNone(self.mock_config_instance.ssl_ca_cert)
68+
self.assertTrue(self.mock_config_instance.verify_ssl)
69+
70+
def test_load_gke_credentials_hostname_endpoint(self):
71+
"""Test loading credentials with a hostname endpoint (should skip ssl_ca_cert)."""
72+
self.mock_clusters.list.return_value.execute.return_value = {
73+
'clusters': [{
74+
'name': 'clusterfuzz-cronjobs-gke',
75+
'endpoint': 'example.com',
76+
'masterAuth': {
77+
'clusterCaCertificate': 'dGVzdA=='
78+
}
79+
}]
80+
}
81+
82+
# Bypass __init__ logic to call _load_gke_credentials directly
83+
with mock.patch.object(service.KubernetesService, '__init__', return_value=None):
84+
kube_service = service.KubernetesService()
85+
86+
# Reset mock to ensure we capture the specific call
87+
self.mock_config_instance.ssl_ca_cert = None
88+
89+
kube_service._load_gke_credentials()
90+
91+
self.assertEqual(self.mock_config_instance.host, 'https://example.com')
92+
self.assertIsNone(self.mock_config_instance.ssl_ca_cert)
93+
self.assertTrue(self.mock_config_instance.verify_ssl)

0 commit comments

Comments
 (0)