Skip to content

Commit 027e650

Browse files
authored
Find and use non local IPv4 address while testing IP filtering (#40234) (#41143)
For pattern "n:localhost" PatternRule#isLocalhost() matches any local address, loopback address. [Note: I think for "localhost" this should not consider IP address as a match when they are bound to network interfaces. It should just be loopback address check unless the intent is to match all local addresses. This class is adopted from Netty3 and I am not sure if this is intended behavior or maybe I am missing something] For now I have fixed this assuming the PatternRule#isLocalhost check is correct by avoiding use of local address to check address denied. Closes #40194
1 parent aef2778 commit 027e650

File tree

1 file changed

+27
-2
lines changed
  • x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/transport/filter

1 file changed

+27
-2
lines changed

x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/transport/filter/IPFilterTests.java

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66
package org.elasticsearch.xpack.security.transport.filter;
77

8+
import org.elasticsearch.common.Numbers;
89
import org.elasticsearch.common.component.Lifecycle;
910
import org.elasticsearch.common.network.InetAddresses;
1011
import org.elasticsearch.common.network.NetworkAddress;
@@ -26,6 +27,9 @@
2627

2728
import java.net.InetAddress;
2829
import java.net.InetSocketAddress;
30+
import java.net.NetworkInterface;
31+
import java.net.SocketException;
32+
import java.net.UnknownHostException;
2933
import java.util.ArrayList;
3034
import java.util.Arrays;
3135
import java.util.Collections;
@@ -35,6 +39,7 @@
3539
import java.util.Map;
3640

3741
import static org.hamcrest.Matchers.is;
42+
import static org.hamcrest.Matchers.notNullValue;
3843
import static org.mockito.Matchers.eq;
3944
import static org.mockito.Mockito.mock;
4045
import static org.mockito.Mockito.verify;
@@ -140,7 +145,8 @@ public void testThatProfilesAreSupported() throws Exception {
140145
ipFilter = new IPFilter(settings, auditTrail, clusterSettings, licenseState);
141146
ipFilter.setBoundTransportAddress(transport.boundAddress(), transport.profileBoundAddresses());
142147
assertAddressIsAllowed("127.0.0.1");
143-
assertAddressIsDenied("192.168.0.1");
148+
// when "localhost" is used, ES considers all local addresses see PatternRule#isLocalhost()
149+
assertAddressIsDenied(randomNonLocalIPv4Address());
144150
assertAddressIsAllowedForProfile("client", "192.168.0.1");
145151
assertAddressIsDeniedForProfile("client", "192.168.0.2");
146152
}
@@ -161,7 +167,8 @@ public void testThatProfilesAreUpdateable() throws Exception {
161167
clusterSettings.updateDynamicSettings(newSettings, updatedSettingsBuilder, Settings.builder(), "test");
162168
clusterSettings.applySettings(updatedSettingsBuilder.build());
163169
assertAddressIsAllowed("127.0.0.1");
164-
assertAddressIsDenied("192.168.0.1");
170+
// when "localhost" is used, ES considers all local addresses see PatternRule#isLocalhost()
171+
assertAddressIsDenied(randomNonLocalIPv4Address());
165172
assertAddressIsAllowedForProfile("client", "192.168.0.1", "192.168.0.2");
166173
assertAddressIsDeniedForProfile("client", "192.168.0.3");
167174
}
@@ -297,4 +304,22 @@ private void assertAddressIsDeniedForProfile(String profile, String ... inetAddr
297304
private void assertAddressIsDenied(String ... inetAddresses) {
298305
assertAddressIsDeniedForProfile("default", inetAddresses);
299306
}
307+
308+
private String randomNonLocalIPv4Address() throws SocketException, UnknownHostException {
309+
String ipv4Address = null;
310+
int noOfRetries = 0;
311+
do {
312+
noOfRetries++;
313+
final InetAddress address = InetAddress.getByAddress(Numbers.intToBytes(randomInt()));
314+
if (address.isAnyLocalAddress() || address.isLoopbackAddress() || NetworkInterface.getByInetAddress(address) != null) {
315+
continue;
316+
} else {
317+
ipv4Address = NetworkAddress.format(address);
318+
break;
319+
}
320+
} while (ipv4Address == null && noOfRetries < 25);
321+
assertThat("could not generate random IPv4 address which is not local address", ipv4Address, notNullValue());
322+
return ipv4Address;
323+
}
324+
300325
}

0 commit comments

Comments
 (0)