|
| 1 | +use std::path::PathBuf; |
| 2 | +use std::error::Error; |
| 3 | +use std::fs::File; |
| 4 | +use std::io::Read; |
| 5 | +use log::{error, info}; |
| 6 | + |
| 7 | +use rocket::fairing::{Fairing, Info, Kind}; |
| 8 | +use rocket::serde::json::Json; |
| 9 | +use rocket::Rocket; |
| 10 | +use rocket::Build; |
| 11 | + |
| 12 | +use crate::services::policies::PolicyStore; |
| 13 | +use crate::schemas::policies::Policy; |
| 14 | +use crate::config; |
| 15 | + |
| 16 | +pub struct InitPoliciesFairing; |
| 17 | + |
| 18 | +pub(crate) async fn init(conf: &config::Config, policy_store: &Box<dyn PolicyStore>) { |
| 19 | + |
| 20 | + if conf.policies.is_none() { |
| 21 | + return; |
| 22 | + } |
| 23 | + |
| 24 | + let file_path = conf.policies.clone().unwrap(); |
| 25 | + let policies_file_path = &file_path; |
| 26 | + let policies = match load_policies_from_file(policies_file_path.to_path_buf()).await { |
| 27 | + Ok(policies) => policies, |
| 28 | + Err(err) => { |
| 29 | + error!("Failed to load policies from file: {}", err); |
| 30 | + return; |
| 31 | + } |
| 32 | + }; |
| 33 | + |
| 34 | + match policy_store.update_policies(policies.into_inner()).await { |
| 35 | + Ok(policies) => { |
| 36 | + info!("Successfully updated policies from file {}: {} policies", &file_path.display(), policies.len()); |
| 37 | + } |
| 38 | + Err(err) => { |
| 39 | + error!("Failed to update policies: {}", err); |
| 40 | + return; |
| 41 | + } |
| 42 | + }; |
| 43 | +} |
| 44 | + |
| 45 | +pub async fn load_policies_from_file(path: PathBuf) -> Result<Json<Vec<Policy>>, Box<dyn Error>> { |
| 46 | + |
| 47 | + if !path.try_exists().unwrap_or(false) || !path.is_file() { |
| 48 | + return Err("File does not exist".into()); |
| 49 | + } |
| 50 | + |
| 51 | + if path.extension().unwrap() != "json" { |
| 52 | + return Err("File is not a json file".into()); |
| 53 | + } |
| 54 | + |
| 55 | + let mut file = match File::open(&path) { |
| 56 | + Ok(file) => file, |
| 57 | + Err(err) => return Err(format!("Failed to open file: {}", err).into()), |
| 58 | + }; |
| 59 | + |
| 60 | + let mut contents = String::new(); |
| 61 | + if let Err(err) = file.read_to_string(&mut contents) { |
| 62 | + return Err(format!("Failed to read file: {}", err).into()); |
| 63 | + } |
| 64 | + |
| 65 | + let policies: Vec<Policy> = match rocket::serde::json::from_str(&contents) { |
| 66 | + Ok(policies) => policies, |
| 67 | + Err(err) => return Err(format!("Failed to deserialize JSON: {}", err).into()), |
| 68 | + }; |
| 69 | + |
| 70 | + Ok(Json(policies)) |
| 71 | +} |
| 72 | + |
| 73 | +#[async_trait::async_trait] |
| 74 | +impl Fairing for InitPoliciesFairing { |
| 75 | + async fn on_ignite(&self, rocket: Rocket<Build>) -> Result<Rocket<Build>, Rocket<Build>> { |
| 76 | + let config = rocket.state::<config::Config>(); |
| 77 | + |
| 78 | + if config.is_none() { |
| 79 | + return Ok(rocket); |
| 80 | + } |
| 81 | + |
| 82 | + init(config.unwrap(), rocket.state::<Box<dyn PolicyStore>>().unwrap()).await; |
| 83 | + |
| 84 | + Ok(rocket) |
| 85 | + } |
| 86 | + |
| 87 | + fn info(&self) -> Info { |
| 88 | + Info { |
| 89 | + name: "Init Policies", |
| 90 | + kind: Kind::Ignite |
| 91 | + } |
| 92 | + } |
| 93 | +} |
0 commit comments