pipedash-core 0.9.0

Framework-agnostic core library for Pipedash
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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
use std::path::{
    Path,
    PathBuf,
};
use std::sync::Arc;

use tokio::sync::RwLock;

use crate::domain::{
    DomainError,
    DomainResult,
};
use crate::infrastructure::config::{
    PipedashConfig,
    StorageBackend as ConfigStorageBackend,
};
use crate::infrastructure::database::init_database;
#[cfg(feature = "postgres")]
use crate::infrastructure::secrets::PostgresTokenStore;
use crate::infrastructure::secrets::SqliteTokenStore;
use crate::infrastructure::storage::{
    LocalStorage,
    StorageBackend,
};
use crate::infrastructure::token_store::{
    MemoryTokenStore,
    TokenStore,
};
#[cfg(feature = "postgres")]
use crate::infrastructure::PostgresConfigBackend;
use crate::infrastructure::{
    ConfigBackend,
    SqliteConfigBackend,
};

pub struct StorageManager {
    config: PipedashConfig,
    token_store: Arc<RwLock<Arc<dyn TokenStore>>>,
    config_backend: Arc<dyn ConfigBackend>,
    cache_backend: Arc<dyn StorageBackend>,
    database_pool: crate::infrastructure::database::DatabasePool,
    vault_locked: Arc<RwLock<bool>>,
    data_dir: PathBuf,
    is_desktop: bool,
}

impl StorageManager {
    pub async fn from_config(config: PipedashConfig, is_desktop: bool) -> DomainResult<Self> {
        Self::from_config_internal(config, is_desktop, false).await
    }

    pub async fn from_config_allow_locked(
        config: PipedashConfig, is_desktop: bool,
    ) -> DomainResult<Self> {
        Self::from_config_internal(config, is_desktop, true).await
    }

    async fn from_config_internal(
        config: PipedashConfig, is_desktop: bool, allow_locked: bool,
    ) -> DomainResult<Self> {
        let data_dir = config.data_dir();

        std::fs::create_dir_all(&data_dir).map_err(|e| {
            DomainError::DatabaseError(format!("Failed to create data directory: {}", e))
        })?;

        let backend = config.storage.backend;

        #[cfg(feature = "postgres")]
        if backend == ConfigStorageBackend::Postgres {
            use crate::infrastructure::database::init_postgres_database;

            init_postgres_database(&config.storage.postgres.connection_string)
                .await
                .map_err(|e| {
                    DomainError::DatabaseError(format!(
                        "Failed to initialize PostgreSQL database: {}",
                        e
                    ))
                })?;

            tracing::info!("PostgreSQL migrations completed");
        }

        let (token_store, vault_locked) =
            match Self::create_token_store(&config, backend, is_desktop, &data_dir).await {
                Ok(store) => (store, false),
                Err(e) if allow_locked && Self::is_vault_password_error(&e) => {
                    tracing::warn!("Vault password not set - starting in locked mode");
                    (
                        Arc::new(MemoryTokenStore::new()) as Arc<dyn TokenStore>,
                        true,
                    )
                }
                Err(e) => return Err(e),
            };

        let (config_backend, database_pool) =
            Self::create_config_backend(&config, backend, &data_dir).await?;
        let cache_backend = Self::create_cache_backend(&config, backend, &data_dir).await?;

        Ok(Self {
            config,
            token_store: Arc::new(RwLock::new(token_store)),
            config_backend,
            cache_backend,
            database_pool,
            vault_locked: Arc::new(RwLock::new(vault_locked)),
            data_dir,
            is_desktop,
        })
    }

    fn is_vault_password_error(error: &DomainError) -> bool {
        match error {
            DomainError::InvalidConfig(msg) => {
                msg.contains("PIPEDASH_VAULT_PASSWORD") || msg.contains("vault password")
            }
            DomainError::AuthenticationFailed(msg) => {
                msg.contains("vault password") || msg.contains("decrypt")
            }
            _ => false,
        }
    }

    pub async fn is_vault_locked(&self) -> bool {
        *self.vault_locked.read().await
    }

