feat: Improve DHCP architecture, much better, architecture feels good right now

This commit is contained in:
jeangab 2024-09-26 22:22:56 -04:00
parent 1e1aa53eaa
commit 13ba3964c0
4 changed files with 48 additions and 34 deletions

View File

@ -1,5 +1,18 @@
use super::{IpAddress, LogicalHost}; 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 { pub trait Firewall: Send + Sync {
fn add_rule(&mut self, rule: FirewallRule) -> Result<(), FirewallError>; fn add_rule(&mut self, rule: FirewallRule) -> Result<(), FirewallError>;
fn remove_rule(&mut self, rule_id: &str) -> Result<(), FirewallError>; fn remove_rule(&mut self, rule_id: &str) -> Result<(), FirewallError>;
@ -19,8 +32,8 @@ pub struct NetworkDomain {
} }
pub trait DhcpServer: Send + Sync { pub trait DhcpServer: Send + Sync {
fn add_static_mapping(&mut self, mac: MacAddress, ip: IpAddress) -> Result<(), DhcpError>; fn add_static_mapping(&self, entry: &DHCPStaticEntry) -> Result<(), DhcpError>;
fn remove_static_mapping(&mut self, mac: &MacAddress) -> Result<(), DhcpError>; fn remove_static_mapping(&self, mac: &MacAddress) -> Result<(), DhcpError>;
fn list_static_mappings(&self) -> Vec<(MacAddress, IpAddress)>; fn list_static_mappings(&self) -> Vec<(MacAddress, IpAddress)>;
fn get_ip(&self) -> IpAddress; fn get_ip(&self) -> IpAddress;
fn get_host(&self) -> LogicalHost; fn get_host(&self) -> LogicalHost;
@ -78,7 +91,7 @@ pub struct MacAddress(pub [u8; 6]);
impl MacAddress { impl MacAddress {
pub fn dummy() -> Self { pub fn dummy() -> Self {
Self([0,0,0,0,0,0]) Self([0, 0, 0, 0, 0, 0])
} }
} }

View File

@ -2,8 +2,8 @@ mod management;
pub use management::*; pub use management::*;
use crate::topology::{ use crate::topology::{
Backend, DhcpServer, DnsServer, Firewall, FirewallError, FirewallRule, Frontend, IpAddress, Backend, DHCPStaticEntry, DhcpServer, DnsServer, Firewall, FirewallError, FirewallRule,
LoadBalancer, LoadBalancerError, LogicalHost, Frontend, IpAddress, LoadBalancer, LoadBalancerError, LogicalHost,
}; };
use derive_new::new; use derive_new::new;
@ -34,7 +34,7 @@ impl Firewall for OPNSenseFirewall {
fn get_ip(&self) -> IpAddress { fn get_ip(&self) -> IpAddress {
OPNSenseFirewall::get_ip(self) OPNSenseFirewall::get_ip(self)
} }
fn get_host(&self) -> LogicalHost{ fn get_host(&self) -> LogicalHost {
self.host.clone() self.host.clone()
} }
} }
@ -67,22 +67,21 @@ impl LoadBalancer for OPNSenseFirewall {
fn get_ip(&self) -> IpAddress { fn get_ip(&self) -> IpAddress {
OPNSenseFirewall::get_ip(self) OPNSenseFirewall::get_ip(self)
} }
fn get_host(&self) -> LogicalHost{ fn get_host(&self) -> LogicalHost {
self.host.clone() self.host.clone()
} }
} }
impl DhcpServer for OPNSenseFirewall { impl DhcpServer for OPNSenseFirewall {
fn add_static_mapping( fn add_static_mapping(
&mut self, &self,
_mac: crate::topology::MacAddress, entry: &DHCPStaticEntry,
_ip: IpAddress,
) -> Result<(), crate::topology::DhcpError> { ) -> Result<(), crate::topology::DhcpError> {
todo!() todo!("Register {:?}", entry)
} }
fn remove_static_mapping( fn remove_static_mapping(
&mut self, &self,
_mac: &crate::topology::MacAddress, _mac: &crate::topology::MacAddress,
) -> Result<(), crate::topology::DhcpError> { ) -> Result<(), crate::topology::DhcpError> {
todo!() todo!()
@ -95,7 +94,7 @@ impl DhcpServer for OPNSenseFirewall {
fn get_ip(&self) -> IpAddress { fn get_ip(&self) -> IpAddress {
OPNSenseFirewall::get_ip(self) OPNSenseFirewall::get_ip(self)
} }
fn get_host(&self) -> LogicalHost{ fn get_host(&self) -> LogicalHost {
self.host.clone() self.host.clone()
} }
} }
@ -125,7 +124,7 @@ impl DnsServer for OPNSenseFirewall {
OPNSenseFirewall::get_ip(&self) OPNSenseFirewall::get_ip(&self)
} }
fn get_host(&self) -> LogicalHost{ fn get_host(&self) -> LogicalHost {
self.host.clone() self.host.clone()
} }
} }

View File

@ -10,18 +10,11 @@ use crate::{
infra::executors::russh::RusshClient, infra::executors::russh::RusshClient,
interpret::{Interpret, InterpretError, InterpretName, Outcome}, interpret::{Interpret, InterpretError, InterpretName, Outcome},
inventory::Inventory, inventory::Inventory,
topology::{HAClusterTopology, HostBinding, IpAddress, MacAddress}, topology::{DHCPStaticEntry, HAClusterTopology, HostBinding},
}; };
use crate::domain::score::Score; 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 /// OPNSenseDhcpScore will set static DHCP entries using index based hostname
/// and ip addresses. /// and ip addresses.
/// ///
@ -59,30 +52,30 @@ pub struct DHCPStaticEntry {
/// ] /// ]
/// ``` /// ```
#[derive(Debug, new, Clone)] #[derive(Debug, new, Clone)]
pub struct OPNSenseDhcpScore { pub struct DhcpScore {
host_binding: Vec<HostBinding>, host_binding: Vec<HostBinding>,
} }
impl Score for OPNSenseDhcpScore { impl Score for DhcpScore {
type InterpretType = OPNSenseDhcpInterpret; type InterpretType = DhcpInterpret;
fn create_interpret(self) -> OPNSenseDhcpInterpret { fn create_interpret(self) -> DhcpInterpret {
OPNSenseDhcpInterpret::new(self) DhcpInterpret::new(self)
} }
} }
// https://docs.opnsense.org/manual/dhcp.html#advanced-settings // https://docs.opnsense.org/manual/dhcp.html#advanced-settings
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct OPNSenseDhcpInterpret { pub struct DhcpInterpret {
score: OPNSenseDhcpScore, score: DhcpScore,
version: Version, version: Version,
id: Id, id: Id,
name: String, name: String,
status: InterpretStatus, status: InterpretStatus,
} }
impl OPNSenseDhcpInterpret { impl DhcpInterpret {
pub fn new(score: OPNSenseDhcpScore) -> Self { pub fn new(score: DhcpScore) -> Self {
let version = Version::from("1.0.0").expect("Version should be valid"); let version = Version::from("1.0.0").expect("Version should be valid");
let name = "OPNSenseDhcpScore".to_string(); let name = "OPNSenseDhcpScore".to_string();
let id = Id::from_string(format!("{name}_{version}")); let id = Id::from_string(format!("{name}_{version}"));
@ -98,7 +91,7 @@ impl OPNSenseDhcpInterpret {
} }
#[async_trait] #[async_trait]
impl Interpret for OPNSenseDhcpInterpret { impl Interpret for DhcpInterpret {
fn get_name(&self) -> InterpretName { fn get_name(&self) -> InterpretName {
InterpretName::OPNSenseDHCP InterpretName::OPNSenseDHCP
} }
@ -134,7 +127,16 @@ impl Interpret for OPNSenseDhcpInterpret {
}) })
.collect(); .collect();
info!("DHCPStaticEntry : {:?}", entries); 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( Ok(Outcome::new(
InterpretStatus::SUCCESS, InterpretStatus::SUCCESS,

View File

@ -1 +1 @@
pub mod opnsense_dhcp; pub mod dhcp;