Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Move sentry from backend to core #1511

Merged
merged 2 commits into from
Sep 29, 2024
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
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion backend/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ publish = false

[dependencies]
async-smtp = "0.6"
check-if-email-exists = { path = "../core", features = ["headless"] }
check-if-email-exists = { path = "../core", features = ["headless", "sentry"] }
csv = "1.3.0"
dotenv = "0.15.0"
openssl = { version = "0.10.64", features = ["vendored"] }
Expand Down
36 changes: 2 additions & 34 deletions backend/src/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,42 +16,10 @@

//! This file contains shared logic for checking one email.

use std::{env, time::Duration};
use std::env;

use check_if_email_exists::{check_email as ciee_check_email, CheckEmailInput, CheckEmailOutput};
use warp::Filter;

use super::sentry_util;

/// Same as `check-if-email-exists`'s check email, but adds some additional
/// inputs and error handling.
pub async fn check_email(input: CheckEmailInput) -> CheckEmailOutput {
let from_email =
env::var("RCH_FROM_EMAIL").unwrap_or_else(|_| CheckEmailInput::default().from_email);
let hello_name =
env::var("RCH_HELLO_NAME").unwrap_or_else(|_| CheckEmailInput::default().hello_name);
let smtp_timeout = env::var("RCH_SMTP_TIMEOUT")
.ok()
.and_then(|s| s.parse::<u64>().ok())
.map(Duration::from_secs)
.or_else(|| CheckEmailInput::default().smtp_timeout);

let input = CheckEmailInput {
// If we want to override core check-if-email-exists's default values
// for CheckEmailInput for the backend, we do it here.
from_email,
hello_name,
smtp_timeout,
..input
};

let res = ciee_check_email(&input).await;

sentry_util::log_unknown_errors(&res);

res
}

/// The header which holds the Reacher backend secret.
pub const REACHER_SECRET_HEADER: &str = "x-reacher-secret";

Expand All @@ -68,7 +36,7 @@ pub fn check_header() -> warp::filters::BoxedFilter<()> {

let secret: &'static str = Box::leak(Box::new(secret));

warp::header::exact("x-reacher-secret", secret).boxed()
warp::header::exact(REACHER_SECRET_HEADER, secret).boxed()
}
Err(_) => warp::any().boxed(),
}
Expand Down
8 changes: 4 additions & 4 deletions backend/src/http/v0/bulk/task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@

//! This file implements the `POST /bulk` endpoint.

use check_if_email_exists::LOG_TARGET;
use check_if_email_exists::{CheckEmailInput, CheckEmailInputProxy, CheckEmailOutput, Reachable};
use check_if_email_exists::{
check_email, CheckEmailInput, CheckEmailInputProxy, CheckEmailOutput, Reachable, LOG_TARGET,
};
use serde::{Deserialize, Serialize};
use sqlx::{Pool, Postgres};
use sqlxmq::{job, CurrentJob};
Expand All @@ -26,7 +27,6 @@ use tracing::{debug, error};
use uuid::Uuid;

use super::error::BulkError;
use crate::check::check_email;

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct TaskInput {
Expand Down Expand Up @@ -159,7 +159,7 @@ pub async fn email_verification_task(
);

let to_email = check_email_input.to_email.clone();
let response = check_email(check_email_input).await;
let response = check_email(&check_email_input).await;

debug!(
target: LOG_TARGET,
Expand Down
7 changes: 3 additions & 4 deletions backend/src/http/v0/check_email/post.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,10 @@

//! This file implements the `POST /v0/check_email` endpoint.

use check_if_email_exists::CheckEmailInput;
use check_if_email_exists::LOG_TARGET;
use check_if_email_exists::{check_email, CheckEmailInput, LOG_TARGET};
use warp::{http, Filter};

use crate::check::{check_email, check_header};
use crate::check::check_header;
use crate::errors;

/// The main endpoint handler that implements the logic of this route.
Expand All @@ -33,7 +32,7 @@ async fn handler(body: CheckEmailInput) -> Result<impl warp::Reply, warp::Reject
}))
} else {
// Run the future to check an email.
Ok(warp::reply::json(&check_email(body).await))
Ok(warp::reply::json(&check_email(&body).await))
}
}

Expand Down
4 changes: 2 additions & 2 deletions backend/src/http/version/get.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

//! This file implements the `GET /version` endpoint.

