zopp-store-postgres 0.1.1

PostgreSQL storage backend for zopp secrets manager
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
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
use super::*;
use rand_core::RngCore;
use sqlx::postgres::PgConnection;
use sqlx::{Connection, Executor};
use zopp_storage::{
    CreateEnvParams, CreateInviteParams, CreatePrincipalData, CreateProjectParams,
    CreateUserParams, CreateWorkspaceParams, Store, StoreError, UserId, WorkspaceId,
};

/// Create a unique test database and return the PostgresStore
async fn test_store() -> (PostgresStore, String) {
    let test_id = std::process::id();
    let timestamp = std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .unwrap()
        .as_nanos();
    let random: u64 = rand_core::OsRng.next_u64();
    let db_name = format!("zopp_test_{}_{}_{}", test_id, timestamp, random);

    // Allow overriding credentials via environment variables for CI/different setups
    let pg_user = std::env::var("POSTGRES_USER").unwrap_or_else(|_| "postgres".to_string());
    let pg_pass = std::env::var("POSTGRES_PASSWORD").unwrap_or_else(|_| "postgres".to_string());
    let pg_host = std::env::var("POSTGRES_HOST").unwrap_or_else(|_| "localhost".to_string());
    let pg_port = std::env::var("POSTGRES_PORT").unwrap_or_else(|_| "5433".to_string());

    let admin_url = format!(
        "postgres://{}:{}@{}:{}/postgres",
        pg_user, pg_pass, pg_host, pg_port
    );
    let mut conn = PgConnection::connect(&admin_url).await.unwrap();

    // Drop if exists
    let drop_query = format!("DROP DATABASE IF EXISTS {}", db_name);
    let _ = conn.execute(drop_query.as_str()).await;

    // Create test database
    let create_query = format!("CREATE DATABASE {}", db_name);
    conn.execute(create_query.as_str()).await.unwrap();
    drop(conn);

    let db_url = format!(
        "postgres://{}:{}@{}:{}/{}",
        pg_user, pg_pass, pg_host, pg_port, db_name
    );
    let store = PostgresStore::open(&db_url).await.unwrap();

    (store, db_name)
}

/// Cleanup test database
async fn cleanup_db(db_name: &str) {
    let pg_user = std::env::var("POSTGRES_USER").unwrap_or_else(|_| "postgres".to_string());
    let pg_pass = std::env::var("POSTGRES_PASSWORD").unwrap_or_else(|_| "postgres".to_string());
    let pg_host = std::env::var("POSTGRES_HOST").unwrap_or_else(|_| "localhost".to_string());
    let pg_port = std::env::var("POSTGRES_PORT").unwrap_or_else(|_| "5433".to_string());

    let admin_url = format!(
        "postgres://{}:{}@{}:{}/postgres",
        pg_user, pg_pass, pg_host, pg_port
    );
    match PgConnection::connect(&admin_url).await {
        Ok(mut conn) => {
            let drop_query = format!("DROP DATABASE IF EXISTS {}", db_name);
            if let Err(e) = conn.execute(drop_query.as_str()).await {
                eprintln!("Warning: Failed to drop test database {}: {}", db_name, e);
            }
        }
        Err(e) => {
            eprintln!("Warning: Failed to connect to database for cleanup: {}", e);
        }
    }
}

fn workspace_params(owner_user_id: UserId, name: &str) -> CreateWorkspaceParams {
    CreateWorkspaceParams {
        id: WorkspaceId(uuid::Uuid::now_v7()),
        name: name.to_string(),
        owner_user_id,
        kdf_salt: b"test_salt_16byte".to_vec(),
        m_cost_kib: 2048,
        t_cost: 3,
        p_cost: 2,
    }
}

#[tokio::test]
async fn test_user_creation_and_retrieval() {
    let (store, db_name) = test_store().await;

    let (user_id, principal_id) = store
        .create_user(&CreateUserParams {
            email: "alice@example.com".to_string(),
            principal: Some(CreatePrincipalData {
                name: "laptop".to_string(),
                public_key: vec![1, 2, 3],
                x25519_public_key: Some(vec![4, 5, 6]),
                is_service: false,
            }),
            workspace_ids: vec![],
        })
        .await
        .unwrap();

    assert!(principal_id.is_some());

    let user = store.get_user_by_id(&user_id).await.unwrap();
    assert_eq!(user.email, "alice@example.com");

    cleanup_db(&db_name).await;
}

