-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: qjerome <qjerome@rawsec.lu>
- Loading branch information
Showing
7 changed files
with
134 additions
and
44 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
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,120 @@ | ||
use core::str::FromStr; | ||
use gene::FieldGetter; | ||
use kunai_common::cgroup::Cgroup; | ||
use kunai_macros::StrEnum; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
#[derive(StrEnum, Debug, PartialEq, Clone, Copy)] | ||
pub enum Container { | ||
#[str("lxc")] | ||
Lxc, | ||
#[str("docker")] | ||
Docker, | ||
#[str("firejail")] | ||
Firejail, | ||
} | ||
|
||
impl Serialize for Container { | ||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> | ||
where | ||
S: serde::Serializer, | ||
{ | ||
serializer.serialize_str(self.as_str()) | ||
} | ||
} | ||
|
||
impl<'de> Deserialize<'de> for Container { | ||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> | ||
where | ||
D: serde::Deserializer<'de>, | ||
{ | ||
// Deserialization logic goes here | ||
struct ContainerVisitor; | ||
const VARIANTS: &'static [&'static str] = &Container::variants_str(); | ||
|
||
impl<'de> serde::de::Visitor<'de> for ContainerVisitor { | ||
type Value = Container; | ||
|
||
fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result { | ||
formatter.write_str("a valid representation of Container enum") | ||
} | ||
|
||
fn visit_str<E>(self, value: &str) -> Result<Self::Value, E> | ||
where | ||
E: serde::de::Error, | ||
{ | ||
Container::from_str(value) | ||
.map_err(|_| serde::de::Error::unknown_variant(value, VARIANTS)) | ||
} | ||
} | ||
|
||
deserializer.deserialize_str(ContainerVisitor) | ||
} | ||
} | ||
|
||
impl FieldGetter for Container { | ||
fn get_from_iter( | ||
&self, | ||
i: core::slice::Iter<'_, std::string::String>, | ||
) -> Option<gene::FieldValue> { | ||
if i.len() > 0 { | ||
return None; | ||
} | ||
return Some(self.as_str().into()); | ||
} | ||
} | ||
|
||
impl Container { | ||
#[inline] | ||
pub fn from_cgroup(cgrp: &Cgroup) -> Option<Container> { | ||
let s: Vec<String> = cgrp.to_vec(); | ||
|
||
if let Some(last) = s.last() { | ||
if last.starts_with("docker-") { | ||
return Some(Container::Docker); | ||
} | ||
} | ||
|
||
if let Some(first) = s.get(1) { | ||
if first.starts_with("lxc.payload.") { | ||
return Some(Container::Lxc); | ||
} | ||
} | ||
|
||
None | ||
} | ||
|
||
#[inline] | ||
pub fn from_ancestors(ancestors: Vec<String>) -> Option<Container> { | ||
for a in ancestors { | ||
match a.as_str() { | ||
"/usr/bin/firejail" => return Some(Container::Firejail), | ||
"/usr/bin/containerd-shim-runc-v2" => return Some(Container::Docker), | ||
_ => {} | ||
}; | ||
|
||
if a.starts_with("/snap/lxd/") && a.ends_with("/bin/lxd/") { | ||
return Some(Container::Lxc); | ||
} | ||
} | ||
None | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use super::*; | ||
|
||
#[test] | ||
fn test_serde() { | ||
let c = Container::Docker; | ||
|
||
let ser = serde_json::to_string(&c).unwrap(); | ||
assert_eq!(ser, r#""docker""#); | ||
let de: Container = serde_json::from_str(&ser).unwrap(); | ||
assert_eq!(de, Container::Docker); | ||
|
||
// this is an unknown variant so we should get an error | ||
assert!(serde_json::from_str::<'_, Container>(r#""lxk""#).is_err()); | ||
} | ||
} |
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