Skip to content

Commit

Permalink
fix: Fix hubblo-org#175
Browse files Browse the repository at this point in the history
- Remove carriage return in process cmdline.
  • Loading branch information
uggla committed Jul 17, 2022
1 parent 6e2eff3 commit f1ed60e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/exporters/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -692,7 +692,7 @@ impl MetricGenerator {
attributes.insert("exe".to_string(), exe.clone());

if let Some(cmdline_str) = cmdline {
attributes.insert("cmdline".to_string(), cmdline_str.replace('\"', "\\\""));
attributes.insert("cmdline".to_string(), utils::filter_cmdline(&cmdline_str));

if self.qemu {
if let Some(vmname) = utils::filter_qemu_cmdline(&cmdline_str) {
Expand Down
20 changes: 20 additions & 0 deletions src/exporters/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,16 @@ use clap::crate_version;
use docker_sync::Docker;
use k8s_sync::{errors::KubernetesError, kubernetes::Kubernetes};

/// Returns a cmdline String filtered from potential characters that
/// could break exporters output.
///
/// Here we replace:
/// 1. Double quote by backslash double quote.
/// 2. Remove carriage return.
pub fn filter_cmdline(cmdline: &str) -> String {
cmdline.replace('\"', "\\\"").replace('\n', "")
}

/// Returns an Option containing the VM name of a qemu process.
///
/// Then VM name is extracted from the command line.
Expand Down Expand Up @@ -81,6 +91,16 @@ mod tests {
let cmdline = "qemu-system-x86_64,file=/var/lib/libvirt/qemu/domain-1-fedora33/master-key.aes-object-Sguest=";
assert_eq!(filter_qemu_cmdline(cmdline), None);
}

#[test]
// Fix bug https://github.com/hubblo-org/scaphandre/issues/175
fn test_filter_cmdline_with_carriage_return() {
let cmdline = "bash-csleep infinity;\n> echo plop";
assert_eq!(
filter_cmdline(cmdline),
String::from("bash-csleep infinity;> echo plop")
);
}
}

pub fn get_docker_client() -> Result<Docker, std::io::Error> {
Expand Down

0 comments on commit f1ed60e

Please sign in to comment.