Skip to content

Commit

Permalink
feat: Move "ADM" router support behind a feature flag. (#629)
Browse files Browse the repository at this point in the history
Closes: SYNC-4151

Also kinda wanted this for the next PR.

---------

Co-authored-by: Philip Jenvey <pjenvey@underboss.org>
  • Loading branch information
jrconlin and pjenvey authored May 14, 2024
1 parent 61bf2f0 commit 38830f0
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 13 deletions.
1 change: 1 addition & 0 deletions autoendpoint/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,4 @@ bigtable = ["autopush_common/bigtable"]
dynamodb = ["autopush_common/dynamodb"]
dual = ["bigtable", "dynamodb"]
emulator = ["bigtable"]
adm = []
1 change: 1 addition & 0 deletions autoendpoint/src/extractors/router_data_input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ impl FromRequest for RouterDataInput {
RouterType::FCM | RouterType::GCM | RouterType::APNS => {
VALID_TOKEN.is_match(&data.token)
}
#[cfg(feature = "adm")]
RouterType::ADM => VALID_ADM_TOKEN.is_match(&data.token),
};

Expand Down
7 changes: 7 additions & 0 deletions autoendpoint/src/extractors/routers.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::error::{ApiError, ApiResult};
#[cfg(feature = "adm")]
use crate::routers::adm::router::AdmRouter;
use crate::routers::apns::router::ApnsRouter;
use crate::routers::fcm::router::FcmRouter;
Expand All @@ -21,6 +22,7 @@ pub enum RouterType {
FCM,
GCM,
APNS,
#[cfg(feature = "adm")]
ADM,
}

Expand All @@ -33,6 +35,7 @@ impl FromStr for RouterType {
"fcm" => Ok(RouterType::FCM),
"gcm" => Ok(RouterType::GCM),
"apns" => Ok(RouterType::APNS),
#[cfg(feature = "adm")]
"adm" => Ok(RouterType::ADM),
_ => Err(()),
}
Expand All @@ -46,6 +49,7 @@ impl Display for RouterType {
RouterType::FCM => "fcm",
RouterType::GCM => "gcm",
RouterType::APNS => "apns",
#[cfg(feature = "adm")]
RouterType::ADM => "adm",
})
}
Expand All @@ -57,6 +61,7 @@ pub struct Routers {
webpush: WebPushRouter,
fcm: Arc<FcmRouter>,
apns: Arc<ApnsRouter>,
#[cfg(feature = "adm")]
adm: Arc<AdmRouter>,
}

Expand All @@ -78,6 +83,7 @@ impl FromRequest for Routers {
},
fcm: app_state.fcm_router.clone(),
apns: app_state.apns_router.clone(),
#[cfg(feature = "adm")]
adm: app_state.adm_router.clone(),
})
}
Expand All @@ -90,6 +96,7 @@ impl Routers {
RouterType::WebPush => &self.webpush,
RouterType::FCM | RouterType::GCM => self.fcm.as_ref(),
RouterType::APNS => self.apns.as_ref(),
#[cfg(feature = "adm")]
RouterType::ADM => self.adm.as_ref(),
}
}
Expand Down
7 changes: 7 additions & 0 deletions autoendpoint/src/routers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use crate::error::ApiResult;
use crate::extractors::notification::Notification;
use crate::extractors::router_data_input::RouterDataInput;
#[cfg(feature = "adm")]
use crate::routers::adm::error::AdmError;
use crate::routers::apns::error::ApnsError;
use crate::routers::fcm::error::FcmError;
Expand All @@ -16,6 +17,7 @@ use autopush_common::errors::ReportableError;
use std::collections::HashMap;
use thiserror::Error;

