From a77006d7f077c94274d874568235f2af94befcd6 Mon Sep 17 00:00:00 2001 From: Thomas Spring Date: Tue, 6 Oct 2020 09:09:35 -0700 Subject: [PATCH 1/2] ignore SocketExceptions when attempting to connect to cloud providers --- .../java/com/newrelic/agent/utilization/CloudUtility.java | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/newrelic-agent/src/main/java/com/newrelic/agent/utilization/CloudUtility.java b/newrelic-agent/src/main/java/com/newrelic/agent/utilization/CloudUtility.java index e7bc187de9..38188c6586 100644 --- a/newrelic-agent/src/main/java/com/newrelic/agent/utilization/CloudUtility.java +++ b/newrelic-agent/src/main/java/com/newrelic/agent/utilization/CloudUtility.java @@ -24,6 +24,7 @@ import org.apache.http.util.EntityUtils; import java.io.IOException; +import java.net.SocketException; import java.net.SocketTimeoutException; import java.net.UnknownHostException; @@ -31,7 +32,7 @@ public class CloudUtility { // Spec: All characters should be in the following character class: over U+007F private static final int MIN_CHAR_CODEPOINT = "\u007F".codePointAt(0); - private final Function httpClientCreator; + private final Function httpClientCreator; public CloudUtility() { this(new Function() { @@ -58,7 +59,8 @@ public String httpPut(String url, int requestTimeoutMillis, String... headers) t return makeHttpRequest(httpPut, requestTimeoutMillis, headers); } - private String makeHttpRequest(HttpUriRequest request, int requestTimeoutMillis, String[] headers) throws IOException { + private String makeHttpRequest(HttpUriRequest request, int requestTimeoutMillis, String[] headers) + throws IOException { try (CloseableHttpClient httpclient = httpClientCreator.apply(requestTimeoutMillis)) { for (String header : headers) { String[] parts = header.split(":"); @@ -70,7 +72,7 @@ private String makeHttpRequest(HttpUriRequest request, int requestTimeoutMillis, if (response.getStatusLine().getStatusCode() <= HttpStatus.SC_MULTI_STATUS) { return EntityUtils.toString(response.getEntity(), "UTF-8"); } - } catch (ConnectTimeoutException | UnknownHostException | SocketTimeoutException ignored) { + } catch (ConnectTimeoutException | UnknownHostException | SocketTimeoutException | SocketException ignored) { // we expect these values in situations where there is no cloud provider, or // we're on a different cloud provider than expected. } From 00ecb3a5bfa44502c319f42f63fb0a8fcf58b27b Mon Sep 17 00:00:00 2001 From: Thomas Spring Date: Tue, 20 Oct 2020 11:39:31 -0700 Subject: [PATCH 2/2] Add test for SocketException --- .../agent/utilization/CloudUtilityTest.java | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/newrelic-agent/src/test/java/com/newrelic/agent/utilization/CloudUtilityTest.java b/newrelic-agent/src/test/java/com/newrelic/agent/utilization/CloudUtilityTest.java index 695e2c9d89..b16182b820 100644 --- a/newrelic-agent/src/test/java/com/newrelic/agent/utilization/CloudUtilityTest.java +++ b/newrelic-agent/src/test/java/com/newrelic/agent/utilization/CloudUtilityTest.java @@ -19,6 +19,7 @@ import org.junit.Test; import org.mockito.ArgumentCaptor; +import java.net.SocketException; import java.net.SocketTimeoutException; import static org.junit.Assert.assertEquals; @@ -111,6 +112,19 @@ public void testSocketTimeoutExceptionIsHandled() throws Exception { assertNull(result); } + @Test + public void testSocketExceptionIsHandled() throws Exception { + Function mockClientCreator = mock(Function.class); + CloseableHttpClient explodingClient = mock(CloseableHttpClient.class); + + when(mockClientCreator.apply(anyInt())).thenReturn(explodingClient); + when(explodingClient.execute(isA(HttpUriRequest.class))).thenThrow(new SocketException("oof")); + + CloudUtility testClass = new CloudUtility(mockClientCreator); + String result = testClass.httpGet("https://example.com/some/path", 12, "foo:bar"); + assertNull(result); + } + @Before public void before() { MockServiceManager mockServiceManager = new MockServiceManager();