diff --git a/pom.xml b/pom.xml index b2983a9..ee70d1f 100644 --- a/pom.xml +++ b/pom.xml @@ -67,6 +67,12 @@ test + + commons-net + commons-net + 3.3 + provided + diff --git a/src/main/java/com/asquera/elasticsearch/plugins/http/auth/InetAddressWhitelist.java b/src/main/java/com/asquera/elasticsearch/plugins/http/auth/InetAddressWhitelist.java index dd416f2..ffadfc8 100644 --- a/src/main/java/com/asquera/elasticsearch/plugins/http/auth/InetAddressWhitelist.java +++ b/src/main/java/com/asquera/elasticsearch/plugins/http/auth/InetAddressWhitelist.java @@ -1,4 +1,5 @@ package com.asquera.elasticsearch.plugins.http.auth; +import org.apache.commons.net.util.SubnetUtils; import org.elasticsearch.common.logging.Loggers; import java.util.ArrayList; @@ -90,20 +91,40 @@ Set getStringWhitelist() { * */ static Set toInetAddress(List ips) { - List listIps = new ArrayList(); - Iterator iterator = ips.iterator(); - while (iterator.hasNext()) { - String next = iterator.next(); - try { - listIps.add(InetAddress.getByName(next)); - } catch (UnknownHostException e) { - String template = "an ip set in the whitelist settings raised an " + - "UnknownHostException: {}, dropping it"; - Loggers.getLogger(InetAddressWhitelist.class).info(template, e.getMessage()); - } - } - return new HashSet(listIps); - } + List listIps = new ArrayList(); + Iterator iterator = ips.iterator(); + while (iterator.hasNext()) { + String next = iterator.next(); + if (next == null) { + try { + listIps.add(InetAddress.getByName("localhost")); + } catch (UnknownHostException e) { + String template = "an ip set in the whitelist settings raised an " + + "UnknownHostException: {}, dropping it"; + Loggers.getLogger(InetAddressWhitelist.class).info(template, e.getMessage()); + } + continue; + } + + try { + if (next.contains("/")) { + SubnetUtils subnetUtils = new SubnetUtils(next); + String[] allAddressesInRange = subnetUtils.getInfo().getAllAddresses(); + for (String addressInRange : allAddressesInRange) { + listIps.add(InetAddress.getByName(addressInRange)); + } + } else { + listIps.add(InetAddress.getByName(next)); + } + } catch (UnknownHostException e) { + String template = "an ip set in the whitelist settings raised an " + + "UnknownHostException: {}, dropping it"; + Loggers.getLogger(InetAddressWhitelist.class).info(template, e.getMessage()); + } + } + + return new HashSet(listIps); + } /** * delegate method