witmproxy 0.0.2-alpha

A WASM-in-the-middle proxy
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
use crate::db::Db;
use crate::db::tenants::*;

async fn setup_db() -> (Db, tempfile::TempDir) {
    let temp_dir = tempfile::tempdir().unwrap();
    let db_path = temp_dir.path().join("test.db");
    let db = Db::from_path(db_path, "test_password").await.unwrap();
    db.migrate().await.unwrap();
    (db, temp_dir)
}

/// Insert a dummy plugin row so FK constraints on tenant_plugin_* tables are satisfied.
async fn insert_dummy_plugin(pool: &sqlx::SqlitePool, namespace: &str, name: &str) {
    sqlx::query(
        "INSERT INTO plugins (namespace, name, version, author, description, license, url, publickey, enabled, component)
         VALUES (?, ?, '0.0.1', 'test', 'test plugin', 'MIT', '', X'00', 1, X'00')",
    )
    .bind(namespace)
    .bind(name)
    .execute(pool)
    .await
    .unwrap();
}

// ---------------------------------------------------------------------------
// Tenant CRUD
// ---------------------------------------------------------------------------

#[tokio::test]
async fn create_and_retrieve_tenant() {
    let (db, _dir) = setup_db().await;
    let pool = &db.pool;

    let tenant = Tenant::create(
        pool,
        "t1",
        "Alice",
        Some("alice@test.com"),
        None,
        None,
        None,
    )
    .await
    .unwrap();
    assert_eq!(tenant.id, "t1");
    assert_eq!(tenant.display_name, "Alice");
    assert_eq!(tenant.email.as_deref(), Some("alice@test.com"));
    assert!(tenant.enabled);
}

#[tokio::test]
async fn tenant_by_email() {
    let (db, _dir) = setup_db().await;
    let pool = &db.pool;

    Tenant::create(
        pool,
        "t1",
        "Alice",
        Some("alice@test.com"),
        None,
        None,
        None,
    )
    .await
    .unwrap();

    let found = Tenant::by_email(pool, "alice@test.com").await.unwrap();
    assert!(found.is_some());
    assert_eq!(found.unwrap().id, "t1");

    let not_found = Tenant::by_email(pool, "nobody@test.com").await.unwrap();
    assert!(not_found.is_none());
}

#[tokio::test]
async fn tenant_update_enabled() {
    let (db, _dir) = setup_db().await;
    let pool = &db.pool;

    Tenant::create(pool, "t1", "Alice", None, None, None, None)
        .await
        .unwrap();

    Tenant::update_enabled(pool, "t1", false).await.unwrap();
    let tenant = Tenant::by_id(pool, "t1").await.unwrap().unwrap();
    assert!(!tenant.enabled);

    Tenant::update_enabled(pool, "t1", true).await.unwrap();
    let tenant = Tenant::by_id(pool, "t1").await.unwrap().unwrap();
    assert!(tenant.enabled);
}

#[tokio::test]
async fn tenant_delete() {
    let (db, _dir) = setup_db().await;
    let pool = &db.pool;

    Tenant::create(pool, "t1", "Alice", None, None, None, None)
        .await
        .unwrap();

    let deleted = Tenant::delete(pool, "t1").await.unwrap();
    assert!(deleted);

    let not_found = Tenant::by_id(pool, "t1").await.unwrap();
    assert!(not_found.is_none());

    // Delete non-existent returns false
    let deleted = Tenant::delete(pool, "t1").await.unwrap();
    assert!(!deleted);
}

#[tokio::test]
async fn tenant_all() {
    let (db, _dir) = setup_db().await;
    let pool = &db.pool;

    Tenant::create(pool, "t1", "Alice", None, None, None, None)
        .await
        .unwrap();
    Tenant::create(pool, "t2", "Bob", None, None, None, None)
        .await
        .unwrap();

    let all = Tenant::all(pool).await.unwrap();
    let ids: Vec<&str> = all.iter().map(|t| t.id.as_str()).collect();
    assert!(ids.contains(&"t1"), "Should contain tenant t1");
    assert!(ids.contains(&"t2"), "Should contain tenant t2");
}

// ---------------------------------------------------------------------------
// Group CRUD & Membership
// ---------------------------------------------------------------------------

