Skip to content

Commit 1dd29d8

Browse files
authored
Merge branch 'main' into gpu-skip
2 parents 236b97a + 74dc788 commit 1dd29d8

File tree

4 files changed

+152
-22
lines changed

4 files changed

+152
-22
lines changed

tests/model_registry/conftest.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,42 @@ def sa_namespace(request: pytest.FixtureRequest, admin_client: DynamicClient) ->
414414
yield ns
415415

416416

417+
@pytest.fixture()
418+
def login_as_test_user(
419+
is_byoidc: bool, api_server_url: str, original_user: str, test_idp_user
420+
) -> Generator[None, None, None]:
421+
"""
422+
Fixture to log in as a test user and restore original user after test.
423+
424+
This fixture is used for RBAC tests to switch context to a non-admin test user.
425+
Used by both model registry and model catalog RBAC tests.
426+
"""
427+
if is_byoidc:
428+
yield
429+
else:
430+
from utilities.user_utils import UserTestSession
431+
432+
if isinstance(test_idp_user, UserTestSession):
433+
username = test_idp_user.username
434+
password = test_idp_user.password
435+
else:
436+
username = test_idp_user
437+
password = None
438+
439+
LOGGER.info(f"Logging in as {username}")
440+
login_with_user_password(
441+
api_address=api_server_url,
442+
user=username,
443+
password=password,
444+
)
445+
yield
446+
LOGGER.info(f"Logging in as {original_user}")
447+
login_with_user_password(
448+
api_address=api_server_url,
449+
user=original_user,
450+
)
451+
452+
417453
@pytest.fixture(scope="class")
418454
def service_account(admin_client: DynamicClient, sa_namespace: Namespace) -> Generator[Any, None, None]:
419455
"""
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
"""
2+
Test suite for verifying RBAC permissions for Model Catalog ConfigMaps.
3+
"""
4+
5+
import pytest
6+
from simple_logger.logger import get_logger
7+
8+
from kubernetes.dynamic import DynamicClient
9+
from kubernetes.client.rest import ApiException
10+
from ocp_resources.config_map import ConfigMap
11+
from ocp_resources.resource import get_client
12+
13+
from tests.model_registry.constants import DEFAULT_CUSTOM_MODEL_CATALOG, DEFAULT_MODEL_CATALOG_CM
14+
15+
LOGGER = get_logger(name=__name__)
16+
17+
pytestmark = [
18+
pytest.mark.usefixtures(
19+
"updated_dsc_component_state_scope_session",
20+
"model_registry_namespace",
21+
)
22+
]
23+
24+
25+
@pytest.mark.skip_must_gather
26+
class TestCatalogRBAC:
27+
"""Test suite for catalog ConfigMap RBAC"""
28+
29+
@pytest.mark.smoke
30+
@pytest.mark.pre_upgrade
31+
@pytest.mark.post_upgrade
32+
@pytest.mark.install
33+
@pytest.mark.parametrize("configmap_name", [DEFAULT_MODEL_CATALOG_CM, DEFAULT_CUSTOM_MODEL_CATALOG])
34+
def test_admin_can_read_catalog_configmaps(
35+
self,
36+
admin_client: DynamicClient,
37+
model_registry_namespace: str,
38+
configmap_name: str,
39+
):
40+
"""
41+
RHOAIENG-41850: Verify that admin users can read both catalog ConfigMaps.
42+
43+
Admins should have:
44+
- get/watch on model-catalog-default-sources (read-only)
45+
- get/watch/update/patch on model-catalog-sources (read/write)
46+
47+
Note: Admin write access to model-catalog-sources is already tested by existing tests
48+
(test_custom_model_catalog.py, test_catalog_source_merge.py) which use admin_client
49+
to successfully update ConfigMaps via ResourceEditor.
50+
"""
51+
catalog_cm = ConfigMap(
52+
name=configmap_name,
53+
namespace=model_registry_namespace,
54+
client=admin_client,
55+
)
56+
57+
assert catalog_cm.exists, f"ConfigMap '{configmap_name}' not found in namespace '{model_registry_namespace}'"
58+
59+
data = catalog_cm.instance.data
60+
assert data is not None, f"Admin should be able to read ConfigMap '{configmap_name}' data"
61+
62+
sources_yaml = data.get("sources.yaml")
63+
assert sources_yaml is not None, f"ConfigMap '{configmap_name}' should contain 'sources.yaml' key"
64+
65+
LOGGER.info(f"Admin successfully read ConfigMap '{configmap_name}'")
66+
67+
@pytest.mark.smoke
68+
@pytest.mark.parametrize("configmap_name", [DEFAULT_MODEL_CATALOG_CM, DEFAULT_CUSTOM_MODEL_CATALOG])
69+
def test_non_admin_cannot_access_catalog_configmaps(
70+
self,
71+
is_byoidc: bool,
72+
model_registry_namespace: str,
73+
user_credentials_rbac: dict[str, str],
74+
login_as_test_user: None,
75+
configmap_name: str,
76+
):
77+
"""
78+
RHOAIENG-41850: Verify that non-admin users cannot access catalog ConfigMaps,
79+
receiving a 403 Forbidden error.
80+
"""
81+
if is_byoidc:
82+
pytest.skip(reason="BYOIDC test users may have pre-configured group memberships")
83+
84+
# get_client() uses the current kubeconfig context (set by login_as_test_user fixture)
85+
user_client = get_client()
86+
87+
with pytest.raises(ApiException) as exc_info:
88+
catalog_cm = ConfigMap(
89+
name=configmap_name,
90+
namespace=model_registry_namespace,
91+
client=user_client,
92+
)
93+
_ = catalog_cm.instance # Access the ConfigMap instance to trigger the API call
94+
95+
assert exc_info.value.status == 403, (
96+
f"Expected HTTP 403 Forbidden for non-admin user accessing '{configmap_name}', "
97+
f"but got {exc_info.value.status}: {exc_info.value.reason}"
98+
)
99+
LOGGER.info(
100+
f"Non-admin user '{user_credentials_rbac['username']}' correctly denied access "
101+
f"to ConfigMap '{configmap_name}'"
102+
)

tests/model_registry/model_catalog/huggingface/test_huggingface_negative.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,20 @@ class TestHuggingFaceNegative:
115115
"failed to expand model patterns: no models found",
116116
id="test_hf_source_non_existent_allowed_organization",
117117
),
118+
pytest.param(
119+
"""
120+
catalogs:
121+
- name: HuggingFace Hub
122+
id: error_catalog
123+
type: hf
124+
enabled: true
125+
includedModels:
126+
- 'microsoft/phi-3-abc-random'
127+
""",
128+
"Failed to fetch some models, ensure models exist and are accessible with given credentials. "
129+
"Failed models: [microsoft/phi-3-abc-random]",
130+
id="test_hf_bad_model_name",
131+
),
118132
],
119133
indirect=["updated_catalog_config_map_scope_function"],
120134
)

tests/model_registry/model_registry/rbac/conftest.py

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020

2121
from tests.model_registry.model_registry.rbac.utils import create_role_binding
2222
from utilities.user_utils import UserTestSession
23-
from utilities.infra import login_with_user_password
2423
from tests.model_registry.model_registry.rbac.group_utils import create_group
2524
from tests.model_registry.constants import (
2625
MR_INSTANCE_NAME,
@@ -238,27 +237,6 @@ def model_registry_instance_parametrized(
238237
yield model_registry_instances
239238

240239

241-
@pytest.fixture()
242-
def login_as_test_user(
243-
is_byoidc: bool, api_server_url: str, original_user: str, test_idp_user: UserTestSession
244-
) -> Generator[None, None, None]:
245-
if is_byoidc:
246-
yield
247-
else:
248-
LOGGER.info(f"Logging in as {test_idp_user.username}")
249-
login_with_user_password(
250-
api_address=api_server_url,
251-
user=test_idp_user.username,
252-
password=test_idp_user.password,
253-
)
254-
yield
255-
LOGGER.info(f"Logging in as {original_user}")
256-
login_with_user_password(
257-
api_address=api_server_url,
258-
user=original_user,
259-
)
260-
261-
262240
@pytest.fixture()
263241
def skip_test_on_byoidc(is_byoidc: bool) -> None:
264242
if is_byoidc:

0 commit comments

Comments
 (0)