Skip to content

Commit

Permalink
Respond 502 when target SSL untrusted
Browse files Browse the repository at this point in the history
Response with status code 502 and message "Target SSL Untrusted" when attempting a target connection fails for what appears to be an SSL trust issue

Fixes #307
  • Loading branch information
andrewazores committed Oct 23, 2020
1 parent ebd97e6 commit 8fe2746
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,14 @@
package com.redhat.rhjmc.containerjfr.net.web.http;

import java.nio.charset.StandardCharsets;
import java.rmi.ConnectIOException;
import java.util.Base64;
import java.util.concurrent.Future;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang3.exception.ExceptionUtils;

import org.openjdk.jmc.rjmx.ConnectionException;

import com.redhat.rhjmc.containerjfr.core.net.Credentials;
Expand Down Expand Up @@ -86,9 +89,13 @@ public void handle(RoutingContext ctx) {
Throwable cause = e.getCause();
if (cause instanceof SecurityException) {
ctx.response().putHeader(JMX_AUTHENTICATE_HEADER, "Basic");
throw new HttpStatusException(427, e);
throw new HttpStatusException(427, "JMX Authentication Failure", e);
}
Throwable rootCause = ExceptionUtils.getRootCause(e);
if (rootCause instanceof ConnectIOException) {
throw new HttpStatusException(502, "Target SSL Untrusted", e);
}
throw new HttpStatusException(404, e);
throw new HttpStatusException(500, e);
} catch (Exception e) {
throw new HttpStatusException(500, e.getMessage(), e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@

import static org.mockito.Mockito.when;

import java.rmi.ConnectIOException;
import java.util.concurrent.CompletableFuture;

import org.hamcrest.MatcherAssert;
Expand Down Expand Up @@ -124,13 +125,13 @@ void shouldPropagateIfHandlerThrowsHttpStatusException() {
}

@Test
void shouldThrow404IfConnectionFails() {
void shouldThrow500IfConnectionFails() {
Exception expectedException = new ConnectionException("");
handler = new ThrowingAuthenticatedHandler(auth, expectedException);

HttpStatusException ex =
Assertions.assertThrows(HttpStatusException.class, () -> handler.handle(ctx));
MatcherAssert.assertThat(ex.getStatusCode(), Matchers.equalTo(404));
MatcherAssert.assertThat(ex.getStatusCode(), Matchers.equalTo(500));
}

@Test
Expand All @@ -148,6 +149,19 @@ void shouldThrow427IfConnectionFailsDueToTargetAuth() {
Mockito.verify(resp).putHeader("X-JMX-Authenticate", "Basic");
}

@Test
void shouldThrow502IfConnectionFailsDueToSslTrust() {
Exception cause = new ConnectIOException("SSL trust");
Exception expectedException = new ConnectionException("");
expectedException.initCause(cause);
handler = new ThrowingAuthenticatedHandler(auth, expectedException);

HttpStatusException ex =
Assertions.assertThrows(HttpStatusException.class, () -> handler.handle(ctx));
MatcherAssert.assertThat(ex.getStatusCode(), Matchers.equalTo(502));
MatcherAssert.assertThat(ex.getPayload(), Matchers.equalTo("Target SSL Untrusted"));
}

@Test
void shouldThrow500IfHandlerThrowsUnexpectedly() {
Exception expectedException = new NullPointerException();
Expand Down

0 comments on commit 8fe2746

Please sign in to comment.