Skip to content

Commit 1e7e366

Browse files
authored
Merge pull request #231 from xenit-eu/DOCKER-471
DOCKER-471 Added remote IP valve to Tomcat
2 parents 522e5dc + fd8622d commit 1e7e366

File tree

12 files changed

+116
-6
lines changed

12 files changed

+116
-6
lines changed

Changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
1515
* [PR #78](https://github.com/xenit-eu/docker-alfresco/pull/78) DOCKER-408 Add timeout to health check commands
1616

1717
### Added
18+
* [PR #231](https://github.com/xenit-eu/docker-alfresco/pull/231) DOCKER-471 Added remote ip valve to Tomcat 10
1819
* [PR #223](https://github.com/xenit-eu/docker-alfresco/pull/223) DOCKER-466 Added Support for v25.1
1920
* [PR #222](https://github.com/xenit-eu/docker-alfresco/pull/222) DOCKER-464 Added json logging test
2021
* [PR #219](https://github.com/xenit-eu/docker-alfresco/pull/219) ALFREDAPI-569 Added Alfresco V23.3 & V23.4 support

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ in the following tables, are the values that are used when the environment varia
7575
| TOMCAT_ALLOW_CASUAL_MULTIPART_PARSING | false | Set to true if Tomcat should automatically parse multipart/form-data request bodies when HttpServletRequest.getPart* or HttpServletRequest.getParameter* is called. The default is false. |
7676
| TOMCAT_ALLOW_MULTIPLE_LEADING_FORWARD_SLASH_IN_PATH | false | Tomcat will collapse multiple leading / characters at the start of the return value for HttpServletRequest#getContextPath() to a single /. The default is false. |
7777
| TOMCAT_CROSS_CONTEXT | false | Set to true if you want calls within this application to ServletContext.getContext() to successfully return a request dispatcher for other web applications running on this virtual host. Set to false (the default) in security conscious environments, to make getContext() always return null. |
78+
| TOMCAT_REMOTE_IP_VALVE_ENABLED | true | If this is set to true, it will allow Tomcat to pick up remote ip headers like "X-Forwarded-For“, “X-Forwarded-Proto“ and “X-Forwarded-Port“. If Alfresco/Share are behind a proxy, they will be aware of that. |
7879
| JAVA_XMS | | -Xmx |
7980
| JAVA_XMX | | -Xms |
8081
| DEBUG | false | -Xdebug -Xrunjdwp:transport=dt_socket,address=8000,server=y,suspend=n |

tomcat-base/build.gradle

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ subprojects {
5050
implementation "eu.xenit.logging:json-logging:${jsonLoggingVersion}"
5151
testImplementation "org.junit.jupiter:junit-jupiter-api:5.10.0"
5252
testImplementation("org.junit-pioneer:junit-pioneer:2.1.0")
53+
testImplementation group: 'io.rest-assured', name: 'rest-assured', version: '5.3.2'
54+
testImplementation group: 'io.rest-assured', name: 'json-path', version: '5.3.2'
55+
testImplementation group: 'io.rest-assured', name: 'rest-assured-common', version: '5.3.2'
5356
testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:5.10.0"
5457
sharedLibs("eu.xenit.logging:json-logging:${jsonLoggingVersion}") {
5558
transitive = false
@@ -82,7 +85,9 @@ subprojects {
8285
test {
8386
java {
8487
srcDir file("$project.parent.projectDir/src/shared/test/java")
85-
88+
if (tomcatVersion.contains("10")) {
89+
srcDir file("$project.projectDir/src/test/java")
90+
}
8691
}
8792
resources {
8893
srcDir file("$project.parent.projectDir/src/shared/test/resources")

tomcat-base/src/shared/main/java/eu/xenit/alfresco/tomcat/embedded/alfresco/tomcat/AlfrescoTomcatFactoryHelper.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ public static void createSSLConnector(Tomcat tomcat, AlfrescoConfiguration alfre
6363
tomcatConfiguration.getTomcatMaxThreads(),
6464
tomcatConfiguration.getTomcatMaxHttpHeaderSize(),
6565
tomcatConfiguration.getTomcatRelaxedPathChars(),
66-
tomcatConfiguration.getTomcatRelaxedQueryChars()
66+
tomcatConfiguration.getTomcatRelaxedQueryChars(),
67+
tomcatConfiguration.isRemoteIpValveEnabled()
6768
);
6869

6970
SSLHostConfig sslHostConfig = new SSLHostConfig();

tomcat-base/src/shared/main/java/eu/xenit/alfresco/tomcat/embedded/config/DefaultConfigurationProvider.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ public TomcatConfiguration getConfiguration(TomcatConfiguration baseConfiguratio
2222
baseConfiguration.setAllowCasualMultipartParsing(false);
2323
baseConfiguration.setAllowMultipleLeadingForwardSlashInPath(false);
2424
baseConfiguration.setCrossContext(false);
25+
baseConfiguration.setRemoteIpValveEnabled(true);
2526
return baseConfiguration;
2627
}
2728
}

tomcat-base/src/shared/main/java/eu/xenit/alfresco/tomcat/embedded/config/EnvironmentVariableConfigurationProvider.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import static eu.xenit.alfresco.tomcat.embedded.config.EnvironmentVariables.TOMCAT_BASE_DIR;
1515
import static eu.xenit.alfresco.tomcat.embedded.config.EnvironmentVariables.TOMCAT_CACHE_MAX_SIZE;
1616
import static eu.xenit.alfresco.tomcat.embedded.config.EnvironmentVariables.TOMCAT_CROSS_CONTEXT;
17+
import static eu.xenit.alfresco.tomcat.embedded.config.EnvironmentVariables.TOMCAT_REMOTE_IP_VALVE_ENABLED;
1718
import static eu.xenit.alfresco.tomcat.embedded.config.EnvironmentVariables.TOMCAT_MAX_HTTP_HEADER_SIZE;
1819
import static eu.xenit.alfresco.tomcat.embedded.config.EnvironmentVariables.TOMCAT_MAX_THREADS;
1920
import static eu.xenit.alfresco.tomcat.embedded.config.EnvironmentVariables.TOMCAT_PORT;
@@ -50,6 +51,7 @@ public TomcatConfiguration getConfiguration(TomcatConfiguration baseConfiguratio
5051
setPropertyFromEnv(TOMCAT_ALLOW_CASUAL_MULTIPART_PARSING, value -> baseConfiguration.setAllowCasualMultipartParsing(Boolean.parseBoolean(value)));
5152
setPropertyFromEnv(TOMCAT_ALLOW_MULTIPLE_LEADING_FORWARD_SLASH_IN_PATH, value -> baseConfiguration.setAllowMultipleLeadingForwardSlashInPath(Boolean.parseBoolean(value)));
5253
setPropertyFromEnv(TOMCAT_CROSS_CONTEXT, value -> baseConfiguration.setCrossContext(Boolean.parseBoolean(value)));
54+
setPropertyFromEnv(TOMCAT_REMOTE_IP_VALVE_ENABLED, value -> baseConfiguration.setRemoteIpValveEnabled(Boolean.parseBoolean(value)));
5355
return baseConfiguration;
5456
}
5557
}

tomcat-base/src/shared/main/java/eu/xenit/alfresco/tomcat/embedded/config/EnvironmentVariables.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ public class EnvironmentVariables {
2222
public static final String TOMCAT_ALLOW_CASUAL_MULTIPART_PARSING = "TOMCAT_ALLOW_CASUAL_MULTIPART_PARSING";
2323
public static final String TOMCAT_ALLOW_MULTIPLE_LEADING_FORWARD_SLASH_IN_PATH = "TOMCAT_ALLOW_MULTIPLE_LEADING_FORWARD_SLASH_IN_PATH";
2424
public static final String TOMCAT_CROSS_CONTEXT = "TOMCAT_CROSS_CONTEXT";
25+
public static final String TOMCAT_REMOTE_IP_VALVE_ENABLED = "TOMCAT_REMOTE_IP_VALVE_ENABLED";
2526
private EnvironmentVariables() {
2627
}
2728

tomcat-base/src/shared/main/java/eu/xenit/alfresco/tomcat/embedded/config/TomcatConfiguration.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,5 @@ public class TomcatConfiguration {
3131
protected boolean allowCasualMultipartParsing;
3232
protected boolean allowMultipleLeadingForwardSlashInPath;
3333
protected boolean crossContext;
34+
protected boolean remoteIpValveEnabled;
3435
}

tomcat-base/src/shared/test/java/eu/xenit/alfresco/tomcat/embedded/config/DefaultConfigurationProviderTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ void testGetConfiguration() {
2828
expected.setAllowCasualMultipartParsing(false);
2929
expected.setAllowMultipleLeadingForwardSlashInPath(false);
3030
expected.setCrossContext(false);
31+
expected.setRemoteIpValveEnabled(true);
3132
assertEquals(configuration, expected);
3233
}
3334

tomcat-base/tomcat-embedded-10/src/main/java/eu/xenit/alfresco/tomcat/embedded/tomcat/TomcatFactory.java

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import org.apache.catalina.connector.Connector;
1818
import org.apache.catalina.core.StandardContext;
1919
import org.apache.catalina.startup.Tomcat;
20+
import org.apache.catalina.valves.RemoteIpValve;
2021
import org.apache.catalina.webresources.DirResourceSet;
2122
import org.apache.catalina.webresources.StandardRoot;
2223

@@ -37,7 +38,8 @@ public static Connector getConnector(
3738
int maxThreads,
3839
int maxHttpHeaderSize,
3940
String relaxedPathChars,
40-
String relaxedQueryChars) {
41+
String relaxedQueryChars,
42+
boolean isRemoteIpValveEnabled) {
4143
Connector connector = new Connector(protocol);
4244
connector.setPort(port);
4345
connector.setProperty("connectionTimeout", "240000");
@@ -49,11 +51,25 @@ public static Connector getConnector(
4951
connector.setProperty("relaxedQueryChars", relaxedQueryChars);
5052
connector.setScheme(scheme);
5153
Service service = tomcat.getService();
54+
if (isRemoteIpValveEnabled) {
55+
RemoteIpValve remoteIpValve = createRemoteIpValve();
56+
tomcat.getEngine().getPipeline().addValve(remoteIpValve);
57+
}
5258
service.setContainer(tomcat.getEngine());
5359
connector.setService(service);
5460
return connector;
5561
}
5662

63+
private static RemoteIpValve createRemoteIpValve() {
64+
RemoteIpValve remoteIpValve = new RemoteIpValve();
65+
remoteIpValve.setRemoteIpHeader("X-Forwarded-For");
66+
remoteIpValve.setProtocolHeader("X-Forwarded-Proto");
67+
remoteIpValve.setHostHeader("Host");
68+
remoteIpValve.setPortHeader("X-Forwarded-Port");
69+
70+
return remoteIpValve;
71+
}
72+
5773
private TomcatConfiguration getConfiguration() {
5874
return configuration;
5975
}
@@ -147,7 +163,8 @@ private void createDefaultConnector(Tomcat tomcat) {
147163
getConfiguration().getTomcatMaxThreads(),
148164
getConfiguration().getTomcatMaxHttpHeaderSize(),
149165
getConfiguration().getTomcatRelaxedPathChars(),
150-
getConfiguration().getTomcatRelaxedQueryChars()
166+
getConfiguration().getTomcatRelaxedQueryChars(),
167+
getConfiguration().isRemoteIpValveEnabled()
151168
);
152169
connector.setRedirectPort(getConfiguration().getTomcatSslPort());
153170
tomcat.setConnector(connector);

0 commit comments

Comments
 (0)