Add authentication options for SMTP server

* Update README with SMTP configurations
     * Update entrypoint.sh with new SMTP configurations
     * Add username,password with starttls authentication for smtpserver
This commit is contained in:
olblak 2017-06-02 15:17:53 +02:00
parent fa149f7287
commit 4fafa8a2f9
5 changed files with 49 additions and 2 deletions

View File

@ -50,7 +50,10 @@ _Require ssh tunnel to an ldap server and an WAR archive_
LDAP_NEW_USER_BASE_DN=ou=people,dc=jenkins-ci,dc=org
RECAPTCHA_PRIVATE_KEY=recaptcha_private_key
RECAPTCHA_PUBLIC_KEY=recaptcha_public_key
SMTP_SERVER=localhost
SMTP_SERVER=smtp.jenkins.io
SMTP_USER=user@jenkins.io
SMTP_AUTH=true
SMTP_PASSWORD=password
```
* Run docker-compose
```docker-compose up --build accountapp```
@ -89,6 +92,9 @@ we may want to use environment variable.
* RECAPTCHA_PUBLIC_KEY
* RECAPTCHA_PRIVATE_KEY
* SMTP_SERVER
* SMTP_USER
* SMTP_PASSWORD
* SMTP_AUTH
```
## Makefile
@ -96,3 +102,10 @@ we may want to use environment variable.
``` make build```: Build build/libs/accountapp-2.5.war and docker image
``` make run ```: Run docker container
``` make clean ```: Clean build environment
## SMTP
The accountapp support different types of SMTP configuration to send emails.
* Nothing is configured, the application try to connect on localhost:25
* SMTP_AUTH is set to false, the accountapp will connect on $SMTP_SERVER:25
* SMTP_AUTH is set to true, the accountapp will connect on $SMTP_SERVER:587 with tls authentication
and will use username: $SMTP_USER with password $SMTP_PASSWORD.

View File

@ -10,6 +10,9 @@ newUserBaseDN=LDAP_NEW_USER_BASE_DN
# Host which accountapp can use for sending out password reset and other emails
# Optional: Default value set to localhost
smtpServer=SMTP_SERVER
smtpUser=SMTP_USER
smtpAuth=SMTP_AUTH
smtpPassword=SMTP_PASSWORD
recaptchaPublicKey=RECAPTCHA_PUBLIC_KEY
recaptchaPrivateKey=RECAPTCHA_PRIVATE_KEY

View File

@ -11,6 +11,9 @@ init_config_properties() {
# /etc/accountapp/config.properties
: "${SMTP_SERVER:? SMTP Server required}"
: "${SMTP_USER:? SMTP User required}"
: "${SMTP_AUTH:? SMTP Auth required}"
: "${SMTP_PASSWORD:? SMTP Password required}"
: "${RECAPTCHA_PUBLIC_KEY:? Recaptcha private key}"
: "${RECAPTCHA_PRIVATE_KEY:? Recaptcha private key}"
: "${APP_URL:? Application url required}"
@ -35,6 +38,9 @@ init_config_properties() {
# Using # as variables may contain /
sed -i "s#SMTP_SERVER#$SMTP_SERVER#" /etc/accountapp/config.properties
sed -i "s#SMTP_USER#$SMTP_USER#" /etc/accountapp/config.properties
sed -i "s#SMTP_AUTH#$SMTP_AUTH#" /etc/accountapp/config.properties
sed -i "s#SMTP_PASSWORD#$SMTP_PASSWORD#" /etc/accountapp/config.properties
sed -i "s#LDAP_URL#$LDAP_URL#" /etc/accountapp/config.properties
sed -i "s#LDAP_PASSWORD#$LDAP_PASSWORD#" /etc/accountapp/config.properties
sed -i "s#RECAPTCHA_PUBLIC_KEY#$RECAPTCHA_PUBLIC_KEY#" /etc/accountapp/config.properties

View File

@ -26,6 +26,7 @@ import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.PasswordAuthentication;
import javax.naming.AuthenticationException;
import javax.naming.Context;
import javax.naming.NameAlreadyBoundException;
@ -487,9 +488,24 @@ public class Application {
}
private Session createJavaMailSession() {
Session session;
Properties props = new Properties(System.getProperties());
System.out.printf(params.smtpAuth());
props.put("mail.smtp.host",params.smtpServer());
return Session.getInstance(props);
if(params.smtpAuth().equals("true")) {
props.put("mail.smtp.auth", params.smtpAuth());
props.put("mail.smtp.starttls.enable", true);
props.put("mail.smtp.port", 587);
session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(params.smtpUser(), params.smtpPassword());
}
});
} else {
session = Session.getInstance(props);
}
return session;
}
/**

View File

@ -20,7 +20,16 @@ public interface Parameters {
String managerPassword();
String server();
/**
* smtpServer: The SMTP server to connect to.
* smtpUser: Default user name for SMTP.
* smtpAuth: If true, attempt to authenticate the user using the AUTH command.
* smtpPassword: SMTP password for SMTP server.
*/
String smtpServer();
String smtpUser();
String smtpAuth();
String smtpPassword();
String recaptchaPublicKey();
String recaptchaPrivateKey();