Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.apache.cloudstack.metrics;

import com.cloud.utils.component.ManagerBase;
import com.cloud.utils.net.NetUtils;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;
Expand All @@ -26,9 +27,15 @@
import javax.inject.Inject;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.UnknownHostException;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Collections;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

public class PrometheusExporterServerImpl extends ManagerBase implements PrometheusExporterServer, Configurable {

Expand All @@ -37,6 +44,9 @@
@Inject
private PrometheusExporter prometheusExporter;

private String allowedAddressesConfig;
private Set<String> allowedAddresses = Collections.emptySet();

private final class ExporterHandler implements HttpHandler {
private PrometheusExporter prometheusExporter;

Expand All @@ -47,11 +57,11 @@

@Override
public void handle(final HttpExchange httpExchange) throws IOException {
final String remoteClientAddress = httpExchange.getRemoteAddress().getAddress().toString().replace("/", "");
final String remoteClientAddress = httpExchange.getRemoteAddress().getAddress().getHostAddress();
logger.debug("Prometheus exporter received client request from: " + remoteClientAddress);
String response = "Forbidden";
int responseCode = 403;
if (Arrays.asList(PrometheusExporterAllowedAddresses.value().split(",")).contains(remoteClientAddress)) {
if (getAllowedAddresses().contains(remoteClientAddress)) {
prometheusExporter.updateMetrics();
response = prometheusExporter.getMetrics();
responseCode = 200;
Expand All @@ -74,6 +84,42 @@
}
}

private synchronized Set<String> getAllowedAddresses() {

Check warning on line 87 in plugins/integrations/prometheus/src/main/java/org/apache/cloudstack/metrics/PrometheusExporterServerImpl.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Move this method into "ExporterHandler".

See more on https://sonarcloud.io/project/issues?id=apache_cloudstack&issues=AaGiDKUuFbDJMNha67MJ&open=AaGiDKUuFbDJMNha67MJ&pullRequest=13245
final String config = PrometheusExporterAllowedAddresses.value();
if (!config.equals(allowedAddressesConfig)) {
allowedAddresses = normalizeAddresses(config);
allowedAddressesConfig = config;
}
return allowedAddresses;
}

/**
* Normalizes a comma separated list of IP addresses to their canonical text form, so that
* equivalent notations (e.g. ::1 and 0:0:0:0:0:0:0:1) match the remote client address.
* Blank entries and anything that is not an IP literal are ignored, so no DNS resolution takes place.
*/
protected Set<String> normalizeAddresses(final String addresses) {
return Arrays.stream(addresses.split(","))
.map(String::trim)
.filter(addr -> !addr.isEmpty())
.map(this::normalizeAddress)
.filter(Objects::nonNull)
.collect(Collectors.toSet());
}

private String normalizeAddress(final String address) {
if (!NetUtils.isValidIp4(address) && !NetUtils.isValidIp6(address)) {
logger.warn("Ignoring invalid IP address [{}] in {}", address, PrometheusExporterAllowedAddresses.key());

Check warning on line 112 in plugins/integrations/prometheus/src/main/java/org/apache/cloudstack/metrics/PrometheusExporterServerImpl.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Invoke method(s) only conditionally.

See more on https://sonarcloud.io/project/issues?id=apache_cloudstack&issues=AaGiDKUuFbDJMNha67MI&open=AaGiDKUuFbDJMNha67MI&pullRequest=13245
return null;
}
try {
return InetAddress.getByName(address).getHostAddress();
} catch (UnknownHostException e) {
logger.warn("Ignoring IP address [{}] in {} that could not be parsed", address, PrometheusExporterAllowedAddresses.key(), e);
return null;
}
}

@Override
public boolean start() {
if (EnablePrometheusExporter.value()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package org.apache.cloudstack.metrics;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import java.util.Set;

import org.junit.Test;

public class PrometheusExporterServerImplTest {

private final PrometheusExporterServerImpl server = new PrometheusExporterServerImpl();

@Test
public void testNormalizeAddressesIpv6LoopbackForms() {
Set<String> addresses = server.normalizeAddresses("::1");
assertEquals(server.normalizeAddresses("0:0:0:0:0:0:0:1"), addresses);
assertTrue(addresses.contains("0:0:0:0:0:0:0:1"));
}

@Test
public void testNormalizeAddressesTrimsEntries() {
Set<String> addresses = server.normalizeAddresses(" 127.0.0.1 , 10.1.1.1");
assertEquals(Set.of("127.0.0.1", "10.1.1.1"), addresses);
}

@Test
public void testNormalizeAddressesIgnoresBlankEntries() {
assertTrue(server.normalizeAddresses("").isEmpty());
assertEquals(Set.of("10.1.1.1"), server.normalizeAddresses(",10.1.1.1,, ,"));
}

@Test
public void testNormalizeAddressesIgnoresNonIpEntries() {
assertEquals(Set.of("10.1.1.1"), server.normalizeAddresses("localhost,10.1.1.1,not-an-ip"));
}
}
Loading