#[cfg(feature = "adm")]
pub mod adm;
pub mod apns;
mod common;
Expand Down Expand Up @@ -74,6 +76,7 @@ impl From<RouterResponse> for HttpResponse {

#[derive(Debug, Error)]
pub enum RouterError {
#[cfg(feature = "adm")]
#[error(transparent)]
Adm(#[from] AdmError),

Expand Down Expand Up @@ -118,6 +121,7 @@ impl RouterError {
/// Get the associated HTTP status code
pub fn status(&self) -> StatusCode {
match self {
#[cfg(feature = "adm")]
RouterError::Adm(e) => e.status(),
RouterError::Apns(e) => e.status(),
RouterError::Fcm(e) => StatusCode::from_u16(e.status().as_u16()).unwrap_or_default(),
Expand All @@ -139,6 +143,7 @@ impl RouterError {
/// Get the associated error number
pub fn errno(&self) -> Option<usize> {
match self {
#[cfg(feature = "adm")]
RouterError::Adm(e) => e.errno(),
RouterError::Apns(e) => e.errno(),
RouterError::Fcm(e) => e.errno(),
Expand Down Expand Up @@ -168,6 +173,7 @@ impl RouterError {
// callbacks, whereas some are emitted via this method. These 2 should
// be consoliated: https://mozilla-hub.atlassian.net/browse/SYNC-3695
let err = match self {
#[cfg(feature = "adm")]
RouterError::Adm(AdmError::InvalidProfile | AdmError::NoProfile) => {
"notification.bridge.error.adm.profile"
}
Expand All @@ -189,6 +195,7 @@ impl RouterError {

pub fn is_sentry_event(&self) -> bool {
match self {
#[cfg(feature = "adm")]
RouterError::Adm(e) => !matches!(e, AdmError::InvalidProfile | AdmError::NoProfile),
// apns handle_error emits a metric for ApnsError::Unregistered
RouterError::Apns(ApnsError::SizeLimit(_))
Expand Down
25 changes: 14 additions & 11 deletions autoendpoint/src/routes/health.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
//! Health and Dockerflow routes
use std::collections::HashMap;
use std::thread;

use actix_web::{
Expand All @@ -17,18 +18,20 @@ use crate::server::AppState;
pub async fn health_route(state: Data<AppState>) -> Json<serde_json::Value> {
let router_health = interpret_table_health(state.db.router_table_exists().await);
let message_health = interpret_table_health(state.db.message_table_exists().await);
let mut routers: HashMap<&str, bool> = HashMap::new();
#[cfg(feature = "adm")]
routers.insert("adm", state.adm_router.active());
routers.insert("apns", state.apns_router.active());
routers.insert("fcm", state.fcm_router.active());

Json(json!({
"status": "OK",
"version": env!("CARGO_PKG_VERSION"),
"router_table": router_health,
"message_table": message_health,
"routers": {
"adm": state.adm_router.active(),
"apns": state.apns_router.active(),
"fcm": state.fcm_router.active(),
}
}))
let health = json!({
"status": "OK",
"version": env!("CARGO_PKG_VERSION"),
"router_table": router_health,
"message_table": message_health,
"routers": routers});

Json(health)
}

/// Convert the result of a DB health check to JSON
Expand Down
7 changes: 6 additions & 1 deletion autoendpoint/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ use autopush_common::{

use crate::error::{ApiError, ApiErrorKind, ApiResult};
use crate::metrics;
use crate::routers::{adm::router::AdmRouter, apns::router::ApnsRouter, fcm::router::FcmRouter};
#[cfg(feature = "adm")]
use crate::routers::adm::router::AdmRouter;
use crate::routers::{apns::router::ApnsRouter, fcm::router::FcmRouter};
use crate::routes::{
health::{health_route, lb_heartbeat_route, log_check, status_route, version_route},
registration::{
Expand All @@ -45,6 +47,7 @@ pub struct AppState {
pub http: reqwest::Client,
pub fcm_router: Arc<FcmRouter>,
pub apns_router: Arc<ApnsRouter>,
#[cfg(feature = "adm")]
pub adm_router: Arc<AdmRouter>,
}

Expand Down Expand Up @@ -117,6 +120,7 @@ impl Server {
)
.await?,
);
#[cfg(feature = "adm")]
let adm_router = Arc::new(AdmRouter::new(
settings.adm.clone(),
endpoint_url,
Expand All @@ -132,6 +136,7 @@ impl Server {
http,
fcm_router,
apns_router,
#[cfg(feature = "adm")]
adm_router,
};

Expand Down
3 changes: 3 additions & 0 deletions autoendpoint/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use fernet::{Fernet, MultiFernet};
use serde::Deserialize;
use url::Url;

#[cfg(feature = "adm")]
use crate::routers::adm::settings::AdmSettings;
use crate::routers::apns::settings::ApnsSettings;
use crate::routers::fcm::settings::FcmSettings;
Expand Down Expand Up @@ -42,6 +43,7 @@ pub struct Settings {

pub fcm: FcmSettings,
pub apns: ApnsSettings,
#[cfg(feature = "adm")]
pub adm: AdmSettings,
}

Expand Down Expand Up @@ -71,6 +73,7 @@ impl Default for Settings {
statsd_label: "autoendpoint".to_string(),
fcm: FcmSettings::default(),
apns: ApnsSettings::default(),
#[cfg(feature = "adm")]
adm: AdmSettings::default(),
}
}
Expand Down
2 changes: 1 addition & 1 deletion autopush-common/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ pub enum ApcErrorKind {
#[error("Database Error: {0}")]
DatabaseError(String),

// TODO: option this.
#[cfg(feature = "dynamodb")]
#[error("Rusoto Error: {0}")]
RusotoError(String),
}
Expand Down

0 comments on commit 38830f0

Please sign in to comment.