Skip to content

Commit 2c81a97

Browse files
authored
Switch to container auth token for pods client (#10)
1 parent 296e483 commit 2c81a97

File tree

3 files changed

+20
-7
lines changed

3 files changed

+20
-7
lines changed

localstack-sdk-python/localstack/clients.py

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import os
2-
31
from localstack.sdk.api_client import ApiClient
42
from localstack.sdk.configuration import Configuration
53

@@ -12,7 +10,14 @@ class BaseClient:
1210
auth_token: str | None
1311

1412
def __init__(self, host: str | None = None, auth_token: str | None = None, **kwargs) -> None:
13+
"""
14+
Initialize a base client to interact with LocalStack developer endpoint.
15+
:param host: the host, http://localhost.localstack.cloud:4566 by default.
16+
:param auth_token: if provided, this token would be used for authentication against platform. It not, the
17+
LocalStack runtime will use the one used to start the container. The token used determines the Cloud
18+
Pods identity, i.e., which pods are available.
19+
"""
1520
_host = host or "http://localhost.localstack.cloud:4566"
16-
self.auth_token = auth_token or os.getenv("LOCALSTACK_AUTH_TOKEN", "").strip("'\" ")
21+
self.auth_token = auth_token
1722
self.configuration = Configuration(host=_host)
1823
self._api_client = ApiClient(configuration=self.configuration)

localstack-sdk-python/localstack/sdk/pods/client.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,12 @@ class PodsClient(BaseClient):
2424
def __init__(self, **args) -> None:
2525
super().__init__(**args)
2626
self._client = PodsApi(self._api_client)
27-
# https://github.com/localstack/localstack-ext/pull/3469 could be avoided after this
28-
assert self.auth_token
29-
auth_header = get_platform_auth_header(self.auth_token)
30-
self._api_client.set_default_header("Authorization", auth_header["Authorization"])
27+
if self.auth_token:
28+
# If an auth token is provided, it will be used to authenticate platform calls for Cloud Pods.
29+
# Only the pods tied to this token will be visible. If not provided, the token will be fetched from the
30+
# container. This allows to separate container identity for caller identity, if needed.
31+
auth_header = get_platform_auth_header(self.auth_token)
32+
self._api_client.set_default_header("Authorization", auth_header["Authorization"])
3133

3234
def save_pod(self, pod_name: str) -> None:
3335
"""

tests/integration/test_pods.py

+6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import pytest
2+
13
from localstack.sdk.pods import PodsClient
24

35
POD_NAME = "ls-sdk-integration"
@@ -14,3 +16,7 @@ def test_pod_crud(self):
1416
self.client.save_pod(pod_name=POD_NAME)
1517
self.client.load_pod(pod_name=POD_NAME)
1618
self.client.delete_pod(pod_name=POD_NAME)
19+
20+
def test_not_existing_pod(self):
21+
with pytest.raises(Exception):
22+
self.client.load_pod(pod_name="i-do-not-exists")

0 commit comments

Comments
 (0)