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

feat: Suspend Broken Tenants & Delete Broken Clients #177

Merged
merged 5 commits into from
Aug 9, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
5 changes: 5 additions & 0 deletions migrations/1691518766_add-suspension.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
alter table public.tenants
add suspended bool not null default false;

alter table public.tenants
add suspended_reason text;
chris13524 marked this conversation as resolved.
Show resolved Hide resolved
1 change: 0 additions & 1 deletion migrations/new.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
#!/bin/bash
DESCRIPTION=$1
touch "./$(date +%s)_$DESCRIPTION.sql"
18 changes: 18 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,12 @@ pub enum Error {

#[error("invalid apns creds")]
BadApnsCredentials,

#[error("invalid device token")]
HarryET marked this conversation as resolved.
Show resolved Hide resolved
ClientDeleted,

#[error("invalid tenant configuration")]
HarryET marked this conversation as resolved.
Show resolved Hide resolved
TenantSuspended,
}

impl IntoResponse for Error {
Expand Down Expand Up @@ -525,6 +531,18 @@ impl IntoResponse for Error {
location: ErrorLocation::Path,
}
]),
Error::ClientDeleted => crate::handlers::Response::new_failure(StatusCode::ACCEPTED, vec![
ResponseError {
name: "client_deleted".to_string(),
message: "Request Accepted, client deleted due to invalid token".to_string(),
},
], vec![]),
Error::TenantSuspended => crate::handlers::Response::new_failure(StatusCode::ACCEPTED, vec![
ResponseError {
name: "tenant_suspended".to_string(),
message: "Request Accepted, tenant suspended due to invalid configuration".to_string(),
},
], vec![]),
e => {
warn!("Error does not have response clause, {:?}", e);

Expand Down
4 changes: 4 additions & 0 deletions src/handlers/get_tenant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ pub struct GetTenantResponse {
enabled_providers: Vec<String>,
apns_topic: Option<String>,
apns_type: Option<ApnsType>,
suspended: bool,
suspended_reason: Option<String>,
}

pub async fn handler(
Expand Down Expand Up @@ -64,6 +66,8 @@ pub async fn handler(
enabled_providers: tenant.providers().iter().map(Into::into).collect(),
apns_topic: None,
apns_type: None,
suspended: tenant.suspended,
suspended_reason: tenant.suspended_reason,
};

if providers.contains(&ProviderKind::Apns) {
Expand Down
31 changes: 27 additions & 4 deletions src/handlers/push_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@
return Err((Error::MissmatchedTenantId, analytics));
}

return Err((Error::MissmatchedTenantId, None));

Check warning on line 245 in src/handlers/push_message.rs

View workflow job for this annotation

GitHub Actions / [ubuntu-latest/rust-stable] Unit Tests

unreachable statement
}
}

Expand Down Expand Up @@ -346,6 +346,10 @@
"fetched tenant"
);

if tenant.suspended {
return Err((Error::TenantSuspended, analytics.clone()));
}

let mut provider = tenant
.provider(&client.push_type)
.map_err(|e| (e, analytics.clone()))?;
Expand All @@ -358,10 +362,29 @@
"fetched provider"
);

provider
.send_notification(client.token, body.payload)
.await
.map_err(|e| (e, analytics.clone()))?;
match provider.send_notification(client.token, body.payload).await {
Ok(_) => Ok(()),
Err(error) => match error {
Error::BadDeviceToken => {
state.client_store.delete_client(&tenant_id, &id);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this a soft delete?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No its a hard delete as the data we have stored is now useless, we still have data about the client in datalake

Err(Error::ClientDeleted)
}
Error::BadApnsCredentials => {
state
.tenant_store
.suspend_tenant(&tenant_id, "Invalid APNS Credentials");
Err(Error::TenantSuspended)
}
Error::BadFcmApiKey => {
state
.tenant_store
.suspend_tenant(&tenant_id, "Invalid FCM Credentials");
Err(Error::TenantSuspended)
}
e => Err(e),
},
}
.map_err(|e| (e, analytics.clone()))?;

info!(
%request_id,
Expand Down Expand Up @@ -390,5 +413,5 @@
return Ok(((StatusCode::ACCEPTED).into_response(), analytics));
}

Ok(((StatusCode::ACCEPTED).into_response(), None))

Check warning on line 416 in src/handlers/push_message.rs

View workflow job for this annotation

GitHub Actions / [ubuntu-latest/rust-stable] Unit Tests

