Skip to content

Commit 3362d5a

Browse files
author
Daan Hoogland
committed
get forward header and apply it fro proxies
1 parent d4229d3 commit 3362d5a

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
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.NCSARequestLog;
@@ -185,6 +188,7 @@ public void start() throws Exception {
185188
httpConfig.setResponseHeaderSize(8192);
186189
httpConfig.setSendServerVersion(false);
187190
httpConfig.setSendDateHeader(false);
191+
addForwordingCustomiser(httpConfig);
188192

189193
// HTTP Connector
190194
createHttpConnector(httpConfig);
@@ -207,6 +211,21 @@ public void start() throws Exception {
207211
server.join();
208212
}
209213

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

0 commit comments

Comments
 (0)