Skip to content

The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]

feat: add workload identity federation between GCP and AWS (#10210)

Pascal Limad2e2302e2d6
diff --git a/litellm/llms/vertex_ai/vertex_llm_base.py b/litellm/llms/vertex_ai/vertex_llm_base.py
index 5ea443513bb..d66c498d4ca 100644
--- a/litellm/llms/vertex_ai/vertex_llm_base.py
+++ b/litellm/llms/vertex_ai/vertex_llm_base.py
@@ -79,7 +79,15 @@ class VertexBase:
# Check if the JSON object contains Workload Identity Federation configuration
if "type" in json_obj and json_obj["type"] == "external_account":
- creds = self._credentials_from_identity_pool(json_obj)
+ # If environment_id key contains "aws" value it corresponds to an AWS config file
+ if (
+ "credential_source" in json_obj
+ and "environment_id" in json_obj["credential_source"]
+ and "aws" in json_obj["credential_source"]["environment_id"]
+ ):
+ creds = self._credentials_from_identity_pool_with_aws(json_obj)
+ else:
+ creds = self._credentials_from_identity_pool(json_obj)
# Check if the JSON object contains Authorized User configuration (via gcloud auth application-default login)
elif "type" in json_obj and json_obj["type"] == "authorized_user":
creds = self._credentials_from_authorized_user(
@@ -122,6 +130,11 @@ class VertexBase:
from google.auth import identity_pool
return identity_pool.Credentials.from_info(json_obj)
+
+ def _credentials_from_identity_pool_with_aws(self, json_obj):
+ from google.auth import aws
+
+ return aws.Credentials.from_info(json_obj)
def _credentials_from_authorized_user(self, json_obj, scopes):
import google.oauth2.credentials
diff --git a/tests/test_litellm/llms/vertex_ai/test_vertex_llm_base.py b/tests/test_litellm/llms/vertex_ai/test_vertex_llm_base.py
index c463404a971..350a90f0d94 100644
--- a/tests/test_litellm/llms/vertex_ai/test_vertex_llm_base.py
+++ b/tests/test_litellm/llms/vertex_ai/test_vertex_llm_base.py
@@ -243,6 +243,98 @@ class TestVertexBase:
assert token == "refreshed-token"
assert project == not_quota_project_id
+ @pytest.mark.parametrize("is_async", [True, False], ids=["async", "sync"])
+ @pytest.mark.asyncio
+ async def test_identity_pool_credentials(self, is_async):
+ vertex_base = VertexBase()
+
+ # Test case: Using Workload Identity Federation for Microsoft Azure and
+ # OIDC identity providers (default behavior)
+ credentials = {
+ "project_id": "test-project",
+ "refresh_token": "fake-refresh-token",
+ "type": "external_account",
+ }
+ mock_creds = MagicMock()
+ mock_creds.token = "token-1"
+ mock_creds.expired = False
+ mock_creds.project_id = "test-project"
+
+ with patch.object(
+ vertex_base, "_credentials_from_identity_pool", return_value=mock_creds
+ ) as mock_credentials_from_identity_pool, patch.object(
+ vertex_base, "refresh_auth"
+ ) as mock_refresh:
+
+ def mock_refresh_impl(creds):
+ creds.token = "refreshed-token"
+
+ mock_refresh.side_effect = mock_refresh_impl
+
+ if is_async:
+ token, _ = await vertex_base._ensure_access_token_async(
+ credentials=credentials,
+ project_id=None,
+ custom_llm_provider="vertex_ai",
+ )
+ else:
+ token, _ = vertex_base._ensure_access_token(
+ credentials=credentials,
+ project_id=None,
+ custom_llm_provider="vertex_ai",
+ )
+
+ assert mock_credentials_from_identity_pool.called
+ assert token == "refreshed-token"
+
+ @pytest.mark.parametrize("is_async", [True, False], ids=["async", "sync"])
+ @pytest.mark.asyncio
+ async def test_identity_pool_credentials_with_aws(self, is_async):
+ vertex_base = VertexBase()
+
+ # Test case: Using Workload Identity Federation for Microsoft Azure and
+ # OIDC identity providers (default behavior)
+ credentials = {
+ "project_id": "test-project",
+ "refresh_token": "fake-refresh-token",
+ "type": "external_account",
+ "credential_source": {
+ "environment_id": "aws1"
+ }
+ }
+ mock_creds = MagicMock()
+ mock_creds.token = "token-1"
+ mock_creds.expired = False
+ mock_creds.project_id = "test-project"
+
+ with patch.object(
+ vertex_base, "_credentials_from_identity_pool_with_aws", return_value=mock_creds
+ ) as mock_credentials_from_identity_pool_with_aws, patch.object(
+ vertex_base, "refresh_auth"
+ ) as mock_refresh:
+
+ def mock_refresh_impl(creds):
+ creds.token = "refreshed-token"
+
+ mock_refresh.side_effect = mock_refresh_impl
+
+ if is_async:
+ token, _ = await vertex_base._ensure_access_token_async(
+ credentials=credentials,
+ project_id=None,
+ custom_llm_provider="vertex_ai",
+ )
+ else:
+ token, _ = vertex_base._ensure_access_token(
+ credentials=credentials,
+ project_id=None,
+ custom_llm_provider="vertex_ai",
+ )
+
+ assert mock_credentials_from_identity_pool_with_aws.called
+ assert token == "refreshed-token"
+
+
@pytest.mark.parametrize(
"api_base, vertex_location, expected",
[
@@ -270,6 +362,7 @@ class TestVertexBase:
),
],
)
+
def test_get_api_base(self, api_base, vertex_location, expected):
vertex_base = VertexBase()
assert (