Skip to content

fix(server): remove graph path in auth api path#2899

Merged
imbajin merged 7 commits intoapache:masterfrom
hugegraph:fix/auth
Nov 4, 2025
Merged

fix(server): remove graph path in auth api path#2899
imbajin merged 7 commits intoapache:masterfrom
hugegraph:fix/auth

Conversation

@Tsukilc
Copy link
Collaborator

@Tsukilc Tsukilc commented Nov 4, 2025

as tilte

@dosubot dosubot bot added size:XL This PR changes 500-999 lines, ignoring generated files. api Changes of API labels Nov 4, 2025
@imbajin imbajin requested a review from Copilot November 4, 2025 10:52
validType(type);
AuthManager authManager = manager.authManager();
validUser(authManager, user);

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ 重要问题: ManagerAPI 缺少适当的错误处理

createManager 方法中,多个验证方法(如 validGraphSpace, validPermission)被调用,但没有看到统一的异常处理策略。

建议:

Suggested change
// 考虑添加更详细的错误消息和异常处理
try {
validGraphSpace(manager, graphSpace);
validPermission(
hasAdminOrSpaceManagerPerm(manager, graphSpace, creator),
creator, "manager.create");
} catch (IllegalArgumentException e) {
LOG.error("Failed to validate graph space: {}", graphSpace, e);
throw new BadRequestException(
String.format("Invalid graph space '%s': %s", graphSpace, e.getMessage()));
}

@Status(Status.CREATED)
@Consumes(APPLICATION_JSON)
@Produces(APPLICATION_JSON_WITH_CHARSET)
@RolesAllowed({"admin"})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ 重要问题: GroupAPI 新增 @RolesAllowed 注解但缺少一致性

GroupAPI 中为所有方法添加了 @RolesAllowed({"admin"}) 注解,但其他API(如 AccessAPI, BelongAPI)却没有添加相应的权限控制注解。

问题:

  1. 为什么只有 GroupAPI 需要admin权限而其他认证API不需要?
  2. 这种不一致性是否符合设计意图?

建议:

  • 统一所有认证相关API的权限控制策略
  • 在代码注释中说明为什么 GroupAPI 需要特殊的权限要求

HugeAccess access = jsonAccess.build();
access.id(manager.authManager().createAccess(access));
return manager.serializer(g).writeAuthElement(access);
return manager.serializer().writeAuthElement(access);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ 重要问题: manager.serializer() 调用未验证返回值

在所有API中,manager.serializer() 的调用从 manager.serializer(g) 改为 manager.serializer(),但没有看到对应的空值检查或错误处理。

潜在风险:
如果 manager.serializer() 返回 null,会导致 NPE

建议:

Suggested change
return manager.serializer().writeAuthElement(access);
JsonSerializer serializer = manager.serializer();
E.checkNotNull(serializer, "Serializer must not be null");
return serializer.writeAuthElement(access);

