Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/dstack/api/_public/runs.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
get_service_port,
)
from dstack._internal.core.models.runs import Run as RunModel
from dstack._internal.core.models.users import UserWithCreds
from dstack._internal.core.services.configs import ConfigManager
from dstack._internal.core.services.logs import URLReplacer
from dstack._internal.core.services.ssh.attach import SSHAttach
Expand Down Expand Up @@ -283,7 +284,7 @@ def attach(
user = self._api_client.users.get_my_user()
run_ssh_key_pub = self._run.run_spec.ssh_key_pub
config_manager = ConfigManager()
if user.ssh_public_key == run_ssh_key_pub:
if isinstance(user, UserWithCreds) and user.ssh_public_key == run_ssh_key_pub:
token_hash = hashlib.sha1(user.creds.token.encode()).hexdigest()[:8]
config_manager.dstack_ssh_dir.mkdir(parents=True, exist_ok=True)
ssh_identity_file = config_manager.dstack_ssh_dir / token_hash
Expand Down
21 changes: 18 additions & 3 deletions src/dstack/api/server/_users.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import List

from pydantic import parse_obj_as
from pydantic import ValidationError, parse_obj_as

from dstack._internal.core.models.users import GlobalRole, User, UserWithCreds
from dstack._internal.server.schemas.users import (
Expand All @@ -17,9 +17,24 @@ def list(self) -> List[User]:
resp = self._request("/api/users/list")
return parse_obj_as(List[User.__response__], resp.json())

def get_my_user(self) -> UserWithCreds:
def get_my_user(self) -> User:
"""
Returns `User` with pre-0.19.33 servers, or `UserWithCreds` with newer servers.
"""

resp = self._request("/api/users/get_my_user")
return parse_obj_as(UserWithCreds.__response__, resp.json())
try:
return parse_obj_as(UserWithCreds.__response__, resp.json())
except ValidationError as e:
# Compatibility with pre-0.19.33 server
if (
len(e.errors()) == 1
and e.errors()[0]["loc"] == ("__root__", "creds")
and e.errors()[0]["type"] == "value_error.missing"
):
return parse_obj_as(User.__response__, resp.json())
else:
raise

def get_user(self, username: str) -> User:
body = GetUserRequest(username=username)
Expand Down