Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for CIDR ranges in ignore_hosts setting. #5099

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

shikharj05
Copy link
Contributor

@shikharj05 shikharj05 commented Feb 7, 2025

Description

This change adds support to specify CIDR ranges in ignore_hosts settings.

Issues Resolved

#4927

Is this a backport? If so, please add backport PR # and/or commits #, and remove backport-failed label from the original PR.

Do these changes introduce new permission(s) to be displayed in the static dropdown on the front-end? If so, please open a draft PR in the security dashboards plugin and link the draft PR here

Testing

[Please provide details of testing done: unit testing, integration testing and manual testing]

Check List

  • New functionality includes testing
  • [TODO] New functionality has been documented
  • [NA] New Roles/Permissions have a corresponding security dashboards plugin PR
  • [NA] API changes companion pull request created
  • Commits are signed per the DCO using --signoff

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
For more information on following Developer Certificate of Origin and signing off your commits, please check here.

Copy link

codecov bot commented Feb 7, 2025

Codecov Report

Attention: Patch coverage is 86.66667% with 4 lines in your changes missing coverage. Please review.

Project coverage is 71.56%. Comparing base (e1c05c3) to head (6603a89).

Files with missing lines Patch % Lines
.../org/opensearch/security/auth/BackendRegistry.java 60.00% 0 Missing and 2 partials ⚠️
...org/opensearch/security/support/SecurityUtils.java 89.47% 0 Missing and 2 partials ⚠️
Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff           @@
##             main    #5099   +/-   ##
=======================================
  Coverage   71.55%   71.56%           
=======================================
  Files         336      336           
  Lines       22649    22667   +18     
  Branches     3603     3604    +1     
=======================================
+ Hits        16206    16221   +15     
- Misses       4641     4643    +2     
- Partials     1802     1803    +1     
Files with missing lines Coverage Δ
...ch/security/auth/limiting/AbstractRateLimiter.java 96.42% <100.00%> (+0.97%) ⬆️
.../org/opensearch/security/auth/BackendRegistry.java 77.60% <60.00%> (-0.80%) ⬇️
...org/opensearch/security/support/SecurityUtils.java 72.85% <89.47%> (+6.19%) ⬆️

... and 3 files with indirect coverage changes

@cwperks
Copy link
Member

cwperks commented Feb 7, 2025

Thank you for this PR @shikharj05. We will also want to update the documentation accordingly.

We could also consider adding support for config.dynamic.hosts_resolver_mode: ip-hostname or config.dynamic.hosts_resolver_mode: ip-hostname-lookup (IP + Hostname resolution when looking at requests) in a future feature request. Currently this only supports config.dynamic.hosts_resolver_mode: ip-only

^ Looks like these cases are already being handled as well


import org.opensearch.security.support.WildcardMatcher;
import org.opensearch.security.user.AuthCredentials;

public interface AuthFailureListener {
List<String> getIgnoreHosts();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There may be some overlap between this and the WildcardMatcher down below. Do you think we should remove the one below on favor of a single List<String> getIgnoreHosts(); which covers both static IPs and CIDR blocks?

@shikharj05
Copy link
Contributor Author

Thank you for this PR @shikharj05. We will also want to update the documentation accordingly.

Ack, will create a separate PR for docs.

@@ -66,6 +77,16 @@ public WildcardMatcher getIgnoreHostsMatcher() {
return hostMatcher;
}

@Override
public SubnetUtils.SubnetInfo getSubnetForCidr(String cidr) {
return SUBNET_CACHE.computeIfAbsent(cidr, pattern -> {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need a cache here? Can't the SubnetUtils instances be created upon initialization of an instance of this class (or a subclass)?

@@ -46,6 +54,7 @@ public final class SecurityUtils {
static final Pattern ENVBC_PATTERN = Pattern.compile("\\$\\{envbc" + ENV_PATTERN_SUFFIX);
static final Pattern ENVBASE64_PATTERN = Pattern.compile("\\$\\{envbase64" + ENV_PATTERN_SUFFIX);
public static Locale EN_Locale = forEN();
private static final Map<String, SubnetInfo> cidrCache = new ConcurrentHashMap<>();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need a cache on top on the cache in AbstractRateLimiter?

}
AuthFailureListener authFailureListener = (AuthFailureListener) clientBlockRegistry;
String hostAddress = address.getHostAddress();
return authFailureListener.getIgnoreHosts().parallelStream().filter(pattern -> pattern.indexOf('/') != -1).anyMatch(pattern -> {
Copy link
Collaborator

@nibix nibix Feb 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is using a parallelStream really justified here? AFAIK, it should be rather used in situations where the per-item operation clearly outweighs the cost of the thread management. I have doubts whether CIDR matching is really so heavy.

See: https://www.baeldung.com/java-when-to-use-parallel-stream#1-the-overhead

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants