Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor Watcher webhook execution into WebhookService #92576

Merged
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 @@ -131,6 +131,7 @@
import org.elasticsearch.xpack.watcher.input.transform.TransformInput;
import org.elasticsearch.xpack.watcher.input.transform.TransformInputFactory;
import org.elasticsearch.xpack.watcher.notification.NotificationService;
import org.elasticsearch.xpack.watcher.notification.WebhookService;
import org.elasticsearch.xpack.watcher.notification.email.Account;
import org.elasticsearch.xpack.watcher.notification.email.EmailService;
import org.elasticsearch.xpack.watcher.notification.email.HtmlSanitizer;
Expand Down Expand Up @@ -341,11 +342,13 @@ public Collection<Object> createComponents(
JiraService jiraService = new JiraService(settings, httpClient, clusterService.getClusterSettings());
SlackService slackService = new SlackService(settings, httpClient, clusterService.getClusterSettings());
PagerDutyService pagerDutyService = new PagerDutyService(settings, httpClient, clusterService.getClusterSettings());
WebhookService webhookService = new WebhookService(settings, httpClient, clusterService.getClusterSettings());

reloadableServices.add(emailService);
reloadableServices.add(jiraService);
reloadableServices.add(slackService);
reloadableServices.add(pagerDutyService);
reloadableServices.add(webhookService);

TextTemplateEngine templateEngine = new TextTemplateEngine(scriptService);
Map<String, EmailAttachmentParser<?>> emailAttachmentParsers = new HashMap<>();
Expand Down Expand Up @@ -386,7 +389,7 @@ public Collection<Object> createComponents(
// actions
final Map<String, ActionFactory> actionFactoryMap = new HashMap<>();
actionFactoryMap.put(EmailAction.TYPE, new EmailActionFactory(settings, emailService, templateEngine, emailAttachmentsParser));
actionFactoryMap.put(WebhookAction.TYPE, new WebhookActionFactory(httpClient, templateEngine));
actionFactoryMap.put(WebhookAction.TYPE, new WebhookActionFactory(webhookService, templateEngine));
actionFactoryMap.put(IndexAction.TYPE, new IndexActionFactory(settings, client));
actionFactoryMap.put(LoggingAction.TYPE, new LoggingActionFactory(templateEngine));
actionFactoryMap.put(JiraAction.TYPE, new JiraActionFactory(templateEngine, jiraService));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,41 +11,22 @@
import org.elasticsearch.xpack.core.watcher.actions.ExecutableAction;
import org.elasticsearch.xpack.core.watcher.execution.WatchExecutionContext;
import org.elasticsearch.xpack.core.watcher.watch.Payload;
import org.elasticsearch.xpack.watcher.common.http.HttpClient;
import org.elasticsearch.xpack.watcher.common.http.HttpRequest;
import org.elasticsearch.xpack.watcher.common.http.HttpResponse;
import org.elasticsearch.xpack.watcher.common.text.TextTemplateEngine;
import org.elasticsearch.xpack.watcher.support.Variables;

import java.util.Map;
import org.elasticsearch.xpack.watcher.notification.WebhookService;

public class ExecutableWebhookAction extends ExecutableAction<WebhookAction> {

private final HttpClient httpClient;
private final WebhookService webhookService;
private final TextTemplateEngine templateEngine;

public ExecutableWebhookAction(WebhookAction action, Logger logger, HttpClient httpClient, TextTemplateEngine templateEngine) {
public ExecutableWebhookAction(WebhookAction action, Logger logger, WebhookService webhookService, TextTemplateEngine templateEngine) {
super(action, logger);
this.httpClient = httpClient;
this.webhookService = webhookService;
this.templateEngine = templateEngine;
}

@Override
public Action.Result execute(String actionId, WatchExecutionContext ctx, Payload payload) throws Exception {
Map<String, Object> model = Variables.createCtxParamsMap(ctx, payload);

HttpRequest request = action.requestTemplate.render(templateEngine, model);

if (ctx.simulateAction(actionId)) {
return new WebhookAction.Result.Simulated(request);
}

HttpResponse response = httpClient.execute(request);

if (response.status() >= 400) {
return new WebhookAction.Result.Failure(request, response);
} else {
return new WebhookAction.Result.Success(request, response);
}
return webhookService.execute(actionId, action, templateEngine, ctx, payload);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,25 @@
import org.apache.logging.log4j.LogManager;
import org.elasticsearch.xcontent.XContentParser;
import org.elasticsearch.xpack.core.watcher.actions.ActionFactory;
import org.elasticsearch.xpack.watcher.common.http.HttpClient;
import org.elasticsearch.xpack.watcher.common.text.TextTemplateEngine;
import org.elasticsearch.xpack.watcher.notification.WebhookService;

import java.io.IOException;

public class WebhookActionFactory extends ActionFactory {

private final HttpClient httpClient;
private final TextTemplateEngine templateEngine;
private final WebhookService webhookService;

public WebhookActionFactory(HttpClient httpClient, TextTemplateEngine templateEngine) {
public WebhookActionFactory(WebhookService webhookService, TextTemplateEngine templateEngine) {
super(LogManager.getLogger(ExecutableWebhookAction.class));
this.httpClient = httpClient;
this.templateEngine = templateEngine;
this.webhookService = webhookService;
}

@Override
public ExecutableWebhookAction parseExecutable(String watchId, String actionId, XContentParser parser) throws IOException {
return new ExecutableWebhookAction(WebhookAction.parse(watchId, actionId, parser), actionLogger, httpClient, templateEngine);
return new ExecutableWebhookAction(WebhookAction.parse(watchId, actionId, parser), actionLogger, webhookService, templateEngine);

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

package org.elasticsearch.xpack.watcher.notification;

import org.elasticsearch.common.settings.ClusterSettings;
import org.elasticsearch.common.settings.Setting;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.xpack.core.watcher.actions.Action;
import org.elasticsearch.xpack.core.watcher.execution.WatchExecutionContext;
import org.elasticsearch.xpack.core.watcher.watch.Payload;
import org.elasticsearch.xpack.watcher.actions.webhook.WebhookAction;
import org.elasticsearch.xpack.watcher.common.http.HttpClient;
import org.elasticsearch.xpack.watcher.common.http.HttpRequest;
import org.elasticsearch.xpack.watcher.common.http.HttpResponse;
import org.elasticsearch.xpack.watcher.common.text.TextTemplateEngine;
import org.elasticsearch.xpack.watcher.support.Variables;

import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
* The WebhookService class handles executing webhook requests for Watcher actions. These can be
* regular "webhook" actions as well as parts of an "email" action with attachments that make HTTP
* requests.
*/
public class WebhookService extends NotificationService<WebhookService.WebhookAccount> {

private final HttpClient httpClient;

public WebhookService(Settings settings, HttpClient httpClient, ClusterSettings clusterSettings) {
super("webhook", settings, clusterSettings, List.of(), getSecureSettings());
this.httpClient = httpClient;
// do an initial load
reload(settings);
}

private static List<Setting<?>> getSecureSettings() {
return List.of();
}

@Override
protected WebhookAccount createAccount(String name, Settings accountSettings) {
return new WebhookAccount();
}

public Action.Result execute(
String actionId,
WebhookAction action,
TextTemplateEngine templateEngine,
WatchExecutionContext ctx,
Payload payload
) throws IOException {
Map<String, Object> model = Variables.createCtxParamsMap(ctx, payload);

HttpRequest request = action.getRequest().render(templateEngine, model);

if (ctx.simulateAction(actionId)) {
return new WebhookAction.Result.Simulated(request);
}

HttpResponse response = httpClient.execute(request);

if (response.status() >= 400) {
return new WebhookAction.Result.Failure(request, response);
} else {
return new WebhookAction.Result.Success(request, response);
}
}

public static final class WebhookAccount {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import org.elasticsearch.ElasticsearchParseException;
import org.elasticsearch.client.internal.Client;
import org.elasticsearch.common.settings.ClusterSettings;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.env.Environment;
import org.elasticsearch.env.TestEnvironment;
Expand All @@ -33,6 +34,7 @@
import org.elasticsearch.xpack.watcher.common.text.TextTemplate;
import org.elasticsearch.xpack.watcher.common.text.TextTemplateEngine;
import org.elasticsearch.xpack.watcher.execution.TriggeredExecutionContext;
import org.elasticsearch.xpack.watcher.notification.WebhookService;
import org.elasticsearch.xpack.watcher.notification.email.Attachment;
import org.elasticsearch.xpack.watcher.support.search.WatcherSearchTemplateService;
import org.elasticsearch.xpack.watcher.test.MockTextTemplateEngine;
Expand Down Expand Up @@ -92,7 +94,12 @@ public void testExecute() throws Exception {
HttpRequestTemplate httpRequest = getHttpRequestTemplate(method, TEST_HOST, TEST_PORT, testPath, testBody, null);

WebhookAction action = new WebhookAction(httpRequest);
ExecutableWebhookAction executable = new ExecutableWebhookAction(action, logger, httpClient, templateEngine);
WebhookService webhookService = new WebhookService(
Settings.EMPTY,
httpClient,
new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS)
);
ExecutableWebhookAction executable = new ExecutableWebhookAction(action, logger, webhookService, templateEngine);
WatchExecutionContext ctx = WatcherTestUtils.mockExecutionContext("_id", new Payload.Simple("foo", "bar"));

Action.Result actionResult = executable.execute("_id", ctx, Payload.EMPTY);
Expand Down Expand Up @@ -155,7 +162,12 @@ public void testParserSelfGenerated() throws Exception {

HttpRequestTemplate request = getHttpRequestTemplate(method, host, TEST_PORT, path, body, null);
WebhookAction action = new WebhookAction(request);
ExecutableWebhookAction executable = new ExecutableWebhookAction(action, logger, ExecuteScenario.Success.client(), templateEngine);
WebhookService webhookService = new WebhookService(
Settings.EMPTY,
ExecuteScenario.Success.client(),
new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS)
);
ExecutableWebhookAction executable = new ExecutableWebhookAction(action, logger, webhookService, templateEngine);

XContentBuilder builder = jsonBuilder();
executable.toXContent(builder, ToXContent.EMPTY_PARAMS);
Expand Down Expand Up @@ -217,7 +229,12 @@ public void testParserFailure() throws Exception {
}

private WebhookActionFactory webhookFactory(HttpClient client) {
return new WebhookActionFactory(client, templateEngine);
WebhookService webhookService = new WebhookService(
Settings.EMPTY,
client,
new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS)
);
return new WebhookActionFactory(webhookService, templateEngine);
}

public void testThatSelectingProxyWorks() throws Exception {
Expand All @@ -235,7 +252,12 @@ public void testThatSelectingProxyWorks() throws Exception {
.proxy(new HttpProxy("localhost", proxyServer.getPort()));
WebhookAction action = new WebhookAction(builder.build());

ExecutableWebhookAction executable = new ExecutableWebhookAction(action, logger, httpClient, templateEngine);
WebhookService webhookService = new WebhookService(
Settings.EMPTY,
httpClient,
new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS)
);
ExecutableWebhookAction executable = new ExecutableWebhookAction(action, logger, webhookService, templateEngine);
String watchId = "test_url_encode" + randomAlphaOfLength(10);
ScheduleTriggerEvent triggerEvent = new ScheduleTriggerEvent(
watchId,
Expand Down Expand Up @@ -268,7 +290,12 @@ public void testValidUrls() throws Exception {
HttpRequestTemplate requestTemplate = getHttpRequestTemplate(method, host, TEST_PORT, path, testBody, null);
WebhookAction action = new WebhookAction(requestTemplate);

ExecutableWebhookAction executable = new ExecutableWebhookAction(action, logger, client, templateEngine);
WebhookService webhookService = new WebhookService(
Settings.EMPTY,
client,
new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS)
);
ExecutableWebhookAction executable = new ExecutableWebhookAction(action, logger, webhookService, templateEngine);

ScheduleTriggerEvent triggerEvent = new ScheduleTriggerEvent(
watchId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.elasticsearch.action.search.SearchType;
import org.elasticsearch.client.internal.Client;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.settings.ClusterSettings;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentHelper;
import org.elasticsearch.core.TimeValue;
Expand Down Expand Up @@ -43,6 +44,7 @@
import org.elasticsearch.xpack.watcher.execution.TriggeredExecutionContext;
import org.elasticsearch.xpack.watcher.input.simple.ExecutableSimpleInput;
import org.elasticsearch.xpack.watcher.input.simple.SimpleInput;
import org.elasticsearch.xpack.watcher.notification.WebhookService;
import org.elasticsearch.xpack.watcher.notification.email.Authentication;
import org.elasticsearch.xpack.watcher.notification.email.EmailService;
import org.elasticsearch.xpack.watcher.notification.email.EmailTemplate;
Expand Down Expand Up @@ -179,13 +181,19 @@ public static Watch createTestWatch(
httpRequest.method(HttpMethod.POST);
httpRequest.path(new TextTemplate("/foobarbaz/{{ctx.watch_id}}"));
httpRequest.body(new TextTemplate("{{ctx.watch_id}} executed with {{ctx.payload.response.hits.total_hits}} hits"));

WebhookService webhookService = new WebhookService(
Settings.EMPTY,
httpClient,
new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS)
);
actions.add(
new ActionWrapper(
"_webhook",
actionThrottler,
null,
null,
new ExecutableWebhookAction(new WebhookAction(httpRequest.build()), logger, httpClient, engine),
new ExecutableWebhookAction(new WebhookAction(httpRequest.build()), logger, webhookService, engine),
null,
null
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
import org.elasticsearch.xpack.watcher.input.simple.ExecutableSimpleInput;
import org.elasticsearch.xpack.watcher.input.simple.SimpleInput;
import org.elasticsearch.xpack.watcher.input.simple.SimpleInputFactory;
import org.elasticsearch.xpack.watcher.notification.WebhookService;
import org.elasticsearch.xpack.watcher.notification.email.DataAttachment;
import org.elasticsearch.xpack.watcher.notification.email.EmailService;
import org.elasticsearch.xpack.watcher.notification.email.EmailTemplate;
Expand Down Expand Up @@ -153,6 +154,7 @@ public class WatchTests extends ESTestCase {
private Client client;
private HttpClient httpClient;
private EmailService emailService;
private WebhookService webhookService;
private TextTemplateEngine templateEngine;
private HtmlSanitizer htmlSanitizer;
private XPackLicenseState licenseState;
Expand All @@ -166,6 +168,7 @@ public void init() throws Exception {
client = mock(Client.class);
httpClient = mock(HttpClient.class);
emailService = mock(EmailService.class);
webhookService = mock(WebhookService.class);
templateEngine = mock(TextTemplateEngine.class);
htmlSanitizer = mock(HtmlSanitizer.class);
licenseState = mock(XPackLicenseState.class);
Expand Down Expand Up @@ -658,7 +661,7 @@ private List<ActionWrapper> randomActions() {
randomThrottler(),
AlwaysConditionTests.randomCondition(scriptService),
randomTransform(),
new ExecutableWebhookAction(action, logger, httpClient, templateEngine),
new ExecutableWebhookAction(action, logger, webhookService, templateEngine),
null,
null
)
Expand All @@ -676,7 +679,7 @@ private ActionRegistry registry(List<ActionWrapper> actions, ConditionRegistry c
new EmailActionFactory(settings, emailService, templateEngine, new EmailAttachmentsParser(Collections.emptyMap()))
);
case IndexAction.TYPE -> parsers.put(IndexAction.TYPE, new IndexActionFactory(settings, client));
case WebhookAction.TYPE -> parsers.put(WebhookAction.TYPE, new WebhookActionFactory(httpClient, templateEngine));
case WebhookAction.TYPE -> parsers.put(WebhookAction.TYPE, new WebhookActionFactory(webhookService, templateEngine));
case LoggingAction.TYPE -> parsers.put(LoggingAction.TYPE, new LoggingActionFactory(new MockTextTemplateEngine()));
}
}
Expand Down