diff options
author | Bjørn Christian Seime <bjorncs@oath.com> | 2018-08-14 17:38:48 +0200 |
---|---|---|
committer | Bjørn Christian Seime <bjorncs@oath.com> | 2018-08-14 17:38:48 +0200 |
commit | 13d7b9aed7b5141896c27e56653432ce67777e69 (patch) | |
tree | 5967c77c06818b67cacdda0cdf92e2f6c6da3524 /service-monitor/src | |
parent | 09869d3ba2590ee29b3349eb68da9e75fd6bd525 (diff) |
Simplify HealthClient by using ServiceIdentitySslSocketFactory
Diffstat (limited to 'service-monitor/src')
6 files changed, 14 insertions, 72 deletions
diff --git a/service-monitor/src/main/java/com/yahoo/vespa/service/monitor/internal/health/HealthClient.java b/service-monitor/src/main/java/com/yahoo/vespa/service/monitor/internal/health/HealthClient.java index c8b2d5d25f7..7ee736d7020 100644 --- a/service-monitor/src/main/java/com/yahoo/vespa/service/monitor/internal/health/HealthClient.java +++ b/service-monitor/src/main/java/com/yahoo/vespa/service/monitor/internal/health/HealthClient.java @@ -2,8 +2,6 @@ package com.yahoo.vespa.service.monitor.internal.health; import com.fasterxml.jackson.databind.ObjectMapper; -import com.yahoo.vespa.athenz.api.AthenzService; -import com.yahoo.vespa.athenz.identity.ServiceIdentityProvider; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.config.RequestConfig; @@ -21,9 +19,7 @@ import org.apache.http.impl.conn.BasicHttpClientConnectionManager; import org.apache.http.protocol.HttpContext; import org.apache.http.util.EntityUtils; -import javax.net.ssl.SSLContext; import java.util.function.Function; -import java.util.function.Supplier; import static com.yahoo.yolean.Exceptions.uncheck; @@ -34,7 +30,7 @@ import static com.yahoo.yolean.Exceptions.uncheck; * * @author hakon */ -public class HealthClient implements AutoCloseable, ServiceIdentityProvider.Listener { +public class HealthClient implements AutoCloseable { private static final ObjectMapper mapper = new ObjectMapper(); private static final long MAX_CONTENT_LENGTH = 1L << 20; // 1 MB private static final int DEFAULT_TIMEOUT_MILLIS = 1_000; @@ -54,36 +50,24 @@ public class HealthClient implements AutoCloseable, ServiceIdentityProvider.List }; private final HealthEndpoint endpoint; - private final Supplier<CloseableHttpClient> clientSupplier; + private final CloseableHttpClient httpClient; private final Function<HttpEntity, String> getContentFunction; - private CloseableHttpClient httpClient = null; - public HealthClient(HealthEndpoint endpoint) { this(endpoint, - () -> makeCloseableHttpClient(endpoint), + makeCloseableHttpClient(endpoint), entity -> uncheck(() -> EntityUtils.toString(entity))); } /** For testing. */ HealthClient(HealthEndpoint endpoint, - Supplier<CloseableHttpClient> clientSupplier, + CloseableHttpClient httpClient, Function<HttpEntity, String> getContentFunction) { this.endpoint = endpoint; - this.clientSupplier = clientSupplier; + this.httpClient = httpClient; this.getContentFunction = getContentFunction; } - public void start() { - updateHttpClient(); - endpoint.registerListener(this); - } - - @Override - public void onCredentialsUpdate(SSLContext sslContext, AthenzService ignored) { - updateHttpClient(); - } - public HealthEndpoint getEndpoint() { return endpoint; } @@ -98,27 +82,11 @@ public class HealthClient implements AutoCloseable, ServiceIdentityProvider.List @Override public void close() { - endpoint.removeListener(this); - - if (httpClient != null) { - try { - httpClient.close(); - } catch (Exception e) { - // ignore - } - httpClient = null; - } - } - - private void updateHttpClient() { - CloseableHttpClient httpClient = clientSupplier.get(); - - if (this.httpClient != null) { - // Note: close() can be called any number of times. - uncheck(() -> this.httpClient.close()); + try { + httpClient.close(); + } catch (Exception e) { + // ignore } - - this.httpClient = httpClient; } private static CloseableHttpClient makeCloseableHttpClient(HealthEndpoint endpoint) { diff --git a/service-monitor/src/main/java/com/yahoo/vespa/service/monitor/internal/health/HealthEndpoint.java b/service-monitor/src/main/java/com/yahoo/vespa/service/monitor/internal/health/HealthEndpoint.java index 38139d28d7f..c74124da862 100644 --- a/service-monitor/src/main/java/com/yahoo/vespa/service/monitor/internal/health/HealthEndpoint.java +++ b/service-monitor/src/main/java/com/yahoo/vespa/service/monitor/internal/health/HealthEndpoint.java @@ -34,7 +34,5 @@ public interface HealthEndpoint { URL getStateV1HealthUrl(); ConnectionSocketFactory getConnectionSocketFactory(); - void registerListener(ServiceIdentityProvider.Listener listener); - void removeListener(ServiceIdentityProvider.Listener listener); String description(); } diff --git a/service-monitor/src/main/java/com/yahoo/vespa/service/monitor/internal/health/HealthMonitor.java b/service-monitor/src/main/java/com/yahoo/vespa/service/monitor/internal/health/HealthMonitor.java index 2574f782afb..28e1fc88816 100644 --- a/service-monitor/src/main/java/com/yahoo/vespa/service/monitor/internal/health/HealthMonitor.java +++ b/service-monitor/src/main/java/com/yahoo/vespa/service/monitor/internal/health/HealthMonitor.java @@ -45,7 +45,6 @@ public class HealthMonitor implements AutoCloseable { } public void startMonitoring() { - healthClient.start(); executor.scheduleWithFixedDelay( this::updateSynchronously, initialDelayInMillis(delay.toMillis()), diff --git a/service-monitor/src/main/java/com/yahoo/vespa/service/monitor/internal/health/HttpHealthEndpoint.java b/service-monitor/src/main/java/com/yahoo/vespa/service/monitor/internal/health/HttpHealthEndpoint.java index 254cb9785e1..97357fc0e2e 100644 --- a/service-monitor/src/main/java/com/yahoo/vespa/service/monitor/internal/health/HttpHealthEndpoint.java +++ b/service-monitor/src/main/java/com/yahoo/vespa/service/monitor/internal/health/HttpHealthEndpoint.java @@ -1,7 +1,6 @@ // Copyright 2018 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.vespa.service.monitor.internal.health; -import com.yahoo.vespa.athenz.identity.ServiceIdentityProvider; import org.apache.http.conn.socket.ConnectionSocketFactory; import org.apache.http.conn.socket.PlainConnectionSocketFactory; @@ -30,14 +29,6 @@ class HttpHealthEndpoint implements HealthEndpoint { } @Override - public void registerListener(ServiceIdentityProvider.Listener listener) { - } - - @Override - public void removeListener(ServiceIdentityProvider.Listener listener) { - } - - @Override public String description() { return url.toString(); } diff --git a/service-monitor/src/main/java/com/yahoo/vespa/service/monitor/internal/health/HttpsHealthEndpoint.java b/service-monitor/src/main/java/com/yahoo/vespa/service/monitor/internal/health/HttpsHealthEndpoint.java index f1ebb80f500..5201ac48dd3 100644 --- a/service-monitor/src/main/java/com/yahoo/vespa/service/monitor/internal/health/HttpsHealthEndpoint.java +++ b/service-monitor/src/main/java/com/yahoo/vespa/service/monitor/internal/health/HttpsHealthEndpoint.java @@ -2,11 +2,11 @@ package com.yahoo.vespa.service.monitor.internal.health; import com.yahoo.vespa.athenz.identity.ServiceIdentityProvider; +import com.yahoo.vespa.athenz.identity.ServiceIdentitySslSocketFactory; import org.apache.http.conn.socket.ConnectionSocketFactory; import org.apache.http.conn.ssl.SSLConnectionSocketFactory; import javax.net.ssl.HostnameVerifier; -import javax.net.ssl.SSLContext; import java.net.URL; /** @@ -32,18 +32,7 @@ public class HttpsHealthEndpoint implements HealthEndpoint { @Override public ConnectionSocketFactory getConnectionSocketFactory() { - SSLContext sslContext = serviceIdentityProvider.getIdentitySslContext(); - return new SSLConnectionSocketFactory(sslContext, hostnameVerifier); - } - - @Override - public void registerListener(ServiceIdentityProvider.Listener listener) { - serviceIdentityProvider.addIdentityListener(listener); - } - - @Override - public void removeListener(ServiceIdentityProvider.Listener listener) { - serviceIdentityProvider.removeIdentityListener(listener); + return new SSLConnectionSocketFactory(new ServiceIdentitySslSocketFactory(serviceIdentityProvider), hostnameVerifier); } @Override diff --git a/service-monitor/src/test/java/com/yahoo/vespa/service/monitor/internal/health/HealthClientTest.java b/service-monitor/src/test/java/com/yahoo/vespa/service/monitor/internal/health/HealthClientTest.java index c3e06faaf92..43200a8fa2e 100644 --- a/service-monitor/src/test/java/com/yahoo/vespa/service/monitor/internal/health/HealthClientTest.java +++ b/service-monitor/src/test/java/com/yahoo/vespa/service/monitor/internal/health/HealthClientTest.java @@ -110,8 +110,7 @@ public class HealthClientTest { HttpEntity httpEntity = mock(HttpEntity.class); when(response.getEntity()).thenReturn(httpEntity); - try (HealthClient healthClient = new HealthClient(endpoint, () -> client, entry -> content)) { - healthClient.start(); + try (HealthClient healthClient = new HealthClient(endpoint, client, entry -> content)) { when(httpEntity.getContentLength()).thenReturn((long) content.length()); return healthClient.getHealthInfo(); @@ -125,8 +124,7 @@ public class HealthClientTest { when(client.execute(any())).thenThrow(new ConnectTimeoutException("exception string")); - try (HealthClient healthClient = new HealthClient(endpoint, () -> client, entry -> "")) { - healthClient.start(); + try (HealthClient healthClient = new HealthClient(endpoint, client, entry -> "")) { HealthInfo info = healthClient.getHealthInfo(); assertFalse(info.isHealthy()); assertEquals(ServiceStatus.DOWN, info.toServiceStatus()); @@ -152,8 +150,7 @@ public class HealthClientTest { when(response.getEntity()).thenReturn(httpEntity); String content = "{}"; - try (HealthClient healthClient = new HealthClient(endpoint, () -> client, entry -> content)) { - healthClient.start(); + try (HealthClient healthClient = new HealthClient(endpoint, client, entry -> content)) { when(httpEntity.getContentLength()).thenReturn((long) content.length()); HealthInfo info = healthClient.getHealthInfo(); |