Skip to content

Commit fe8f3c8

Browse files
DaanHooglandDaan Hoogland
andauthored
get forward header for proxies and apply it in Jetty (#11386)
* get forward header and apply it fro proxies Co-authored-by: Daan Hoogland <[email protected]>
1 parent f020b5b commit fe8f3c8

File tree

3 files changed

+22
-6
lines changed

3 files changed

+22
-6
lines changed

client/src/main/java/org/apache/cloudstack/ServerDaemon.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,15 @@
2424
import java.io.InputStream;
2525
import java.lang.management.ManagementFactory;
2626
import java.net.URL;
27+
import java.util.Arrays;
2728
import java.util.Properties;
2829

30+
import com.cloud.api.ApiServer;
2931
import org.apache.commons.daemon.Daemon;
3032
import org.apache.commons.daemon.DaemonContext;
3133
import org.apache.commons.lang3.StringUtils;
3234
import org.eclipse.jetty.jmx.MBeanContainer;
35+
import org.eclipse.jetty.server.ForwardedRequestCustomizer;
3336
import org.eclipse.jetty.server.HttpConfiguration;
3437
import org.eclipse.jetty.server.HttpConnectionFactory;
3538
import org.eclipse.jetty.server.RequestLog;
@@ -184,6 +187,7 @@ public void start() throws Exception {
184187
httpConfig.setResponseHeaderSize(8192);
185188
httpConfig.setSendServerVersion(false);
186189
httpConfig.setSendDateHeader(false);
190+
addForwardingCustomiser(httpConfig);
187191

188192
// HTTP Connector
189193
createHttpConnector(httpConfig);
@@ -206,6 +210,21 @@ public void start() throws Exception {
206210
server.join();
207211
}
208212

213+
/**
214+
* Adds a ForwardedRequestCustomizer to the HTTP configuration to handle forwarded headers.
215+
* The header used for forwarding is determined by the ApiServer.listOfForwardHeaders property.
216+
* Only non empty headers are considered and only the first of the comma-separated list is used.
217+
* @param httpConfig the HTTP configuration to which the customizer will be added
218+
*/
219+
private static void addForwardingCustomiser(HttpConfiguration httpConfig) {
220+
ForwardedRequestCustomizer customiser = new ForwardedRequestCustomizer();
221+
String header = Arrays.stream(ApiServer.listOfForwardHeaders.value().split(",")).findFirst().orElse(null);
222+
if (com.cloud.utils.StringUtils.isNotEmpty(header)) {
223+
customiser.setForwardedForHeader(header);
224+
}
225+
httpConfig.addCustomizer(customiser);
226+
}
227+
209228
@Override
210229
public void stop() throws Exception {
211230
server.stop();

server/src/main/java/com/cloud/api/ApiServer.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -315,14 +315,14 @@ public class ApiServer extends ManagerBase implements HttpRequestHandler, ApiSer
315315
, "enables/disables checking of ipaddresses from a proxy set header. See \"proxy.header.names\" for the headers to allow."
316316
, true
317317
, ConfigKey.Scope.Global);
318-
static final ConfigKey<String> listOfForwardHeaders = new ConfigKey<>(ConfigKey.CATEGORY_NETWORK
318+
public static final ConfigKey<String> listOfForwardHeaders = new ConfigKey<>(ConfigKey.CATEGORY_NETWORK
319319
, String.class
320320
, "proxy.header.names"
321321
, "X-Forwarded-For,HTTP_CLIENT_IP,HTTP_X_FORWARDED_FOR"
322322
, "a list of names to check for allowed ipaddresses from a proxy set header. See \"proxy.cidr\" for the proxies allowed to set these headers."
323323
, true
324324
, ConfigKey.Scope.Global);
325-
static final ConfigKey<String> proxyForwardList = new ConfigKey<>(ConfigKey.CATEGORY_NETWORK
325+
public static final ConfigKey<String> proxyForwardList = new ConfigKey<>(ConfigKey.CATEGORY_NETWORK
326326
, String.class
327327
, "proxy.cidr"
328328
, ""

utils/src/main/java/com/cloud/utils/ConstantTimeComparator.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919

2020
package com.cloud.utils;
2121

22-
import java.nio.charset.Charset;
23-
2422
public class ConstantTimeComparator {
2523

2624
public static boolean compareBytes(byte[] b1, byte[] b2) {
@@ -36,7 +34,6 @@ public static boolean compareBytes(byte[] b1, byte[] b2) {
3634
}
3735

3836
public static boolean compareStrings(String s1, String s2) {
39-
final Charset encoding = Charset.forName("UTF-8");
40-
return compareBytes(s1.getBytes(encoding), s2.getBytes(encoding));
37+
return compareBytes(s1.getBytes(StringUtils.getPreferredCharset()), s2.getBytes(StringUtils.getPreferredCharset()));
4138
}
4239
}

0 commit comments

Comments
 (0)