Skip to content
This repository has been archived by the owner on Aug 2, 2022. It is now read-only.

Commit

Permalink
fix null user in detector (#301)
Browse files Browse the repository at this point in the history
  • Loading branch information
ylwu-amzn authored Oct 30, 2020
1 parent 9900493 commit 746360b
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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<IndexAnomalyDetectorResponse> listener) {
User user = getUserContext(client);
anomalyDetectionIndices.updateMappingIfNecessary();
String detectorId = request.getDetectorID();
long seqNo = request.getSeqNo();
Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ public class SearchAnomalyDetectorTransportAction extends HandledTransportAction

private final Client client;
private volatile Boolean filterEnabled;
private User user;

@Inject
public SearchAnomalyDetectorTransportAction(
Expand All @@ -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<SearchResponse> 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<SearchResponse> listener) {
private void validateRole(SearchRequest request, User user, ActionListener<SearchResponse> listener) {
if (user == null) {
// Auth Header is empty when 1. Security is disabled. 2. When user is super-admin
// Proceed with search
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ public class SearchAnomalyResultTransportAction extends HandledTransportAction<S

private final Client client;
private volatile Boolean filterEnabled;
private User user;

@Inject
public SearchAnomalyResultTransportAction(
Expand All @@ -56,21 +55,20 @@ public SearchAnomalyResultTransportAction(
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<SearchResponse> 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<SearchResponse> listener) {
private void validateRole(SearchRequest request, User user, ActionListener<SearchResponse> listener) {
if (user == null) {
// Auth Header is empty when 1. Security is disabled. 2. When user is super-admin
// Proceed with search
Expand Down

0 comments on commit 746360b

Please sign in to comment.