Skip to content

Commit

Permalink
fix: integrating changes from @uggla in #195
Browse files Browse the repository at this point in the history
  • Loading branch information
bpetit committed Oct 28, 2022
1 parent 85912e6 commit aa15ae2
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/exporters/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -735,7 +735,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));

#[cfg(target_os = "linux")]
if self.qemu {
Expand Down
22 changes: 21 additions & 1 deletion src/exporters/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,16 @@ 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 @@ -112,6 +122,16 @@ pub fn get_kubernetes_client() -> Result<Kubernetes, KubernetesError> {
}
}

#[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")
);
}

// Copyright 2020 The scaphandre authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -124,4 +144,4 @@ pub fn get_kubernetes_client() -> Result<Kubernetes, KubernetesError> {
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// limitations under the License.

0 comments on commit aa15ae2

Please sign in to comment.