|
| 1 | +""" |
| 2 | +Functional tests for the hardcoded LIGHTWELL_READONLY_GROUP_NAME group enforced by |
| 3 | +DomainBasedPermission, scoped specifically to the "lightwell" domain's non-PyPI endpoints |
| 4 | +(Pulp REST API: repository listing, etc.). |
| 5 | +
|
| 6 | +Unlike normal groups (created through CreateDomainView's group_name flow and linked to a |
| 7 | +domain via DomainOrg, which grant unrestricted read+write access), membership in this |
| 8 | +hardcoded group grants read-only (SAFE_METHODS) access to the lightwell domain, independent |
| 9 | +of any DomainOrg association. It does not grant write access, and it does not apply to the |
| 10 | +lightwell domain's PyPI views, which remain gated exclusively by the lightwell-network |
| 11 | +feature check (see test_lightwell_feature_permission.py) -- group membership must not bypass |
| 12 | +that check. |
| 13 | +
|
| 14 | +These follow the pattern used in test_group_based_permissions.py (group setup via gen_group |
| 15 | +/ UsersApi / GroupsUsersApi) and test_lightwell_feature_permission.py (the "lightwell" |
| 16 | +domain/PyPI fixtures). |
| 17 | +
|
| 18 | +NOTE: like test_lightwell_feature_permission.py, this is keyed off the literal domain name |
| 19 | +"lightwell" (see pulp_service.app.authorization.LIGHTWELL_DOMAIN_NAME), so the domain created |
| 20 | +here can't use a random per-test suffix. These tests assume they run against an ephemeral |
| 21 | +Pulp instance where no "lightwell" domain already exists, and are not run concurrently with |
| 22 | +other tests that also create a "lightwell" domain. |
| 23 | +""" |
| 24 | + |
| 25 | +import json |
| 26 | +from base64 import b64encode |
| 27 | +from urllib.parse import urljoin |
| 28 | +from uuid import uuid4 |
| 29 | + |
| 30 | +import pytest |
| 31 | +import requests |
| 32 | + |
| 33 | +from pulp_service.app.authorization import LIGHTWELL_READONLY_GROUP_NAME |
| 34 | + |
| 35 | +LIGHTWELL_DOMAIN_NAME = "lightwell" |
| 36 | + |
| 37 | +# An org with no DomainOrg association with the test domain and no lightwell-network |
| 38 | +# feature entitlement; only used to own the test domain/repo/distribution. |
| 39 | +DOMAIN_OWNER_ORG_ID = "555555555" |
| 40 | +# A distinct org used for the group members created in these tests, so they never |
| 41 | +# accidentally collide with the domain owner's DomainOrg association. |
| 42 | +GROUP_MEMBER_ORG_ID = "666666666" |
| 43 | + |
| 44 | + |
| 45 | +def _identity_header(org_id, username): |
| 46 | + identity = { |
| 47 | + "identity": { |
| 48 | + "org_id": org_id, |
| 49 | + "internal": {"org_id": org_id}, |
| 50 | + "user": {"username": username}, |
| 51 | + } |
| 52 | + } |
| 53 | + return b64encode(json.dumps(identity).encode()).decode() |
| 54 | + |
| 55 | + |
| 56 | +def _combined_username(org_id, username): |
| 57 | + """Matches the "{org_id}|{username}" format RHTermsBasedRegistryAuthentication resolves |
| 58 | + identity headers to (see pulp_service.app.authentication).""" |
| 59 | + return f"{org_id}|{username}" |
| 60 | + |
| 61 | + |
| 62 | +@pytest.fixture |
| 63 | +def lightwell_readonly_group(gen_group): |
| 64 | + """The hardcoded read-only group, created with its real (hardcoded) name.""" |
| 65 | + return gen_group(name=LIGHTWELL_READONLY_GROUP_NAME) |
| 66 | + |
| 67 | + |
| 68 | +@pytest.fixture |
| 69 | +def configure_lightwell_domain( |
| 70 | + anonymous_user, |
| 71 | + gen_object_with_cleanup, |
| 72 | + pulpcore_bindings, |
| 73 | + file_bindings, |
| 74 | + python_bindings, |
| 75 | + bindings_cfg, |
| 76 | +): |
| 77 | + """ |
| 78 | + Creates the "lightwell" domain (owned by DOMAIN_OWNER_ORG_ID, no relation to the |
| 79 | + read-only group), with a File repository and a PyPI-distributed Python repository. |
| 80 | +
|
| 81 | + Returns (repos_url, pypi_url, owner_header). |
| 82 | + """ |
| 83 | + owner_header = _identity_header(DOMAIN_OWNER_ORG_ID, "lightwell-readonly-test-owner") |
| 84 | + |
| 85 | + with anonymous_user: |
| 86 | + pulpcore_bindings.DomainsApi.api_client.default_headers["x-rh-identity"] = owner_header |
| 87 | + |
| 88 | + gen_object_with_cleanup( |
| 89 | + pulpcore_bindings.DomainsApi, |
| 90 | + { |
| 91 | + "name": LIGHTWELL_DOMAIN_NAME, |
| 92 | + "storage_class": "pulpcore.app.models.storage.FileSystem", |
| 93 | + "storage_settings": {"MEDIA_ROOT": "/var/lib/pulp/media/"}, |
| 94 | + }, |
| 95 | + ) |
| 96 | + |
| 97 | + file_bindings.RepositoriesFileApi.api_client.default_headers["x-rh-identity"] = owner_header |
| 98 | + gen_object_with_cleanup( |
| 99 | + file_bindings.RepositoriesFileApi, {"name": str(uuid4())}, pulp_domain=LIGHTWELL_DOMAIN_NAME |
| 100 | + ) |
| 101 | + |
| 102 | + python_bindings.RepositoriesPythonApi.api_client.default_headers["x-rh-identity"] = owner_header |
| 103 | + repo = gen_object_with_cleanup( |
| 104 | + python_bindings.RepositoriesPythonApi, {"name": str(uuid4())}, pulp_domain=LIGHTWELL_DOMAIN_NAME |
| 105 | + ) |
| 106 | + |
| 107 | + python_bindings.DistributionsPypiApi.api_client.default_headers["x-rh-identity"] = owner_header |
| 108 | + pypi_base_path = str(uuid4()) |
| 109 | + gen_object_with_cleanup( |
| 110 | + python_bindings.DistributionsPypiApi, |
| 111 | + {"name": str(uuid4()), "base_path": pypi_base_path, "repository": repo.pulp_href}, |
| 112 | + pulp_domain=LIGHTWELL_DOMAIN_NAME, |
| 113 | + ) |
| 114 | + |
| 115 | + repos_url = urljoin(bindings_cfg.host, f"/api/pulp/{LIGHTWELL_DOMAIN_NAME}/api/v3/repositories/file/file/") |
| 116 | + pypi_url = urljoin(bindings_cfg.host, f"/api/pypi/{LIGHTWELL_DOMAIN_NAME}/{pypi_base_path}/simple/") |
| 117 | + |
| 118 | + yield repos_url, pypi_url, owner_header |
| 119 | + |
| 120 | + pulpcore_bindings.DomainsApi.api_client.default_headers.pop("x-rh-identity", None) |
| 121 | + file_bindings.RepositoriesFileApi.api_client.default_headers.pop("x-rh-identity", None) |
| 122 | + python_bindings.RepositoriesPythonApi.api_client.default_headers.pop("x-rh-identity", None) |
| 123 | + python_bindings.DistributionsPypiApi.api_client.default_headers.pop("x-rh-identity", None) |
| 124 | + |
| 125 | + |
| 126 | +@pytest.fixture |
| 127 | +def gen_readonly_group_member(pulpcore_bindings, gen_object_with_cleanup, lightwell_readonly_group): |
| 128 | + """Creates a user that's a member of the hardcoded read-only group, and returns an |
| 129 | + x-rh-identity header for that user.""" |
| 130 | + |
| 131 | + def _gen_member(username_suffix): |
| 132 | + username = f"readonly-member-{username_suffix}-{uuid4()}" |
| 133 | + combined_username = _combined_username(GROUP_MEMBER_ORG_ID, username) |
| 134 | + gen_object_with_cleanup( |
| 135 | + pulpcore_bindings.UsersApi, |
| 136 | + {"username": combined_username, "groups": [lightwell_readonly_group.pulp_href]}, |
| 137 | + ) |
| 138 | + return _identity_header(GROUP_MEMBER_ORG_ID, username) |
| 139 | + |
| 140 | + return _gen_member |
| 141 | + |
| 142 | + |
| 143 | +def test_readonly_group_member_can_read_lightwell_repositories(configure_lightwell_domain, gen_readonly_group_member): |
| 144 | + """A user with no DomainOrg association, whose only access path is membership in the |
| 145 | + hardcoded read-only group, can list repositories in the lightwell domain.""" |
| 146 | + repos_url, _, _ = configure_lightwell_domain |
| 147 | + headers = {"x-rh-identity": gen_readonly_group_member("read")} |
| 148 | + |
| 149 | + response = requests.get(repos_url, headers=headers, timeout=30) |
| 150 | + |
| 151 | + assert response.status_code == 200 |
| 152 | + |
| 153 | + |
| 154 | +def test_non_member_denied_reading_lightwell_repositories( |
| 155 | + configure_lightwell_domain, gen_object_with_cleanup, pulpcore_bindings |
| 156 | +): |
| 157 | + """A user with no DomainOrg association and no read-only group membership gets 403 when |
| 158 | + listing repositories in the lightwell domain.""" |
| 159 | + repos_url, _, _ = configure_lightwell_domain |
| 160 | + username = f"non-member-{uuid4()}" |
| 161 | + combined_username = _combined_username(GROUP_MEMBER_ORG_ID, username) |
| 162 | + gen_object_with_cleanup(pulpcore_bindings.UsersApi, {"username": combined_username}) |
| 163 | + headers = {"x-rh-identity": _identity_header(GROUP_MEMBER_ORG_ID, username)} |
| 164 | + |
| 165 | + response = requests.get(repos_url, headers=headers, timeout=30) |
| 166 | + |
| 167 | + assert response.status_code == 403 |
| 168 | + |
| 169 | + |
| 170 | +def test_readonly_group_member_write_denied(configure_lightwell_domain, gen_readonly_group_member): |
| 171 | + """The read-only group grants no write access -- a member must still be denied when |
| 172 | + trying to create a repository in the lightwell domain.""" |
| 173 | + repos_url, _, _ = configure_lightwell_domain |
| 174 | + headers = {"x-rh-identity": gen_readonly_group_member("write")} |
| 175 | + |
| 176 | + response = requests.post(repos_url, headers=headers, json={"name": str(uuid4())}, timeout=30) |
| 177 | + |
| 178 | + assert response.status_code in (401, 403) |
| 179 | + |
| 180 | + |
| 181 | +def test_readonly_group_member_pypi_still_requires_feature(configure_lightwell_domain, gen_readonly_group_member): |
| 182 | + """Read-only group membership does not bypass the lightwell-network feature check on |
| 183 | + PyPI views -- a member with no feature entitlement and no DomainOrg association still |
| 184 | + gets 403 on the PyPI simple API.""" |
| 185 | + _, pypi_url, _ = configure_lightwell_domain |
| 186 | + headers = {"x-rh-identity": gen_readonly_group_member("pypi")} |
| 187 | + |
| 188 | + response = requests.get(pypi_url, headers=headers, timeout=30) |
| 189 | + |
| 190 | + assert response.status_code == 403 |
| 191 | + |
| 192 | + |
| 193 | +def test_readonly_group_member_denied_on_other_domains( |
| 194 | + pulpcore_bindings, anonymous_user, gen_object_with_cleanup, gen_readonly_group_member, bindings_cfg |
| 195 | +): |
| 196 | + """Membership in the lightwell read-only group grants no access to domains other than |
| 197 | + lightwell.""" |
| 198 | + other_domain_owner_header = _identity_header("777777777", "other-domain-owner") |
| 199 | + domain_name = f"not-lightwell-{uuid4()}" |
| 200 | + |
| 201 | + with anonymous_user: |
| 202 | + pulpcore_bindings.DomainsApi.api_client.default_headers["x-rh-identity"] = other_domain_owner_header |
| 203 | + gen_object_with_cleanup( |
| 204 | + pulpcore_bindings.DomainsApi, |
| 205 | + { |
| 206 | + "name": domain_name, |
| 207 | + "storage_class": "pulpcore.app.models.storage.FileSystem", |
| 208 | + "storage_settings": {"MEDIA_ROOT": "/var/lib/pulp/media/"}, |
| 209 | + }, |
| 210 | + ) |
| 211 | + pulpcore_bindings.DomainsApi.api_client.default_headers.pop("x-rh-identity", None) |
| 212 | + |
| 213 | + repos_url = urljoin(bindings_cfg.host, f"/api/pulp/{domain_name}/api/v3/repositories/file/file/") |
| 214 | + headers = {"x-rh-identity": gen_readonly_group_member("other-domain")} |
| 215 | + |
| 216 | + response = requests.get(repos_url, headers=headers, timeout=30) |
| 217 | + |
| 218 | + assert response.status_code == 403 |
| 219 | + |
| 220 | + |
| 221 | +def test_readonly_group_member_sees_lightwell_domain_in_listing( |
| 222 | + configure_lightwell_domain, gen_readonly_group_member, bindings_cfg |
| 223 | +): |
| 224 | + """The lightwell domain shows up in GET /domains/ for read-only group members, via |
| 225 | + DomainBasedPermission.scope_queryset().""" |
| 226 | + del configure_lightwell_domain # ensure the "lightwell" domain exists |
| 227 | + headers = {"x-rh-identity": gen_readonly_group_member("domain-list")} |
| 228 | + domains_url = urljoin(bindings_cfg.host, "/api/pulp/api/v3/domains/") |
| 229 | + |
| 230 | + response = requests.get(domains_url, headers=headers, timeout=30) |
| 231 | + |
| 232 | + assert response.status_code == 200 |
| 233 | + domain_names = {domain["name"] for domain in response.json()["results"]} |
| 234 | + assert LIGHTWELL_DOMAIN_NAME in domain_names |
0 commit comments