[java-idp-plugin-jetty] 01/03: JJETTY-26 Hostname derivation seems different than IdP installer

Rod Widdowson rdw at steadingsoftware.com
Wed Nov 12 20:08:49 UTC 2025


This is an automated email from the git hooks/post-receive script.

rdw pushed a commit to branch main
in repository java-idp-plugin-jetty.

View the commit online:
https://git.shibboleth.net/view/?p=java-idp-plugin-jetty.git;a=commit;h=5db1cf1f5203bff8cb7826d360bc1b4a37eb5416

commit 5db1cf1f5203bff8cb7826d360bc1b4a37eb5416
Author: Rod Widdowson <rdw at steadingsoftware.com>
AuthorDate: Wed Nov 12 17:10:15 2025 +0000

    JJETTY-26 Hostname derivation seems different than IdP installer
    
    https://shibboleth.atlassian.net/browse/JJETTY-26
    
    Borrow code from the installer (for V5 only) to derive a nicer name.
---
 .../shibboleth/idp/module/jetty/JettyModule.java   | 77 +++++++++++++++++++++-
 1 file changed, 76 insertions(+), 1 deletion(-)

diff --git a/jetty-impl/src/main/java/net/shibboleth/idp/module/jetty/JettyModule.java b/jetty-impl/src/main/java/net/shibboleth/idp/module/jetty/JettyModule.java
index 5d860d8..9bc5827 100644
--- a/jetty-impl/src/main/java/net/shibboleth/idp/module/jetty/JettyModule.java
+++ b/jetty-impl/src/main/java/net/shibboleth/idp/module/jetty/JettyModule.java
@@ -22,9 +22,13 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
 import java.io.PrintStream;
+import java.net.InetAddress;
+import java.net.NetworkInterface;
+import java.net.SocketException;
 import java.nio.file.DirectoryStream;
 import java.nio.file.Files;
 import java.nio.file.Path;
+import java.util.Collections;
 import java.util.Iterator;
 import java.util.Map;
 
@@ -33,6 +37,7 @@ import javax.annotation.Nonnull;
 import org.slf4j.Logger;
 
 import net.shibboleth.idp.Version;
+import net.shibboleth.idp.installer.InstallerSupport;
 import net.shibboleth.idp.installer.impl.InstallerProperties;
 import net.shibboleth.idp.module.impl.PluginIdPModule;
 import net.shibboleth.profile.module.ModuleContext;
@@ -176,7 +181,7 @@ public class JettyModule extends PluginIdPModule {
         final SelfSignedCertificateGenerator generator = new SelfSignedCertificateGenerator();
         generator.setKeystoreFile(keyStore.toFile());
         generator.setKeySize(InstallerProperties.DEFAULT_KEY_SIZE);
-        final String hostName = "localhost";
+        final String hostName = getBestHostName();
         generator.setHostName(hostName);
         final String altName = "https://" + hostName + "/idp/shibboleth";
         generator.setURISubjectAltNames(CollectionSupport.singletonList(altName));
@@ -190,4 +195,74 @@ public class JettyModule extends PluginIdPModule {
           throw new ModuleException("Error Key Store", e);
       }
     }
+
+    /**
+     * Is this address named?
+     *
+     * <p>Helper method for {@link getBestHostName}.</p>
+     * @param addr what to look at
+     * @return true unless the name is the canonical name
+     */
+    private boolean hasHostName(final InetAddress addr) {
+        return !addr.getHostAddress().equals(addr.getCanonicalHostName());
+    }
+
+    /**
+     * Find the most apposite network connector, taken from {@link InstallerSupport} (v5.2) taken from Ant.
+     * @deprecated in V6 we will go back to using the version in {@link InstallerSupport}
+     * @return the best name we can work out
+     */
+    // CheckStyle: CyclomaticComplexity OFF
+    @Nonnull
+    public String getBestHostName() {
+        InetAddress bestSoFar = null;
+        try {
+            for (final NetworkInterface netInterface : Collections.list(NetworkInterface.getNetworkInterfaces())) {
+                for (final InetAddress address : Collections.list(netInterface.getInetAddresses())) {
+                    if (bestSoFar == null) {
+                        // none selected so far, so this one is better.
+                        bestSoFar = address;
+                    } else if (address == null || address.isLoopbackAddress()) {
+                        // definitely not better than the previously selected address.
+                    } else if (address.isLinkLocalAddress()) {
+                        // link local considered better than loopback
+                        if (bestSoFar.isLoopbackAddress()) {
+                            bestSoFar = address;
+                        }
+                    } else if (address.isSiteLocalAddress()) {
+                        // site local considered better than link local (and loopback)
+                        // address with hostname resolved considered better than
+                        // address without hostname
+                        if (bestSoFar.isLoopbackAddress()
+                                || bestSoFar.isLinkLocalAddress()
+                                || (bestSoFar.isSiteLocalAddress() && !hasHostName(bestSoFar))) {
+                            bestSoFar = address;
+                        }
+                    } else {
+                        // current is a "Global address", considered better than
+                        // site local (and better than link local, loopback)
+                        // address with hostname resolved considered better than
+                        // address without hostname
+                        if (bestSoFar.isLoopbackAddress()
+                                || bestSoFar.isLinkLocalAddress()
+                                || bestSoFar.isSiteLocalAddress()
+                                || !hasHostName(bestSoFar)) {
+                            bestSoFar = address;
+                        }
+                    }
+                }
+            }
+        } catch (final SocketException e) {
+            log.error("Could not get host information", e);
+        }
+        if (bestSoFar == null) {
+            return "localhost.localdomain";
+        }
+        final String result = bestSoFar.getCanonicalHostName();
+        assert result!=null;
+        return result;
+    }
+    // CheckStyle: CyclomaticComplexity ON
+
+
 }

-- 
To stop receiving notification emails like this one, please contact
the administrator of this repository.


More information about the commits mailing list