diff --git a/pom.xml b/pom.xml index 12cc3dd..85ef039 100644 --- a/pom.xml +++ b/pom.xml @@ -55,6 +55,12 @@ test-jar + + commons-net + commons-net + 3.3 + provided + diff --git a/src/main/java/com/asquera/elasticsearch/plugins/http/HttpBasicServer.java b/src/main/java/com/asquera/elasticsearch/plugins/http/HttpBasicServer.java index 2590060..1311fc9 100644 --- a/src/main/java/com/asquera/elasticsearch/plugins/http/HttpBasicServer.java +++ b/src/main/java/com/asquera/elasticsearch/plugins/http/HttpBasicServer.java @@ -158,7 +158,7 @@ private boolean authBasic(final HttpRequest request) { String givenPass = userAndPassword[1]; if (this.user.equals(givenUser) && this.password.equals(givenPass)) return true; - } + } } catch (Exception e) { logger.warn("Retrieving of user and password failed for " + decoded + " ," + e.getMessage()); } 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..dc0e149 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,7 @@ package com.asquera.elasticsearch.plugins.http.auth; + +import org.apache.commons.net.util.SubnetUtils; + import org.elasticsearch.common.logging.Loggers; import java.util.ArrayList; @@ -12,8 +15,8 @@ /** * - * Wraps the configured whitelisted ips. - * It uses a set of {@link InetAddress} internally. + * Wraps the configured whitelisted ips. It uses a set of {@link InetAddress} + * internally. *

* * @@ -22,95 +25,109 @@ */ public class InetAddressWhitelist { - private Set whitelist; - /** - * - * - * @param whitelist - */ - public InetAddressWhitelist(Set whitelist) { - this.whitelist = whitelist; - } + private Set whitelist; + + /** + * + * + * @param whitelist + */ + public InetAddressWhitelist(Set whitelist) { + this.whitelist = whitelist; + } + + /** + * + * + * @param sWhitelist + * + */ + public InetAddressWhitelist(String[] sWhitelist) { + this(toInetAddress(Arrays.asList(sWhitelist))); + } + + /** + * Checks the request ip for inclusion. Since that ip comes in a + * {@link InetAddress} representation, it is checked against the whitelist. + * + * @param candidate + * @return if the ip is included in the whitelist + */ + public Boolean contains(InetAddress candidate) { + return this.whitelist.contains(candidate); + } - /** - * - * - * @param sWhitelist - * - */ - public InetAddressWhitelist(String[] sWhitelist) { - this(toInetAddress(Arrays.asList(sWhitelist))); - } + /** + * + * Checks the xForwardedFor defined client ip for inclusion. Since that ip + * comes in a String representation, it is checked against the String + * representation of the defined whitelist. + * + * @param candidate + * @return if the ip is included in the String representation of the + * whitelist ips + */ + public Boolean contains(String candidate) { + return getStringWhitelist().contains(candidate); + } - /** - * Checks the request ip for inclusion. - * Since that ip comes in a {@link InetAddress} representation, it is checked - * against the whitelist. - * - * @param candidate - * @return if the ip is included in the whitelist - */ - public Boolean contains(InetAddress candidate) { - return this.whitelist.contains(candidate); - } + /** + * @return set of the string representations of the whitelist + */ + Set getStringWhitelist() { + Iterator iterator = this.whitelist.iterator(); + Set set = new HashSet(); + while (iterator.hasNext()) { + InetAddress next = iterator.next(); + set.add(next.getHostAddress()); + } + return set; + } - /** - * - * Checks the xForwardedFor defined client ip for inclusion. - * Since that ip comes in a String representation, it is checked against - * the String representation of the defined whitelist. - * - * @param candidate - * @return if the ip is included in the String representation of the - * whitelist ips - */ - public Boolean contains(String candidate) { - return getStringWhitelist().contains(candidate); - } + /** + * when an configured InetAddress is Unkown or Invalid it is dropped from + * the whitelist + * + * @param ips + * a list of string ips + * @return a list of {@link InetAddress} objects + * + */ + static Set toInetAddress(List ips) { + List listIps = new ArrayList(); + Iterator iterator = ips.iterator(); + while (iterator.hasNext()) { + String next = iterator.next(); + if (next == null) { + next = "localhost"; + } - /** - * @return set of the string representations of the whitelist - */ - Set getStringWhitelist() { - Iterator iterator = this.whitelist.iterator(); - Set set = new HashSet(); - while (iterator.hasNext()) { - InetAddress next = iterator.next(); - set.add(next.getHostAddress()); - } - return set; - } + 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()); + } + } - /** - * when an configured InetAddress is Unkown or Invalid it is dropped from the - * whitelist - * - * @param ips a list of string ips - * @return a list of {@link InetAddress} objects - * - */ - 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); - } + return new HashSet(listIps); + } - /** - * delegate method - */ - @Override - public String toString() { - return whitelist.toString(); - } + /** + * delegate method + */ + @Override + public String toString() { + return whitelist.toString(); + } }