Skip to content

Commit e0f6a89

Browse files
committed
Optimize getFieldFilter to only return a predicate when index has FLS restrictions for user
Signed-off-by: Craig Perkins <[email protected]>
1 parent 6660542 commit e0f6a89

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

src/main/java/org/opensearch/security/OpenSearchSecurityPlugin.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2328,13 +2328,24 @@ public Collection<Class<? extends LifecycleComponent>> getGuiceServiceClasses()
23282328
public Function<String, Predicate<String>> getFieldFilter() {
23292329
return index -> {
23302330
if (threadPool == null || dlsFlsValve == null) {
2331-
return field -> true;
2331+
return NOOP_FIELD_PREDICATE;
23322332
}
23332333

23342334
PrivilegesEvaluationContext ctx = this.dlsFlsBaseContext != null
23352335
? this.dlsFlsBaseContext.getPrivilegesEvaluationContext()
23362336
: null;
23372337

2338+
boolean indexHasRestrictions = false;
2339+
try {
2340+
indexHasRestrictions = dlsFlsValve.indexHasFlsRestrictions(index, ctx);
2341+
} catch (PrivilegesEvaluationException e) {
2342+
log.error("Error while evaluating FLS restrictions for {}", index, e);
2343+
}
2344+
2345+
if (!indexHasRestrictions) {
2346+
return NOOP_FIELD_PREDICATE;
2347+
}
2348+
23382349
return field -> {
23392350
try {
23402351
return dlsFlsValve.isFieldAllowed(index, field, ctx);

src/main/java/org/opensearch/security/configuration/DlsFlsRequestValve.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ public interface DlsFlsRequestValve {
5151

5252
boolean isFieldAllowed(String index, String field, PrivilegesEvaluationContext ctx) throws PrivilegesEvaluationException;
5353

54+
boolean indexHasFlsRestrictions(String index, PrivilegesEvaluationContext ctx) throws PrivilegesEvaluationException;
55+
5456
public static class NoopDlsFlsRequestValve implements DlsFlsRequestValve {
5557

5658
@Override
@@ -87,6 +89,11 @@ public boolean hasFieldMasking(String index) {
8789
public boolean isFieldAllowed(String index, String field, PrivilegesEvaluationContext ctx) {
8890
return true;
8991
}
92+
93+
@Override
94+
public boolean indexHasFlsRestrictions(String index, PrivilegesEvaluationContext ctx) {
95+
return false;
96+
}
9097
}
9198

9299
}

src/main/java/org/opensearch/security/configuration/DlsFlsValveImpl.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,15 @@ public boolean isFieldAllowed(String index, String field, PrivilegesEvaluationCo
538538
return config.getFieldPrivileges().getRestriction(ctx, index).isAllowedRecursive(field);
539539
}
540540

541+
@Override
542+
public boolean indexHasFlsRestrictions(String index, PrivilegesEvaluationContext ctx) throws PrivilegesEvaluationException {
543+
if (ctx == null) {
544+
return false;
545+
}
546+
DlsFlsProcessedConfig config = this.dlsFlsProcessedConfig.get();
547+
return !config.getFieldPrivileges().getRestriction(ctx, index).isUnrestricted();
548+
}
549+
541550
private static InternalAggregation aggregateBuckets(InternalAggregation aggregation) {
542551
if (aggregation instanceof StringTerms) {
543552
StringTerms stringTerms = (StringTerms) aggregation;

0 commit comments

Comments
 (0)