unreachable expression
}
33 changes: 27 additions & 6 deletions src/providers/fcm.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use {
crate::{
blob::DecryptedPayloadBlob,
error::Error,
handlers::push_message::MessagePayload,
providers::PushProvider,
},
async_trait::async_trait,
fcm::{MessageBuilder, NotificationBuilder},
fcm::{ErrorReason, FcmError, FcmResponse, MessageBuilder, NotificationBuilder},
std::fmt::{Debug, Formatter},
tracing::span,
};
Expand Down Expand Up @@ -36,12 +37,12 @@ impl PushProvider for FcmProvider {

let mut message_builder = MessageBuilder::new(self.api_key.as_str(), token.as_str());

if payload.is_encrypted() {
let result = if payload.is_encrypted() {
message_builder.data(&payload)?;

let fcm_message = message_builder.finalize();

let _ = self.client.send(fcm_message).await?;
self.client.send(fcm_message).await
} else {
let blob = DecryptedPayloadBlob::from_base64_encoded(payload.clone().blob)?;

Expand All @@ -55,10 +56,30 @@ impl PushProvider for FcmProvider {

let fcm_message = message_builder.finalize();

let _ = self.client.send(fcm_message).await?;
self.client.send(fcm_message).await
};

match result {
Ok(val) => match val {
FcmResponse { error, .. } => {
if let Some(error) = error {
match error {
ErrorReason::MissingRegistration
| ErrorReason::InvalidRegistration
| ErrorReason::NotRegistered => Err(Error::BadDeviceToken),
ErrorReason::InvalidApnsCredential => Err(Error::BadApnsCredentials),
_ => Ok(()),
HarryET marked this conversation as resolved.
Show resolved Hide resolved
}
} else {
Ok(())
}
}
},
Err(e) => match e {
FcmError::Unauthorized => Err(Error::BadFcmApiKey),
_ => Ok(()),
HarryET marked this conversation as resolved.
Show resolved Hide resolved
},
}

Ok(())
}
}

Expand Down
21 changes: 21 additions & 0 deletions src/stores/tenant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@
pub apns_key_id: Option<String>,
pub apns_team_id: Option<String>,

// Suspension
pub suspended: bool,
pub suspended_reason: Option<String>,

pub created_at: DateTime<Utc>,
pub updated_at: DateTime<Utc>,
}
Expand Down Expand Up @@ -261,6 +265,7 @@
id: &str,
params: TenantApnsUpdateAuth,
) -> Result<Tenant>;
async fn suspend_tenant(&self, id: &str, reason: &str) -> Result<()>;
}

#[async_trait]
Expand Down Expand Up @@ -373,6 +378,18 @@

Ok(res)
}

async fn suspend_tenant(&self, id: &str, reason: &str) -> Result<()> {
sqlx::query_as::<sqlx::postgres::Postgres, Tenant>(
"UPDATE public.tenants SET suspended = true, suspended_reason = $2::text WHERE id = \
$1 RETURNING *;",
HarryET marked this conversation as resolved.
Show resolved Hide resolved
)
.bind(id)
.bind(reason)
.await?;

Check failure on line 389 in src/stores/tenant.rs

View workflow job for this annotation

GitHub Actions / [ubuntu-latest/rust-stable] Clippy

`sqlx::query::QueryAs<'_, sqlx::Postgres, stores::tenant::Tenant, sqlx::postgres::PgArguments>` is not a future

Check failure on line 389 in src/stores/tenant.rs

View workflow job for this annotation

GitHub Actions / [ubuntu-latest/rust-stable] Unit Tests

`QueryAs<'_, Postgres, Tenant, PgArguments>` is not a future

Ok(())
}
}

#[cfg(not(feature = "multitenant"))]
Expand All @@ -381,7 +398,7 @@
#[cfg(not(feature = "multitenant"))]
impl DefaultTenantStore {
pub fn new(config: Arc<Config>) -> Result<DefaultTenantStore> {
Ok(DefaultTenantStore(Tenant {

Check failure on line 401 in src/stores/tenant.rs

View workflow job for this annotation

GitHub Actions / [ubuntu-latest/rust-stable] Clippy

missing fields `suspended` and `suspended_reason` in initializer of `stores::tenant::Tenant`
id: DEFAULT_TENANT_ID.to_string(),
fcm_api_key: config.fcm_api_key.clone(),
apns_type: config.apns_type,
Expand Down Expand Up @@ -435,4 +452,8 @@
) -> Result<Tenant> {
panic!("Shouldn't have run in single tenant mode")
}

async fn suspend_tenant(&self, id: &str, reason: &str) -> Result<()> {
panic!("Shouldn't have run in single tenant mode")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Error message could be improved:

Suggested change
panic!("Shouldn't have run in single tenant mode")
panic!("Invalid APNS or FCM credentials. Cannot suspend tenant in single-tenant mode, so panicking instead.")

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There error message is the same for all the functions, they cannot be run in single-tenant mode

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe the other functions' error messages could be improved too?

I just don't see any debugging info that would help a service maintainer know that their credential had expired.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wdym? The panics here are for function calls that won't/can't be done in single-tenant mode

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, if they are impossible to call, then you should use unreachable!() instead of panic!()

}
}
Loading