-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(process-monitor): Detect new containers
Enhance the process monitor with an ability to detect when a container runtime creates a new PID namespace, which we can consider as a creation of a new container. Achieve that by: * Registering the inodes of container runtime binaries we want to track in the user-space, saving them in a BPF map. * In BPF, every time a process is being executed using the runtime binary, checking whether the PID namespace was changed.
- Loading branch information
1 parent
57fb058
commit b43c665
Showing
13 changed files
with
452 additions
and
43 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
use std::{ | ||
fs::File, | ||
io::{self, BufReader}, | ||
process::Command, | ||
}; | ||
|
||
use serde::{Deserialize, Serialize}; | ||
use thiserror::Error; | ||
use validatron::Validatron; | ||
|
||
#[derive(Error, Debug)] | ||
pub enum ContainerError { | ||
#[error("reading file {path} failed")] | ||
ReadFile { | ||
#[source] | ||
source: io::Error, | ||
path: String, | ||
}, | ||
#[error("parsing config from `{path}` failed")] | ||
ParseConfig { | ||
#[source] | ||
source: serde_json::error::Error, | ||
path: String, | ||
}, | ||
#[error("executing {command} failed")] | ||
Exec { | ||
#[source] | ||
source: io::Error, | ||
command: String, | ||
}, | ||
#[error("executing {command} failed with status {code:?}")] | ||
ExecStatus { command: String, code: Option<i32> }, | ||
#[error("parsing image digest {digest} failed")] | ||
ParseDigest { digest: String }, | ||
#[error("invalid hash function {hash_fn}")] | ||
InvalidHashFunction { hash_fn: String }, | ||
} | ||
|
||
#[derive(Clone, Debug, PartialEq, Eq)] | ||
pub enum ContainerId { | ||
Docker(String), | ||
Libpod(String), | ||
} | ||
|
||
#[derive(Debug, Deserialize)] | ||
struct DockerConfig { | ||
#[serde(rename = "Config")] | ||
config: DockerContainerConfig, | ||
#[serde(rename = "Image")] | ||
image_digest: String, | ||
#[serde(rename = "Name")] | ||
name: String, | ||
} | ||
|
||
#[derive(Debug, Deserialize)] | ||
struct DockerContainerConfig { | ||
#[serde(rename = "Image")] | ||
image: String, | ||
} | ||
|
||
#[derive(Debug, Deserialize)] | ||
struct LibpodConfig { | ||
#[serde(rename = "Name")] | ||
name: String, | ||
#[serde(rename = "Image")] | ||
image: String, | ||
#[serde(rename = "ImageDigest")] | ||
image_digest: String, | ||
} | ||
|
||
#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize, Validatron)] | ||
pub struct ContainerInfo { | ||
pub id: String, | ||
pub name: String, | ||
pub image: String, | ||
pub image_digest: String, | ||
} | ||
|
||
pub fn get_container_info(id: ContainerId) -> Result<ContainerInfo, ContainerError> { | ||
match id { | ||
ContainerId::Docker(id) => { | ||
let path = format!("/var/lib/docker/containers/{}/config.v2.json", id); | ||
let file = File::open(&path).map_err(|source| ContainerError::ReadFile { | ||
source, | ||
path: path.clone(), | ||
})?; | ||
|
||
let reader = BufReader::new(file); | ||
let config: DockerConfig = serde_json::from_reader(reader) | ||
.map_err(|source| ContainerError::ParseConfig { source, path })?; | ||
|
||
let name = config.name; | ||
let name = if name.starts_with('/') { | ||
name[1..].to_owned() | ||
} else { | ||
name | ||
}; | ||
let image = config.config.image; | ||
let image_digest = config.image_digest; | ||
|
||
Ok(ContainerInfo { | ||
id, | ||
name, | ||
image, | ||
image_digest, | ||
}) | ||
} | ||
ContainerId::Libpod(id) => { | ||
// TODO(vadorovsky): Find a file from which that information | ||
// could be retrieved. | ||
let output = Command::new("podman") | ||
.arg("inspect") | ||
.arg("--type=container") | ||
.arg(&id) | ||
.output() | ||
.map_err(|source| ContainerError::Exec { | ||
source, | ||
command: "podman".to_owned(), | ||
})?; | ||
|
||
if !output.status.success() { | ||
return Err(ContainerError::ExecStatus { | ||
command: "podman".to_owned(), | ||
code: output.status.code(), | ||
}); | ||
} | ||
|
||
let config: LibpodConfig = | ||
serde_json::from_slice(&output.stdout).map_err(|source| { | ||
ContainerError::ParseConfig { | ||
source, | ||
path: format!("podman inspect --type=container {id}"), | ||
} | ||
})?; | ||
|
||
let name = config.name; | ||
let image = config.image; | ||
let image_digest = config.image_digest; | ||
|
||
Ok(ContainerInfo { | ||
id, | ||
name, | ||
image, | ||
image_digest, | ||
}) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
pub mod containers; | ||
pub mod procfs; | ||
|
||
mod buffer_index; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.