Skip to content

Commit

Permalink
Add POST /firewall/update/:id endpoint
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
enriquegomeztagle committed Nov 28, 2024
1 parent 1bdfdc2 commit 8a8adba
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
18 changes: 17 additions & 1 deletion controller/src/firewall/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ pub fn router() -> Router<AppState> {
.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)
Expand Down Expand Up @@ -231,6 +232,16 @@ pub async fn halt(State(s): State<AppState>) {
s.firewall_pool.get().await.unwrap().halt().await;
}

pub async fn update_rule(
State(s): State<AppState>,
Path((id,)): Path<(u32,)>,
Json(rule): Json<StoredRuleDecoded>,
) -> Json<firewall::Response> {
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<UnixStream, firewall::Response, Message, AsyncDestination> =
Expand Down Expand Up @@ -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
}
}
27 changes: 27 additions & 0 deletions firewall/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand Down
2 changes: 2 additions & 0 deletions message/src/firewall.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub enum Response {
Status(Status),
RuleChange(RuleChange),
Events(Vec<firewall_common::StoredEventDecoded>),
UpdateRule(firewall_common::StoredRuleDecoded),
}

#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
Expand Down Expand Up @@ -60,4 +61,5 @@ pub enum Request {
GetRules,
Status,
GetEvents(crate::EventQuery),
UpdateRule(u32, firewall_common::StoredRuleDecoded),
}

0 comments on commit 8a8adba

Please sign in to comment.