Skip to content

Commit

Permalink
feat: update sed command to work for windows os
Browse files Browse the repository at this point in the history
  • Loading branch information
MarantosGeorge committed Jan 15, 2025
1 parent dd62b61 commit fb65a5e
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 7 deletions.
10 changes: 10 additions & 0 deletions wazuh-cert-oauth2-client/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion wazuh-cert-oauth2-client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ openssl = { version = "0.10.66", features = ["vendored"] }
mid = "3.0.0"
local-ip-address = "0.6.3"
rand = "0.8.5"
diacritics = "0.2.2"
diacritics = "0.2.2"
quick-xml = "0.30"
6 changes: 4 additions & 2 deletions wazuh-cert-oauth2-client/src/services/set_name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,20 @@ pub async fn set_name(name: &str) -> Result<()> {
info!("Updating agent name to {} in {}", agent_name, ossec_conf);

let update_cmd = format!(r"s|<agent_name>.*</agent_name>|<agent_name>{}</agent_name>|g", agent_name);
sed_command(&update_cmd, &ossec_conf).await?;
sed_command(&update_cmd, &ossec_conf, &agent_name).await?;

info!("Agent name updated to {}", agent_name);
restart_agent().await?;
Ok(())
}



#[cfg(target_os = "windows")]
async fn restart_agent() -> Result<()> {
let status = Command::new("Restart-Service")
.arg("-Name")
.arg("wazuh")
.arg("WazuhSvc")
.status().await?;

if !status.success() {
Expand Down
61 changes: 57 additions & 4 deletions wazuh-cert-oauth2-client/src/shared/sed_command.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,77 @@
use anyhow::Result;
use std::process::ExitStatus;
use tokio::process::Command;
use tokio::{process::Command};
use std::fs::{self, OpenOptions};
use quick_xml::events::{Event, BytesStart, BytesText};
use quick_xml::Reader;
use quick_xml::Writer;
use std::io::{BufReader};
use std::os::unix::process::ExitStatusExt;

/// Run a sed command to replace the content of a file.
pub async fn sed_command(content: &str, file_path: &str) -> Result<ExitStatus> {
pub async fn sed_command(content: &str, file_path: &str, agent_name: &str) -> Result<ExitStatus> {
let status = if cfg!(target_os = "macos") {
Command::new("sed")
.arg("-i").arg("")
.arg(&content)
.arg(&file_path)
.status()
.await?
} else {
} else if cfg!(target_os = "linux") {
Command::new("sed")
.arg("-i")
.arg(&content)
.arg(&file_path)
.status()
.await?
} else {
// Handle the case for Windows or other OSs
let input_file = fs::File::open(file_path)?;
let buf_reader = BufReader::new(input_file);
let mut reader = Reader::from_reader(buf_reader);
reader.trim_text(true);

let output_file = OpenOptions::new().write(true).truncate(true).open(file_path)?;
let mut writer = Writer::new(output_file);

let mut buf = Vec::new();
let mut in_agent_name = false;

loop {
match reader.read_event_into(&mut buf) {
Ok(Event::Start(e)) => {
if e.name() == quick_xml::name::QName(b"agent_name") {
in_agent_name = true;
writer.write_event(Event::Start(e.clone()))?;
} else {
writer.write_event(Event::Start(e))?;
}
}
Ok(Event::Text(e)) => {
if in_agent_name {
writer.write_event(Event::Text(BytesText::new(agent_name)))?;
} else {
writer.write_event(Event::Text(e))?;
}
}
Ok(Event::End(e)) => {
if e.name() == quick_xml::name::QName(b"agent_name") {
in_agent_name = false;
writer.write_event(Event::End(e.clone()))?;
} else {
writer.write_event(Event::End(e))?;
}
}
Ok(Event::Eof) => break,
Err(e) => panic!("Error at position {}: {:?}", reader.buffer_position(), e),
_ => (),
}
buf.clear();
}

// Return a dummy ExitStatus, as no system command is run for this block
ExitStatus::from_raw(0).into()
};

Ok(status)
}
}

0 comments on commit fb65a5e

Please sign in to comment.