dtpm config and proto file

This commit is contained in:
Noor 2024-12-11 16:13:28 +05:30
parent 1a88755767
commit e5ef2c1e97
4 changed files with 317 additions and 0 deletions

188
Cargo.lock generated

@ -2,6 +2,194 @@
# It is not intended for manual editing. # It is not intended for manual editing.
version = 4 version = 4
[[package]]
name = "anyhow"
version = "1.0.94"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c1fd03a028ef38ba2276dce7e33fcd6369c158a1bca17946c4b1b701891c1ff7"
[[package]]
name = "base64"
version = "0.22.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "72b3254f16251a8381aa12e40e3c4d2f0199f8c6508fbecb9d91f575e0fbb8c6"
[[package]]
name = "bytes"
version = "1.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "325918d6fe32f23b19878fe4b34794ae41fc19ddbe53b10571a4874d44ffd39b"
[[package]] [[package]]
name = "detee-shared" name = "detee-shared"
version = "0.1.0" version = "0.1.0"
dependencies = [
"base64",
"prost",
"serde",
"serde_yml",
]
[[package]]
name = "either"
version = "1.13.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "60b1af1c220855b6ceac025d3f6ecdd2b7c4894bfe9cd9bda4fbb4bc7c0d4cf0"
[[package]]
name = "equivalent"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5"
[[package]]
name = "hashbrown"
version = "0.15.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bf151400ff0baff5465007dd2f3e717f3fe502074ca563069ce3a6629d07b289"
[[package]]
name = "indexmap"
version = "2.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "62f822373a4fe84d4bb149bf54e584a7f4abec90e072ed49cda0edea5b95471f"
dependencies = [
"equivalent",
"hashbrown",
]
[[package]]
name = "itertools"
version = "0.13.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "413ee7dfc52ee1a4949ceeb7dbc8a33f2d6c088194d9f922fb8318faf1f01186"
dependencies = [
"either",
]
[[package]]
name = "itoa"
version = "1.0.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d75a2a4b1b190afb6f5425f10f6a8f959d2ea0b9c2b1d79553551850539e4674"
[[package]]
name = "libyml"
version = "0.0.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3302702afa434ffa30847a83305f0a69d6abd74293b6554c18ec85c7ef30c980"
dependencies = [
"anyhow",
"version_check",
]
[[package]]
name = "memchr"
version = "2.7.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3"
[[package]]
name = "proc-macro2"
version = "1.0.92"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "37d3544b3f2748c54e147655edb5025752e2303145b5aefb3c3ea2c78b973bb0"
dependencies = [
"unicode-ident",
]
[[package]]
name = "prost"
version = "0.13.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2c0fef6c4230e4ccf618a35c59d7ede15dea37de8427500f50aff708806e42ec"
dependencies = [
"bytes",
"prost-derive",
]
[[package]]
name = "prost-derive"
version = "0.13.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "157c5a9d7ea5c2ed2d9fb8f495b64759f7816c7eaea54ba3978f0d63000162e3"
dependencies = [
"anyhow",
"itertools",
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "quote"
version = "1.0.37"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b5b9d34b8991d19d98081b46eacdd8eb58c6f2b201139f7c5f643cc155a633af"
dependencies = [
"proc-macro2",
]
[[package]]
name = "ryu"
version = "1.0.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f3cb5ba0dc43242ce17de99c180e96db90b235b8a9fdc9543c96d2209116bd9f"
[[package]]
name = "serde"
version = "1.0.216"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0b9781016e935a97e8beecf0c933758c97a5520d32930e460142b4cd80c6338e"
dependencies = [
"serde_derive",
]
[[package]]
name = "serde_derive"
version = "1.0.216"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "46f859dbbf73865c6627ed570e78961cd3ac92407a2d117204c49232485da55e"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "serde_yml"
version = "0.0.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "59e2dd588bf1597a252c3b920e0143eb99b0f76e4e082f4c92ce34fbc9e71ddd"
dependencies = [
"indexmap",
"itoa",
"libyml",
"memchr",
"ryu",
"serde",
"version_check",
]
[[package]]
name = "syn"
version = "2.0.90"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "919d3b74a5dd0ccd15aeb8f93e7006bd9e14c295087c9896a110f490752bcf31"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]]
name = "unicode-ident"
version = "1.0.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "adb9e6ca4f869e1180728b7950e35922a7fc6397f7b641499e8f3ef06e50dc83"
[[package]]
name = "version_check"
version = "0.9.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a"

