From 8a8adba2d10beaa5c62d3a734b35d54656d7dc6a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Enrique=20Ulises=20B=C3=A1ez=20G=C3=B3mez=20Tagle?= <89673617+enriquegomeztagle@users.noreply.github.com> Date: Wed, 27 Nov 2024 22:06:20 -0600 Subject: [PATCH] Add POST /firewall/update/:id endpoint Related to #30 Add POST /firewall/update/:id endpoint to update firewall rules by ID. * Add a new request type `UpdateRule` to the `Request` and `Response` enums in `message/src/firewall.rs`. * Add a new route for `POST /firewall/update/:id` in the `rules` router in `controller/src/firewall/mod.rs`. * Add a new handler function `update_rule` for the `POST /firewall/update/:id` route in `controller/src/firewall/mod.rs`. * Implement the logic to update the rule in the `update_rule` function in `controller/src/firewall/mod.rs`. * Add a new match arm for `Request::UpdateRule` in the `handle_message` function in `firewall/src/main.rs`. * Implement the logic to update the rule in the `handle_message` function in `firewall/src/main.rs`. --- For more details, open the [Copilot Workspace session](https://copilot-workspace.githubnext.com/AOx0/adam/issues/30?shareId=XXXX-XXXX-XXXX-XXXX). --- controller/src/firewall/mod.rs | 18 +++++++++++++++++- firewall/src/main.rs | 27 +++++++++++++++++++++++++++ message/src/firewall.rs | 2 ++ 3 files changed, 46 insertions(+), 1 deletion(-) diff --git a/controller/src/firewall/mod.rs b/controller/src/firewall/mod.rs index cfbfddf..fda9a8b 100644 --- a/controller/src/firewall/mod.rs +++ b/controller/src/firewall/mod.rs @@ -63,7 +63,8 @@ pub fn router() -> Router { .route("/:idx/disable", routing::post(disable)) .route("/:idx/toggle", routing::post(toggle)) .route("/:idx", routing::get(get_rule).delete(delete)) - .route("/", routing::get(get_rules).post(add)); + .route("/", routing::get(get_rules).post(add)) + .route("/update/:id", routing::post(update_rule)); // P2f78 Router::new() .nest("/rules", rules) @@ -231,6 +232,16 @@ pub async fn halt(State(s): State) { s.firewall_pool.get().await.unwrap().halt().await; } +pub async fn update_rule( + State(s): State, + Path((id,)): Path<(u32,)>, + Json(rule): Json, +) -> Json { + let mut socket = s.firewall_pool.get().await.unwrap(); + socket.update(id, rule).await; + Json(socket.read().await) +} + impl Socket { pub async fn new() -> Self { let stream: AsyncBincodeStream = @@ -349,4 +360,9 @@ impl Socket { pub async fn term(&mut self) { self.send(Message::Terminate).await } + + pub async fn update(&mut self, id: u32, rule: StoredRuleDecoded) { + self.send(Message::Firewall(firewall::Request::UpdateRule(id, rule))) + .await + } } diff --git a/firewall/src/main.rs b/firewall/src/main.rs index 46b13f6..bb2b08e 100644 --- a/firewall/src/main.rs +++ b/firewall/src/main.rs @@ -594,6 +594,33 @@ async fn handle_message( Some(Response::Events(b)) } + Request::UpdateRule(id, new_rule) => { + if let Ok(mut rule @ Rule { init: true, .. }) = config.get(&id, 0) { + rule = new_rule.rule; + rule.id = id; + config.set(id, rule, 0).unwrap(); + + let mut db = get_db().await; + diesel::update(rules::table.filter(rules::dsl::id.eq(id as i32))) + .set(( + rules::dsl::name.eq(new_rule.name), + rules::dsl::description.eq(new_rule.description), + rules::dsl::rule.eq(bincode::serialize(&rule).unwrap()), + )) + .execute(&mut db) + .await + .unwrap(); + + Some(Response::UpdateRule(StoredRuleDecoded { + id: rule.id as i32, + name: new_rule.name, + description: new_rule.description, + rule, + })) + } else { + Some(Response::DoesNotExist) + } + } }) } Message::Halt => { diff --git a/message/src/firewall.rs b/message/src/firewall.rs index 1814e33..efdb625 100644 --- a/message/src/firewall.rs +++ b/message/src/firewall.rs @@ -13,6 +13,7 @@ pub enum Response { Status(Status), RuleChange(RuleChange), Events(Vec), + UpdateRule(firewall_common::StoredRuleDecoded), } #[derive(Debug, Clone, Copy, Serialize, Deserialize)] @@ -60,4 +61,5 @@ pub enum Request { GetRules, Status, GetEvents(crate::EventQuery), + UpdateRule(u32, firewall_common::StoredRuleDecoded), }