|
17 | 17 | */ |
18 | 18 |
|
19 | 19 | use actix_web::{http::header::ContentType, web, HttpResponse, Responder}; |
| 20 | +use bytes::Bytes; |
20 | 21 | use http::StatusCode; |
21 | 22 |
|
22 | 23 | use crate::{ |
23 | | - option::CONFIG, |
| 24 | + option::{Mode, CONFIG}, |
24 | 25 | rbac::{ |
25 | 26 | map::{mut_roles, DEFAULT_ROLE}, |
26 | 27 | role::model::DefaultPrivilege, |
27 | 28 | }, |
28 | 29 | storage::{self, ObjectStorageError, StorageMetadata}, |
29 | 30 | }; |
30 | 31 |
|
| 32 | +use super::cluster::sync_role_update_with_ingestors; |
| 33 | + |
31 | 34 | // Handler for PUT /api/v1/role/{name} |
32 | 35 | // Creates a new role or update existing one |
33 | | -pub async fn put( |
34 | | - name: web::Path<String>, |
35 | | - body: web::Json<Vec<DefaultPrivilege>>, |
36 | | -) -> Result<impl Responder, RoleError> { |
| 36 | +pub async fn put(name: web::Path<String>, body: Bytes) -> Result<impl Responder, RoleError> { |
37 | 37 | let name = name.into_inner(); |
38 | | - let privileges = body.into_inner(); |
| 38 | + let privileges = serde_json::from_slice::<Vec<DefaultPrivilege>>(&body)?; |
39 | 39 | let mut metadata = get_metadata().await?; |
40 | 40 | metadata.roles.insert(name.clone(), privileges.clone()); |
41 | | - put_metadata(&metadata).await?; |
42 | | - mut_roles().insert(name, privileges); |
| 41 | + if CONFIG.parseable.mode == Mode::Ingest { |
| 42 | + let _ = storage::put_staging_metadata(&metadata); |
| 43 | + mut_roles().insert(name.clone(), privileges.clone()); |
| 44 | + } else { |
| 45 | + put_metadata(&metadata).await?; |
| 46 | + mut_roles().insert(name.clone(), privileges.clone()); |
| 47 | + if CONFIG.parseable.mode == Mode::Query { |
| 48 | + sync_role_update_with_ingestors(name.clone(), privileges.clone()).await?; |
| 49 | + } |
| 50 | + } |
| 51 | + |
43 | 52 | Ok(HttpResponse::Ok().finish()) |
44 | 53 | } |
45 | 54 |
|
@@ -118,13 +127,22 @@ pub enum RoleError { |
118 | 127 | ObjectStorageError(#[from] ObjectStorageError), |
119 | 128 | #[error("Cannot perform this operation as role is assigned to an existing user.")] |
120 | 129 | RoleInUse, |
| 130 | + #[error("Error: {0}")] |
| 131 | + Anyhow(#[from] anyhow::Error), |
| 132 | + #[error("{0}")] |
| 133 | + SerdeError(#[from] serde_json::Error), |
| 134 | + #[error("Network Error: {0}")] |
| 135 | + Network(#[from] reqwest::Error), |
121 | 136 | } |
122 | 137 |
|
123 | 138 | impl actix_web::ResponseError for RoleError { |
124 | 139 | fn status_code(&self) -> http::StatusCode { |
125 | 140 | match self { |
126 | 141 | Self::ObjectStorageError(_) => StatusCode::INTERNAL_SERVER_ERROR, |
127 | 142 | Self::RoleInUse => StatusCode::BAD_REQUEST, |
| 143 | + Self::Anyhow(_) => StatusCode::INTERNAL_SERVER_ERROR, |
| 144 | + Self::SerdeError(_) => StatusCode::BAD_REQUEST, |
| 145 | + Self::Network(_) => StatusCode::BAD_GATEWAY, |
128 | 146 | } |
129 | 147 | } |
130 | 148 |
|
|
0 commit comments