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

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


The following commit(s) were added to refs/heads/master by this push:
     new 31fa63e7894 Web console: better management proxy detection (#15453)
31fa63e7894 is described below

commit 31fa63e7894e7e18bcaec7c98f27ef4d71b6fbfe
Author: Vadim Ogievetsky <[email protected]>
AuthorDate: Wed Nov 29 21:43:42 2023 -0800

    Web console: better management proxy detection (#15453)
    
    * better management proxy detection
    
    * fix checkstyle issue
    
    * add test
    
    * test should read the body also
    
    * use ObjectMapper
    
    * assert read ammount
---
 .../druid/server/AsyncManagementForwardingServlet.java   | 16 ++++++++++++++++
 .../server/AsyncManagementForwardingServletTest.java     | 15 +++++++++++++++
 web-console/src/helpers/capabilities.ts                  |  6 ++++--
 3 files changed, 35 insertions(+), 2 deletions(-)

diff --git 
a/server/src/main/java/org/apache/druid/server/AsyncManagementForwardingServlet.java
 
b/server/src/main/java/org/apache/druid/server/AsyncManagementForwardingServlet.java
index 65109c9f51f..22087edab6b 100644
--- 
a/server/src/main/java/org/apache/druid/server/AsyncManagementForwardingServlet.java
+++ 
b/server/src/main/java/org/apache/druid/server/AsyncManagementForwardingServlet.java
@@ -63,6 +63,9 @@ public class AsyncManagementForwardingServlet extends 
AsyncProxyServlet
   private static final String ARBITRARY_COORDINATOR_BASE_PATH = 
"/proxy/coordinator";
   private static final String ARBITRARY_OVERLORD_BASE_PATH = "/proxy/overlord";
 
+  // This path is used to check if the managment proxy is enabled, it simply 
returns {"enabled":true}
+  private static final String ENABLED_PATH = "/proxy/enabled";
+
   private final ObjectMapper jsonMapper;
   private final Provider<HttpClient> httpClientProvider;
   private final DruidHttpClientConfig httpClientConfig;
@@ -106,6 +109,9 @@ public class AsyncManagementForwardingServlet extends 
AsyncProxyServlet
           MODIFIED_PATH_ATTRIBUTE,
           
request.getRequestURI().substring(ARBITRARY_OVERLORD_BASE_PATH.length())
       );
+    } else if (ENABLED_PATH.equals(requestURI)) {
+      handleEnabledRequest(response);
+      return;
     } else {
       handleBadRequest(response, StringUtils.format("Unsupported proxy 
destination [%s]", request.getRequestURI()));
       return;
@@ -188,4 +194,14 @@ public class AsyncManagementForwardingServlet extends 
AsyncProxyServlet
     }
     response.flushBuffer();
   }
+
+  private void handleEnabledRequest(HttpServletResponse response) throws 
IOException
+  {
+    if (!response.isCommitted()) {
+      response.resetBuffer();
+      response.setStatus(HttpServletResponse.SC_OK);
+      jsonMapper.writeValue(response.getOutputStream(), 
ImmutableMap.of("enabled", true));
+    }
+    response.flushBuffer();
+  }
 }
diff --git 
a/server/src/test/java/org/apache/druid/server/AsyncManagementForwardingServletTest.java
 
b/server/src/test/java/org/apache/druid/server/AsyncManagementForwardingServletTest.java
index 5fff015a03b..9cba38e97fd 100644
--- 
a/server/src/test/java/org/apache/druid/server/AsyncManagementForwardingServletTest.java
+++ 
b/server/src/test/java/org/apache/druid/server/AsyncManagementForwardingServletTest.java
@@ -317,6 +317,21 @@ public class AsyncManagementForwardingServletTest extends 
BaseJettyTest
     Assert.assertTrue("overlord called", OVERLORD_EXPECTED_REQUEST.called);
   }
 
+  @Test
+  public void testProxyEnebledCheck() throws Exception
+  {
+    HttpURLConnection connection = ((HttpURLConnection)
+        new URL(StringUtils.format("http://localhost:%d/proxy/enabled";, 
port)).openConnection());
+    connection.setRequestMethod("GET");
+
+    Assert.assertEquals(200, connection.getResponseCode());
+    byte[] bytes = new byte[connection.getContentLength()];
+    Assert.assertEquals(connection.getInputStream().read(bytes), 
connection.getContentLength());
+    Assert.assertEquals(ImmutableMap.of("enabled", true), new 
ObjectMapper().readValue(bytes, Map.class));
+    Assert.assertFalse("coordinator called", 
COORDINATOR_EXPECTED_REQUEST.called);
+    Assert.assertFalse("overlord called", OVERLORD_EXPECTED_REQUEST.called);
+  }
+
   @Test
   public void testBadProxyDestination() throws Exception
   {
diff --git a/web-console/src/helpers/capabilities.ts 
b/web-console/src/helpers/capabilities.ts
index 87ff3a53b2f..4c5c9357cc1 100644
--- a/web-console/src/helpers/capabilities.ts
+++ b/web-console/src/helpers/capabilities.ts
@@ -102,11 +102,13 @@ export class Capabilities {
 
   static async detectManagementProxy(): Promise<boolean> {
     try {
-      await Api.instance.get(`/proxy/coordinator/status?capabilities`, {
+      await Api.instance.get(`/proxy/enabled?capabilities`, {
         timeout: Capabilities.STATUS_TIMEOUT,
       });
     } catch (e) {
-      return false;
+      const { response } = e;
+      // If we detect error code 400 the management proxy is enabled but just 
does not know about the recently added /proxy/enabled route so treat this as a 
win.
+      return response.status === 400;
     }
 
     return true;


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to