BedrockAgentCoreContext.get_workload_access_token() returns None for plain FastAPI runtimes (3 silent gotchas)
Maintainer antworten meist innerhalb von 1 Tag
Dieses Issue hat noch niemand übernommen.
Bewertung
- Schwierigkeit
- 5/5
- Geschätzter Aufwand
- Über eine Woche
- Anfängerfreundlichkeit
- 42/100
- Issue-Typ
- Bug
- Klarheit
- Größtenteils klar
- Aktivitätsstatus
- Ruhig
- Bereich
- authentication, backend, security
Rechercherichtung
Beginne mit _build_request_context in bedrock_agentcore/runtime/app.py, lies anschließend BedrockAgentCoreContext und IdentityClient sowie die Dokumentation runtime-oauth.html. Verfolge, wie einfache FastAPI-Anfragen und Hintergrund-Threads das Token erhalten, und prüfe die dokumentierten IAM-Anforderungen. Als erledigt gilt die Aufgabe, wenn die Maintainer der API-Struktur zustimmen und die Hinweise zu Kontext, Threading und Secrets Manager entsprechend aktualisiert sind.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Beschreibung
Summary
Inside an AgentCore Runtime container, BedrockAgentCoreContext.get_workload_access_token() returns None whenever the agent is served by a plain FastAPI/Starlette ASGI app rather than BedrockAgentCoreApp. This breaks @requires_api_key and @requires_access_token, plus any imperative IdentityClient.get_api_key() callers.
While debugging, I found three sequential silent failures that all need fixing for the documented identity flow to actually work end-to-end. Filing as one issue because they layer — masking each other if you only fix the top one.
Reproduction
- Build an AgentCore Runtime image whose ASGI entrypoint is a plain
fastapi.FastAPI()app exposingPOST /invocations(rather thanBedrockAgentCoreApp+@app.entrypoint). - The agent does work in a background thread spawned via
threading.Thread(target=run_pipeline, ...)(common pattern for return-200-fast async work). - Inside the pipeline, call
BedrockAgentCoreContext.get_workload_access_token(). ObserveNone. - Caller-side: orchestrator Lambda calls
InvokeAgentRuntimeCommand({ ..., runtimeUserId: 'cognito-sub' })with all required IAM (InvokeAgentRuntime+InvokeAgentRuntimeForUser). - Runtime role has
bedrock-agentcore:GetResourceApiKey+GetWorkloadAccessToken*+bedrock-agentcore-identity!*SLR exists.
Three silent failures, in the order you hit them
1. Plain FastAPI bypasses _build_request_context
The SDK populates the ContextVar via _build_request_context in bedrock_agentcore/runtime/app.py (called from BedrockAgentCoreApp._handle_invocation lines 537+). Plain FastAPI/Starlette apps bypass this entirely — the middleware never installs, so BedrockAgentCoreContext.set_workload_access_token(...) is never called even though the platform delivered the token on the wire.
Header diagnostic (logged from inside our handler against a SigV4-authorized runtime in us-east-1, 2026-05-18) — the platform sends the token under TWO header spellings on the same request:
HEADER (token-shaped) workloadaccesstoken: AgV4diWP... # matches ACCESS_TOKEN_HEADER constant
HEADER (token-shaped) x-amzn-bedrock-agentcore-runtime-workload-accesstoken: AgV4diWP... # undocumented
The undocumented long-form should either be removed or aliased explicitly. Today, code that reads only WorkloadAccessToken per the SDK constant is correct; code that reads the long form is relying on undocumented behavior.
2. ContextVar is per-thread
After bridging the header in user middleware, get_workload_access_token() still returned None in our pipeline. Cause: Python ContextVar storage is per-thread, not propagated across threading.Thread boundaries. Setting the token in the request handler thread doesn't reach a pipeline thread spawned from the handler.
This isn't called out in the BedrockAgentCoreContext docstring or in the runtime-oauth.html documentation. Issue #219 alludes to it (ContextVar is "safe under ASGI") but doesn't surface the threading caveat that bites users running long pipelines outside the request task.
3. Runtime role needs secretsmanager:GetSecretValue on bedrock-agentcore-identity!*
After (1) and (2) were fixed, IdentityClient.get_api_key() actually fired and got:
AccessDeniedException: ... not authorized to perform: secretsmanager:GetSecretValue
AgentCore Identity stores api-key credentials in Secrets Manager under reserved prefix bedrock-agentcore-identity!*. The GetResourceApiKey API surfaces the underlying secret to the caller, and AWS verifies the caller role (the runtime execution role) has GetSecretValue on the actual secret resource — not the SLR. This is not documented in https://docs.aws.amazon.com/bedrock-agentcore/latest/devguide/runtime-oauth.html or in the GetResourceApiKey API reference.
Tightly-scoped fix:
runtime_role.addToPrincipalPolicy(new iam.PolicyStatement({
actions: ['secretsmanager:GetSecretValue'],
resources: [
f'arn:aws:secretsmanager:{region}:{account}:secret:bedrock-agentcore-identity!*',
],
}))
This is an SDK-adjacent issue rather than a strict SDK bug — but the right place to surface it is alongside the other Identity-flow gotchas, and the SDK's IdentityClient.get_api_key could include the canonical IAM grant in its docstring.
Workaround
# 1. Read the workload token off the request and thread it explicitly into
# the background pipeline so the new thread can re-set the ContextVar.
def _extract_workload_access_token(request: Request) -> str:
return (
request.headers.get("WorkloadAccessToken")
or request.headers.get("x-amzn-bedrock-agentcore-runtime-workload-accesstoken")
or ""
)
@app.post("/invocations")
async def invoke(request: Request, body: ...):
token = _extract_workload_access_token(request)
# ... start background thread, pass token as kwarg ...
spawn(target=run_pipeline, kwargs={..., 'workload_access_token': token})
return {"accepted": True}
def run_pipeline(*, workload_access_token: str = "", **kwargs):
if workload_access_token:
from bedrock_agentcore.runtime.context import BedrockAgentCoreContext
BedrockAgentCoreContext.set_workload_access_token(workload_access_token)
# ... rest of pipeline runs in this thread; @requires_api_key works now ...
Plus IAM grant on the runtime execution role (above).
Expected behavior
At least one of:
-
An exported, opt-in middleware:
bedrock_agentcore.runtime.middleware.BedrockAgentCoreContextMiddlewareusers canapp.add_middleware(...)to get parity withBedrockAgentCoreAppfor the request-handler path. Combined with a clearly-documented per-thread propagation pattern in the docstring. -
OR a
BedrockAgentCoreContextAPI that doesn't depend on ContextVar: e.g.,BedrockAgentCoreContext.from_request(request)that reads the headers directly and returns a context object the user can pass into@requires_api_key(context=...). Sidesteps both the middleware gap and the threading gap. -
AND: docstring updates on
BedrockAgentCoreContext,IdentityClient, and the Identity getting-started doc that explicitly call out:- "ContextVar is per-thread; if you spawn background threads, re-set the token on entry"
- "Caller IAM principal must have
secretsmanager:GetSecretValueonbedrock-agentcore-identity!*" - The undocumented long-form header — either remove or formally alias
Environment
bedrock-agentcore1.9.1(PyPI)- AgentCore Runtime: ARM64 container, Python 3.13, FastAPI 0.136.1, uvicorn 0.47.0
- Region:
us-east-1 - Inbound auth: SigV4 (default IAM, no
AuthorizerConfigurationset)
PR proposal
I'm willing to send a PR for the middleware + docstring updates. Two questions before I draft:
- Shape preference? Opt-in
BedrockAgentCoreContextMiddlewarethat users explicitly add (option 1 above) vs. helper API onBedrockAgentCoreContext(option 2)? The middleware is a smaller diff but only fixes the request-handler path; the helper is more invasive but addresses the threading gotcha cleanly. - Should the threading caveat live in code (e.g., a
BedrockAgentCoreContext.copy_for_thread()helper) or just docs?
Happy to send whichever shape maintainers prefer.
- Vorherrschende Sprache
- Python
- Sterne
- 786
- Forks
- 163
- Ø Merge
- 4 Std.
- Gemergte PRs (30 T.)
- 22
Entwicklungsumgebung
- Kein Dockerfile und keine Docker-Compose-Datei
- Keine Pull-Request-Vorlage
- Beitragsleitfaden lesen
Erste Schritte
- Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
- Forken Sie das Repository und arbeiten Sie in einem Branch.
- Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.
Mehr aus aws/bedrock-agentcore-sdk-python
-
bug high-severity
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 72/100
aws/bedrock-agentcore-sdk-python#752 ·
Maintainer antworten meist innerhalb von 1 Tag
-
OpenAIConverseConverter restores an empty history when one blob or text payload fails to parseOffenbug
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 82/100
aws/bedrock-agentcore-sdk-python#751 · 1 Reaktion ·
Maintainer antworten meist innerhalb von 1 Tag
-
delete_all_long_term_memories_in_namespace deletes only the first 10 recordsEvtl. vergeben @Fire162 hat das vor 18 Tagen übernommen. Offenbug high-severity
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 75/100
aws/bedrock-agentcore-sdk-python#727 ·
Maintainer antworten meist innerhalb von 1 Tag
-
MPP: credential is always sent in Authorization, ignoring header="Payment-Authorization" in the challengeEvtl. vergeben @abhiramshaji-cyber hat das vor 25 Tagen übernommen. Offenbug
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 68/100
aws/bedrock-agentcore-sdk-python#702 ·
Maintainer antworten meist innerhalb von 1 Tag
-
[Bug] update_message fails with parameter validation error when SessionMessage.message_id is a Strands positional integer indexEvtl. vergeben @citizen204 hat das vor 144 Tagen übernommen. Offen
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 68/100
aws/bedrock-agentcore-sdk-python#556 ·
Maintainer antworten meist innerhalb von 1 Tag
Alle Issues in aws/bedrock-agentcore-sdk-python
Ähnliche Issues
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 82/100
-
bug difficulty:2
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 72/100
MakazhanAlpamys/Soup#3167 · 1 Kommentar ·
Maintainer antworten meist innerhalb von 1 Tag
-
Schwierigkeit 1/5 Unter einer Stunde Anfängerfreundlichkeit 88/100
snap-stanford/Biomni#380 ·
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 70/100
jsboige/CoursIA#27768 · 2 Kommentare ·
Maintainer antworten meist innerhalb von 1 Tag
-
Schwierigkeit 1/5 Unter einer Stunde Anfängerfreundlichkeit 66/100
rossant/awesome-math#206 ·