fix(a2a): send agent litellm_params.api_key as bearer auth on the A2A hop (#49474)

* test(a2a): cover agent api key authentication

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>

* fix(a2a): authenticate plain agent hops with api key

Co-authored-by: Afnan Mojumder <afnan.mojumder@gmail.com>
Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>

---------

Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-authored-by: Afnan Mojumder <afnan.mojumder@gmail.com>
This commit is contained in:
moe-berri 2026-10-31 12:06:52 -07:00 • committed by GitHub
parent 6f86b117b5
commit 7e4403ea66
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 155 additions and 9 deletions

View file

@ -160,8 +160,11 @@ async def resolve_a2a_hop_auth_header(
litellm_params: Mapping[str, object], litellm_params: Mapping[str, object],
custom_llm_provider: object, custom_llm_provider: object,
resolve_entra_header: AgentAuthHeaderResolver = resolve_azure_ai_agent_auth_header, resolve_entra_header: AgentAuthHeaderResolver = resolve_azure_ai_agent_auth_header,
agent_extra_headers: Mapping[str, str] | None = None,
) -> Mapping[str, str] | None: ) -> Mapping[str, str] | None:
"""Entra credentials authenticate the A2A hop only; a completion-bridge agent hands them to the model provider it bridges to.""" if a2a_hop_uses_entra(litellm_params, custom_llm_provider):
if not a2a_hop_uses_entra(litellm_params, custom_llm_provider): return await resolve_entra_header(litellm_params)
if custom_llm_provider or any(name.lower() == "authorization" for name in (agent_extra_headers or {})):
return None return None
return await resolve_entra_header(litellm_params) api_key: Final = litellm_params.get("api_key")
return {"Authorization": f"Bearer {api_key}"} if isinstance(api_key, str) and api_key else None

View file