#[tokio::test]
async fn test_workspace_creation_and_kdf_params() {
    let (store, db_name) = test_store().await;

    let (user_id, _) = store
        .create_user(&CreateUserParams {
            email: "bob@example.com".to_string(),
            principal: None,
            workspace_ids: vec![],
        })
        .await
        .unwrap();

    let ws_id = store
        .create_workspace(&workspace_params(user_id, "acme"))
        .await
        .unwrap();

    let workspace = store.get_workspace(&ws_id).await.unwrap();
    assert_eq!(workspace.name, "acme");
    assert_eq!(workspace.kdf_salt, b"test_salt_16byte");
    assert_eq!(workspace.m_cost_kib, 2048);
    assert_eq!(workspace.t_cost, 3);
    assert_eq!(workspace.p_cost, 2);

    cleanup_db(&db_name).await;
}

#[tokio::test]
async fn test_project_creation_and_duplicate_error() {
    let (store, db_name) = test_store().await;

    let (user_id, _) = store
        .create_user(&CreateUserParams {
            email: "user@test.com".to_string(),
            principal: None,
            workspace_ids: vec![],
        })
        .await
        .unwrap();

    let ws_id = store
        .create_workspace(&workspace_params(user_id, "workspace"))
        .await
        .unwrap();

    // Create project
    let project_id = store
        .create_project(&CreateProjectParams {
            workspace_id: ws_id.clone(),
            name: "api".to_string(),
        })
        .await
        .unwrap();

    assert!(project_id.0 != uuid::Uuid::nil());

    // Duplicate should fail
    let err = store
        .create_project(&CreateProjectParams {
            workspace_id: ws_id,
            name: "api".to_string(),
        })
        .await
        .unwrap_err();

    assert!(matches!(err, StoreError::AlreadyExists));

    cleanup_db(&db_name).await;
}

#[tokio::test]
async fn test_environment_creation_and_duplicate_error() {
    let (store, db_name) = test_store().await;

    let (user_id, _) = store
        .create_user(&CreateUserParams {
            email: "dev@test.com".to_string(),
            principal: None,
            workspace_ids: vec![],
        })
        .await
        .unwrap();

    let ws_id = store
        .create_workspace(&workspace_params(user_id, "workspace"))
        .await
        .unwrap();

    let project_id = store
        .create_project(&CreateProjectParams {
            workspace_id: ws_id,
            name: "backend".to_string(),
        })
        .await
        .unwrap();

    // Create environment
    let env_id = store
        .create_env(&CreateEnvParams {
            project_id: project_id.clone(),
            name: "production".to_string(),
            dek_wrapped: vec![10; 32],
            dek_nonce: vec![20; 24],
        })
        .await
        .unwrap();

    assert!(env_id.0 != uuid::Uuid::nil());

    // Duplicate should fail
    let err = store
        .create_env(&CreateEnvParams {
            project_id,
            name: "production".to_string(),
            dek_wrapped: vec![99; 32],
            dek_nonce: vec![88; 24],
        })
        .await
        .unwrap_err();

    assert!(matches!(err, StoreError::AlreadyExists));

    cleanup_db(&db_name).await;
}

#[tokio::test]
async fn test_secret_upsert_and_retrieval() {
    let (store, db_name) = test_store().await;

    let (user_id, _) = store
        .create_user(&CreateUserParams {
            email: "secret@test.com".to_string(),
            principal: None,
            workspace_ids: vec![],
        })
        .await
        .unwrap();

    let ws_id = store
        .create_workspace(&workspace_params(user_id, "workspace"))
        .await
        .unwrap();

    let project_id = store
        .create_project(&CreateProjectParams {
            workspace_id: ws_id,
            name: "app".to_string(),
        })
        .await
        .unwrap();

    let env_id = store
        .create_env(&CreateEnvParams {
            project_id,
            name: "dev".to_string(),
            dek_wrapped: vec![1; 32],
            dek_nonce: vec![2; 24],
        })
        .await
        .unwrap();

    // Insert secret (nonce=24 bytes, ciphertext=variable)
    store
        .upsert_secret(&env_id, "API_KEY", &[4; 24], &[3; 48])
        .await
        .unwrap();

    // Retrieve secret
    let secret = store.get_secret(&env_id, "API_KEY").await.unwrap();
    assert_eq!(secret.nonce, vec![4; 24]);
    assert_eq!(secret.ciphertext, vec![3; 48]);

    // Update secret
    store
        .upsert_secret(&env_id, "API_KEY", &[6; 24], &[5; 48])
        .await
        .unwrap();

    let updated = store.get_secret(&env_id, "API_KEY").await.unwrap();
    assert_eq!(updated.nonce, vec![6; 24]);
    assert_eq!(updated.ciphertext, vec![5; 48]);

    cleanup_db(&db_name).await;
}

#[tokio::test]
async fn test_workspace_isolation() {
    let (store, db_name) = test_store().await;

    let (user_id, _) = store
        .create_user(&CreateUserParams {
            email: "isolation@test.com".to_string(),
            principal: None,
            workspace_ids: vec![],
        })
        .await
        .unwrap();

    // Create two workspaces
    let ws1 = store
        .create_workspace(&workspace_params(user_id.clone(), "workspace1"))
        .await
        .unwrap();

    let ws2 = store
        .create_workspace(&workspace_params(user_id, "workspace2"))
        .await
        .unwrap();

    // Create identical project/env structure in both
    let p1 = store
        .create_project(&CreateProjectParams {
            workspace_id: ws1,
            name: "app".to_string(),
        })
        .await
        .unwrap();

    let e1 = store
        .create_env(&CreateEnvParams {
            project_id: p1,
            name: "prod".to_string(),
            dek_wrapped: vec![1; 32],
            dek_nonce: vec![2; 24],
        })
        .await
        .unwrap();

    let p2 = store
        .create_project(&CreateProjectParams {
            workspace_id: ws2,
            name: "app".to_string(),
        })
        .await
        .unwrap();

    let e2 = store
        .create_env(&CreateEnvParams {
            project_id: p2,
            name: "prod".to_string(),
            dek_wrapped: vec![3; 32],
            dek_nonce: vec![4; 24],
        })
        .await
        .unwrap();

    // Insert secret in workspace1
    store
        .upsert_secret(&e1, "TOKEN", &[11; 24], &[10; 48])
        .await
        .unwrap();

    // workspace2 should NOT see workspace1's secret
    let err = store.get_secret(&e2, "TOKEN").await.unwrap_err();
    assert!(matches!(err, StoreError::NotFound));

    cleanup_db(&db_name).await;
}

#[tokio::test]
async fn test_list_secrets_sorted() {
    let (store, db_name) = test_store().await;

    let (user_id, _) = store
        .create_user(&CreateUserParams {
            email: "list@test.com".to_string(),
            principal: None,
            workspace_ids: vec![],
        })
        .await
        .unwrap();

    let ws_id = store
        .create_workspace(&workspace_params(user_id, "workspace"))
        .await
        .unwrap();

    let project_id = store
        .create_project(&CreateProjectParams {
            workspace_id: ws_id,
            name: "app".to_string(),
        })
        .await
        .unwrap();

    let env_id = store
        .create_env(&CreateEnvParams {
            project_id,
            name: "staging".to_string(),
            dek_wrapped: vec![1; 32],
            dek_nonce: vec![2; 24],
        })
        .await
        .unwrap();

    // Insert secrets in non-alphabetical order
    store
        .upsert_secret(&env_id, "ZEBRA", &[1; 24], &[1; 48])
        .await
        .unwrap();
    store
        .upsert_secret(&env_id, "ALPHA", &[2; 24], &[2; 48])
        .await
        .unwrap();
    store
        .upsert_secret(&env_id, "BETA", &[3; 24], &[3; 48])
        .await
        .unwrap();

    let keys = store.list_secret_keys(&env_id).await.unwrap();
    assert_eq!(keys, vec!["ALPHA", "BETA", "ZEBRA"]);

    cleanup_db(&db_name).await;
}

#[tokio::test]
async fn test_unicode_handling() {
    let (store, db_name) = test_store().await;

    let (user_id, _) = store
        .create_user(&CreateUserParams {
            email: "unicode@测试.com".to_string(),
            principal: None,
            workspace_ids: vec![],
        })
        .await
        .unwrap();

    let ws_id = store
        .create_workspace(&workspace_params(user_id, "工作区"))
        .await
        .unwrap();

    let project_id = store
        .create_project(&CreateProjectParams {
            workspace_id: ws_id.clone(),
            name: "プロジェクト".to_string(),
        })
        .await
        .unwrap();

    let env_id = store
        .create_env(&CreateEnvParams {
            project_id,
            name: "환경".to_string(),
            dek_wrapped: vec![1; 32],
            dek_nonce: vec![2; 24],
        })
        .await
        .unwrap();

    store
        .upsert_secret(&env_id, "🔑_KEY", &[6; 24], &[5; 48])
        .await
        .unwrap();

    let secret = store.get_secret(&env_id, "🔑_KEY").await.unwrap();
    assert_eq!(secret.nonce, vec![6; 24]);
    assert_eq!(secret.ciphertext, vec![5; 48]);

    let workspace = store.get_workspace(&ws_id).await.unwrap();
    assert_eq!(workspace.name, "工作区");

    cleanup_db(&db_name).await;
}

#[tokio::test]
async fn test_invite_creation_and_revocation() {
    let (store, db_name) = test_store().await;

    let (user_id, _) = store
        .create_user(&CreateUserParams {
            email: "inviter@test.com".to_string(),
            principal: None,
            workspace_ids: vec![],
        })
        .await
        .unwrap();

    let ws_id = store
        .create_workspace(&workspace_params(user_id.clone(), "team"))
        .await
        .unwrap();

    // Create invite token (hash of invite secret)
    let invite_secret = b"supersecretinvite123";
    let token_hash = {
        use sha2::{Digest, Sha256};
        let mut hasher = Sha256::new();
        hasher.update(invite_secret);
        hex::encode(hasher.finalize())
    };

    let expires_at = chrono::Utc::now() + chrono::Duration::hours(24);
    let invite = store
        .create_invite(&CreateInviteParams {
            workspace_ids: vec![ws_id.clone()],
            token: token_hash.clone(),
            kek_encrypted: Some(vec![7; 48]),
            kek_nonce: Some(vec![8; 24]),
            expires_at,
            created_by_user_id: Some(user_id.clone()),
        })
        .await
        .unwrap();

    // Get invite by token
    let retrieved = store.get_invite_by_token(&token_hash).await.unwrap();
    assert_eq!(retrieved.workspace_ids, vec![ws_id]);
    assert_eq!(retrieved.kek_encrypted, Some(vec![7; 48]));

    // List invites for user
    let invites = store.list_invites(Some(&user_id)).await.unwrap();
    assert_eq!(invites.len(), 1);
    assert_eq!(invites[0].token, token_hash);

    // Revoke invite
    store.revoke_invite(&invite.id).await.unwrap();

    // Getting revoked invite should fail
    let err = store.get_invite_by_token(&token_hash).await.unwrap_err();
    assert!(matches!(err, StoreError::NotFound));

    cleanup_db(&db_name).await;
}

#[tokio::test]
async fn test_sequential_secret_updates() {
    let (store, db_name) = test_store().await;

    let (user_id, _) = store
        .create_user(&CreateUserParams {
            email: "sequential@test.com".to_string(),
            principal: None,
            workspace_ids: vec![],
        })
        .await
        .unwrap();

    let ws_id = store
        .create_workspace(&workspace_params(user_id, "workspace"))
        .await
        .unwrap();

    let project_id = store
        .create_project(&CreateProjectParams {
            workspace_id: ws_id,
            name: "test".to_string(),
        })
        .await
        .unwrap();

    let env_id = store
        .create_env(&CreateEnvParams {
            project_id,
            name: "test".to_string(),
            dek_wrapped: vec![1; 32],
            dek_nonce: vec![2; 24],
        })
        .await
        .unwrap();

    // Perform multiple upserts - last one should win
    store
        .upsert_secret(&env_id, "KEY", &[1; 24], &[1; 48])
        .await
        .unwrap();

    store
        .upsert_secret(&env_id, "KEY", &[2; 24], &[2; 48])
        .await
        .unwrap();

    store
        .upsert_secret(&env_id, "KEY", &[3; 24], &[3; 48])
        .await
        .unwrap();

    let final_secret = store.get_secret(&env_id, "KEY").await.unwrap();
    assert_eq!(final_secret.nonce, vec![3; 24]);
    assert_eq!(final_secret.ciphertext, vec![3; 48]);

    cleanup_db(&db_name).await;
}