Skip to content
Tutorials Center
My learning

Topics

Lesson 34 of 37 ยท Projects and Practice

Project: Review a Production server.xml

Build a server.xml linter in Java that parses the XML and flags risky settings, then use its report to harden and tune a Tomcat configuration step by step.

In this project you review a Tomcat server.xml the way a careful operator would before a launch. You will build a small linter that parses the XML and checks the rules from this chapter's lessons: security (hardening, TLS), connector sizing (thread pools) and logging (access logs). Then you fix the file by hand, guided by the report.

Tomcat cannot run on this site, so the linter is plain Java: it uses the JDK's XML parser on a configuration embedded in the program. The same code would work on a real file read from conf/server.xml.

The starting configuration

This is a trimmed but realistic default-style file with several problems, which we will hunt for:

XML
<Server port="8005" shutdown="SHUTDOWN">
  <Service name="Catalina">
    <Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443"/>
    <Connector port="8009" protocol="AJP/1.3" address="0.0.0.0" secretRequired="false"/>
    <Engine name="Catalina" defaultHost="localhost">
      <Realm className="org.apache.catalina.realm.UserDatabaseRealm" resourceName="UserDatabase"/>
      <Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true">
        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="localhost_access_log" suffix=".txt" pattern="%h %l %u %t &quot;%r&quot; %s %b"/>
      </Host>
    </Engine>
  </Service>
</Server>

Step 1: parse and run the first rules

The linter loads the XML, walks the elements and applies rules, each of which returns a message or nothing. Run it and read the report.

Java Project step 1: the linter

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

The two HIGH items are the exposed AJP connector. The rest are hygiene.

Step 2: fix the file

Work through the report and produce a corrected server.xml. This is the target (read-only; you cannot run it here):

XML
<Server port="-1" shutdown="SHUTDOWN">
  <Service name="Catalina">
    <Connector port="8080" protocol="HTTP/1.1" maxThreads="200" connectionTimeout="20000"
               redirectPort="8443" server="web" xpoweredBy="false"/>
    <Engine name="Catalina" defaultHost="localhost" jvmRoute="node1">
      <Realm className="org.apache.catalina.realm.LockOutRealm" failureCount="5" lockOutTime="300">
        <Realm className="org.apache.catalina.realm.UserDatabaseRealm" resourceName="UserDatabase"/>
      </Realm>
      <Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="false">
        <Valve className="org.apache.catalina.valves.RemoteIpValve" internalProxies="10\.0\.0\.\d+"/>
        <Valve className="org.apache.catalina.valves.ErrorReportValve" showReport="false" showServerInfo="false"/>
        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="localhost_access_log" suffix=".txt"
               pattern="%h %l %u %t &quot;%r&quot; %s %b %D" maxDays="14"/>
      </Host>
    </Engine>
  </Service>
</Server>

The AJP connector is removed entirely because nothing needs it. A TLS connector is left out because TLS ends at the proxy in this layout; if it did not, you would add the SSLHostConfig from the TLS lesson.

Step 3: prove the fix

Re-run the linter against the corrected XML. Paste the fixed file into the XML text block of the program (or change the program to read a file path) and confirm the report is clean. The program below embeds the fixed file and checks the important properties one by one.

Java Project step 3: verify the fixed file

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Extend the project

  • Read the file path from args and run the linter in your CI pipeline, failing the build on HIGH findings.
  • Add a rule that maxThreads is not above a limit you choose, and one that each Connector has connectionTimeout.
  • Warn when a TLS connector allows protocols older than TLS 1.2.
  • Check context.xml files for plain-text database passwords.
  • Compare two configurations and print only what changed between staging and production.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why does the linter treat an AJP connector without a secret as HIGH risk?

    The Ghostcat vulnerability and similar attacks abuse an exposed, unauthenticated AJP port.
  2. 2. What does port="-1" on the Server element do?

    A negative port turns the shutdown port off.
  3. 3. Which pattern code should be added to the access log for latency analysis?

    %D records processing time in milliseconds.
  4. 4. Why is a linter in CI useful?

    Automated checks keep every environment aligned with the agreed rules.

Summary

  • A configuration review is a checklist; code can run it for you on every change.
  • The biggest wins in the sample file were removing AJP, disabling the shutdown port, wrapping the realm and hiding errors.
  • Keep timeouts, thread limits, access log timing and autoDeploy explicit.
  • Extend the linter with the rules your team agrees on.

Next: Project: zero-downtime deployment