-
Notifications
You must be signed in to change notification settings - Fork 205
Use server-managed user SSH keys for new runs #3216
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
Merged
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| import os | ||
| from dataclasses import dataclass | ||
| from datetime import datetime, timedelta | ||
| from pathlib import Path | ||
| from typing import TYPE_CHECKING, Optional | ||
|
|
||
| from dstack._internal.core.models.users import UserWithCreds | ||
|
|
||
| if TYPE_CHECKING: | ||
| from dstack.api.server import APIClient | ||
|
|
||
| KEY_REFRESH_RATE = timedelta(minutes=10) # redownload the key periodically in case it was rotated | ||
|
|
||
|
|
||
| @dataclass | ||
| class UserSSHKey: | ||
| public_key: str | ||
| private_key_path: Path | ||
|
|
||
|
|
||
| class UserSSHKeyManager: | ||
| def __init__(self, api_client: "APIClient", ssh_keys_dir: Path) -> None: | ||
| self._api_client = api_client | ||
| self._key_path = ssh_keys_dir / api_client.get_token_hash() | ||
| self._pub_key_path = self._key_path.with_suffix(".pub") | ||
|
|
||
| def get_user_key(self) -> Optional[UserSSHKey]: | ||
| """ | ||
| Return the up-to-date user key, or None if the user has no key (if created before 0.19.33) | ||
| """ | ||
| if ( | ||
| not self._key_path.exists() | ||
| or not self._pub_key_path.exists() | ||
| or datetime.now() - datetime.fromtimestamp(self._key_path.stat().st_mtime) | ||
| > KEY_REFRESH_RATE | ||
| ): | ||
| if not self._download_user_key(): | ||
| return None | ||
| return UserSSHKey( | ||
| public_key=self._pub_key_path.read_text(), private_key_path=self._key_path | ||
| ) | ||
|
|
||
| def _download_user_key(self) -> bool: | ||
| user = self._api_client.users.get_my_user() | ||
| if not (isinstance(user, UserWithCreds) and user.ssh_public_key and user.ssh_private_key): | ||
| return False | ||
|
|
||
| def key_opener(path, flags): | ||
| return os.open(path, flags, 0o600) | ||
|
|
||
| with open(self._key_path, "w", opener=key_opener) as f: | ||
| f.write(user.ssh_private_key) | ||
| with open(self._pub_key_path, "w") as f: | ||
| f.write(user.ssh_public_key) | ||
|
|
||
| return 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
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why put UserSSHKeyManager in core instead of api?