    pub fn requires_vault_password(&self) -> bool {
        match (self.config.storage.backend, self.is_desktop) {
            (ConfigStorageBackend::Sqlite, true) => false, // Keyring
            (ConfigStorageBackend::Sqlite, false) => true, // SQLite encrypted
            (ConfigStorageBackend::Postgres, _) => true,   // Postgres encrypted
        }
    }

    pub async fn is_first_time_vault_setup(&self) -> DomainResult<bool> {
        use crate::infrastructure::database::DatabasePool;

        const QUERY: &str = "SELECT 1 FROM vault_verifier";
        let verifier = match &self.database_pool {
            DatabasePool::Sqlite(pool) => sqlx::query(QUERY)
                .fetch_optional(pool)
                .await
                .map(|r| r.is_some()),
            DatabasePool::Postgres(pool) => sqlx::query(QUERY)
                .fetch_optional(pool)
                .await
                .map(|r| r.is_some()),
        }
        .map_err(|e| DomainError::DatabaseError(format!("Failed to read vault verifier: {}", e)))?;

        Ok(!verifier && self.config_backend.list_providers().await?.is_empty())
    }

    pub async fn unlock_vault(&self, password: &str) -> DomainResult<()> {
        if !*self.vault_locked.read().await {
            return Ok(()); // Already unlocked
        }

        std::env::set_var("PIPEDASH_VAULT_PASSWORD", password);

        let new_store = Self::create_token_store_with_password(
            &self.config,
            self.config.storage.backend,
            self.is_desktop,
            &self.data_dir,
            password,
        )
        .await?;

        {
            let mut store = self.token_store.write().await;
            *store = new_store;
        }

        {
            let mut locked = self.vault_locked.write().await;
            *locked = false;
        }

        tracing::info!("Vault unlocked successfully");
        Ok(())
    }

    async fn create_token_store_with_password(
        config: &PipedashConfig, backend: ConfigStorageBackend, _is_desktop: bool, data_dir: &Path,
        password: &str,
    ) -> DomainResult<Arc<dyn TokenStore>> {
        match backend {
            ConfigStorageBackend::Sqlite => {
                let db_path = data_dir.join("pipedash.db");
                let pool = init_database(db_path).await.map_err(|e| {
                    DomainError::DatabaseError(format!(
                        "Failed to initialize SQLite database for token storage: {}",
                        e
                    ))
                })?;

                let store = SqliteTokenStore::new(pool, Some(password.to_string())).await?;
                Ok(Arc::new(store))
            }
            ConfigStorageBackend::Postgres => {
                #[cfg(feature = "postgres")]
                {
                    let connection_string = &config.storage.postgres.connection_string;
                    if connection_string.is_empty() {
                        return Err(DomainError::InvalidConfig(
                            "PostgreSQL connection string required for token storage".into(),
                        ));
                    }

                    let store =
                        PostgresTokenStore::new(connection_string, Some(password.to_string()))
                            .await?;
                    Ok(Arc::new(store))
                }
                #[cfg(not(feature = "postgres"))]
                {
                    let _ = config; // silence unused warning
                    Err(DomainError::InvalidConfig(
                        "PostgreSQL feature not enabled".into(),
                    ))
                }
            }
        }
    }

    async fn create_token_store(
        config: &PipedashConfig, backend: ConfigStorageBackend, is_desktop: bool, data_dir: &Path,
    ) -> DomainResult<Arc<dyn TokenStore>> {
        let vault_password = config
            .storage
            .vault_password
            .clone()
            .or_else(|| std::env::var("PIPEDASH_VAULT_PASSWORD").ok());

        match (backend, is_desktop) {
            (ConfigStorageBackend::Sqlite, true) if vault_password.is_none() => {
                Err(DomainError::InvalidConfig(
                    "Keyring backend must be created externally (Tauri-specific)".into(),
                ))
            }
            (ConfigStorageBackend::Sqlite, _) => {
                let db_path = data_dir.join("pipedash.db");
                let pool = init_database(db_path).await.map_err(|e| {
                    DomainError::DatabaseError(format!(
                        "Failed to initialize SQLite database for token storage: {}",
                        e
                    ))
                })?;

                let store = SqliteTokenStore::new(pool, vault_password).await?;
                Ok(Arc::new(store))
            }
            (ConfigStorageBackend::Postgres, _) => {
                #[cfg(feature = "postgres")]
                {
                    let connection_string = &config.storage.postgres.connection_string;
                    if connection_string.is_empty() {
                        return Err(DomainError::InvalidConfig(
                            "PostgreSQL connection string required for token storage".into(),
                        ));
                    }

                    let vault_password = config
                        .storage
                        .vault_password
                        .clone()
                        .or_else(|| std::env::var("PIPEDASH_VAULT_PASSWORD").ok());
                    let store = PostgresTokenStore::new(connection_string, vault_password).await?;
                    Ok(Arc::new(store))
                }
                #[cfg(not(feature = "postgres"))]
                {
                    let _ = config; // silence unused warning
                    Err(DomainError::InvalidConfig(
                        "PostgreSQL feature not enabled".into(),
                    ))
                }
            }
        }
    }

    async fn create_config_backend(
        config: &PipedashConfig, backend: ConfigStorageBackend, data_dir: &Path,
    ) -> DomainResult<(
        Arc<dyn ConfigBackend>,
        crate::infrastructure::database::DatabasePool,
    )> {
        match backend {
            ConfigStorageBackend::Sqlite => {
                let db_path = data_dir.join("pipedash.db");
                let pool = init_database(db_path).await.map_err(|e| {
                    DomainError::DatabaseError(format!("Failed to initialize database: {}", e))
                })?;
                let db_pool = crate::infrastructure::database::DatabasePool::Sqlite(pool.clone());
                let config_backend = SqliteConfigBackend::new(pool);
                Ok((Arc::new(config_backend), db_pool))
            }
            ConfigStorageBackend::Postgres => {
                #[cfg(feature = "postgres")]
                {
                    if config.storage.postgres.connection_string.is_empty() {
                        return Err(DomainError::InvalidConfig(
                            "PostgreSQL connection string not configured".into(),
                        ));
                    }
                    let config_backend =
                        PostgresConfigBackend::new(&config.storage.postgres.connection_string)
                            .await?;
                    let db_pool = crate::infrastructure::database::DatabasePool::Postgres(
                        config_backend.pool().clone(),
                    );
                    Ok((Arc::new(config_backend), db_pool))
                }
                #[cfg(not(feature = "postgres"))]
                {
                    let _ = config; // silence unused warning
                    Err(DomainError::InvalidConfig(
                        "PostgreSQL feature not enabled".into(),
                    ))
                }
            }
        }
    }

    async fn create_cache_backend(
        config: &PipedashConfig, backend: ConfigStorageBackend, data_dir: &Path,
    ) -> DomainResult<Arc<dyn StorageBackend>> {
        match backend {
            ConfigStorageBackend::Sqlite => {
                let cache_dir = data_dir.join("cache");
                let storage = LocalStorage::new(cache_dir);
                storage.ensure_directories().await?;
                Ok(Arc::new(storage))
            }
            ConfigStorageBackend::Postgres => {
                #[cfg(feature = "postgres")]
                {
                    use crate::infrastructure::storage::PostgresStorage;

                    if config.storage.postgres.connection_string.is_empty() {
                        return Err(DomainError::InvalidConfig(
                            "PostgreSQL connection string not configured".into(),
                        ));
                    }
                    let storage = PostgresStorage::from_connection_string(
                        &config.storage.postgres.connection_string,
                    )
                    .await?;
                    Ok(Arc::new(storage))
                }
                #[cfg(not(feature = "postgres"))]
                {
                    let _ = config; // silence unused warning
                    Err(DomainError::InvalidConfig(
                        "PostgreSQL feature not enabled".into(),
                    ))
                }
            }
        }
    }

    pub fn config(&self) -> &PipedashConfig {
        &self.config
    }

    pub async fn token_store(&self) -> Arc<dyn TokenStore> {
        let guard = self.token_store.read().await;
        Arc::clone(&guard)
    }

