Skip to content

Commit 2feb7d7

Browse files
committed
Fix Prometheus exporter not recognizing ::1 as IPv6 loopback
Normalize IPv6 addresses using InetAddress.getHostAddress() so that compressed forms like ::1 and full forms like 0:0:0:0:0:0:0:1 are treated as equivalent when checking the allowed IPs list. Fixes #6714
1 parent 2ae1015 commit 2feb7d7

1 file changed

Lines changed: 16 additions & 2 deletions

File tree

plugins/integrations/prometheus/src/main/java/org/apache/cloudstack/metrics/PrometheusExporterServerImpl.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,13 @@
2626
import javax.inject.Inject;
2727
import java.io.IOException;
2828
import java.io.OutputStream;
29+
import java.net.InetAddress;
2930
import java.net.InetSocketAddress;
31+
import java.net.UnknownHostException;
3032
import java.nio.charset.StandardCharsets;
3133
import java.util.Arrays;
34+
import java.util.List;
35+
import java.util.stream.Collectors;
3236

3337
public class PrometheusExporterServerImpl extends ManagerBase implements PrometheusExporterServer, Configurable {
3438

@@ -47,11 +51,21 @@ private final class ExporterHandler implements HttpHandler {
4751

4852
@Override
4953
public void handle(final HttpExchange httpExchange) throws IOException {
50-
final String remoteClientAddress = httpExchange.getRemoteAddress().getAddress().toString().replace("/", "");
54+
final String remoteClientAddress = httpExchange.getRemoteAddress().getAddress().getHostAddress();
5155
logger.debug("Prometheus exporter received client request from: " + remoteClientAddress);
5256
String response = "Forbidden";
5357
int responseCode = 403;
54-
if (Arrays.asList(PrometheusExporterAllowedAddresses.value().split(",")).contains(remoteClientAddress)) {
58+
final List<String> allowedAddresses = Arrays.stream(PrometheusExporterAllowedAddresses.value().split(","))
59+
.map(addr -> {
60+
try {
61+
return InetAddress.getByName(addr.trim()).getHostAddress();
62+
} catch (UnknownHostException e) {
63+
logger.warn("Invalid IP address in prometheus.exporter.allowed.ips: " + addr.trim());
64+
return addr.trim();
65+
}
66+
})
67+
.collect(Collectors.toList());
68+
if (allowedAddresses.contains(remoteClientAddress)) {
5569
prometheusExporter.updateMetrics();
5670
response = prometheusExporter.getMetrics();
5771
responseCode = 200;

0 commit comments

Comments
 (0)