Skip to content

Commit d30ddee

Browse files
dhoardfstab
authored andcommitted
Added cleaner SSL support to HTTPServer
Signed-off-by: Doug Hoard <doug.hoard@gmail.com>
1 parent ffb1416 commit d30ddee

File tree

3 files changed

+277
-28
lines changed

3 files changed

+277
-28
lines changed

simpleclient_httpserver/src/main/java/io/prometheus/client/exporter/HTTPServer.java

+23-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package io.prometheus.client.exporter;
22

3+
import com.sun.net.httpserver.HttpsConfigurator;
4+
import com.sun.net.httpserver.HttpsServer;
35
import io.prometheus.client.CollectorRegistry;
46
import io.prometheus.client.SampleNameFilter;
57
import io.prometheus.client.Predicate;
@@ -200,6 +202,7 @@ public static class Builder {
200202
private Predicate<String> sampleNameFilter;
201203
private Supplier<Predicate<String>> sampleNameFilterSupplier;
202204
private Authenticator authenticator;
205+
private HttpsConfigurator httpsConfigurator;
203206

204207
/**
205208
* Port to bind to. Must not be called together with {@link #withInetSocketAddress(InetSocketAddress)}
@@ -299,6 +302,14 @@ public Builder withAuthenticator(Authenticator authenticator) {
299302
return this;
300303
}
301304

305+
/**
306+
* Optional: {@link HttpsConfigurator} to use to support TLS/SSL
307+
*/
308+
public Builder withHttpsConfigurator(HttpsConfigurator configurator) {
309+
this.httpsConfigurator = configurator;
310+
return this;
311+
}
312+
302313
/**
303314
* Build the HTTPServer
304315
* @throws IOException
@@ -308,11 +319,13 @@ public HTTPServer build() throws IOException {
308319
assertNull(sampleNameFilterSupplier, "cannot configure 'sampleNameFilter' and 'sampleNameFilterSupplier' at the same time");
309320
sampleNameFilterSupplier = SampleNameFilterSupplier.of(sampleNameFilter);
310321
}
322+
311323
if (httpServer != null) {
312324
assertZero(port, "cannot configure 'httpServer' and 'port' at the same time");
313325
assertNull(hostname, "cannot configure 'httpServer' and 'hostname' at the same time");
314326
assertNull(inetAddress, "cannot configure 'httpServer' and 'inetAddress' at the same time");
315327
assertNull(inetSocketAddress, "cannot configure 'httpServer' and 'inetSocketAddress' at the same time");
328+
assertNull(httpsConfigurator, "cannot configure 'httpServer' and 'httpsConfigurator' at the same time");
316329
return new HTTPServer(httpServer, registry, daemon, sampleNameFilterSupplier, authenticator);
317330
} else if (inetSocketAddress != null) {
318331
assertZero(port, "cannot configure 'inetSocketAddress' and 'port' at the same time");
@@ -326,7 +339,16 @@ public HTTPServer build() throws IOException {
326339
} else {
327340
inetSocketAddress = new InetSocketAddress(port);
328341
}
329-
return new HTTPServer(HttpServer.create(inetSocketAddress, 3), registry, daemon, sampleNameFilterSupplier, authenticator);
342+
343+
HttpServer httpServer = null;
344+
if (httpsConfigurator != null) {
345+
httpServer = HttpsServer.create(inetSocketAddress, 3);
346+
((HttpsServer)httpServer).setHttpsConfigurator(httpsConfigurator);
347+
} else {
348+
httpServer = HttpServer.create(inetSocketAddress, 3);
349+
}
350+
351+
return new HTTPServer(httpServer, registry, daemon, sampleNameFilterSupplier, authenticator);
330352
}
331353

332354
private void assertNull(Object o, String msg) {

0 commit comments

Comments
 (0)