@ -4,3 +4,7 @@ version = "0.1.0"
edition = "2021" edition = "2021"
[dependencies] [dependencies]
base64 = "0.22.1"
prost = "0.13.4"
serde = { version = "1.0.216", features = ["derive"] }
serde_yml = "0.0.12"

@ -0,0 +1,39 @@
syntax = "proto3";
package dtpm_proto;
message SetConfigResponse {
string status = 1;
}
// The main Config structure
message ManagerConfigPB {
repeated FileEntry filesystem = 1;
repeated EnvironmentEntry environment = 2;
repeated ChildProcess child_processes = 3;
}
// Represents a file entry with a path and content
message FileEntry {
string path = 1;
oneof content {
string data = 2;
string file_path = 3; // For the `Path` variant in FileContent
}
}
// Represents an environment variable entry
message EnvironmentEntry {
string name = 1;
string value = 2;
}
// Represents a child process configuration
message ChildProcess {
string path = 1;
repeated string arguments = 2;
}
service ConfigManager {
rpc SetConfig(ManagerConfigPB) returns (SetConfigResponse) {}
}

@ -1 +1,87 @@
pub mod config {
use base64::{engine::general_purpose::STANDARD as BASE64, Engine};
use serde::{Deserialize, Serialize};
#[derive(Clone, Serialize, Deserialize, prost::Oneof)]
pub enum FileContent {
#[serde(rename = "path")]
#[prost(string, tag = "2")]
Path(String),
#[serde(rename = "data")]
#[prost(string, tag = "3")]
Data(String),
}
impl Default for FileContent {
fn default() -> Self {
FileContent::Data("".to_string())
}
}
#[derive(Clone, Serialize, Deserialize, prost::Message)]
pub struct FileEntry {
#[prost(string, tag = "1")]
pub path: String,
#[prost(oneof = "FileContent", tags = "2, 3")]
pub content: Option<FileContent>,
}
#[derive(Clone, Serialize, Deserialize, prost::Message)]
pub struct EnvironmentEntry {
#[prost(string, tag = "1")]
pub name: String,
#[prost(string, tag = "2")]
pub value: String,
}
#[derive(Clone, Serialize, Deserialize, prost::Message)]
pub struct ChildProcess {
#[prost(string, tag = "1")]
pub path: String,
#[prost(string, repeated, tag = "2")]
pub arguments: Vec<String>,
}
#[derive(Clone, Serialize, Deserialize, prost::Message)]
pub struct Config {
#[prost(message, repeated, tag = "1")]
pub filesystem: Vec<FileEntry>,
#[prost(message, repeated, tag = "2")]
pub environment: Vec<EnvironmentEntry>,
#[prost(message, repeated, tag = "3")]
pub child_processes: Vec<ChildProcess>,
}
impl Config {
pub fn from_path(path: &str) -> Result<Self, Box<dyn std::error::Error>> {
let config_str = std::fs::read_to_string(path)?;
Ok(serde_yml::from_str(&config_str)?)
}
pub fn _print_sample() {
println!(
"{}",
serde_yml::to_string(&Config {
filesystem: vec![FileEntry {
path: "/etc/hostname".to_string(),
content: Some(FileContent::Data("foo".to_string())),
}],
..Default::default()
})
.unwrap()
);
}
pub fn load_data(mut self) -> Result<Self, Box<dyn std::error::Error>> {
self.filesystem.iter_mut().for_each(|x| {
if let Some(FileContent::Path(path)) = &x.content {
let content = std::fs::read(path).expect("Unable to read file {path}");
let encoded = BASE64.encode(content);
x.content = Some(FileContent::Data(encoded));
}
});
Ok(self)
}
}
}