Add support to StartTLS on Quota's mailing (#4573)

Co-authored-by: Daniel Augusto Veronezi Salvador <daniel@scclouds.com.br>
This commit is contained in:
Daniel Augusto Veronezi Salvador 2021-04-13 04:55:02 -03:00 committed by GitHub
parent 911376e495
commit b28d638ade
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 16 additions and 4 deletions

View File

@ -63,6 +63,7 @@ import com.sun.mail.smtp.SMTPMessage;
import com.sun.mail.smtp.SMTPSSLTransport; import com.sun.mail.smtp.SMTPSSLTransport;
import com.sun.mail.smtp.SMTPTransport; import com.sun.mail.smtp.SMTPTransport;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang.BooleanUtils;
@Component @Component
public class QuotaAlertManagerImpl extends ManagerBase implements QuotaAlertManager { public class QuotaAlertManagerImpl extends ManagerBase implements QuotaAlertManager {
@ -116,9 +117,11 @@ public class QuotaAlertManagerImpl extends ManagerBase implements QuotaAlertMana
String smtpPassword = configs.get(QuotaConfig.QuotaSmtpPassword.key()); String smtpPassword = configs.get(QuotaConfig.QuotaSmtpPassword.key());
String emailSender = configs.get(QuotaConfig.QuotaSmtpSender.key()); String emailSender = configs.get(QuotaConfig.QuotaSmtpSender.key());
String smtpEnabledSecurityProtocols = configs.get(QuotaConfig.QuotaSmtpEnabledSecurityProtocols.key()); String smtpEnabledSecurityProtocols = configs.get(QuotaConfig.QuotaSmtpEnabledSecurityProtocols.key());
String useStartTLSStr = configs.get(QuotaConfig.QuotaSmtpUseStartTLS.key());
boolean useStartTLS = BooleanUtils.toBoolean(useStartTLSStr);
_lockAccountEnforcement = "true".equalsIgnoreCase(configs.get(QuotaConfig.QuotaEnableEnforcement.key())); _lockAccountEnforcement = "true".equalsIgnoreCase(configs.get(QuotaConfig.QuotaEnableEnforcement.key()));
_emailQuotaAlert = new EmailQuotaAlert(smtpHost, smtpPort, useAuth, smtpUsername, smtpPassword, emailSender, smtpEnabledSecurityProtocols, _smtpDebug);
_emailQuotaAlert = new EmailQuotaAlert(smtpHost, smtpPort, useAuth, smtpUsername, smtpPassword, emailSender, smtpEnabledSecurityProtocols, useStartTLS, _smtpDebug);
return true; return true;
} }
@ -342,14 +345,16 @@ public class QuotaAlertManagerImpl extends ManagerBase implements QuotaAlertMana
private final String _smtpUsername; private final String _smtpUsername;
private final String _smtpPassword; private final String _smtpPassword;
private final String _emailSender; private final String _emailSender;
private final boolean smtpUseStartTLS;
public EmailQuotaAlert(String smtpHost, int smtpPort, boolean smtpUseAuth, final String smtpUsername, final String smtpPassword, String emailSender, String smtpEnabledSecurityProtocols, boolean smtpDebug) { public EmailQuotaAlert(String smtpHost, int smtpPort, boolean smtpUseAuth, final String smtpUsername, final String smtpPassword, String emailSender, String smtpEnabledSecurityProtocols, boolean smtpUseStartTLS, boolean smtpDebug) {
_smtpHost = smtpHost; _smtpHost = smtpHost;
_smtpPort = smtpPort; _smtpPort = smtpPort;
_smtpUseAuth = smtpUseAuth; _smtpUseAuth = smtpUseAuth;
_smtpUsername = smtpUsername; _smtpUsername = smtpUsername;
_smtpPassword = smtpPassword; _smtpPassword = smtpPassword;
_emailSender = emailSender; _emailSender = emailSender;
this.smtpUseStartTLS = smtpUseStartTLS;
if (!Strings.isNullOrEmpty(_smtpHost)) { if (!Strings.isNullOrEmpty(_smtpHost)) {
Properties smtpProps = new Properties(); Properties smtpProps = new Properties();
@ -371,6 +376,10 @@ public class QuotaAlertManagerImpl extends ManagerBase implements QuotaAlertMana
smtpProps.put("mail.smtp.ssl.protocols", smtpEnabledSecurityProtocols); smtpProps.put("mail.smtp.ssl.protocols", smtpEnabledSecurityProtocols);
} }
if (smtpUseAuth) {
smtpProps.put("mail.smtp.starttls.enable", smtpUseStartTLS);
}
if (!Strings.isNullOrEmpty(smtpUsername) && !Strings.isNullOrEmpty(smtpPassword)) { if (!Strings.isNullOrEmpty(smtpUsername) && !Strings.isNullOrEmpty(smtpPassword)) {
_smtpSession = Session.getInstance(smtpProps, new Authenticator() { _smtpSession = Session.getInstance(smtpProps, new Authenticator() {
@Override @Override
@ -413,7 +422,7 @@ public class QuotaAlertManagerImpl extends ManagerBase implements QuotaAlertMana
msg.saveChanges(); msg.saveChanges();
SMTPTransport smtpTrans = null; SMTPTransport smtpTrans = null;
if (_smtpUseAuth) { if (_smtpUseAuth && !this.smtpUseStartTLS) {
smtpTrans = new SMTPSSLTransport(_smtpSession, new URLName("smtp", _smtpHost, _smtpPort, null, _smtpUsername, _smtpPassword)); smtpTrans = new SMTPSSLTransport(_smtpSession, new URLName("smtp", _smtpHost, _smtpPort, null, _smtpUsername, _smtpPassword));
} else { } else {
smtpTrans = new SMTPTransport(_smtpSession, new URLName("smtp", _smtpHost, _smtpPort, null, _smtpUsername, _smtpPassword)); smtpTrans = new SMTPTransport(_smtpSession, new URLName("smtp", _smtpHost, _smtpPort, null, _smtpUsername, _smtpPassword));

View File

@ -54,6 +54,9 @@ public interface QuotaConfig {
public static final ConfigKey<String> QuotaSmtpEnabledSecurityProtocols = new ConfigKey<String>("Advanced", String.class, "quota.usage.smtp.enabledSecurityProtocols", "", public static final ConfigKey<String> QuotaSmtpEnabledSecurityProtocols = new ConfigKey<String>("Advanced", String.class, "quota.usage.smtp.enabledSecurityProtocols", "",
"White-space separated security protocols; ex: \"TLSv1 TLSv1.1\". Supported protocols: SSLv2Hello, SSLv3, TLSv1, TLSv1.1 and TLSv1.2", true); "White-space separated security protocols; ex: \"TLSv1 TLSv1.1\". Supported protocols: SSLv2Hello, SSLv3, TLSv1, TLSv1.1 and TLSv1.2", true);
public static final ConfigKey<String> QuotaSmtpUseStartTLS = new ConfigKey<String>("Advanced", String.class, "quota.usage.smtp.useStartTLS", "false",
"If set to true and if we enable security via quota.usage.smtp.useAuth, this will enable StartTLS to secure the conection.", true);
enum QuotaEmailTemplateTypes { enum QuotaEmailTemplateTypes {
QUOTA_LOW, QUOTA_EMPTY, QUOTA_UNLOCK_ACCOUNT, QUOTA_STATEMENT QUOTA_LOW, QUOTA_EMPTY, QUOTA_UNLOCK_ACCOUNT, QUOTA_STATEMENT
} }

View File

@ -137,7 +137,7 @@ public class QuotaServiceImpl extends ManagerBase implements QuotaService, Confi
@Override @Override
public ConfigKey<?>[] getConfigKeys() { public ConfigKey<?>[] getConfigKeys() {
return new ConfigKey<?>[] {QuotaPluginEnabled, QuotaEnableEnforcement, QuotaCurrencySymbol, QuotaStatementPeriod, QuotaSmtpHost, QuotaSmtpPort, QuotaSmtpTimeout, return new ConfigKey<?>[] {QuotaPluginEnabled, QuotaEnableEnforcement, QuotaCurrencySymbol, QuotaStatementPeriod, QuotaSmtpHost, QuotaSmtpPort, QuotaSmtpTimeout,
QuotaSmtpUser, QuotaSmtpPassword, QuotaSmtpAuthType, QuotaSmtpSender, QuotaSmtpEnabledSecurityProtocols}; QuotaSmtpUser, QuotaSmtpPassword, QuotaSmtpAuthType, QuotaSmtpSender, QuotaSmtpEnabledSecurityProtocols, QuotaSmtpUseStartTLS};
} }
@Override @Override