From 13ba3964c07bf9aac3b8746e1a0e9079c291921c Mon Sep 17 00:00:00 2001 From: jeangab Date: Thu, 26 Sep 2024 22:22:56 -0400 Subject: [PATCH] feat: Improve DHCP architecture, much better, architecture feels good right now --- .../harmony/src/domain/topology/network.rs | 19 +++++++-- harmony-rs/harmony/src/infra/opnsense/mod.rs | 21 +++++----- .../src/modules/{opnsense_dhcp.rs => dhcp.rs} | 40 ++++++++++--------- harmony-rs/harmony/src/modules/mod.rs | 2 +- 4 files changed, 48 insertions(+), 34 deletions(-) rename harmony-rs/harmony/src/modules/{opnsense_dhcp.rs => dhcp.rs} (82%) diff --git a/harmony-rs/harmony/src/domain/topology/network.rs b/harmony-rs/harmony/src/domain/topology/network.rs index f88731d..00189e4 100644 --- a/harmony-rs/harmony/src/domain/topology/network.rs +++ b/harmony-rs/harmony/src/domain/topology/network.rs @@ -1,5 +1,18 @@ use super::{IpAddress, LogicalHost}; +#[derive(Debug)] +pub struct DHCPStaticEntry { + pub name: String, + pub mac: MacAddress, + pub ip: IpAddress, +} + +impl std::fmt::Display for DHCPStaticEntry { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.write_fmt(format_args!("DHCPStaticEntry : name {}, mac {}, ip {}", self.name, self.mac, self.ip)) + } +} + pub trait Firewall: Send + Sync { fn add_rule(&mut self, rule: FirewallRule) -> Result<(), FirewallError>; fn remove_rule(&mut self, rule_id: &str) -> Result<(), FirewallError>; @@ -19,8 +32,8 @@ pub struct NetworkDomain { } pub trait DhcpServer: Send + Sync { - fn add_static_mapping(&mut self, mac: MacAddress, ip: IpAddress) -> Result<(), DhcpError>; - fn remove_static_mapping(&mut self, mac: &MacAddress) -> Result<(), DhcpError>; + fn add_static_mapping(&self, entry: &DHCPStaticEntry) -> Result<(), DhcpError>; + fn remove_static_mapping(&self, mac: &MacAddress) -> Result<(), DhcpError>; fn list_static_mappings(&self) -> Vec<(MacAddress, IpAddress)>; fn get_ip(&self) -> IpAddress; fn get_host(&self) -> LogicalHost; @@ -78,7 +91,7 @@ pub struct MacAddress(pub [u8; 6]); impl MacAddress { pub fn dummy() -> Self { - Self([0,0,0,0,0,0]) + Self([0, 0, 0, 0, 0, 0]) } } diff --git a/harmony-rs/harmony/src/infra/opnsense/mod.rs b/harmony-rs/harmony/src/infra/opnsense/mod.rs index 35e2a2e..92f1826 100644 --- a/harmony-rs/harmony/src/infra/opnsense/mod.rs +++ b/harmony-rs/harmony/src/infra/opnsense/mod.rs @@ -2,8 +2,8 @@ mod management; pub use management::*; use crate::topology::{ - Backend, DhcpServer, DnsServer, Firewall, FirewallError, FirewallRule, Frontend, IpAddress, - LoadBalancer, LoadBalancerError, LogicalHost, + Backend, DHCPStaticEntry, DhcpServer, DnsServer, Firewall, FirewallError, FirewallRule, + Frontend, IpAddress, LoadBalancer, LoadBalancerError, LogicalHost, }; use derive_new::new; @@ -34,7 +34,7 @@ impl Firewall for OPNSenseFirewall { fn get_ip(&self) -> IpAddress { OPNSenseFirewall::get_ip(self) } - fn get_host(&self) -> LogicalHost{ + fn get_host(&self) -> LogicalHost { self.host.clone() } } @@ -67,22 +67,21 @@ impl LoadBalancer for OPNSenseFirewall { fn get_ip(&self) -> IpAddress { OPNSenseFirewall::get_ip(self) } - fn get_host(&self) -> LogicalHost{ + fn get_host(&self) -> LogicalHost { self.host.clone() } } impl DhcpServer for OPNSenseFirewall { fn add_static_mapping( - &mut self, - _mac: crate::topology::MacAddress, - _ip: IpAddress, + &self, + entry: &DHCPStaticEntry, ) -> Result<(), crate::topology::DhcpError> { - todo!() + todo!("Register {:?}", entry) } fn remove_static_mapping( - &mut self, + &self, _mac: &crate::topology::MacAddress, ) -> Result<(), crate::topology::DhcpError> { todo!() @@ -95,7 +94,7 @@ impl DhcpServer for OPNSenseFirewall { fn get_ip(&self) -> IpAddress { OPNSenseFirewall::get_ip(self) } - fn get_host(&self) -> LogicalHost{ + fn get_host(&self) -> LogicalHost { self.host.clone() } } @@ -125,7 +124,7 @@ impl DnsServer for OPNSenseFirewall { OPNSenseFirewall::get_ip(&self) } - fn get_host(&self) -> LogicalHost{ + fn get_host(&self) -> LogicalHost { self.host.clone() } } diff --git a/harmony-rs/harmony/src/modules/opnsense_dhcp.rs b/harmony-rs/harmony/src/modules/dhcp.rs similarity index 82% rename from harmony-rs/harmony/src/modules/opnsense_dhcp.rs rename to harmony-rs/harmony/src/modules/dhcp.rs index 68f9e8b..ec100ea 100644 --- a/harmony-rs/harmony/src/modules/opnsense_dhcp.rs +++ b/harmony-rs/harmony/src/modules/dhcp.rs @@ -10,18 +10,11 @@ use crate::{ infra::executors::russh::RusshClient, interpret::{Interpret, InterpretError, InterpretName, Outcome}, inventory::Inventory, - topology::{HAClusterTopology, HostBinding, IpAddress, MacAddress}, + topology::{DHCPStaticEntry, HAClusterTopology, HostBinding}, }; use crate::domain::score::Score; -#[derive(Debug)] -pub struct DHCPStaticEntry { - name: String, - mac: MacAddress, - ip: IpAddress, -} - /// OPNSenseDhcpScore will set static DHCP entries using index based hostname /// and ip addresses. /// @@ -59,30 +52,30 @@ pub struct DHCPStaticEntry { /// ] /// ``` #[derive(Debug, new, Clone)] -pub struct OPNSenseDhcpScore { +pub struct DhcpScore { host_binding: Vec, } -impl Score for OPNSenseDhcpScore { - type InterpretType = OPNSenseDhcpInterpret; +impl Score for DhcpScore { + type InterpretType = DhcpInterpret; - fn create_interpret(self) -> OPNSenseDhcpInterpret { - OPNSenseDhcpInterpret::new(self) + fn create_interpret(self) -> DhcpInterpret { + DhcpInterpret::new(self) } } // https://docs.opnsense.org/manual/dhcp.html#advanced-settings #[derive(Debug, Clone)] -pub struct OPNSenseDhcpInterpret { - score: OPNSenseDhcpScore, +pub struct DhcpInterpret { + score: DhcpScore, version: Version, id: Id, name: String, status: InterpretStatus, } -impl OPNSenseDhcpInterpret { - pub fn new(score: OPNSenseDhcpScore) -> Self { +impl DhcpInterpret { + pub fn new(score: DhcpScore) -> Self { let version = Version::from("1.0.0").expect("Version should be valid"); let name = "OPNSenseDhcpScore".to_string(); let id = Id::from_string(format!("{name}_{version}")); @@ -98,7 +91,7 @@ impl OPNSenseDhcpInterpret { } #[async_trait] -impl Interpret for OPNSenseDhcpInterpret { +impl Interpret for DhcpInterpret { fn get_name(&self) -> InterpretName { InterpretName::OPNSenseDHCP } @@ -134,7 +127,16 @@ impl Interpret for OPNSenseDhcpInterpret { }) .collect(); info!("DHCPStaticEntry : {:?}", entries); - todo!("Filter proper network interfaces and prepare the DHCP configuration"); + + let dhcp = topology.dhcp_server.clone(); + info!("DHCP server : {:?}", dhcp); + entries.iter().for_each(|entry| { + match dhcp.add_static_mapping(&entry) { + Ok(_) => info!("Successfully registered DHCPStaticEntry {}", entry), + Err(_) => todo!(), + } + }); + todo!("Configure DHCPServer"); Ok(Outcome::new( InterpretStatus::SUCCESS, diff --git a/harmony-rs/harmony/src/modules/mod.rs b/harmony-rs/harmony/src/modules/mod.rs index f47d64a..5833493 100644 --- a/harmony-rs/harmony/src/modules/mod.rs +++ b/harmony-rs/harmony/src/modules/mod.rs @@ -1 +1 @@ -pub mod opnsense_dhcp; +pub mod dhcp;