Skip to content

Commit

Permalink
Removing governor from private apis
Browse files Browse the repository at this point in the history
  • Loading branch information
sagojez committed Apr 29, 2024
1 parent b24e68c commit e20cc1f
Show file tree
Hide file tree
Showing 9 changed files with 6 additions and 53 deletions.
2 changes: 1 addition & 1 deletion api/src/endpoints/common_enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub async fn read(
query: Option<Query<BTreeMap<String, String>>>,
State(state): State<Arc<AppState>>,
) -> Result<Json<ReadResponse<CommonEnum>>, ApiError> {
let mut query = shape_mongo_filter(query, None, None, false);
let mut query = shape_mongo_filter(query, None, None);
query.filter.remove(DELETED_STR);

let store = &state.app_stores.common_enum;
Expand Down
4 changes: 0 additions & 4 deletions api/src/endpoints/common_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,6 @@ impl CrudHook<CommonModel> for CreateRequest {
impl CrudRequest for CreateRequest {
type Output = CommonModel;

fn filterable() -> bool {
false
}

fn output(&self) -> Option<Self::Output> {
let mut record = Self::Output {
id: Id::now(IdPrefix::CommonModel),
Expand Down
4 changes: 0 additions & 4 deletions api/src/endpoints/connection_definition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,10 +259,6 @@ pub async fn public_get_connection_details(
impl CrudRequest for CreateRequest {
type Output = ConnectionDefinition;

fn filterable() -> bool {
false
}

fn output(&self) -> Option<Self::Output> {
let auth_secrets: Vec<AuthSecret> = self
.authentication
Expand Down
4 changes: 0 additions & 4 deletions api/src/endpoints/connection_model_definition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,10 +303,6 @@ impl CrudHook<ConnectionModelDefinition> for CreateRequest {}
impl CrudRequest for CreateRequest {
type Output = ConnectionModelDefinition;

fn filterable() -> bool {
false
}

fn output(&self) -> Option<Self::Output> {
let key = format!(
"api::{}::{}::{}::{}::{}::{}",
Expand Down
4 changes: 0 additions & 4 deletions api/src/endpoints/connection_model_schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,6 @@ impl CrudHook<ConnectionModelSchema> for CreateRequest {}
impl CrudRequest for CreateRequest {
type Output = ConnectionModelSchema;

fn filterable() -> bool {
false
}

fn output(&self) -> Option<Self::Output> {
let key = format!(
"api::{}::{}::{}",
Expand Down
4 changes: 0 additions & 4 deletions api/src/endpoints/connection_oauth_definition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,6 @@ fn default_separator() -> Option<String> {
impl CrudRequest for CreateRequest {
type Output = ConnectionOAuthDefinition;

fn filterable() -> bool {
false
}

fn output(&self) -> Option<Self::Output> {
Some(Self::Output {
id: Id::new(IdPrefix::ConnectionOAuthDefinition, Utc::now()),
Expand Down
10 changes: 1 addition & 9 deletions api/src/endpoints/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,6 @@ pub trait CrudRequest: Sized {
/// Update the output of the request based on the input.
fn update(&self, _: &mut Self::Output) -> Unit {}

/// Whether the Output can be filtered by the environment and ownership.
fn filterable() -> bool {
true
}

/// Get the store for the request.
fn get_store(stores: AppStores) -> MongoStore<Self::Output>;
}
Expand Down Expand Up @@ -180,7 +175,6 @@ where
e
}),
Some(headers),
T::filterable(),
);

let store = T::get_store(state.app_stores.clone());
Expand Down Expand Up @@ -222,7 +216,7 @@ where

let res = cache
.try_get_with(query.as_ref().map(|q| q.0.clone()), async {
let query = shape_mongo_filter(query, None, None, T::filterable());
let query = shape_mongo_filter(query, None, None);

println!("{:?}", query);
let store = T::get_store(state.app_stores.clone());
Expand Down Expand Up @@ -282,7 +276,6 @@ where
e
}),
None,
T::filterable(),
);
query.filter.insert("_id", id.clone());

Expand Down Expand Up @@ -348,7 +341,6 @@ where
e
}),
None,
T::filterable(),
);
query.filter.insert("_id", id.clone());

Expand Down
19 changes: 1 addition & 18 deletions api/src/routes/private.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,11 @@ use crate::{
connection_model_definition::{self, test_connection_model_definition},
connection_model_schema, connection_oauth_definition, openapi,
},
middleware::{
extractor::OwnershipId,
jwt_auth::{self, JwtState},
},
middleware::jwt_auth::{self, JwtState},
server::AppState,
};
use axum::{middleware::from_fn_with_state, routing::post, Router};
use std::sync::Arc;
use tower_governor::{governor::GovernorConfigBuilder, GovernorLayer};
use tower_http::trace::TraceLayer;

pub async fn get_router(state: &Arc<AppState>) -> Router<Arc<AppState>> {
Expand Down Expand Up @@ -40,20 +36,7 @@ pub async fn get_router(state: &Arc<AppState>) -> Router<Arc<AppState>> {
)
.nest("/common-models", common_model::get_router());

let config = Box::new(
GovernorConfigBuilder::default()
.per_second(state.config.burst_rate_limit)
.burst_size(state.config.burst_size)
.key_extractor(OwnershipId)
.use_headers()
.finish()
.expect("Failed to build GovernorConfig"),
);

routes
.layer(GovernorLayer {
config: Box::leak(config),
})
.layer(from_fn_with_state(
Arc::new(JwtState::new(state)),
jwt_auth::jwt_auth,
Expand Down
8 changes: 3 additions & 5 deletions api/src/util/shape_mongo_filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ pub fn shape_mongo_filter(
query: Option<Query<BTreeMap<String, String>>>,
event_access: Option<Arc<EventAccess>>,
headers: Option<HeaderMap>,
is_filterable: bool,
) -> MongoQuery {
let mut filter = doc! {};
let mut skip = 0;
Expand All @@ -46,7 +45,7 @@ pub fn shape_mongo_filter(

filter.insert(DELETED_STR, false);

if is_filterable && event_access.is_some() {
if event_access.is_some() {
filter.insert(
OWNERSHIP_STR,
event_access.as_ref().unwrap().ownership.id.to_string(),
Expand Down Expand Up @@ -107,7 +106,7 @@ mod test {
filter: mut doc,
skip,
limit,
} = shape_mongo_filter(Some(Query(params.clone())), None, None, false);
} = shape_mongo_filter(Some(Query(params.clone())), None, None);
assert_eq!(doc.get_str(OWNERSHIP_STR).unwrap(), "foo");
assert_eq!(doc.get_str(ENVIRONMENT_STR).unwrap(), "bar");
assert!(!doc.get_bool(DELETED_STR).unwrap());
Expand All @@ -134,7 +133,7 @@ mod test {
});

let MongoQuery { filter: doc, .. } =
shape_mongo_filter(Some(Query(params)), Some(event_access), None, true);
shape_mongo_filter(Some(Query(params)), Some(event_access), None);
assert_eq!(doc.get_str(OWNERSHIP_STR).unwrap(), "baz");
assert_eq!(doc.get_str(ENVIRONMENT_STR).unwrap(), "test");
}
Expand Down Expand Up @@ -170,7 +169,6 @@ mod test {
Some(Query(params.clone())),
Some(event_access),
Some(headers),
true,
);

assert!(!doc.contains_key(ENVIRONMENT_STR));
Expand Down

0 comments on commit e20cc1f

Please sign in to comment.