|
| 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 | + ) |
0 commit comments