use crate::sentry_util::CARGO_PKG_VERSION;
use crate::CARGO_PKG_VERSION;
use serde::{Deserialize, Serialize};
use warp::Filter;

Expand All @@ -39,7 +39,7 @@ pub fn get_version() -> impl Filter<Extract = (impl warp::Reply,), Error = warp:
#[cfg(test)]
mod tests {
use super::get_version;
use crate::sentry_util::CARGO_PKG_VERSION;
use crate::CARGO_PKG_VERSION;
use warp::http::StatusCode;
use warp::test::request;

Expand Down
3 changes: 2 additions & 1 deletion backend/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@
pub mod check;
mod errors;
pub mod http;
pub mod sentry_util;

const CARGO_PKG_VERSION: &str = env!("CARGO_PKG_VERSION");
9 changes: 4 additions & 5 deletions backend/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,12 @@
//! Main entry point of the `reacher_backend` binary. It has two `main`
//! functions, depending on whether the `bulk` feature is enabled or not.

use check_if_email_exists::LOG_TARGET;
use check_if_email_exists::{setup_sentry, LOG_TARGET};
use tracing::info;

use reacher_backend::{
http::run_warp_server,
sentry_util::{setup_sentry, CARGO_PKG_VERSION},
};
use reacher_backend::http::run_warp_server;

const CARGO_PKG_VERSION: &str = env!("CARGO_PKG_VERSION");

/// Run a HTTP server using warp with bulk endpoints.
#[tokio::main]
Expand Down
2 changes: 2 additions & 0 deletions core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@ pwned = "0.5.0"
rand = { version = "0.8.5", features = ["small_rng"] }
regex = "1.10.2"
reqwest = { version = "0.12.5", features = ["json", "socks"] }
sentry = { version = "0.23", optional = true }
serde = { version = "1.0.195", features = ["derive"] }
serde_json = "1.0.124"
tracing = "0.1.40"

[dev-dependencies]
tokio = { version = "1.35.1" }
Expand Down
11 changes: 9 additions & 2 deletions core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@
//! - Email deliverability: Is an email sent to this address deliverable?
//! - Syntax validation. Is the address syntactically valid?
//! - DNS records validation. Does the domain of the email address have valid
//! MX DNS records?

Check warning on line 26 in core/src/lib.rs

View workflow job for this annotation

GitHub Actions / lints

doc list item without indentation
//! - Disposable email address (DEA) validation. Is the address provided by a
//! known disposable email address provider?

Check warning on line 28 in core/src/lib.rs

View workflow job for this annotation

GitHub Actions / lints

doc list item without indentation
//! - SMTP server validation. Can the mail exchanger of the email address
//! domain be contacted successfully?

Check warning on line 30 in core/src/lib.rs

View workflow job for this annotation

GitHub Actions / lints

doc list item without indentation
//! - Mailbox disabled. Has this email address been disabled by the email
//! provider?

Check warning on line 32 in core/src/lib.rs

View workflow job for this annotation

GitHub Actions / lints

doc list item without indentation
//! - Full inbox. Is the inbox of this mailbox full?
//! - Catch-all address. Is this email address a catch-all address?
//!
Expand Down Expand Up @@ -79,6 +79,8 @@
use syntax::{check_syntax, get_similar_mail_provider};
pub use util::constants::LOG_TARGET;
pub use util::input_output::*;
#[cfg(feature = "sentry")]
pub use util::sentry::*;

use crate::rules::{has_rule, Rule};

Expand Down Expand Up @@ -246,7 +248,7 @@
}

let end_time = SystemTime::now();
CheckEmailOutput {
let res = CheckEmailOutput {
input: to_email.to_string(),
is_reachable: calculate_reachable(&my_misc, &my_smtp),
misc: Ok(my_misc),
Expand All @@ -262,5 +264,10 @@
smtp: smtp_debug,
..Default::default()
},
}
};

#[cfg(feature = "sentry")]
log_unknown_errors(&res);

