From cdccd6a0fcd7a8daab0e0d0ac95784caa48e3f8c Mon Sep 17 00:00:00 2001 From: Dan Galdi Date: Mon, 12 Aug 2024 13:19:53 -0400 Subject: [PATCH 1/8] Add smtp auth to the model config --- .../java/org/gusdb/wdk/model/Utilities.java | 23 ++++++++++++++++--- .../gusdb/wdk/model/config/ModelConfig.java | 16 ++++++++++++- .../wdk/model/config/ModelConfigBuilder.java | 16 ++++++++++++- 3 files changed, 50 insertions(+), 5 deletions(-) diff --git a/Model/src/main/java/org/gusdb/wdk/model/Utilities.java b/Model/src/main/java/org/gusdb/wdk/model/Utilities.java index 921c68038..b709d1c25 100644 --- a/Model/src/main/java/org/gusdb/wdk/model/Utilities.java +++ b/Model/src/main/java/org/gusdb/wdk/model/Utilities.java @@ -18,9 +18,11 @@ import javax.activation.DataHandler; import javax.mail.Address; +import javax.mail.Authenticator; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.Multipart; +import javax.mail.PasswordAuthentication; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; @@ -215,11 +217,17 @@ public static void sendEmail(String smtpServer, String sendTos, String reply, String subject, String content, String ccAddresses, Attachment[] attachments) throws WdkModelException { // call the 8 parameter one - sendEmail(smtpServer, sendTos, reply, subject, content, ccAddresses, null, attachments); + sendEmail(smtpServer, null, null, sendTos, reply, subject, content, ccAddresses, null, attachments); } - // sendEmail() all 8 parameters public static void sendEmail(String smtpServer, String sendTos, String reply, + String subject, String content, String ccAddresses, String bccAddresses, + Attachment[] attachments) throws WdkModelException { + sendEmail(smtpServer, null, null, sendTos, reply, subject, content, ccAddresses, bccAddresses, attachments); + } + + // sendEmail() all 10 parameters + public static void sendEmail(String smtpServer, String username, String password, String sendTos, String reply, String subject, String content, String ccAddresses, String bccAddresses, Attachment[] attachments) throws WdkModelException { @@ -230,7 +238,16 @@ public static void sendEmail(String smtpServer, String sendTos, String reply, Properties props = new Properties(); props.put("mail.smtp.host", smtpServer); props.put("mail.debug", "true"); - Session session = Session.getInstance(props); + props.put("mail.smtp.auth", "true"); + Authenticator auth = new Authenticator() { + @Override + protected PasswordAuthentication getPasswordAuthentication() { + return new PasswordAuthentication(username, password); + // "postmaster@sandbox5c3df189e6f6421db320cda505abc1fd.mailgun.org", "fafedf8a5d8866e3fd5ca0c1cb8b14c2-a26b1841-5d728638"); + } + }; +// props.put("main.smtp.port", Integer.toString()) + Session session = Session.getInstance(props, auth); // instantiate a message Message message = new MimeMessage(session); diff --git a/Model/src/main/java/org/gusdb/wdk/model/config/ModelConfig.java b/Model/src/main/java/org/gusdb/wdk/model/config/ModelConfig.java index 5431946e1..3bbf88092 100644 --- a/Model/src/main/java/org/gusdb/wdk/model/config/ModelConfig.java +++ b/Model/src/main/java/org/gusdb/wdk/model/config/ModelConfig.java @@ -41,6 +41,18 @@ public String getName() { */ private final String _smtpServer; + /** + * the SMTP server used to send registration & recover password emails. + */ + private final Optional _smtpUserName; + + + /** + * the SMTP server used to send registration & recover password emails. + */ + private final Optional _smtpPassword; + + /** * the reply of the registration & recover password emails. */ @@ -143,7 +155,7 @@ public String getName() { public ModelConfig(String modelName, String projectId, Path gusHome, boolean caching, boolean useWeights, String paramRegex, Optional secretKeyFile, String secretKey, Path wdkTempDir, String webServiceUrl, String assetsUrl, - String smtpServer, String supportEmail, List adminEmails, String emailSubject, + String smtpServer, Optional smtpUser, Optional smtpPassword, String supportEmail, List adminEmails, String emailSubject, String emailContent, ModelConfigUserDB userDB, ModelConfigAppDB appDB, ModelConfigUserDatasetStore userDatasetStoreConfig, QueryMonitor queryMonitor, boolean monitorBlockedThreads, int blockedThreshold, AuthenticationMethod authenticationMethod, @@ -171,6 +183,8 @@ public ModelConfig(String modelName, String projectId, Path gusHome, boolean cac // email setup _smtpServer = smtpServer; + _smtpUserName = smtpUser; + _smtpPassword = smtpPassword; _supportEmail = supportEmail; _adminEmails = adminEmails; _emailSubject = emailSubject; diff --git a/Model/src/main/java/org/gusdb/wdk/model/config/ModelConfigBuilder.java b/Model/src/main/java/org/gusdb/wdk/model/config/ModelConfigBuilder.java index 39bdab57b..4d87234e0 100644 --- a/Model/src/main/java/org/gusdb/wdk/model/config/ModelConfigBuilder.java +++ b/Model/src/main/java/org/gusdb/wdk/model/config/ModelConfigBuilder.java @@ -38,6 +38,8 @@ public class ModelConfigBuilder { // email setup private String _smtpServer = "localhost"; + private String _smtpUsername; + private String _smtpPassword; private String _supportEmail; private List _adminEmails = Collections.emptyList(); private String _emailSubject = ""; @@ -93,6 +95,8 @@ public ModelConfig build() throws WdkModelException { assertNonNull("appDb", _appDB); // TODO: should probably have a default stub for this to avoid NPEs //assertNonNull("userDatasetStoreConfig", _userDatasetStoreConfig); + Optional smtpUsername = Optional.ofNullable(_smtpUsername).filter(s -> !s.isBlank()); + Optional smtpPassword = Optional.ofNullable(_smtpPassword).filter(s -> !s.isBlank()); return new ModelConfig( @@ -117,6 +121,8 @@ public ModelConfig build() throws WdkModelException { // email setup _smtpServer, + smtpUsername, + smtpPassword, _supportEmail, _adminEmails, _emailSubject, @@ -199,6 +205,14 @@ public void setSmtpServer(String smtpServer) { _smtpServer = smtpServer; } + public void setSmtpUser(String smtpUser) { + _smtpServer = smtpUser; + } + + public void setSmtpPassword(String smtpPassword) { + _smtpServer = smtpPassword; + } + /** * @param emailContent * The emailContent to set. @@ -256,7 +270,7 @@ public void setSecretKeyFile(String secretKeyFile) { } /** - * @param secretKeyFile + * @param secretKey * the secretKeyFile to set */ public void setSecretKey(String secretKey) { From 42c55ac35ed4a153513ea96713b7570c95bb1c9a Mon Sep 17 00:00:00 2001 From: Dan Galdi Date: Mon, 12 Aug 2024 13:35:37 -0400 Subject: [PATCH 2/8] Fix extra comments --- .../java/org/gusdb/wdk/model/Utilities.java | 21 +++++++++++-------- .../gusdb/wdk/model/config/ModelConfig.java | 4 ++-- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/Model/src/main/java/org/gusdb/wdk/model/Utilities.java b/Model/src/main/java/org/gusdb/wdk/model/Utilities.java index b709d1c25..73a963296 100644 --- a/Model/src/main/java/org/gusdb/wdk/model/Utilities.java +++ b/Model/src/main/java/org/gusdb/wdk/model/Utilities.java @@ -238,15 +238,18 @@ public static void sendEmail(String smtpServer, String username, String password Properties props = new Properties(); props.put("mail.smtp.host", smtpServer); props.put("mail.debug", "true"); - props.put("mail.smtp.auth", "true"); - Authenticator auth = new Authenticator() { - @Override - protected PasswordAuthentication getPasswordAuthentication() { - return new PasswordAuthentication(username, password); - // "postmaster@sandbox5c3df189e6f6421db320cda505abc1fd.mailgun.org", "fafedf8a5d8866e3fd5ca0c1cb8b14c2-a26b1841-5d728638"); - } - }; -// props.put("main.smtp.port", Integer.toString()) + Authenticator auth = null; + + if (username != null & password != null) { + props.put("mail.smtp.auth", "true"); + auth = new Authenticator() { + @Override + protected PasswordAuthentication getPasswordAuthentication() { + return new PasswordAuthentication(username, password); + } + }; + } + Session session = Session.getInstance(props, auth); // instantiate a message diff --git a/Model/src/main/java/org/gusdb/wdk/model/config/ModelConfig.java b/Model/src/main/java/org/gusdb/wdk/model/config/ModelConfig.java index 3bbf88092..60f87e8d2 100644 --- a/Model/src/main/java/org/gusdb/wdk/model/config/ModelConfig.java +++ b/Model/src/main/java/org/gusdb/wdk/model/config/ModelConfig.java @@ -42,13 +42,13 @@ public String getName() { private final String _smtpServer; /** - * the SMTP server used to send registration & recover password emails. + * the SMTP username used to authenticate with smtpServer. */ private final Optional _smtpUserName; /** - * the SMTP server used to send registration & recover password emails. + * the SMTP password used to authenticate with smtpServer. */ private final Optional _smtpPassword; From 95e0f29bbe540e664c757586744cb0018ba80598 Mon Sep 17 00:00:00 2001 From: Dan Galdi Date: Mon, 12 Aug 2024 13:40:43 -0400 Subject: [PATCH 3/8] Email notification plumbing --- Model/src/main/java/org/gusdb/wdk/model/Utilities.java | 3 +-- .../main/java/org/gusdb/wdk/model/config/ModelConfig.java | 8 ++++++++ .../org/gusdb/wdk/model/user/UserPasswordEmailer.java | 8 +++++++- 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/Model/src/main/java/org/gusdb/wdk/model/Utilities.java b/Model/src/main/java/org/gusdb/wdk/model/Utilities.java index 73a963296..209732c6b 100644 --- a/Model/src/main/java/org/gusdb/wdk/model/Utilities.java +++ b/Model/src/main/java/org/gusdb/wdk/model/Utilities.java @@ -228,8 +228,7 @@ public static void sendEmail(String smtpServer, String sendTos, String reply, // sendEmail() all 10 parameters public static void sendEmail(String smtpServer, String username, String password, String sendTos, String reply, - String subject, String content, String ccAddresses, String bccAddresses, - Attachment[] attachments) throws WdkModelException { + String subject, String content, String ccAddresses, String bccAddresses, Attachment[] attachments) throws WdkModelException { LOG.debug("Sending message to: " + sendTos + ", bcc to: " + bccAddresses + ",reply: " + reply + ", using SMPT: " + smtpServer); diff --git a/Model/src/main/java/org/gusdb/wdk/model/config/ModelConfig.java b/Model/src/main/java/org/gusdb/wdk/model/config/ModelConfig.java index 60f87e8d2..feea6767a 100644 --- a/Model/src/main/java/org/gusdb/wdk/model/config/ModelConfig.java +++ b/Model/src/main/java/org/gusdb/wdk/model/config/ModelConfig.java @@ -269,6 +269,14 @@ public String getSmtpServer() { return _smtpServer; } + public Optional getSmtpUserName() { + return _smtpUserName; + } + + public Optional getSmtpPassword() { + return _smtpPassword; + } + /** * @return Returns the emailContent. */ diff --git a/Model/src/main/java/org/gusdb/wdk/model/user/UserPasswordEmailer.java b/Model/src/main/java/org/gusdb/wdk/model/user/UserPasswordEmailer.java index 7a083f706..99602422a 100644 --- a/Model/src/main/java/org/gusdb/wdk/model/user/UserPasswordEmailer.java +++ b/Model/src/main/java/org/gusdb/wdk/model/user/UserPasswordEmailer.java @@ -1,7 +1,9 @@ package org.gusdb.wdk.model.user; +import java.util.Optional; import java.util.regex.Matcher; +import org.gusdb.wdk.model.Attachment; import org.gusdb.wdk.model.Utilities; import org.gusdb.wdk.model.WdkModel; import org.gusdb.wdk.model.WdkModelException; @@ -37,6 +39,10 @@ public void emailTemporaryPassword(User user, String password) throws WdkModelEx String supportEmail = wdkModelConfig.getSupportEmail(); String emailSubject = wdkModelConfig.getEmailSubject(); + // Unwrap optionals here to maintain consistency with nullable arguments in Utilities.sendEmail method. + String smtpUser = wdkModelConfig.getSmtpUserName().orElse(null); + String smtpPass = wdkModelConfig.getSmtpPassword().orElse(null); + // populate email content macros with user data String emailContent = wdkModelConfig.getEmailContent() .replaceAll("\\$\\$" + EMAIL_MACRO_USER_NAME + "\\$\\$", @@ -46,6 +52,6 @@ public void emailTemporaryPassword(User user, String password) throws WdkModelEx .replaceAll("\\$\\$" + EMAIL_MACRO_PASSWORD + "\\$\\$", Matcher.quoteReplacement(password)); - Utilities.sendEmail(smtpServer, user.getEmail(), supportEmail, emailSubject, emailContent); + Utilities.sendEmail(smtpServer, user.getEmail(), supportEmail, emailSubject, emailContent, null, null, smtpUser, smtpPass, new Attachment[]{}); } } From f7156e6868b268e8cf84f8c0dc1c324b525452f7 Mon Sep 17 00:00:00 2001 From: Dan Galdi Date: Mon, 12 Aug 2024 14:27:23 -0400 Subject: [PATCH 4/8] Fix WDK model validation --- Model/lib/rng/wdkModel-config.rng | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Model/lib/rng/wdkModel-config.rng b/Model/lib/rng/wdkModel-config.rng index b5ffc0544..e94c137d8 100644 --- a/Model/lib/rng/wdkModel-config.rng +++ b/Model/lib/rng/wdkModel-config.rng @@ -6,6 +6,8 @@ + + From ad48694d93fc69a32a62f9ecb75b1350b72c8536 Mon Sep 17 00:00:00 2001 From: Dan Galdi Date: Mon, 12 Aug 2024 15:29:45 -0400 Subject: [PATCH 5/8] Fix default smtp host logic --- .../java/org/gusdb/wdk/model/config/ModelConfigBuilder.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Model/src/main/java/org/gusdb/wdk/model/config/ModelConfigBuilder.java b/Model/src/main/java/org/gusdb/wdk/model/config/ModelConfigBuilder.java index 4d87234e0..228fd81c3 100644 --- a/Model/src/main/java/org/gusdb/wdk/model/config/ModelConfigBuilder.java +++ b/Model/src/main/java/org/gusdb/wdk/model/config/ModelConfigBuilder.java @@ -37,7 +37,7 @@ public class ModelConfigBuilder { private String _assetsUrl; // email setup - private String _smtpServer = "localhost"; + private String _smtpServer; private String _smtpUsername; private String _smtpPassword; private String _supportEmail; @@ -97,6 +97,7 @@ public ModelConfig build() throws WdkModelException { //assertNonNull("userDatasetStoreConfig", _userDatasetStoreConfig); Optional smtpUsername = Optional.ofNullable(_smtpUsername).filter(s -> !s.isBlank()); Optional smtpPassword = Optional.ofNullable(_smtpPassword).filter(s -> !s.isBlank()); + String smtpServer = _smtpServer == null ? "localhost" : _smtpServer; return new ModelConfig( @@ -120,7 +121,7 @@ public ModelConfig build() throws WdkModelException { _assetsUrl, // email setup - _smtpServer, + smtpServer, smtpUsername, smtpPassword, _supportEmail, From 7ae1e463461ce53c9302853d14240116478271a8 Mon Sep 17 00:00:00 2001 From: Dan Galdi Date: Tue, 13 Aug 2024 10:10:48 -0400 Subject: [PATCH 6/8] Fix argument order --- .../java/org/gusdb/wdk/model/config/ModelConfigBuilder.java | 6 +++--- .../java/org/gusdb/wdk/model/user/UserPasswordEmailer.java | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Model/src/main/java/org/gusdb/wdk/model/config/ModelConfigBuilder.java b/Model/src/main/java/org/gusdb/wdk/model/config/ModelConfigBuilder.java index 228fd81c3..c5ea41243 100644 --- a/Model/src/main/java/org/gusdb/wdk/model/config/ModelConfigBuilder.java +++ b/Model/src/main/java/org/gusdb/wdk/model/config/ModelConfigBuilder.java @@ -206,12 +206,12 @@ public void setSmtpServer(String smtpServer) { _smtpServer = smtpServer; } - public void setSmtpUser(String smtpUser) { - _smtpServer = smtpUser; + public void setSmtpUsername(String smtpUsername) { + _smtpUsername = smtpUsername; } public void setSmtpPassword(String smtpPassword) { - _smtpServer = smtpPassword; + _smtpPassword = smtpPassword; } /** diff --git a/Model/src/main/java/org/gusdb/wdk/model/user/UserPasswordEmailer.java b/Model/src/main/java/org/gusdb/wdk/model/user/UserPasswordEmailer.java index 99602422a..6a1373721 100644 --- a/Model/src/main/java/org/gusdb/wdk/model/user/UserPasswordEmailer.java +++ b/Model/src/main/java/org/gusdb/wdk/model/user/UserPasswordEmailer.java @@ -52,6 +52,6 @@ public void emailTemporaryPassword(User user, String password) throws WdkModelEx .replaceAll("\\$\\$" + EMAIL_MACRO_PASSWORD + "\\$\\$", Matcher.quoteReplacement(password)); - Utilities.sendEmail(smtpServer, user.getEmail(), supportEmail, emailSubject, emailContent, null, null, smtpUser, smtpPass, new Attachment[]{}); + Utilities.sendEmail(smtpServer, smtpUser, smtpPass, user.getEmail(), supportEmail, emailSubject, emailContent, null, null, new Attachment[]{}); } } From e8e20d507c6d03cf2b3ae1852daeb2dafe31ac30 Mon Sep 17 00:00:00 2001 From: Dan Galdi Date: Tue, 13 Aug 2024 10:12:30 -0400 Subject: [PATCH 7/8] Fixes for validatin --- Model/lib/rng/wdkModel-config.rng | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/Model/lib/rng/wdkModel-config.rng b/Model/lib/rng/wdkModel-config.rng index e94c137d8..6bdd9e4c7 100644 --- a/Model/lib/rng/wdkModel-config.rng +++ b/Model/lib/rng/wdkModel-config.rng @@ -6,11 +6,15 @@ - - - + + + + + + + From e6c035a723907fd076c22afd75367b4b7cf51856 Mon Sep 17 00:00:00 2001 From: Dan Galdi Date: Tue, 13 Aug 2024 10:13:22 -0400 Subject: [PATCH 8/8] Accidental bitwise & --- Model/src/main/java/org/gusdb/wdk/model/Utilities.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Model/src/main/java/org/gusdb/wdk/model/Utilities.java b/Model/src/main/java/org/gusdb/wdk/model/Utilities.java index 209732c6b..ab6b8e2b1 100644 --- a/Model/src/main/java/org/gusdb/wdk/model/Utilities.java +++ b/Model/src/main/java/org/gusdb/wdk/model/Utilities.java @@ -239,7 +239,7 @@ public static void sendEmail(String smtpServer, String username, String password props.put("mail.debug", "true"); Authenticator auth = null; - if (username != null & password != null) { + if (username != null && password != null) { props.put("mail.smtp.auth", "true"); auth = new Authenticator() { @Override