From 5f1363a7ce045f6be709660f2413373114e84860 Mon Sep 17 00:00:00 2001 From: "tim.csitkovics" Date: Thu, 1 Oct 2015 18:16:06 +0200 Subject: [PATCH 1/3] Implemented cidr in version 1.4 --- pom.xml | 6 +++ .../http/auth/InetAddressWhitelist.java | 50 +++++++++++++------ 2 files changed, 42 insertions(+), 14 deletions(-) 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..092857e 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,41 @@ 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) { + 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()); + } + } + + 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()); + } + + return new HashSet(listIps); + } /** * delegate method From 4d38dec519c8b956700ab0ee639361ac63e40f8a Mon Sep 17 00:00:00 2001 From: "tim.csitkovics" Date: Fri, 2 Oct 2015 11:04:02 +0200 Subject: [PATCH 2/3] changes --- .../plugins/http/auth/InetAddressWhitelist.java | 8 -------- 1 file changed, 8 deletions(-) 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 092857e..d1d99e4 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 @@ -116,14 +116,6 @@ static Set toInetAddress(List ips) { } } - 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()); - } - return new HashSet(listIps); } From 7b93f486215dd79e0ac25766d78b5322a1d5c898 Mon Sep 17 00:00:00 2001 From: "tim.csitkovics" Date: Fri, 2 Oct 2015 13:00:56 +0200 Subject: [PATCH 3/3] Added localhost to comply with tests --- .../plugins/http/auth/InetAddressWhitelist.java | 7 +++++++ 1 file changed, 7 insertions(+) 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 d1d99e4..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 @@ -96,6 +96,13 @@ static Set toInetAddress(List ips) { 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; }