gnodet commented on code in PR #25621:
URL: https://github.com/apache/camel/pull/25621#discussion_r3853328772


##########
components/camel-spiffe/src/main/java/org/apache/camel/component/spiffe/SpiffeProducer.java:
##########
@@ -0,0 +1,110 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.spiffe;
+
+import java.util.Arrays;
+
+import io.spiffe.svid.jwtsvid.JwtSvid;
+import io.spiffe.svid.x509svid.X509Svid;
+import io.spiffe.workloadapi.WorkloadApiClient;
+import org.apache.camel.Exchange;
+import org.apache.camel.Message;
+import org.apache.camel.support.DefaultProducer;
+import org.apache.camel.util.ObjectHelper;
+
+public class SpiffeProducer extends DefaultProducer {
+
+    public SpiffeProducer(final SpiffeEndpoint endpoint) {
+        super(endpoint);
+    }
+
+    @Override
+    public SpiffeEndpoint getEndpoint() {
+        return (SpiffeEndpoint) super.getEndpoint();
+    }
+
+    @Override
+    public void process(Exchange exchange) throws Exception {
+        final WorkloadApiClient client = getEndpoint().getWorkloadApiClient();
+        switch (determineOperation(exchange)) {
+            case fetchX509Svid -> fetchX509Svid(client, exchange);
+            case fetchJwtSvid -> fetchJwtSvid(client, exchange);
+            case validateJwtSvid -> validateJwtSvid(client, exchange);
+            default -> throw new IllegalArgumentException("Unsupported 
operation");
+        }
+    }
+
+    private void fetchX509Svid(WorkloadApiClient client, Exchange exchange) 
throws Exception {
+        X509Svid svid = client.fetchX509Context().getDefaultSvid();
+        Message message = getMessageForResponse(exchange);
+        message.setBody(svid);
+        message.setHeader(SpiffeConstants.SPIFFE_ID, 
svid.getSpiffeId().toString());
+    }
+
+    private void fetchJwtSvid(WorkloadApiClient client, Exchange exchange) 
throws Exception {
+        String[] audiences = resolveAudiences(exchange);
+        JwtSvid svid = audiences.length > 1
+                ? client.fetchJwtSvid(audiences[0], 
Arrays.copyOfRange(audiences, 1, audiences.length))
+                : client.fetchJwtSvid(audiences[0]);
+        Message message = getMessageForResponse(exchange);
+        message.setBody(svid.getToken());
+        message.setHeader(SpiffeConstants.SPIFFE_ID, 
svid.getSpiffeId().toString());
+        message.setHeader(SpiffeConstants.EXPIRY, svid.getExpiry());
+    }
+
+    private void validateJwtSvid(WorkloadApiClient client, Exchange exchange) 
throws Exception {
+        String token = exchange.getIn().getHeader(SpiffeConstants.TOKEN, 
String.class);
+        if (ObjectHelper.isEmpty(token)) {
+            token = exchange.getIn().getBody(String.class);
+        }
+        if (ObjectHelper.isEmpty(token)) {
+            throw new IllegalArgumentException(
+                    "A JWT-SVID token is required for validateJwtSvid (set the 
CamelSpiffeToken header or the body)");
+        }
+        String[] audiences = resolveAudiences(exchange);
+        JwtSvid svid = client.validateJwtSvid(token, audiences[0]);
+        Message message = getMessageForResponse(exchange);
+        message.setBody(svid);

Review Comment:
   **Observation (non-blocking):** `validateJwtSvid` silently uses only 
`audiences[0]` when the user provides multiple comma-separated audiences. This 
is correct for the SPIFFE API (which validates one audience at a time), but 
it's inconsistent with `fetchJwtSvid` which passes all audiences through.
   
   A user who sets `audience=aud1,aud2` may not realize only `aud1` is used for 
validation. Consider either:
   - Logging a warning when extra audiences are ignored, or
   - Documenting this single-audience behavior explicitly in the `@UriParam` 
description



##########
components/camel-spiffe/src/main/java/org/apache/camel/component/spiffe/SpiffeConfiguration.java:
##########
@@ -0,0 +1,93 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.spiffe;
+
+import io.spiffe.workloadapi.WorkloadApiClient;
+import org.apache.camel.RuntimeCamelException;
+import org.apache.camel.spi.UriParam;
+import org.apache.camel.spi.UriParams;
+
+@UriParams
+public class SpiffeConfiguration implements Cloneable {
+
+    @UriParam(defaultValue = "fetchX509Svid")
+    private SpiffeOperation operation = SpiffeOperation.fetchX509Svid;
+
+    @UriParam(label = "security")
+    private String spiffeSocketPath;
+
+    @UriParam
+    private String audience;
+
+    @UriParam(label = "advanced", description = "An existing WorkloadApiClient 
to use. When set, the component does not"
+                                                + " create or close its own 
client and spiffeSocketPath is ignored.")
+    private WorkloadApiClient workloadApiClient;
+
+    /**
+     * The operation to perform on the SPIFFE Workload API.
+     */

Review Comment:
   **Minor convention suggestion (non-blocking):** The `workloadApiClient` 
option is not marked with `autowired = true` on its `@UriParam`. Many Camel 
components mark their injectable client objects as autowired (e.g., AWS 
components with SDK clients), which enables automatic lookup from the Camel 
registry without needing the `#beanRef` syntax.
   
   Consider adding `autowired = true` to the annotation.



##########
components/camel-spiffe/src/main/java/org/apache/camel/component/spiffe/SpiffeProducer.java:
##########
@@ -0,0 +1,110 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.spiffe;
+
+import java.util.Arrays;
+
+import io.spiffe.svid.jwtsvid.JwtSvid;
+import io.spiffe.svid.x509svid.X509Svid;
+import io.spiffe.workloadapi.WorkloadApiClient;
+import org.apache.camel.Exchange;
+import org.apache.camel.Message;
+import org.apache.camel.support.DefaultProducer;
+import org.apache.camel.util.ObjectHelper;
+
+public class SpiffeProducer extends DefaultProducer {
+
+    public SpiffeProducer(final SpiffeEndpoint endpoint) {
+        super(endpoint);
+    }
+
+    @Override
+    public SpiffeEndpoint getEndpoint() {
+        return (SpiffeEndpoint) super.getEndpoint();
+    }
+
+    @Override
+    public void process(Exchange exchange) throws Exception {
+        final WorkloadApiClient client = getEndpoint().getWorkloadApiClient();
+        switch (determineOperation(exchange)) {
+            case fetchX509Svid -> fetchX509Svid(client, exchange);
+            case fetchJwtSvid -> fetchJwtSvid(client, exchange);
+            case validateJwtSvid -> validateJwtSvid(client, exchange);
+            default -> throw new IllegalArgumentException("Unsupported 
operation");
+        }
+    }
+
+    private void fetchX509Svid(WorkloadApiClient client, Exchange exchange) 
throws Exception {
+        X509Svid svid = client.fetchX509Context().getDefaultSvid();
+        Message message = getMessageForResponse(exchange);
+        message.setBody(svid);
+        message.setHeader(SpiffeConstants.SPIFFE_ID, 
svid.getSpiffeId().toString());
+    }
+
+    private void fetchJwtSvid(WorkloadApiClient client, Exchange exchange) 
throws Exception {
+        String[] audiences = resolveAudiences(exchange);
+        JwtSvid svid = audiences.length > 1
+                ? client.fetchJwtSvid(audiences[0], 
Arrays.copyOfRange(audiences, 1, audiences.length))
+                : client.fetchJwtSvid(audiences[0]);
+        Message message = getMessageForResponse(exchange);
+        message.setBody(svid.getToken());
+        message.setHeader(SpiffeConstants.SPIFFE_ID, 
svid.getSpiffeId().toString());
+        message.setHeader(SpiffeConstants.EXPIRY, svid.getExpiry());
+    }
+
+    private void validateJwtSvid(WorkloadApiClient client, Exchange exchange) 
throws Exception {
+        String token = exchange.getIn().getHeader(SpiffeConstants.TOKEN, 
String.class);
+        if (ObjectHelper.isEmpty(token)) {
+            token = exchange.getIn().getBody(String.class);
+        }
+        if (ObjectHelper.isEmpty(token)) {
+            throw new IllegalArgumentException(
+                    "A JWT-SVID token is required for validateJwtSvid (set the 
CamelSpiffeToken header or the body)");
+        }
+        String[] audiences = resolveAudiences(exchange);
+        JwtSvid svid = client.validateJwtSvid(token, audiences[0]);
+        Message message = getMessageForResponse(exchange);
+        message.setBody(svid);
+        message.setHeader(SpiffeConstants.SPIFFE_ID, 
svid.getSpiffeId().toString());
+    }
+
+    private SpiffeOperation determineOperation(Exchange exchange) {
+        SpiffeOperation operation
+                = exchange.getIn().getHeader(SpiffeConstants.OPERATION, 
SpiffeOperation.class);
+        return operation != null ? operation : 
getEndpoint().getConfiguration().getOperation();
+    }
+
+    private String[] resolveAudiences(Exchange exchange) {
+        String audience = exchange.getIn().getHeader(SpiffeConstants.AUDIENCE, 
String.class);
+        if (ObjectHelper.isEmpty(audience)) {
+            audience = getEndpoint().getConfiguration().getAudience();
+        }
+        if (ObjectHelper.isEmpty(audience)) {
+            throw new IllegalArgumentException(
+                    "At least one audience is required (set the audience 
option or the CamelSpiffeAudience header)");
+        }

Review Comment:
   **Minor robustness suggestion (non-blocking):** `resolveAudiences` splits on 
comma and trims, but doesn't filter out empty/blank strings. An input like 
`"aud1,,aud2"` would produce an empty-string audience entry that would be 
passed to the SPIFFE API.
   
   Consider adding a filter:
   ```java
   return Arrays.stream(audience.split(","))
       .map(String::trim)
       .filter(s -> !s.isEmpty())
       .toArray(String[]::new);
   ```



-- 
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