Skip to content

Commit

Permalink
Fix: Watcher API watcher_get_settings clears product header (elastic#…
Browse files Browse the repository at this point in the history
…103003) (elastic#103256)

Current code do stashing and un-stashing of the context to ignore warning headers about accessing a system index. However, this ignores all header, omitting the X-elastic-product header from the response.

This PR removes the stashing/un-stashing and instead calls the concreteIndexNames variant that allows system index access (which does not generate the warning), concreteIndexNamesWithSystemIndexAccess
  • Loading branch information
ldematte authored Dec 11, 2023
1 parent 1059567 commit 90d9e41
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 34 deletions.
6 changes: 6 additions & 0 deletions docs/changelog/103003.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
pr: 103003
summary: "Fix: Watcher REST API `GET /_watcher/settings` now includes product header"
area: "Watcher"
type: bug
issues:
- 102928
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.support.ActionFilters;
import org.elasticsearch.action.support.IndicesOptions;
import org.elasticsearch.action.support.master.TransportMasterNodeAction;
import org.elasticsearch.cluster.ClusterState;
import org.elasticsearch.cluster.block.ClusterBlockException;
Expand All @@ -20,14 +19,14 @@
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.util.concurrent.EsExecutors;
import org.elasticsearch.common.util.concurrent.ThreadContext;
import org.elasticsearch.tasks.Task;
import org.elasticsearch.threadpool.ThreadPool;
import org.elasticsearch.transport.TransportService;
import org.elasticsearch.xpack.core.watcher.transport.actions.put.GetWatcherSettingsAction;
import org.elasticsearch.xpack.core.watcher.transport.actions.put.UpdateWatcherSettingsAction;

import static org.elasticsearch.xpack.watcher.transport.actions.TransportUpdateWatcherSettingsAction.WATCHER_INDEX_NAME;
import static org.elasticsearch.xpack.watcher.transport.actions.TransportUpdateWatcherSettingsAction.WATCHER_INDEX_REQUEST;

public class TransportGetWatcherSettingsAction extends TransportMasterNodeAction<
GetWatcherSettingsAction.Request,
Expand Down Expand Up @@ -61,15 +60,11 @@ protected void masterOperation(
ClusterState state,
ActionListener<GetWatcherSettingsAction.Response> listener
) {
final ThreadContext threadContext = threadPool.getThreadContext();
// Stashing and un-stashing the context allows warning headers about accessing a system index to be ignored.
try (ThreadContext.StoredContext ignore = threadContext.stashContext()) {
IndexMetadata metadata = state.metadata().index(WATCHER_INDEX_NAME);
if (metadata == null) {
listener.onResponse(new GetWatcherSettingsAction.Response(Settings.EMPTY));
} else {
listener.onResponse(new GetWatcherSettingsAction.Response(filterSettableSettings(metadata.getSettings())));
}
IndexMetadata metadata = state.metadata().index(WATCHER_INDEX_NAME);
if (metadata == null) {
listener.onResponse(new GetWatcherSettingsAction.Response(Settings.EMPTY));
} else {
listener.onResponse(new GetWatcherSettingsAction.Response(filterSettableSettings(metadata.getSettings())));
}
}

Expand All @@ -95,7 +90,7 @@ protected ClusterBlockException checkBlock(GetWatcherSettingsAction.Request requ
return state.blocks()
.indicesBlockedException(
ClusterBlockLevel.METADATA_READ,
indexNameExpressionResolver.concreteIndexNames(state, IndicesOptions.LENIENT_EXPAND_OPEN, WATCHER_INDEX_NAME)
indexNameExpressionResolver.concreteIndexNamesWithSystemIndexAccess(state, WATCHER_INDEX_REQUEST)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import org.elasticsearch.ResourceNotFoundException;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.IndicesRequest;
import org.elasticsearch.action.admin.indices.settings.put.UpdateSettingsClusterStateUpdateRequest;
import org.elasticsearch.action.support.ActionFilters;
import org.elasticsearch.action.support.IndicesOptions;
Expand All @@ -24,7 +25,6 @@
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.util.concurrent.EsExecutors;
import org.elasticsearch.common.util.concurrent.ThreadContext;
import org.elasticsearch.index.Index;
import org.elasticsearch.logging.LogManager;
import org.elasticsearch.logging.Logger;
Expand All @@ -37,7 +37,19 @@ public class TransportUpdateWatcherSettingsAction extends TransportMasterNodeAct
UpdateWatcherSettingsAction.Request,
AcknowledgedResponse> {

public static final String WATCHER_INDEX_NAME = ".watches";
static final String WATCHER_INDEX_NAME = ".watches";

static final IndicesRequest WATCHER_INDEX_REQUEST = new IndicesRequest() {
@Override
public String[] indices() {
return new String[] { WATCHER_INDEX_NAME };
}

@Override
public IndicesOptions indicesOptions() {
return IndicesOptions.LENIENT_EXPAND_OPEN;
}
};

private static final Logger logger = LogManager.getLogger(TransportUpdateWatcherSettingsAction.class);
private final MetadataUpdateSettingsService updateSettingsService;
Expand Down Expand Up @@ -83,27 +95,23 @@ protected void masterOperation(
new Index[] { watcherIndexMd.getIndex() }
).settings(newSettings).ackTimeout(request.timeout()).masterNodeTimeout(request.masterNodeTimeout());

final ThreadContext threadContext = threadPool.getThreadContext();
// Stashing and un-stashing the context allows warning headers about accessing a system index to be ignored.
try (ThreadContext.StoredContext ignore = threadContext.stashContext()) {
updateSettingsService.updateSettings(clusterStateUpdateRequest, new ActionListener<>() {
@Override
public void onResponse(AcknowledgedResponse acknowledgedResponse) {
if (acknowledgedResponse.isAcknowledged()) {
logger.info("successfully updated Watcher service settings to {}", request.settings());
} else {
logger.warn("updating Watcher service settings to {} was not acknowledged", request.settings());
}
listener.onResponse(acknowledgedResponse);
updateSettingsService.updateSettings(clusterStateUpdateRequest, new ActionListener<>() {
@Override
public void onResponse(AcknowledgedResponse acknowledgedResponse) {
if (acknowledgedResponse.isAcknowledged()) {
logger.info("successfully updated Watcher service settings to {}", request.settings());
} else {
logger.warn("updating Watcher service settings to {} was not acknowledged", request.settings());
}
listener.onResponse(acknowledgedResponse);
}

@Override
public void onFailure(Exception e) {
logger.debug(() -> "failed to update settings for Watcher service", e);
listener.onFailure(e);
}
});
}
@Override
public void onFailure(Exception e) {
logger.debug(() -> "failed to update settings for Watcher service", e);
listener.onFailure(e);
}
});
}

@Override
Expand All @@ -115,7 +123,7 @@ protected ClusterBlockException checkBlock(UpdateWatcherSettingsAction.Request r
return state.blocks()
.indicesBlockedException(
ClusterBlockLevel.METADATA_WRITE,
indexNameExpressionResolver.concreteIndexNames(state, IndicesOptions.LENIENT_EXPAND_OPEN, WATCHER_INDEX_NAME)
indexNameExpressionResolver.concreteIndexNamesWithSystemIndexAccess(state, WATCHER_INDEX_REQUEST)
);
}
}

0 comments on commit 90d9e41

Please sign in to comment.