Skip to content

Commit 6a0b194

Browse files
Make hostname resolution for loopback address more robust. (#89788) (#89898)
Implemented a fall-back to `localhost` when FQDN for loopback address (`127.0.0.1`) cannot be resolved. This can happen if test platform's DNS resolution is not properly configured. Closes #89324
1 parent 3f328b7 commit 6a0b194

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

x-pack/qa/kerberos-tests/src/javaRestTest/java/org/elasticsearch/xpack/security/authc/kerberos/KerberosAuthenticationIT.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,8 +165,22 @@ public void testGetOauth2TokenInExchangeForKerberosTickets() throws PrivilegedAc
165165
@SuppressForbidden(reason = "SPNEGO relies on hostnames and we need to ensure host isn't a IP address")
166166
protected HttpHost buildHttpHost(String host, int port) {
167167
try {
168-
InetAddress inetAddress = InetAddress.getByName(host);
169-
return super.buildHttpHost(inetAddress.getCanonicalHostName(), port);
168+
final InetAddress address = InetAddress.getByName(host);
169+
final String hostname = address.getCanonicalHostName();
170+
// InetAddress#getCanonicalHostName depends on the system configuration (e.g. /etc/hosts) to return the FQDN.
171+
// In case InetAddress cannot resolve the FQDN it will return the textual representation of the IP address.
172+
if (hostname.equals(address.getHostAddress())) {
173+
if (address.isLoopbackAddress()) {
174+
// Fall-back and return "localhost" for loopback address if it's not resolved.
175+
// This is safe because InetAddress implements a reverse fall-back to loopback address
176+
// in case the resolution of "localhost" hostname fails.
177+
return super.buildHttpHost("localhost", port);
178+
} else {
179+
throw new IllegalStateException("failed to resolve [" + host + "] to FQDN");
180+
}
181+
} else {
182+
return super.buildHttpHost(hostname, port);
183+
}
170184
} catch (UnknownHostException e) {
171185
assumeNoException("failed to resolve host [" + host + "]", e);
172186
}

0 commit comments

Comments
 (0)