#[tokio::test]
async fn group_create_and_retrieve() {
    let (db, _dir) = setup_db().await;
    let pool = &db.pool;

    let group = Group::create(pool, "g1", "admins", "Administrator group")
        .await
        .unwrap();
    assert_eq!(group.id, "g1");
    assert_eq!(group.name, "admins");

    let found = Group::by_name(pool, "admins").await.unwrap();
    assert!(found.is_some());
}

#[tokio::test]
async fn group_membership() {
    let (db, _dir) = setup_db().await;
    let pool = &db.pool;

    Tenant::create(pool, "t1", "Alice", None, None, None, None)
        .await
        .unwrap();
    Tenant::create(pool, "t2", "Bob", None, None, None, None)
        .await
        .unwrap();
    Group::create(pool, "g1", "admins", "").await.unwrap();

    // Add members
    Group::add_member(pool, "g1", "t1").await.unwrap();
    Group::add_member(pool, "g1", "t2").await.unwrap();

    // Verify tenant groups
    let t1 = Tenant::by_id(pool, "t1").await.unwrap().unwrap();
    let groups = t1.groups(pool).await.unwrap();
    assert_eq!(groups.len(), 1);
    assert_eq!(groups[0].name, "admins");

    // Remove member
    Group::remove_member(pool, "g1", "t2").await.unwrap();
    let t2 = Tenant::by_id(pool, "t2").await.unwrap().unwrap();
    let groups = t2.groups(pool).await.unwrap();
    assert_eq!(groups.len(), 0);
}

#[tokio::test]
async fn group_delete_cascades_membership() {
    let (db, _dir) = setup_db().await;
    let pool = &db.pool;

    Tenant::create(pool, "t1", "Alice", None, None, None, None)
        .await
        .unwrap();
    Group::create(pool, "g1", "admins", "").await.unwrap();
    Group::add_member(pool, "g1", "t1").await.unwrap();

    Group::delete(pool, "g1").await.unwrap();

    let t1 = Tenant::by_id(pool, "t1").await.unwrap().unwrap();
    let groups = t1.groups(pool).await.unwrap();
    assert_eq!(groups.len(), 0);
}

// ---------------------------------------------------------------------------
// Permissions
// ---------------------------------------------------------------------------

#[tokio::test]
async fn tenant_permissions_aggregated_from_groups() {
    let (db, _dir) = setup_db().await;
    let pool = &db.pool;

    Tenant::create(pool, "t1", "Alice", None, None, None, None)
        .await
        .unwrap();
    Group::create(pool, "g1", "readers", "").await.unwrap();
    Group::create(pool, "g2", "writers", "").await.unwrap();

    Group::add_member(pool, "g1", "t1").await.unwrap();
    Group::add_member(pool, "g2", "t1").await.unwrap();

    Group::add_permission(pool, "p1", "g1", "grant", "tenants:*:read")
        .await
        .unwrap();
    Group::add_permission(pool, "p2", "g2", "grant", "tenants:*:write")
        .await
        .unwrap();

    let t1 = Tenant::by_id(pool, "t1").await.unwrap().unwrap();
    let permissions = t1.permissions(pool).await.unwrap();
    let resources: Vec<String> = permissions.iter().map(|p| p.resource.to_string()).collect();
    assert!(
        resources.contains(&"tenants:*:read".to_string()),
        "Should have read permission from readers group"
    );
    assert!(
        resources.contains(&"tenants:*:write".to_string()),
        "Should have write permission from writers group"
    );
}

#[tokio::test]
async fn group_permissions_crud() {
    let (db, _dir) = setup_db().await;
    let pool = &db.pool;

    Group::create(pool, "g1", "admins", "").await.unwrap();

    Group::add_permission(pool, "p1", "g1", "grant", "tenants:*:read")
        .await
        .unwrap();
    Group::add_permission(pool, "p2", "g1", "deny", "tenants:secret:read")
        .await
        .unwrap();

    let perms = Group::permissions(pool, "g1").await.unwrap();
    let resources: Vec<String> = perms.iter().map(|p| p.resource.to_string()).collect();
    assert!(resources.contains(&"tenants:*:read".to_string()));
    assert!(resources.contains(&"tenants:secret:read".to_string()));

    // Remove the read permission
    let removed = Group::remove_permission(pool, "p1").await.unwrap();
    assert!(removed);

    // Verify only the deny permission remains
    let perms = Group::permissions(pool, "g1").await.unwrap();
    assert_eq!(perms.len(), 1);
    assert_eq!(perms[0].resource.to_string(), "tenants:secret:read");
    assert_eq!(perms[0].effect.to_string(), "deny");
}

// ---------------------------------------------------------------------------
// IP Mappings
// ---------------------------------------------------------------------------

#[tokio::test]
async fn ip_mapping_crud() {
    let (db, _dir) = setup_db().await;
    let pool = &db.pool;

    Tenant::create(pool, "t1", "Alice", None, None, None, None)
        .await
        .unwrap();

    add_ip_mapping(pool, "t1", "192.168.1.100").await.unwrap();
    add_ip_mapping(pool, "t1", "192.168.1.101").await.unwrap();

    let t1 = Tenant::by_id(pool, "t1").await.unwrap().unwrap();
    let mappings = t1.ip_mappings(pool).await.unwrap();
    assert_eq!(mappings.len(), 2);

    // Lookup by IP
    let found = tenant_by_ip(pool, "192.168.1.100").await.unwrap();
    assert!(found.is_some());
    assert_eq!(found.unwrap().id, "t1");

    // Unmapped IP
    let not_found = tenant_by_ip(pool, "10.0.0.1").await.unwrap();
    assert!(not_found.is_none());

    // Remove mapping
    remove_ip_mapping(pool, "t1", "192.168.1.100")
        .await
        .unwrap();
    let mappings = t1.ip_mappings(pool).await.unwrap();
    assert_eq!(mappings.len(), 1);
}

#[tokio::test]
async fn tenant_by_ip_lookup() {
    let (db, _dir) = setup_db().await;
    let pool = &db.pool;

    Tenant::create(pool, "t1", "Alice", None, None, None, None)
        .await
        .unwrap();
    Tenant::create(pool, "t2", "Bob", None, None, None, None)
        .await
        .unwrap();

    add_ip_mapping(pool, "t1", "10.0.0.1").await.unwrap();
    add_ip_mapping(pool, "t2", "10.0.0.2").await.unwrap();

    let found1 = Tenant::by_ip(pool, "10.0.0.1").await.unwrap().unwrap();
    assert_eq!(found1.id, "t1");

    let found2 = Tenant::by_ip(pool, "10.0.0.2").await.unwrap().unwrap();
    assert_eq!(found2.id, "t2");
}

// ---------------------------------------------------------------------------
// Plugin Overrides & Config
// ---------------------------------------------------------------------------

#[tokio::test]
async fn plugin_override_set_and_retrieve() {
    let (db, _dir) = setup_db().await;
    let pool = &db.pool;

    Tenant::create(pool, "t1", "Alice", None, None, None, None)
        .await
        .unwrap();
    insert_dummy_plugin(pool, "ns", "myplugin").await;

    set_plugin_override(pool, "t1", "ns", "myplugin", Some(false))
        .await
        .unwrap();

    let t1 = Tenant::by_id(pool, "t1").await.unwrap().unwrap();
    let overrides = t1.plugin_overrides(pool).await.unwrap();
    assert_eq!(overrides.len(), 1);
    assert_eq!(overrides[0].plugin_namespace, "ns");
    assert_eq!(overrides[0].plugin_name, "myplugin");
    assert_eq!(overrides[0].enabled, Some(false));
}

#[tokio::test]
async fn plugin_config_set_and_retrieve() {
    let (db, _dir) = setup_db().await;
    let pool = &db.pool;

    Tenant::create(pool, "t1", "Alice", None, None, None, None)
        .await
        .unwrap();
    insert_dummy_plugin(pool, "ns", "myplugin").await;

    set_plugin_config(pool, "t1", "ns", "myplugin", "threshold", "42")
        .await
        .unwrap();
    set_plugin_config(pool, "t1", "ns", "myplugin", "mode", "\"strict\"")
        .await
        .unwrap();

    let t1 = Tenant::by_id(pool, "t1").await.unwrap().unwrap();
    let config = t1.plugin_config(pool).await.unwrap();
    assert_eq!(config.len(), 2);

    let threshold = config.iter().find(|c| c.input_name == "threshold").unwrap();
    assert_eq!(threshold.input_value, "42");
}

