Skip to content

Commit 66ae0e4

Browse files
committed
Remove tls in secret name variable
1 parent 9ea0ddb commit 66ae0e4

File tree

5 files changed

+20
-34
lines changed

5 files changed

+20
-34
lines changed

app/conftest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def k8s_apps_client_mock():
4444

4545

4646
@pytest.fixture
47-
def k8s_tls_secret_mock():
47+
def k8s_secret_mock():
4848
return kubernetes.client.V1Secret(
4949
type="kubernetes.io/tls",
5050
metadata=kubernetes.client.V1ObjectMeta(name="gateway-tls"),

app/handlers/tests/test_handlers_services.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -176,11 +176,11 @@ def test_kubernetes_resource_type_annotation(
176176
self,
177177
example_cluster_ip_gateway_service_body,
178178
k8s_core_client_mock,
179-
k8s_tls_secret_mock,
179+
k8s_secret_mock,
180180
):
181181
tls_object_name = "gateway-tls"
182182
namespace = "custom-namespace"
183-
k8s_core_client_mock.read_namespaced_secret.return_value = k8s_tls_secret_mock
183+
k8s_core_client_mock.read_namespaced_secret.return_value = k8s_secret_mock
184184

185185
with patch(
186186
"app.handlers.handlers_services.get_ca_cert", wraps=get_ca_cert
@@ -189,7 +189,7 @@ def test_kubernetes_resource_type_annotation(
189189
example_cluster_ip_gateway_service_body, namespace
190190
)
191191

192-
get_ca_cert_mock.assert_called_once_with(k8s_tls_secret_mock)
192+
get_ca_cert_mock.assert_called_once_with(k8s_secret_mock)
193193
k8s_core_client_mock.read_namespaced_secret.assert_called_once_with(
194194
namespace=namespace, name=tls_object_name
195195
)
@@ -258,13 +258,13 @@ def test_kubernetes_resource_with_load_balancer_service_type(
258258
self,
259259
example_load_balancer_gateway_service_body,
260260
k8s_core_client_mock,
261-
k8s_tls_secret_mock,
261+
k8s_secret_mock,
262262
status,
263263
expected,
264264
):
265265
tls_object_name = "gateway-tls"
266266
namespace = "default"
267-
k8s_core_client_mock.read_namespaced_secret.return_value = k8s_tls_secret_mock
267+
k8s_core_client_mock.read_namespaced_secret.return_value = k8s_secret_mock
268268

269269
with patch(
270270
"kopf._cogs.structs.bodies.Body.status",

app/tests/test_crds_resource.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -295,18 +295,18 @@ def test_resource_proxy_get_certificate_authority_cert_without_secret_ref():
295295

296296

297297
def test_resource_proxy_get_certificate_authority_cert_with_secret_ref(
298-
k8s_core_client_mock, k8s_tls_secret_mock
298+
k8s_core_client_mock, k8s_secret_mock
299299
):
300300
proxy = ResourceProxy(
301301
address="proxy.default.cluster.local",
302302
certificate_authority_cert_secret_ref=_KubernetesObjectRef(name="gateway-tls"),
303303
certificate_authority_cert=None,
304304
)
305-
k8s_core_client_mock.read_namespaced_secret.return_value = k8s_tls_secret_mock
305+
k8s_core_client_mock.read_namespaced_secret.return_value = k8s_secret_mock
306306

307307
with patch("app.crds.get_ca_cert", wraps=get_ca_cert) as get_ca_cert_mock:
308308
assert proxy.get_certificate_authority_cert() == VALID_CA_CERT
309-
get_ca_cert_mock.assert_called_once_with(k8s_tls_secret_mock)
309+
get_ca_cert_mock.assert_called_once_with(k8s_secret_mock)
310310

311311

312312
def test_network_resource_spec_to_graphql_arguments(sample_network_resource_object):

app/tests/test_utils_k8s.py

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -78,34 +78,25 @@ def test_reraises_non_404_exceptions(self, k8s_core_client_mock):
7878

7979

8080
class TestGetCACert:
81-
def test_get_ca_cert(self, k8s_tls_secret_mock):
82-
assert get_ca_cert(k8s_tls_secret_mock) == BASE64_OF_VALID_CA_CERT
81+
def test_get_ca_cert(self, k8s_secret_mock):
82+
assert get_ca_cert(k8s_secret_mock) == BASE64_OF_VALID_CA_CERT
8383

84-
def test_get_ca_cert_with_invalid_secret_type(self, k8s_tls_secret_mock):
85-
k8s_tls_secret_mock.type = "kubernetes.io/token"
86-
87-
with pytest.raises(
88-
kopf.PermanentError,
89-
match=r"Kubernetes Secret object: gateway-tls type is invalid.",
90-
):
91-
get_ca_cert(k8s_tls_secret_mock)
92-
93-
def test_get_ca_cert_with_missing_ca_cert(self, k8s_tls_secret_mock):
94-
k8s_tls_secret_mock.data = {}
84+
def test_get_ca_cert_with_missing_ca_cert(self, k8s_secret_mock):
85+
k8s_secret_mock.data = {}
9586

9687
with pytest.raises(
9788
kopf.PermanentError,
9889
match=r"Kubernetes Secret object: gateway-tls is missing ca.crt.",
9990
):
100-
get_ca_cert(k8s_tls_secret_mock)
91+
get_ca_cert(k8s_secret_mock)
10192

102-
def test_get_ca_cert_with_invalid_ca_cert(self, k8s_tls_secret_mock):
103-
k8s_tls_secret_mock.data["ca.crt"] = (
93+
def test_get_ca_cert_with_invalid_ca_cert(self, k8s_secret_mock):
94+
k8s_secret_mock.data["ca.crt"] = (
10495
"LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tIE1JSUZmakNDQTJhZ0F3SUJBZ0lVQk50IC0tLS0tRU5EIENFUlRJRklDQVRFLS0tLS0="
10596
)
10697

10798
with pytest.raises(
10899
kopf.PermanentError,
109100
match=r"Kubernetes Secret object: gateway-tls ca.crt is invalid.",
110101
):
111-
get_ca_cert(k8s_tls_secret_mock)
102+
get_ca_cert(k8s_secret_mock)

app/utils_k8s.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,22 +60,17 @@ def k8s_get_secret(namespace: str, name: str) -> kubernetes.client.V1Secret | No
6060

6161

6262
def get_ca_cert(tls_secret: kubernetes.client.V1Secret) -> str:
63-
tls_secret_name = tls_secret.metadata.name
64-
if tls_secret.type != "kubernetes.io/tls":
65-
raise kopf.PermanentError(
66-
f"Kubernetes Secret object: {tls_secret_name} type is invalid."
67-
)
68-
63+
secret_name = tls_secret.metadata.name
6964
if not (ca_cert := tls_secret.data.get("ca.crt")):
7065
raise kopf.PermanentError(
71-
f"Kubernetes Secret object: {tls_secret_name} is missing ca.crt."
66+
f"Kubernetes Secret object: {secret_name} is missing ca.crt."
7267
)
7368

7469
try:
7570
validate_pem_x509_certificate(base64.b64decode(ca_cert).decode())
7671
except ValueError as ex:
7772
raise kopf.PermanentError(
78-
f"Kubernetes Secret object: {tls_secret_name} ca.crt is invalid."
73+
f"Kubernetes Secret object: {secret_name} ca.crt is invalid."
7974
) from ex
8075

8176
return ca_cert

0 commit comments

Comments
 (0)