|
| 1 | +import io |
| 2 | +import tempfile |
| 3 | +from pathlib import Path |
| 4 | +from unittest import mock |
| 5 | +from unittest.mock import MagicMock |
| 6 | + |
| 7 | +import aiohttp |
| 8 | +import pytest |
| 9 | +from aleph_message.models import ItemHash |
| 10 | + |
| 11 | +from aleph.vm.conf import settings |
| 12 | +from aleph.vm.orchestrator.supervisor import setup_webapp |
| 13 | +from aleph.vm.storage import get_message |
| 14 | + |
| 15 | + |
| 16 | +@pytest.mark.asyncio |
| 17 | +async def test_operator_confidential_initialize_not_authorized(aiohttp_client): |
| 18 | + """Test that the confidential initialize endpoint rejects if the sender is not the good one. Auth needed""" |
| 19 | + |
| 20 | + settings.ENABLE_QEMU_SUPPORT = True |
| 21 | + settings.ENABLE_CONFIDENTIAL_COMPUTING = True |
| 22 | + settings.setup() |
| 23 | + |
| 24 | + class FakeExecution: |
| 25 | + message = None |
| 26 | + is_running: bool = True |
| 27 | + is_confidential: bool = False |
| 28 | + |
| 29 | + class FakeVmPool: |
| 30 | + executions: dict[ItemHash, FakeExecution] = {} |
| 31 | + |
| 32 | + def __init__(self): |
| 33 | + self.executions[settings.FAKE_INSTANCE_ID] = FakeExecution() |
| 34 | + |
| 35 | + with mock.patch( |
| 36 | + "aleph.vm.orchestrator.views.authentication.authenticate_jwk", |
| 37 | + return_value="", |
| 38 | + ): |
| 39 | + with mock.patch( |
| 40 | + "aleph.vm.orchestrator.views.operator.is_sender_authorized", |
| 41 | + return_value=False, |
| 42 | + ) as is_sender_authorized_mock: |
| 43 | + app = setup_webapp() |
| 44 | + app["vm_pool"] = FakeVmPool() |
| 45 | + client = await aiohttp_client(app) |
| 46 | + response = await client.post( |
| 47 | + f"/control/machine/{settings.FAKE_INSTANCE_ID}/confidential/initialize", |
| 48 | + ) |
| 49 | + assert response.status == 403 |
| 50 | + assert await response.text() == "Unauthorized sender" |
| 51 | + is_sender_authorized_mock.assert_called_once() |
| 52 | + |
| 53 | + |
| 54 | +@pytest.mark.asyncio |
| 55 | +async def test_operator_confidential_initialize_already_running(aiohttp_client): |
| 56 | + """Test that the confidential initialize endpoint rejects if the VM is already running. Auth needed""" |
| 57 | + |
| 58 | + settings.ENABLE_QEMU_SUPPORT = True |
| 59 | + settings.ENABLE_CONFIDENTIAL_COMPUTING = True |
| 60 | + settings.setup() |
| 61 | + |
| 62 | + vm_hash = ItemHash(settings.FAKE_INSTANCE_ID) |
| 63 | + instance_message = await get_message(ref=vm_hash) |
| 64 | + |
| 65 | + class FakeExecution: |
| 66 | + message = instance_message.content |
| 67 | + is_running: bool = True |
| 68 | + is_confidential: bool = False |
| 69 | + |
| 70 | + class FakeVmPool: |
| 71 | + executions: dict[ItemHash, FakeExecution] = {} |
| 72 | + |
| 73 | + def __init__(self): |
| 74 | + self.executions[vm_hash] = FakeExecution() |
| 75 | + |
| 76 | + with mock.patch( |
| 77 | + "aleph.vm.orchestrator.views.authentication.authenticate_jwk", |
| 78 | + return_value=instance_message.sender, |
| 79 | + ): |
| 80 | + app = setup_webapp() |
| 81 | + app["vm_pool"] = FakeVmPool() |
| 82 | + client = await aiohttp_client(app) |
| 83 | + response = await client.post( |
| 84 | + f"/control/machine/{vm_hash}/confidential/initialize", |
| 85 | + json={"persistent_vms": []}, |
| 86 | + ) |
| 87 | + assert response.status == 403 |
| 88 | + assert await response.text() == f"VM with ref {vm_hash} already running" |
| 89 | + |
| 90 | + |
| 91 | +@pytest.mark.asyncio |
| 92 | +async def test_operator_confidential_initialize(aiohttp_client): |
| 93 | + """Test that the certificates system endpoint responds. No auth needed""" |
| 94 | + |
| 95 | + settings.ENABLE_QEMU_SUPPORT = True |
| 96 | + settings.ENABLE_CONFIDENTIAL_COMPUTING = True |
| 97 | + settings.setup() |
| 98 | + |
| 99 | + vm_hash = ItemHash(settings.FAKE_INSTANCE_ID) |
| 100 | + instance_message = await get_message(ref=vm_hash) |
| 101 | + |
| 102 | + class FakeExecution: |
| 103 | + message = instance_message.content |
| 104 | + is_running: bool = False |
| 105 | + is_confidential: bool = True |
| 106 | + controller_service: str = "" |
| 107 | + |
| 108 | + class MockSystemDManager: |
| 109 | + enable_and_start = MagicMock(return_value=True) |
| 110 | + |
| 111 | + class FakeVmPool: |
| 112 | + executions: dict[ItemHash, FakeExecution] = {} |
| 113 | + |
| 114 | + def __init__(self): |
| 115 | + self.executions[vm_hash] = FakeExecution() |
| 116 | + self.systemd_manager = MockSystemDManager() |
| 117 | + |
| 118 | + with tempfile.NamedTemporaryFile() as temp_file: |
| 119 | + form_data = aiohttp.FormData() |
| 120 | + form_data.add_field("session", open(temp_file.name, "rb"), filename="session.b64") |
| 121 | + form_data.add_field("godh", open(temp_file.name, "rb"), filename="godh.b64") |
| 122 | + |
| 123 | + with mock.patch( |
| 124 | + "aleph.vm.orchestrator.views.authentication.authenticate_jwk", |
| 125 | + return_value=instance_message.sender, |
| 126 | + ): |
| 127 | + app = setup_webapp() |
| 128 | + app["vm_pool"] = FakeVmPool() |
| 129 | + client = await aiohttp_client(app) |
| 130 | + response = await client.post( |
| 131 | + f"/control/machine/{vm_hash}/confidential/initialize", |
| 132 | + data=form_data, |
| 133 | + ) |
| 134 | + assert response.status == 200 |
| 135 | + assert await response.text() == f"Started VM with ref {vm_hash}" |
| 136 | + app["vm_pool"].systemd_manager.enable_and_start.assert_called_once() |
0 commit comments