#[tokio::test]
async fn plugin_override_upsert() {
    let (db, _dir) = setup_db().await;
    let pool = &db.pool;

    Tenant::create(pool, "t1", "Alice", None, None, None, None)
        .await
        .unwrap();
    insert_dummy_plugin(pool, "ns", "myplugin").await;

    // Set to false
    set_plugin_override(pool, "t1", "ns", "myplugin", Some(false))
        .await
        .unwrap();

    // Update to true (upsert)
    set_plugin_override(pool, "t1", "ns", "myplugin", Some(true))
        .await
        .unwrap();

    let t1 = Tenant::by_id(pool, "t1").await.unwrap().unwrap();
    let overrides = t1.plugin_overrides(pool).await.unwrap();
    assert_eq!(overrides.len(), 1);
    assert_eq!(overrides[0].enabled, Some(true));
}

// ---------------------------------------------------------------------------
// ACL integration (permissions evaluated via acl::evaluate)
// ---------------------------------------------------------------------------

#[tokio::test]
async fn acl_evaluate_with_db_permissions() {
    use crate::acl;

    let (db, _dir) = setup_db().await;
    let pool = &db.pool;

    Tenant::create(pool, "t1", "Alice", None, None, None, None)
        .await
        .unwrap();
    Group::create(pool, "g1", "readers", "").await.unwrap();
    Group::add_member(pool, "g1", "t1").await.unwrap();
    Group::add_permission(pool, "p1", "g1", "grant", "tenants:*:read")
        .await
        .unwrap();

    let t1 = Tenant::by_id(pool, "t1").await.unwrap().unwrap();
    let permissions = t1.permissions(pool).await.unwrap();

    // Should be granted read access
    assert!(acl::evaluate(&permissions, "tenants:123:read"));
    // Should be denied write access (no grant)
    assert!(!acl::evaluate(&permissions, "tenants:123:write"));
}

#[tokio::test]
async fn acl_deny_overrides_grant_at_same_specificity() {
    use crate::acl;

    let (db, _dir) = setup_db().await;
    let pool = &db.pool;

    Tenant::create(pool, "t1", "Alice", None, None, None, None)
        .await
        .unwrap();
    Group::create(pool, "g1", "mixed", "").await.unwrap();
    Group::add_member(pool, "g1", "t1").await.unwrap();

    // Grant and deny at same specificity
    Group::add_permission(pool, "p1", "g1", "grant", "tenants:*:read")
        .await
        .unwrap();
    Group::add_permission(pool, "p2", "g1", "deny", "tenants:*:read")
        .await
        .unwrap();

    let t1 = Tenant::by_id(pool, "t1").await.unwrap().unwrap();
    let permissions = t1.permissions(pool).await.unwrap();

    // Deny wins at equal specificity
    assert!(!acl::evaluate(&permissions, "tenants:123:read"));
}

#[tokio::test]
async fn acl_more_specific_grant_overrides_less_specific_deny() {
    use crate::acl;

    let (db, _dir) = setup_db().await;
    let pool = &db.pool;

    Tenant::create(pool, "t1", "Alice", None, None, None, None)
        .await
        .unwrap();
    Group::create(pool, "g1", "mixed", "").await.unwrap();
    Group::add_member(pool, "g1", "t1").await.unwrap();

    // Deny all tenants read, but grant specific tenant read
    Group::add_permission(pool, "p1", "g1", "deny", "tenants:*:read")
        .await
        .unwrap();
    Group::add_permission(pool, "p2", "g1", "grant", "tenants:123:read")
        .await
        .unwrap();

    let t1 = Tenant::by_id(pool, "t1").await.unwrap().unwrap();
    let permissions = t1.permissions(pool).await.unwrap();

    // More specific grant wins
    assert!(acl::evaluate(&permissions, "tenants:123:read"));
    // Other tenants still denied
    assert!(!acl::evaluate(&permissions, "tenants:456:read"));
}

#[tokio::test]
async fn tenant_delete_cascades_ip_mappings() {
    let (db, _dir) = setup_db().await;
    let pool = &db.pool;

    Tenant::create(pool, "t1", "Alice", None, None, None, None)
        .await
        .unwrap();
    add_ip_mapping(pool, "t1", "10.0.0.1").await.unwrap();

    Tenant::delete(pool, "t1").await.unwrap();

    // IP mapping should be gone
    let found = tenant_by_ip(pool, "10.0.0.1").await.unwrap();
    assert!(found.is_none());
}