Skip to content

Commit

Permalink
NMS-15759: Reverting some changes
Browse files Browse the repository at this point in the history
  • Loading branch information
christianpape committed Aug 30, 2023
1 parent f7f813a commit 625d1f0
Show file tree
Hide file tree
Showing 17 changed files with 148 additions and 67 deletions.
4 changes: 4 additions & 0 deletions opennms-ackd/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -118,5 +118,9 @@
<artifactId>postgresql</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.opennms.core.mate</groupId>
<artifactId>org.opennms.core.mate.model</artifactId>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@
import javax.mail.MessagingException;
import javax.mail.internet.InternetAddress;

import org.opennms.core.mate.api.EntityScopeProvider;
import org.opennms.core.mate.api.Interpolator;
import org.opennms.core.utils.StringUtils;
import org.opennms.javamail.JavaMailerException;
import org.opennms.javamail.JavaReadMailer;
Expand Down Expand Up @@ -80,8 +78,6 @@ class MailAckProcessor implements AckProcessor {
private AcknowledgmentDao m_ackDao;

private volatile JavaMailConfigurationDao m_jmConfigDao;

private EntityScopeProvider m_entityScopeProvider;

/**
* <p>afterPropertiesSet</p>
Expand Down Expand Up @@ -290,7 +286,7 @@ protected List<Message> retrieveAckMessages() throws JavaMailerException {

//TODO: make flag for folder open mode
//TODO: Make sure configuration supports flag for deleting acknowledgments
JavaReadMailer readMailer = new JavaReadMailer((ReadmailConfig) Interpolator.interpolate(readMailConfig, m_entityScopeProvider.getScopeForScv()), true);
JavaReadMailer readMailer = new JavaReadMailer(readMailConfig, true);

String notifRe = m_ackdDao.getConfig().getNotifyidMatchExpression();
notifRe = notifRe.startsWith("~") ? notifRe.substring(1) : notifRe;
Expand Down Expand Up @@ -409,11 +405,7 @@ public synchronized void setAckdConfigDao(final AckdConfigurationDao configDao)
public synchronized void setAcknowledgmentDao(final AcknowledgmentDao ackDao) {
m_ackDao = ackDao;
}

public void setEntityScopeProvider(final EntityScopeProvider entityScopeProvider) {
m_entityScopeProvider = entityScopeProvider;
}


/**
* <p>reloadConfigs</p>
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
<property name="ackdConfigDao" ref="ackdConfigurationDao" />
<property name="acknowledgmentDao" ref="acknowledgmentDao" />
<property name="jmConfigDao" ref="javamailConfigDao" />
<property name="entityScopeProvider" ref="entityScopeProvider"/>
</bean>


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,30 +28,24 @@

package org.opennms.netmgt.ackd.readers;

import static org.junit.Assert.fail;

import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.concurrent.Future;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import javax.mail.Address;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.Message.RecipientType;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Message.RecipientType;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

import org.junit.Assert;

import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand All @@ -60,7 +54,6 @@
import org.opennms.core.test.db.annotations.JUnitTemporaryDatabase;
import org.opennms.javamail.JavaMailerException;
import org.opennms.javamail.JavaSendMailer;
import org.opennms.netmgt.ackd.AckReader;
import org.opennms.netmgt.ackd.Ackd;
import org.opennms.netmgt.config.ackd.AckdConfiguration;
import org.opennms.netmgt.config.javamail.End2endMailConfig;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@
import java.util.List;
import java.util.Map;

import org.opennms.core.mate.api.EntityScopeProvider;
import org.opennms.core.mate.api.Interpolator;
import org.opennms.core.utils.PropertiesUtils;
import org.opennms.core.xml.JaxbUtils;
import org.opennms.javamail.JavaMailerException;
Expand Down Expand Up @@ -89,18 +87,15 @@ public class EmailNorthbounder extends AbstractNorthbounder implements Initializ
/** The initialized flag (it will be true when the NBI is properly initialized). */
private boolean initialized = false;

private EntityScopeProvider m_entityScopeProvider;

/**
* Instantiates a new SNMP Trap northbounder.
*
* @param configDao the SNMP Trap configuration DAO
* @param javaMailDao the JavaMail configuration DAO
* @param destinationName the destination name
*/
public EmailNorthbounder(EmailNorthbounderConfigDao configDao, JavaMailConfigurationDao javaMailDao, String destinationName, EntityScopeProvider entityScopeProvider) {
public EmailNorthbounder(EmailNorthbounderConfigDao configDao, JavaMailConfigurationDao javaMailDao, String destinationName) {
super(NBI_NAME + ":" + destinationName);
m_entityScopeProvider = entityScopeProvider;
m_configDao = configDao;
m_destination = configDao.getConfig().getEmailDestination(destinationName);

Expand Down Expand Up @@ -194,7 +189,7 @@ public void forwardAlarms(List<NorthboundAlarm> alarms) throws NorthbounderExcep
LOG.info("Forwarding {} alarms to destination {}", alarms.size(), m_destination.getName());
for (NorthboundAlarm alarm : alarms) {
try {
JavaSendMailer mailer = new JavaSendMailer((SendmailConfig) Interpolator.interpolate(getSendmailConfig(alarm), m_entityScopeProvider.getScopeForScv()), false);
JavaSendMailer mailer = new JavaSendMailer(getSendmailConfig(alarm), false);
mailer.send();
} catch (JavaMailerException e) {
LOG.error("Can't send email for {}", alarm, e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import java.util.HashMap;
import java.util.Map;

import org.opennms.core.mate.api.EntityScopeProvider;
import org.opennms.core.soa.Registration;
import org.opennms.core.soa.ServiceRegistry;
import org.opennms.netmgt.alarmd.api.NorthboundAlarm;
Expand Down Expand Up @@ -67,9 +66,6 @@ public class EmailNorthbounderManager implements InitializingBean, Northbounder,
@Autowired
private JavaMailConfigurationDao m_javaMailDao;

@Autowired
private EntityScopeProvider m_entityScopeProvider;

/** The registrations map. */
private Map<String, Registration> m_registrations = new HashMap<String, Registration>();

Expand Down Expand Up @@ -102,7 +98,7 @@ private void registerNorthbounders() throws Exception {
}
for (EmailDestination destination : m_configDao.getConfig().getEmailDestinations()) {
LOG.info("Registering Email northbound configuration for destination {}.", destination.getName());
EmailNorthbounder nbi = new EmailNorthbounder(m_configDao, m_javaMailDao, destination.getName(), m_entityScopeProvider);
EmailNorthbounder nbi = new EmailNorthbounder(m_configDao, m_javaMailDao, destination.getName());
nbi.afterPropertiesSet();
m_registrations.put(nbi.getName(), m_serviceRegistry.register(nbi, Northbounder.class));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

import org.opennms.core.mate.api.EntityScopeProvider;
import org.opennms.core.mate.api.Interpolator;
import org.opennms.javamail.JavaMailerException;
import org.opennms.javamail.JavaSendMailer;
import org.opennms.netmgt.config.javamail.SendmailConfig;
Expand All @@ -55,8 +53,6 @@
*/
public class JavaMailDeliveryService implements ReportDeliveryService {

private EntityScopeProvider m_entityScopeProvider;

/** The Constant LOG. */
private static final Logger LOG = LoggerFactory.getLogger(JavaMailDeliveryService.class);

Expand All @@ -80,7 +76,7 @@ public void deliverReport(Report report, String fileName) throws ReportDeliveryE
config = m_JavamailConfigDao.getDefaultSendmailConfig();
}

JavaSendMailer sm = new JavaSendMailer((SendmailConfig) Interpolator.interpolate(config, m_entityScopeProvider.getScopeForScv()));
JavaSendMailer sm = new JavaSendMailer(config);
MimeMessage msg = new MimeMessage(sm.getSession());

if (config.getSendmailMessage() != null && config.getSendmailProtocol() != null) {
Expand Down Expand Up @@ -136,10 +132,6 @@ public void setJavamailConfigDao(JavaMailConfigurationDao javamailConfigDao) {
m_JavamailConfigDao = javamailConfigDao;
}

public void setEntityScopeProvider(EntityScopeProvider entityScopeProvider) {
m_entityScopeProvider = entityScopeProvider;
}

/* (non-Javadoc)
* @see org.opennms.netmgt.reporting.service.ReportDeliveryService#reloadConfiguration()
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@

<bean id="deliveryService" class="org.opennms.netmgt.reporting.service.JavaMailDeliveryService">
<property name="javamailConfigDao" ref="javamailConfigDao" />
<property name="entityScopeProvider" ref="entityScopeProvider"/>
</bean>

<bean id="reportdConfigDao" class="org.opennms.netmgt.dao.jaxb.DefaultReportdConfigurationDao">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

import org.opennms.core.utils.PropertiesUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public abstract class JavaMailer2 {

private static final Logger LOG = LoggerFactory.getLogger(JavaMailer2.class);


private Session m_session = null;
private Properties m_mailProps;

Expand Down Expand Up @@ -96,11 +97,10 @@ public JavaMailer2() throws JavaMailerException {
*/
public Authenticator createAuthenticator(final String user, final String password) {
Authenticator auth;

auth = new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(user, password);
return new PasswordAuthentication(JavaMailerConfig.interpolate(user), JavaMailerConfig.interpolate(password));
}
};
return auth;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,31 +34,95 @@
import java.io.InputStream;
import java.util.Properties;

import org.opennms.core.mate.api.EntityScopeProvider;
import org.opennms.core.mate.api.Interpolator;
import org.opennms.core.mate.api.Scope;
import org.opennms.core.spring.BeanUtils;
import org.opennms.core.utils.ConfigFileConstants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.FatalBeanException;

/**
* Provides access to the default javamail configuration data.
*/
public abstract class JavaMailerConfig {

private static final Logger LOG = LoggerFactory.getLogger(JavaMailerConfig.class);

private static Scope secureCredentialsVaultScope;

private static synchronized Scope getSecureCredentialsScope() {
if (secureCredentialsVaultScope == null) {
try {
final EntityScopeProvider entityScopeProvider = BeanUtils.getBean("daoContext", "entityScopeProvider", EntityScopeProvider.class);

if (entityScopeProvider != null) {
secureCredentialsVaultScope = entityScopeProvider.getScopeForScv();
} else {
LOG.warn("JavaMailConfig: EntityScopeProvider is null, SecureCredentialsVault not available for metadata interpolation");
}
} catch (FatalBeanException e) {
e.printStackTrace();
LOG.warn("JavaMailConfig: Error retrieving EntityScopeProvider bean");
}
}

return secureCredentialsVaultScope;
}

public static void setSecureCredentialsVaultScope(final Scope secureCredentialsVaultScope) {
JavaMailerConfig.secureCredentialsVaultScope = secureCredentialsVaultScope;
}

/**
* This loads the configuration file.
*
* @return a Properties object representing the configuration properties
* @throws java.io.IOException if any.
*/
public static synchronized Properties getProperties() throws IOException {
LOG.debug("Loading javamail properties.");
LOG.debug("JavaMailConfig: Loading javamail properties");
Properties properties = new Properties();
File configFile = ConfigFileConstants.getFile(ConfigFileConstants.JAVA_MAIL_CONFIG_FILE_NAME);
InputStream in = new FileInputStream(configFile);
properties.load(in);
in.close();
return interpolate(properties);
}

private static Properties interpolate(final Properties properties, final String key, final Scope scope) {
final String value = properties.getProperty(key);

if (value != null) {
properties.put(key, Interpolator.interpolate(value, scope).output);
}

return properties;
}

private static Properties interpolate(final Properties properties) {
final Scope scope = getSecureCredentialsScope();

if (scope == null) {
LOG.warn("JavaMailConfig: Scope is null, cannot interpolate metadata of properties");
return properties;
}

interpolate(properties, "org.opennms.core.utils.authenticateUser", scope);
interpolate(properties, "org.opennms.core.utils.authenticatePassword", scope);

return properties;
}

public static String interpolate(final String string) {
final Scope scope = getSecureCredentialsScope();

if (scope == null) {
LOG.warn("JavaMailConfig: Scope is null, cannot interpolate metadata of string");
return string;
}

return Interpolator.interpolate(string, scope).output;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ public JavaReadMailer(final ReadmailConfig config, Boolean closeOnDelete) throws
m_deleteOnClose = closeOnDelete;
}
m_config = config;

final UserAuth userAuth = getUserAuth(config);
m_session = Session.getInstance(configureProperties(), createAuthenticator(userAuth.getUserName(), userAuth.getPassword()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ public Authenticator createAuthenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
if (m_config.getUserAuth() != null) {
final UserAuth userAuth = m_config.getUserAuth();
return new PasswordAuthentication(userAuth.getUserName(), userAuth.getPassword());
return new PasswordAuthentication(JavaMailerConfig.interpolate(userAuth.getUserName()), JavaMailerConfig.interpolate(userAuth.getPassword()));
}
LOG.debug("No user authentication configured.");
return new PasswordAuthentication(null,null);
Expand Down Expand Up @@ -362,7 +362,7 @@ public void send(MimeMessage message) throws JavaMailerException {
if (m_config.isUseAuthentication() && m_config.getUserAuth() != null) {
LOG.debug("authenticating to {}", sendmailHost.getHost());
final UserAuth userAuth = m_config.getUserAuth();
t.connect(sendmailHost.getHost(), sendmailHost.getPort(), userAuth.getUserName(), userAuth.getPassword());
t.connect(sendmailHost.getHost(), sendmailHost.getPort(), JavaMailerConfig.interpolate(userAuth.getUserName()), JavaMailerConfig.interpolate(userAuth.getPassword()));
} else {
LOG.debug("not authenticating to {}", sendmailHost.getHost());
t.connect(sendmailHost.getHost(), sendmailHost.getPort(), null, null);
Expand Down
Loading

0 comments on commit 625d1f0

Please sign in to comment.