diff --git a/meilisearch-auth/src/lib.rs b/meilisearch-auth/src/lib.rs index bfc39940c..35532e0c4 100644 --- a/meilisearch-auth/src/lib.rs +++ b/meilisearch-auth/src/lib.rs @@ -116,7 +116,7 @@ impl AuthController { Ok(key) } - pub fn update_key(&self, uid: Uuid, patch: PatchApiKey) -> Result { + pub async fn update_key(&self, uid: Uuid, patch: PatchApiKey) -> Result { let mut key = self.get_key(uid)?; match patch.description { Setting::NotSet => (), @@ -127,7 +127,14 @@ impl AuthController { name => key.name = name.set(), }; key.updated_at = OffsetDateTime::now_utc(); - self.store.put_api_key(key) + let store = self.store.clone(); + // TODO: we may commit only after zk persisted the keys + let key = tokio::task::spawn_blocking(move || store.put_api_key(key)).await??; + if let Some(ref zk) = self.zk { + zk.set_data(&format!("/auth/{}", key.uid), &serde_json::to_vec_pretty(&key)?, None) + .await?; + } + Ok(key) } pub fn get_key(&self, uid: Uuid) -> Result { diff --git a/meilisearch/src/routes/api_key.rs b/meilisearch/src/routes/api_key.rs index 9b2193b01..834750e64 100644 --- a/meilisearch/src/routes/api_key.rs +++ b/meilisearch/src/routes/api_key.rs @@ -106,17 +106,11 @@ pub async fn patch_api_key( ) -> Result { let key = path.into_inner().key; let patch_api_key = body.into_inner(); - let res = tokio::task::spawn_blocking(move || -> Result<_, AuthControllerError> { - let uid = - Uuid::parse_str(&key).or_else(|_| auth_controller.get_uid_from_encoded_key(&key))?; - let key = auth_controller.update_key(uid, patch_api_key)?; + let uid = Uuid::parse_str(&key).or_else(|_| auth_controller.get_uid_from_encoded_key(&key))?; + let key = auth_controller.update_key(uid, patch_api_key).await?; + let key = KeyView::from_key(key, &auth_controller); - Ok(KeyView::from_key(key, &auth_controller)) - }) - .await - .map_err(|e| ResponseError::from_msg(e.to_string(), Code::Internal))??; - - Ok(HttpResponse::Ok().json(res)) + Ok(HttpResponse::Ok().json(key)) } pub async fn delete_api_key(