Watcher: Add SMTP default timeouts
This adds default timeouts to the SMTP configuration to prevent infinite timeouts, that can lead to stuck watches. This also requires to use time values instead of just milliseconds. Closes elastic/elasticsearch#1830 Original commit: elastic/x-pack-elasticsearch@c886da7bff
This commit is contained in:
parent
366498eca4
commit
9a5e60b58f
|
@ -9,6 +9,7 @@ import org.elasticsearch.SpecialPermission;
|
|||
import org.elasticsearch.common.logging.ESLogger;
|
||||
import org.elasticsearch.common.settings.Settings;
|
||||
import org.elasticsearch.common.settings.SettingsException;
|
||||
import org.elasticsearch.common.unit.TimeValue;
|
||||
import org.elasticsearch.watcher.support.secret.SecretService;
|
||||
|
||||
import javax.activation.CommandMap;
|
||||
|
@ -51,6 +52,12 @@ public class Account {
|
|||
});
|
||||
}
|
||||
|
||||
static final Settings DEFAULT_SMTP_TIMEOUT_SETTINGS = Settings.builder()
|
||||
.put("connection_timeout", TimeValue.timeValueMinutes(2))
|
||||
.put("write_timeout", TimeValue.timeValueMinutes(2))
|
||||
.put("timeout", TimeValue.timeValueMinutes(2))
|
||||
.build();
|
||||
|
||||
private final Config config;
|
||||
private final SecretService secretService;
|
||||
private final ESLogger logger;
|
||||
|
@ -67,6 +74,10 @@ public class Account {
|
|||
return config.name;
|
||||
}
|
||||
|
||||
Config getConfig() {
|
||||
return config;
|
||||
}
|
||||
|
||||
public Email send(Email email, Authentication auth, Profile profile) throws MessagingException {
|
||||
|
||||
// applying the defaults on missing emails fields
|
||||
|
@ -196,9 +207,11 @@ public class Account {
|
|||
* "unreadable" keys. We'll then use these settings when crea
|
||||
*/
|
||||
static Properties loadSmtpProperties(Settings settings) {
|
||||
Settings.Builder builder = Settings.builder().put(settings);
|
||||
replace(builder, "connection_timeout", "connectiontimeout");
|
||||
replace(builder, "write_timeout", "writetimeout");
|
||||
Settings.Builder builder = Settings.builder().put(DEFAULT_SMTP_TIMEOUT_SETTINGS).put(settings);
|
||||
replaceTimeValue(builder, "connection_timeout", "connectiontimeout");
|
||||
replaceTimeValue(builder, "write_timeout", "writetimeout");
|
||||
replaceTimeValue(builder, "timeout", "timeout");
|
||||
|
||||
replace(builder, "local_address", "localaddress");
|
||||
replace(builder, "local_port", "localport");
|
||||
replace(builder, "allow_8bitmime", "allow8bitmime");
|
||||
|
@ -224,6 +237,12 @@ public class Account {
|
|||
}
|
||||
}
|
||||
|
||||
static void replaceTimeValue(Settings.Builder settings, String currentKey, String newKey) {
|
||||
String value = settings.remove(currentKey);
|
||||
if (value != null) {
|
||||
settings.put(newKey, TimeValue.parseTimeValue(value, currentKey).millis());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -5,7 +5,9 @@
|
|||
*/
|
||||
package org.elasticsearch.watcher.actions.email.service;
|
||||
|
||||
import org.elasticsearch.ElasticsearchException;
|
||||
import org.elasticsearch.common.settings.Settings;
|
||||
import org.elasticsearch.common.unit.TimeValue;
|
||||
import org.elasticsearch.test.ESTestCase;
|
||||
import org.elasticsearch.watcher.actions.email.service.support.EmailServer;
|
||||
import org.elasticsearch.watcher.support.secret.Secret;
|
||||
|
@ -131,6 +133,13 @@ public class AccountTests extends ESTestCase {
|
|||
smtpBuilder.put(name, value);
|
||||
}
|
||||
|
||||
// default properties
|
||||
for (String name : new String[]{ "connection_timeout", "write_timeout", "timeout"}) {
|
||||
String propertyName = name.replaceAll("_", "");
|
||||
smtpProps.put("mail.smtp." + propertyName,
|
||||
String.valueOf(TimeValue.parseTimeValue(Account.DEFAULT_SMTP_TIMEOUT_SETTINGS.get(name), name).millis()));
|
||||
}
|
||||
|
||||
Settings smtpSettings = smtpBuilder.build();
|
||||
for (String name : smtpSettings.names()) {
|
||||
builder.put("smtp." + name, smtpSettings.get(name));
|
||||
|
@ -263,4 +272,42 @@ public class AccountTests extends ESTestCase {
|
|||
|
||||
handle.remove();
|
||||
}
|
||||
|
||||
public void testDefaultAccountTimeout() {
|
||||
Account account = new Account(new Account.Config("default", Settings.builder()
|
||||
.put("smtp.host", "localhost")
|
||||
.put("smtp.port", server.port())
|
||||
.build()), SecretService.Insecure.INSTANCE, logger);
|
||||
|
||||
Properties mailProperties = account.getConfig().smtp.properties;
|
||||
assertThat(mailProperties.get("mail.smtp.connectiontimeout"), is(String.valueOf(TimeValue.timeValueMinutes(2).millis())));
|
||||
assertThat(mailProperties.get("mail.smtp.writetimeout"), is(String.valueOf(TimeValue.timeValueMinutes(2).millis())));
|
||||
assertThat(mailProperties.get("mail.smtp.timeout"), is(String.valueOf(TimeValue.timeValueMinutes(2).millis())));
|
||||
}
|
||||
|
||||
public void testAccountTimeoutsCanBeConfigureAsTimeValue() {
|
||||
Account account = new Account(new Account.Config("default", Settings.builder()
|
||||
.put("smtp.host", "localhost")
|
||||
.put("smtp.port", server.port())
|
||||
.put("smtp.connection_timeout", TimeValue.timeValueMinutes(4))
|
||||
.put("smtp.write_timeout", TimeValue.timeValueMinutes(6))
|
||||
.put("smtp.timeout", TimeValue.timeValueMinutes(8))
|
||||
.build()), SecretService.Insecure.INSTANCE, logger);
|
||||
|
||||
Properties mailProperties = account.getConfig().smtp.properties;
|
||||
|
||||
assertThat(mailProperties.get("mail.smtp.connectiontimeout"), is(String.valueOf(TimeValue.timeValueMinutes(4).millis())));
|
||||
assertThat(mailProperties.get("mail.smtp.writetimeout"), is(String.valueOf(TimeValue.timeValueMinutes(6).millis())));
|
||||
assertThat(mailProperties.get("mail.smtp.timeout"), is(String.valueOf(TimeValue.timeValueMinutes(8).millis())));
|
||||
}
|
||||
|
||||
public void testAccountTimeoutsConfiguredAsNumberAreRejected() {
|
||||
expectThrows(ElasticsearchException.class, () -> {
|
||||
new Account(new Account.Config("default", Settings.builder()
|
||||
.put("smtp.host", "localhost")
|
||||
.put("smtp.port", server.port())
|
||||
.put("smtp.connection_timeout", 4000)
|
||||
.build()), SecretService.Insecure.INSTANCE, logger);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue