Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,9 @@ jobs:
- stage: fit
name: "Integration Tests: Wildfly / H2 / JSON Content-Type"
script: mvn -f fit/core-reference/pom.xml -P wildfly-it -Dinvoker.streamLogs=true -Dmodernizer.skip=true -Dianal.skip=true -Drat.skip=true -Dcheckstyle.skip=true -Djacoco.skip=true
#- stage: fit
#name: "Integration Tests: Payara / H2 / JSON Content-Type"
#script: mvn -f fit/core-reference/pom.xml -P payara-it -Dinvoker.streamLogs=true -Dmodernizer.skip=true -Dianal.skip=true -Drat.skip=true -Dcheckstyle.skip=true -Djacoco.skip=true
- stage: fit
name: "Integration Tests: Payara / H2 / JSON Content-Type"
script: mvn -f fit/core-reference/pom.xml -P payara-it -Dinvoker.streamLogs=true -Dmodernizer.skip=true -Dianal.skip=true -Drat.skip=true -Dcheckstyle.skip=true -Djacoco.skip=true
- stage: fit
name: "Integration Tests: Tomcat / PostgreSQL / JSON Content-Type"
script: mvn -f fit/core-reference/pom.xml -P postgres-it -Dinvoker.streamLogs=true -Dmodernizer.skip=true -Dianal.skip=true -Drat.skip=true -Dcheckstyle.skip=true -Djacoco.skip=true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,30 @@

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import javax.servlet.ReadListener;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.ws.rs.core.HttpHeaders;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.apache.syncope.common.rest.api.batch.BatchRequestItem;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.MediaType;

