Skip to content

Open source, composable payments platform | PCI compliant | SaaS and Self-host options | Enables connectivity to multiple payment, payout, fraud, vault and tokenization providers | Uplifts authorization with intelligent routing and revenue recovery | Reduce payment processing costs with cost observability | Reduces payment ops with reconciliation

refactor(router): scope event apis to jwt auth and profile, and read listings from the replica (#13703)

Ayush Anand61a387991107
diff --git a/api-reference/v1/openapi_spec_v1.json b/api-reference/v1/openapi_spec_v1.json
index 5f72edef71..a18417500a 100644
--- a/api-reference/v1/openapi_spec_v1.json
+++ b/api-reference/v1/openapi_spec_v1.json
@@ -7623,7 +7623,7 @@
},
"security": [
{
- "admin_api_key": []
+ "jwt_key": []
}
]
}
@@ -7731,7 +7731,7 @@
},
"security": [
{
- "admin_api_key": []
+ "jwt_key": []
}
]
}
@@ -7778,7 +7778,7 @@
},
"security": [
{
- "admin_api_key": []
+ "jwt_key": []
}
]
}
diff --git a/crates/openapi/src/routes/webhook_events.rs b/crates/openapi/src/routes/webhook_events.rs
index 3aef711d9e..98aa41b13d 100644
--- a/crates/openapi/src/routes/webhook_events.rs
+++ b/crates/openapi/src/routes/webhook_events.rs
@@ -35,7 +35,7 @@
),
tag = "Event",
operation_id = "List all Events associated with a Merchant Account or Profile",
- security(("admin_api_key" = []))
+ security(("jwt_key" = []))
)]
pub fn list_initial_webhook_delivery_attempts() {}
@@ -88,7 +88,7 @@ pub fn list_initial_webhook_delivery_attempts_with_jwtauth() {}
),
tag = "Event",
operation_id = "List all delivery attempts for an Event",
- security(("admin_api_key" = []))
+ security(("jwt_key" = []))
)]
pub fn list_webhook_delivery_attempts() {}
@@ -112,6 +112,6 @@ pub fn list_webhook_delivery_attempts() {}
),
tag = "Event",
operation_id = "Manually retry the delivery of an Event",
- security(("admin_api_key" = []))
+ security(("jwt_key" = []))
)]
pub fn retry_webhook_delivery_attempt() {}
diff --git a/crates/router/src/db/events.rs b/crates/router/src/db/events.rs
index c95e8d3878..cd69dd7164 100644
--- a/crates/router/src/db/events.rs
+++ b/crates/router/src/db/events.rs
@@ -4,6 +4,7 @@ use common_utils::ext_traits::AsyncExt;
use error_stack::{report, ResultExt};
use futures::future::try_join_all;
use router_env::{instrument, tracing};
+use storage_impl::DatabaseStore;
use super::{MockDb, Store};
use crate::{
@@ -334,7 +335,11 @@ impl EventInterface for Store {
merchant_key_store: &domain::MerchantKeyStore,
event_recipient: Option<common_enums::EventRecipient>,
) -> CustomResult<Vec<domain::Event>, errors::StorageError> {
- let conn = connection::pg_connection_read(self).await?;
+ let conn = self
+ .get_replica_pool()
+ .get()
+ .await
+ .change_context(errors::StorageError::DatabaseConnectionError)?;
storage::Event::list_initial_attempts_by_initiator_merchant_id_constraints(
&conn,
initiator_merchant_id,
@@ -415,7 +420,11 @@ impl EventInterface for Store {
merchant_key_store: &domain::MerchantKeyStore,
event_recipient: Option<common_enums::EventRecipient>,
) -> CustomResult<Vec<domain::Event>, errors::StorageError> {
- let conn = connection::pg_connection_read(self).await?;
+ let conn = self
+ .get_replica_pool()
+ .get()
+ .await
+ .change_context(errors::StorageError::DatabaseConnectionError)?;
storage::Event::list_by_initiator_merchant_id_initial_attempt_id(
&conn,
initial_attempt_id,
@@ -482,7 +491,11 @@ impl EventInterface for Store {
merchant_key_store: &domain::MerchantKeyStore,
event_recipient: Option<common_enums::EventRecipient>,
) -> CustomResult<Vec<domain::Event>, errors::StorageError> {
- let conn = connection::pg_connection_read(self).await?;
+ let conn = self
+ .get_replica_pool()
+ .get()
+ .await
+ .change_context(errors::StorageError::DatabaseConnectionError)?;
storage::Event::list_initial_attempts_by_initiator_merchant_id_primary_object_id(
&conn,
initiator_merchant_id,
@@ -588,7 +601,11 @@ impl EventInterface for Store {
merchant_key_store: &domain::MerchantKeyStore,
event_recipient: Option<common_enums::EventRecipient>,
) -> CustomResult<Vec<domain::Event>, errors::StorageError> {
- let conn = connection::pg_connection_read(self).await?;
+ let conn = self
+ .get_replica_pool()
+ .get()
+ .await
+ .change_context(errors::StorageError::DatabaseConnectionError)?;
storage::Event::list_initial_attempts_by_profile_id_constraints(
&conn,
profile_id,
@@ -741,7 +758,11 @@ impl EventInterface for Store {
is_delivered: Option<bool>,
event_recipient: Option<common_enums::EventRecipient>,
) -> CustomResult<i64, errors::StorageError> {
- let conn = connection::pg_connection_read(self).await?;
+ let conn = self
+ .get_replica_pool()
+ .get()
+ .await
+ .change_context(errors::StorageError::DatabaseConnectionError)?;
storage::Event::count_initial_attempts_by_profile_id_constraints(
&conn,
profile_id,
@@ -766,7 +787,11 @@ impl EventInterface for Store {
is_delivered: Option<bool>,
event_recipient: Option<common_enums::EventRecipient>,
) -> CustomResult<i64, errors::StorageError> {
- let conn = connection::pg_connection_read(self).await?;
+ let conn = self
+ .get_replica_pool()
+ .get()
+ .await
+ .change_context(errors::StorageError::DatabaseConnectionError)?;
storage::Event::count_initial_attempts_by_initiator_merchant_id_constraints(
&conn,
initiator_merchant_id,
diff --git a/crates/router/src/routes/webhook_events.rs b/crates/router/src/routes/webhook_events.rs
index 5bed12a279..44f08dfce9 100644
--- a/crates/router/src/routes/webhook_events.rs
+++ b/crates/router/src/routes/webhook_events.rs
@@ -1,15 +1,13 @@
use actix_web::{web, HttpRequest, Responder};
-use common_enums::EntityType;
-use error_stack::ResultExt;
use router_env::{instrument, tracing, Flow};
use crate::{
- core::{api_locking, errors, webhooks::webhook_events},
+ core::{api_locking, webhooks::webhook_events},
routes::AppState,
services::{
api,
authentication::{self as auth, UserFromToken},
- authorization::{permissions::Permission, roles::RoleInfo},
+ authorization::permissions::Permission,
},
types::api::webhook_events::{
EventListConstraints, EventListRequestInternal, WebhookDeliveryAttemptListRequestInternal,
@@ -38,23 +36,19 @@ pub async fn list_initial_webhook_delivery_attempts(
state,
&req,
request_internal,
- |state, _, request_internal, _| {
+ |state, _: (), request_internal, _| {
webhook_events::list_initial_delivery_attempts(
state,
request_internal.merchant_id,
request_internal.constraints,
)
},
- auth::auth_type(
- &auth::AdminApiAuth,
- &auth::JWTAuthMerchantFromRoute {
- merchant_id,
- required_permission: Permission::MerchantWebhookEventRead,
- allow_connected: true,
- allow_platform: true,
- },
- req.headers(),
- ),
+ &auth::JWTAuthMerchantFromRoute {
+ merchant_id,
+ required_permission: Permission::MerchantWebhookEventRead,
+ allow_connected: true,
+ allow_platform: true,
+ },
api_locking::LockAction::NotApplicable,
))
.await
@@ -80,23 +74,13 @@ pub async fn list_initial_webhook_delivery_attempts_with_jwtauth(
&req,
request_internal,
|state, auth: UserFromToken, request_internal, _| async move {
- let role_info = RoleInfo::from_role_id_org_id_tenant_id(
- &state,
- &auth.role_id,
- &auth.org_id,
- auth.tenant_id.as_ref().unwrap_or(&state.tenant.tenant_id),
- )
- .await
- .change_context(errors::ApiErrorResponse::InternalServerError)
- .attach_printable("Failed to fetch role info while listing webhook events")?;
-
- // Merchant-or-higher scopes search across all profiles in the merchant;
- // profile-scoped users stay confined to their JWT's profile_id.
+ // Every JWT carries a profile_id, so scope every caller to it regardless of
+ // entity type. This keeps the listing anchored on business_profile_id, which
+ // the events index is built on.
let request_internal = EventListRequestInternal {
merchant_id: auth.merchant_id,
constraints: EventListConstraints {
- profile_id: (role_info.get_entity_type() == EntityType::Profile)
- .then_some(auth.profile_id),
+ profile_id: Some(auth.profile_id),
..request_internal.constraints
},
};
@@ -137,23 +121,19 @@ pub async fn list_webhook_delivery_attempts(
state,
&req,
request_internal,
- |state, _, request_internal, _| {
+ |state, _: (), request_internal, _| {
webhook_events::list_delivery_attempts(
state,
request_internal.merchant_id,
request_internal.initial_attempt_id,
)
},
- auth::auth_type(
- &auth::AdminApiAuth,
- &auth::JWTAuthMerchantFromRoute {
- merchant_id,
- required_permission: Permission::MerchantWebhookEventRead,
- allow_connected: true,
- allow_platform: true,
- },
- req.headers(),
- ),
+ &auth::JWTAuthMerchantFromRoute {
+ merchant_id,
+ required_permission: Permission::MerchantWebhookEventRead,
+ allow_connected: true,
+ allow_platform: true,
+ },
api_locking::LockAction::NotApplicable,
))
.await
@@ -179,23 +159,19 @@ pub async fn retry_webhook_delivery_attempt(
state,
&req,
request_internal,
- |state, _, request_internal, _| {
+ |state, _: (), request_internal, _| {
webhook_events::retry_delivery_attempt(
state,
request_internal.merchant_id,
request_internal.event_id,
)
},
- auth::auth_type(
- &auth::AdminApiAuth,
- &auth::JWTAuthMerchantFromRoute {
- merchant_id,
- required_permission: Permission::MerchantWebhookEventWrite,
- allow_connected: true,
- allow_platform: true,
- },
- req.headers(),
- ),
+ &auth::JWTAuthMerchantFromRoute {
+ merchant_id,
+ required_permission: Permission::MerchantWebhookEventWrite,
+ allow_connected: true,
+ allow_platform: true,
+ },
api_locking::LockAction::NotApplicable,
))
.await