Skip to content

Commit

Permalink
fix(server): better code
Browse files Browse the repository at this point in the history
  • Loading branch information
SunnyBoy-WYH committed Mar 13, 2024
1 parent 6a50b99 commit ed2b24a
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@
import java.io.IOException;
import java.net.URI;

import org.apache.hugegraph.auth.HugeGraphAuthProxy;
import org.apache.hugegraph.config.HugeConfig;
import org.apache.hugegraph.config.ServerOptions;
import org.apache.hugegraph.core.GraphManager;
import org.apache.hugegraph.metrics.MetricsUtil;
import org.apache.hugegraph.util.Log;
import org.slf4j.Logger;
Expand Down Expand Up @@ -56,6 +56,9 @@ public class AccessLogFilter implements ContainerResponseFilter {
@Context
private jakarta.inject.Provider<HugeConfig> configProvider;

@Context
private jakarta.inject.Provider<GraphManager> managerProvider;

public static boolean needRecordLog(ContainerRequestContext context) {
// TODO: add test for 'path' result ('/gremlin' or 'gremlin')
String path = context.getUriInfo().getPath();
Expand Down Expand Up @@ -117,7 +120,8 @@ public void filter(ContainerRequestContext requestContext,
}

// Unset the context in "HugeAuthenticator", need distinguish Graph/Auth server lifecycle
HugeGraphAuthProxy.resetContext();
GraphManager graphManager = managerProvider.get();
graphManager.unAuthenticate((AuthenticationFilter.Authorizer) requestContext.getSecurityContext());
}

private boolean statusOk(int status) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

import org.apache.commons.lang.NotImplementedException;
import org.apache.hugegraph.HugeGraph;
import org.apache.hugegraph.api.filter.AuthenticationFilter;
import org.apache.hugegraph.backend.id.IdGenerator;
import org.apache.hugegraph.config.HugeConfig;
import org.apache.hugegraph.config.ServerOptions;
Expand Down Expand Up @@ -80,6 +81,11 @@ public UserWithRole authenticate(final String username,
return new UserWithRole(IdGenerator.of(username), username, role);
}

@Override
public void unAuthenticate(AuthenticationFilter.Authorizer authorizer) {
throw new NotImplementedException("unAuthenticate is unsupported by ConfigAuthenticator");
}

@Override
public AuthManager authManager() {
throw new NotImplementedException("AuthManager is unsupported by ConfigAuthenticator");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

import org.apache.hugegraph.HugeException;
import org.apache.hugegraph.HugeGraph;
import org.apache.hugegraph.api.filter.AuthenticationFilter;
import org.apache.hugegraph.auth.HugeGraphAuthProxy.Context;
import org.apache.hugegraph.auth.SchemaDefine.AuthElement;
import org.apache.hugegraph.backend.id.Id;
Expand Down Expand Up @@ -64,6 +65,8 @@ public interface HugeAuthenticator extends Authenticator {

UserWithRole authenticate(String username, String password, String token);

void unAuthenticate(AuthenticationFilter.Authorizer authorizer);

AuthManager authManager();

HugeGraph graph();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1818,7 +1818,7 @@ protected static Context setContext(Context context) {
return old;
}

public static void resetContext() {
protected static void resetContext() {
CONTEXTS.remove();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

import org.apache.commons.lang.StringUtils;
import org.apache.hugegraph.HugeGraph;
import org.apache.hugegraph.api.filter.AuthenticationFilter;
import org.apache.hugegraph.config.CoreOptions;
import org.apache.hugegraph.config.HugeConfig;
import org.apache.hugegraph.config.ServerOptions;
Expand Down Expand Up @@ -192,6 +193,11 @@ public UserWithRole authenticate(String username, String password,
userWithRole.username(), role);
}

@Override
public void unAuthenticate(AuthenticationFilter.Authorizer authorizer) {
HugeGraphAuthProxy.resetContext();
}

@Override
public AuthManager authManager() {
return this.graph().authManager();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.apache.commons.lang3.StringUtils;
import org.apache.hugegraph.HugeFactory;
import org.apache.hugegraph.HugeGraph;
import org.apache.hugegraph.api.filter.AuthenticationFilter;
import org.apache.hugegraph.auth.AuthManager;
import org.apache.hugegraph.auth.HugeAuthenticator;
import org.apache.hugegraph.auth.HugeFactoryAuthProxy;
Expand Down Expand Up @@ -263,6 +264,10 @@ public HugeAuthenticator.User authenticate(Map<String, String> credentials)
return this.authenticator().authenticate(credentials);
}

public void unAuthenticate(AuthenticationFilter.Authorizer authorizer) {
this.authenticator().unAuthenticate(authorizer);
}

public AuthManager authManager() {
return this.authenticator().authManager();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,36 @@ public void testArthasStart() {

@Test
public void testArthasApi() {
String body = "{\n" +
// command exec
String execBody = "{\n" +
" \"action\": \"exec\",\n" +
" \"command\": \"version\"\n" +
"}";
RestClient arthasApiClient = new RestClient(ARTHAS_API_BASE_URL, false);
Response r = arthasApiClient.post(ARTHAS_API_PATH, body);
String result = assertResponseStatus(200, r);
Response execResponse = arthasApiClient.post(ARTHAS_API_PATH, execBody);
String result = assertResponseStatus(200, execResponse);
assertJsonContains(result, "state");
assertJsonContains(result, "body");

// command session
String sessionBody = "{\n" +
" \"action\":\"init_session\"\n" +
"}";
Response sessionResponse = arthasApiClient.post(ARTHAS_API_PATH, sessionBody);
String sessionResult = assertResponseStatus(200, sessionResponse);
assertJsonContains(sessionResult, "sessionId");
assertJsonContains(sessionResult, "consumerId");
assertJsonContains(sessionResult,"state");


// join session: using invalid sessionId
String joinSessionBody = "{\n" +
" \"action\":\"join_session\",\n" +
" \"sessionId\" : \"xxx\"\n" +
"}";
Response joinSessionResponse = arthasApiClient.post(ARTHAS_API_PATH, joinSessionBody);
String joinSessionResult = assertResponseStatus(200, joinSessionResponse);
assertJsonContains(joinSessionResult,"message");
assertJsonContains(joinSessionResult,"state");
}
}

0 comments on commit ed2b24a

Please sign in to comment.