Skip to content

Commit 2432603

Browse files
authored
fix(server): fix npe in non-auth mode (#2912)
1 parent eec3871 commit 2432603

6 files changed

Lines changed: 214 additions & 5 deletions

File tree

hugegraph-server/hugegraph-api/src/main/java/org/apache/hugegraph/api/auth/ManagerAPI.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public String createManager(@Context GraphManager manager,
7373
AuthManager authManager = manager.authManager();
7474
validUser(authManager, user);
7575

76-
String creator = HugeGraphAuthProxy.getContext().user().username();
76+
String creator = HugeGraphAuthProxy.username();
7777
switch (type) {
7878
case SPACE:
7979
validGraphSpace(manager, graphSpace);
@@ -124,7 +124,7 @@ public void delete(@Context GraphManager manager,
124124
AuthManager authManager = manager.authManager();
125125
validType(type);
126126
validUser(authManager, user);
127-
String actionUser = HugeGraphAuthProxy.getContext().user().username();
127+
String actionUser = HugeGraphAuthProxy.username();
128128

129129
switch (type) {
130130
case SPACE:
@@ -193,7 +193,7 @@ public String checkRole(@Context GraphManager manager,
193193

194194
validType(type);
195195
AuthManager authManager = manager.authManager();
196-
String user = HugeGraphAuthProxy.getContext().user().username();
196+
String user = HugeGraphAuthProxy.username();
197197

198198
boolean result;
199199
switch (type) {

hugegraph-server/hugegraph-api/src/main/java/org/apache/hugegraph/api/profile/GraphsAPI.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ public Object create(@Context GraphManager manager,
199199
}
200200
}
201201

202-
String creator = HugeGraphAuthProxy.getContext().user().username();
202+
String creator = HugeGraphAuthProxy.username();
203203

204204
if (StringUtils.isNotEmpty(clone)) {
205205
// Clone from existing graph

hugegraph-server/hugegraph-api/src/main/java/org/apache/hugegraph/api/space/GraphSpaceAPI.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ public String create(@Context GraphManager manager,
104104

105105
jsonGraphSpace.checkCreate(false);
106106

107-
String creator = HugeGraphAuthProxy.getContext().user().username();
107+
String creator = HugeGraphAuthProxy.username();
108108
GraphSpace exist = manager.graphSpace(jsonGraphSpace.name);
109109
E.checkArgument(exist == null, "The graph space '%s' has existed",
110110
jsonGraphSpace.name);

hugegraph-server/hugegraph-api/src/main/java/org/apache/hugegraph/auth/HugeGraphAuthProxy.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@ public static Context setAdmin() {
186186
public static Context getContext() {
187187
// Return task context first
188188
String taskContext = TaskManager.getContext();
189+
189190
User user = User.fromJson(taskContext);
190191
if (user != null) {
191192
return new Context(user);
@@ -953,6 +954,14 @@ public void updateTime(Date updateTime) {
953954
this.hugegraph.updateTime(updateTime);
954955
}
955956

957+
public static String username() {
958+
Context context = HugeGraphAuthProxy.getContext();
959+
if (context == null) {
960+
return "anonymous";
961+
}
962+
return context.user.username();
963+
}
964+
956965
private <V> Cache<Id, V> cache(String prefix, long capacity,
957966
long expiredTime) {
958967
String name = prefix + "-" + this.hugegraph.spaceGraphName();

hugegraph-server/hugegraph-test/src/main/java/org/apache/hugegraph/unit/UnitTestSuite.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import org.apache.hugegraph.core.RoleElectionStateMachineTest;
2121
import org.apache.hugegraph.unit.api.filter.PathFilterTest;
22+
import org.apache.hugegraph.unit.auth.HugeGraphAuthProxyTest;
2223
import org.apache.hugegraph.unit.cache.CacheManagerTest;
2324
import org.apache.hugegraph.unit.cache.CacheTest;
2425
import org.apache.hugegraph.unit.cache.CachedGraphTransactionTest;
@@ -117,6 +118,7 @@
117118
PageStateTest.class,
118119
SystemSchemaStoreTest.class,
119120
RoleElectionStateMachineTest.class,
121+
HugeGraphAuthProxyTest.class,
120122

121123
/* serializer */
122124
BytesBufferTest.class,
Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.hugegraph.unit.auth;
19+
20+
import java.lang.reflect.Method;
21+
22+
import org.apache.hugegraph.auth.HugeAuthenticator;
23+
import org.apache.hugegraph.auth.HugeGraphAuthProxy;
24+
import org.apache.hugegraph.auth.RolePermission;
25+
import org.apache.hugegraph.task.TaskManager;
26+
import org.apache.hugegraph.testutil.Assert;
27+
import org.apache.hugegraph.unit.BaseUnitTest;
28+
import org.junit.After;
29+
import org.junit.Test;
30+
31+
public class HugeGraphAuthProxyTest extends BaseUnitTest {
32+
33+
private static HugeGraphAuthProxy.Context setContext(
34+
HugeGraphAuthProxy.Context context) {
35+
try {
36+
Method method = HugeGraphAuthProxy.class.getDeclaredMethod(
37+
"setContext",
38+
HugeGraphAuthProxy.Context.class);
39+
method.setAccessible(true);
40+
return (HugeGraphAuthProxy.Context) method.invoke(null, context);
41+
} catch (Exception e) {
42+
throw new RuntimeException(e);
43+
}
44+
}
45+
46+
@After
47+
public void tearDown() {
48+
// Clean up contexts after each test
49+
HugeGraphAuthProxy.resetContext();
50+
TaskManager.resetContext();
51+
}
52+
53+
@Test
54+
public void testUsernameWithNullContext() {
55+
// Ensure no context is set
56+
HugeGraphAuthProxy.resetContext();
57+
TaskManager.resetContext();
58+
59+
// When context is null, username() should return "anonymous"
60+
String username = HugeGraphAuthProxy.username();
61+
Assert.assertEquals("anonymous", username);
62+
}
63+
64+
@Test
65+
public void testUsernameWithValidContext() {
66+
// Create a user with a specific username
67+
HugeAuthenticator.User user = new HugeAuthenticator.User(
68+
"test_user",
69+
RolePermission.admin()
70+
);
71+
72+
// Set context with this user
73+
HugeGraphAuthProxy.Context context = new HugeGraphAuthProxy.Context(user);
74+
setContext(context);
75+
76+
// username() should return the user's username
77+
String username = HugeGraphAuthProxy.username();
78+
Assert.assertEquals("test_user", username);
79+
}
80+
81+
@Test
82+
public void testUsernameWithAdminUser() {
83+
// Test with ADMIN user
84+
HugeAuthenticator.User adminUser = HugeAuthenticator.User.ADMIN;
85+
HugeGraphAuthProxy.Context context = new HugeGraphAuthProxy.Context(
86+
adminUser);
87+
setContext(context);
88+
89+
String username = HugeGraphAuthProxy.username();
90+
Assert.assertEquals("admin", username);
91+
}
92+
93+
@Test
94+
public void testGetContextReturnsNull() {
95+
// Ensure both TaskManager context and CONTEXTS are null
96+
HugeGraphAuthProxy.resetContext();
97+
TaskManager.resetContext();
98+
99+
HugeGraphAuthProxy.Context context = HugeGraphAuthProxy.getContext();
100+
Assert.assertNull(context);
101+
}
102+
103+
@Test
104+
public void testGetContextFromThreadLocal() {
105+
// Set context via setContext (which sets CONTEXTS ThreadLocal)
106+
HugeAuthenticator.User user = new HugeAuthenticator.User(
107+
"thread_local_user",
108+
RolePermission.admin()
109+
);
110+
HugeGraphAuthProxy.Context expectedContext = new HugeGraphAuthProxy.Context(
111+
user);
112+
setContext(expectedContext);
113+
114+
// Ensure TaskManager context is null
115+
TaskManager.resetContext();
116+
117+
// getContext() should return the context from CONTEXTS ThreadLocal
118+
HugeGraphAuthProxy.Context context = HugeGraphAuthProxy.getContext();
119+
Assert.assertNotNull(context);
120+
Assert.assertEquals("thread_local_user", context.user().username());
121+
}
122+
123+
@Test
124+
public void testGetContextFromTaskManager() {
125+
// Clear CONTEXTS ThreadLocal
126+
HugeGraphAuthProxy.resetContext();
127+
128+
// Create a user and set it in TaskManager context
129+
HugeAuthenticator.User user = new HugeAuthenticator.User(
130+
"task_user",
131+
RolePermission.admin()
132+
);
133+
String userJson = user.toJson();
134+
TaskManager.setContext(userJson);
135+
136+
// getContext() should return context from TaskManager
137+
HugeGraphAuthProxy.Context context = HugeGraphAuthProxy.getContext();
138+
Assert.assertNotNull(context);
139+
Assert.assertEquals("task_user", context.user().username());
140+
}
141+
142+
@Test
143+
public void testGetContextPrioritizesTaskManager() {
144+
// Set both TaskManager context and CONTEXTS ThreadLocal
145+
HugeAuthenticator.User taskUser = new HugeAuthenticator.User(
146+
"task_user",
147+
RolePermission.admin()
148+
);
149+
String taskUserJson = taskUser.toJson();
150+
TaskManager.setContext(taskUserJson);
151+
152+
HugeAuthenticator.User threadUser = new HugeAuthenticator.User(
153+
"thread_user",
154+
RolePermission.admin()
155+
);
156+
HugeGraphAuthProxy.Context threadContext = new HugeGraphAuthProxy.Context(
157+
threadUser);
158+
setContext(threadContext);
159+
160+
// getContext() should prioritize TaskManager context
161+
HugeGraphAuthProxy.Context context = HugeGraphAuthProxy.getContext();
162+
Assert.assertNotNull(context);
163+
Assert.assertEquals("task_user", context.user().username());
164+
}
165+
166+
@Test
167+
public void testGetContextWithNullTaskManagerJson() {
168+
// Clear CONTEXTS ThreadLocal
169+
HugeGraphAuthProxy.resetContext();
170+
171+
// Set null in TaskManager
172+
TaskManager.setContext(null);
173+
174+
// getContext() should return null
175+
HugeGraphAuthProxy.Context context = HugeGraphAuthProxy.getContext();
176+
Assert.assertNull(context);
177+
}
178+
179+
@Test
180+
public void testUsernameAfterResetContext() {
181+
// Set a context first
182+
HugeAuthenticator.User user = new HugeAuthenticator.User(
183+
"temp_user",
184+
RolePermission.admin()
185+
);
186+
HugeGraphAuthProxy.Context context = new HugeGraphAuthProxy.Context(user);
187+
setContext(context);
188+
189+
// Verify it's set
190+
Assert.assertEquals("temp_user", HugeGraphAuthProxy.username());
191+
192+
// Reset context
193+
HugeGraphAuthProxy.resetContext();
194+
195+
// username() should now return "anonymous"
196+
Assert.assertEquals("anonymous", HugeGraphAuthProxy.username());
197+
}
198+
}

0 commit comments

Comments
 (0)