Skip to content

Commit

Permalink
Add authentication options for SMTP server
Browse files Browse the repository at this point in the history
     * Update README with SMTP configurations
     * Update entrypoint.sh with new SMTP configurations
     * Add username,password with starttls authentication for smtpserver
  • Loading branch information
olblak committed Jun 2, 2017
1 parent fa149f7 commit 4fafa8a
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 2 deletions.
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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```
Expand Down Expand Up @@ -89,10 +92,20 @@ we may want to use environment variable.
* RECAPTCHA_PUBLIC_KEY
* RECAPTCHA_PRIVATE_KEY
* SMTP_SERVER
* SMTP_USER
* SMTP_PASSWORD
* SMTP_AUTH
```

## Makefile

``` 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.
3 changes: 3 additions & 0 deletions config.properties.example
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand All @@ -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
Expand Down
18 changes: 17 additions & 1 deletion src/main/java/org/jenkinsci/account/Application.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
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;
Expand Down Expand Up @@ -487,9 +488,24 @@ public void delete(DirContext con) throws NamingException {
}

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;
}

/**
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/org/jenkinsci/account/Parameters.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 4fafa8a

Please sign in to comment.