Copilot commented on code in PR #3159:
URL: https://github.com/apache/hugegraph/pull/3159#discussion_r3883400804
##########
hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/StandardHugeGraph.java:
##########
@@ -519,6 +519,9 @@ public void clearBackend() {
LockUtil.lock(this.spaceGraphName(), LockUtil.GRAPH_LOCK);
try {
+ if (this.isHstore()) {
+ ((CachedSchemaTransactionV2) this.schemaTransaction()).clear();
Review Comment:
The unconditional cast to CachedSchemaTransactionV2 can throw a
ClassCastException if schemaTransaction() isn’t a CachedSchemaTransactionV2
even when isHstore() is true. Prefer guarding with an instanceof check (and
either calling clear() on the base type or throwing a clearer exception) to
make backend clearing robust across schema-transaction implementations.
##########
hugegraph-server/hugegraph-api/src/main/java/org/apache/hugegraph/api/space/GraphSpaceAPI.java:
##########
@@ -203,20 +205,25 @@ public String checkDefaultRole(@Context GraphManager
manager,
defaultRole.equals(HugeDefaultRole.SPACE)) {
throw new ForbiddenException("Forbidden to check role " + role);
}
- boolean hasGraph = defaultRole.equals(HugeDefaultRole.OBSERVER);
- E.checkArgument(!hasGraph || StringUtils.isNotEmpty(graph),
- "Must set a graph for observer");
+ boolean hasGraph = defaultRole.equals(HugeDefaultRole.OBSERVER) &&
+ StringUtils.isNotEmpty(graph);
if (hasGraph) {
validGraph(manager, name, graph);
}
boolean result;
if (hasGraph) {
- result = authManager.isDefaultRole(name, graph, user,
- defaultRole);
+ result = authManager.isDefaultRole(name, graph, user, defaultRole);
} else {
- result = authManager.isDefaultRole(name, user,
- defaultRole);
+ result = authManager.isDefaultRole(name, user, defaultRole);
+ if (!result && defaultRole.equals(HugeDefaultRole.OBSERVER)) {
+ for (String currentGraph : manager.graphs(name)) {
+ if (authManager.isDefaultRole(name, currentGraph, user,
defaultRole)) {
+ result = true;
+ break;
+ }
+ }
+ }
Review Comment:
The OBSERVER legacy fallback does an O(#graphs) scan with a per-graph
authManager call. For graphSpaces with many graphs this can noticeably increase
latency and load. Consider adding an AuthManager API that checks ‘any legacy
observer role exists’ in one call, or fetching the legacy roles once and
evaluating locally, to avoid N calls per request.
##########
hugegraph-server/hugegraph-api/src/main/java/org/apache/hugegraph/api/auth/ManagerAPI.java:
##########
@@ -287,20 +287,24 @@ public String checkDefaultRole(@Context GraphManager
manager,
defaultRole = null; // unreachable, satisfies compiler
}
validGraphSpace(manager, graphSpace);
- boolean hasGraph = defaultRole.equals(HugeDefaultRole.OBSERVER);
- E.checkArgument(!hasGraph || StringUtils.isNotEmpty(graph),
- "Must set a graph for observer");
+ boolean hasGraph = defaultRole.equals(HugeDefaultRole.OBSERVER) &&
StringUtils.isNotEmpty(graph);
if (hasGraph) {
validGraph(manager, graphSpace, graph);
}
boolean result;
if (hasGraph) {
- result = authManager.isDefaultRole(graphSpace, graph, user,
- defaultRole);
+ result = authManager.isDefaultRole(graphSpace, graph, user,
defaultRole);
} else {
- result = authManager.isDefaultRole(graphSpace, user,
- defaultRole);
+ result = authManager.isDefaultRole(graphSpace, user, defaultRole);
+ if (!result && defaultRole.equals(HugeDefaultRole.OBSERVER)) {
+ for (String currentGraph : manager.graphs(graphSpace)) {
+ if (authManager.isDefaultRole(graphSpace, currentGraph,
user, defaultRole)) {
+ result = true;
+ break;
+ }
+ }
+ }
Review Comment:
Same as GraphSpaceAPI: this introduces an O(#graphs) per-request fallback
with repeated authManager lookups. If this endpoint is called frequently,
consider consolidating into a single AuthManager method (or a bulk query) to
avoid per-graph checks.
##########
hugegraph-server/hugegraph-api/src/main/java/org/apache/hugegraph/core/GraphManager.java:
##########
@@ -2434,19 +2428,14 @@ public static ConsumerWrapper wrap(Consumer consumer) {
@Override
public void accept(T t) {
- boolean grpcThread = false;
try {
- grpcThread = Thread.currentThread().getName().contains("grpc");
- if (grpcThread) {
- HugeGraphAuthProxy.setAdmin();
+ if (Thread.currentThread().getName().contains("grpc")) {
+ HugeGraphAuthProxy.runAsAdmin(() ->
this.consumer.accept(t));
+ } else {
+ this.consumer.accept(t);
}
Review Comment:
Elevating to admin based on a thread-name substring is brittle and can be
unintentionally triggered (or break if thread naming changes). A safer approach
is to pass an explicit ‘internal call’ marker/context into this path (or
execute internal listeners on a dedicated executor with a controlled context)
rather than inferring privileges from thread names.
##########
hugegraph-server/hugegraph-test/src/main/java/org/apache/hugegraph/unit/api/space/SchemaTemplateAPITest.java:
##########
@@ -0,0 +1,81 @@
+/*
+ * 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.hugegraph.unit.api.space;
+
+import java.util.function.Supplier;
+
+import org.apache.hugegraph.api.space.SchemaTemplateAPI;
+import org.apache.hugegraph.auth.AuthManager;
+import org.apache.hugegraph.testutil.Assert;
+import org.apache.hugegraph.testutil.Whitebox;
+import org.junit.Test;
+import org.mockito.Mockito;
+
+public class SchemaTemplateAPITest {
+
+ private static final String GRAPHSPACE = "space";
+ private static final String CREATOR = "creator";
+
+ @Test
+ public void testCreatorCanManageTemplate() {
+ Supplier<AuthManager> authManager =
+ Mockito.mock(Supplier.class);
+
+ Assert.assertTrue(canManage(authManager, CREATOR));
+ Mockito.verifyZeroInteractions(authManager);
Review Comment:
Mockito.verifyZeroInteractions(...) is deprecated in newer Mockito versions;
prefer Mockito.verifyNoInteractions(...) for forward compatibility.
##########
hugegraph-server/hugegraph-api/src/main/java/org/apache/hugegraph/auth/HugeAuthenticator.java:
##########
@@ -359,8 +363,15 @@ public static boolean match(Object role, RolePermission
grant,
}
}
- RolePermission rolePerm = RolePermission.fromJson(role);
- return rolePerm.contains(grant);
+ RolePermission grantedRole = RolePermission.fromJson(grant);
+ RolePerm rolePerm = RolePerm.fromJson(role);
+ if (resourceObject != null &&
+ !RolePermission.isAdmin(grantedRole) &&
+ grantedRole.roles().containsKey(resourceObject.graphSpace()) &&
+ rolePerm.matchSpace(resourceObject.graphSpace(), "space")) {
+ return true;
+ }
+ return RolePermission.fromJson(role).contains(grantedRole);
Review Comment:
This code parses `role` twice (RolePerm.fromJson(role) and
RolePermission.fromJson(role)), which is both harder to follow and potentially
expensive on an auth hot-path. Consider parsing once and reusing the parsed
representation for the final contains() check (or adding an API on
RolePerm/RolePermission to avoid re-parsing).
--
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]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]