@ -172,13 +172,16 @@ def _caller_identity_headers(user_api_key_dict: UserAPIKeyAuth) -> Mapping[str,
async def _resolve_backend_auth( async def _resolve_backend_auth(
litellm_params: dict[str, object], litellm_params: dict[str, object],
custom_llm_provider: object, custom_llm_provider: object,
agent_extra_headers: Mapping[str, str] | None = None,
) -> AgentBackendAuth: ) -> AgentBackendAuth:
databricks_auth: Final = await resolve_databricks_backend_auth(litellm_params) databricks_auth: Final = await resolve_databricks_backend_auth(litellm_params)
if has_databricks_oauth(litellm_params): if has_databricks_oauth(litellm_params):
return databricks_auth return databricks_auth
return AgentBackendAuth( return AgentBackendAuth(
litellm_params=databricks_auth.litellm_params, litellm_params=databricks_auth.litellm_params,
header=await resolve_a2a_hop_auth_header(litellm_params, custom_llm_provider), header=await resolve_a2a_hop_auth_header(
litellm_params, custom_llm_provider, agent_extra_headers=agent_extra_headers
),
) )
@ -834,14 +837,15 @@ async def invoke_agent_a2a(
if header_name: if header_name:
dynamic_headers[header_name] = val dynamic_headers[header_name] = val
backend_auth: Final = await _resolve_backend_auth(litellm_params, custom_llm_provider) merged_agent_headers: Final = merge_agent_headers(
dynamic_headers=dynamic_headers or None,
static_headers=static_headers or None,
)
backend_auth: Final = await _resolve_backend_auth(litellm_params, custom_llm_provider, merged_agent_headers)
agent_extra_headers: Final = _forwarding_headers( agent_extra_headers: Final = _forwarding_headers(
caller_identity=caller_identity, caller_identity=caller_identity,
request_data=data, request_data=data,
agent_extra_headers=merge_agent_headers( agent_extra_headers=merged_agent_headers,
dynamic_headers=dynamic_headers or None,
static_headers=static_headers or None,
),
backend_auth_header=backend_auth.header, backend_auth_header=backend_auth.header,
) )
forwarded_litellm_params: Final = backend_auth.litellm_params forwarded_litellm_params: Final = backend_auth.litellm_params

View file

@ -348,6 +348,105 @@ _TRANSPORT_HEADERS: Final = frozenset(
) )
def _a2a_auth_requests(
gateway: Gateway, method: str, agent_options: dict[str, JsonValue], client_headers: dict[str, str]
) -> tuple[Request, ...]:
marker: Final = "a2akey" + uuid.uuid4().hex[:12]
def upstream(request: Request) -> Reply:
if request.method == "GET":
return Reply(body=json.dumps(_peer_card(wire.url, marker)).encode())
body: Final = json.loads(request.body)
if body["method"] == "message/stream":
frames: Final = tuple(
f"data: {json.dumps({'jsonrpc': '2.0', 'id': body['id'], 'result': event})}\n\n".encode()
for event in _STREAM_EVENTS
)
return Reply(content_type="text/event-stream", chunks=frames)
if body["method"] == "tasks/get":
return Reply(
body=json.dumps(
{
"jsonrpc": "2.0",
"id": body["id"],
"result": {"kind": "task", "id": "t1", "contextId": "c1", "status": {"state": "completed"}},
}
).encode()
)
return _message_reply(body, "key test")
with wire_server(upstream) as wire, gateway.scenario() as scenario:
identity: Final = _register_agent(
gateway,
scenario,
{"agent_name": marker, "agent_card_params": _peer_card(wire.url, marker, "1.0"), **agent_options},
)
params: Final = {"id": "t1"} if method == "tasks/get" else {"message": _MESSAGE_V10}
headers: Final = {
"Authorization": f"Bearer {gateway.key}",
"a2a-version": "1.0",
**{name.replace("{agent}", marker): value for name, value in client_headers.items()},
}
response: Final = gateway.client.post(
f"/a2a/{identity}/message/send",
headers=headers,
json={"jsonrpc": "2.0", "id": marker, "method": method, "params": params},
)
assert response.status_code == 200, response.text
assert "error" not in response.text, response.text
return wire.drain()
@pytest.mark.parametrize(
("method", "observed_method"),
(("SendMessage", "GET"), ("SendMessage", "POST"), ("SendStreamingMessage", "POST"), ("tasks/get", "POST")),
ids=("card", "send", "stream", "task"),
)
def test_regression_db_agent_api_key_is_bearer_on_each_a2a_hop(
gateway: Gateway, method: str, observed_method: str
) -> None:
key: Final = "synthetic-backend-" + uuid.uuid4().hex
requests: Final = _a2a_auth_requests(gateway, method, {"litellm_params": {"api_key": key}}, {})
observed: Final = tuple(request for request in requests if request.method == observed_method)
assert len(observed) == 1, requests
assert observed[0].headers.get("authorization") == f"Bearer {key}", observed[0].headers
@pytest.mark.parametrize("header_name", ("Authorization", "aUtHoRiZaTiOn"))
def test_guard_static_authorization_overrides_api_key(gateway: Gateway, header_name: str) -> None:
requests: Final = _a2a_auth_requests(
gateway,
"SendMessage",
{"litellm_params": {"api_key": "backend-key"}, "static_headers": {header_name: "Bearer static-key"}},
{},
)
assert tuple(request.method for request in requests) == ("GET", "POST"), requests
assert tuple(request.headers.get("authorization") for request in requests) == ("Bearer static-key",) * 2
@pytest.mark.parametrize("forwarding", ("configured", "convention"))
def test_guard_forwarded_authorization_arrives_with_api_key(gateway: Gateway, forwarding: str) -> None:
options: Final = {
"litellm_params": {"api_key": "backend-key"},
**({"extra_headers": ["authorization"]} if forwarding == "configured" else {}),
}
client_headers: Final = (
{"x-litellm-api-key": gateway.key, "Authorization": "Bearer client-agent-key"}
if forwarding == "configured"
else {"x-a2a-{agent}-authorization": "Bearer client-agent-key"}
)
requests: Final = _a2a_auth_requests(gateway, "SendMessage", options, client_headers)
assert tuple(request.method for request in requests) == ("GET", "POST"), requests
assert tuple(request.headers.get("authorization") for request in requests) == ("Bearer client-agent-key",) * 2
def test_guard_no_agent_key_or_proxy_caller_key_is_sent(gateway: Gateway) -> None:
requests: Final = _a2a_auth_requests(gateway, "SendMessage", {}, {})
assert tuple(request.method for request in requests) == ("GET", "POST"), requests
assert tuple(request.headers.get("authorization") for request in requests) == (None, None)
assert all(gateway.key not in value for request in requests for value in request.headers.values()), requests
@pytest.mark.parametrize( @pytest.mark.parametrize(
("version", "method", "message"), ("version", "method", "message"),
(("0.3", "message/stream", _MESSAGE_V03), ("1.0", "SendStreamingMessage", _MESSAGE_V10)), (("0.3", "message/stream", _MESSAGE_V03), ("1.0", "SendStreamingMessage", _MESSAGE_V10)),

View file

@ -2,6 +2,7 @@
from collections.abc import Mapping from collections.abc import Mapping
from types import MappingProxyType from types import MappingProxyType
from typing import Final
import pytest import pytest
@ -50,3 +51,42 @@ async def test_agent_without_entra_credentials_gets_no_bearer():
assert header is None assert header is None
assert resolver.calls == [] assert resolver.calls == []
@pytest.mark.parametrize(
("api_key", "expected"),
(
("secret", "Bearer secret"),
("Bearer secret", "Bearer Bearer secret"),
("os.environ/SYNTHETIC_KEY", "Bearer os.environ/SYNTHETIC_KEY"),
("", None),
(None, None),
(42, None),
),
)
async def test_plain_agent_key_is_used_verbatim_only_for_nonempty_strings(
api_key: object, expected: str | None
) -> None:
header: Final = await resolve_a2a_hop_auth_header({"api_key": api_key}, None)
assert header == ({"Authorization": expected} if expected is not None else None)
@pytest.mark.parametrize("provider", ("azure_ai", "databricks_agent"))
async def test_completion_bridge_api_key_is_not_used_for_a2a_hop(provider: str) -> None:
assert await resolve_a2a_hop_auth_header({"api_key": "secret"}, provider) is None
async def test_entra_bearer_overrides_agent_key_and_passthrough() -> None:
resolver: Final = _RecordingEntraResolver()
params: Final = {**_SERVICE_PRINCIPAL, "api_key": "secret"}
header: Final = await resolve_a2a_hop_auth_header(params, None, resolver, {"authorization": "Bearer forwarded"})
assert header == {"Authorization": "Bearer minted-entra-token"}
assert resolver.calls == [params]
@pytest.mark.parametrize("name", ("Authorization", "aUtHoRiZaTiOn"))
async def test_passthrough_authorization_takes_precedence_over_plain_agent_key(name: str) -> None:
assert (
await resolve_a2a_hop_auth_header({"api_key": "secret"}, None, agent_extra_headers={name: "Bearer forwarded"})
is None
)