Skip to content

Commit 7377187

Browse files
evolutiontheorydonmanue
andauthored
Added request header allowlist support (#93)
Add support for specifying request header allowlist feature which is supported with agent core runtime with the latest release. This change will extract any request headers passed as per the request header allowlist. These headers are set in BedrockAgentCoreContext and passed via RequestContext. Co-authored-by: donmanue <[email protected]>
1 parent 8f9bbf5 commit 7377187

File tree

7 files changed

+504
-17
lines changed

7 files changed

+504
-17
lines changed

pyproject.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ classifiers = [
2626
"Topic :: Software Development :: Libraries :: Python Modules",
2727
]
2828
dependencies = [
29-
"boto3>=1.39.7",
30-
"botocore>=1.39.7",
29+
"boto3>=1.40.35",
30+
"botocore>=1.40.35",
3131
"pydantic>=2.0.0,<3.0.0",
3232
"urllib3>=1.26.0",
3333
"starlette>=0.46.2",

src/bedrock_agentcore/runtime/app.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
from .context import BedrockAgentCoreContext, RequestContext
2222
from .models import (
2323
ACCESS_TOKEN_HEADER,
24+
AUTHORIZATION_HEADER,
25+
CUSTOM_HEADER_PREFIX,
2426
REQUEST_ID_HEADER,
2527
SESSION_HEADER,
2628
TASK_ACTION_CLEAR_FORCED_STATUS,
@@ -279,7 +281,27 @@ def _build_request_context(self, request) -> RequestContext:
279281
if agent_identity_token:
280282
BedrockAgentCoreContext.set_workload_access_token(agent_identity_token)
281283

282-
return RequestContext(session_id=session_id)
284+
# Collect relevant request headers (Authorization + Custom headers)
285+
request_headers = {}
286+
287+
# Add Authorization header if present
288+
authorization_header = headers.get(AUTHORIZATION_HEADER)
289+
if authorization_header is not None:
290+
request_headers[AUTHORIZATION_HEADER] = authorization_header
291+
292+
# Add custom headers with the specified prefix
293+
for header_name, header_value in headers.items():
294+
if header_name.lower().startswith(CUSTOM_HEADER_PREFIX.lower()):
295+
request_headers[header_name] = header_value
296+
297+
# Set in context if any headers were found
298+
if request_headers:
299+
BedrockAgentCoreContext.set_request_headers(request_headers)
300+
301+
# Get the headers from context to pass to RequestContext
302+
req_headers = BedrockAgentCoreContext.get_request_headers()
303+
304+
return RequestContext(session_id=session_id, request_headers=req_headers)
283305
except Exception as e:
284306
self.logger.warning("Failed to build request context: %s: %s", type(e).__name__, e)
285307
request_id = str(uuid.uuid4())

src/bedrock_agentcore/runtime/context.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"""
55

66
from contextvars import ContextVar
7-
from typing import Optional
7+
from typing import Dict, Optional
88

99
from pydantic import BaseModel, Field
1010

@@ -13,6 +13,7 @@ class RequestContext(BaseModel):
1313
"""Request context containing metadata from HTTP requests."""
1414

1515
session_id: Optional[str] = Field(None)
16+
request_headers: Optional[Dict[str, str]] = Field(None)
1617

1718

1819
class BedrockAgentCoreContext:
@@ -21,6 +22,7 @@ class BedrockAgentCoreContext:
2122
_workload_access_token: ContextVar[Optional[str]] = ContextVar("workload_access_token")
2223
_request_id: ContextVar[Optional[str]] = ContextVar("request_id")
2324
_session_id: ContextVar[Optional[str]] = ContextVar("session_id")
25+
_request_headers: ContextVar[Optional[Dict[str, str]]] = ContextVar("request_headers")
2426

2527
@classmethod
2628
def set_workload_access_token(cls, token: str):
@@ -56,3 +58,16 @@ def get_session_id(cls) -> Optional[str]:
5658
return cls._session_id.get()
5759
except LookupError:
5860
return None
61+
62+
@classmethod
63+
def set_request_headers(cls, headers: Dict[str, str]):
64+
"""Set request headers in the context."""
65+
cls._request_headers.set(headers)
66+
67+
@classmethod
68+
def get_request_headers(cls) -> Optional[Dict[str, str]]:
69+
"""Get request headers from the context."""
70+
try:
71+
return cls._request_headers.get()
72+
except LookupError:
73+
return None

src/bedrock_agentcore/runtime/models.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ class PingStatus(str, Enum):
1717
SESSION_HEADER = "X-Amzn-Bedrock-AgentCore-Runtime-Session-Id"
1818
REQUEST_ID_HEADER = "X-Amzn-Bedrock-AgentCore-Runtime-Request-Id"
1919
ACCESS_TOKEN_HEADER = "WorkloadAccessToken" # nosec
20+
AUTHORIZATION_HEADER = "Authorization"
21+
CUSTOM_HEADER_PREFIX = "X-Amzn-Bedrock-AgentCore-Runtime-Custom-"
2022

2123
# Task action constants
2224
TASK_ACTION_PING_STATUS = "ping_status"

0 commit comments

Comments
 (0)