@@ -64,15 +63,13 @@ public class TargetAPI extends API {
@Produces(APPLICATION_JSON_WITH_CHARSET)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ 重要问题: TargetAPI 缺少权限控制

TargetAPI 移除了 graph 路径参数,但与 GroupAPI 不同,没有添加任何 @RolesAllowed 注解进行权限控制。

安全隐患:
Target 资源的创建、更新、删除操作没有明确的权限限制,可能导致未授权访问。

建议:
根据业务需求添加适当的权限控制注解,例如:

Suggested change
@Produces(APPLICATION_JSON_WITH_CHARSET)
@RolesAllowed({"admin", "space-manager"})
public String create(@Context GraphManager manager,


String creator = HugeGraphAuthProxy.getContext().user().username();
switch (type) {
case SPACE:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ 重要问题: ManagerAPI 使用 switch-case 但缺少 break

createManagerdelete 方法中,switch-case 语句虽然每个 case 都有明确的逻辑,但代码可读性可以提升。

建议:
虽然当前代码功能正确,但考虑使用策略模式或提取方法来改善代码结构:

Suggested change
case SPACE:
private void handleSpaceManagerCreation(GraphManager manager, String graphSpace,
String user, String creator) {
validGraphSpace(manager, graphSpace);
validPermission(
hasAdminOrSpaceManagerPerm(manager, graphSpace, creator),
creator, "manager.create");
AuthManager authManager = manager.authManager();
if (authManager.isSpaceMember(graphSpace, user)) {
authManager.deleteSpaceMember(graphSpace, user);
}
authManager.createSpaceManager(graphSpace, user);
}

然后在 switch 中调用这些方法,会使代码更易维护。

case ADMIN:
adminManagers = authManager.listAdminManager();
break;
default:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 次要问题: 日志消息不一致

ManagerAPI.list 方法中,返回值使用了 writeList("admins", adminManagers),但列表名称为 "admins" 与实际的 managers 语义不符。

建议:

Suggested change
default:
return manager.serializer().writeList("managers", adminManagers);

保持命名的一致性和准确性。

@Timed
@StatusFilter.Status(StatusFilter.Status.CREATED)
@Consumes(APPLICATION_JSON)
@Produces(APPLICATION_JSON_WITH_CHARSET)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 次要问题: 缺少输入参数验证

ManagerAPI.createManager 方法中,应该先验证 graphSpaceuser 参数的有效性,然后再进行业务逻辑处理。

建议:

Suggested change
@Produces(APPLICATION_JSON_WITH_CHARSET)
public String createManager(@Context GraphManager manager,
@PathParam("graphspace") String graphSpace,
JsonManager jsonManager) {
LOG.debug("Create manager: {}", jsonManager);
E.checkArgumentNotNull(graphSpace, "Graph space cannot be null");
E.checkArgument(!graphSpace.trim().isEmpty(),
"Graph space cannot be empty");
String user = jsonManager.user;
HugePermission type = jsonManager.type;
// ... rest of the method

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR refactors authentication-related APIs to remove graph-level path parameters and dependencies, shifting auth management from graph-scoped to graphspace-scoped. The changes also remove unnecessary permission checks for group operations and update serializer method calls to use a parameterless version.

Key changes:

  • Auth API endpoints (User, Project, Target, Group, Access, Belong, Manager, Login) now operate at the graphspace level instead of requiring a graph parameter
  • Permission verification removed from group CRUD operations in HugeGraphAuthProxy
  • Serializer calls changed from manager.serializer(g) to manager.serializer() across multiple API classes
  • Test paths updated to match new API structure

Reviewed Changes

Copilot reviewed 25 out of 25 changed files in this pull request and generated 12 comments.

Show a summary per file
File Description
UserApiTest.java Updated test path to remove graph segment from auth users endpoint
ProjectApiTest.java Updated test path to remove graph segment from auth projects endpoint
ManagerApiTest.java Updated test path to remove graph segment from auth users endpoint
LoginApiTest.java Updated test paths to use simplified auth endpoints without graph parameters
HugeGraphAuthProxy.java Removed permission verification calls for group operations
VerticesAPI.java Changed serializer calls to use parameterless method
PersonalRankAPI.java Changed serializer calls to use parameterless method
NeighborRankAPI.java Changed serializer calls to use parameterless method
EdgesAPI.java Changed serializer calls to use parameterless method
CountAPI.java Changed serializer calls to use parameterless method
SchemaAPI.java Changed serializer calls to use parameterless method
VertexLabelAPI.java Changed serializer calls to use parameterless method
PropertyKeyAPI.java Changed serializer calls to use parameterless method
IndexLabelAPI.java Changed serializer calls to use parameterless method
EdgeLabelAPI.java Changed serializer calls to use parameterless method
VertexAPI.java Changed serializer calls to use parameterless method
EdgeAPI.java Changed serializer calls to use parameterless method
UserAPI.java Removed graph path parameter and HugeGraph dependency from all endpoints
TargetAPI.java Removed graph path parameter and HugeGraph dependency; added toString method
ProjectAPI.java Removed graph path parameter and HugeGraph dependency from all endpoints
ManagerAPI.java New file implementing graphspace-scoped manager API
LoginAPI.java Simplified to remove graph path parameters; now uses global /auth path
GroupAPI.java Removed graph path parameter and added @RolesAllowed annotations
BelongAPI.java Removed graph path parameter and HugeGraph dependency from all endpoints
AccessAPI.java Removed graph path parameter; fixed log message typo

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +185 to +186


Copy link

Copilot AI Nov 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Extra blank line should be removed to maintain consistent code formatting.

Suggested change

Copilot uses AI. Check for mistakes.
@PathParam("graph") String graph,
JsonAccess jsonAccess) {
LOG.debug("Graph [{}] create access: {}", graph, jsonAccess);
LOG.debug("GraphSpace [{}] create access: {}", graphSpace, jsonAccess);
Copy link

Copilot AI Nov 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Default toString(): JsonAccess inherits toString() from Object, and so is not suitable for printing.

Copilot uses AI. Check for mistakes.
@PathParam("id") String id,
JsonAccess jsonAccess) {
LOG.debug("Graph [{}] update access: {}", graph, jsonAccess);
LOG.debug("GraphSpace [{}] update access: {}", graphSpace, jsonAccess);
Copy link

Copilot AI Nov 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Default toString(): JsonAccess inherits toString() from Object, and so is not suitable for printing.

Copilot uses AI. Check for mistakes.
@PathParam("graph") String graph,
JsonBelong jsonBelong) {
LOG.debug("Graph [{}] create belong: {}", graph, jsonBelong);
LOG.debug("GraphSpace [{}] create belong: {}", graphSpace, jsonBelong);
Copy link

Copilot AI Nov 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Default toString(): JsonBelong inherits toString() from Object, and so is not suitable for printing.

Copilot uses AI. Check for mistakes.
@PathParam("id") String id,
JsonBelong jsonBelong) {
LOG.debug("Graph [{}] update belong: {}", graph, jsonBelong);
LOG.debug("GraphSpace [{}] update belong: {}", graphSpace, jsonBelong);
Copy link

Copilot AI Nov 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Default toString(): JsonBelong inherits toString() from Object, and so is not suitable for printing.

Copilot uses AI. Check for mistakes.
@PathParam("graph") String graph,
JsonLogin jsonLogin) {
LOG.debug("Graph [{}] user login: {}", graph, jsonLogin);
LOG.debug("user login: {}", jsonLogin);
Copy link

Copilot AI Nov 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Default toString(): JsonLogin inherits toString() from Object, and so is not suitable for printing.

Copilot uses AI. Check for mistakes.
public String createManager(@Context GraphManager manager,
@PathParam("graphspace") String graphSpace,
JsonManager jsonManager) {
LOG.debug("Create manager: {}", jsonManager);
Copy link

Copilot AI Nov 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Default toString(): JsonManager inherits toString() from Object, and so is not suitable for printing.

Copilot uses AI. Check for mistakes.
@PathParam("graph") String graph,
JsonProject jsonProject) {
LOG.debug("Graph [{}] create project: {}", graph, jsonProject);
LOG.debug("GraphSpace [{}] create project: {}", graphSpace, jsonProject);
Copy link

Copilot AI Nov 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Default toString(): JsonProject inherits toString() from Object, and so is not suitable for printing.

Copilot uses AI. Check for mistakes.
@PathParam("graph") String graph,
JsonUser jsonUser) {
LOG.debug("Graph [{}] create user: {}", graph, jsonUser);
LOG.debug("GraphSpace [{}] create user: {}", graphSpace, jsonUser);
Copy link

Copilot AI Nov 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Default toString(): JsonUser inherits toString() from Object, and so is not suitable for printing.

Copilot uses AI. Check for mistakes.
@PathParam("id") String id,
JsonUser jsonUser) {
LOG.debug("Graph [{}] update user: {}", graph, jsonUser);
LOG.debug("GraphSpace [{}] update user: {}", graphSpace, jsonUser);
Copy link

Copilot AI Nov 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Default toString(): JsonUser inherits toString() from Object, and so is not suitable for printing.

Copilot uses AI. Check for mistakes.
Copy link
Member

@imbajin imbajin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Merge it first, enhance it in another PR (FIXME/TODOs)

@dosubot dosubot bot added the lgtm This PR has been approved by a maintainer label Nov 4, 2025
@imbajin imbajin merged commit b7998c1 into apache:master Nov 4, 2025
19 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

api Changes of API lgtm This PR has been approved by a maintainer size:XL This PR changes 500-999 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants