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. 22
      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

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

@ -404,3 +404,25 @@ spring: @@ -404,3 +404,25 @@ spring:
- docker0
- 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 { @@ -98,7 +98,8 @@ public class InetUtils implements Closeable {
.getInetAddresses(); addrs.hasMoreElements();) {
InetAddress address = addrs.nextElement();
if (address instanceof Inet4Address
&& !address.isLoopbackAddress()) {
&& !address.isLoopbackAddress()
&& !ignoreAddress(address)) {
log.trace("Found non-loopback interface: "
+ ifc.getDisplayName());
result = address;
@ -127,7 +128,23 @@ public class InetUtils implements Closeable { @@ -127,7 +128,23 @@ public class InetUtils implements Closeable {
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()) {
if (interfaceName.matches(regex)) {
log.trace("Ignoring interface: " + interfaceName);

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

@ -1,5 +1,6 @@ @@ -1,5 +1,6 @@
package org.springframework.cloud.commons.util;
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.List;
@ -36,4 +37,14 @@ public class InetUtilsProperties { @@ -36,4 +37,14 @@ public class InetUtilsProperties {
* List of Java regex expressions for network interfaces that will be ignored.
*/
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 { @@ -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