This is an automated email from the ASF dual-hosted git repository.

dockerzhang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/inlong.git


The following commit(s) were added to refs/heads/master by this push:
     new fa8338e2e0 [INLONG-8169][Manager] Add default tenant "public" if the 
request does not specify (#8172)
fa8338e2e0 is described below

commit fa8338e2e04471693522e8572758f47564e7d9f3
Author: vernedeng <[email protected]>
AuthorDate: Wed Jun 7 15:30:19 2023 +0800

    [INLONG-8169][Manager] Add default tenant "public" if the request does not 
specify (#8172)
---
 .../org/apache/inlong/common/util/BasicAuth.java   |  3 +
 .../inlong/manager/web/InlongManagerMain.java      |  2 +
 ...etRequestFilter.java => EmptyTenantFilter.java} | 28 +++----
 .../web/filter/HttpServletRequestFilter.java       |  8 +-
 .../inlong/manager/web/filter/WebFilterConfig.java | 49 ++++++++++++
 .../manager/web/utils/InlongRequestWrapper.java    |  5 ++
 .../manager/web/filter/EmptyTenantFilterTest.java  | 87 ++++++++++++++++++++++
 7 files changed, 159 insertions(+), 23 deletions(-)

diff --git 
a/inlong-common/src/main/java/org/apache/inlong/common/util/BasicAuth.java 
b/inlong-common/src/main/java/org/apache/inlong/common/util/BasicAuth.java
index de03d9a32b..a8560e081a 100644
--- a/inlong-common/src/main/java/org/apache/inlong/common/util/BasicAuth.java
+++ b/inlong-common/src/main/java/org/apache/inlong/common/util/BasicAuth.java
@@ -28,6 +28,9 @@ import java.util.Base64;
 public class BasicAuth {
 
     public static final String BASIC_AUTH_HEADER = "Authorization";
+    public static final String BASIC_AUTH_TENANT_HEADER = "Tenant";
+
+    public static final String DEFAULT_TENANT = "public";
     public static final String BASIC_AUTH_PREFIX = "Basic";
     public static final String BASIC_AUTH_SEPARATOR = " ";
     public static final String BASIC_AUTH_JOINER = ":";
diff --git 
a/inlong-manager/manager-web/src/main/java/org/apache/inlong/manager/web/InlongManagerMain.java
 
b/inlong-manager/manager-web/src/main/java/org/apache/inlong/manager/web/InlongManagerMain.java
index 52b1288bd5..49584b27f7 100644
--- 
a/inlong-manager/manager-web/src/main/java/org/apache/inlong/manager/web/InlongManagerMain.java
+++ 
b/inlong-manager/manager-web/src/main/java/org/apache/inlong/manager/web/InlongManagerMain.java
@@ -20,6 +20,7 @@ package org.apache.inlong.manager.web;
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
 import 
org.springframework.boot.context.properties.EnableConfigurationProperties;
+import org.springframework.boot.web.servlet.ServletComponentScan;
 import org.springframework.context.annotation.ComponentScan;
 import org.springframework.scheduling.annotation.EnableScheduling;
 
@@ -27,6 +28,7 @@ import 
org.springframework.scheduling.annotation.EnableScheduling;
 @EnableConfigurationProperties
 @EnableScheduling
 @ComponentScan(basePackages = "org.apache.inlong.manager")
+@ServletComponentScan
 public class InlongManagerMain {
 
     public static void main(String[] args) {
diff --git 
a/inlong-manager/manager-web/src/main/java/org/apache/inlong/manager/web/filter/HttpServletRequestFilter.java
 
b/inlong-manager/manager-web/src/main/java/org/apache/inlong/manager/web/filter/EmptyTenantFilter.java
similarity index 66%
copy from 
inlong-manager/manager-web/src/main/java/org/apache/inlong/manager/web/filter/HttpServletRequestFilter.java
copy to 
inlong-manager/manager-web/src/main/java/org/apache/inlong/manager/web/filter/EmptyTenantFilter.java
index 55fdaefb98..efe619e0b1 100644
--- 
a/inlong-manager/manager-web/src/main/java/org/apache/inlong/manager/web/filter/HttpServletRequestFilter.java
+++ 
b/inlong-manager/manager-web/src/main/java/org/apache/inlong/manager/web/filter/EmptyTenantFilter.java
@@ -19,38 +19,34 @@ package org.apache.inlong.manager.web.filter;
 
 import org.apache.inlong.manager.web.utils.InlongRequestWrapper;
 
-import lombok.extern.slf4j.Slf4j;
-import org.springframework.core.annotation.Order;
-import org.springframework.stereotype.Component;
+import org.apache.commons.lang3.StringUtils;
 
 import javax.servlet.Filter;
 import javax.servlet.FilterChain;
 import javax.servlet.ServletException;
 import javax.servlet.ServletRequest;
 import javax.servlet.ServletResponse;
-import javax.servlet.annotation.WebFilter;
 import javax.servlet.http.HttpServletRequest;
 
 import java.io.IOException;
 
+import static org.apache.inlong.common.util.BasicAuth.BASIC_AUTH_TENANT_HEADER;
+import static org.apache.inlong.common.util.BasicAuth.DEFAULT_TENANT;
+
 /**
- * HttpServletRequestFilter
- * Make All
+ * Empty tenant filter.
+ * Add default tenant to header if the tenant has not been specified.
  */
-@Slf4j
-@Component
-@WebFilter
-@Order(0)
-public class HttpServletRequestFilter implements Filter {
+public class EmptyTenantFilter implements Filter {
 
     @Override
     public void doFilter(ServletRequest servletRequest, ServletResponse 
servletResponse, FilterChain filterChain)
             throws IOException, ServletException {
-        if (servletRequest instanceof HttpServletRequest) {
-            ServletRequest requestWrapper = new 
InlongRequestWrapper((HttpServletRequest) servletRequest);
-            filterChain.doFilter(requestWrapper, servletResponse);
-        } else {
-            filterChain.doFilter(servletRequest, servletResponse);
+        HttpServletRequest request = (HttpServletRequest) servletRequest;
+        String tenant = request.getHeader(BASIC_AUTH_TENANT_HEADER);
+        if (StringUtils.isBlank(tenant)) {
+            ((InlongRequestWrapper) 
servletRequest).addHeader(BASIC_AUTH_TENANT_HEADER, DEFAULT_TENANT);
         }
+        filterChain.doFilter(servletRequest, servletResponse);
     }
 }
diff --git 
a/inlong-manager/manager-web/src/main/java/org/apache/inlong/manager/web/filter/HttpServletRequestFilter.java
 
b/inlong-manager/manager-web/src/main/java/org/apache/inlong/manager/web/filter/HttpServletRequestFilter.java
index 55fdaefb98..d6b5e5a623 100644
--- 
a/inlong-manager/manager-web/src/main/java/org/apache/inlong/manager/web/filter/HttpServletRequestFilter.java
+++ 
b/inlong-manager/manager-web/src/main/java/org/apache/inlong/manager/web/filter/HttpServletRequestFilter.java
@@ -20,27 +20,21 @@ package org.apache.inlong.manager.web.filter;
 import org.apache.inlong.manager.web.utils.InlongRequestWrapper;
 
 import lombok.extern.slf4j.Slf4j;
-import org.springframework.core.annotation.Order;
-import org.springframework.stereotype.Component;
 
 import javax.servlet.Filter;
 import javax.servlet.FilterChain;
 import javax.servlet.ServletException;
 import javax.servlet.ServletRequest;
 import javax.servlet.ServletResponse;
-import javax.servlet.annotation.WebFilter;
 import javax.servlet.http.HttpServletRequest;
 
 import java.io.IOException;
 
 /**
  * HttpServletRequestFilter
- * Make All
+ * Make all request body modifiable
  */
 @Slf4j
-@Component
-@WebFilter
-@Order(0)
 public class HttpServletRequestFilter implements Filter {
 
     @Override
diff --git 
a/inlong-manager/manager-web/src/main/java/org/apache/inlong/manager/web/filter/WebFilterConfig.java
 
b/inlong-manager/manager-web/src/main/java/org/apache/inlong/manager/web/filter/WebFilterConfig.java
new file mode 100644
index 0000000000..fecddeb36c
--- /dev/null
+++ 
b/inlong-manager/manager-web/src/main/java/org/apache/inlong/manager/web/filter/WebFilterConfig.java
@@ -0,0 +1,49 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.inlong.manager.web.filter;
+
+import org.springframework.boot.web.servlet.FilterRegistrationBean;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+
+@Configuration
+public class WebFilterConfig {
+
+    @Bean
+    public FilterRegistrationBean registerHttpServletRequestFilter() {
+        FilterRegistrationBean<HttpServletRequestFilter> bean = new 
FilterRegistrationBean<>();
+        bean.setOrder(0);
+        bean.setFilter(new HttpServletRequestFilter());
+        bean.addUrlPatterns("/*");
+        bean.setMatchAfter(false);
+        bean.setName("httpServletRequestFilter");
+        return bean;
+    }
+
+    @Bean
+    public FilterRegistrationBean registerEmptyTenantFilter() {
+        FilterRegistrationBean<EmptyTenantFilter> bean = new 
FilterRegistrationBean<>();
+        bean.setOrder(1);
+        bean.setFilter(new EmptyTenantFilter());
+        bean.addUrlPatterns("/*");
+        bean.setMatchAfter(false);
+        bean.setName("emptyTenantFilter");
+        return bean;
+    }
+
+}
diff --git 
a/inlong-manager/manager-web/src/main/java/org/apache/inlong/manager/web/utils/InlongRequestWrapper.java
 
b/inlong-manager/manager-web/src/main/java/org/apache/inlong/manager/web/utils/InlongRequestWrapper.java
index b137bc1306..276bc77e1f 100644
--- 
a/inlong-manager/manager-web/src/main/java/org/apache/inlong/manager/web/utils/InlongRequestWrapper.java
+++ 
b/inlong-manager/manager-web/src/main/java/org/apache/inlong/manager/web/utils/InlongRequestWrapper.java
@@ -128,6 +128,11 @@ public class InlongRequestWrapper extends 
HttpServletRequestWrapper {
         return new BufferedReader(new 
InputStreamReader(this.getInputStream()));
     }
 
+    @Override
+    public String getHeader(String name) {
+        return this.headers.get(name);
+    }
+
     public void addHeader(String name, String value) {
         headers.put(name, value);
     }
diff --git 
a/inlong-manager/manager-web/src/test/java/org/apache/inlong/manager/web/filter/EmptyTenantFilterTest.java
 
b/inlong-manager/manager-web/src/test/java/org/apache/inlong/manager/web/filter/EmptyTenantFilterTest.java
new file mode 100644
index 0000000000..4bc9972d9a
--- /dev/null
+++ 
b/inlong-manager/manager-web/src/test/java/org/apache/inlong/manager/web/filter/EmptyTenantFilterTest.java
@@ -0,0 +1,87 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.inlong.manager.web.filter;
+
+import org.apache.inlong.common.util.BasicAuth;
+import org.apache.inlong.manager.web.WebBaseTest;
+
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+import org.springframework.mock.web.MockFilterChain;
+import org.springframework.mock.web.MockHttpServletRequest;
+import org.springframework.mock.web.MockHttpServletResponse;
+
+import javax.servlet.Filter;
+import javax.servlet.FilterChain;
+import javax.servlet.Servlet;
+import javax.servlet.ServletException;
+import javax.servlet.ServletRequest;
+import javax.servlet.ServletResponse;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+
+import java.io.IOException;
+
+public class EmptyTenantFilterTest extends WebBaseTest {
+
+    @Test
+    public void testNoTenant() {
+        MockHttpServletRequest req = new MockHttpServletRequest();
+        Servlet servlet = new HttpServlet() {
+        };
+        MockHttpServletResponse res = new MockHttpServletResponse();
+        HttpServletRequestFilter httpServletRequestFilter = new 
HttpServletRequestFilter();
+        EmptyTenantFilter emptyTenantFilter = new EmptyTenantFilter();
+        FilterChecker checker = new FilterChecker(BasicAuth.DEFAULT_TENANT);
+        MockFilterChain filterChain =
+                new MockFilterChain(servlet, httpServletRequestFilter, 
emptyTenantFilter, checker);
+        Assertions.assertDoesNotThrow(() -> filterChain.doFilter(req, res));
+    }
+
+    @Test
+    public void testWithTenant() {
+        MockHttpServletRequest req = new MockHttpServletRequest();
+        String testTenant = "testTenant";
+        req.addHeader(BasicAuth.BASIC_AUTH_TENANT_HEADER, testTenant);
+        Servlet servlet = new HttpServlet() {
+        };
+        MockHttpServletResponse res = new MockHttpServletResponse();
+        HttpServletRequestFilter httpServletRequestFilter = new 
HttpServletRequestFilter();
+        EmptyTenantFilter emptyTenantFilter = new EmptyTenantFilter();
+        FilterChecker checker = new FilterChecker(testTenant);
+        MockFilterChain filterChain =
+                new MockFilterChain(servlet, httpServletRequestFilter, 
emptyTenantFilter, checker);
+        Assertions.assertDoesNotThrow(() -> filterChain.doFilter(req, res));
+    }
+
+    class FilterChecker implements Filter {
+
+        String targetTenant;
+
+        public FilterChecker(String targetTenant) {
+            this.targetTenant = targetTenant;
+        }
+        @Override
+        public void doFilter(ServletRequest request, ServletResponse response, 
FilterChain chain)
+                throws IOException, ServletException {
+            HttpServletRequest httpServletRequest = (HttpServletRequest) 
request;
+            String tenant = 
httpServletRequest.getHeader(BasicAuth.BASIC_AUTH_TENANT_HEADER);
+            Assertions.assertEquals(targetTenant, tenant);
+        }
+    }
+}
\ No newline at end of file

Reply via email to