netviper-talos 0.2.4

A Rust-based secure licensing system.
Documentation
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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
//! Secure storage abstraction for license and cache data.
//!
//! This module provides a unified storage interface that:
//! 1. Tries OS keyring first (most secure)
//! 2. Falls back to file storage in app data directory
//! 3. Handles migration from legacy CWD-based files
//!
//! ## Storage Locations
//!
//! **Keyring (Primary):**
//! - Service: `talos`
//! - Keys: `license:{hardware_id}` and `cache:{hardware_id}`
//!
//! **File Fallback (Secondary):**
//! - Windows: `%APPDATA%\talos\`
//! - macOS: `~/Library/Application Support/talos/`
//! - Linux: `~/.local/share/talos/`
//!
//! ## Migration
//!
//! On load, if data is not found in keyring or app data directory,
//! the module checks for legacy files in the current working directory
//! and automatically migrates them.

use crate::errors::{LicenseError, LicenseResult};
use crate::hardware::get_hardware_id;

use std::io::ErrorKind;
use std::path::PathBuf;
use tokio::fs;

/// File names for stored data.
const LICENSE_FILE: &str = "talos_license.enc";
const CACHE_FILE: &str = "talos_cache.enc";

/// Service name for keyring storage.
const KEYRING_SERVICE: &str = "talos";

/// Identifies what type of data is being stored.
#[derive(Debug, Clone, Copy)]
pub enum StorageKey {
    License,
    Cache,
}

impl StorageKey {
    /// Get the keyring entry name for this storage key.
    fn keyring_name(&self) -> String {
        let hw_id = get_hardware_id();
        match self {
            StorageKey::License => format!("license:{}", hw_id),
            StorageKey::Cache => format!("cache:{}", hw_id),
        }
    }

    /// Get the filename for file-based storage.
    fn filename(&self) -> &'static str {
        match self {
            StorageKey::License => LICENSE_FILE,
            StorageKey::Cache => CACHE_FILE,
        }
    }
}

/// Get the application data directory for talos.
///
/// Returns platform-specific paths:
/// - Windows: `%APPDATA%\talos\`
/// - macOS: `~/Library/Application Support/talos/`
/// - Linux: `~/.local/share/talos/`
fn get_app_data_dir() -> Option<PathBuf> {
    dirs::data_dir().map(|p| p.join("talos"))
}

/// Get the full path for file-based storage.
fn get_storage_path(key: StorageKey) -> Option<PathBuf> {
    get_app_data_dir().map(|dir| dir.join(key.filename()))
}

/// Get the legacy (CWD-based) path for migration.
fn get_legacy_path(key: StorageKey) -> PathBuf {
    PathBuf::from(key.filename())
}

// === Keyring Operations ===

/// Save data to the OS keyring.
fn save_to_keyring(key: StorageKey, data: &str) -> Result<(), keyring::Error> {
    let entry = keyring::Entry::new(KEYRING_SERVICE, &key.keyring_name())?;
    entry.set_password(data)
}

/// Load data from the OS keyring.
fn load_from_keyring(key: StorageKey) -> Result<String, keyring::Error> {
    let entry = keyring::Entry::new(KEYRING_SERVICE, &key.keyring_name())?;
    entry.get_password()
}

/// Delete data from the OS keyring.
fn clear_from_keyring(key: StorageKey) -> Result<(), keyring::Error> {
    let entry = keyring::Entry::new(KEYRING_SERVICE, &key.keyring_name())?;
    entry.delete_credential()
}

// === File Operations ===

/// Save data to file in app data directory.
async fn save_to_file(key: StorageKey, data: &str) -> LicenseResult<()> {
    let dir = get_app_data_dir().ok_or_else(|| {
        LicenseError::StorageError(std::io::Error::new(
            ErrorKind::NotFound,
            "Could not determine app data directory",
        ))
    })?;

    // Ensure directory exists
    fs::create_dir_all(&dir).await?;

    let path = dir.join(key.filename());
    fs::write(&path, data).await?;
    Ok(())
}

/// Load data from file in app data directory.
async fn load_from_file(key: StorageKey) -> LicenseResult<String> {
    let path = get_storage_path(key).ok_or_else(|| {
        LicenseError::StorageError(std::io::Error::new(
            ErrorKind::NotFound,
            "Could not determine app data directory",
        ))
    })?;

    match fs::read_to_string(&path).await {
        Ok(data) => Ok(data),
        Err(e) if e.kind() == ErrorKind::NotFound => Err(LicenseError::InvalidLicense(
            "No stored data found.".to_string(),
        )),
        Err(e) => Err(LicenseError::StorageError(e)),
    }
}

/// Delete file from app data directory.
async fn clear_from_file(key: StorageKey) -> LicenseResult<()> {
    if let Some(path) = get_storage_path(key) {
        match fs::remove_file(&path).await {
            Ok(_) => Ok(()),
            Err(e) if e.kind() == ErrorKind::NotFound => Ok(()),
            Err(e) => Err(LicenseError::StorageError(e)),
        }
    } else {
        Ok(())
    }
}

// === Legacy Migration ===

/// Check for and load data from legacy CWD-based file.
async fn load_from_legacy(key: StorageKey) -> Option<String> {
    let path = get_legacy_path(key);
    fs::read_to_string(&path).await.ok()
}

/// Delete legacy CWD-based file after migration.
async fn clear_legacy_file(key: StorageKey) {
    let path = get_legacy_path(key);
    let _ = fs::remove_file(&path).await;
}

// === Public API ===

/// Save encrypted data to storage.
///
/// Tries keyring first, falls back to file storage if keyring fails.
pub async fn save_to_storage(key: StorageKey, data: &str) -> LicenseResult<()> {
    // Try keyring first
    match save_to_keyring(key, data) {
        Ok(()) => {
            log::debug!("Saved {:?} to keyring", key);
            // Verify the save worked by reading back
            if load_from_keyring(key).is_ok() {
                return Ok(());
            }
            log::debug!(
                "Keyring save verification failed for {:?}, falling back to file",
                key
            );
        }
        Err(e) => {
            log::debug!(
                "Keyring save failed for {:?}: {}, falling back to file",
                key,
                e
            );
        }
    }

    // Fall back to file storage
    save_to_file(key, data).await?;
    log::debug!("Saved {:?} to app data directory", key);
    Ok(())
}

/// Load encrypted data from storage.
///
/// Checks in order:
/// 1. Keyring
/// 2. App data directory file
/// 3. Legacy CWD file (with automatic migration)
pub async fn load_from_storage(key: StorageKey) -> LicenseResult<String> {
    // 1. Try keyring
    match load_from_keyring(key) {
        Ok(data) => {
            log::debug!("Loaded {:?} from keyring", key);
            return Ok(data);
        }
        Err(e) => {
            log::debug!("Keyring load failed for {:?}: {}", key, e);
        }
    }

    // 2. Try app data directory
    match load_from_file(key).await {
        Ok(data) => {
            log::debug!("Loaded {:?} from app data directory", key);
            // Try to migrate to keyring for next time
            if save_to_keyring(key, &data).is_ok() {
                log::debug!("Migrated {:?} from app data to keyring", key);
            }
            return Ok(data);
        }
        Err(LicenseError::InvalidLicense(_)) => {
            // Not found, continue to legacy check
        }
        Err(e) => {
            log::debug!("App data file load failed for {:?}: {}", key, e);
        }
    }

    // 3. Try legacy CWD file (migration path)
    if let Some(data) = load_from_legacy(key).await {
        log::info!("Found legacy {:?} file in CWD, migrating...", key);

        // Migrate to new storage
        if let Err(e) = save_to_storage(key, &data).await {
            log::warn!("Failed to migrate {:?} to new storage: {}", key, e);
            // Still return the data even if migration failed
        } else {
            // Migration successful, delete legacy file
            clear_legacy_file(key).await;
            log::info!("Successfully migrated {:?} and cleaned up legacy file", key);
        }

        return Ok(data);
    }

    // Not found anywhere
    Err(LicenseError::InvalidLicense(
        "No stored data found.".to_string(),
    ))
}

/// Clear data from all storage locations.
///
/// Clears from keyring, app data directory, and legacy CWD location.
pub async fn clear_from_storage(key: StorageKey) -> LicenseResult<()> {
    let mut last_error: Option<LicenseError> = None;

    // Clear from keyring (ignore "not found" errors)
    if let Err(e) = clear_from_keyring(key) {
        match e {
            keyring::Error::NoEntry => {}
            _ => log::debug!("Failed to clear {:?} from keyring: {}", key, e),
        }
    }

    // Clear from app data directory
    if let Err(e) = clear_from_file(key).await {
        last_error = Some(e);
    }

    // Clear legacy file too (cleanup)
    clear_legacy_file(key).await;

    match last_error {
        Some(e) => Err(e),
        None => Ok(()),
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use serial_test::serial;
    use tokio::test as tokio_test;

    // Test file operations in app data directory
    #[tokio_test]
    #[serial]
    async fn test_file_storage_roundtrip() {
        let test_data = "test_encrypted_data_12345";

        // Save to file
        save_to_file(StorageKey::License, test_data)
            .await
            .expect("save should succeed");

        // Load from file
        let loaded = load_from_file(StorageKey::License)
            .await
            .expect("load should succeed");

        assert_eq!(loaded, test_data);

        // Cleanup
        clear_from_file(StorageKey::License)
            .await
            .expect("clear should succeed");
    }

    #[tokio_test]
    #[serial]
    async fn test_missing_file_returns_invalid_license() {
        // Ensure file doesn't exist
        let _ = clear_from_file(StorageKey::Cache).await;

        let result = load_from_file(StorageKey::Cache).await;
        assert!(matches!(result, Err(LicenseError::InvalidLicense(_))));
    }

    #[tokio_test]
    #[serial]
    async fn test_storage_api_roundtrip() {
        let test_data = "storage_api_test_data";

        // Clear any existing data first
        let _ = clear_from_storage(StorageKey::License).await;

        // Save through public API
        save_to_storage(StorageKey::License, test_data)
            .await
            .expect("save should succeed");

        // Load through public API
        let loaded = load_from_storage(StorageKey::License)
            .await
            .expect("load should succeed");

        assert_eq!(loaded, test_data);

        // Cleanup
        clear_from_storage(StorageKey::License)
            .await
            .expect("clear should succeed");
    }

    #[tokio_test]
    #[serial]
    async fn test_legacy_migration() {
        let test_data = "legacy_migration_test_data";
        let legacy_path = get_legacy_path(StorageKey::Cache);

        // Clear any existing data
        let _ = clear_from_storage(StorageKey::Cache).await;

        // Create a legacy file in CWD
        fs::write(&legacy_path, test_data)
            .await
            .expect("creating legacy file should succeed");

        // Load should find and migrate the legacy file
        let loaded = load_from_storage(StorageKey::Cache)
            .await
            .expect("load should succeed");

        assert_eq!(loaded, test_data);

        // Legacy file should be deleted after migration
        assert!(
            !legacy_path.exists(),
            "legacy file should be deleted after migration"
        );

        // Data should still be accessible (now from new storage)
        let loaded_again = load_from_storage(StorageKey::Cache)
            .await
            .expect("load should still succeed after migration");

        assert_eq!(loaded_again, test_data);

        // Cleanup
        let _ = clear_from_storage(StorageKey::Cache).await;
    }

    #[test]
    fn test_storage_key_names() {
        // Just verify the naming patterns are consistent
        let license_name = StorageKey::License.keyring_name();
        let cache_name = StorageKey::Cache.keyring_name();

        assert!(license_name.starts_with("license:"));
        assert!(cache_name.starts_with("cache:"));
        assert_eq!(StorageKey::License.filename(), "talos_license.enc");
        assert_eq!(StorageKey::Cache.filename(), "talos_cache.enc");
    }

    #[test]
    fn test_app_data_dir_exists() {
        // This should work on all platforms
        let dir = get_app_data_dir();
        assert!(dir.is_some(), "app data directory should be determinable");
    }
}