wip: RawXml ser/de works well, still a few more complex cases to test but seems good wnough for the opnsense config case

This commit is contained in:
Jean-Gabriel Gill-Couture 2024-10-20 07:39:41 -04:00
parent e0acbf304b
commit ebdc83b21b
6 changed files with 302 additions and 37 deletions

View File

@ -181,11 +181,20 @@ mod tests {
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 config = Config::new(repository)
.await
.expect("Failed to load config");
println!("Config {:?}", config);
assert!(false);
let yaserde_cfg = yaserde::ser::Config { perform_indent: true,
.. Default::default()
};
let serialized = yaserde::ser::to_string_with_config(&config.opnsense, &yaserde_cfg).unwrap();
fs::write("/tmp/serialized.xml", &serialized).unwrap();
assert_eq!(config_file_str, serialized);
}
}

View File

@ -0,0 +1,249 @@
use xml::reader::{EventReader, XmlEvent as ReadEvent};
use xml::writer::{EventWriter, XmlEvent as WriteEvent};
use yaserde::{ser, YaDeserialize as YaDeserializeTrait, YaSerialize as YaSerializeTrait};
use yaserde_derive::{YaDeserialize, YaSerialize};
#[derive(Debug, PartialEq, Default, YaDeserialize)]
pub struct Parent {
// pub rawxml_child: RawXml,
pub string_child: String,
pub child_child: Child,
}
#[derive(Debug, PartialEq, Default, YaDeserialize)]
pub struct Child {
pub child_val: String,
pub child_val2: String,
pub child_option: Option<String>,
}
#[derive(Debug, PartialEq, Default)]
pub struct RawXml(String);
impl YaDeserializeTrait for RawXml {
fn deserialize<R: std::io::Read>(
reader: &mut yaserde::de::Deserializer<R>,
) -> Result<Self, String> {
let mut buffer = String::new();
let mut depth = 0;
let own_name = match reader.peek()? {
ReadEvent::StartElement { name, .. } => name.local_name.clone(),
_ => return Err("RawXml Should start deserializing with StartElement".to_string()),
};
println!("RawXml deserialize from root element name : {own_name}");
loop {
let current_event = reader.peek()?.to_owned();
match current_event.clone() {
ReadEvent::StartElement {
name, attributes, ..
} => {
println!("StartElement {name} depth {depth}");
depth += 1;
let mut attr_string = String::new();
attributes.iter().for_each(|a| {
attr_string.push_str(&format!(r#" {}="{}""#, &a.name, &a.value));
});
buffer.push_str(&format!("<{}{}>", name, attr_string));
let _event = reader.next_event()?;
}
ReadEvent::EndElement { name } => {
println!("EndElement {name} depth {depth}");
depth -= 1;
buffer.push_str(&format!("</{}>", name));
println!(
"Checking if name.local_name {} matches own_name {} at depth {depth}",
&name.local_name, &own_name
);
if name.local_name == own_name && depth == 0 {
println!(
"Found next EndElement is closing my struct, breaking out of loop"
);
break;
} else {
let _event = reader.next_event()?;
}
}
ReadEvent::Characters(content) => {
println!("Characters {content} depth {depth}");
buffer.push_str(&content);
let _event = reader.next_event()?;
}
ReadEvent::StartDocument {
version,
encoding,
standalone,
} => todo!(
"StartDocument {:?} {:?} {:?}",
version,
encoding,
standalone
),
ReadEvent::EndDocument => todo!(),
ReadEvent::ProcessingInstruction { name, data } => {
todo!("ProcessingInstruction {:?}, {:?}", name, data)
}
ReadEvent::CData(cdata) => todo!("CData, {:?}", cdata),
ReadEvent::Comment(comment) => todo!("Comment, {:?}", comment),
ReadEvent::Whitespace(whitespace) => todo!("Whitespace, {:?}", whitespace),
}
let next = reader.peek()?;
println!(
"Processing done on \ncurrent_event : {:?} \nnext : {:?}",
&current_event, &next
);
}
println!("buffered events {buffer}");
Ok(RawXml(buffer))
}
}
impl YaSerializeTrait for RawXml {
fn serialize<W: std::io::Write>(&self, writer: &mut ser::Serializer<W>) -> Result<(), String> {
let content = self.0.clone();
let content = xml::EventReader::from_str(content.as_str());
let mut reader = yaserde::de::Deserializer::new(content);
loop {
let e = reader.next_event()?;
if let ReadEvent::EndDocument = e {
break;
}
writer
.write(e.as_writer_event().unwrap())
.expect("Writer should write write event");
}
Ok(())
}
fn serialize_attributes(
&self,
attributes: Vec<xml::attribute::OwnedAttribute>,
namespace: xml::namespace::Namespace,
) -> Result<
(
Vec<xml::attribute::OwnedAttribute>,
xml::namespace::Namespace,
),
String,
> {
todo!()
}
}
// impl YaSerializeTrait for RawXml {
// fn serialize<W: std::io::Write>(
// &self,
// writer: &mut yaserde::ser::Serializer<W>,
// ) -> Result<(), String> {
// let mut reader = EventReader::from_str(&self.0);
// loop {
// match reader.next() {
// Ok(ReadEvent::StartElement {
// name,
// attributes,
// namespace,
// }) => {
// let write = WriteEvent::from(reader.next().unwrap());
// writer
// .write(WriteEvent::StartElement {
// name: name.clone(),
// attributes: attributes.clone(),
// namespace: namespace.clone(),
// })
// .map_err(|e| e.to_string())?;
// }
// Ok(ReadEvent::EndElement { name }) => {
// writer
// .write(WriteEvent::EndElement { name: Some(name) })
// .map_err(|e| e.to_string())?;
// }
// Ok(ReadEvent::Characters(content)) => {
// writer
// .write(WriteEvent::Characters(&content))
// .map_err(|e| e.to_string())?;
// }
// Ok(ReadEvent::Eof) => break,
// Err(e) => return Err(e.to_string()),
// _ => {}
// }
// }
// Ok(())
// }
// }
//
#[test]
fn rawxml_should_buffer_empty_element() {
let rawxml: RawXml = yaserde::de::from_str("<something/>").unwrap();
assert_eq!(rawxml.0, String::from("<something></something>"));
}
#[test]
fn rawxml_should_buffer_elements_with_different_case_as_they_are() {
let xml = "<xml><Some_thing></Some_thing><something></something></xml>";
let rawxml: RawXml = yaserde::de::from_str(xml).unwrap();
assert_eq!(rawxml.0, String::from(xml));
}
#[test]
fn rawxml_should_buffer_elements_with_attributes() {
let xml = r#"<xml version="ababa"><Some_thing></Some_thing><something></something></xml>"#;
let rawxml: RawXml = yaserde::de::from_str(xml).unwrap();
assert_eq!(rawxml.0, String::from(xml));
}
#[test]
fn rawxml_should_handle_complex_documents() {
let xml = r#"<xml><OpenVPN version="1.0.0"><Overwrites></Overwrites><Instances></Instances><StaticKeys></StaticKeys></OpenVPN><Gateways version="0.0.1"></Gateways><HAProxy version="4.0.0"><general><enabled>1</enabled><gracefulStop>0</gracefulStop><hardStopAfter>60s</hardStopAfter><closeSpreadTime></closeSpreadTime><seamlessReload>0</seamlessReload><storeOcsp>0</storeOcsp><showIntro>1</showIntro><peers><enabled>0</enabled><name1></name1><listen1></listen1><port1>1024</port1><name2></name2><listen2></listen2><port2>1024</port2></peers><tuning><root>0</root><maxConnections></maxConnections><nbthread>1</nbthread><sslServerVerify>ignore</sslServerVerify><maxDHSize>2048</maxDHSize><bufferSize>16384</bufferSize></tuning></general></HAProxy></xml>"#;
let rawxml: RawXml = yaserde::de::from_str(xml).unwrap();
assert_eq!(rawxml.0, String::from(xml));
}
#[test]
fn rawxml_should_serialize_simple_documents() {
let xml = r#"<?xml version="1.0" encoding="utf-8"?><xml />"#;
let rawxml: RawXml = yaserde::de::from_str(xml).unwrap();
assert_eq!(yaserde::ser::to_string(&rawxml).unwrap(), xml);
}
#[test]
fn rawxml_should_serialize_complex_documents() {
let xml = r#"<?xml version="1.0" encoding="utf-8"?><xml><OpenVPN version="1.0.0"><Overwrites /><Instances /><StaticKeys /></OpenVPN><Gateways version="0.0.1" /><HAProxy version="4.0.0"><general><enabled>1</enabled><gracefulStop>0</gracefulStop><hardStopAfter>60s</hardStopAfter><closeSpreadTime /><seamlessReload>0</seamlessReload><storeOcsp>0</storeOcsp><showIntro>1</showIntro><peers><enabled>0</enabled><name1 /><listen1 /><port1>1024</port1><name2 /><listen2 /><port2>1024</port2></peers><tuning><root>0</root><maxConnections /><nbthread>1</nbthread><sslServerVerify>ignore</sslServerVerify><maxDHSize>2048</maxDHSize><bufferSize>16384</bufferSize></tuning></general></HAProxy></xml>"#;
let rawxml: RawXml = yaserde::de::from_str(xml).unwrap();
assert_eq!(yaserde::ser::to_string(&rawxml).unwrap(), xml);
}
#[test]
fn rawxml_should_allow_siblings_before() {
#[derive(YaDeserialize, YaSerialize)]
struct Config {
paul: Vec<String>,
raw: RawXml,
}
let xml = r#"<?xml version="1.0" encoding="utf-8"?><Config><paul>bobob</paul><paul>patate</paul><raw>allo something</raw></Config>"#;
let config: Config = yaserde::de::from_str(xml).unwrap();
assert_eq!(yaserde::ser::to_string(&config).unwrap(), xml);
}
#[test]
fn rawxml_should_allow_siblings_after() {
#[derive(YaDeserialize, YaSerialize)]
struct Config {
raw: RawXml,
paul: Vec<String>,
}
let xml = r#"<?xml version="1.0" encoding="utf-8"?><Config><raw>allo something</raw><paul>bobob</paul><paul>patate</paul></Config>"#;
let config: Config = yaserde::de::from_str(xml).unwrap();
assert_eq!(config.paul.get(0).unwrap(), "bobob");
assert_eq!(config.paul.get(1).unwrap(), "patate");
assert_eq!(config.paul.len(), 2);
assert_eq!(config.raw.0, "<raw>allo something</raw>");
assert_eq!(yaserde::ser::to_string(&config).unwrap(), xml);
}
#[test]
fn rawxml_should_allow_being_end_of_document() {
let xml = r#"<?xml version="1.0" encoding="utf-8"?><Config><raw>allo something</raw><paul>bobob</paul><paul>patate</paul></Config>"#;
let config: RawXml = yaserde::de::from_str(xml).unwrap();
assert_eq!(yaserde::ser::to_string(&config).unwrap(), xml);
}

View File

@ -0,0 +1,3 @@
pub mod generic_xml;

View File

@ -1,6 +1,7 @@
pub mod config;
pub mod modules;
pub mod error;
pub mod infra;
pub use config::Config;
pub use error::Error;

View File

@ -8,7 +8,8 @@ impl YaSerialize for Interfaces {
&self,
writer: &mut yaserde::ser::Serializer<W>,
) -> Result<(), String> {
todo!()
writer.write("Interfaces serializer TODO");
Ok(())
}
fn serialize_attributes(
@ -88,7 +89,7 @@ impl YaDeserialize for Interfaces {
}
let peek = reader.peek()?;
let mut read_next = true;
let read_next = true;
if let XmlEvent::EndElement { name } = peek {
if name.local_name == "interfaces" {
println!("Got endElement interfaces, not reading next event");

View File

@ -6,7 +6,7 @@ pub struct OPNsense {
pub dhcpd: Dhcpd,
pub theme: String,
pub sysctl: Sysctl,
pub system: System,
pub system: RawXml,
pub interfaces: Interfaces,
pub snmpd: Snmpd,
pub syslog: Syslog,
@ -107,7 +107,7 @@ pub struct Rule {
#[derive(Default, PartialEq, Debug, YaSerialize, YaDeserialize)]
pub struct Source {
pub any: Option<bool>,
pub any: Option<u8>,
}
#[derive(Default, PartialEq, Debug, YaSerialize, YaDeserialize)]
@ -168,24 +168,24 @@ pub struct System {
pub timezone: String,
pub timeservers: String,
pub webgui: WebGui,
pub usevirtualterminal: bool,
pub disableconsolemenu: bool,
pub disablevlanhwfilter: bool,
pub disablechecksumoffloading: bool,
pub disablesegmentationoffloading: bool,
pub disablelargereceiveoffloading: bool,
pub ipv6allow: bool,
pub usevirtualterminal: u8,
pub disableconsolemenu: u8,
pub disablevlanhwfilter: u8,
pub disablechecksumoffloading: u8,
pub disablesegmentationoffloading: u8,
pub disablelargereceiveoffloading: u8,
pub ipv6allow: u8,
pub powerd_ac_mode: String,
pub powerd_battery_mode: String,
pub powerd_normal_mode: String,
pub bogons: Bogons,
pub crypto_hardware: String,
pub pf_share_forward: bool,
pub lb_use_sticky: bool,
pub kill_states: bool,
pub pf_share_forward: u8,
pub lb_use_sticky: u8,
pub kill_states: u8,
pub ssh: Ssh,
pub firmware: Firmware,
pub sudo_allow_wheel: bool,
pub sudo_allow_wheel: u8,
pub sudo_allow_group: String,
pub enablenatreflectionhelper: String,
pub rulesetoptimization: String,
@ -211,23 +211,23 @@ pub struct System {
pub dns7gw: String,
#[yaserde(rename = "dns8gw")]
pub dns8gw: String,
pub dnsallowoverride: bool,
pub dnsallowoverride: u8,
pub dnsallowoverride_exclude: Option<String>,
}
#[derive(Default, PartialEq, Debug, YaSerialize, YaDeserialize)]
pub struct Ssh {
pub group: String,
pub noauto: bool,
pub noauto: u8,
pub interfaces: Option<String>,
pub kex: Option<String>,
pub ciphers: Option<String>,
pub macs: Option<String>,
pub keys: Option<String>,
pub enabled: String,
pub passwordauth: bool,
pub passwordauth: u8,
pub keysig: Option<String>,
pub permitrootlogin: bool,
pub permitrootlogin: u8,
}
#[derive(Default, PartialEq, Debug, YaSerialize, YaDeserialize)]
@ -288,6 +288,8 @@ pub struct WebGui {
use std::collections::HashMap;
use crate::infra::generic_xml::RawXml;
#[derive(Default, PartialEq, Debug)]
pub struct Interfaces {
pub interfaces: HashMap<String, Interface>,
@ -313,7 +315,7 @@ pub struct Interface {
#[yaserde(rename = "virtual")]
pub r#virtual: Option<String>,
pub subnet: Option<String>,
pub networks: Option<bool>,
pub networks: Option<u8>,
pub subnetv6: Option<String>,
pub ipaddrv6: Option<String>,
#[yaserde(rename = "track6-interface")]
@ -374,7 +376,7 @@ pub struct Snmpd {
#[derive(Default, PartialEq, Debug, YaSerialize, YaDeserialize)]
pub struct Syslog {
pub reverse: Option<bool>,
pub reverse: Option<u8>,
pub preservelogs: i32,
}
@ -399,7 +401,7 @@ pub struct NatRule {
pub ipprotocol: String,
pub descr: Option<String>,
pub tag: Option<String>,
pub tagged: Option<bool>,
pub tagged: Option<u8>,
pub poolopts: PoolOpts,
#[yaserde(rename = "associated-rule-id")]
pub associated_rule_id: String,
@ -666,7 +668,7 @@ pub struct Capture {
#[yaserde(rename = "interfaces")]
pub interfaces: Option<String>,
#[yaserde(rename = "egress_only")]
pub egress_only: Option<bool>,
pub egress_only: Option<u8>,
#[yaserde(rename = "version")]
pub version: Option<String>,
#[yaserde(rename = "targets")]
@ -1536,7 +1538,7 @@ pub struct Backend {
#[yaserde(rename = "id")]
pub id: String,
#[yaserde(rename = "enabled")]
pub enabled: bool,
pub enabled: u8,
#[yaserde(rename = "name")]
pub name: String,
#[yaserde(rename = "description")]
@ -1562,11 +1564,11 @@ pub struct Backend {
#[yaserde(rename = "source")]
pub source: Option<String>,
#[yaserde(rename = "healthCheckEnabled")]
pub health_check_enabled: bool,
pub health_check_enabled: u8,
#[yaserde(rename = "healthCheck")]
pub health_check: Option<String>,
#[yaserde(rename = "healthCheckLogStatus")]
pub health_check_log_status: bool,
pub health_check_log_status: u8,
#[yaserde(rename = "checkInterval")]
pub check_interval: Option<String>,
#[yaserde(rename = "checkDownInterval")]
@ -1578,9 +1580,9 @@ pub struct Backend {
#[yaserde(rename = "linkedMailer")]
pub linked_mailer: Option<String>,
#[yaserde(rename = "http2Enabled")]
pub http2_enabled: bool,
pub http2_enabled: u8,
#[yaserde(rename = "http2Enabled_nontls")]
pub http2_enabled_nontls: bool,
pub http2_enabled_nontls: u8,
#[yaserde(rename = "ba_advertised_protocols")]
pub ba_advertised_protocols: String,
#[yaserde(rename = "persistence")]
@ -1590,7 +1592,7 @@ pub struct Backend {
#[yaserde(rename = "persistence_cookiename")]
pub persistence_cookiename: Option<String>,
#[yaserde(rename = "persistence_stripquotes")]
pub persistence_stripquotes: bool,
pub persistence_stripquotes: u8,
#[yaserde(rename = "stickiness_pattern")]
pub stickiness_pattern: String,
#[yaserde(rename = "stickiness_dataTypes")]
@ -1616,7 +1618,7 @@ pub struct Backend {
#[yaserde(rename = "stickiness_bytesOutRatePeriod")]
pub stickiness_bytes_out_rate_period: String,
#[yaserde(rename = "basicAuthEnabled")]
pub basic_auth_enabled: bool,
pub basic_auth_enabled: u8,
#[yaserde(rename = "basicAuthUsers")]
pub basic_auth_users: Option<String>,
#[yaserde(rename = "basicAuthGroups")]
@ -1634,11 +1636,11 @@ pub struct Backend {
#[yaserde(rename = "tuning_defaultserver")]
pub tuning_defaultserver: Option<String>,
#[yaserde(rename = "tuning_noport")]
pub tuning_noport: bool,
pub tuning_noport: u8,
#[yaserde(rename = "tuning_httpreuse")]
pub tuning_httpreuse: Option<String>,
#[yaserde(rename = "tuning_caching")]
pub tuning_caching: bool,
pub tuning_caching: u8,
#[yaserde(rename = "linkedActions")]
pub linked_actions: Option<String>,
#[yaserde(rename = "linkedErrorfiles")]
@ -1658,7 +1660,7 @@ pub struct HAProxyServer {
#[yaserde(rename = "id")]
pub id: String,
#[yaserde(rename = "enabled")]
pub enabled: bool,
pub enabled: u8,
#[yaserde(rename = "name")]
pub name: String,
#[yaserde(rename = "description")]
@ -1686,11 +1688,11 @@ pub struct HAProxyServer {
#[yaserde(rename = "resolvePrefer")]
pub resolve_prefer: Option<String>,
#[yaserde(rename = "ssl")]
pub ssl: bool,
pub ssl: u8,
#[yaserde(rename = "sslSNI")]
pub ssl_sni: Option<String>,
#[yaserde(rename = "sslVerify")]
pub ssl_verify: bool,
pub ssl_verify: u8,
#[yaserde(rename = "sslCA")]
pub ssl_ca: Option<String>,
#[yaserde(rename = "sslCRL")]
@ -1731,7 +1733,7 @@ pub struct HAProxyHealthCheck {
pub ssl: String,
#[yaserde(rename = "sslSNI")]
pub ssl_sni: Option<String>,
pub force_ssl: bool,
pub force_ssl: u8,
pub checkport: Option<String>,
pub http_method: String,
pub http_uri: String,