Skip to content

Commit 4f3906c

Browse files
authored
[Performance] Call AdminDns.isAdmin once per request (#5752)
Signed-off-by: Craig Perkins <[email protected]>
1 parent b07702d commit 4f3906c

File tree

5 files changed

+18
-8
lines changed

5 files changed

+18
-8
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
2121
- Use RestRequestFilter.getFilteredRequest to declare sensitive API params ([#5710](https://github.com/opensearch-project/security/pull/5710))
2222
- Fix deprecated SSL transport settings in demo certificates ([#5723](https://github.com/opensearch-project/security/pull/5723))
2323
- Updates DlsFlsValveImpl condition to return true if request is internal and not a protected resource request ([#5721](https://github.com/opensearch-project/security/pull/5721))
24+
- [Performance] Call AdminDns.isAdmin once per request ([#5752](https://github.com/opensearch-project/security/pull/5752))
2425

2526
### Refactoring
2627
- [Resource Sharing] Make migrate api require default access level to be supplied and updates documentations + tests ([#5717](https://github.com/opensearch-project/security/pull/5717))

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@
165165
import org.opensearch.security.http.XFFResolver;
166166
import org.opensearch.security.identity.SecurePluginSubject;
167167
import org.opensearch.security.identity.SecurityTokenManager;
168+
import org.opensearch.security.privileges.PrivilegesEvaluationContext;
168169
import org.opensearch.security.privileges.PrivilegesEvaluationException;
169170
import org.opensearch.security.privileges.PrivilegesEvaluator;
170171
import org.opensearch.security.privileges.PrivilegesInterceptor;
@@ -2329,9 +2330,13 @@ public Function<String, Predicate<String>> getFieldFilter() {
23292330
return field -> true;
23302331
}
23312332

2333+
PrivilegesEvaluationContext ctx = this.dlsFlsBaseContext != null
2334+
? this.dlsFlsBaseContext.getPrivilegesEvaluationContext()
2335+
: null;
2336+
23322337
return field -> {
23332338
try {
2334-
return dlsFlsValve.isFieldAllowed(index, field);
2339+
return dlsFlsValve.isFieldAllowed(index, field, ctx);
23352340
} catch (PrivilegesEvaluationException e) {
23362341
log.error("Error while evaluating FLS for {}.{}", index, field, e);
23372342
return false;

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public interface DlsFlsRequestValve {
4949

5050
boolean hasFieldMasking(String index) throws PrivilegesEvaluationException;
5151

52-
boolean isFieldAllowed(String index, String field) throws PrivilegesEvaluationException;
52+
boolean isFieldAllowed(String index, String field, PrivilegesEvaluationContext ctx) throws PrivilegesEvaluationException;
5353

5454
public static class NoopDlsFlsRequestValve implements DlsFlsRequestValve {
5555

@@ -84,7 +84,7 @@ public boolean hasFieldMasking(String index) {
8484
}
8585

8686
@Override
87-
public boolean isFieldAllowed(String index, String field) {
87+
public boolean isFieldAllowed(String index, String field, PrivilegesEvaluationContext ctx) {
8888
return true;
8989
}
9090
}

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -529,14 +529,13 @@ public boolean hasFieldMasking(String index) throws PrivilegesEvaluationExceptio
529529
}
530530

531531
@Override
532-
public boolean isFieldAllowed(String index, String field) throws PrivilegesEvaluationException {
533-
PrivilegesEvaluationContext privilegesEvaluationContext = this.dlsFlsBaseContext.getPrivilegesEvaluationContext();
534-
if (privilegesEvaluationContext == null) {
532+
public boolean isFieldAllowed(String index, String field, PrivilegesEvaluationContext ctx) throws PrivilegesEvaluationException {
533+
if (ctx == null) {
535534
return true;
536535
}
537536

538537
DlsFlsProcessedConfig config = this.dlsFlsProcessedConfig.get();
539-
return config.getFieldPrivileges().getRestriction(privilegesEvaluationContext, index).isAllowedRecursive(field);
538+
return config.getFieldPrivileges().getRestriction(ctx, index).isAllowedRecursive(field);
540539
}
541540

542541
private static InternalAggregation aggregateBuckets(InternalAggregation aggregation) {

src/main/java/org/opensearch/security/privileges/dlsfls/DlsFlsBaseContext.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,18 @@ public DlsFlsBaseContext(PrivilegesEvaluator privilegesEvaluator, ThreadContext
3737
* associated with a user. This indicates a system action. In these cases, no privilege evaluation should be performed.
3838
*/
3939
public PrivilegesEvaluationContext getPrivilegesEvaluationContext() {
40+
if (threadContext.getTransient("tmp_dls_fls_ctx") != null) {
41+
return threadContext.getTransient("tmp_dls_fls_ctx");
42+
}
4043
User user = threadContext.getTransient(ConfigConstants.OPENDISTRO_SECURITY_USER);
4144

4245
if (HeaderHelper.isInternalOrPluginRequest(threadContext) || adminDNs.isAdmin(user)) {
4346
return null;
4447
}
4548

46-
return this.privilegesEvaluator.createContext(user, null);
49+
PrivilegesEvaluationContext ctx = this.privilegesEvaluator.createContext(user, null);
50+
threadContext.putTransient("tmp_dls_fls_ctx", ctx);
51+
return ctx;
4752
}
4853

4954
public boolean isDlsDoneOnFilterLevel() {

0 commit comments

Comments
 (0)