feat: DhcpConfig can now effectively manage a config file to add a static map entry

This commit is contained in:
Jean-Gabriel Gill-Couture
2024-11-18 17:05:48 -05:00
parent cb1fea1eda
commit cc9bcb902c
13 changed files with 1595 additions and 1114 deletions

View File

@@ -170,9 +170,11 @@ impl Config {
#[cfg(test)]
mod tests {
use crate::modules::dhcp::DhcpConfig;
use super::*;
use std::path::PathBuf;
use pretty_assertions::assert_eq;
use std::path::PathBuf;
#[tokio::test]
async fn test_load_config_from_local_file() {
@@ -199,23 +201,31 @@ mod tests {
#[tokio::test]
async fn test_add_dhcpd_static_entry() {
let mut test_file_path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
test_file_path.push("src/tests/data/config-full-1.xml");
test_file_path.push("src/tests/data/config-structure.xml");
let config_file_path = test_file_path.to_str().unwrap().to_string();
println!("File path {config_file_path}");
let repository = Box::new(LocalFileConfigRepository::new(config_file_path));
let config_file_str = repository.load().await.unwrap();
let mut config = Config::new(repository)
.await
.expect("Failed to load config");
println!("Config {:?}", config);
let mut dhcp_config = DhcpConfig::new(&mut config.opnsense);
dhcp_config.add_static_mapping("00:00:00:00:00:00", Ipv4Addr::new(192,168,20,100), "hostname").expect("Should add static mapping");
let serialized = config.opnsense.to_xml();
fs::write("/tmp/serialized.xml", &serialized).unwrap();
assert_eq!(config_file_str, serialized);
todo!();
let mut test_file_path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
test_file_path.push("src/tests/data/config-structure-with-dhcp-staticmap-entry.xml");
let config_file_path = test_file_path.to_str().unwrap().to_string();
println!("File path {config_file_path}");
let repository = Box::new(LocalFileConfigRepository::new(config_file_path));
let expected_config_file_str = repository.load().await.unwrap();
assert_eq!(expected_config_file_str, serialized);
}
}

View File

@@ -51,7 +51,16 @@ impl<'a> DhcpConfig<'a> {
) -> Result<(), DhcpError> {
let mac = mac.to_string();
let hostname = hostname.to_string();
let range = &self.opnsense.dhcpd.lan.range;
let lan_dhcpd = &mut self
.opnsense
.dhcpd
.elements
.iter_mut()
.find(|(name, _config)| return name == "lan")
.expect("Interface lan should have dhcpd activated")
.1;
let range = &lan_dhcpd.range;
if !Self::is_valid_mac(&mac) {
return Err(DhcpError::InvalidMacAddress(mac));
@@ -61,7 +70,7 @@ impl<'a> DhcpConfig<'a> {
return Err(DhcpError::IpAddressOutOfRange(ipaddr.to_string()));
}
let existing_mappings = &self.opnsense.dhcpd.lan.staticmaps;
let existing_mappings: &mut Vec<StaticMap> = &mut lan_dhcpd.staticmaps;
if existing_mappings.iter().any(|m| {
m.ipaddr
@@ -86,14 +95,10 @@ impl<'a> DhcpConfig<'a> {
ntpserver: Default::default(),
};
self.opnsense.dhcpd.lan.staticmaps.push(static_map);
existing_mappings.push(static_map);
Ok(())
}
pub fn get_static_mappings(&self) -> &[StaticMap] {
&self.opnsense.dhcpd.lan.staticmaps
}
fn is_valid_mac(mac: &str) -> bool {
let parts: Vec<&str> = mac.split(':').collect();
if parts.len() != 6 {
@@ -158,5 +163,4 @@ mod test {
let ip = "192.168.1.201".parse::<Ipv4Addr>().unwrap();
assert_eq!(DhcpConfig::is_ip_in_range(&ip, &range), false);
}
}

View File

@@ -320,9 +320,9 @@
<enable>1</enable>
<lock>1</lock>
<spoofmac/>
<ipaddr>pppoe</ipaddr>
<blockpriv>1</blockpriv>
<blockbogons>1</blockbogons>
<ipaddr>pppoe</ipaddr>
</wan>
<lan>
<if>em1</if>
@@ -337,15 +337,15 @@
</lan>
<lo0>
<internal_dynamic>1</internal_dynamic>
<if>lo0</if>
<descr>Loopback</descr>
<enable>1</enable>
<if>lo0</if>
<ipaddr>127.0.0.1</ipaddr>
<ipaddrv6>::1</ipaddrv6>
<subnet>8</subnet>
<subnetv6>128</subnetv6>
<type>none</type>
<virtual>1</virtual>
<subnet>8</subnet>
<ipaddrv6>::1</ipaddrv6>
<subnetv6>128</subnetv6>
</lo0>
<opt1>
<if>em5</if>
@@ -358,18 +358,18 @@
</opt1>
<wireguard>
<internal_dynamic>1</internal_dynamic>
<descr>WireGuard (Group)</descr>
<if>wireguard</if>
<virtual>1</virtual>
<descr>WireGuard (Group)</descr>
<enable>1</enable>
<type>group</type>
<virtual>1</virtual>
<networks/>
</wireguard>
<openvpn>
<internal_dynamic>1</internal_dynamic>
<enable>1</enable>
<if>openvpn</if>
<descr>OpenVPN</descr>
<enable>1</enable>
<type>group</type>
<virtual>1</virtual>
<networks/>

View File

@@ -132,9 +132,9 @@
<enable>1</enable>
<lock>1</lock>
<spoofmac/>
<ipaddr>pppoe</ipaddr>
<blockpriv>1</blockpriv>
<blockbogons>1</blockbogons>
<ipaddr>pppoe</ipaddr>
</wan>
<lan>
<if>em1</if>
@@ -149,15 +149,15 @@
</lan>
<lo0>
<internal_dynamic>1</internal_dynamic>
<if>lo0</if>
<descr>Loopback</descr>
<enable>1</enable>
<if>lo0</if>
<ipaddr>127.0.0.1</ipaddr>
<ipaddrv6>::1</ipaddrv6>
<subnet>8</subnet>
<subnetv6>128</subnetv6>
<type>none</type>
<virtual>1</virtual>
<subnet>8</subnet>
<ipaddrv6>::1</ipaddrv6>
<subnetv6>128</subnetv6>
</lo0>
<opt1>
<if>em5</if>
@@ -170,18 +170,18 @@
</opt1>
<wireguard>
<internal_dynamic>1</internal_dynamic>
<descr>WireGuard (Group)</descr>
<if>wireguard</if>
<virtual>1</virtual>
<descr>WireGuard (Group)</descr>
<enable>1</enable>
<type>group</type>
<virtual>1</virtual>
<networks/>
</wireguard>
<openvpn>
<internal_dynamic>1</internal_dynamic>
<enable>1</enable>
<if>openvpn</if>
<descr>OpenVPN</descr>
<enable>1</enable>
<type>group</type>
<virtual>1</virtual>
<networks/>
@@ -337,19 +337,19 @@
</rule>
<rule>
<associated-rule-id>nat_670979b3279551.73601303</associated-rule-id>
<interface>wan</interface>
<ipprotocol>inet</ipprotocol>
<statetype>keep state</statetype>
<descr>port forwarding for virtual ip for someservice2 servers</descr>
<category/>
<protocol>tcp</protocol>
<source>
<any>1</any>
</source>
<interface>wan</interface>
<statetype>keep state</statetype>
<protocol>tcp</protocol>
<ipprotocol>inet</ipprotocol>
<destination>
<address>192.168.20.1</address>
<port>55555</port>
</destination>
<descr>port forwarding for virtual ip for someservice2 servers</descr>
<category/>
<created>
<username>root@172.12.0.12</username>
<time>1728674227.1622</time>
@@ -1264,13 +1264,15 @@
<tcp_matchType>string</tcp_matchType>
<tcp_negate>0</tcp_negate>
<tcp_matchValue/>
<agentPort/>
<agent_port/>
<mysql_user/>
<mysql_post41>0</mysql_post41>
<pgsql_user/>
<smtp_domain/>
<esmtp_domain/>
<agentPort/>
<dbUser/>
<smtpDomain/>
</healthcheck>
</healthchecks>
<acls/>