Skip to content

Commit d15652f

Browse files
committed
Adding HostResolverMode as enum, minor changes
Signed-off-by: shikharj05 <8859327+shikharj05@users.noreply.github.com>
1 parent 903ac93 commit d15652f

File tree

5 files changed

+48
-16
lines changed

5 files changed

+48
-16
lines changed

src/main/java/org/opensearch/security/securityconf/ConfigModelV7.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
import org.opensearch.security.securityconf.impl.v7.RoleV7;
5353
import org.opensearch.security.securityconf.impl.v7.TenantV7;
5454
import org.opensearch.security.support.ConfigConstants;
55+
import org.opensearch.security.support.HostResolverMode;
5556
import org.opensearch.security.support.WildcardMatcher;
5657
import org.opensearch.security.user.User;
5758

@@ -330,15 +331,16 @@ private Set<String> map(final User user, final TransportAddress caller) {
330331
}
331332

332333
if (caller.address() != null
333-
&& (hostResolverMode.equalsIgnoreCase("ip-hostname") || hostResolverMode.equalsIgnoreCase("ip-hostname-lookup"))) {
334+
&& (hostResolverMode.equalsIgnoreCase(HostResolverMode.IP_HOSTNAME.getValue())
335+
|| hostResolverMode.equalsIgnoreCase(HostResolverMode.IP_HOSTNAME_LOOKUP.getValue()))) {
334336
final String hostName = caller.address().getHostString();
335337

336338
for (String p : WildcardMatcher.getAllMatchingPatterns(hostMatchers, hostName)) {
337339
securityRoles.addAll(hosts.get(p));
338340
}
339341
}
340342

341-
if (caller.address() != null && hostResolverMode.equalsIgnoreCase("ip-hostname-lookup")) {
343+
if (caller.address() != null && hostResolverMode.equalsIgnoreCase(HostResolverMode.IP_HOSTNAME_LOOKUP.getValue())) {
342344

343345
final String resolvedHostName = caller.address().getHostName();
344346

src/main/java/org/opensearch/security/support/HostAndCidrMatcher.java

+4-7
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,6 @@
2525
* This matcher supports both wildcard hostname patterns (e.g., *.example.com) and CIDR notation (e.g., 192.168.1.0/24).
2626
*/
2727
public class HostAndCidrMatcher {
28-
private static final String IP_HOSTNAME = "ip-hostname";
29-
private static final String IP_HOSTNAME_LOOKUP = "ip-hostname-lookup";
30-
3128
protected final Logger log = LogManager.getLogger(HostAndCidrMatcher.class);
3229
private final WildcardMatcher hostMatcher;
3330
private final List<IPAddressString> cidrMatchers;
@@ -66,7 +63,7 @@ public boolean matchesCidr(InetAddress address) {
6663
return cidrMatchers.stream().anyMatch(cidrAddress -> cidrAddress.contains(addressString));
6764
} catch (Exception e) {
6865
log.warn("Failed to process IP address {}: {}", address, e.getMessage());
69-
return false;
66+
throw new RuntimeException("Invalid Address format used");
7067
}
7168
}
7269

@@ -75,8 +72,7 @@ public boolean matchesCidr(InetAddress address) {
7572
* This method can perform DNS lookups depending on the hostResolverMode.
7673
*
7774
* @param address The IP address to check
78-
* @param hostResolverMode The resolution mode. Must be either "ip-hostname" or
79-
* "ip-hostname-lookup" to enable hostname matching
75+
* @param hostResolverMode The resolution mode. Must be one of {@link HostResolverMode} to enable hostname matching
8076
* @return true if the address matches any configured hostname pattern, false otherwise,
8177
* if the address is null, or if the resolver mode is invalid
8278
* @implNote This method may perform DNS lookups which could impact performance
@@ -88,7 +84,8 @@ public boolean matchesHostname(InetAddress address, String hostResolverMode) {
8884

8985
List<String> valuesToCheck = new ArrayList<>(List.of(address.getHostAddress()));
9086
if (hostResolverMode != null
91-
&& (hostResolverMode.equalsIgnoreCase(IP_HOSTNAME) || hostResolverMode.equalsIgnoreCase(IP_HOSTNAME_LOOKUP))) {
87+
&& (hostResolverMode.equalsIgnoreCase(HostResolverMode.IP_HOSTNAME.getValue())
88+
|| hostResolverMode.equalsIgnoreCase(HostResolverMode.IP_HOSTNAME_LOOKUP.getValue()))) {
9289
try {
9390
final String hostName = address.getHostName(); // potential blocking call
9491
valuesToCheck.add(hostName);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package org.opensearch.security.support;
2+
3+
public enum HostResolverMode {
4+
IP_HOSTNAME("ip-hostname"),
5+
IP_HOSTNAME_LOOKUP("ip-hostname-lookup");
6+
7+
private final String value;
8+
9+
HostResolverMode(String value) {
10+
this.value = value;
11+
}
12+
13+
public String getValue() {
14+
return value;
15+
}
16+
}

src/test/java/org/opensearch/security/support/HostAndCidrMatcherTest.java

-7
Original file line numberDiff line numberDiff line change
@@ -177,13 +177,6 @@ public void shouldHandleInvalidCidrNotation() throws Exception {
177177
assertThat(matcher.matchesCidr(address), is(false));
178178
}
179179

180-
@Test(expected = Exception.class)
181-
public void shouldHandleMalformedIpAddresses() throws Exception {
182-
matcher = new HostAndCidrMatcher(Arrays.asList(PRIVATE_CLASS_C_CIDR));
183-
InetAddress address = InetAddress.getByName("invalid.ip.address");
184-
matcher.matchesCidr(address);
185-
}
186-
187180
@Test
188181
public void shouldMatchIpHostnameLookupMode() throws Exception {
189182
matcher = new HostAndCidrMatcher(Arrays.asList(OPENSEARCH_DOMAIN));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package org.opensearch.security.support;
2+
3+
import org.junit.Test;
4+
5+
import static org.hamcrest.MatcherAssert.assertThat;
6+
import static org.hamcrest.Matchers.is;
7+
8+
public class HostResolverModeTest {
9+
10+
@Test
11+
public void testIpHostnameValue() {
12+
assertThat(HostResolverMode.IP_HOSTNAME.getValue(), is("ip-hostname"));
13+
}
14+
15+
@Test
16+
public void testIpHostnameLookupValue() {
17+
assertThat(HostResolverMode.IP_HOSTNAME_LOOKUP.getValue(), is("ip-hostname-lookup"));
18+
}
19+
20+
@Test
21+
public void testEnumCount() {
22+
assertThat(HostResolverMode.values().length, is(2));
23+
}
24+
}

0 commit comments

Comments
 (0)