Skip to content

Commit

Permalink
Fills the hasExplicitIndexPermission method in v6 config
Browse files Browse the repository at this point in the history
Signed-off-by: Darshit Chanpura <dchanp@amazon.com>
  • Loading branch information
DarshitChanpura committed Sep 6, 2023
1 parent c2ab7e6 commit 8f48a10
Showing 1 changed file with 30 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -455,8 +455,27 @@ public boolean hasExplicitIndexPermission(
IndexNameExpressionResolver resolver,
ClusterService cs
) {
// TODO: Handle this scenario in V6 config
return false;
final Set<String> indicesForRequest = new HashSet<>(resolved.getAllIndicesResolved(cs, resolver));

Check warning on line 458 in src/main/java/org/opensearch/security/securityconf/ConfigModelV6.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearch/security/securityconf/ConfigModelV6.java#L458

Added line #L458 was not covered by tests
if (indicesForRequest.isEmpty()) {
// If no indices could be found on the request there is no way to check for the explicit permissions
return false;

Check warning on line 461 in src/main/java/org/opensearch/security/securityconf/ConfigModelV6.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearch/security/securityconf/ConfigModelV6.java#L461

Added line #L461 was not covered by tests
}

final Set<String> explicitlyAllowedIndices = roles.stream()
.map(role -> role.getAllResolvedPermittedIndices(resolved, user, actions, resolver, cs, true))
.flatMap(Collection::stream)
.collect(Collectors.toSet());

Check warning on line 467 in src/main/java/org/opensearch/security/securityconf/ConfigModelV6.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearch/security/securityconf/ConfigModelV6.java#L464-L467

Added lines #L464 - L467 were not covered by tests

if (log.isDebugEnabled()) {
log.debug(

Check warning on line 470 in src/main/java/org/opensearch/security/securityconf/ConfigModelV6.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearch/security/securityconf/ConfigModelV6.java#L470

Added line #L470 was not covered by tests
"ExplicitIndexPermission check indices for request {}, explicitly allowed indices {}",
indicesForRequest.toString(),
explicitlyAllowedIndices.toString()

Check warning on line 473 in src/main/java/org/opensearch/security/securityconf/ConfigModelV6.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearch/security/securityconf/ConfigModelV6.java#L472-L473

Added lines #L472 - L473 were not covered by tests
);
}

indicesForRequest.removeAll(explicitlyAllowedIndices);
return indicesForRequest.isEmpty();

Check warning on line 478 in src/main/java/org/opensearch/security/securityconf/ConfigModelV6.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearch/security/securityconf/ConfigModelV6.java#L477-L478

Added lines #L477 - L478 were not covered by tests
}

// opensearchDashboards special only, terms eval
Expand All @@ -469,7 +488,7 @@ public Set<String> getAllPermittedIndicesForDashboards(
) {
Set<String> retVal = new HashSet<>();
for (SecurityRole sr : roles) {
retVal.addAll(sr.getAllResolvedPermittedIndices(Resolved._LOCAL_ALL, user, actions, resolver, cs));
retVal.addAll(sr.getAllResolvedPermittedIndices(Resolved._LOCAL_ALL, user, actions, resolver, cs, false));

Check warning on line 491 in src/main/java/org/opensearch/security/securityconf/ConfigModelV6.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearch/security/securityconf/ConfigModelV6.java#L491

Added line #L491 was not covered by tests
retVal.addAll(resolved.getRemoteIndices());
}
return Collections.unmodifiableSet(retVal);
Expand All @@ -479,7 +498,7 @@ public Set<String> getAllPermittedIndicesForDashboards(
public Set<String> reduce(Resolved resolved, User user, String[] actions, IndexNameExpressionResolver resolver, ClusterService cs) {
Set<String> retVal = new HashSet<>();
for (SecurityRole sr : roles) {
retVal.addAll(sr.getAllResolvedPermittedIndices(resolved, user, actions, resolver, cs));
retVal.addAll(sr.getAllResolvedPermittedIndices(resolved, user, actions, resolver, cs, false));

Check warning on line 501 in src/main/java/org/opensearch/security/securityconf/ConfigModelV6.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearch/security/securityconf/ConfigModelV6.java#L501

Added line #L501 was not covered by tests
}
if (log.isDebugEnabled()) {
log.debug("Reduced requested resolved indices {} to permitted indices {}.", resolved, retVal.toString());
Expand Down Expand Up @@ -547,7 +566,8 @@ private Set<String> getAllResolvedPermittedIndices(
User user,
String[] actions,
IndexNameExpressionResolver resolver,
ClusterService cs
ClusterService cs,
boolean matchExplicitly
) {

final Set<String> retVal = new HashSet<>();
Expand All @@ -556,7 +576,11 @@ private Set<String> getAllResolvedPermittedIndices(
boolean patternMatch = false;
final Set<TypePerm> tperms = p.getTypePerms();
for (TypePerm tp : tperms) {
if (tp.typeMatcher.matchAny(resolved.getTypes())) {
// if matchExplicitly is true we don't want to match against `*` pattern
WildcardMatcher matcher = matchExplicitly && (tp.getTypeMatcher() == WildcardMatcher.ANY)
? WildcardMatcher.NONE
: tp.getTypeMatcher();

Check warning on line 582 in src/main/java/org/opensearch/security/securityconf/ConfigModelV6.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearch/security/securityconf/ConfigModelV6.java#L581-L582

Added lines #L581 - L582 were not covered by tests
if (matcher.matchAny(resolved.getTypes())) {
patternMatch = tp.getPerms().matchAll(actions);
}
}
Expand Down

0 comments on commit 8f48a10

Please sign in to comment.