Skip to content

Commit 75cda6b

Browse files
fix: remove user_id from API requests in Dashboards and Filters (#918)
Fetch session key from HttpRequest then fetch username from the session updated all APIs, removed user_id from API requests while saving the json, generate hash for user_id Dashboard API changes: GET /dashboards/{user_id} -> GET /dashboards this fetches all dashboards for user fetched from HttpRequest GET /dashboards/dashboard/{dashboard_id} -> GET /dashboards/{dashboard_id} DELETE /dashboards/dashboard/{dashboard_id} -> DELETE /dashboards/{dashboard_id} PUT /dashboards/dashboard/{dashboard_id} -> PUT /dashboards/{dashboard_id} Filter API changes: GET /filters/{user_id} -> GET /filters this fetches all filters for user fetched from HttpRequest GET /filters/filter/{filter_id} -> GET /filters/{filter_id} DELETE /filters/filter/{filter_id} -> DELETE /filters/{filter_id} PUT /filters/filter/{filter_id} -> PUT /filters/{filter_id}
1 parent 4154fc2 commit 75cda6b

File tree

8 files changed

+240
-127
lines changed

8 files changed

+240
-127
lines changed

server/src/handlers/http/modal/server.rs

Lines changed: 44 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -197,72 +197,64 @@ impl Server {
197197
pub fn get_dashboards_webscope() -> Scope {
198198
web::scope("/dashboards")
199199
.service(
200-
web::resource("").route(
201-
web::post()
202-
.to(dashboards::post)
203-
.authorize(Action::CreateDashboard),
204-
),
205-
)
206-
.service(
207-
web::scope("/dashboard").service(
208-
web::resource("/{dashboard_id}")
209-
.route(
210-
web::get()
211-
.to(dashboards::get)
212-
.authorize(Action::GetDashboard),
213-
)
214-
.route(
215-
web::delete()
216-
.to(dashboards::delete)
217-
.authorize(Action::DeleteDashboard),
218-
)
219-
.route(
220-
web::put()
221-
.to(dashboards::update)
222-
.authorize(Action::CreateDashboard),
223-
),
224-
),
225-
)
226-
.service(
227-
web::scope("/{user_id}").service(
228-
web::resource("").route(
200+
web::resource("")
201+
.route(
202+
web::post()
203+
.to(dashboards::post)
204+
.authorize(Action::CreateDashboard),
205+
)
206+
.route(
229207
web::get()
230208
.to(dashboards::list)
231209
.authorize(Action::ListDashboard),
232210
),
233-
),
211+
)
212+
.service(
213+
web::resource("/{dashboard_id}")
214+
.route(
215+
web::get()
216+
.to(dashboards::get)
217+
.authorize(Action::GetDashboard),
218+
)
219+
.route(
220+
web::delete()
221+
.to(dashboards::delete)
222+
.authorize(Action::DeleteDashboard),
223+
)
224+
.route(
225+
web::put()
226+
.to(dashboards::update)
227+
.authorize(Action::CreateDashboard),
228+
),
234229
)
235230
}
236231

237232
// get the filters web scope
238233
pub fn get_filters_webscope() -> Scope {
239234
web::scope("/filters")
240235
.service(
241-
web::resource("").route(
242-
web::post()
243-
.to(filters::post)
244-
.authorize(Action::CreateFilter),
245-
),
236+
web::resource("")
237+
.route(
238+
web::post()
239+
.to(filters::post)
240+
.authorize(Action::CreateFilter),
241+
)
242+
.route(web::get().to(filters::list).authorize(Action::ListFilter)),
246243
)
247244
.service(
248-
web::scope("/filter").service(
249-
web::resource("/{filter_id}")
250-
.route(web::get().to(filters::get).authorize(Action::GetFilter))
251-
.route(
252-
web::delete()
253-
.to(filters::delete)
254-
.authorize(Action::DeleteFilter),
255-
)
256-
.route(
257-
web::put()
258-
.to(filters::update)
259-
.authorize(Action::CreateFilter),
260-
),
261-
),
245+
web::resource("/{filter_id}")
246+
.route(web::get().to(filters::get).authorize(Action::GetFilter))
247+
.route(
248+
web::delete()
249+
.to(filters::delete)
250+
.authorize(Action::DeleteFilter),
251+
)
252+
.route(
253+
web::put()
254+
.to(filters::update)
255+
.authorize(Action::CreateFilter),
256+
),
262257
)
263-
.service(web::scope("/{user_id}").service(
264-
web::resource("").route(web::get().to(filters::list).authorize(Action::ListFilter)),
265-
))
266258
}
267259

268260
// get the query factory

server/src/handlers/http/users/dashboards.rs

Lines changed: 33 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,11 @@
1717
*/
1818

1919
use crate::{
20-
handlers::http::ingest::PostError,
20+
handlers::http::rbac::RBACError,
2121
option::CONFIG,
2222
storage::{object_storage::dashboard_path, ObjectStorageError},
2323
users::dashboards::{Dashboard, CURRENT_DASHBOARD_VERSION, DASHBOARDS},
24+
utils::{get_hash, get_user_from_request},
2425
};
2526
use actix_web::{http::header::ContentType, web, HttpRequest, HttpResponse, Responder};
2627
use bytes::Bytes;
@@ -30,43 +31,39 @@ use http::StatusCode;
3031
use serde_json::Error as SerdeError;
3132

3233
pub async fn list(req: HttpRequest) -> Result<impl Responder, DashboardError> {
33-
let user_id = req
34-
.match_info()
35-
.get("user_id")
36-
.ok_or(DashboardError::Metadata("No User Id Provided"))?;
37-
let dashboards = DASHBOARDS.list_dashboards_by_user(user_id);
34+
let user_id = get_user_from_request(&req)?;
35+
let dashboards = DASHBOARDS.list_dashboards_by_user(&get_hash(&user_id));
3836

3937
Ok((web::Json(dashboards), StatusCode::OK))
4038
}
4139

4240
pub async fn get(req: HttpRequest) -> Result<impl Responder, DashboardError> {
41+
let user_id = get_user_from_request(&req)?;
4342
let dashboard_id = req
4443
.match_info()
4544
.get("dashboard_id")
4645
.ok_or(DashboardError::Metadata("No Dashboard Id Provided"))?;
4746

48-
if let Some(dashboard) = DASHBOARDS.get_dashboard(dashboard_id) {
47+
if let Some(dashboard) = DASHBOARDS.get_dashboard(dashboard_id, &get_hash(&user_id)) {
4948
return Ok((web::Json(dashboard), StatusCode::OK));
5049
}
5150

5251
Err(DashboardError::Metadata("Dashboard does not exist"))
5352
}
5453

55-
pub async fn post(body: Bytes) -> Result<impl Responder, PostError> {
54+
pub async fn post(req: HttpRequest, body: Bytes) -> Result<impl Responder, DashboardError> {
55+
let user_id = get_user_from_request(&req)?;
5656
let mut dashboard: Dashboard = serde_json::from_slice(&body)?;
57-
let dashboard_id = format!("{}.{}", &dashboard.user_id, Utc::now().timestamp_millis());
57+
let dashboard_id = get_hash(Utc::now().timestamp_micros().to_string().as_str());
5858
dashboard.dashboard_id = Some(dashboard_id.clone());
5959
dashboard.version = Some(CURRENT_DASHBOARD_VERSION.to_string());
60+
dashboard.user_id = Some(get_hash(&user_id));
6061
for tile in dashboard.tiles.iter_mut() {
61-
tile.tile_id = Some(format!(
62-
"{}.{}",
63-
&dashboard.user_id,
64-
Utc::now().timestamp_micros()
65-
));
62+
tile.tile_id = Some(get_hash(Utc::now().timestamp_micros().to_string().as_str()));
6663
}
6764
DASHBOARDS.update(&dashboard);
6865

69-
let path = dashboard_path(&dashboard.user_id, &format!("{}.json", dashboard_id));
66+
let path = dashboard_path(&user_id, &format!("{}.json", dashboard_id));
7067

7168
let store = CONFIG.storage().get_object_store();
7269
let dashboard_bytes = serde_json::to_vec(&dashboard)?;
@@ -77,31 +74,29 @@ pub async fn post(body: Bytes) -> Result<impl Responder, PostError> {
7774
Ok((web::Json(dashboard), StatusCode::OK))
7875
}
7976

80-
pub async fn update(req: HttpRequest, body: Bytes) -> Result<impl Responder, PostError> {
77+
pub async fn update(req: HttpRequest, body: Bytes) -> Result<impl Responder, DashboardError> {
78+
let user_id = get_user_from_request(&req)?;
8179
let dashboard_id = req
8280
.match_info()
8381
.get("dashboard_id")
8482
.ok_or(DashboardError::Metadata("No Dashboard Id Provided"))?;
85-
if DASHBOARDS.get_dashboard(dashboard_id).is_none() {
86-
return Err(PostError::DashboardError(DashboardError::Metadata(
87-
"Dashboard does not exist",
88-
)));
83+
if DASHBOARDS
84+
.get_dashboard(dashboard_id, &get_hash(&user_id))
85+
.is_none()
86+
{
87+
return Err(DashboardError::Metadata("Dashboard does not exist"));
8988
}
9089
let mut dashboard: Dashboard = serde_json::from_slice(&body)?;
9190
dashboard.dashboard_id = Some(dashboard_id.to_string());
9291
dashboard.version = Some(CURRENT_DASHBOARD_VERSION.to_string());
9392
for tile in dashboard.tiles.iter_mut() {
9493
if tile.tile_id.is_none() {
95-
tile.tile_id = Some(format!(
96-
"{}.{}",
97-
&dashboard.user_id,
98-
Utc::now().timestamp_micros()
99-
));
94+
tile.tile_id = Some(get_hash(Utc::now().timestamp_micros().to_string().as_str()));
10095
}
10196
}
10297
DASHBOARDS.update(&dashboard);
10398

104-
let path = dashboard_path(&dashboard.user_id, &format!("{}.json", dashboard_id));
99+
let path = dashboard_path(&user_id, &format!("{}.json", dashboard_id));
105100

106101
let store = CONFIG.storage().get_object_store();
107102
let dashboard_bytes = serde_json::to_vec(&dashboard)?;
@@ -112,16 +107,19 @@ pub async fn update(req: HttpRequest, body: Bytes) -> Result<impl Responder, Pos
112107
Ok((web::Json(dashboard), StatusCode::OK))
113108
}
114109

115-
pub async fn delete(req: HttpRequest) -> Result<HttpResponse, PostError> {
110+
pub async fn delete(req: HttpRequest) -> Result<HttpResponse, DashboardError> {
111+
let user_id = get_user_from_request(&req)?;
116112
let dashboard_id = req
117113
.match_info()
118114
.get("dashboard_id")
119115
.ok_or(DashboardError::Metadata("No Dashboard Id Provided"))?;
120-
let dashboard = DASHBOARDS
121-
.get_dashboard(dashboard_id)
122-
.ok_or(DashboardError::Metadata("Dashboard does not exist"))?;
123-
124-
let path = dashboard_path(&dashboard.user_id, &format!("{}.json", dashboard_id));
116+
if DASHBOARDS
117+
.get_dashboard(dashboard_id, &get_hash(&user_id))
118+
.is_none()
119+
{
120+
return Err(DashboardError::Metadata("Dashboard does not exist"));
121+
}
122+
let path = dashboard_path(&user_id, &format!("{}.json", dashboard_id));
125123
let store = CONFIG.storage().get_object_store();
126124
store.delete_object(&path).await?;
127125

@@ -138,6 +136,8 @@ pub enum DashboardError {
138136
Serde(#[from] SerdeError),
139137
#[error("Cannot perform this operation: {0}")]
140138
Metadata(&'static str),
139+
#[error("User does not exist")]
140+
UserDoesNotExist(#[from] RBACError),
141141
}
142142

143143
impl actix_web::ResponseError for DashboardError {
@@ -146,6 +146,7 @@ impl actix_web::ResponseError for DashboardError {
146146
Self::ObjectStorage(_) => StatusCode::INTERNAL_SERVER_ERROR,
147147
Self::Serde(_) => StatusCode::BAD_REQUEST,
148148
Self::Metadata(_) => StatusCode::BAD_REQUEST,
149+
Self::UserDoesNotExist(_) => StatusCode::NOT_FOUND,
149150
}
150151
}
151152

0 commit comments

Comments
 (0)