Skip to content

Commit

Permalink
Merge pull request #123 from sebadob/nicer-ux-if-no-smtp-url-set
Browse files Browse the repository at this point in the history
handle a not set SMTP_URL nicely for "just testing out" use cases
  • Loading branch information
sebadob authored Oct 30, 2023
2 parents 2abb071 + e0360b1 commit 8b98d3c
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 17 deletions.
7 changes: 3 additions & 4 deletions rauthy-common/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,10 +163,9 @@ lazy_static! {
.expect("SMTP_USERNAME is not set")
.trim()
.to_string();
pub static ref SMTP_URL: String = env::var("SMTP_URL")
.expect("SMTP_URL is not set")
.trim()
.to_string();
pub static ref SMTP_URL: Option<String> = env::var("SMTP_URL")
.ok()
.map(|url| url.trim().to_string());
pub static ref SMTP_FROM: String = env::var("SMTP_FROM")
.expect("SMTP_FROM is not set")
.trim()
Expand Down
2 changes: 1 addition & 1 deletion rauthy-common/src/error_response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ impl From<sqlx::Error> for ErrorResponse {
sqlx::Error::Configuration(e) => (ErrorResponseType::Database, e.to_string()),
sqlx::Error::Database(e) => {
let s = e.to_string();
if s.contains("UNIQUE") {
if s.contains("duplicate key") || s.contains("UNIQUE") {
// basically returns http 400 on duplicate id column errors -> no distinct err type
(ErrorResponseType::BadRequest, s)
} else {
Expand Down
29 changes: 18 additions & 11 deletions rauthy-models/src/email.rs
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,11 @@ pub async fn sender(mut rx: Receiver<EMail>, test_mode: bool) {

// to make the integration tests not panic, results are taken and just thrown away
// not the nicest approach for now, but it works
if test_mode {
if test_mode || SMTP_URL.is_none() {
if SMTP_URL.is_none() {
error!("SMTP_URL is not configured, cannot send out any E-Mails!");
}

loop {
let req = rx.recv().await;
if req.is_some() {
Expand All @@ -413,10 +417,11 @@ pub async fn sender(mut rx: Receiver<EMail>, test_mode: bool) {
}
}

let smtp_url = SMTP_URL.as_deref().unwrap();
let mailer = {
let mut retries = 0;

let mut conn = connect_test_smtp().await;
let mut conn = connect_test_smtp(smtp_url).await;
while let Err(err) = conn {
error!("{:?}", err);

Expand All @@ -426,7 +431,7 @@ pub async fn sender(mut rx: Receiver<EMail>, test_mode: bool) {
retries += 1;
tokio::time::sleep(Duration::from_secs(5)).await;

conn = connect_test_smtp().await;
conn = connect_test_smtp(smtp_url).await;
}
conn.unwrap()
};
Expand Down Expand Up @@ -474,44 +479,46 @@ pub async fn sender(mut rx: Receiver<EMail>, test_mode: bool) {
}
}

async fn connect_test_smtp() -> Result<AsyncSmtpTransport<lettre::Tokio1Executor>, ErrorResponse> {
async fn connect_test_smtp(
smtp_url: &str,
) -> Result<AsyncSmtpTransport<lettre::Tokio1Executor>, ErrorResponse> {
let creds = authentication::Credentials::new(SMTP_USERNAME.clone(), SMTP_PASSWORD.clone());

// always try fully wrapped TLS first
let mut conn = AsyncSmtpTransport::<lettre::Tokio1Executor>::relay(&SMTP_URL)
let mut conn = AsyncSmtpTransport::<lettre::Tokio1Executor>::relay(smtp_url)
.expect("Connection Error with 'SMTP_URL'")
.credentials(creds.clone())
.timeout(Some(Duration::from_secs(10)))
.build();

match conn.test_connection().await {
Ok(true) => {
info!("Successfully connected to {} via TLS", *SMTP_URL);
info!("Successfully connected to {} via TLS", smtp_url);
}
Ok(false) | Err(_) => {
warn!(
"Could not connect to {} via TLS. Trying downgrade to STARTTLS",
*SMTP_URL,
smtp_url,
);

// only if full TLS fails, try STARTTLS
conn = AsyncSmtpTransport::<lettre::Tokio1Executor>::starttls_relay(&SMTP_URL)
conn = AsyncSmtpTransport::<lettre::Tokio1Executor>::starttls_relay(smtp_url)
.expect("Connection Error with 'SMTP_URL'")
.credentials(creds)
.timeout(Some(Duration::from_secs(10)))
.build();

match conn.test_connection().await {
Ok(true) => {
info!("Successfully connected to {} via STARTTLS", *SMTP_URL);
info!("Successfully connected to {} via STARTTLS", smtp_url);
}
Ok(false) | Err(_) => {
error!("Could not connect to {} via STARTTLS either", *SMTP_URL);
error!("Could not connect to {} via STARTTLS either", smtp_url);
return Err(ErrorResponse::new(
ErrorResponseType::Internal,
format!(
"Could not connect to {} - neither TLS nor STARTTLS worked",
*SMTP_URL
smtp_url
),
));
}
Expand Down
2 changes: 1 addition & 1 deletion rauthy.deploy.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ DATABASE_URL=sqlite::memory:

SMTP_USERNAME=
SMTP_PASSWORD=
SMTP_URL=smtp.gmail.com
SMTP_URL=smtp.change.me
# Format: "Rauthy <[email protected]>"
SMTP_FROM="Rauthy <[email protected]>"

Expand Down

0 comments on commit 8b98d3c

Please sign in to comment.