res
}
11 changes: 8 additions & 3 deletions core/src/util/input_output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,12 +237,17 @@ impl Default for CheckEmailInput {
fn default() -> Self {
CheckEmailInput {
to_email: "".into(),
from_email: "reacher.email@gmail.com".into(), // Unused, owned by Reacher
hello_name: "gmail.com".into(),
from_email: env::var("RCH_FROM_EMAIL")
.unwrap_or_else(|_| "reacher.email@gmail.com".into()), // Unused, owned by Reacher
hello_name: env::var("RCH_HELLO_NAME").unwrap_or_else(|_| "gmail.com".into()),
proxy: None,
smtp_port: 25,
smtp_security: SmtpSecurity::default(),
smtp_timeout: Some(Duration::from_secs(45)),
smtp_timeout: env::var("RCH_SMTP_TIMEOUT")
.ok()
.and_then(|s| s.parse::<u64>().ok())
.map(Duration::from_secs)
.or(Some(Duration::from_secs(30))),
#[cfg(not(feature = "headless"))]
yahoo_verif_method: YahooVerifMethod::Api,
#[cfg(feature = "headless")]
Expand Down
2 changes: 2 additions & 0 deletions core/src/util/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,6 @@

pub mod constants;
pub mod input_output;
#[cfg(feature = "sentry")]
pub mod sentry;
pub mod ser_with_display;
17 changes: 8 additions & 9 deletions backend/src/sentry_util.rs → core/src/util/sentry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,15 @@
use std::env;

use async_smtp::smtp::error::Error as AsyncSmtpError;
use check_if_email_exists::misc::MiscError;
use check_if_email_exists::mx::MxError;
use check_if_email_exists::LOG_TARGET;
use check_if_email_exists::{smtp::SmtpError, CheckEmailOutput};
use sentry::protocol::{Event, Exception, Level, Values};
use tracing::{debug, info};

use super::sentry_util;
use crate::misc::MiscError;
use crate::mx::MxError;
use crate::LOG_TARGET;
use crate::{smtp::SmtpError, CheckEmailOutput};

pub const CARGO_PKG_VERSION: &str = env!("CARGO_PKG_VERSION");
const CARGO_PKG_VERSION: &str = env!("CARGO_PKG_VERSION");

/// Setup Sentry.
pub fn setup_sentry() -> sentry::ClientInitGuard {
Expand Down Expand Up @@ -64,9 +63,9 @@
enum SentryError<'a> {
// TODO: Probably a good idea would be to `impl std:error:Error` for the
// three errors below.
Misc(&'a MiscError),

Check warning on line 66 in core/src/util/sentry.rs

View workflow job for this annotation

GitHub Actions / lints

field `0` is never read

Check warning on line 66 in core/src/util/sentry.rs

View workflow job for this annotation

GitHub Actions / check

field `0` is never read

Check warning on line 66 in core/src/util/sentry.rs

View workflow job for this annotation

GitHub Actions / test

field `0` is never read
Mx(&'a MxError),

Check warning on line 67 in core/src/util/sentry.rs

View workflow job for this annotation

GitHub Actions / lints

field `0` is never read

Check warning on line 67 in core/src/util/sentry.rs

View workflow job for this annotation

GitHub Actions / check

field `0` is never read

Check warning on line 67 in core/src/util/sentry.rs

View workflow job for this annotation

GitHub Actions / test

field `0` is never read
Smtp(&'a SmtpError),

Check warning on line 68 in core/src/util/sentry.rs

View workflow job for this annotation

GitHub Actions / lints

field `0` is never read

Check warning on line 68 in core/src/util/sentry.rs

View workflow job for this annotation

GitHub Actions / check

field `0` is never read

Check warning on line 68 in core/src/util/sentry.rs

View workflow job for this annotation

GitHub Actions / test

field `0` is never read
}

impl<'a> SentryError<'a> {
Expand Down Expand Up @@ -128,11 +127,11 @@
match (&result.misc, &result.mx, &result.smtp) {
(Err(err), _, _) => {
// We log all misc errors.
sentry_util::error(SentryError::Misc(err), result);
error(SentryError::Misc(err), result);
}
(_, Err(err), _) => {
// We log all mx errors.
sentry_util::error(SentryError::Mx(err), result);
error(SentryError::Mx(err), result);
}
(_, _, Err(err)) if err.get_description().is_some() => {
// If the SMTP error is known, we don't track it in Sentry.
Expand All @@ -155,7 +154,7 @@
// Sentry, to be able to debug them better. We don't want to
// spam Sentry and log all instances of the error, hence the
// `count` check.
sentry_util::error(SentryError::Smtp(err), result);
error(SentryError::Smtp(err), result);
}
// If everything is ok, we just return the result.
(Ok(_), Ok(_), Ok(_)) => {}
Expand Down
Loading