feat: Improve DHCP architecture, much better, architecture feels good right now
This commit is contained in:
		
							parent
							
								
									1e1aa53eaa
								
							
						
					
					
						commit
						13ba3964c0
					
				| @ -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]) | ||||
|     } | ||||
| } | ||||
| 
 | ||||
|  | ||||
| @ -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() | ||||
|     } | ||||
| } | ||||
|  | ||||
| @ -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<HostBinding>, | ||||
| } | ||||
| 
 | ||||
| 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, | ||||
| @ -1 +1 @@ | ||||
| pub mod opnsense_dhcp; | ||||
| pub mod dhcp; | ||||
|  | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user