public class BatchItemRequest extends HttpServletRequestWrapper {

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

private final String basePath;

private final BatchRequestItem batchItem;
Expand Down Expand Up @@ -85,6 +95,28 @@ public String getMethod() {
return batchItem.getMethod();
}

@Override
public String getServerName() {
try {
return super.getServerName();
} catch (Exception e) {
LOG.debug("While delegating to wrapped request", e);
return Optional.ofNullable(getHeader(HttpHeaders.HOST)).
map(host -> StringUtils.substringBefore(host, ":")).orElse(null);
}
}

@Override
public int getServerPort() {
try {
return super.getServerPort();
} catch (Exception e) {
LOG.debug("While delegating to wrapped request", e);
return Optional.ofNullable(getHeader(HttpHeaders.HOST)).
map(host -> NumberUtils.toInt(StringUtils.substringAfter(host, ":"))).orElse(0);
}
}

@Override
public StringBuffer getRequestURL() {
return new StringBuffer(basePath).append(getRequestURI());
Expand Down Expand Up @@ -121,21 +153,65 @@ public long getContentLengthLong() {

@Override
public String getHeader(final String name) {
return batchItem.getHeaders().containsKey(name)
? batchItem.getHeaders().get(name).get(0).toString()
: HttpHeaders.CONTENT_TYPE.equals(name) || HttpHeaders.ACCEPT.equals(name)
? MediaType.ALL_VALUE
: super.getHeader(name);
try {
return batchItem.getHeaders().containsKey(name)
? batchItem.getHeaders().get(name).get(0).toString()
: HttpHeaders.CONTENT_TYPE.equals(name) || HttpHeaders.ACCEPT.equals(name)
? MediaType.ALL_VALUE
: super.getHeader(name);
} catch (Exception e) {
LOG.debug("While delegating to wrapped request", e);
return null;
}
}

@Override
public Enumeration<String> getHeaders(final String name) {
return batchItem.getHeaders().containsKey(name)
? Collections.enumeration(
batchItem.getHeaders().get(name).stream().map(Object::toString).collect(Collectors.toList()))
: HttpHeaders.CONTENT_TYPE.equals(name) || HttpHeaders.ACCEPT.equals(name)
? Collections.enumeration(List.of(MediaType.ALL_VALUE))
: super.getHeaders(name);
try {
return batchItem.getHeaders().containsKey(name)
? Collections.enumeration(batchItem.getHeaders().get(name).stream().
map(Object::toString).collect(Collectors.toList()))
: HttpHeaders.CONTENT_TYPE.equals(name) || HttpHeaders.ACCEPT.equals(name)
? Collections.enumeration(List.of(MediaType.ALL_VALUE))
: super.getHeaders(name);
} catch (Exception e) {
LOG.debug("While delegating to wrapped request", e);
return Collections.emptyEnumeration();
}
}

@Override
public Enumeration<String> getHeaderNames() {
try {
return super.getHeaderNames();
} catch (Exception e) {
LOG.debug("While delegating to wrapped request", e);

Set<String> names = new HashSet<>(batchItem.getHeaders().keySet());
names.add(HttpHeaders.CONTENT_TYPE);
names.add(HttpHeaders.ACCEPT);
return Collections.enumeration(names);
}
}

@Override
public Object getAttribute(final String name) {
try {
return super.getAttribute(name);
} catch (Exception e) {
LOG.debug("While delegating to wrapped request", e);
return null;
}
}

@Override
public String getCharacterEncoding() {
try {
return super.getCharacterEncoding();
} catch (Exception e) {
LOG.debug("While delegating to wrapped request", e);
return StandardCharsets.UTF_8.name();
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,23 @@
*/
package org.apache.syncope.core.provisioning.java;

import java.io.PrintStream;
import java.lang.reflect.InvocationTargetException;
import java.nio.charset.StandardCharsets;
import java.util.Enumeration;
import java.util.Properties;
import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;
import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.naming.NamingException;
import javax.sql.DataSource;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.syncope.common.lib.LogOutputStream;
import org.apache.syncope.common.lib.PropertyUtils;
import org.apache.syncope.core.provisioning.api.AnyObjectProvisioningManager;
import org.apache.syncope.core.provisioning.api.AuditManager;
import org.apache.syncope.core.provisioning.api.ConnIdBundleManager;
Expand All @@ -41,6 +51,8 @@
import org.apache.syncope.core.provisioning.java.job.SchedulerDBInit;
import org.apache.syncope.core.provisioning.java.job.SchedulerShutdown;
import org.apache.syncope.core.provisioning.java.propagation.PropagationManagerImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.ApplicationContext;
Expand All @@ -55,6 +67,7 @@
import org.springframework.core.env.Environment;
import org.springframework.core.io.ClassPathResource;
import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator;
import org.springframework.jndi.JndiObjectFactoryBean;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.scheduling.annotation.AsyncConfigurer;
Expand All @@ -74,6 +87,8 @@
@Configuration
public class ProvisioningContext implements EnvironmentAware, AsyncConfigurer {

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

@Resource(name = "MasterDataSource")
private DataSource masterDataSource;

Expand Down Expand Up @@ -188,14 +203,74 @@ public JobManager jobManager() {

@ConditionalOnMissingBean
@Bean
public JavaMailSender mailSender() {
JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
public JavaMailSender mailSender() throws IllegalArgumentException, NamingException {
JavaMailSenderImpl mailSender = new JavaMailSenderImpl() {

@Override
protected Transport connectTransport() throws MessagingException {
// ensure that no auth means no auth
if (StringUtils.isBlank(getUsername())) {
Transport transport = getTransport(getSession());
transport.connect(getHost(), getPort(), null, null);
return transport;
}

return super.connectTransport();
}
};
mailSender.setDefaultEncoding(env.getProperty("smtpEncoding"));
mailSender.setHost(env.getProperty("smtpHost"));
mailSender.setPort(env.getProperty("smtpPort", Integer.class));
mailSender.setUsername(env.getProperty("smtpUsername"));
mailSender.setPassword(env.getProperty("smtpPassword"));
mailSender.setProtocol(env.getProperty("smtpProtocol"));

if (LOG.isDebugEnabled()) {
LOG.debug("[Mail] host:port = {}:{}", mailSender.getHost(), mailSender.getPort());
LOG.debug("[Mail] protocol = {}", mailSender.getProtocol());
LOG.debug("[Mail] username = {}", mailSender.getUsername());
LOG.debug("[Mail] default encoding = {}", mailSender.getDefaultEncoding());
}

JndiObjectFactoryBean mailSession = new JndiObjectFactoryBean();
mailSession.setJndiName("mail/syncopeNotification");
try {
mailSession.afterPropertiesSet();
} catch (NamingException e) {
LOG.debug("While looking up JNDI for mail session", e);
}

Session session = (Session) mailSession.getObject();
if (session == null) {
Properties javaMailProperties = mailSender.getJavaMailProperties();

Properties props = PropertyUtils.read(ProvisioningContext.class, "mail.properties", "conf.directory");
for (Enumeration<?> e = props.propertyNames(); e.hasMoreElements();) {
String prop = (String) e.nextElement();
if (prop.startsWith("mail.smtp.")) {
javaMailProperties.setProperty(prop, props.getProperty(prop));
}
}

if (StringUtils.isNotBlank(mailSender.getUsername())) {
javaMailProperties.setProperty("mail.smtp.auth", "true");
}

if (LOG.isDebugEnabled()) {
mailSender.getJavaMailProperties().
forEach((key, value) -> LOG.debug("[Mail] property: {} = {}", key, value));
}

String mailDebug = props.getProperty("mail.debug", "false");
if (BooleanUtils.toBoolean(mailDebug)) {
session = mailSender.getSession();
session.setDebug(true);
session.setDebugOut(new PrintStream(new LogOutputStream(LOG)));
}
} else {
mailSender.setSession(session);
}

return mailSender;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,11 @@
*/
package org.apache.syncope.core.provisioning.java.job.notification;

import java.io.PrintStream;
import java.util.Date;
import java.util.Enumeration;
import java.util.List;
import java.util.Properties;
import java.util.concurrent.atomic.AtomicReference;
import javax.mail.Session;
import javax.mail.internet.MimeMessage;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.syncope.common.lib.LogOutputStream;
import org.apache.syncope.common.lib.PropertyUtils;
import org.apache.syncope.common.lib.types.AuditElements;
import org.apache.syncope.common.lib.types.TaskType;
import org.apache.syncope.common.lib.types.TraceLevel;
Expand All @@ -42,20 +35,17 @@
import org.apache.syncope.core.provisioning.api.notification.NotificationJobDelegate;
import org.apache.syncope.core.provisioning.api.notification.NotificationManager;
import org.apache.syncope.core.spring.security.AuthContextUtils;
import org.apache.syncope.core.spring.security.Encryptor;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

@Component
public class DefaultNotificationJobDelegate implements InitializingBean, NotificationJobDelegate {
public class DefaultNotificationJobDelegate implements NotificationJobDelegate {

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

Expand All @@ -80,36 +70,6 @@ public class DefaultNotificationJobDelegate implements InitializingBean, Notific

private boolean interrupted;

@Override
public void afterPropertiesSet() throws Exception {
if (mailSender instanceof JavaMailSenderImpl) {
JavaMailSenderImpl javaMailSender = (JavaMailSenderImpl) mailSender;

Properties javaMailProperties = javaMailSender.getJavaMailProperties();

Properties props = PropertyUtils.read(Encryptor.class, "mail.properties", "conf.directory");
for (Enumeration<?> e = props.propertyNames(); e.hasMoreElements();) {
String prop = (String) e.nextElement();
if (prop.startsWith("mail.smtp.")) {
javaMailProperties.setProperty(prop, props.getProperty(prop));
}
}

if (StringUtils.isNotBlank(javaMailSender.getUsername())) {
javaMailProperties.setProperty("mail.smtp.auth", "true");
}

javaMailSender.setJavaMailProperties(javaMailProperties);

String mailDebug = props.getProperty("mail.debug", "false");
if (BooleanUtils.toBoolean(mailDebug)) {
Session session = javaMailSender.getSession();
session.setDebug(true);
session.setDebugOut(new PrintStream(new LogOutputStream(LOG)));
}
}
}

@Override
public String currentStatus() {
return status.get();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ protected boolean authenticate(final User user, final String password) {
Optional<String> connObjectKeyValue = mappingManager.getConnObjectKeyValue(user, provision.get());
if (connObjectKeyValue.isEmpty()) {
throw new AccountNotFoundException(
"Unable to locate conn object key value for " + userType.getKey());
"Unable to locate conn object key value for " + userType.getKey());
}
connObjectKey = connObjectKeyValue.get();
Uid uid = connFactory.getConnector(resource).authenticate(connObjectKey, password, null);
Expand Down Expand Up @@ -405,8 +405,7 @@ public Pair<String, Set<SyncopeGrantedAuthority>> authenticate(final JWTAuthenti

if (BooleanUtils.isTrue(user.isMustChangePassword())) {
LOG.debug("User {} must change password, resetting authorities", username);
authorities = Set.of(
new SyncopeGrantedAuthority(IdRepoEntitlement.MUST_CHANGE_PASSWORD));
authorities = Set.of(new SyncopeGrantedAuthority(IdRepoEntitlement.MUST_CHANGE_PASSWORD));
}
}

Expand Down
Loading