Skip to content

Commit 6e1b187

Browse files
committed
Watcher: Make settings reloadable (#31746)
This commit allows for rebuilding watcher secure secrets via the reload_secure_settings API call. The commit also renames a method in the Notification Service to make it a bit more readable.
1 parent 92d7430 commit 6e1b187

File tree

9 files changed

+68
-11
lines changed

9 files changed

+68
-11
lines changed

x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/Watcher.java

+21-1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import org.elasticsearch.node.Node;
3939
import org.elasticsearch.plugins.ActionPlugin;
4040
import org.elasticsearch.plugins.Plugin;
41+
import org.elasticsearch.plugins.ReloadablePlugin;
4142
import org.elasticsearch.plugins.ScriptPlugin;
4243
import org.elasticsearch.rest.RestController;
4344
import org.elasticsearch.rest.RestHandler;
@@ -123,6 +124,7 @@
123124
import org.elasticsearch.xpack.watcher.input.simple.SimpleInputFactory;
124125
import org.elasticsearch.xpack.watcher.input.transform.TransformInput;
125126
import org.elasticsearch.xpack.watcher.input.transform.TransformInputFactory;
127+
import org.elasticsearch.xpack.watcher.notification.NotificationService;
126128
import org.elasticsearch.xpack.watcher.notification.email.Account;
127129
import org.elasticsearch.xpack.watcher.notification.email.EmailService;
128130
import org.elasticsearch.xpack.watcher.notification.email.HtmlSanitizer;
@@ -197,7 +199,7 @@
197199

198200
import static java.util.Collections.emptyList;
199201

200-
public class Watcher extends Plugin implements ActionPlugin, ScriptPlugin {
202+
public class Watcher extends Plugin implements ActionPlugin, ScriptPlugin, ReloadablePlugin {
201203

202204
// This setting is only here for backward compatibility reasons as 6.x indices made use of it. It can be removed in 8.x.
203205
@Deprecated
@@ -240,6 +242,7 @@ public class Watcher extends Plugin implements ActionPlugin, ScriptPlugin {
240242
protected final boolean transportClient;
241243
protected final boolean enabled;
242244
protected final Environment env;
245+
protected List<NotificationService> reloadableServices = new ArrayList<>();
243246

244247
public Watcher(Settings settings) {
245248
this.settings = settings;
@@ -294,6 +297,12 @@ public Collection<Object> createComponents(Client client, ClusterService cluster
294297
SlackService slackService = new SlackService(settings, httpClient, clusterService.getClusterSettings());
295298
PagerDutyService pagerDutyService = new PagerDutyService(settings, httpClient, clusterService.getClusterSettings());
296299

300+
reloadableServices.add(emailService);
301+
reloadableServices.add(hipChatService);
302+
reloadableServices.add(jiraService);
303+
reloadableServices.add(slackService);
304+
reloadableServices.add(pagerDutyService);
305+
297306
TextTemplateEngine templateEngine = new TextTemplateEngine(settings, scriptService);
298307
Map<String, EmailAttachmentParser> emailAttachmentParsers = new HashMap<>();
299308
emailAttachmentParsers.put(HttpEmailAttachementParser.TYPE, new HttpEmailAttachementParser(httpClient, httpTemplateParser,
@@ -635,4 +644,15 @@ public List<ScriptContext> getContexts() {
635644
public void close() throws IOException {
636645
IOUtils.closeWhileHandlingException(httpClient);
637646
}
647+
648+
/**
649+
* Reloads all the reloadable services in watcher.
650+
*/
651+
@Override
652+
public void reload(Settings settings) {
653+
if (enabled == false || transportClient) {
654+
return;
655+
}
656+
reloadableServices.forEach(s -> s.reload(settings));
657+
}
638658
}

x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/notification/NotificationService.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public abstract class NotificationService<Account> extends AbstractComponent {
3131
public NotificationService(Settings settings, String type,
3232
ClusterSettings clusterSettings, List<Setting<?>> pluginSettings) {
3333
this(settings, type);
34-
clusterSettings.addSettingsUpdateConsumer(this::setAccountSetting, pluginSettings);
34+
clusterSettings.addSettingsUpdateConsumer(this::reload, pluginSettings);
3535
}
3636

3737
// Used for testing only
@@ -40,7 +40,7 @@ public NotificationService(Settings settings, String type,
4040
this.type = type;
4141
}
4242

43-
protected synchronized void setAccountSetting(Settings settings) {
43+
public synchronized void reload(Settings settings) {
4444
Tuple<Map<String, Account>, Account> accounts = buildAccounts(settings, this::createAccount);
4545
this.accounts = Collections.unmodifiableMap(accounts.v1());
4646
this.defaultAccount = accounts.v2();

x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/notification/email/EmailService.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ public EmailService(Settings settings, @Nullable CryptoService cryptoService, Cl
127127
clusterSettings.addAffixUpdateConsumer(SETTING_SMTP_SEND_PARTIAL, (s, o) -> {}, (s, o) -> {});
128128
clusterSettings.addAffixUpdateConsumer(SETTING_SMTP_WAIT_ON_QUIT, (s, o) -> {}, (s, o) -> {});
129129
// do an initial load
130-
setAccountSetting(settings);
130+
reload(settings);
131131
}
132132

133133
@Override

x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/notification/hipchat/HipChatService.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,13 @@ public HipChatService(Settings settings, HttpClient httpClient, ClusterSettings
7878
clusterSettings.addAffixUpdateConsumer(SETTING_PORT, (s, o) -> {}, (s, o) -> {});
7979
clusterSettings.addAffixUpdateConsumer(SETTING_MESSAGE_DEFAULTS, (s, o) -> {}, (s, o) -> {});
8080

81-
setAccountSetting(settings);
81+
reload(settings);
8282
}
8383

8484
@Override
85-
protected synchronized void setAccountSetting(Settings settings) {
85+
public synchronized void reload(Settings settings) {
8686
defaultServer = new HipChatServer(settings.getByPrefix("xpack.notification.hipchat."));
87-
super.setAccountSetting(settings);
87+
super.reload(settings);
8888
}
8989

9090
@Override

x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/notification/jira/JiraService.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public JiraService(Settings settings, HttpClient httpClient, ClusterSettings clu
7575
clusterSettings.addAffixUpdateConsumer(SETTING_SECURE_PASSWORD, (s, o) -> {}, (s, o) -> {});
7676
clusterSettings.addAffixUpdateConsumer(SETTING_DEFAULTS, (s, o) -> {}, (s, o) -> {});
7777
// do an initial load
78-
setAccountSetting(settings);
78+
reload(settings);
7979
}
8080

8181
@Override

x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/notification/pagerduty/PagerDutyService.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public PagerDutyService(Settings settings, HttpClient httpClient, ClusterSetting
4646
clusterSettings.addAffixUpdateConsumer(SETTING_SERVICE_API_KEY, (s, o) -> {}, (s, o) -> {});
4747
clusterSettings.addAffixUpdateConsumer(SETTING_SECURE_SERVICE_API_KEY, (s, o) -> {}, (s, o) -> {});
4848
clusterSettings.addAffixUpdateConsumer(SETTING_DEFAULTS, (s, o) -> {}, (s, o) -> {});
49-
setAccountSetting(settings);
49+
reload(settings);
5050
}
5151

5252
@Override

x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/notification/slack/SlackService.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public SlackService(Settings settings, HttpClient httpClient, ClusterSettings cl
4646
clusterSettings.addAffixUpdateConsumer(SETTING_URL, (s, o) -> {}, (s, o) -> {});
4747
clusterSettings.addAffixUpdateConsumer(SETTING_URL_SECURE, (s, o) -> {}, (s, o) -> {});
4848
clusterSettings.addAffixUpdateConsumer(SETTING_DEFAULTS, (s, o) -> {}, (s, o) -> {});
49-
setAccountSetting(settings);
49+
reload(settings);
5050
}
5151

5252
@Override

x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/WatcherPluginTests.java

+37
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,18 @@
1515
import org.elasticsearch.test.IndexSettingsModule;
1616
import org.elasticsearch.threadpool.ExecutorBuilder;
1717
import org.elasticsearch.xpack.core.watcher.watch.Watch;
18+
import org.elasticsearch.xpack.watcher.notification.NotificationService;
1819

1920
import java.util.List;
2021

2122
import static java.util.Collections.emptyMap;
2223
import static org.hamcrest.Matchers.containsString;
2324
import static org.hamcrest.Matchers.hasSize;
2425
import static org.hamcrest.Matchers.is;
26+
import static org.mockito.Mockito.mock;
27+
import static org.mockito.Mockito.times;
28+
import static org.mockito.Mockito.verify;
29+
import static org.mockito.Mockito.verifyNoMoreInteractions;
2530

2631
public class WatcherPluginTests extends ESTestCase {
2732

@@ -97,4 +102,36 @@ public void testWatcherDisabledTests() {
97102
// also no component creation if not enabled
98103
assertThat(watcher.createComponents(null, null, null, null, null, null, null, null, null), hasSize(0));
99104
}
105+
106+
public void testReload() {
107+
Settings settings = Settings.builder()
108+
.put("xpack.watcher.enabled", true)
109+
.put("path.home", createTempDir())
110+
.build();
111+
NotificationService mockService = mock(NotificationService.class);
112+
Watcher watcher = new TestWatcher(settings, mockService);
113+
114+
watcher.reload(settings);
115+
verify(mockService, times(1)).reload(settings);
116+
}
117+
118+
public void testReloadDisabled() {
119+
Settings settings = Settings.builder()
120+
.put("xpack.watcher.enabled", false)
121+
.put("path.home", createTempDir())
122+
.build();
123+
NotificationService mockService = mock(NotificationService.class);
124+
Watcher watcher = new TestWatcher(settings, mockService);
125+
126+
watcher.reload(settings);
127+
verifyNoMoreInteractions(mockService);
128+
}
129+
130+
private class TestWatcher extends Watcher {
131+
132+
TestWatcher(Settings settings, NotificationService service) {
133+
super(settings);
134+
reloadableServices.add(service);
135+
}
136+
}
100137
}

x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/notification/NotificationServiceTests.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ private static class TestNotificationService extends NotificationService<String>
8282

8383
TestNotificationService(Settings settings) {
8484
super(settings, "test");
85-
setAccountSetting(settings);
85+
reload(settings);
8686
}
8787

8888
@Override

0 commit comments

Comments
 (0)