Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hex-encode/decode the sha256 container ID #182

Merged
merged 1 commit into from
Dec 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ simple_logger = ">= 1"
syslog = "6"
thiserror = "1"
anyhow = "1"
faster-hex = "0.9"

[target.'cfg(target_os = "linux")'.dependencies]
caps = "0.5"
Expand Down
4 changes: 3 additions & 1 deletion src/coalesce.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ use std::io::Write;
use std::ops::Range;
use std::time::{SystemTime, UNIX_EPOCH};

use faster_hex::hex_string;

use serde_json::json;

use crate::constants::{msg_type::*, ARCH_NAMES, SYSCALL_NAMES};
Expand Down Expand Up @@ -988,7 +990,7 @@ impl<'a> Coalesce<'a> {
#[cfg(all(feature = "procfs", target_os = "linux"))]
if let (true, Some(c)) = (self.settings.enrich_container, &proc.container_info) {
let mut ci = Record::default();
let r = ci.put(&c.id);
let r = ci.put(hex_string(&c.id));
ci.elems
.push((Key::Literal("ID"), Value::Str(r, Quote::None)));
ev.body.insert(CONTAINER_INFO, EventValues::Single(ci));
Expand Down
14 changes: 2 additions & 12 deletions src/proc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use std::fmt::{self, Display};
use std::iter::Iterator;
use std::vec::Vec;

use serde::ser::SerializeMap;
use serde::{Serialize, Serializer};

use crate::label_matcher::LabelMatcher;
Expand All @@ -15,21 +14,12 @@ use crate::types::EventID;
#[cfg(all(feature = "procfs", target_os = "linux"))]
use crate::procfs;

#[derive(Clone, Debug, Default)]
#[derive(Clone, Debug, Default, Serialize)]
pub struct ContainerInfo {
#[serde(with = "faster_hex::nopfx_lowercase")]
pub id: Vec<u8>,
}

impl Serialize for ContainerInfo {
fn serialize<S: Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
let mut map = s.serialize_map(Some(1))?;
// safety: id contains entirely of hex-digits
let converted = unsafe { std::str::from_utf8_unchecked(&self.id) };
map.serialize_entry("id", converted)?;
map.end()
}
}

/// Host-unique identifier for processes
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ProcessKey {
Expand Down
19 changes: 9 additions & 10 deletions src/procfs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::os::unix::ffi::OsStrExt;
use std::path::Path;
use std::str::FromStr;

use faster_hex::hex_decode;
use lazy_static::lazy_static;
use nix::sys::time::TimeSpec;
use nix::time::{clock_gettime, ClockId};
Expand Down Expand Up @@ -178,15 +179,13 @@ pub(crate) fn parse_proc_pid(pid: u32) -> Result<ProcPidInfo, ProcFSError> {
})
}

fn extract_sha256(buf: &[u8]) -> Option<&[u8]> {
if buf.len() < 64 {
None
} else if buf[buf.len() - 64..].iter().all(u8::is_ascii_hexdigit) {
Some(&buf[buf.len() - 64..])
} else if buf[..64].iter().all(u8::is_ascii_hexdigit) {
Some(&buf[..64])
} else {
None
fn extract_sha256(buf: &[u8]) -> Option<Vec<u8>> {
let mut dec = [0u8; 32];
match buf.len() {
n if n < 64 => None,
_ if hex_decode(&buf[buf.len() - 64..], &mut dec).is_ok() => Some(Vec::from(dec)),
_ if hex_decode(&buf[..64], &mut dec).is_ok() => Some(Vec::from(dec)),
_ => None,
}
}

Expand All @@ -209,7 +208,7 @@ fn parse_cgroup_buf(buf: &[u8]) -> Result<Option<Vec<u8>>, ProcFSError> {
};
match extract_sha256(fragment) {
None => continue,
Some(id) => return Ok(Some(Vec::from(id))),
Some(id) => return Ok(Some(id)),
}
}
}
Expand Down
Loading