-
Notifications
You must be signed in to change notification settings - Fork 18
Implement Start Confidential endpoint #627
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
olethanh
merged 23 commits into
dev-confidential
from
andres-feature-endpoint_start_confidential
Jun 12, 2024
Merged
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
82e83e4
Problem: The server don't have a directory to save the platform certi…
nesitor 39bee9a
Problem: The aren't an endpoint to be able to get the confidential pl…
nesitor 5f2ef49
Fix: Solved code quality issues.
nesitor 1439096
Fix: Added 2 test cases for that endpoint.
nesitor 3c29477
Fix: Added PR suggestions.
nesitor 7c6c3ac
Fix: Modified test mock to let the tests work
nesitor d1454a4
Problem: Now isn't possible as a VM operator to get the client sessio…
nesitor a6559c2
Fix: Remove useless aiofiles import
nesitor 5d7044b
Fix: Solve test issues after code quality fixes
nesitor 0987636
Merge branch 'refs/heads/andres-feature-endpoint_platform_certificate…
nesitor e37ba59
Fix: Solve code quality issues.
nesitor 017115e
Fix: Solve code quality issues.
nesitor 8fc195c
Fix: Write file in sync mode to avoid adding a new dependency. Files …
nesitor 5d2eb5f
Fix: Solved PR comments and wrong conditionals.
nesitor 68413be
Fix: Solved more PR comments.
nesitor 1b3a7c7
Fix: Removed unexisting import
nesitor 2f66b85
Merge branch 'refs/heads/andres-feature-endpoint_platform_certificate…
nesitor 722db64
Merge branch 'refs/heads/main' into andres-feature-endpoint_platform_…
nesitor e88e6ea
Merge branch 'refs/heads/andres-feature-endpoint_platform_certificate…
nesitor e983c4e
Fix: Added useless command requested on the PR review.
nesitor ffd469e
Fix: Changed endpoint path and added automatic tests for that endpoint.
nesitor e1c0afa
Fix: Solved settings singleton issue with testing, adding an `initial…
nesitor e3259f7
Fix: Just disable the setting that is failing and remove previous met…
nesitor File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| from pathlib import Path | ||
|
|
||
| from aleph.vm.utils import run_in_subprocess | ||
|
|
||
|
|
||
| class SevClient: | ||
| def __init__(self, sev_dir: Path): | ||
| self.sev_dir = sev_dir | ||
| self.certificates_dir = sev_dir / "platform" | ||
| self.certificates_dir.mkdir(exist_ok=True, parents=True) | ||
| self.certificates_archive = self.certificates_dir / "certs_export.cert" | ||
|
|
||
| async def sevctl_cmd(self, *args) -> bytes: | ||
| return await run_in_subprocess( | ||
| ["sevctl", *args], | ||
| check=True, | ||
| ) | ||
|
|
||
| async def get_certificates(self) -> Path: | ||
| if not self.certificates_archive.is_file(): | ||
| _ = await self.sevctl_cmd("export", str(self.certificates_archive)) | ||
|
|
||
| return self.certificates_archive |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,136 @@ | ||
| import io | ||
| import tempfile | ||
| from pathlib import Path | ||
| from unittest import mock | ||
| from unittest.mock import MagicMock | ||
|
|
||
| import aiohttp | ||
| import pytest | ||
| from aleph_message.models import ItemHash | ||
|
|
||
| from aleph.vm.conf import settings | ||
| from aleph.vm.orchestrator.supervisor import setup_webapp | ||
| from aleph.vm.storage import get_message | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_operator_confidential_initialize_not_authorized(aiohttp_client): | ||
| """Test that the confidential initialize endpoint rejects if the sender is not the good one. Auth needed""" | ||
|
|
||
| settings.ENABLE_QEMU_SUPPORT = True | ||
| settings.ENABLE_CONFIDENTIAL_COMPUTING = True | ||
| settings.setup() | ||
|
|
||
| class FakeExecution: | ||
| message = None | ||
| is_running: bool = True | ||
| is_confidential: bool = False | ||
|
|
||
| class FakeVmPool: | ||
| executions: dict[ItemHash, FakeExecution] = {} | ||
|
|
||
| def __init__(self): | ||
| self.executions[settings.FAKE_INSTANCE_ID] = FakeExecution() | ||
|
|
||
| with mock.patch( | ||
| "aleph.vm.orchestrator.views.authentication.authenticate_jwk", | ||
| return_value="", | ||
| ): | ||
| with mock.patch( | ||
| "aleph.vm.orchestrator.views.operator.is_sender_authorized", | ||
| return_value=False, | ||
| ) as is_sender_authorized_mock: | ||
| app = setup_webapp() | ||
| app["vm_pool"] = FakeVmPool() | ||
| client = await aiohttp_client(app) | ||
| response = await client.post( | ||
| f"/control/machine/{settings.FAKE_INSTANCE_ID}/confidential/initialize", | ||
| ) | ||
| assert response.status == 403 | ||
| assert await response.text() == "Unauthorized sender" | ||
| is_sender_authorized_mock.assert_called_once() | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_operator_confidential_initialize_already_running(aiohttp_client): | ||
| """Test that the confidential initialize endpoint rejects if the VM is already running. Auth needed""" | ||
|
|
||
| settings.ENABLE_QEMU_SUPPORT = True | ||
| settings.ENABLE_CONFIDENTIAL_COMPUTING = True | ||
| settings.setup() | ||
|
|
||
| vm_hash = ItemHash(settings.FAKE_INSTANCE_ID) | ||
| instance_message = await get_message(ref=vm_hash) | ||
|
|
||
| class FakeExecution: | ||
| message = instance_message.content | ||
| is_running: bool = True | ||
| is_confidential: bool = False | ||
|
|
||
| class FakeVmPool: | ||
| executions: dict[ItemHash, FakeExecution] = {} | ||
|
|
||
| def __init__(self): | ||
| self.executions[vm_hash] = FakeExecution() | ||
|
|
||
| with mock.patch( | ||
| "aleph.vm.orchestrator.views.authentication.authenticate_jwk", | ||
| return_value=instance_message.sender, | ||
| ): | ||
| app = setup_webapp() | ||
| app["vm_pool"] = FakeVmPool() | ||
| client = await aiohttp_client(app) | ||
| response = await client.post( | ||
| f"/control/machine/{vm_hash}/confidential/initialize", | ||
| json={"persistent_vms": []}, | ||
| ) | ||
| assert response.status == 403 | ||
| assert await response.text() == f"VM with ref {vm_hash} already running" | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_operator_confidential_initialize(aiohttp_client): | ||
| """Test that the certificates system endpoint responds. No auth needed""" | ||
|
|
||
| settings.ENABLE_QEMU_SUPPORT = True | ||
| settings.ENABLE_CONFIDENTIAL_COMPUTING = True | ||
| settings.setup() | ||
|
|
||
| vm_hash = ItemHash(settings.FAKE_INSTANCE_ID) | ||
| instance_message = await get_message(ref=vm_hash) | ||
|
|
||
| class FakeExecution: | ||
| message = instance_message.content | ||
| is_running: bool = False | ||
| is_confidential: bool = True | ||
| controller_service: str = "" | ||
|
|
||
| class MockSystemDManager: | ||
| enable_and_start = MagicMock(return_value=True) | ||
|
|
||
| class FakeVmPool: | ||
| executions: dict[ItemHash, FakeExecution] = {} | ||
|
|
||
| def __init__(self): | ||
| self.executions[vm_hash] = FakeExecution() | ||
| self.systemd_manager = MockSystemDManager() | ||
|
|
||
| with tempfile.NamedTemporaryFile() as temp_file: | ||
| form_data = aiohttp.FormData() | ||
| form_data.add_field("session", open(temp_file.name, "rb"), filename="session.b64") | ||
| form_data.add_field("godh", open(temp_file.name, "rb"), filename="godh.b64") | ||
|
|
||
| with mock.patch( | ||
| "aleph.vm.orchestrator.views.authentication.authenticate_jwk", | ||
| return_value=instance_message.sender, | ||
| ): | ||
| app = setup_webapp() | ||
| app["vm_pool"] = FakeVmPool() | ||
| client = await aiohttp_client(app) | ||
| response = await client.post( | ||
| f"/control/machine/{vm_hash}/confidential/initialize", | ||
| data=form_data, | ||
| ) | ||
| assert response.status == 200 | ||
| assert await response.text() == f"Started VM with ref {vm_hash}" | ||
| app["vm_pool"].systemd_manager.enable_and_start.assert_called_once() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.