-
Couldn't load subscription status.
- Fork 5
Implement VmConfidentialClient class
#138
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 16 commits into
1yam-vm-client
from
andres-feature-implement_confidential_vm_initialization
Jul 4, 2024
Merged
Changes from 7 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
b464bc5
Problem: A user cannot initialize an already created confidential VM.
nesitor 3198773
Fix: Solve test responses to pass on the CI.
nesitor 6b2166a
Fix: Solve code quality issues.
nesitor a3047e6
Fix: Solve code quality issues.
nesitor f907294
Fix: Solve code quality issues.
nesitor 0c815c3
Fix: Remove useless comments.
nesitor d5b6a42
Fix: Added 2 new missing tests to check every feature.
nesitor c15f7fe
Fix: Implemented measure validation and refactored secret building.
nesitor 8c5aa8f
Merge branch 'refs/heads/1yam-vm-client' into andres-feature-implemen…
nesitor d42a4ee
Problem: Auth was not working
olethanh e852717
mypy
olethanh dd2639a
Fix tests
olethanh 04d31f7
mypy
olethanh 1bbce23
isort
olethanh 196fd92
Fix confidential tests
olethanh d8e4d1d
Merge remote-tracking branch 'origin/1yam-vm-client' into andres-feat…
olethanh 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
File renamed without changes.
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,157 @@ | ||
| import json | ||
| import logging | ||
| import tempfile | ||
| from pathlib import Path | ||
| from typing import Any, Dict, Optional, Tuple | ||
|
|
||
| import aiohttp | ||
| from aleph_message.models import ItemHash | ||
|
|
||
| from aleph.sdk.client.vm_client import VmClient | ||
| from aleph.sdk.types import Account | ||
| from aleph.sdk.utils import run_in_subprocess | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| class VmConfidentialClient(VmClient): | ||
| sevctl_path: Path | ||
|
|
||
| def __init__( | ||
| self, | ||
| account: Account, | ||
| sevctl_path: Path, | ||
| node_url: str = "", | ||
| session: Optional[aiohttp.ClientSession] = None, | ||
| ): | ||
| super().__init__(account, node_url, session) | ||
| self.sevctl_path = sevctl_path | ||
|
|
||
| async def get_certificates(self) -> Tuple[Optional[int], str]: | ||
| url = f"{self.node_url}/about/certificates" | ||
| try: | ||
| async with self.session.get(url) as response: | ||
| data = await response.read() | ||
| with tempfile.NamedTemporaryFile(delete=False) as tmp_file: | ||
| tmp_file.write(data) | ||
| return response.status, tmp_file.name | ||
|
|
||
| except aiohttp.ClientError as e: | ||
| logger.error( | ||
| f"HTTP error getting node certificates on {self.node_url}: {str(e)}" | ||
| ) | ||
| return None, str(e) | ||
|
|
||
| async def create_session( | ||
| self, vm_id: ItemHash, certificate_path: Path, policy: int | ||
| ) -> Path: | ||
| current_path = Path().cwd() | ||
| args = [ | ||
| "session", | ||
| "--name", | ||
| str(vm_id), | ||
| str(certificate_path), | ||
| str(policy), | ||
| ] | ||
| try: | ||
| # TODO: Check command result | ||
| await self.sevctl_cmd(*args) | ||
| return current_path | ||
| except Exception as e: | ||
| raise ValueError(f"Session creation have failed, reason: {str(e)}") | ||
|
|
||
| async def initialize( | ||
| self, vm_id: ItemHash, session: Path, godh: Path | ||
| ) -> Tuple[Optional[int], str]: | ||
| session_file = session.read_bytes() | ||
| godh_file = godh.read_bytes() | ||
| params = { | ||
| "session": session_file, | ||
| "godh": godh_file, | ||
| } | ||
| return await self.perform_confidential_operation( | ||
| vm_id, "confidential/initialize", params=params | ||
| ) | ||
|
|
||
| async def measurement(self, vm_id: ItemHash) -> Tuple[Optional[int], str]: | ||
| status, text = await self.perform_confidential_operation( | ||
| vm_id, "confidential/measurement" | ||
| ) | ||
| if status: | ||
| response = json.loads(text) | ||
| return status, response | ||
|
|
||
| return status, text | ||
|
|
||
| async def validate_measurement(self, vm_id: ItemHash) -> bool: | ||
| # TODO: Implement measurement validation | ||
| return True | ||
|
|
||
| async def build_secret( | ||
| self, tek_path: Path, tik_path: Path, measurement: str, secret: str | ||
| ) -> Tuple[Path, Path]: | ||
| current_path = Path().cwd() | ||
| secret_header_path = current_path / "secret_header.bin" | ||
| secret_payload_path = current_path / "secret_payload.bin" | ||
| args = [ | ||
| "secret", | ||
| "build", | ||
| "--tik", | ||
| str(tik_path), | ||
| "--tek", | ||
| str(tek_path), | ||
| "--launch-measure-blob", | ||
| measurement, | ||
| "--secret", | ||
| secret, | ||
| str(secret_header_path), | ||
| str(secret_payload_path), | ||
| ] | ||
| try: | ||
| # TODO: Check command result | ||
| await self.sevctl_cmd(*args) | ||
| return secret_header_path, secret_payload_path | ||
| except Exception as e: | ||
| raise ValueError(f"Secret building have failed, reason: {str(e)}") | ||
|
|
||
| async def inject_secret( | ||
| self, vm_id: ItemHash, packed_header: str, secret: str | ||
| ) -> Tuple[Optional[int], str]: | ||
| params = { | ||
| "packed_header": packed_header, | ||
| "secret": secret, | ||
| } | ||
| status, text = await self.perform_confidential_operation( | ||
| vm_id, "confidential/inject_secret", params=params | ||
| ) | ||
|
|
||
| if status: | ||
| response = json.loads(text) | ||
| return status, response | ||
|
|
||
| return status, text | ||
|
|
||
| async def perform_confidential_operation( | ||
| self, vm_id: ItemHash, operation: str, params: Optional[Dict[str, Any]] = None | ||
| ) -> Tuple[Optional[int], str]: | ||
| if not self.pubkey_signature_header: | ||
| self.pubkey_signature_header = ( | ||
| await self._generate_pubkey_signature_header() | ||
| ) | ||
|
|
||
| url, header = await self._generate_header(vm_id=vm_id, operation=operation) | ||
|
|
||
| try: | ||
| async with self.session.post(url, headers=header, data=params) as response: | ||
| response_text = await response.text() | ||
| return response.status, response_text | ||
|
|
||
| except aiohttp.ClientError as e: | ||
| logger.error(f"HTTP error during operation {operation}: {str(e)}") | ||
| return None, str(e) | ||
|
|
||
| async def sevctl_cmd(self, *args) -> bytes: | ||
| return await run_in_subprocess( | ||
| [str(self.sevctl_path), *args], | ||
| check=True, | ||
| ) |
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
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.