Skip to content

Commit 153a1de

Browse files
authored
chore: Managing tenant in JSONRPC. (#613)
If the tenant is not defined in the JSONRPC payload, we use the one from the request path. Fixes #612 🦕 Signed-off-by: Emmanuel Hugonnet <ehugonne@redhat.com>
1 parent 168ff2d commit 153a1de

7 files changed

Lines changed: 267 additions & 12 deletions

File tree

‎reference/jsonrpc/src/main/java/io/a2a/server/apps/quarkus/A2AServerRoutes.java‎

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import static io.a2a.transport.jsonrpc.context.JSONRPCContextKeys.HEADERS_KEY;
44
import static io.a2a.transport.jsonrpc.context.JSONRPCContextKeys.METHOD_NAME_KEY;
5+
import static io.a2a.transport.jsonrpc.context.JSONRPCContextKeys.TENANT_KEY;
56
import static io.vertx.core.http.HttpHeaders.CONTENT_TYPE;
67
import static jakarta.ws.rs.core.MediaType.APPLICATION_JSON;
78
import static jakarta.ws.rs.core.MediaType.SERVER_SENT_EVENTS;
@@ -102,7 +103,7 @@ public void invokeJSONRPCHandler(@Body String body, RoutingContext rc) {
102103
Multi<? extends A2AResponse<?>> streamingResponse = null;
103104
A2AErrorResponse error = null;
104105
try {
105-
A2ARequest<?> request = JSONRPCUtils.parseRequestBody(body);
106+
A2ARequest<?> request = JSONRPCUtils.parseRequestBody(body, extractTenant(rc));
106107
context.getState().put(METHOD_NAME_KEY, request.getMethod());
107108
if (request instanceof NonStreamingJSONRPCRequest nonStreamingRequest) {
108109
nonStreamingResponse = processNonStreamingRequest(nonStreamingRequest, context);
@@ -218,7 +219,6 @@ static void setStreamingMultiSseSupportSubscribedRunnable(Runnable runnable) {
218219
}
219220

220221
private ServerCallContext createCallContext(RoutingContext rc) {
221-
222222
if (callContextFactory.isUnsatisfied()) {
223223
User user;
224224
if (rc.user() == null) {
@@ -245,6 +245,7 @@ public String getUsername() {
245245
Set<String> headerNames = rc.request().headers().names();
246246
headerNames.forEach(name -> headers.put(name, rc.request().getHeader(name)));
247247
state.put(HEADERS_KEY, headers);
248+
state.put(TENANT_KEY, extractTenant(rc));
248249

249250
// Extract requested protocol version from X-A2A-Version header
250251
String requestedVersion = rc.request().getHeader(A2AHeaders.X_A2A_VERSION);
@@ -260,6 +261,20 @@ public String getUsername() {
260261
}
261262
}
262263

264+
private String extractTenant(RoutingContext rc) {
265+
String tenantPath = rc.normalizedPath();
266+
if (tenantPath == null || tenantPath.isBlank()) {
267+
return "";
268+
}
269+
if (tenantPath.startsWith("/")) {
270+
tenantPath = tenantPath.substring(1);
271+
}
272+
if(tenantPath.endsWith("/")) {
273+
tenantPath = tenantPath.substring(0, tenantPath.length() -1);
274+
}
275+
return tenantPath;
276+
}
277+
263278
private static String serializeResponse(A2AResponse<?> response) {
264279
// For error responses, use Jackson serialization (errors are standardized)
265280
if (response instanceof A2AErrorResponse error) {

‎reference/jsonrpc/src/test/java/io/a2a/server/apps/quarkus/A2AServerRoutesTest.java‎

Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import static io.a2a.spec.A2AMethods.SEND_STREAMING_MESSAGE_METHOD;
66
import static io.a2a.spec.AgentCard.CURRENT_PROTOCOL_VERSION;
77
import static io.a2a.transport.jsonrpc.context.JSONRPCContextKeys.METHOD_NAME_KEY;
8+
import static io.a2a.transport.jsonrpc.context.JSONRPCContextKeys.TENANT_KEY;
89
import static java.util.Collections.singletonList;
910
import static org.junit.jupiter.api.Assertions.assertEquals;
1011
import static org.junit.jupiter.api.Assertions.assertNotNull;
@@ -108,6 +109,7 @@ public void setUp() {
108109
when(mockRoutingContext.user()).thenReturn(null);
109110
when(mockRequest.headers()).thenReturn(mockHeaders);
110111
when(mockRoutingContext.body()).thenReturn(mockRequestBody);
112+
when(mockRoutingContext.normalizedPath()).thenReturn("/");
111113

112114
// Chain the response methods properly
113115
when(mockHttpResponse.setStatusCode(any(Integer.class))).thenReturn(mockHttpResponse);
@@ -520,6 +522,202 @@ public void testGetExtendedCard_MethodNameSetInContext() {
520522
assertEquals(GET_EXTENDED_AGENT_CARD_METHOD, capturedContext.getState().get(METHOD_NAME_KEY));
521523
}
522524

525+
@Test
526+
public void testTenantExtraction_MultiSegmentPath() {
527+
// Arrange - simulate request to /test/titi
528+
when(mockRoutingContext.normalizedPath()).thenReturn("/test/titi");
529+
String jsonRpcRequest = """
530+
{
531+
"jsonrpc": "2.0",
532+
"id": "cd4c76de-d54c-436c-8b9f-4c2703648d64",
533+
"method": "GetTask",
534+
"params": {
535+
"name": "tasks/de38c76d-d54c-436c-8b9f-4c2703648d64",
536+
"historyLength": 10
537+
}
538+
}""";
539+
when(mockRequestBody.asString()).thenReturn(jsonRpcRequest);
540+
541+
Task responseTask = Task.builder()
542+
.id("de38c76d-d54c-436c-8b9f-4c2703648d64")
543+
.contextId("context-1234")
544+
.status(new TaskStatus(TaskState.SUBMITTED))
545+
.build();
546+
GetTaskResponse realResponse = new GetTaskResponse("1", responseTask);
547+
when(mockJsonRpcHandler.onGetTask(any(GetTaskRequest.class), any(ServerCallContext.class)))
548+
.thenReturn(realResponse);
549+
550+
ArgumentCaptor<ServerCallContext> contextCaptor = ArgumentCaptor.forClass(ServerCallContext.class);
551+
552+
// Act
553+
routes.invokeJSONRPCHandler(jsonRpcRequest, mockRoutingContext);
554+
555+
// Assert
556+
verify(mockJsonRpcHandler).onGetTask(any(GetTaskRequest.class), contextCaptor.capture());
557+
ServerCallContext capturedContext = contextCaptor.getValue();
558+
assertNotNull(capturedContext);
559+
assertEquals("test/titi", capturedContext.getState().get(TENANT_KEY));
560+
}
561+
562+
@Test
563+
public void testTenantExtraction_RootPath() {
564+
// Arrange - simulate request to /
565+
when(mockRoutingContext.normalizedPath()).thenReturn("/");
566+
String jsonRpcRequest = """
567+
{
568+
"jsonrpc": "2.0",
569+
"id": "cd4c76de-d54c-436c-8b9f-4c2703648d64",
570+
"method": "GetTask",
571+
"params": {
572+
"name": "tasks/de38c76d-d54c-436c-8b9f-4c2703648d64",
573+
"historyLength": 10
574+
}
575+
}""";
576+
when(mockRequestBody.asString()).thenReturn(jsonRpcRequest);
577+
578+
Task responseTask = Task.builder()
579+
.id("de38c76d-d54c-436c-8b9f-4c2703648d64")
580+
.contextId("context-1234")
581+
.status(new TaskStatus(TaskState.SUBMITTED))
582+
.build();
583+
GetTaskResponse realResponse = new GetTaskResponse("1", responseTask);
584+
when(mockJsonRpcHandler.onGetTask(any(GetTaskRequest.class), any(ServerCallContext.class)))
585+
.thenReturn(realResponse);
586+
587+
ArgumentCaptor<ServerCallContext> contextCaptor = ArgumentCaptor.forClass(ServerCallContext.class);
588+
589+
// Act
590+
routes.invokeJSONRPCHandler(jsonRpcRequest, mockRoutingContext);
591+
592+
// Assert
593+
verify(mockJsonRpcHandler).onGetTask(any(GetTaskRequest.class), contextCaptor.capture());
594+
ServerCallContext capturedContext = contextCaptor.getValue();
595+
assertNotNull(capturedContext);
596+
assertEquals("", capturedContext.getState().get(TENANT_KEY));
597+
}
598+
599+
@Test
600+
public void testTenantExtraction_SingleSegmentPath() {
601+
// Arrange - simulate request to /tenant1
602+
when(mockRoutingContext.normalizedPath()).thenReturn("/tenant1");
603+
String jsonRpcRequest = """
604+
{
605+
"jsonrpc": "2.0",
606+
"id": "cd4c76de-d54c-436c-8b9f-4c2703648d64",
607+
"method": "GetTask",
608+
"params": {
609+
"name": "tasks/de38c76d-d54c-436c-8b9f-4c2703648d64",
610+
"historyLength": 10
611+
}
612+
}""";
613+
when(mockRequestBody.asString()).thenReturn(jsonRpcRequest);
614+
615+
Task responseTask = Task.builder()
616+
.id("de38c76d-d54c-436c-8b9f-4c2703648d64")
617+
.contextId("context-1234")
618+
.status(new TaskStatus(TaskState.SUBMITTED))
619+
.build();
620+
GetTaskResponse realResponse = new GetTaskResponse("1", responseTask);
621+
when(mockJsonRpcHandler.onGetTask(any(GetTaskRequest.class), any(ServerCallContext.class)))
622+
.thenReturn(realResponse);
623+
624+
ArgumentCaptor<ServerCallContext> contextCaptor = ArgumentCaptor.forClass(ServerCallContext.class);
625+
626+
// Act
627+
routes.invokeJSONRPCHandler(jsonRpcRequest, mockRoutingContext);
628+
629+
// Assert
630+
verify(mockJsonRpcHandler).onGetTask(any(GetTaskRequest.class), contextCaptor.capture());
631+
ServerCallContext capturedContext = contextCaptor.getValue();
632+
assertNotNull(capturedContext);
633+
assertEquals("tenant1", capturedContext.getState().get(TENANT_KEY));
634+
}
635+
636+
@Test
637+
public void testTenantExtraction_ThreeSegmentPath() {
638+
// Arrange - simulate request to /tenant1/api/v1
639+
when(mockRoutingContext.normalizedPath()).thenReturn("/tenant1/api/v1");
640+
String jsonRpcRequest = """
641+
{
642+
"jsonrpc": "2.0",
643+
"id": "cd4c76de-d54c-436c-8b9f-4c2703648d64",
644+
"method": "GetTask",
645+
"params": {
646+
"name": "tasks/de38c76d-d54c-436c-8b9f-4c2703648d64",
647+
"historyLength": 10
648+
}
649+
}""";
650+
when(mockRequestBody.asString()).thenReturn(jsonRpcRequest);
651+
652+
Task responseTask = Task.builder()
653+
.id("de38c76d-d54c-436c-8b9f-4c2703648d64")
654+
.contextId("context-1234")
655+
.status(new TaskStatus(TaskState.SUBMITTED))
656+
.build();
657+
GetTaskResponse realResponse = new GetTaskResponse("1", responseTask);
658+
when(mockJsonRpcHandler.onGetTask(any(GetTaskRequest.class), any(ServerCallContext.class)))
659+
.thenReturn(realResponse);
660+
661+
ArgumentCaptor<ServerCallContext> contextCaptor = ArgumentCaptor.forClass(ServerCallContext.class);
662+
663+
// Act
664+
routes.invokeJSONRPCHandler(jsonRpcRequest, mockRoutingContext);
665+
666+
// Assert
667+
verify(mockJsonRpcHandler).onGetTask(any(GetTaskRequest.class), contextCaptor.capture());
668+
ServerCallContext capturedContext = contextCaptor.getValue();
669+
assertNotNull(capturedContext);
670+
assertEquals("tenant1/api/v1", capturedContext.getState().get(TENANT_KEY));
671+
}
672+
673+
@Test
674+
public void testTenantExtraction_StreamingRequest() {
675+
// Arrange - simulate streaming request to /myTenant/api
676+
when(mockRoutingContext.normalizedPath()).thenReturn("/myTenant/api");
677+
String jsonRpcRequest = """
678+
{
679+
"jsonrpc": "2.0",
680+
"id": "cd4c76de-d54c-436c-8b9f-4c2703648d64",
681+
"method": "SendStreamingMessage",
682+
"params": {
683+
"message": {
684+
"messageId": "message-1234",
685+
"contextId": "context-1234",
686+
"role": "ROLE_USER",
687+
"parts": [
688+
{
689+
"text": "tell me a joke"
690+
}
691+
],
692+
"metadata": {}
693+
},
694+
"configuration": {
695+
"acceptedOutputModes": ["text"],
696+
"blocking": true
697+
},
698+
"metadata": {}
699+
}
700+
}""";
701+
when(mockRequestBody.asString()).thenReturn(jsonRpcRequest);
702+
703+
@SuppressWarnings("unchecked")
704+
Flow.Publisher<SendStreamingMessageResponse> mockPublisher = mock(Flow.Publisher.class);
705+
when(mockJsonRpcHandler.onMessageSendStream(any(SendStreamingMessageRequest.class),
706+
any(ServerCallContext.class))).thenReturn(mockPublisher);
707+
708+
ArgumentCaptor<ServerCallContext> contextCaptor = ArgumentCaptor.forClass(ServerCallContext.class);
709+
710+
// Act
711+
routes.invokeJSONRPCHandler(jsonRpcRequest, mockRoutingContext);
712+
713+
// Assert
714+
verify(mockJsonRpcHandler).onMessageSendStream(any(SendStreamingMessageRequest.class),
715+
contextCaptor.capture());
716+
ServerCallContext capturedContext = contextCaptor.getValue();
717+
assertNotNull(capturedContext);
718+
assertEquals("myTenant/api", capturedContext.getState().get(TENANT_KEY));
719+
}
720+
523721
/**
524722
* Helper method to set a field via reflection for testing purposes.
525723
*/

‎reference/rest/src/main/java/io/a2a/server/rest/quarkus/A2AServerRoutes.java‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@
5858
import static io.a2a.spec.A2AMethods.SET_TASK_PUSH_NOTIFICATION_CONFIG_METHOD;
5959
import static io.a2a.spec.A2AMethods.SUBSCRIBE_TO_TASK_METHOD;
6060

61+
import static io.a2a.transport.rest.context.RestContextKeys.TENANT_KEY;
62+
6163
@Singleton
6264
@Authenticated
6365
public class A2AServerRoutes {
@@ -443,6 +445,7 @@ public String getUsername() {
443445
headerNames.forEach(name -> headers.put(name, rc.request().getHeader(name)));
444446
state.put(HEADERS_KEY, headers);
445447
state.put(METHOD_NAME_KEY, jsonRpcMethodName);
448+
state.put(TENANT_KEY, extractTenant(rc));
446449

447450
// Extract requested protocol version from X-A2A-Version header
448451
String requestedVersion = rc.request().getHeader(A2AHeaders.X_A2A_VERSION);

0 commit comments

Comments
 (0)