Skip to content

Commit 11826d9

Browse files
committed
feat(mail): add startup checks for mail configuration IQSS#7424
During the deployment of Dataverse we check for conditions of the mail system that might not be done as people intend to use it. We'll only issue warnings in the log messages, nothing critical here.
1 parent d650725 commit 11826d9

File tree

1 file changed

+46
-1
lines changed

1 file changed

+46
-1
lines changed

src/main/java/edu/harvard/iq/dataverse/settings/ConfigCheckService.java

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,22 @@
11
package edu.harvard.iq.dataverse.settings;
22

3+
import edu.harvard.iq.dataverse.MailServiceBean;
4+
import edu.harvard.iq.dataverse.settings.SettingsServiceBean.Key;
35
import edu.harvard.iq.dataverse.util.FileUtil;
4-
6+
import edu.harvard.iq.dataverse.util.MailSessionProducer;
57
import jakarta.annotation.PostConstruct;
68
import jakarta.ejb.DependsOn;
79
import jakarta.ejb.Singleton;
810
import jakarta.ejb.Startup;
11+
import jakarta.inject.Inject;
12+
import jakarta.mail.internet.InternetAddress;
13+
914
import java.io.IOException;
1015
import java.nio.file.FileSystemException;
1116
import java.nio.file.Files;
1217
import java.nio.file.Path;
1318
import java.util.Map;
19+
import java.util.Optional;
1420
import java.util.logging.Level;
1521
import java.util.logging.Logger;
1622

@@ -20,6 +26,11 @@
2026
public class ConfigCheckService {
2127

2228
private static final Logger logger = Logger.getLogger(ConfigCheckService.class.getCanonicalName());
29+
30+
@Inject
31+
MailSessionProducer mailSessionProducer;
32+
@Inject
33+
MailServiceBean mailService;
2334

2435
public static class ConfigurationError extends RuntimeException {
2536
public ConfigurationError(String message) {
@@ -32,6 +43,9 @@ public void startup() {
3243
if (!checkSystemDirectories()) {
3344
throw new ConfigurationError("Not all configuration checks passed successfully. See logs above.");
3445
}
46+
47+
// Only checks resulting in warnings, nothing critical that needs to stop deployment
48+
checkSystemMailSetup();
3549
}
3650

3751
/**
@@ -77,5 +91,36 @@ public boolean checkSystemDirectories() {
7791
}
7892
return success;
7993
}
94+
95+
/**
96+
* This method is not expected to make a deployment fail, but send out clear warning messages about missing or
97+
* wrong configuration settings.
98+
*/
99+
public void checkSystemMailSetup() {
100+
// Check if a system mail setting has been provided or issue warning about disabled mail notifications
101+
Optional<InternetAddress> mailAddress = mailService.getSystemAddress();
102+
103+
// Not present -> warning
104+
if (mailAddress.isEmpty()) {
105+
logger.warning("Could not find a system mail setting in database (key :" + Key.SystemEmail + ", deprecated) or JVM option '" + JvmSettings.SYSTEM_EMAIL.getScopedKey() + "'");
106+
logger.warning("Mail notifications and system messages are deactivated until you provide a configuration");
107+
}
108+
109+
// If there is an app server provided mail config, let's determine if the setup is matching
110+
// TODO: when support for appserver provided mail session goes away, this code can be deleted
111+
if (mailSessionProducer.hasSessionFromAppServer()) {
112+
if (mailAddress.isEmpty()) {
113+
logger.warning("Found a mail session provided by app server, but no system mail address (see logs above)");
114+
// Check if the "from" in the session is the same as the system mail address (see issue 4210)
115+
} else {
116+
String sessionFrom = mailSessionProducer.getSession().getProperty("mail.from");
117+
if (! mailAddress.get().toString().equals(sessionFrom)) {
118+
logger.warning(() -> String.format(
119+
"Found app server mail session provided 'from' (%s) does not match system mail setting (%s)",
120+
sessionFrom, mailAddress.get()));
121+
}
122+
}
123+
}
124+
}
80125

81126
}

0 commit comments

Comments
 (0)