gnodet-bot commented on code in PR #29822:
URL: https://github.com/apache/camel/pull/29822#discussion_r4585985035


##########
components/camel-spiffe/src/main/java/org/apache/camel/component/spiffe/SpiffeProducer.java:
##########
@@ -68,10 +80,21 @@ private void fetchX509Svid(WorkloadApiClient client, 
Exchange exchange) throws E
 
     private void fetchJwtSvid(WorkloadApiClient client, Exchange exchange) 
throws Exception {
         String[] audiences = resolveAudiences(exchange);
-        JwtSvid svid = audiences.length > 1

Review Comment:
   **Stale header leak on exception:** `resolveAudiences(exchange)` executes 
before the `removeHeader` guards. If it throws (e.g. no audience configured → 
`IllegalArgumentException`), `SPIFFE_ID`/`EXPIRY` are never cleared and a prior 
request's identity leaks downstream.
   
   The X.509 path in `fetchX509Svid` correctly clears headers _before_ calling 
any method that can throw. `fetchJwtSvid` should mirror that order:
   ```java
   Message message = getMessageForResponse(exchange);
   message.removeHeader(SpiffeConstants.SPIFFE_ID);
   message.removeHeader(SpiffeConstants.EXPIRY);
   String[] audiences = resolveAudiences(exchange); // ← move after the clears
   ```



##########
components/camel-spiffe/src/test/java/org/apache/camel/component/spiffe/SpiffeProducerTest.java:
##########
@@ -137,6 +139,29 @@ void fetchJwtSvid() throws Exception {
         
assertThat(out.getMessage().getHeader(SpiffeConstants.EXPIRY)).isEqualTo(expiry);
     }
 
+    @Test
+    void fetchJwtSvidMintsTheTokenForTheConfiguredLocalIdentity() throws 
Exception {
+        SpiffeId billing = SpiffeId.parse("spiffe://example.org/billing");
+        SpiffeId id = spiffeId("spiffe://example.org/billing");
+        Date expiry = new Date();
+        JwtSvid svid = mock(JwtSvid.class);
+        when(svid.getToken()).thenReturn("the-billing-token");
+        when(svid.getSpiffeId()).thenReturn(id);
+        when(svid.getExpiry()).thenReturn(expiry);
+        when(client.fetchJwtSvid(billing, "my-audience")).thenReturn(svid);
+
+        Exchange out = template.request(
+                
"spiffe:test?workloadApiClient=#client&operation=fetchJwtSvid&audience=my-audience"
+                                        + 
"&localSpiffeId=spiffe://example.org/billing",
+                e -> e.getMessage().setHeader(SpiffeConstants.SPIFFE_ID, 
"stale-identity"));
+
+        
assertThat(out.getMessage().getBody(String.class)).isEqualTo("the-billing-token");
+        
assertThat(out.getMessage().getHeader(SpiffeConstants.SPIFFE_ID)).isEqualTo("spiffe://example.org/billing");
+        
assertThat(out.getMessage().getHeader(SpiffeConstants.EXPIRY)).isEqualTo(expiry);
+        // the default identity is never asked for
+        verify(client, never()).fetchJwtSvid("my-audience");
+    }
+

Review Comment:
   This new test covers only the success path (stale header replaced when fetch 
succeeds). The fail-closed scenario is missing: `SpiffeLocalIdentityTest` has 
`selectionTracksRotationAndFailsOnRemovalWithoutStaleOutputs` which asserts 
`SPIFFE_ID`/`EXPIRY` are `null` after a failed X.509 fetch — an analogous JWT 
test is needed.
   
   Suggested shape (once the ordering fix above is applied):
   ```java
   @Test
   void fetchJwtSvidClearsStaleHeadersOnFailure() throws Exception {
       when(client.fetchJwtSvid(anyString())).thenThrow(new 
RuntimeException("workload api down"));
       Exchange out = template.request(
           
"spiffe:test?workloadApiClient=#client&operation=fetchJwtSvid&audience=aud",
           e -> e.getMessage().setHeader(SpiffeConstants.SPIFFE_ID, "stale"));
       
assertThat(out.getMessage().getHeader(SpiffeConstants.SPIFFE_ID)).isNull();
       assertThat(out.getMessage().getHeader(SpiffeConstants.EXPIRY)).isNull();
   }
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to