Commit 4ac7ea82 by Johannes Stelzer

Option to use ip-address of network-interface instead of hostname

If `spring.boot.admin.client.useIpAdressOf=<network-interface>` is set properly the ip-address of the specified network-interface is used in the inferenced url to register with. In case the address cannot be determined an exception is thrown and the client won't register. This is for example useful in Docker environments making the inferenced urls work.
parent de787bf0
......@@ -16,6 +16,9 @@
package de.codecentric.boot.admin.config;
import java.net.InetAddress;
import java.net.InterfaceAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import org.springframework.beans.factory.annotation.Autowired;
......@@ -64,6 +67,12 @@ public class AdminClientProperties implements ApplicationListener<ApplicationEve
@Value("${endpoints.health.id:health}")
private String healthEndpointId;
/**
* If set, the address of the specified interface is used in url inference
* instead of the hostname.
*/
private String useIpAddressOf = null;
@Autowired
private ManagementServerProperties management;
......@@ -165,18 +174,14 @@ public class AdminClientProperties implements ApplicationListener<ApplicationEve
this.name = name;
}
private String getHostname() {
try {
return InetAddress.getLocalHost().getCanonicalHostName();
}
catch (UnknownHostException ex) {
throw new IllegalArgumentException(ex.getMessage(), ex);
}
public void setUseIpAddressOf(String useIpAddressOf) {
this.useIpAddressOf = useIpAddressOf;
}
private String createLocalUri(int port, String path) {
String scheme = server.getSsl() != null && server.getSsl().isEnabled() ? "https" : "http";
return append(scheme + "://" + getHostname() + ":" + port, path);
String scheme = server.getSsl() != null && server.getSsl().isEnabled() ? "https"
: "http";
return append(scheme + "://" + getHost() + ":" + port, path);
}
private String append(String uri, String path) {
......@@ -188,4 +193,65 @@ public class AdminClientProperties implements ApplicationListener<ApplicationEve
String normPath = path.replaceFirst("^/+", "").replaceFirst("/+$", "");
return baseUri + "/" + normPath;
}
}
private String getHost() {
if (useIpAddressOf == null) {
return getHostname();
} else {
return getHostIp();
}
}
private String getHostname() {
try {
return InetAddress.getLocalHost().getCanonicalHostName();
} catch (UnknownHostException ex) {
throw new IllegalArgumentException(ex.getMessage(), ex);
}
}
private String getHostIp() {
NetworkInterface nic;
try {
nic = NetworkInterface.getByName(useIpAddressOf);
} catch (SocketException ex) {
throw new IllegalArgumentException(ex.getMessage(), ex);
}
if (nic != null) {
InetAddress address = findIp(nic);
if (address != null) {
return address.getHostAddress();
}
throw new IllegalStateException(
"Couldn't determin InetAdress for network interface '"
+ useIpAddressOf + "'");
} else {
throw new IllegalArgumentException(
"Network interface"
+ useIpAddressOf
+ " not found! Please specify correct interface for spring.boot.admin.client.useIpAddressOf");
}
}
private InetAddress findIp(NetworkInterface nic) {
InetAddress candidate = null;
for (InterfaceAddress address : nic.getInterfaceAddresses()) {
if (!address.getAddress().isLoopbackAddress()) {
if (address.getAddress().isSiteLocalAddress()) {
return address.getAddress();
}
candidate = address.getAddress();
}
}
if (candidate != null) {
return candidate;
}
return null;
}
}
\ No newline at end of file
......@@ -111,7 +111,6 @@ public class AdminClientPropertiesTest {
+ ":8080/app"));
}
@Test
public void test_contextPath() {
load("server.context-path=app");
......@@ -128,8 +127,6 @@ public class AdminClientPropertiesTest {
+ ":80/app"));
}
@Test
public void test_default() {
load();
......@@ -145,7 +142,7 @@ public class AdminClientPropertiesTest {
assertThat(clientProperties.getServiceUrl(), is("http://" + getHostname()
+ ":8080"));
}
@Test
public void testSsl() {
load("server.ssl.key-store=somefile.jks", "server.ssl.key-store-password=password");
......@@ -162,6 +159,18 @@ public class AdminClientPropertiesTest {
+ ":8080"));
}
@Test(expected = IllegalArgumentException.class)
public void test_preferIpAddress_nic_not_exsts() {
load();
AdminClientProperties clientProperties = new AdminClientProperties();
clientProperties.setUseIpAddressOf("eth-not-exist");
context.getAutowireCapableBeanFactory().autowireBean(clientProperties);
publishServletContainerInitializedEvent(clientProperties, 8080, null);
clientProperties.getManagementUrl();
}
private String getHostname() {
try {
return InetAddress.getLocalHost().getCanonicalHostName();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment