aidaemon_tools/
path_class.rs1use std::path::{Path, PathBuf};
4
5pub const SENSITIVE_PATTERNS: &[&str] = &[
7 ".git",
8 ".ssh",
9 ".gnupg",
10 ".aws",
11 ".azure",
12 ".docker",
13 ".kube",
14 ".pulumi",
15 ".terraform",
16 ".env",
17 ".envrc",
18 ".dev.vars",
19 ".key",
20 ".pem",
21 ".aws/credentials",
22 ".netrc",
23 ".docker/config.json",
24 "credentials",
25 "id_rsa",
26 "id_ed25519",
27 ".npmrc",
28 ".pypirc",
29 ".htpasswd",
30 ".git-credentials",
31 "secrets",
32];
33
34pub const SECRET_READ_PATTERNS: &[&str] = &[
38 ".ssh",
39 ".gnupg",
40 ".aws",
41 ".azure",
42 ".kube",
43 ".env",
44 ".envrc",
45 ".dev.vars",
46 ".key",
47 ".pem",
48 ".aws/credentials",
49 ".netrc",
50 ".docker/config.json",
51 "credentials",
52 "id_rsa",
53 "id_ed25519",
54 ".npmrc",
55 ".pypirc",
56 ".htpasswd",
57 ".git-credentials",
58 "secrets",
59];
60
61#[derive(Debug, Clone, Default, PartialEq, Eq)]
66pub struct PathClassifier {
67 protected: Vec<PathBuf>,
68}
69
70impl PathClassifier {
71 pub fn new(protected: Vec<PathBuf>) -> Self {
72 Self { protected }
73 }
74
75 pub const fn empty() -> Self {
77 Self {
78 protected: Vec::new(),
79 }
80 }
81
82 pub fn protected_paths(&self) -> &[PathBuf] {
84 &self.protected
85 }
86
87 pub fn is_protected(&self, path: &Path) -> bool {
89 self.protected.iter().any(|own| same_file(own, path))
90 }
91
92 pub fn is_secret(&self, path: &Path) -> bool {
95 self.is_secret_ignoring_name_shape(path) || has_secret_name_shape(path)
96 }
97
98 pub fn is_secret_ignoring_name_shape(&self, path: &Path) -> bool {
101 matches_patterns(path, SECRET_READ_PATTERNS) || self.is_protected(path)
102 }
103
104 pub fn is_sensitive(&self, path: &Path) -> bool {
107 matches_patterns(path, SENSITIVE_PATTERNS)
108 || self.is_protected(path)
109 || has_secret_name_shape(path)
110 }
111}
112
113pub fn same_file(a: &Path, b: &Path) -> bool {
116 a == b
117 || matches!(
118 (dunce::canonicalize(a), dunce::canonicalize(b)),
119 (Ok(x), Ok(y)) if x == y
120 )
121}
122
123fn matches_patterns(path: &Path, patterns: &[&str]) -> bool {
124 let path_lower = path.to_string_lossy().to_ascii_lowercase();
125 patterns.iter().any(|pattern| {
126 if pattern.contains('/') {
127 path_lower.contains(&pattern.to_ascii_lowercase())
130 } else {
131 path.components().any(|c| {
134 c.as_os_str()
135 .to_string_lossy()
136 .eq_ignore_ascii_case(pattern)
137 }) || path
138 .file_name()
139 .is_some_and(|f| f.to_string_lossy().eq_ignore_ascii_case(pattern))
140 }
141 })
142}
143
144fn has_secret_name_shape(path: &Path) -> bool {
147 path.components().any(|component| {
148 let component = component.as_os_str().to_string_lossy().to_ascii_lowercase();
149 component.starts_with(".env.")
150 || component.starts_with(".dev.vars.")
151 || component.ends_with(".key")
152 || component.ends_with(".pem")
153 || component.ends_with(".p12")
154 || component.ends_with(".pfx")
155 || component.ends_with(".jks")
156 || component.ends_with(".keystore")
157 || component == "terraform.tfstate"
158 || component.starts_with("terraform.tfstate.")
159 })
160}
161
162#[derive(Debug, Clone, Default)]
167pub struct DaemonPaths {
168 classifier: std::sync::Arc<PathClassifier>,
169}
170
171impl DaemonPaths {
172 pub fn new(protected: Vec<PathBuf>) -> Self {
175 Self {
176 classifier: std::sync::Arc::new(PathClassifier::new(protected)),
177 }
178 }
179
180 pub fn is_secret(&self, path: &Path) -> bool {
183 self.classifier.is_secret(path) || is_configured_env_file(path)
184 }
185
186 pub fn is_secret_ignoring_name_shape(&self, path: &Path) -> bool {
189 self.classifier.is_secret_ignoring_name_shape(path) || is_configured_env_file(path)
190 }
191
192 pub fn is_sensitive(&self, path: &Path) -> bool {
194 self.classifier.is_sensitive(path) || is_configured_env_file(path)
195 }
196
197 pub fn is_own_file(&self, path: &Path) -> bool {
199 self.classifier.is_protected(path)
200 }
201}
202
203fn is_configured_env_file(path: &Path) -> bool {
206 ["AIDAEMON_ENV_FILE", "AIDAEMON_RUNTIME_ENV_FILE"]
207 .iter()
208 .filter_map(std::env::var_os)
209 .filter(|value| !value.is_empty())
210 .any(|value| same_file(Path::new(&value), path))
211}
212
213#[cfg(test)]
214mod tests {
215 use super::*;
216
217 fn path(p: &str) -> &Path {
218 Path::new(p)
219 }
220
221 #[test]
222 fn secrets_and_sensitive_paths() {
223 let classifier = PathClassifier::default();
224 for secret in [
225 "/home/synthetic/.ssh/id_ed25519",
226 "/srv/app/.env",
227 "/srv/app/.env.production",
228 "/srv/app/server.key",
229 "/home/synthetic/.aws/credentials",
230 ] {
231 assert!(classifier.is_secret(path(secret)), "{secret}");
232 assert!(classifier.is_sensitive(path(secret)), "{secret}");
233 }
234 assert!(classifier.is_sensitive(path("/srv/app/.git/config")));
236 assert!(!classifier.is_secret(path("/srv/app/.git/config")));
237 assert!(classifier.is_sensitive(path("/srv/infra/terraform.tfstate")));
238 assert!(!classifier.is_sensitive(path("/srv/app/src/main.rs")));
240 assert!(!classifier.is_sensitive(path("/srv/app/monkey.txt")));
242 }
243
244 #[test]
245 fn name_shape_rules_can_be_ignored_for_templates() {
246 let classifier = PathClassifier::default();
247 let template = path("/srv/app/config/.env.example");
248 assert!(classifier.is_secret(template));
249 assert!(!classifier.is_secret_ignoring_name_shape(template));
250 }
251
252 #[test]
253 fn the_apps_own_files_are_protected() {
254 let own = PathBuf::from("/srv/synthetic-app/app.toml");
255 let classifier = PathClassifier::new(vec![own.clone()]);
256 assert!(classifier.is_protected(&own));
257 assert!(classifier.is_secret(&own));
258 assert!(classifier.is_sensitive(&own));
259 assert!(!PathClassifier::default().is_sensitive(&own));
260 }
261}