Integrated opnsense-config API in harmony, pretty easy, pretty happy. very much encouraging

This commit is contained in:
jeangab
2024-11-26 15:58:42 -05:00
parent d30e909b83
commit 9c18c8be6d
13 changed files with 108 additions and 146 deletions

View File

@@ -1,8 +1,9 @@
use std::sync::Arc;
use std::{net::Ipv4Addr, sync::Arc, time::Duration};
use crate::{error::Error, modules::dhcp::DhcpConfig};
use crate::{config::{SshConfigManager, SshCredentials, SshOPNSenseShell}, error::Error, modules::dhcp::DhcpConfig};
use log::trace;
use opnsense_config_xml::OPNsense;
use russh::client;
use super::{ConfigManager, OPNsenseShell};
@@ -39,6 +40,27 @@ impl Config {
.apply_new_config(&self.opnsense.to_xml())
.await
}
pub async fn from_credentials(ipaddr: std::net::IpAddr, username: &str, password: &str) -> Self {
let config = Arc::new(client::Config {
inactivity_timeout: Some(Duration::from_secs(5)),
..<_>::default()
});
let credentials = SshCredentials::Password {
username: String::from(username),
password: String::from(password),
};
let shell = Arc::new(SshOPNSenseShell::new(
(ipaddr, 22),
credentials,
config,
));
let manager = Arc::new(SshConfigManager::new(shell.clone()));
Config::new(manager, shell).await.unwrap()
}
}
#[cfg(test)]

View File

@@ -7,7 +7,7 @@ pub use local_file::*;
use crate::Error;
#[async_trait]
pub trait ConfigManager: std::fmt::Debug {
pub trait ConfigManager: std::fmt::Debug + Send + Sync {
async fn load_as_str(&self) -> Result<String, Error>;
async fn apply_new_config(&self, content: &str) -> Result<(), Error>;
}

View File

@@ -1,5 +1,5 @@
use std::{
net::Ipv4Addr,
net::IpAddr,
sync::Arc,
time::{SystemTime, UNIX_EPOCH},
};
@@ -20,7 +20,7 @@ use super::OPNsenseShell;
#[derive(Debug)]
pub struct SshOPNSenseShell {
host: (Ipv4Addr, u16),
host: (IpAddr, u16),
credentials: SshCredentials,
ssh_config: Arc<Config>,
}
@@ -78,11 +78,7 @@ impl SshOPNSenseShell {
wait_for_completion(&mut channel).await
}
pub fn new(
host: (Ipv4Addr, u16),
credentials: SshCredentials,
ssh_config: Arc<Config>,
) -> Self {
pub fn new(host: (IpAddr, u16), credentials: SshCredentials, ssh_config: Arc<Config>) -> Self {
Self {
host,
credentials,

View File

@@ -6,15 +6,10 @@ pub use config::Config;
pub use error::Error;
#[cfg(test)]
mod test {
use config::SshConfigManager;
use opnsense_config_xml::StaticMap;
use russh::client;
use std::{net::Ipv4Addr, sync::Arc, time::Duration};
use std::net::Ipv4Addr;
use crate::{
config::{self, SshCredentials, SshOPNSenseShell},
Config,
};
use crate::Config;
use pretty_assertions::assert_eq;
#[tokio::test]
@@ -38,23 +33,7 @@ mod test {
}
async fn initialize_config() -> Config {
let config = Arc::new(client::Config {
inactivity_timeout: Some(Duration::from_secs(5)),
..<_>::default()
});
let credentials = SshCredentials::Password {
username: String::from("root"),
password: String::from("opnsense"),
};
let shell = Arc::new(SshOPNSenseShell::new(
(Ipv4Addr::new(192, 168, 5, 229), 22),
credentials,
config,
));
let manager = Arc::new(SshConfigManager::new(shell.clone()));
Config::new(manager, shell).await.unwrap()
Config::from_credentials(Ipv4Addr::new(192, 168, 5, 229), "root", "opnsense").await
}
async fn get_static_mappings() -> Vec<StaticMap> {