Skip to content

Commit 671520f

Browse files
committed
Merge branch 'hubble2.0-auth-simplified' into hubble2.0
# Conflicts: # hugegraph-hubble/hubble-dist/pom.xml # hugegraph-hubble/hubble-fe/package-lock.json # hugegraph-hubble/hubble-fe/package.json # hugegraph-hubble/hubble-fe/yarn.lock
2 parents 0b543e8 + f81b23f commit 671520f

File tree

13 files changed

+124
-33
lines changed

13 files changed

+124
-33
lines changed

hugegraph-client/src/main/java/org/apache/hugegraph/driver/HugeClient.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,11 @@ public static HugeClientBuilder builder(String url, String graph) {
126126
return new HugeClientBuilder(url, HugeClientBuilder.DEFAULT_GRAPHSPACE, graph);
127127
}
128128

129+
public static HugeClientBuilder builder(String url, String graphSpace, String graph,
130+
boolean skipRequiredChecks) {
131+
return new HugeClientBuilder(url, graphSpace, graph, skipRequiredChecks);
132+
}
133+
129134
public HugeClient assignGraph(String graphSpace, String graph) {
130135
this.graphSpaceName = graphSpace;
131136
this.graphName = graph;

hugegraph-client/src/main/java/org/apache/hugegraph/driver/HugeClientBuilder.java

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,24 @@ public class HugeClientBuilder {
5050
/** Set them null by default to keep compatibility with 'timeout' */
5151
private Integer connectTimeout;
5252
private Integer readTimeout;
53+
private final boolean skipRequiredChecks;
5354

5455
public HugeClientBuilder(String url, String graphSpace, String graph) {
55-
E.checkArgument(url != null && !url.isEmpty(),
56-
"Expect a string value as the url parameter argument, but got: %s", url);
57-
E.checkArgument(graph != null && !graph.isEmpty(),
58-
"Expect a string value as the graph name parameter argument, but got: %s",
59-
graph);
56+
this(url, graphSpace, graph, false);
57+
}
58+
59+
public HugeClientBuilder(String url, String graphSpace, String graph,
60+
boolean skipRequiredChecks) {
61+
this.skipRequiredChecks = skipRequiredChecks;
62+
63+
if (!skipRequiredChecks) {
64+
E.checkArgument(url != null && !url.isEmpty(),
65+
"Expect a string value as the url parameter argument, but got: %s", url);
66+
E.checkArgument(graph != null && !graph.isEmpty(),
67+
"Expect a string value as the graph name parameter argument, but got: %s",
68+
graph);
69+
}
70+
6071
this.url = url;
6172
this.graphSpace = graphSpace;
6273
this.graph = graph;
@@ -76,8 +87,10 @@ public HugeClientBuilder(String url, String graphSpace, String graph) {
7687
}
7788

7889
public HugeClient build() {
79-
E.checkArgument(this.url != null, "The url parameter can't be null");
80-
E.checkArgument(this.graph != null, "The graph parameter can't be null");
90+
if (!this.skipRequiredChecks) {
91+
E.checkArgument(this.url != null, "The url parameter can't be null");
92+
E.checkArgument(this.graph != null, "The graph parameter can't be null");
93+
}
8194
return new HugeClient(this);
8295
}
8396

hugegraph-client/src/test/java/org/apache/hugegraph/api/BaseApiTest.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -171,8 +171,4 @@ protected static void waitUntilTaskCompleted(long taskId, long timeout) {
171171
protected RestClient client() {
172172
return client;
173173
}
174-
175-
protected RestClient client() {
176-
return client;
177-
}
178174
}

hugegraph-hubble/hubble-be/pom.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,10 +328,13 @@
328328
<artifactId>protobuf-java</artifactId>
329329
<version>3.17.3</version>
330330
</dependency>
331+
<!-- Use 'core' classifier to avoid embedded protobuf-java 2.5.0 conflict
332+
with hg-pd-grpc which requires protobuf-java 3.17.x -->
331333
<dependency>
332334
<groupId>org.apache.hive</groupId>
333335
<artifactId>hive-exec</artifactId>
334336
<version>3.1.3</version>
337+
<classifier>core</classifier>
335338
<scope>compile</scope>
336339
<exclusions>
337340
<exclusion>
@@ -342,6 +345,13 @@
342345
<groupId>com.google.protobuf</groupId>
343346
<artifactId>protobuf-java</artifactId>
344347
</exclusion>
348+
<!-- Exclude log4j-slf4j-impl to avoid conflict with
349+
Spring Boot's log4j-to-slf4j from starter-logging.
350+
Both cannot coexist on the classpath. -->
351+
<exclusion>
352+
<groupId>org.apache.logging.log4j</groupId>
353+
<artifactId>log4j-slf4j-impl</artifactId>
354+
</exclusion>
345355
</exclusions>
346356
</dependency>
347357
<!-- https://mvnrepository.com/artifact/com.squareup.okhttp3/okhttp -->

hugegraph-hubble/hubble-be/src/main/java/org/apache/hugegraph/config/ProxyServletConfiguration.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
import org.mitre.dsmiley.httpproxy.ProxyServlet;
2424
import org.springframework.beans.factory.annotation.Autowired;
25+
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
2526
import org.springframework.boot.web.servlet.ServletRegistrationBean;
2627
import org.springframework.context.annotation.Bean;
2728
import org.springframework.context.annotation.Configuration;
@@ -31,11 +32,25 @@ public class ProxyServletConfiguration{
3132
@Autowired
3233
private HugeConfig config;
3334

35+
/**
36+
* Only register the proxy servlet when proxy.servlet_url is configured.
37+
* When not configured, this bean won't be created, preventing the proxy
38+
* servlet from intercepting all requests on root path.
39+
*/
3440
@Bean
41+
@ConditionalOnProperty(name = "proxy.servlet_url", matchIfMissing = false)
3542
public ServletRegistrationBean servletRegistrationBean(){
43+
String servletUrl = config.get(HubbleOptions.PROXY_SERVLET_URL);
44+
String targetUrl = config.get(HubbleOptions.PROXY_TARGET_URL);
45+
46+
// Additional safety check
47+
if (servletUrl == null || servletUrl.isEmpty()) {
48+
return null;
49+
}
50+
3651
ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new IngestionProxyServlet(),
37-
config.get(HubbleOptions.PROXY_SERVLET_URL));
38-
servletRegistrationBean.addInitParameter("targetUri", config.get(HubbleOptions.PROXY_TARGET_URL));
52+
servletUrl);
53+
servletRegistrationBean.addInitParameter("targetUri", targetUrl);
3954
servletRegistrationBean.addInitParameter(ProxyServlet.P_LOG, "true");
4055
return servletRegistrationBean;
4156
}

hugegraph-hubble/hubble-be/src/main/java/org/apache/hugegraph/config/WebMvcConfig.java

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,27 +21,47 @@
2121
import org.apache.hugegraph.handler.LoginInterceptor;
2222
import org.springframework.context.annotation.Bean;
2323
import org.springframework.context.annotation.Configuration;
24+
import org.springframework.core.io.ClassPathResource;
25+
import org.springframework.core.io.Resource;
2426
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
25-
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
27+
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
2628
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
29+
import org.springframework.web.servlet.resource.PathResourceResolver;
2730

2831
import org.apache.hugegraph.handler.CustomInterceptor;
2932

33+
import java.io.IOException;
34+
3035
@Configuration
3136
public class WebMvcConfig implements WebMvcConfigurer {
3237

3338
@Override
34-
public void addViewControllers(ViewControllerRegistry registry) {
35-
registry.addViewController("/{spring:[\\w-]+}")
36-
.setViewName("forward:/");
37-
registry.addViewController("/**/{spring:[\\w-]+}")
38-
.setViewName("forward:/");
39+
public void addResourceHandlers(ResourceHandlerRegistry registry) {
40+
registry.addResourceHandler("/**")
41+
.addResourceLocations("classpath:/ui/")
42+
.resourceChain(true)
43+
.addResolver(new PathResourceResolver() {
44+
@Override
45+
protected Resource getResource(String resourcePath,
46+
Resource location)
47+
throws IOException {
48+
Resource requested = location.createRelative(
49+
resourcePath);
50+
// If the requested resource exists and is readable,
51+
// serve it; otherwise fall back to index.html
52+
// for SPA routing
53+
if (requested.exists() && requested.isReadable()) {
54+
return requested;
55+
}
56+
return new ClassPathResource("/ui/index.html");
57+
}
58+
});
3959
}
4060

4161
@Override
4262
public void addInterceptors(InterceptorRegistry registry) {
4363
registry.addInterceptor(this.customInterceptor())
44-
.addPathPatterns("/**");
64+
.addPathPatterns("/api/**");
4565
registry.addInterceptor(this.loginInterceptor())
4666
.addPathPatterns("/api/**")
4767
.excludePathPatterns("/api/**/auth/login")

hugegraph-hubble/hubble-be/src/main/java/org/apache/hugegraph/controller/BaseController.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,19 @@ protected HugeClient unauthClient() {
153153
return client;
154154
}
155155

156+
protected HugeClient tempTokenClient() {
157+
HttpServletRequest request = getRequest();
158+
if (request.getAttribute("hugeClient") != null) {
159+
HugeClient client = (HugeClient) request.getAttribute("hugeClient");
160+
client.setAuthContext("Basic "+this.getToken());
161+
return client;
162+
}
163+
HugeClient client = this.hugeClientPoolService.createTempTokenClient(this.getToken());
164+
request.setAttribute("hugeClient", client);
165+
return client;
166+
}
167+
168+
156169
protected void clearRequestHugeClient() {
157170
HttpServletRequest request = getRequest();
158171
if (request.getAttribute("hugeClient") != null) {

hugegraph-hubble/hubble-be/src/main/java/org/apache/hugegraph/controller/auth/LoginController.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
import org.apache.hugegraph.controller.BaseController;
3535
import org.apache.hugegraph.structure.auth.LoginResult;
3636

37+
import java.util.Base64;
38+
3739
@RestController
3840
@RequestMapping(Constant.API_VERSION + "auth")
3941
public class LoginController extends BaseController {
@@ -45,8 +47,8 @@ public class LoginController extends BaseController {
4547
public Object login(@RequestBody Login login) {
4648
// Set Expire: 1 Month
4749
login.expire(60 * 60 * 24 * 30);
48-
49-
HugeClient client = unauthClient();
50+
this.setToken(encodeCredentials(login));
51+
HugeClient client = tempTokenClient();
5052
LoginResult result = client.auth().login(login);
5153
this.setUser(login.name());
5254
this.setSession("password", login.password());
@@ -62,6 +64,11 @@ public Object login(@RequestBody Login login) {
6264
return u;
6365
}
6466

67+
private String encodeCredentials(Login login) {
68+
String combined = login.name() + ":" + login.password();
69+
return Base64.getEncoder().encodeToString(combined.getBytes());
70+
}
71+
6572
@GetMapping("/status")
6673
public Object status() {
6774

hugegraph-hubble/hubble-be/src/main/java/org/apache/hugegraph/service/HugeClientPoolService.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,10 @@ public HugeClient createUnauthClient() {
8787
return getOrCreate(null, null, null, null);
8888
}
8989

90+
public HugeClient createTempTokenClient(String token){
91+
return getOrCreate(null,null,null,token);
92+
}
93+
9094
public HugeClient createAuthClient(String graphSpace,
9195
String graph, String token) {
9296
return getOrCreate(null, graphSpace, graph, token);

hugegraph-hubble/hubble-be/src/main/java/org/apache/hugegraph/service/space/VermeerService.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ public Map<String, Object> getVermeer(String username, String password,
6363
}
6464
HugeClientBuilder builder = HugeClient.builder(protocol + "://" + dashboard,
6565
null,
66-
null)
66+
null,
67+
true)
6768
.configUser(username, password);
6869
RestClient client = new RestClient(builder.url(), builder.token(),
6970
builder.timeout(),

0 commit comments

Comments
 (0)