|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import pytest |
| 4 | +import requests |
| 5 | +from kubernetes.dynamic import DynamicClient |
| 6 | +from ocp_resources.service_account import ServiceAccount |
| 7 | +from simple_logger.logger import get_logger |
| 8 | + |
| 9 | +from tests.model_serving.model_server.maas_billing.maas_subscription.utils import ( |
| 10 | + chat_payload_for_url, |
| 11 | + create_maas_subscription, |
| 12 | + poll_expected_status, |
| 13 | +) |
| 14 | +from tests.model_serving.model_server.maas_billing.utils import build_maas_headers |
| 15 | +from utilities.infra import create_inference_token, login_with_user_password |
| 16 | +from utilities.resources.maa_s_auth_policy import MaaSAuthPolicy |
| 17 | + |
| 18 | +LOGGER = get_logger(name=__name__) |
| 19 | + |
| 20 | +MAAS_SUBSCRIPTION_HEADER = "x-maas-subscription" |
| 21 | + |
| 22 | + |
| 23 | +@pytest.mark.usefixtures( |
| 24 | + "maas_unprivileged_model_namespace", |
| 25 | + "maas_controller_enabled_latest", |
| 26 | + "maas_gateway_api", |
| 27 | + "maas_api_gateway_reachable", |
| 28 | + "maas_inference_service_tinyllama_free", |
| 29 | + "maas_model_tinyllama_free", |
| 30 | + "maas_auth_policy_tinyllama_free", |
| 31 | + "maas_subscription_tinyllama_free", |
| 32 | +) |
| 33 | +class TestMultipleSubscriptionsPerModel: |
| 34 | + """ |
| 35 | + Validates behavior when multiple subscriptions exist for the same model. |
| 36 | + """ |
| 37 | + |
| 38 | + @pytest.mark.sanity |
| 39 | + @pytest.mark.parametrize("ocp_token_for_actor", [{"type": "free"}], indirect=True) |
| 40 | + def test_user_in_one_of_two_subscriptions_can_access_model( |
| 41 | + self, |
| 42 | + request_session_http: requests.Session, |
| 43 | + admin_client: DynamicClient, |
| 44 | + maas_free_group: str, |
| 45 | + maas_model_tinyllama_free, |
| 46 | + model_url_tinyllama_free: str, |
| 47 | + ocp_token_for_actor: str, |
| 48 | + maas_subscription_tinyllama_free, |
| 49 | + ) -> None: |
| 50 | + """ |
| 51 | + Create a second subscription for a different group the user is NOT in. |
| 52 | + User should still get 200 when explicitly selecting the correct subscription. |
| 53 | + """ |
| 54 | + assert maas_free_group, "maas_free_group fixture returned empty group name" |
| 55 | + |
| 56 | + with create_maas_subscription( |
| 57 | + admin_client=admin_client, |
| 58 | + subscription_name="extra-subscription", |
| 59 | + owner_group_name="nonexistent-group-xyz", |
| 60 | + model_name=maas_model_tinyllama_free.name, |
| 61 | + tokens_per_minute=999, |
| 62 | + window="1m", |
| 63 | + priority=0, |
| 64 | + teardown=True, |
| 65 | + wait_for_resource=True, |
| 66 | + ) as extra_subscription: |
| 67 | + extra_subscription.wait_for_condition(condition="Ready", status="True", timeout=300) |
| 68 | + |
| 69 | + headers = build_maas_headers(token=ocp_token_for_actor) |
| 70 | + payload = chat_payload_for_url(model_url=model_url_tinyllama_free) |
| 71 | + |
| 72 | + explicit_headers = dict(headers) |
| 73 | + explicit_headers[MAAS_SUBSCRIPTION_HEADER] = maas_subscription_tinyllama_free.name |
| 74 | + |
| 75 | + LOGGER.info( |
| 76 | + "Polling for 200 with explicit subscription selection: " |
| 77 | + f"subscription={maas_subscription_tinyllama_free.name}" |
| 78 | + ) |
| 79 | + |
| 80 | + response = poll_expected_status( |
| 81 | + request_session_http=request_session_http, |
| 82 | + model_url=model_url_tinyllama_free, |
| 83 | + headers=explicit_headers, |
| 84 | + payload=payload, |
| 85 | + expected_statuses={200}, |
| 86 | + ) |
| 87 | + |
| 88 | + assert response.status_code == 200, ( |
| 89 | + f"Expected 200 after adding second subscription, got {response.status_code}: " |
| 90 | + f"{(response.text or '')[:200]}" |
| 91 | + ) |
| 92 | + |
| 93 | + @pytest.mark.sanity |
| 94 | + @pytest.mark.parametrize("ocp_token_for_actor", [{"type": "free"}], indirect=True) |
| 95 | + def test_high_priority_subscription_allows_access_when_explicitly_selected( |
| 96 | + self, |
| 97 | + request_session_http: requests.Session, |
| 98 | + admin_client: DynamicClient, |
| 99 | + maas_free_group: str, |
| 100 | + maas_model_tinyllama_free, |
| 101 | + model_url_tinyllama_free: str, |
| 102 | + ocp_token_for_actor: str, |
| 103 | + maas_subscription_tinyllama_free, |
| 104 | + ) -> None: |
| 105 | + """ |
| 106 | + Create a second (higher priority) subscription for the same group + model. |
| 107 | + User should get 200 when explicitly selecting the high-priority subscription. |
| 108 | + """ |
| 109 | + assert maas_free_group, "maas_free_group fixture returned empty group name" |
| 110 | + _ = maas_subscription_tinyllama_free |
| 111 | + |
| 112 | + with create_maas_subscription( |
| 113 | + admin_client=admin_client, |
| 114 | + subscription_name="high-tier-subscription", |
| 115 | + owner_group_name=maas_free_group, |
| 116 | + model_name=maas_model_tinyllama_free.name, |
| 117 | + tokens_per_minute=9999, |
| 118 | + window="1m", |
| 119 | + priority=10, |
| 120 | + teardown=True, |
| 121 | + wait_for_resource=True, |
| 122 | + ) as high_tier_subscription: |
| 123 | + high_tier_subscription.wait_for_condition(condition="Ready", status="True", timeout=300) |
| 124 | + |
| 125 | + headers = build_maas_headers(token=ocp_token_for_actor) |
| 126 | + payload = chat_payload_for_url(model_url=model_url_tinyllama_free) |
| 127 | + |
| 128 | + explicit_headers = dict(headers) |
| 129 | + explicit_headers[MAAS_SUBSCRIPTION_HEADER] = high_tier_subscription.name |
| 130 | + |
| 131 | + response = poll_expected_status( |
| 132 | + request_session_http=request_session_http, |
| 133 | + model_url=model_url_tinyllama_free, |
| 134 | + headers=explicit_headers, |
| 135 | + payload=payload, |
| 136 | + expected_statuses={200}, |
| 137 | + ) |
| 138 | + |
| 139 | + assert response.status_code == 200, ( |
| 140 | + f"Expected 200 when selecting high-priority subscription '{high_tier_subscription.name}', " |
| 141 | + f"got {response.status_code}: {(response.text or '')[:200]}" |
| 142 | + ) |
| 143 | + |
| 144 | + @pytest.mark.sanity |
| 145 | + def test_service_account_cannot_use_subscription_it_does_not_belong_to( |
| 146 | + self, |
| 147 | + request_session_http: requests.Session, |
| 148 | + admin_client: DynamicClient, |
| 149 | + maas_api_server_url: str, |
| 150 | + original_user: str, |
| 151 | + maas_premium_group: str, |
| 152 | + maas_model_tinyllama_free, |
| 153 | + model_url_tinyllama_free: str, |
| 154 | + maas_unprivileged_model_namespace, |
| 155 | + ) -> None: |
| 156 | + """ |
| 157 | + A service account explicitly selecting a subscription it does not belong to |
| 158 | + should be denied. |
| 159 | + """ |
| 160 | + service_account_name = "test-service-account" |
| 161 | + |
| 162 | + login_ok = login_with_user_password(api_address=maas_api_server_url, user=original_user) |
| 163 | + assert login_ok, f"Failed to login as original_user={original_user}" |
| 164 | + |
| 165 | + applications_namespace = maas_unprivileged_model_namespace.name |
| 166 | + assert applications_namespace, "applications_namespace name is empty" |
| 167 | + |
| 168 | + with ( |
| 169 | + MaaSAuthPolicy( |
| 170 | + client=admin_client, |
| 171 | + name="service-account-access-policy", |
| 172 | + namespace=applications_namespace, |
| 173 | + model_refs=[maas_model_tinyllama_free.name], |
| 174 | + subjects={"groups": [{"name": f"system:serviceaccounts:{applications_namespace}"}]}, |
| 175 | + teardown=True, |
| 176 | + wait_for_resource=True, |
| 177 | + ) as service_account_auth_policy, |
| 178 | + create_maas_subscription( |
| 179 | + admin_client=admin_client, |
| 180 | + subscription_name="premium-subscription", |
| 181 | + owner_group_name=maas_premium_group, |
| 182 | + model_name=maas_model_tinyllama_free.name, |
| 183 | + tokens_per_minute=500, |
| 184 | + window="1m", |
| 185 | + priority=0, |
| 186 | + teardown=True, |
| 187 | + wait_for_resource=True, |
| 188 | + ) as premium_subscription, |
| 189 | + ServiceAccount( |
| 190 | + client=admin_client, |
| 191 | + namespace=applications_namespace, |
| 192 | + name=service_account_name, |
| 193 | + teardown=True, |
| 194 | + ) as service_account, |
| 195 | + ): |
| 196 | + service_account_auth_policy.wait_for_condition(condition="Ready", status="True", timeout=300) |
| 197 | + premium_subscription.wait_for_condition(condition="Ready", status="True", timeout=300) |
| 198 | + service_account.wait(timeout=60) |
| 199 | + |
| 200 | + service_account_token = create_inference_token(model_service_account=service_account) |
| 201 | + headers = build_maas_headers(token=service_account_token) |
| 202 | + headers[MAAS_SUBSCRIPTION_HEADER] = premium_subscription.name |
| 203 | + |
| 204 | + payload = chat_payload_for_url(model_url=model_url_tinyllama_free) |
| 205 | + |
| 206 | + response = poll_expected_status( |
| 207 | + request_session_http=request_session_http, |
| 208 | + model_url=model_url_tinyllama_free, |
| 209 | + headers=headers, |
| 210 | + payload=payload, |
| 211 | + expected_statuses={403, 429}, |
| 212 | + ) |
| 213 | + |
| 214 | + assert response.status_code in {403, 429}, ( |
| 215 | + f"Expected 403/429 when service account selects a subscription it doesn't belong to, " |
| 216 | + f"got {response.status_code}: {(response.text or '')[:200]}" |
| 217 | + ) |
0 commit comments