Skip to content

Commit 91f278b

Browse files
authored
Update main.rs
1 parent 05af2ae commit 91f278b

File tree

1 file changed

+117
-0
lines changed

1 file changed

+117
-0
lines changed

policy-manager/src/main.rs

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,118 @@
1+
// src/main.rs
2+
// Policy manager for Security Mode, written in Rust.
3+
// It manages profiles: "agresywny", "bezpieczny", "monitor-only"
4+
// Each profile defines capabilities, network, disk access.
5+
// CLI commands: list, get <profile>
6+
// Outputs JSON to stdout or to file in /tmp/Security-Mode/policy.json
17

8+
use anyhow::{Context, Result};
9+
use log::{error, info};
10+
use serde::{Deserialize, Serialize};
11+
use std::collections::HashMap;
12+
use std::fs::File;
13+
use std::io::Write;
14+
use std::path::Path;
15+
16+
const TMP_DIR: &str = "/tmp/Security-Mode";
17+
18+
#[derive(Serialize, Deserialize, Debug, Clone)]
19+
struct ProfileConfig {
20+
capabilities: Vec<String>,
21+
network: String,
22+
disk_access: String,
23+
}
24+
25+
fn get_profiles() -> HashMap<String, ProfileConfig> {
26+
let mut profiles = HashMap::new();
27+
profiles.insert(
28+
"agresywny".to_string(),
29+
ProfileConfig {
30+
capabilities: vec!["CAP_NET_ADMIN".to_string(), "CAP_SYS_ADMIN".to_string()],
31+
network: "bridge".to_string(),
32+
disk_access: "full".to_string(),
33+
},
34+
);
35+
profiles.insert(
36+
"bezpieczny".to_string(),
37+
ProfileConfig {
38+
capabilities: vec![],
39+
network: "isolated".to_string(),
40+
disk_access: "read-only".to_string(),
41+
},
42+
);
43+
profiles.insert(
44+
"monitor-only".to_string(),
45+
ProfileConfig {
46+
capabilities: vec![],
47+
network: "none".to_string(),
48+
disk_access: "none".to_string(),
49+
},
50+
);
51+
profiles
52+
}
53+
54+
fn ensure_tmp_dir() -> Result<()> {
55+
std::fs::create_dir_all(TMP_DIR).context("Failed to create tmp dir")
56+
}
57+
58+
fn write_json<P: AsRef<Path>, T: Serialize>(path: P, data: &T) -> Result<()> {
59+
let json = serde_json::to_string(data)?;
60+
let mut file = File::create(path)?;
61+
file.write_all(json.as_bytes())?;
62+
Ok(())
63+
}
64+
65+
fn handle_list() -> Result<()> {
66+
let profiles = get_profiles();
67+
let keys: Vec<String> = profiles.keys().cloned().collect();
68+
let data = HashMap::from([("profiles".to_string(), keys)]);
69+
let json = serde_json::to_string(&data)?;
70+
println!("{}", json);
71+
Ok(())
72+
}
73+
74+
fn handle_get(profile: &str) -> Result<()> {
75+
let profiles = get_profiles();
76+
if let Some(config) = profiles.get(profile) {
77+
ensure_tmp_dir()?;
78+
write_json(format!("{}/policy.json", TMP_DIR), config)?;
79+
info!("Wrote policy for {} to {}/policy.json", profile, TMP_DIR);
80+
println!("{}", serde_json::to_string(config)?);
81+
} else {
82+
error!("Unknown profile: {}", profile);
83+
return Err(anyhow::anyhow!("Unknown profile"));
84+
}
85+
Ok(())
86+
}
87+
88+
fn main() -> Result<()> {
89+
env_logger::init();
90+
91+
let args: Vec<String> = std::env::args().collect();
92+
if args.len() < 2 {
93+
println!("Usage: policy-manager <command> [args]");
94+
println!("Commands:");
95+
println!(" list - List available profiles (JSON)");
96+
println!(" get <profile> - Get config for profile and write to policy.json");
97+
return Ok(());
98+
}
99+
100+
let command = &args[1];
101+
match command.as_str() {
102+
"list" => handle_list()?,
103+
"get" => {
104+
if args.len() > 2 {
105+
handle_get(&args[2])?;
106+
} else {
107+
error!("Missing profile for get command");
108+
return Err(anyhow::anyhow!("Missing profile"));
109+
}
110+
}
111+
_ => {
112+
error!("Unknown command: {}", command);
113+
return Err(anyhow::anyhow!("Unknown command"));
114+
}
115+
}
116+
117+
Ok(())
118+
}

0 commit comments

Comments
 (0)