diff --git a/src/main/java/com/amazon/opendistroforelasticsearch/ad/rest/handler/IndexAnomalyDetectorActionHandler.java b/src/main/java/com/amazon/opendistroforelasticsearch/ad/rest/handler/IndexAnomalyDetectorActionHandler.java index bb264f44..a5c57a08 100644 --- a/src/main/java/com/amazon/opendistroforelasticsearch/ad/rest/handler/IndexAnomalyDetectorActionHandler.java +++ b/src/main/java/com/amazon/opendistroforelasticsearch/ad/rest/handler/IndexAnomalyDetectorActionHandler.java @@ -448,7 +448,7 @@ private void indexAnomalyDetector(String detectorId) throws IOException { anomalyDetector.getSchemaVersion(), Instant.now(), anomalyDetector.getCategoryField(), - anomalyDetector.getUser() + user ); IndexRequest indexRequest = new IndexRequest(ANOMALY_DETECTORS_INDEX) .setRefreshPolicy(refreshPolicy) diff --git a/src/main/java/com/amazon/opendistroforelasticsearch/ad/transport/IndexAnomalyDetectorTransportAction.java b/src/main/java/com/amazon/opendistroforelasticsearch/ad/transport/IndexAnomalyDetectorTransportAction.java index 919476a5..c44ea317 100644 --- a/src/main/java/com/amazon/opendistroforelasticsearch/ad/transport/IndexAnomalyDetectorTransportAction.java +++ b/src/main/java/com/amazon/opendistroforelasticsearch/ad/transport/IndexAnomalyDetectorTransportAction.java @@ -52,7 +52,6 @@ public class IndexAnomalyDetectorTransportAction extends HandledTransportAction< private final AnomalyDetectionIndices anomalyDetectionIndices; private final ClusterService clusterService; private final NamedXContentRegistry xContentRegistry; - private User user; @Inject public IndexAnomalyDetectorTransportAction( @@ -69,12 +68,11 @@ public IndexAnomalyDetectorTransportAction( this.clusterService = clusterService; this.anomalyDetectionIndices = anomalyDetectionIndices; this.xContentRegistry = xContentRegistry; - this.user = null; - } @Override protected void doExecute(Task task, IndexAnomalyDetectorRequest request, ActionListener listener) { + User user = getUserContext(client); anomalyDetectionIndices.updateMappingIfNecessary(); String detectorId = request.getDetectorID(); long seqNo = request.getSeqNo(); @@ -88,7 +86,6 @@ protected void doExecute(Task task, IndexAnomalyDetectorRequest request, ActionL Integer maxAnomalyFeatures = request.getMaxAnomalyFeatures(); checkIndicesAndExecute(detector.getIndices(), () -> { - user = getUserContext(client); try (ThreadContext.StoredContext context = client.threadPool().getThreadContext().stashContext()) { IndexAnomalyDetectorActionHandler indexAnomalyDetectorActionHandler = new IndexAnomalyDetectorActionHandler( clusterService, diff --git a/src/main/java/com/amazon/opendistroforelasticsearch/ad/transport/SearchAnomalyDetectorTransportAction.java b/src/main/java/com/amazon/opendistroforelasticsearch/ad/transport/SearchAnomalyDetectorTransportAction.java index 0be250e7..8c32e776 100644 --- a/src/main/java/com/amazon/opendistroforelasticsearch/ad/transport/SearchAnomalyDetectorTransportAction.java +++ b/src/main/java/com/amazon/opendistroforelasticsearch/ad/transport/SearchAnomalyDetectorTransportAction.java @@ -42,7 +42,6 @@ public class SearchAnomalyDetectorTransportAction extends HandledTransportAction private final Client client; private volatile Boolean filterEnabled; - private User user; @Inject public SearchAnomalyDetectorTransportAction( @@ -56,21 +55,20 @@ public SearchAnomalyDetectorTransportAction( this.client = client; filterEnabled = AnomalyDetectorSettings.FILTER_BY_BACKEND_ROLES.get(settings); clusterService.getClusterSettings().addSettingsUpdateConsumer(FILTER_BY_BACKEND_ROLES, it -> filterEnabled = it); - user = null; } @Override protected void doExecute(Task task, SearchRequest request, ActionListener listener) { - user = getUserContext(client); + User user = getUserContext(client); try (ThreadContext.StoredContext context = client.threadPool().getThreadContext().stashContext()) { - validateRole(request, listener); + validateRole(request, user, listener); } catch (Exception e) { logger.error(e); listener.onFailure(e); } } - private void validateRole(SearchRequest request, ActionListener listener) { + private void validateRole(SearchRequest request, User user, ActionListener listener) { if (user == null) { // Auth Header is empty when 1. Security is disabled. 2. When user is super-admin // Proceed with search diff --git a/src/main/java/com/amazon/opendistroforelasticsearch/ad/transport/SearchAnomalyResultTransportAction.java b/src/main/java/com/amazon/opendistroforelasticsearch/ad/transport/SearchAnomalyResultTransportAction.java index 78e1eecf..5ac713b6 100644 --- a/src/main/java/com/amazon/opendistroforelasticsearch/ad/transport/SearchAnomalyResultTransportAction.java +++ b/src/main/java/com/amazon/opendistroforelasticsearch/ad/transport/SearchAnomalyResultTransportAction.java @@ -42,7 +42,6 @@ public class SearchAnomalyResultTransportAction extends HandledTransportAction filterEnabled = it); - user = null; } @Override protected void doExecute(Task task, SearchRequest request, ActionListener listener) { - user = getUserContext(client); + User user = getUserContext(client); try (ThreadContext.StoredContext context = client.threadPool().getThreadContext().stashContext()) { - validateRole(request, listener); + validateRole(request, user, listener); } catch (Exception e) { logger.error(e); listener.onFailure(e); } } - private void validateRole(SearchRequest request, ActionListener listener) { + private void validateRole(SearchRequest request, User user, ActionListener listener) { if (user == null) { // Auth Header is empty when 1. Security is disabled. 2. When user is super-admin // Proceed with search