Browse Source

Add new properties to customize InetUtils.

Added useOnlySiteLocalInterfaces and preferredNetworks properties to
InetUtils. The preferredNetworks property allows for a whitelist of
networks. The useOnlySiteLocalInterfaces allows InetAddresses where
isSiteLocalAddress is true.

Fixes gh-114
pull/127/head
Tomasz Juchniewicz 9 years ago committed by Spencer Gibb
parent
commit
e6ccb824a6
No known key found for this signature in database
GPG Key ID: 7788A47380690861
  1. 26
      docs/src/main/asciidoc/spring-cloud-commons.adoc
  2. 21
      spring-cloud-commons/src/main/java/org/springframework/cloud/commons/util/InetUtils.java
  3. 11
      spring-cloud-commons/src/main/java/org/springframework/cloud/commons/util/InetUtilsProperties.java
  4. 32
      spring-cloud-commons/src/test/java/org/springframework/cloud/commons/util/InetUtilsTests.java

26
docs/src/main/asciidoc/spring-cloud-commons.adoc

@ -393,7 +393,7 @@ TIP: If you see errors like `java.lang.IllegalArgumentException: Can not set org
[[ignore-network-interfaces]] [[ignore-network-interfaces]]
=== Ignore Network Interfaces === Ignore Network Interfaces
Sometimes it is useful to ignore certain named network interfaces so they can be excluded from Service Discovery registration (eg. running in a Docker container). A list of regular expressions can be set that will cause the desired network interfaces to be ignored. The following configuration will ignore the "docker0" interface and all interfaces that start with "veth". Sometimes it is useful to ignore certain named network interfaces so they can be excluded from Service Discovery registration (eg. running in a Docker container). A list of regular expressions can be set that will cause the desired network interfaces to be ignored. The following configuration will ignore the "docker0" interface and all interfaces that start with "veth".
.application.yml .application.yml
---- ----
@ -403,4 +403,26 @@ spring:
ignoredInterfaces: ignoredInterfaces:
- docker0 - docker0
- veth.* - veth.*
---- ----
You can also force to use only specified network addresses using list of regular expressions:
.application.yml
----
spring:
cloud:
inetutils:
preferredNetworks:
- 192.168
- 10.0
----
You can also force to use only site local addresses. See https://docs.oracle.com/javase/8/docs/api/java/net/Inet4Address.html#isSiteLocalAddress--[Inet4Address.html.isSiteLocalAddress()] for more details what is site local address.
.application.yml
----
spring:
cloud:
inetutils:
useOnlySiteLocalInterfaces: true
----

21
spring-cloud-commons/src/main/java/org/springframework/cloud/commons/util/InetUtils.java

@ -98,7 +98,8 @@ public class InetUtils implements Closeable {
.getInetAddresses(); addrs.hasMoreElements();) { .getInetAddresses(); addrs.hasMoreElements();) {
InetAddress address = addrs.nextElement(); InetAddress address = addrs.nextElement();
if (address instanceof Inet4Address if (address instanceof Inet4Address
&& !address.isLoopbackAddress()) { && !address.isLoopbackAddress()
&& !ignoreAddress(address)) {
log.trace("Found non-loopback interface: " log.trace("Found non-loopback interface: "
+ ifc.getDisplayName()); + ifc.getDisplayName());
result = address; result = address;
@ -127,7 +128,23 @@ public class InetUtils implements Closeable {
return null; return null;
} }
boolean ignoreInterface(String interfaceName) { /** for testing */ boolean ignoreAddress(InetAddress address) {
if (this.properties.isUseOnlySiteLocalInterfaces() && !address.isSiteLocalAddress()) {
log.trace("Ignoring address: " + address.getHostAddress());
return true;
}
for (String regex : this.properties.getPreferredNetworks()) {
if (!address.getHostAddress().matches(regex) && !address.getHostAddress().startsWith(regex)) {
log.trace("Ignoring address: " + address.getHostAddress());
return true;
}
}
return false;
}
/** for testing */ boolean ignoreInterface(String interfaceName) {
for (String regex : this.properties.getIgnoredInterfaces()) { for (String regex : this.properties.getIgnoredInterfaces()) {
if (interfaceName.matches(regex)) { if (interfaceName.matches(regex)) {
log.trace("Ignoring interface: " + interfaceName); log.trace("Ignoring interface: " + interfaceName);

11
spring-cloud-commons/src/main/java/org/springframework/cloud/commons/util/InetUtilsProperties.java

@ -1,5 +1,6 @@
package org.springframework.cloud.commons.util; package org.springframework.cloud.commons.util;
import java.net.InetAddress;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -36,4 +37,14 @@ public class InetUtilsProperties {
* List of Java regex expressions for network interfaces that will be ignored. * List of Java regex expressions for network interfaces that will be ignored.
*/ */
private List<String> ignoredInterfaces = new ArrayList<>(); private List<String> ignoredInterfaces = new ArrayList<>();
/**
* Use only interfaces with site local addresses. See {@link InetAddress#isSiteLocalAddress()} for more details.
*/
private boolean useOnlySiteLocalInterfaces = false;
/**
* List of Java regex expressions for network addresses that will be ignored.
*/
private List<String> preferredNetworks = new ArrayList<>();
} }

32
spring-cloud-commons/src/test/java/org/springframework/cloud/commons/util/InetUtilsTests.java

@ -86,4 +86,36 @@ public class InetUtilsTests {
} }
} }
@Test
public void testSiteLocalAddresses() throws Exception {
InetUtilsProperties properties = new InetUtilsProperties();
properties.setUseOnlySiteLocalInterfaces(true);
try (InetUtils utils = new InetUtils(properties)) {
assertFalse(utils.ignoreAddress(InetAddress.getByName("192.168.0.1")));
assertTrue(utils.ignoreAddress(InetAddress.getByName("5.5.8.1")));
}
}
@Test
public void testPrefferedNetworksRegex() throws Exception {
InetUtilsProperties properties = new InetUtilsProperties();
properties.setPreferredNetworks(Arrays.asList("192.168.*"));
try (InetUtils utils = new InetUtils(properties)) {
assertFalse(utils.ignoreAddress(InetAddress.getByName("192.168.0.1")));
assertTrue(utils.ignoreAddress(InetAddress.getByName("5.5.8.1")));
}
}
@Test
public void testPrefferedNetworksSimple() throws Exception {
InetUtilsProperties properties = new InetUtilsProperties();
properties.setPreferredNetworks(Arrays.asList("192"));
try (InetUtils utils = new InetUtils(properties)) {
assertFalse(utils.ignoreAddress(InetAddress.getByName("192.168.0.1")));
assertTrue(utils.ignoreAddress(InetAddress.getByName("5.5.8.1")));
}
}
} }

Loading…
Cancel
Save