diff --git a/Dockerfile b/Dockerfile index 1b6e9be17..5af326bb3 100644 --- a/Dockerfile +++ b/Dockerfile @@ -50,6 +50,11 @@ RUN /opt/guacamole/bin/build-guacamole.sh "$BUILD_DIR" /opt/guacamole "$BUILD_PR # For the runtime image, we start with the official Tomcat distribution FROM tomcat:${TOMCAT_VERSION}-${TOMCAT_JRE} +# Install XMLStarlet for server.xml alterations +RUN apt-get update -qq \ + && apt-get install -y xmlstarlet \ + && rm -rf /var/lib/apt/lists/* + # This is where the build artifacts go in the runtime image WORKDIR /opt/guacamole @@ -68,4 +73,3 @@ USER guacamole # Start Guacamole under Tomcat, listening on 0.0.0.0:8080 EXPOSE 8080 CMD ["/opt/guacamole/bin/start.sh" ] - diff --git a/guacamole-docker/bin/start.sh b/guacamole-docker/bin/start.sh index b172d5e25..c9d205b58 100755 --- a/guacamole-docker/bin/start.sh +++ b/guacamole-docker/bin/start.sh @@ -910,6 +910,58 @@ associate_json() { # Add required .jar files to GUACAMOLE_EXT ln -s /opt/guacamole/json/guacamole-auth-*.jar "$GUACAMOLE_EXT" } +## +## Sets up Tomcat's remote IP valve that allows gathering the remote IP +## from headers set by a remote proxy +## Upstream documentation: https://tomcat.apache.org/tomcat-8.5-doc/api/org/apache/catalina/valves/RemoteIpValve.html +## +enable_remote_ip_valve() { + # Add element + xmlstarlet edit --inplace \ + --insert '/Server/Service/Engine/Host/*' --type elem -n Valve \ + --insert '/Server/Service/Engine/Host/Valve[not(@className)]' --type attr -n className -v org.apache.catalina.valves.RemoteIpValve \ + $CATALINA_BASE/conf/server.xml + + # Allowed IPs + if [ -z "$PROXY_ALLOWED_IPS_REGEX" ]; then + echo "Using default Tomcat allowed IPs regex" + else + xmlstarlet edit --inplace \ + --insert '/Server/Service/Engine/Host/Valve[@className="org.apache.catalina.valves.RemoteIpValve"]' \ + --type attr -n internalProxies -v "$PROXY_ALLOWED_IPS_REGEX" \ + $CATALINA_BASE/conf/server.xml + fi + + # X-Forwarded-For + if [ -z "$PROXY_IP_HEADER" ]; then + echo "Using default Tomcat proxy IP header" + else + xmlstarlet edit --inplace \ + --insert "/Server/Service/Engine/Host/Valve[@className='org.apache.catalina.valves.RemoteIpValve']" \ + --type attr -n remoteIpHeader -v "$PROXY_IP_HEADER" \ + $CATALINA_BASE/conf/server.xml + fi + + # X-Forwarded-Proto + if [ -z "$PROXY_PROTOCOL_HEADER" ]; then + echo "Using default Tomcat proxy protocol header" + else + xmlstarlet edit --inplace \ + --insert "/Server/Service/Engine/Host/Valve[@className='org.apache.catalina.valves.RemoteIpValve']" \ + --type attr -n protocolHeader -v "$PROXY_PROTOCOL_HEADER" \ + $CATALINA_BASE/conf/server.xml + fi + + # X-Forwarded-By + if [ -z "$PROXY_BY_HEADER" ]; then + echo "Using default Tomcat proxy forwarded by header" + else + xmlstarlet edit --inplace \ + --insert "/Server/Service/Engine/Host/Valve[@className='org.apache.catalina.valves.RemoteIpValve']" \ + --type attr -n remoteIpProxiesHeader -v "$PROXY_BY_HEADER" \ + $CATALINA_BASE/conf/server.xml + fi +} ## ## Adds api-session-timeout to guacamole.properties @@ -932,6 +984,11 @@ start_guacamole() { done cp -R /usr/local/tomcat/conf $CATALINA_BASE + # Set up Tomcat RemoteIPValve + if [ "$REMOTE_IP_VALVE_ENABLED" = "true" ]; then + enable_remote_ip_valve + fi + # Install webapp ln -sf /opt/guacamole/guacamole.war $CATALINA_BASE/webapps/${WEBAPP_CONTEXT:-guacamole}.war