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 18, 2024
1 parent ed250f2 commit 2e3325c
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public void filter(ContainerRequestContext requestContext,
GraphManager manager = managerProvider.get();
// TODO transfer Authorizer if we need after.
if (manager.requireAuthentication()) {
manager.unAuthenticate(null);
manager.unauthorize(requestContext.getSecurityContext());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,8 @@ protected User authenticate(ContainerRequestContext context) {

// Check whiteIp
if (enabledWhiteIpCheck == null) {
enabledWhiteIpCheck = Objects.equals(this.configProvider.get().get(WHITE_IP_STATUS), STRING_ENABLE);
String whiteIpStatus = this.configProvider.get().get(WHITE_IP_STATUS);
enabledWhiteIpCheck = Objects.equals(whiteIpStatus, STRING_ENABLE);
}

if (enabledWhiteIpCheck && request != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,14 @@

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;
import org.apache.hugegraph.util.E;
import org.apache.tinkerpop.gremlin.groovy.jsr223.dsl.credential.CredentialGraphTokens;

import jakarta.ws.rs.core.SecurityContext;

public class ConfigAuthenticator implements HugeAuthenticator {

public static final String KEY_USERNAME = CredentialGraphTokens.PROPERTY_USERNAME;
Expand Down Expand Up @@ -82,8 +83,7 @@ public UserWithRole authenticate(final String username,
}

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

Check warning on line 87 in hugegraph-server/hugegraph-api/src/main/java/org/apache/hugegraph/auth/ConfigAuthenticator.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-server/hugegraph-api/src/main/java/org/apache/hugegraph/auth/ConfigAuthenticator.java#L87

Added line #L87 was not covered by tests

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

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 All @@ -40,6 +39,8 @@
import org.apache.tinkerpop.gremlin.server.auth.Authenticator;
import org.apache.tinkerpop.shaded.jackson.annotation.JsonProperty;

import jakarta.ws.rs.core.SecurityContext;

public interface HugeAuthenticator extends Authenticator {

String KEY_USERNAME = CredentialGraphTokens.PROPERTY_USERNAME;
Expand All @@ -65,7 +66,7 @@ public interface HugeAuthenticator extends Authenticator {

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

void unAuthenticate(AuthenticationFilter.Authorizer authorizer);
void unauthorize(SecurityContext context);

AuthManager authManager();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
import org.apache.tinkerpop.gremlin.server.auth.AuthenticationException;
import org.apache.tinkerpop.gremlin.structure.util.GraphFactory;

import jakarta.ws.rs.core.SecurityContext;

public class StandardAuthenticator implements HugeAuthenticator {

private static final String INITING_STORE = "initing_store";
Expand Down Expand Up @@ -194,7 +196,7 @@ public UserWithRole authenticate(String username, String password,
}

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
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 @@ -77,6 +76,8 @@

import com.alipay.sofa.rpc.config.ServerConfig;

import jakarta.ws.rs.core.SecurityContext;

public final class GraphManager {

private static final Logger LOG = Log.logger(GraphManager.class);
Expand Down Expand Up @@ -264,8 +265,8 @@ public HugeAuthenticator.User authenticate(Map<String, String> credentials)
return this.authenticator().authenticate(credentials);
}

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

public AuthManager authManager() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ public void testArthasStart() {
public void testArthasApi() {
// command exec
String execBody = "{\n" +
" \"action\": \"exec\",\n" +
" \"command\": \"version\"\n" +
"}";
" \"action\": \"exec\",\n" +
" \"command\": \"version\"\n" +
"}";
RestClient arthasApiClient = new RestClient(ARTHAS_API_BASE_URL, false);
Response execResponse = arthasApiClient.post(ARTHAS_API_PATH, execBody);
String result = assertResponseStatus(200, execResponse);
Expand All @@ -57,8 +57,7 @@ public void testArthasApi() {
String sessionResult = assertResponseStatus(200, sessionResponse);
assertJsonContains(sessionResult, "sessionId");
assertJsonContains(sessionResult, "consumerId");
assertJsonContains(sessionResult,"state");

assertJsonContains(sessionResult, "state");

// join session: using invalid sessionId
String joinSessionBody = "{\n" +
Expand All @@ -67,7 +66,7 @@ public void testArthasApi() {
"}";
Response joinSessionResponse = arthasApiClient.post(ARTHAS_API_PATH, joinSessionBody);
String joinSessionResult = assertResponseStatus(200, joinSessionResponse);
assertJsonContains(joinSessionResult,"message");
assertJsonContains(joinSessionResult,"state");
assertJsonContains(joinSessionResult, "message");
assertJsonContains(joinSessionResult, "state");
}
}

0 comments on commit 2e3325c

Please sign in to comment.