|
12 | 12 | from renku_data_services.data_api.dependencies import DependencyManager |
13 | 13 | from renku_data_services.migrations.core import run_migrations_for_app |
14 | 14 | from renku_data_services.storage.rclone import RCloneValidator |
15 | | -from renku_data_services.storage.rclone_patches import BANNED_STORAGE, OAUTH_PROVIDERS |
| 15 | +from renku_data_services.storage.rclone_patches import BANNED_SFTP_OPTIONS, BANNED_STORAGE, OAUTH_PROVIDERS |
16 | 16 | from test.utils import SanicReusableASGITestClient |
17 | 17 |
|
18 | 18 | _valid_storage: dict[str, Any] = { |
@@ -249,6 +249,21 @@ async def storage_test_client( |
249 | 249 | 422, |
250 | 250 | "", |
251 | 251 | ), |
| 252 | + ( |
| 253 | + { |
| 254 | + "project_id": "123456", |
| 255 | + "name": "mystorage", |
| 256 | + "configuration": { |
| 257 | + "type": "sftp", |
| 258 | + "host": "myhost", |
| 259 | + "ssh": "ssh", # passing in banned option |
| 260 | + }, |
| 261 | + "source_path": "bucket/myfolder", |
| 262 | + "target_path": "my/target", |
| 263 | + }, |
| 264 | + 422, |
| 265 | + "", |
| 266 | + ), |
252 | 267 | ], |
253 | 268 | ) |
254 | 269 | @pytest.mark.asyncio |
@@ -508,6 +523,39 @@ async def test_storage_patch_unauthorized(storage_test_client, valid_storage_pay |
508 | 523 | assert res.status_code == 403, res.text |
509 | 524 |
|
510 | 525 |
|
| 526 | +@pytest.mark.asyncio |
| 527 | +async def test_storage_patch_banned_option(storage_test_client, valid_storage_payload) -> None: |
| 528 | + storage_test_client, _ = storage_test_client |
| 529 | + # NOTE: The keycloak dummy client used to authorize the storage patch requests only has info |
| 530 | + # on a user with name Admin Doe, using a different user will fail with a 401 error. |
| 531 | + access_token = json.dumps({"is_admin": False, "id": "some-id", "full_name": "Admin Doe"}) |
| 532 | + payload = dict(valid_storage_payload) |
| 533 | + payload["configuration"] = { |
| 534 | + "type": "sftp", |
| 535 | + "host": "myhost", |
| 536 | + } |
| 537 | + _, res = await storage_test_client.post( |
| 538 | + "/api/data/storage", |
| 539 | + headers={"Authorization": f"bearer {access_token}"}, |
| 540 | + data=json.dumps(payload), |
| 541 | + ) |
| 542 | + assert res.status_code == 201 |
| 543 | + assert res.json["storage"]["storage_type"] == "sftp" |
| 544 | + storage_id = res.json["storage"]["storage_id"] |
| 545 | + |
| 546 | + _, res = await storage_test_client.patch( |
| 547 | + f"/api/data/storage/{storage_id}", |
| 548 | + headers={"Authorization": f"bearer {access_token}"}, |
| 549 | + data=json.dumps( |
| 550 | + { |
| 551 | + "configuration": {"key_file": "my_key"}, |
| 552 | + } |
| 553 | + ), |
| 554 | + ) |
| 555 | + assert res.status_code == 422 |
| 556 | + assert "key_file option is not allowed" in res.text |
| 557 | + |
| 558 | + |
511 | 559 | @pytest.mark.asyncio |
512 | 560 | async def test_storage_obscure(storage_test_client) -> None: |
513 | 561 | storage_test_client, _ = storage_test_client |
@@ -632,9 +680,20 @@ async def test_storage_schema_patches(storage_test_client, snapshot) -> None: |
632 | 680 | oauth_providers = [s for s in schema if s["prefix"] in OAUTH_PROVIDERS] |
633 | 681 | assert all(o["name"] != "client_id" and o["name"] != "client_secret" for p in oauth_providers for o in p["options"]) |
634 | 682 |
|
| 683 | + # check the OAUTH_PROVIDERS list |
| 684 | + not_exists = set(p for p in OAUTH_PROVIDERS if p not in set(s["prefix"] for s in schema)) |
| 685 | + assert not_exists == set() |
| 686 | + |
635 | 687 | # check custom webdav storage is added |
636 | 688 | assert any(s["prefix"] == "polybox" for s in schema) |
637 | 689 | assert any(s["prefix"] == "switchDrive" for s in schema) |
| 690 | + |
| 691 | + # check that unsafe SFTP options are removed |
| 692 | + sftp = next((e for e in schema if e["prefix"] == "sftp"), None) |
| 693 | + assert sftp |
| 694 | + assert all(o["name"] not in BANNED_SFTP_OPTIONS for o in sftp["options"]) |
| 695 | + |
| 696 | + # snapshot the schema |
638 | 697 | assert schema == snapshot |
639 | 698 |
|
640 | 699 |
|
|
0 commit comments