Use built-in basic auth support as it no longer requires JAX-B

Closes gh-28829
pull/29295/head
Andy Wilkinson 3 years ago
parent 29c654530c
commit 9a05eb63c4

@ -16,21 +16,16 @@
package org.springframework.boot.actuate.autoconfigure.metrics.export.prometheus; package org.springframework.boot.actuate.autoconfigure.metrics.export.prometheus;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.net.URL; import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.time.Duration; import java.time.Duration;
import java.util.Base64;
import java.util.Map; import java.util.Map;
import io.micrometer.core.instrument.Clock; import io.micrometer.core.instrument.Clock;
import io.micrometer.prometheus.PrometheusConfig; import io.micrometer.prometheus.PrometheusConfig;
import io.micrometer.prometheus.PrometheusMeterRegistry; import io.micrometer.prometheus.PrometheusMeterRegistry;
import io.prometheus.client.CollectorRegistry; import io.prometheus.client.CollectorRegistry;
import io.prometheus.client.exporter.DefaultHttpConnectionFactory; import io.prometheus.client.exporter.BasicAuthHttpConnectionFactory;
import io.prometheus.client.exporter.HttpConnectionFactory;
import io.prometheus.client.exporter.PushGateway; import io.prometheus.client.exporter.PushGateway;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
@ -159,29 +154,4 @@ public class PrometheusMetricsExportAutoConfiguration {
} }
static class BasicAuthHttpConnectionFactory implements HttpConnectionFactory {
private final HttpConnectionFactory delegate = new DefaultHttpConnectionFactory();
private final String authorizationHeader;
BasicAuthHttpConnectionFactory(String username, String password) {
this.authorizationHeader = "Basic " + new String(
Base64.getEncoder().encode(new String(username + ":" + password).getBytes(StandardCharsets.UTF_8)),
StandardCharsets.UTF_8);
}
String getAuthorizationHeader() {
return this.authorizationHeader;
}
@Override
public HttpURLConnection create(String url) throws IOException {
HttpURLConnection connection = this.delegate.create(url);
connection.setRequestProperty("Authorization", this.authorizationHeader);
return connection;
}
}
} }

@ -20,6 +20,7 @@ import io.micrometer.core.instrument.Clock;
import io.micrometer.prometheus.PrometheusConfig; import io.micrometer.prometheus.PrometheusConfig;
import io.micrometer.prometheus.PrometheusMeterRegistry; import io.micrometer.prometheus.PrometheusMeterRegistry;
import io.prometheus.client.CollectorRegistry; import io.prometheus.client.CollectorRegistry;
import io.prometheus.client.exporter.BasicAuthHttpConnectionFactory;
import io.prometheus.client.exporter.DefaultHttpConnectionFactory; import io.prometheus.client.exporter.DefaultHttpConnectionFactory;
import io.prometheus.client.exporter.HttpConnectionFactory; import io.prometheus.client.exporter.HttpConnectionFactory;
import io.prometheus.client.exporter.PushGateway; import io.prometheus.client.exporter.PushGateway;
@ -27,7 +28,6 @@ import org.assertj.core.api.ThrowingConsumer;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.boot.actuate.autoconfigure.metrics.export.prometheus.PrometheusMetricsExportAutoConfiguration.BasicAuthHttpConnectionFactory;
import org.springframework.boot.actuate.autoconfigure.web.server.ManagementContextAutoConfiguration; import org.springframework.boot.actuate.autoconfigure.web.server.ManagementContextAutoConfiguration;
import org.springframework.boot.actuate.metrics.export.prometheus.PrometheusPushGatewayManager; import org.springframework.boot.actuate.metrics.export.prometheus.PrometheusPushGatewayManager;
import org.springframework.boot.actuate.metrics.export.prometheus.PrometheusScrapeEndpoint; import org.springframework.boot.actuate.metrics.export.prometheus.PrometheusScrapeEndpoint;
@ -192,11 +192,8 @@ class PrometheusMetricsExportAutoConfigurationTests {
"management.metrics.export.prometheus.pushgateway.username=admin", "management.metrics.export.prometheus.pushgateway.username=admin",
"management.metrics.export.prometheus.pushgateway.password=secret") "management.metrics.export.prometheus.pushgateway.password=secret")
.withUserConfiguration(BaseConfiguration.class) .withUserConfiguration(BaseConfiguration.class)
.run(hasHttpConnectionFactory((httpConnectionFactory) -> { .run(hasHttpConnectionFactory((httpConnectionFactory) -> assertThat(httpConnectionFactory)
assertThat(httpConnectionFactory).isInstanceOf(BasicAuthHttpConnectionFactory.class); .isInstanceOf(BasicAuthHttpConnectionFactory.class)));
assertThat(((BasicAuthHttpConnectionFactory) httpConnectionFactory).getAuthorizationHeader())
.isEqualTo("Basic YWRtaW46c2VjcmV0");
}));
} }
private void hasGatewayURL(AssertableApplicationContext context, String url) { private void hasGatewayURL(AssertableApplicationContext context, String url) {

Loading…
Cancel
Save