    pub fn config_backend(&self) -> Arc<dyn ConfigBackend> {
        Arc::clone(&self.config_backend)
    }

    pub fn cache_backend(&self) -> Arc<dyn StorageBackend> {
        Arc::clone(&self.cache_backend)
    }

    pub fn database_pool(&self) -> crate::infrastructure::database::DatabasePool {
        self.database_pool.clone()
    }

    pub async fn with_token_store(
        config: PipedashConfig, token_store: Arc<dyn TokenStore>, is_desktop: bool,
    ) -> DomainResult<Self> {
        let data_dir = config.data_dir();
        let backend = config.storage.backend;

        std::fs::create_dir_all(&data_dir).map_err(|e| {
            DomainError::DatabaseError(format!("Failed to create data directory: {}", e))
        })?;

        let (config_backend, database_pool) =
            Self::create_config_backend(&config, backend, &data_dir).await?;
        let cache_backend = Self::create_cache_backend(&config, backend, &data_dir).await?;

        Ok(Self {
            config,
            token_store: Arc::new(RwLock::new(token_store)),
            config_backend,
            cache_backend,
            database_pool,
            vault_locked: Arc::new(RwLock::new(false)), // Custom token store means not locked
            data_dir,
            is_desktop,
        })
    }

    pub async fn with_token_store_locked(
        config: PipedashConfig, token_store: Arc<dyn TokenStore>, is_desktop: bool,
    ) -> DomainResult<Self> {
        let data_dir = config.data_dir();
        let backend = config.storage.backend;

        std::fs::create_dir_all(&data_dir).map_err(|e| {
            DomainError::DatabaseError(format!("Failed to create data directory: {}", e))
        })?;

        let (config_backend, database_pool) =
            Self::create_config_backend(&config, backend, &data_dir).await?;
        let cache_backend = Self::create_cache_backend(&config, backend, &data_dir).await?;

        tracing::info!("StorageManager created in locked vault mode");

        Ok(Self {
            config,
            token_store: Arc::new(RwLock::new(token_store)),
            config_backend,
            cache_backend,
            database_pool,
            vault_locked: Arc::new(RwLock::new(true)), // Vault is locked
            data_dir,
            is_desktop,
        })
    }
}

#[cfg(test)]
mod tests {
    use tempfile::TempDir;

    use super::*;

    #[tokio::test]
    async fn test_storage_manager_creation() {
        let temp_dir = TempDir::new().unwrap();
        let mut config = PipedashConfig::default();
        config.storage.data_dir = temp_dir.path().to_string_lossy().to_string();

        let token_store: Arc<dyn TokenStore> = Arc::new(MemoryTokenStore::new());

        let manager = StorageManager::with_token_store(config, token_store, true)
            .await
            .unwrap();

        assert!(manager.token_store().await.get_token(1).await.is_err());
        assert!(manager.cache_backend().is_available().await);
    }

    #[tokio::test]
    async fn test_storage_manager_vault_locked_mode() {
        let temp_dir = TempDir::new().unwrap();
        let mut config = PipedashConfig::default();
        config.storage.data_dir = temp_dir.path().to_string_lossy().to_string();

        let manager = StorageManager::from_config_allow_locked(config, false)
            .await
            .unwrap();

        assert!(manager.is_vault_locked().await);
        assert!(manager.requires_vault_password());
    }

    #[test]
    fn test_storage_config_defaults() {
        let config = PipedashConfig::default();

        assert_eq!(config.storage.backend, ConfigStorageBackend::Sqlite);
    }

    #[test]
    fn test_requires_vault_password() {
        let config = PipedashConfig::default();

        let is_desktop = true;
        let is_server = false;

        match (config.storage.backend, is_desktop) {
            (ConfigStorageBackend::Sqlite, true) => {} // Keyring - expected
            _ => panic!("Expected Keyring for desktop SQLite"),
        }

        match (config.storage.backend, is_server) {
            (ConfigStorageBackend::Sqlite, false) => {} // SQLite encrypted - expected
            _ => panic!("Expected SQLite encrypted for server SQLite"),
        }
    }
}