Skip to content
53 changes: 53 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,59 @@ It is generated with [Stainless](https://www.stainless.com/).

The REST API documentation can be found on [lim.run](https://lim.run). The full API of this library can be found in [api.md](api.md).

## Workload identity federation

Authenticate with a JWT from your identity provider after an organization admin
creates a [federation binding](https://docs.limrun.com/docs/platform/workload-identity-federation).
Point the client at a file your runtime keeps current, such as a Kubernetes projected token:

```python
client = Limrun(
workload_token_file="/var/run/identity/token",
binding_id="binding_REPLACE_WITH_BINDING_ID",
)
```

Or pass the JWT itself in `workload_token`. A string is exchanged as is on every renewal, so it
stops working once that JWT expires. Pass a callable instead when the client outlives the JWT;
the SDK calls it for every exchange. `AsyncLimrun` also accepts an async callable:

```python
client = Limrun(
workload_token=lambda: get_id_token_from_your_provider(),
binding_id="binding_REPLACE_WITH_BINDING_ID",
)
```

Some platforms never show the JWT to the workload. Their egress proxy adds it to outbound
requests instead. Configure the proxy to send the JWT to `sts.limrun.com` in the
`X-Limrun-Subject-Token` header, and tell the client not to send one:

```python
client = Limrun(
workload_token_injected=True,
binding_id="binding_REPLACE_WITH_BINDING_ID",
)
```

`workload_token_injected` cannot be combined with another source. If the proxy terminates TLS
with its own certificate authority, point `SSL_CERT_FILE` at a bundle that includes it.

Alternatively, set `LIMRUN_WORKLOAD_IDENTITY_BINDING_ID` and one of
`LIMRUN_WORKLOAD_IDENTITY_TOKEN` (the JWT), `LIMRUN_WORKLOAD_IDENTITY_TOKEN_FILE`, or
`LIMRUN_WORKLOAD_IDENTITY_TOKEN_INJECTED=true`, and use the default client constructor. Explicit options take precedence over environment variables,
and a token over a file. An API key takes precedence over workload identity when configured.
The SDK caches a short-lived Limrun access token and reads the identity again on renewal.
The access token never outlives the JWT it was exchanged for. `LIMRUN_STS_BASE_URL` overrides
the default token service at `https://sts.limrun.com`.

The instance's `status.token` grants `control` on that instance. It permits runtime
operations and reading the instance record, but cannot create, update, or delete
instance records. Use your configured API key or federated client for those calls,
with the corresponding `create`, `update`, or `delete` permission. Existing `all`
grants include both runtime control and lifecycle management. See the
[permissions reference](https://docs.limrun.com/docs/reference/permissions).

## Installation

```sh
Expand Down
156 changes: 154 additions & 2 deletions src/limrun_api/_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
)
from ._utils import is_given, get_async_library
from ._compat import cached_property
from ._models import FinalRequestOptions
from ._version import __version__
from ._streaming import Stream as Stream, AsyncStream as AsyncStream
from ._exceptions import APIStatusError
Expand All @@ -30,6 +31,15 @@
SyncAPIClient,
AsyncAPIClient,
)
from ._workload_identity import (
WorkloadToken,
WorkloadIdentity,
AsyncWorkloadToken,
AsyncWorkloadIdentity,
replace_identity,
resolve_identity,
resolve_injected,
)

if TYPE_CHECKING:
from .resources import assets, ios_instances, xcode_instances, android_instances
Expand All @@ -44,11 +54,17 @@
class Limrun(SyncAPIClient):
# client options
api_key: str | None
workload_token: WorkloadToken | None

def __init__(
self,
*,
api_key: str | None = None,
workload_token: WorkloadToken | None = None,
workload_token_file: str | None = None,
workload_token_injected: bool | None = None,
binding_id: str | None = None,
sts_base_url: str | None = None,
base_url: str | httpx.URL | None = None,
timeout: float | Timeout | None | NotGiven = not_given,
max_retries: int = DEFAULT_MAX_RETRIES,
Expand All @@ -75,6 +91,35 @@ def __init__(
if api_key is None:
api_key = os.environ.get("LIM_API_KEY")
self.api_key = api_key
self.workload_token, self.workload_token_file = resolve_identity(workload_token, workload_token_file)
self.workload_token_injected = resolve_injected(workload_token_injected)
if not api_key and self.workload_token_injected and (self.workload_token or self.workload_token_file):
# The proxy decides which JWT the exchange carries, so a second source would be ignored.
raise ValueError(
"workload_token_injected cannot be combined with workload_token, workload_token_file, "
"or their environment variables"
)
has_identity = bool(self.workload_token or self.workload_token_file or self.workload_token_injected)
self.binding_id = binding_id or os.environ.get("LIMRUN_WORKLOAD_IDENTITY_BINDING_ID")
self.sts_base_url = sts_base_url or os.environ.get("LIMRUN_STS_BASE_URL") or "https://sts.limrun.com"
if not api_key and self.binding_id and not has_identity:
raise ValueError(
"Federation requires workload_token, workload_token_file, LIMRUN_WORKLOAD_IDENTITY_TOKEN, "
"or LIMRUN_WORKLOAD_IDENTITY_TOKEN_FILE"
)
if not api_key and has_identity and not self.binding_id:
raise ValueError("Federation requires binding_id or LIMRUN_WORKLOAD_IDENTITY_BINDING_ID")
self._workload_identity = (
WorkloadIdentity(
self.workload_token_file,
self.binding_id,
self.sts_base_url,
self.workload_token,
self.workload_token_injected,
)
if not api_key and has_identity and self.binding_id
else None
Comment thread
cursor[bot] marked this conversation as resolved.
Resolved
)

if base_url is None:
base_url = os.environ.get("LIMRUN_BASE_URL")
Expand All @@ -92,6 +137,15 @@ def __init__(
_strict_response_validation=_strict_response_validation,
)

@override
def _prepare_options(self, options: FinalRequestOptions) -> FinalRequestOptions:
if self._workload_identity is not None:
headers = dict(options.headers) if not isinstance(options.headers, NotGiven) else {}
if not any(key.lower() == "authorization" for key in {**self._custom_headers, **headers}):
headers["Authorization"] = "Bearer " + self._workload_identity.get(self._client)
options.headers = headers
return options

@cached_property
def android_instances(self) -> AndroidInstancesResource:
from .resources.android_instances import AndroidInstancesResource
Expand Down Expand Up @@ -159,6 +213,11 @@ def copy(
self,
*,
api_key: str | None = None,
workload_token: WorkloadToken | None = None,
workload_token_file: str | None = None,
workload_token_injected: bool | None = None,
binding_id: str | None = None,
sts_base_url: str | None = None,
base_url: str | httpx.URL | None = None,
timeout: float | Timeout | None | NotGiven = not_given,
http_client: httpx.Client | None = None,
Expand Down Expand Up @@ -190,9 +249,21 @@ def copy(
elif set_default_query is not None:
params = set_default_query

identity = replace_identity(
workload_token,
workload_token_file,
workload_token_injected,
(self.workload_token, self.workload_token_file, self.workload_token_injected),
)
http_client = http_client or self._client
return self.__class__(
client = self.__class__(
api_key=api_key or self.api_key,
# A new file replaces the current token, so it is not overridden by it.
workload_token=identity[0],
workload_token_file=identity[1],
workload_token_injected=identity[2],
binding_id=binding_id or self.binding_id,
sts_base_url=sts_base_url or self.sts_base_url,
Comment thread
cursor[bot] marked this conversation as resolved.
Resolved
base_url=base_url or self.base_url,
timeout=self.timeout if isinstance(timeout, NotGiven) else timeout,
http_client=http_client,
Expand All @@ -201,6 +272,16 @@ def copy(
default_query=params,
**_extra_kwargs,
)
if (
client.api_key == self.api_key
and client.workload_token is self.workload_token
and client.workload_token_file == self.workload_token_file
and client.workload_token_injected == self.workload_token_injected
and client.binding_id == self.binding_id
and client.sts_base_url == self.sts_base_url
):
client._workload_identity = self._workload_identity
return client

# Alias for `copy` for nicer inline usage, e.g.
# client.with_options(timeout=10).foo.create(...)
Expand Down Expand Up @@ -243,11 +324,17 @@ def _make_status_error(
class AsyncLimrun(AsyncAPIClient):
# client options
api_key: str | None
workload_token: AsyncWorkloadToken | None

def __init__(
self,
*,
api_key: str | None = None,
workload_token: AsyncWorkloadToken | None = None,
workload_token_file: str | None = None,
workload_token_injected: bool | None = None,
binding_id: str | None = None,
sts_base_url: str | None = None,
base_url: str | httpx.URL | None = None,
timeout: float | Timeout | None | NotGiven = not_given,
max_retries: int = DEFAULT_MAX_RETRIES,
Expand All @@ -274,6 +361,35 @@ def __init__(
if api_key is None:
api_key = os.environ.get("LIM_API_KEY")
self.api_key = api_key
self.workload_token, self.workload_token_file = resolve_identity(workload_token, workload_token_file)
self.workload_token_injected = resolve_injected(workload_token_injected)
if not api_key and self.workload_token_injected and (self.workload_token or self.workload_token_file):
# The proxy decides which JWT the exchange carries, so a second source would be ignored.
raise ValueError(
"workload_token_injected cannot be combined with workload_token, workload_token_file, "
"or their environment variables"
)
has_identity = bool(self.workload_token or self.workload_token_file or self.workload_token_injected)
self.binding_id = binding_id or os.environ.get("LIMRUN_WORKLOAD_IDENTITY_BINDING_ID")
self.sts_base_url = sts_base_url or os.environ.get("LIMRUN_STS_BASE_URL") or "https://sts.limrun.com"
if not api_key and self.binding_id and not has_identity:
raise ValueError(
"Federation requires workload_token, workload_token_file, LIMRUN_WORKLOAD_IDENTITY_TOKEN, "
"or LIMRUN_WORKLOAD_IDENTITY_TOKEN_FILE"
)
if not api_key and has_identity and not self.binding_id:
raise ValueError("Federation requires binding_id or LIMRUN_WORKLOAD_IDENTITY_BINDING_ID")
self._workload_identity = (
AsyncWorkloadIdentity(
self.workload_token_file,
self.binding_id,
self.sts_base_url,
self.workload_token,
self.workload_token_injected,
)
if not api_key and has_identity and self.binding_id
else None
)

if base_url is None:
base_url = os.environ.get("LIMRUN_BASE_URL")
Expand All @@ -291,6 +407,15 @@ def __init__(
_strict_response_validation=_strict_response_validation,
)

@override
async def _prepare_options(self, options: FinalRequestOptions) -> FinalRequestOptions:
if self._workload_identity is not None:
headers = dict(options.headers) if not isinstance(options.headers, NotGiven) else {}
if not any(key.lower() == "authorization" for key in {**self._custom_headers, **headers}):
headers["Authorization"] = "Bearer " + await self._workload_identity.get(self._client)
options.headers = headers
return options

@cached_property
def android_instances(self) -> AsyncAndroidInstancesResource:
from .resources.android_instances import AsyncAndroidInstancesResource
Expand Down Expand Up @@ -358,6 +483,11 @@ def copy(
self,
*,
api_key: str | None = None,
workload_token: AsyncWorkloadToken | None = None,
workload_token_file: str | None = None,
workload_token_injected: bool | None = None,
binding_id: str | None = None,
sts_base_url: str | None = None,
base_url: str | httpx.URL | None = None,
timeout: float | Timeout | None | NotGiven = not_given,
http_client: httpx.AsyncClient | None = None,
Expand Down Expand Up @@ -389,9 +519,21 @@ def copy(
elif set_default_query is not None:
params = set_default_query

identity = replace_identity(
workload_token,
workload_token_file,
workload_token_injected,
(self.workload_token, self.workload_token_file, self.workload_token_injected),
)
http_client = http_client or self._client
return self.__class__(
client = self.__class__(
api_key=api_key or self.api_key,
# A new file replaces the current token, so it is not overridden by it.
workload_token=identity[0],
workload_token_file=identity[1],
workload_token_injected=identity[2],
binding_id=binding_id or self.binding_id,
sts_base_url=sts_base_url or self.sts_base_url,
base_url=base_url or self.base_url,
timeout=self.timeout if isinstance(timeout, NotGiven) else timeout,
http_client=http_client,
Expand All @@ -400,6 +542,16 @@ def copy(
default_query=params,
**_extra_kwargs,
)
if (
client.api_key == self.api_key
and client.workload_token is self.workload_token
and client.workload_token_file == self.workload_token_file
and client.workload_token_injected == self.workload_token_injected
and client.binding_id == self.binding_id
and client.sts_base_url == self.sts_base_url
):
client._workload_identity = self._workload_identity
return client

# Alias for `copy` for nicer inline usage, e.g.
# client.with_options(timeout=10).foo.create(...)
Expand Down
Loading
Loading