1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
//! Telemetry configuration storage and consent management.
//!
//! Configuration is stored at `dirs::config_dir()/agnix/telemetry.json`.
//! The configuration includes:
//! - `enabled`: Whether telemetry is enabled (opt-in)
//! - `installation_id`: Random UUID for aggregate analysis (not tied to identity)
use serde::{Deserialize, Serialize};
use std::env;
use std::fs;
use std::io;
use std::path::PathBuf;
/// Telemetry configuration.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct TelemetryConfig {
/// Whether telemetry collection is enabled.
/// Default: false (opt-in only)
#[serde(default)]
pub enabled: bool,
/// Random installation ID for aggregate analysis.
/// This is NOT tied to user identity - it's a random UUID
/// generated on first consent.
#[serde(skip_serializing_if = "Option::is_none")]
pub installation_id: Option<String>,
/// When consent was given (ISO 8601 timestamp).
#[serde(skip_serializing_if = "Option::is_none")]
pub consent_timestamp: Option<String>,
/// Telemetry endpoint URL (for testing/self-hosting).
#[serde(skip_serializing_if = "Option::is_none")]
pub endpoint: Option<String>,
}
impl TelemetryConfig {
/// Load configuration from disk, or return default if not found.
pub fn load() -> io::Result<Self> {
let path = Self::config_path()?;
if !path.exists() {
return Ok(Self::default());
}
let content = fs::read_to_string(&path)?;
let config: Self = serde_json::from_str(&content).map_err(|e| {
io::Error::new(
io::ErrorKind::InvalidData,
format!("Invalid telemetry config: {}", e),
)
})?;
Ok(config)
}
/// Save configuration to disk.
pub fn save(&self) -> io::Result<()> {
let path = Self::config_path()?;
// Ensure parent directory exists
if let Some(parent) = path.parent() {
fs::create_dir_all(parent)?;
}
let content = serde_json::to_string_pretty(self).map_err(|e| {
io::Error::new(
io::ErrorKind::InvalidData,
format!("Failed to serialize config: {}", e),
)
})?;
fs::write(&path, content)?;
Ok(())
}
/// Check if telemetry is effectively enabled.
///
/// Returns false if:
/// - Config has `enabled: false`
/// - DO_NOT_TRACK environment variable is set (any value)
/// - AGNIX_TELEMETRY=0 or AGNIX_TELEMETRY=false
/// - CI environment variable is set (running in CI)
/// - GITHUB_ACTIONS environment variable is set
/// - TF_BUILD (Azure Pipelines), TRAVIS, CIRCLECI, etc.
pub fn is_enabled(&self) -> bool {
// Check explicit config setting first
if !self.enabled {
return false;
}
// Respect DO_NOT_TRACK (https://consoledonottrack.com/)
if env::var("DO_NOT_TRACK").is_ok() {
return false;
}
// Check AGNIX_TELEMETRY override
if let Ok(val) = env::var("AGNIX_TELEMETRY") {
let val = val.to_lowercase();
if val == "0" || val == "false" || val == "no" || val == "off" {
return false;
}
}
// Disable in CI environments
if Self::is_ci_environment() {
return false;
}
true
}
/// Check if running in a CI environment.
fn is_ci_environment() -> bool {
// Standard CI env var
if env::var("CI").is_ok() {
return true;
}
// Common CI systems
let ci_vars = [
"GITHUB_ACTIONS",
"GITLAB_CI",
"TRAVIS",
"CIRCLECI",
"JENKINS_URL",
"TF_BUILD", // Azure Pipelines
"BUILDKITE",
"TEAMCITY_VERSION",
"CODEBUILD_BUILD_ID", // AWS CodeBuild
"DRONE",
];
ci_vars.iter().any(|var| env::var(var).is_ok())
}
/// Enable telemetry with consent.
pub fn enable(&mut self) -> io::Result<()> {
self.enabled = true;
// Generate installation ID if not present
if self.installation_id.is_none() {
self.installation_id = Some(generate_uuid());
}
// Record consent timestamp
self.consent_timestamp = Some(super::chrono_timestamp());
self.save()
}
/// Disable telemetry.
pub fn disable(&mut self) -> io::Result<()> {
self.enabled = false;
// Keep installation_id for if user re-enables
// Clear consent timestamp
self.consent_timestamp = None;
self.save()
}
/// Get the path to the telemetry config file.
pub fn config_path() -> io::Result<PathBuf> {
let config_dir = dirs::config_dir().ok_or_else(|| {
io::Error::new(
io::ErrorKind::NotFound,
"Could not determine config directory",
)
})?;
Ok(config_dir.join("agnix").join("telemetry.json"))
}
/// Get the telemetry endpoint URL.
#[allow(dead_code)] // Used when telemetry submission feature is compiled in
pub fn endpoint(&self) -> &str {
self.endpoint
.as_deref()
.unwrap_or("https://telemetry.agnix.dev/v1/events")
}
}
/// Generate a random UUID v4 using a CSPRNG.
fn generate_uuid() -> String {
uuid::Uuid::new_v4().to_string()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_default_config_is_disabled() {
let config = TelemetryConfig::default();
assert!(!config.enabled);
assert!(config.installation_id.is_none());
}
#[test]
fn test_is_enabled_respects_config() {
let config = TelemetryConfig::default();
assert!(!config.is_enabled());
let _config = TelemetryConfig {
enabled: true,
installation_id: Some(generate_uuid()),
..Default::default()
};
// Note: config.is_enabled() may still return false if running in CI,
// so we don't assert on it here.
}
#[test]
fn test_generate_uuid_format() {
let uuid = generate_uuid();
// UUID v4 format: xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx
assert_eq!(uuid.len(), 36);
assert_eq!(uuid.chars().nth(8), Some('-'));
assert_eq!(uuid.chars().nth(13), Some('-'));
assert_eq!(uuid.chars().nth(18), Some('-'));
assert_eq!(uuid.chars().nth(23), Some('-'));
// Version 4
assert_eq!(uuid.chars().nth(14), Some('4'));
// Variant (8, 9, a, or b)
let variant_char = uuid.chars().nth(19).expect("UUID v4 must be 36 characters");
assert!(matches!(variant_char, '8' | '9' | 'a' | 'b'));
}
#[test]
fn test_generate_uuid_uniqueness() {
let uuid1 = generate_uuid();
// Small sleep to ensure time-based component changes
std::thread::sleep(std::time::Duration::from_millis(1));
let uuid2 = generate_uuid();
assert_ne!(uuid1, uuid2);
}
#[test]
fn test_ci_detection() {
// This test checks the function doesn't panic
// Actual CI status depends on environment
let _ = TelemetryConfig::is_ci_environment();
}
#[test]
fn test_config_serialization() {
let config = TelemetryConfig {
enabled: true,
installation_id: Some("test-id".to_string()),
consent_timestamp: Some("2024-01-01T00:00:00Z".to_string()),
endpoint: None,
};
let json = serde_json::to_string(&config).unwrap();
assert!(json.contains("\"enabled\":true"));
assert!(json.contains("\"installation_id\":\"test-id\""));
// endpoint should not be serialized when None
assert!(!json.contains("endpoint"));
let parsed: TelemetryConfig = serde_json::from_str(&json).unwrap();
assert_eq!(parsed.enabled, config.enabled);
assert_eq!(parsed.installation_id, config.installation_id);
}
// Mutex to serialize tests that modify environment variables.
// Rust tests run in parallel by default, and env vars are process-wide,
// so concurrent modifications cause flaky failures.
static ENV_MUTEX: std::sync::Mutex<()> = std::sync::Mutex::new(());
/// RAII guard that restores an environment variable to its original state on drop.
/// This ensures cleanup even if a test panics.
struct EnvGuard {
key: &'static str,
original: Option<String>,
}
impl EnvGuard {
/// Set an environment variable, returning a guard that restores the original value on drop.
fn set(key: &'static str, value: &str) -> Self {
let original = std::env::var(key).ok();
unsafe { std::env::set_var(key, value) };
Self { key, original }
}
/// Remove an environment variable, returning a guard that restores the original value on drop.
fn remove(key: &'static str) -> Self {
let original = std::env::var(key).ok();
unsafe { std::env::remove_var(key) };
Self { key, original }
}
}
impl Drop for EnvGuard {
fn drop(&mut self) {
match &self.original {
Some(val) => unsafe { std::env::set_var(self.key, val) },
None => unsafe { std::env::remove_var(self.key) },
}
}
}
#[test]
fn test_do_not_track_disables_telemetry() {
// Hold the lock for the entire test to prevent concurrent env var mutations.
let _guard = ENV_MUTEX.lock().unwrap();
// Set DO_NOT_TRACK; EnvGuard restores the original value on drop (even on panic).
let _env = EnvGuard::set("DO_NOT_TRACK", "1");
let config = TelemetryConfig {
enabled: true,
installation_id: Some(generate_uuid()),
..Default::default()
};
assert!(
!config.is_enabled(),
"DO_NOT_TRACK should disable telemetry"
);
}
#[test]
fn test_agnix_telemetry_env_overrides() {
// Hold the lock for the entire test to prevent concurrent env var mutations.
let _guard = ENV_MUTEX.lock().unwrap();
// Clear DO_NOT_TRACK to isolate this test; guard restores on drop.
let _env_dnt = EnvGuard::remove("DO_NOT_TRACK");
let config = TelemetryConfig {
enabled: true,
installation_id: Some(generate_uuid()),
..Default::default()
};
// Test various override values
for val in &["0", "false", "no", "off"] {
let _env = EnvGuard::set("AGNIX_TELEMETRY", val);
assert!(
!config.is_enabled(),
"AGNIX_TELEMETRY={} should disable telemetry",
val
);
}
}
#[test]
fn test_enable_generates_installation_id() {
let mut config = TelemetryConfig::default();
assert!(config.installation_id.is_none());
let _ = config.enable();
assert!(config.enabled);
assert!(config.installation_id.is_some());
assert!(config.consent_timestamp.is_some());
// Second enable should preserve the ID
let id = config.installation_id.clone();
let _ = config.enable();
assert_eq!(
config.installation_id, id,
"enable() should preserve existing ID"
);
}
#[test]
fn test_disable_preserves_installation_id() {
let mut config = TelemetryConfig::default();
let _ = config.enable();
let id = config.installation_id.clone();
let _ = config.disable();
assert!(!config.enabled);
assert_eq!(
config.installation_id, id,
"disable() should preserve installation_id"
);
// Note: disable() clears consent_timestamp by design
assert!(
config.consent_timestamp.is_none(),
"disable() should clear consent_timestamp"
);
}
}