Skip to content

Commit 778d675

Browse files
committed
fix: refactor mysql ssl deployment
1 parent 5128bbb commit 778d675

File tree

4 files changed

+105
-56
lines changed

4 files changed

+105
-56
lines changed

tests/model_registry/rest_api/conftest.py

Lines changed: 29 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
import os
33
from kubernetes.dynamic import DynamicClient
44
import pytest
5-
from tests.model_registry.rest_api.constants import MODEL_REGISTRY_BASE_URI
5+
import copy
6+
from tests.model_registry.rest_api.constants import MODEL_REGISTRY_BASE_URI, MODEL_REGISTER_DATA
67
from tests.model_registry.rest_api.utils import (
78
register_model_rest_api,
89
execute_model_registry_patch_command,
@@ -11,7 +12,11 @@
1112
from ocp_resources.deployment import Deployment
1213
from tests.model_registry.utils import (
1314
get_model_registry_deployment_template_dict,
15+
apply_mysql_args_and_volume_mounts,
16+
add_mysql_certs_volumes_to_deployment,
17+
generate_random_name,
1418
)
19+
1520
from tests.model_registry.constants import (
1621
DB_RESOURCES_NAME,
1722
CA_MOUNT_PATH,
@@ -250,51 +255,20 @@ def patch_mysql_deployment_with_ssl_ca(
250255
"""
251256
CA_CONFIGMAP_NAME = request.param.get("ca_configmap_name", "mysql-ca-configmap")
252257
CA_MOUNT_PATH = request.param.get("ca_mount_path", "/etc/mysql/ssl")
253-
deployment = Deployment(
254-
client=admin_client,
255-
name=model_registry_db_deployment.name,
256-
namespace=model_registry_namespace,
257-
)
258-
deployment.wait_for_condition(condition="Available", status="True")
259-
original_deployment = deployment.instance.to_dict()
260-
spec = original_deployment["spec"]["template"]["spec"]
258+
deployment = model_registry_db_deployment.instance.to_dict()
259+
spec = deployment["spec"]["template"]["spec"]
261260
my_sql_container = next(container for container in spec["containers"] if container["name"] == "mysql")
262261
assert my_sql_container is not None, "Mysql container not found"
263-
mysql_args = list(my_sql_container.get("args", []))
264-
mysql_args.extend([
265-
f"--ssl-ca={CA_MOUNT_PATH}/ca/ca-bundle.crt",
266-
f"--ssl-cert={CA_MOUNT_PATH}/server_cert/tls.crt",
267-
f"--ssl-key={CA_MOUNT_PATH}/server_key/tls.key",
268-
])
269-
270-
volumes_mounts = list(my_sql_container.get("volumeMounts", []))
271-
volumes_mounts.extend([
272-
{"name": CA_CONFIGMAP_NAME, "mountPath": f"{CA_MOUNT_PATH}/ca", "readOnly": True},
273-
{
274-
"name": "mysql-server-cert",
275-
"mountPath": f"{CA_MOUNT_PATH}/server_cert",
276-
"readOnly": True,
277-
},
278-
{
279-
"name": "mysql-server-key",
280-
"mountPath": f"{CA_MOUNT_PATH}/server_key",
281-
"readOnly": True,
282-
},
283-
])
284262

285-
my_sql_container["args"] = mysql_args
286-
my_sql_container["volumeMounts"] = volumes_mounts
287-
volumes = list(spec["volumes"])
288-
volumes.extend([
289-
{"name": CA_CONFIGMAP_NAME, "configMap": {"name": CA_CONFIGMAP_NAME}},
290-
{"name": "mysql-server-cert", "secret": {"secretName": "mysql-server-cert"}}, # pragma: allowlist secret
291-
{"name": "mysql-server-key", "secret": {"secretName": "mysql-server-key"}}, # pragma: allowlist secret
292-
])
263+
my_sql_container = apply_mysql_args_and_volume_mounts(
264+
my_sql_container=my_sql_container, ca_configmap_name=CA_CONFIGMAP_NAME, ca_mount_path=CA_MOUNT_PATH
265+
)
266+
volumes = add_mysql_certs_volumes_to_deployment(spec=spec, ca_configmap_name=CA_CONFIGMAP_NAME)
293267

294268
patch = {"spec": {"template": {"spec": {"volumes": volumes, "containers": [my_sql_container]}}}}
295-
with ResourceEditor(patches={deployment: patch}):
296-
deployment.wait_for_condition(condition="Available", status="True")
297-
yield deployment
269+
with ResourceEditor(patches={model_registry_db_deployment: patch}):
270+
model_registry_db_deployment.wait_for_condition(condition="Available", status="True")
271+
yield model_registry_db_deployment
298272

299273

300274
@pytest.fixture(scope="class")
@@ -359,3 +333,17 @@ def mysql_ssl_secrets(
359333
"server_cert_secret": server_cert_secret,
360334
"server_key_secret": server_key_secret,
361335
}
336+
337+
338+
@pytest.fixture(scope="function")
339+
def model_data_for_test() -> Generator[dict[str, Any], None, None]:
340+
"""
341+
Generates a model data for the test.
342+
343+
Returns:
344+
dict[str, Any]: The model data for the test
345+
"""
346+
model_name = generate_random_name(prefix="model-rest-api")
347+
model_data = copy.deepcopy(MODEL_REGISTER_DATA)
348+
model_data["register_model_data"]["name"] = model_name
349+
yield model_data

tests/model_registry/rest_api/test_model_registry_secure_db.py

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
import pytest
22
import requests
3-
from typing import Self
4-
import copy
3+
from typing import Self, Any
54
from pytest_testconfig import config as py_config
65
from tests.model_registry.rest_api.utils import register_model_rest_api, validate_resource_attributes
7-
from tests.model_registry.rest_api.constants import MODEL_REGISTER_DATA
8-
from tests.model_registry.utils import generate_random_name
96
from tests.model_registry.constants import CA_MOUNT_PATH
107
from tests.model_registry.utils import get_mr_service_by_label, get_endpoint_from_mr_service
118
from kubernetes.dynamic import DynamicClient
@@ -62,6 +59,7 @@ def test_register_model_with_invalid_ca(
6259
model_registry_rest_headers: dict[str, str],
6360
local_ca_bundle: str,
6461
deploy_secure_mysql_and_mr: ModelRegistry,
62+
model_data_for_test: dict[str, Any],
6563
):
6664
"""
6765
Test that model registration fails with an SSLError when the Model Registry is deployed
@@ -72,15 +70,11 @@ def test_register_model_with_invalid_ca(
7270
)
7371
model_registry_rest_url = get_endpoint_from_mr_service(svc=service, protocol=Protocols.REST)
7472

75-
model_name = generate_random_name(prefix="model-rest-api")
76-
model_data = copy.deepcopy(MODEL_REGISTER_DATA)
77-
model_data["register_model_data"]["name"] = model_name
78-
7973
with pytest.raises(requests.exceptions.SSLError) as exc_info:
8074
register_model_rest_api(
8175
model_registry_rest_url=f"https://{model_registry_rest_url}",
8276
model_registry_rest_headers=model_registry_rest_headers,
83-
data_dict=model_data,
77+
data_dict=model_data_for_test,
8478
verify=local_ca_bundle,
8579
)
8680
assert "SSLError" in str(exc_info.value), (
@@ -109,24 +103,22 @@ def test_register_model_with_valid_ca(
109103
model_registry_rest_headers: dict[str, str],
110104
local_ca_bundle: str,
111105
deploy_secure_mysql_and_mr: ModelRegistry,
106+
model_data_for_test: dict[str, Any],
112107
):
113108
service = get_mr_service_by_label(
114109
client=admin_client, namespace_name=model_registry_namespace, mr_instance=deploy_secure_mysql_and_mr
115110
)
116111
model_registry_rest_url = get_endpoint_from_mr_service(svc=service, protocol=Protocols.REST)
117-
model_name = generate_random_name(prefix="model-rest-api")
118-
model_data = copy.deepcopy(MODEL_REGISTER_DATA)
119-
model_data["register_model_data"]["name"] = model_name
120112

121113
result = register_model_rest_api(
122114
model_registry_rest_url=f"https://{model_registry_rest_url}",
123115
model_registry_rest_headers=model_registry_rest_headers,
124-
data_dict=model_data,
116+
data_dict=model_data_for_test,
125117
verify=local_ca_bundle,
126118
)
127119
assert result["register_model"].get("id"), "Model registration failed with secure DB connection."
128120
validate_resource_attributes(
129-
expected_params=model_data["register_model_data"],
121+
expected_params=model_data_for_test["register_model_data"],
130122
actual_resource_data=result["register_model"],
131123
resource_name="register_model",
132124
)

tests/model_registry/utils.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,3 +261,72 @@ def generate_random_name(prefix: str = "", length: int = 8) -> str:
261261

262262
def generate_namespace_name(file_path: str) -> str:
263263
return (file_path.removesuffix(".py").replace("/", "-").replace("_", "-"))[-63:].split("-", 1)[-1]
264+
265+
266+
def add_mysql_certs_volumes_to_deployment(
267+
spec: dict[str, Any],
268+
ca_configmap_name: str,
269+
) -> list[dict[str, Any]]:
270+
"""
271+
Adds the MySQL certs volumes to the deployment.
272+
273+
Args:
274+
spec: The spec of the deployment
275+
ca_configmap_name: The name of the CA configmap
276+
277+
Returns:
278+
The volumes with the MySQL certs volumes added
279+
"""
280+
281+
volumes = list(spec["volumes"])
282+
volumes.extend([
283+
{"name": ca_configmap_name, "configMap": {"name": ca_configmap_name}},
284+
{"name": "mysql-server-cert", "secret": {"secretName": "mysql-server-cert"}}, # pragma: allowlist secret
285+
{"name": "mysql-server-key", "secret": {"secretName": "mysql-server-key"}}, # pragma: allowlist secret
286+
])
287+
288+
return volumes
289+
290+
291+
def apply_mysql_args_and_volume_mounts(
292+
my_sql_container: dict[str, Any],
293+
ca_configmap_name: str,
294+
ca_mount_path: str,
295+
) -> dict[str, Any]:
296+
"""
297+
Applies the MySQL args and volume mounts to the MySQL container.
298+
299+
Args:
300+
my_sql_container: The MySQL container
301+
ca_configmap_name: The name of the CA configmap
302+
ca_mount_path: The mount path of the CA
303+
304+
Returns:
305+
The MySQL container with the MySQL args and volume mounts applied
306+
"""
307+
308+
mysql_args = list(my_sql_container.get("args", []))
309+
mysql_args.extend([
310+
f"--ssl-ca={ca_mount_path}/ca/ca-bundle.crt",
311+
f"--ssl-cert={ca_mount_path}/server_cert/tls.crt",
312+
f"--ssl-key={ca_mount_path}/server_key/tls.key",
313+
])
314+
315+
volumes_mounts = list(my_sql_container.get("volumeMounts", []))
316+
volumes_mounts.extend([
317+
{"name": ca_configmap_name, "mountPath": f"{ca_mount_path}/ca", "readOnly": True},
318+
{
319+
"name": "mysql-server-cert",
320+
"mountPath": f"{ca_mount_path}/server_cert",
321+
"readOnly": True,
322+
},
323+
{
324+
"name": "mysql-server-key",
325+
"mountPath": f"{ca_mount_path}/server_key",
326+
"readOnly": True,
327+
},
328+
])
329+
330+
my_sql_container["args"] = mysql_args
331+
my_sql_container["volumeMounts"] = volumes_mounts
332+
return my_sql_container

utilities/certificates_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ def create_ca_bundle_with_router_cert(
149149
Creates a CA bundle file by fetching the CA bundle from a ConfigMap and appending the router CA from a Secret.
150150
151151
Args:
152-
client: The admin client to get the CA bundle from a ConfigMap and append the router CA from a Secret.
152+
client: The client to get the CA bundle from a ConfigMap and append the router CA from a Secret.
153153
namespace: The namespace of the ConfigMap and Secret.
154154
ca_bundle_path: The path to the CA bundle file.
155155
cert_name: The name of the certificate in the ConfigMap.

0 commit comments

Comments
 (0)