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

fix null user in detector #301

Merged
merged 1 commit into from
Oct 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We didn’t put new user from thread context to detector, so the save will store null value

);
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