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
This commit is contained in:
Vadim Ogievetsky 2023-11-29 21:43:42 -08:00 committed by GitHub
parent 74ab6024e1
commit 31fa63e789
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 35 additions and 2 deletions

View File

@ -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();
}
}

View File

@ -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
{

View File

@ -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;