Skip to content

fix: Sandbox.list(teamspace=...) can use teamspace-scoped key - #169

Merged
lianakoleva merged 2 commits into
mainfrom
lk/fix-sandbox-can-use-teamspace-scoped-key
Sep 22, 2026
Merged

lianakoleva merged 2 commits into
mainfrom
lk/fix-sandbox-can-use-teamspace-scoped-key

Conversation

@lianakoleva

Copy link
Copy Markdown
Member
Error on main
  File "/opt/hostedtoolcache/Python/3.12.14/x64/lib/python3.12/site-packages/lightning_sdk/lightning_cloud/rest_client.py", line 153, in wrapped
    return func(self, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/hostedtoolcache/Python/3.12.14/x64/lib/python3.12/site-packages/lightning_sdk/lightning_cloud/openapi/api/projects_service_api.py", line 1363, in projects_service_get_project_with_http_info
    return self.api_client.call_api(
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/hostedtoolcache/Python/3.12.14/x64/lib/python3.12/site-packages/lightning_sdk/lightning_cloud/openapi/api_client.py", line 334, in call_api
    return self.__call_api(resource_path, method,
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/hostedtoolcache/Python/3.12.14/x64/lib/python3.12/site-packages/lightning_sdk/lightning_cloud/openapi/api_client.py", line 166, in __call_api
    response_data = self.request(
                    ^^^^^^^^^^^^^
  File "/opt/hostedtoolcache/Python/3.12.14/x64/lib/python3.12/site-packages/lightning_sdk/lightning_cloud/rest_client.py", line 276, in wrap
    raise err
  File "/opt/hostedtoolcache/Python/3.12.14/x64/lib/python3.12/site-packages/lightning_sdk/lightning_cloud/rest_client.py", line 271, in wrap
    response = func(*args, **kwargs)
               ^^^^^^^^^^^^^^^^^^^^^
  File "/opt/hostedtoolcache/Python/3.12.14/x64/lib/python3.12/site-packages/lightning_sdk/lightning_cloud/openapi/api_client.py", line 356, in request
    return self.rest_client.GET(url,
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/hostedtoolcache/Python/3.12.14/x64/lib/python3.12/site-packages/lightning_sdk/lightning_cloud/openapi/rest.py", line 245, in GET
    return self.request("GET", url,
           ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/hostedtoolcache/Python/3.12.14/x64/lib/python3.12/site-packages/lightning_sdk/lightning_cloud/openapi/rest.py", line 239, in request
    raise ApiException(http_resp=r)
lightning_sdk.lightning_cloud.openapi.rest.ApiException: (403)
Reason: Forbidden
HTTP response headers: HTTPHeaderDict({'Date': 'Sat, 19 Sep 2026 00:54:43 GMT', 'Content-Type': 'application/json', 'Transfer-Encoding': 'chunked', 'Connection': 'keep-alive', 'access-control-allow-headers': 'Content-Type,X-Grid-Key,X-Grid-User,Authorization', 'access-control-allow-methods': 'GET,POST,PUT,PATCH,DELETE,HEAD,OPTIONS', 'access-control-allow-origin': '*', 'Cross-Origin-Opener-Policy': 'same-origin', 'vary': 'Accept-Encoding', 'x-content-type-options': 'nosniff', 'x-frame-options': 'SAMEORIGIN', 'x-xss-protection': '1; mode=block', 'cf-cache-status': 'DYNAMIC', 'Strict-Transport-Security': 'max-age=15552000; includeSubDomains; preload', 'Server': 'cloudflare', 'CF-RAY': 'a3d49ee8a801defb-SEA'})
HTTP response body: b'{"code":7, "message":"unauthorized", "details":[]}'


During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/opt/hostedtoolcache/Python/3.12.14/x64/lib/python3.12/site-packages/lightning_sdk/sandbox/sandbox.py", line 34, in _resolve_teamspace_id
    resolved = _resolve_teamspace(teamspace_name, org=owner, user=None)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/hostedtoolcache/Python/3.12.14/x64/lib/python3.12/site-packages/lightning_sdk/utils/resolve.py", line 260, in _resolve_teamspace
    return Teamspace(name=teamspace, org=org, user=user)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/hostedtoolcache/Python/3.12.14/x64/lib/python3.12/site-packages/lightning_sdk/utils/logging.py", line 49, in wrapper
    return func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
  File "/opt/hostedtoolcache/Python/3.12.14/x64/lib/python3.12/site-packages/lightning_sdk/teamspace.py", line 175, in __init__
    self._teamspace = self._teamspace_api.get_teamspace(name=name, owner_id=self.owner.id)
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/hostedtoolcache/Python/3.12.14/x64/lib/python3.12/site-packages/lightning_sdk/api/teamspace_api.py", line 135, in get_teamspace
    teamspaces = self.list_teamspaces(name=name, owner_id=owner_id)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/hostedtoolcache/Python/3.12.14/x64/lib/python3.12/site-packages/lightning_sdk/api/teamspace_api.py", line 178, in list_teamspaces
    teamspaces.append(self._get_teamspace_by_id(teamspace.project_id))
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/hostedtoolcache/Python/3.12.14/x64/lib/python3.12/site-packages/lightning_sdk/api/teamspace_api.py", line 154, in _get_teamspace_by_id
    return self._client.projects_service_get_project(teamspace_id)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/hostedtoolcache/Python/3.12.14/x64/lib/python3.12/site-packages/lightning_sdk/lightning_cloud/rest_client.py", line 153, in wrapped
    return func(self, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/hostedtoolcache/Python/3.12.14/x64/lib/python3.12/site-packages/lightning_sdk/lightning_cloud/openapi/api/projects_service_api.py", line 1304, in projects_service_get_project
    (data) = self.projects_service_get_project_with_http_info(id, **kwargs)  # noqa: E501
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/hostedtoolcache/Python/3.12.14/x64/lib/python3.12/site-packages/lightning_sdk/lightning_cloud/rest_client.py", line 169, in wrapped
    raise Exception(
Exception: The projects_service_get_project_with_http_info request failed to reach the server, response: 403.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/runner/work/lit-model-apis/lit-model-apis/.github/scripts/build_sdk.py", line 48, in <module>
    existing = client.list(teamspace=args.teamspace)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/hostedtoolcache/Python/3.12.14/x64/lib/python3.12/site-packages/lightning_sdk/sandbox/sandbox.py", line 178, in list
    project_id=_resolve_teamspace_id(teamspace),
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/hostedtoolcache/Python/3.12.14/x64/lib/python3.12/site-packages/lightning_sdk/sandbox/sandbox.py", line 36, in _resolve_teamspace_id
    resolved = _resolve_teamspace(teamspace_name, org=None, user=owner)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/hostedtoolcache/Python/3.12.14/x64/lib/python3.12/site-packages/lightning_sdk/utils/resolve.py", line 260, in _resolve_teamspace
    return Teamspace(name=teamspace, org=org, user=user)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/hostedtoolcache/Python/3.12.14/x64/lib/python3.12/site-packages/lightning_sdk/utils/logging.py", line 49, in wrapper
    return func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
  File "/opt/hostedtoolcache/Python/3.12.14/x64/lib/python3.12/site-packages/lightning_sdk/teamspace.py", line 141, in __init__
    self._user = _resolve_user(user)
                 ^^^^^^^^^^^^^^^^^^^
  File "/opt/hostedtoolcache/Python/3.12.14/x64/lib/python3.12/site-packages/lightning_sdk/utils/resolve.py", line 206, in _resolve_user
    return User(name=user)
           ^^^^^^^^^^^^^^^
  File "/opt/hostedtoolcache/Python/3.12.14/x64/lib/python3.12/site-packages/lightning_sdk/utils/logging.py", line 49, in wrapper
    return func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
  File "/opt/hostedtoolcache/Python/3.12.14/x64/lib/python3.12/site-packages/lightning_sdk/user.py", line 32, in __init__
    self._user = self._user_api.get_user(name=name)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/hostedtoolcache/Python/3.12.14/x64/lib/python3.12/site-packages/lightning_sdk/api/user_api.py", line 53, in get_user
    raise ValueError(f"User {name} does not exist.")
ValueError: User lightning-ai does not exist.

Error flow
- _resolve_teamspace_id first tried to resolve using org=owner → 403 Forbidden because the teamspace-scoped API key didn't have org-level access
- fell back to org=None, user=owner → ValueError: User lightning-ai does not exist (there's no user named lightning-ai)

Fix

Now, when both the org-scoped and user-scoped lookups fail, use the projects_service_list_memberships(filter_by_user_id=True) API to list all teamspaces the current API key has access to, and match by name. A teamspace-scoped API key has access to list_memberships for the teamspaces it belongs to, even though it can't do org-level or user-level lookups.

@lianakoleva
lianakoleva merged commit 1ae44f5 into main Sep 22, 2026
25 checks passed
@lianakoleva
lianakoleva deleted the lk/fix-sandbox-can-use-teamspace-scoped-key branch September 22, 2026 19:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants