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


##########
components/camel-ai/camel-mcp-server/src/test/java/org/apache/camel/component/mcp/server/vertx/VertxMcpPrincipalTest.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.mcp.server.vertx;
+
+import io.vertx.core.json.JsonArray;
+import io.vertx.core.json.JsonObject;
+import io.vertx.ext.auth.User;
+import org.junit.jupiter.api.Test;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+/**
+ * Unit tests for {@link VertxMcpPrincipal}: deriving a runtime-neutral name 
and roles from a Vert.x {@link User}.
+ */
+class VertxMcpPrincipalTest {
+
+    @Test
+    void nameFromUsernameClaim() {
+        User user = User.create(new JsonObject().put("username", "camel"));
+        assertThat(VertxMcpPrincipal.name(user)).isEqualTo("camel");
+    }
+
+    @Test
+    void nameFromSubClaimWhenNoUsername() {
+        User user = User.create(new JsonObject().put("sub", "user-123"));
+        assertThat(VertxMcpPrincipal.name(user)).isEqualTo("user-123");
+    }
+
+    @Test
+    void nameIsNullForNullUserOrNoClaims() {
+        assertThat(VertxMcpPrincipal.name(null)).isNull();
+        assertThat(VertxMcpPrincipal.name(User.create(new 
JsonObject()))).isNull();
+    }
+
+    @Test
+    void rolesFromTopLevelRolesClaim() {
+        User user = User.create(new JsonObject().put("roles", new 
JsonArray().add("admin").add("user")));
+        assertThat(VertxMcpPrincipal.roles(user)).containsExactly("admin", 
"user");
+    }
+
+    @Test
+    void rolesFromKeycloakRealmAccess() {
+        User user = User.create(new JsonObject()
+                .put("realm_access", new JsonObject().put("roles", new 
JsonArray().add("kc-role"))));
+        assertThat(VertxMcpPrincipal.roles(user)).containsExactly("kc-role");
+    }
+
+    @Test
+    void rolesFromGroupsClaim() {
+        User user = User.create(new JsonObject().put("groups", new 
JsonArray().add("g1").add("g2")));
+        assertThat(VertxMcpPrincipal.roles(user)).containsExactly("g1", "g2");
+    }
+
+    @Test
+    void topLevelRolesTakePrecedenceOverGroups() {
+        User user = User.create(new JsonObject()
+                .put("roles", new JsonArray().add("a"))
+                .put("groups", new JsonArray().add("b")));
+        assertThat(VertxMcpPrincipal.roles(user)).containsExactly("a");
+    }
+
+    @Test
+    void rolesSkipBlankAndNonStringEntries() {

Review Comment:
   ⚠️ **Missing test case — potential `ClassCastException`:** 
`getJsonArray("roles")` on a Vert.x `JsonObject` throws `ClassCastException` 
when the value is a plain `String` rather than a `JsonArray`. Some IdPs (e.g. 
certain Keycloak configurations) do emit a single-string `"roles": "admin"`. 
The same applies to `realm_access.roles` and `groups`.
   
   Please add a test for the scalar case (should it return `null` or wrap the 
value?) and guard the production code accordingly, e.g.:
   
   ```java
   Object rolesRaw = principal.getValue("roles");
   JsonArray roles = rolesRaw instanceof JsonArray ja ? ja
           : rolesRaw instanceof String s ? new JsonArray().add(s)
           : null;
   ```



##########
components/camel-ai/camel-mcp-server/src/main/java/org/apache/camel/component/mcp/server/vertx/VertxMcpPrincipal.java:
##########
@@ -0,0 +1,105 @@
+/*
+ * 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.mcp.server.vertx;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import io.vertx.core.json.JsonArray;
+import io.vertx.core.json.JsonObject;
+import io.vertx.ext.auth.User;
+
+/**
+ * Derives a runtime-neutral view — a caller name and role names — from a 
Vert.x {@link User}, so the MCP bridge can
+ * stamp a stable caller identity ({@code CamelMcpSecurityPrincipalName} / 
{@code CamelMcpSecurityPrincipalRoles}) that
+ * a tool route or its {@link org.apache.camel.spi.AuthorizationPolicy} can 
authorize on without depending on Vert.x.
+ */
+final class VertxMcpPrincipal {
+
+    private VertxMcpPrincipal() {
+    }
+
+    /**
+     * The caller name: the Vert.x {@link User#subject() subject} when 
non-null and non-blank, else the {@code username}
+     * or {@code sub} claim of the principal when non-null and non-blank, else 
{@code null}.

Review Comment:
   💡 **Javadoc precision:** the phrase "when set" is slightly misleading — 
`isSet()` rejects both `null` and blank strings (line 98: `value != null && 
!value.isBlank()`). A subject that is `" "` would also be skipped, which the 
current wording doesn't convey.
   
   Consider:
   ```suggestion
        * The caller name: the Vert.x {@link User#subject() subject} when 
non-null and non-blank,
        * else the {@code username} or {@code sub} claim of the principal, else 
{@code null}.
   ```



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