allowthem-saas 0.0.9

SaaS multi-tenancy layer for allowthem
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
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
//! DNS resolver trait, Hickory-backed real implementation, and `verify_domain`
//! helper.
//!
//! ## Design notes
//!
//! - No `async-trait` — follow the `AuthFuture` / `DnsFuture` pattern used
//!   by `EmailSender` and `SocialProvider` in `crates/core`.
//! - `MockDnsResolver` is gated with `#[cfg(any(test, feature = "test-util"))]`
//!   so it compiles only in tests and when the `test-util` Cargo feature is
//!   enabled (needed by `binaries/saas` integration tests in Task 4).
//! - `DomainStatus::Active` is intentionally never written here (38y.3 owns
//!   the `Verified → Active` transition).

use std::future::Future;
use std::pin::Pin;

use chrono::Utc;

use crate::control_db::ControlDb;
use crate::domains::{DomainId, DomainStatus};
use crate::error::SaasError;
use crate::tenants::TenantId;

// ── DnsFuture type alias ──────────────────────────────────────────────────────

/// Boxed future type alias mirroring the `AuthFuture` pattern in `crates/core`.
pub type DnsFuture<'a, T> = Pin<Box<dyn Future<Output = Result<T, DnsError>> + Send + 'a>>;

// ── DnsError ──────────────────────────────────────────────────────────────────

/// DNS resolver errors. Mapped to [`SaasError::Dns`] at the `verify_domain`
/// boundary so that `SaasError`'s surface stays tight.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum DnsError {
    /// The queried name does not exist in DNS (NXDOMAIN or no records found).
    NotFound,
    /// The resolver timed out.
    Timeout,
    /// Any other resolver failure (parse error, I/O error, etc.).
    Other(String),
}

impl std::fmt::Display for DnsError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            DnsError::NotFound => write!(f, "no DNS records found"),
            DnsError::Timeout => write!(f, "DNS query timed out"),
            DnsError::Other(msg) => write!(f, "DNS error: {msg}"),
        }
    }
}

// ── DnsResolver trait ─────────────────────────────────────────────────────────

/// Abstracts DNS CNAME resolution for testability.
///
/// Returns the CNAME chain rooted at `host`. An empty `Vec` signals that `host`
/// is an A/AAAA terminus (no CNAME at that name). Errors signal resolver
/// failures (timeout, parse errors, NXDOMAIN).
pub trait DnsResolver: Send + Sync {
    fn lookup_cname_chain<'a>(&'a self, host: &'a str) -> DnsFuture<'a, Vec<String>>;
}

// ── HickoryDnsResolver ────────────────────────────────────────────────────────

/// Real `DnsResolver` backed by `hickory-resolver`.
pub struct HickoryDnsResolver {
    inner: hickory_resolver::TokioResolver,
}

impl HickoryDnsResolver {
    /// Build a resolver using system defaults.
    pub fn new() -> Result<Self, SaasError> {
        use hickory_resolver::Resolver;
        use hickory_resolver::config::ResolverConfig;
        use hickory_resolver::net::runtime::TokioRuntimeProvider;

        let resolver = Resolver::builder_with_config(
            ResolverConfig::default(),
            TokioRuntimeProvider::default(),
        )
        .build()
        .map_err(|e| SaasError::Dns(format!("failed to build DNS resolver: {e}")))?;
        Ok(Self { inner: resolver })
    }
}

impl DnsResolver for HickoryDnsResolver {
    fn lookup_cname_chain<'a>(&'a self, host: &'a str) -> DnsFuture<'a, Vec<String>> {
        Box::pin(async move {
            use hickory_resolver::proto::rr::{RData, RecordType};

            const HOP_CAP: usize = 8;
            let mut chain = Vec::new();
            let mut current = host.to_owned();

            for _ in 0..HOP_CAP {
                match self.inner.lookup(current.as_str(), RecordType::CNAME).await {
                    Ok(lookup) => {
                        // Extract the first CNAME target from the answer section.
                        let target = lookup.answers().iter().find_map(|record| {
                            if let RData::CNAME(cname) = &record.data {
                                let name = cname.0.to_string();
                                // Strip trailing FQDN dot so comparisons are clean.
                                Some(name.trim_end_matches('.').to_lowercase())
                            } else {
                                None
                            }
                        });
                        match target {
                            Some(t) => {
                                chain.push(t.clone());
                                current = t;
                            }
                            // Response with no CNAME records = A/AAAA terminus.
                            None => break,
                        }
                    }
                    Err(e) if e.is_no_records_found() => {
                        // NXDOMAIN or no records of the queried type — chain
                        // terminated. Return what we have (possibly empty).
                        break;
                    }
                    Err(e) => {
                        // Classify the resolver error.
                        use hickory_resolver::net::NetError;
                        let dns_err = match e {
                            NetError::Timeout => DnsError::Timeout,
                            other => DnsError::Other(other.to_string()),
                        };
                        return Err(dns_err);
                    }
                }
            }

            Ok(chain)
        })
    }
}

// ── MockDnsResolver ───────────────────────────────────────────────────────────

/// Controllable `DnsResolver` for tests.
///
/// Each call to `lookup_cname_chain` pops one pre-queued response from the
/// `VecDeque` for that host, allowing tests to model sequences such as
/// "first attempt times out, second returns the correct chain".
///
/// Gated with `#[cfg(any(test, feature = "test-util"))]` so the type does
/// not leak into production builds.
#[cfg(any(test, feature = "test-util"))]
pub struct MockDnsResolver {
    chains: tokio::sync::Mutex<
        std::collections::HashMap<
            String,
            std::collections::VecDeque<Result<Vec<String>, DnsError>>,
        >,
    >,
}

#[cfg(any(test, feature = "test-util"))]
impl MockDnsResolver {
    pub fn new() -> Self {
        Self {
            chains: tokio::sync::Mutex::new(std::collections::HashMap::new()),
        }
    }

    /// Queue a response for the given host. Responses are consumed in order.
    pub async fn queue(&self, host: &str, response: Result<Vec<String>, DnsError>) {
        self.chains
            .lock()
            .await
            .entry(host.to_owned())
            .or_default()
            .push_back(response);
    }
}

#[cfg(any(test, feature = "test-util"))]
impl DnsResolver for MockDnsResolver {
    fn lookup_cname_chain<'a>(&'a self, host: &'a str) -> DnsFuture<'a, Vec<String>> {
        Box::pin(async move {
            let mut guard = self.chains.lock().await;
            guard
                .get_mut(host)
                .and_then(|q| q.pop_front())
                .unwrap_or(Ok(vec![]))
        })
    }
}

// ── verify_domain helper ──────────────────────────────────────────────────────

/// Verify that `domain`'s CNAME chain contains `dns_target`, updating the
/// `tenant_domains` row accordingly.
///
/// - On success (`dns_target` appears anywhere in the chain): status → `Verified`.
/// - On failure (no match or DNS error): status → `Failed`, `last_error` set.
/// - DB write failures bubble as `SaasError`; DNS errors are swallowed into
///   `Failed`.
///
/// `_tenant_id` is carried for caller context and future audit logging; the
/// status update itself is addressed by `domain_id` alone.
pub async fn verify_domain(
    resolver: &dyn DnsResolver,
    control_db: &ControlDb,
    domain_id: DomainId,
    _tenant_id: TenantId,
    domain: &str,
    dns_target: &str,
) -> Result<DomainStatus, SaasError> {
    let target_lower = dns_target.to_lowercase();

    match resolver.lookup_cname_chain(domain).await {
        Ok(chain) => {
            // Check whether dns_target appears anywhere in the chain (§3.6:
            // use `contains`, not `last()`, to survive CDN extra hops).
            let found = chain.iter().any(|hop| {
                let hop_stripped = hop.trim_end_matches('.');
                hop_stripped.eq_ignore_ascii_case(&target_lower)
            });

            if found {
                control_db
                    .set_tenant_domain_status(
                        &domain_id,
                        DomainStatus::Verified,
                        Some(Utc::now()),
                        None,
                    )
                    .await?;
                Ok(DomainStatus::Verified)
            } else {
                let msg = format!("CNAME chain {chain:?} does not contain {dns_target}");
                control_db
                    .set_tenant_domain_status(
                        &domain_id,
                        DomainStatus::Failed,
                        None,
                        Some(msg.as_str()),
                    )
                    .await?;
                Ok(DomainStatus::Failed)
            }
        }
        Err(DnsError::NotFound) => {
            control_db
                .set_tenant_domain_status(
                    &domain_id,
                    DomainStatus::Failed,
                    None,
                    Some("no CNAME record found"),
                )
                .await?;
            Ok(DomainStatus::Failed)
        }
        Err(e) => {
            let msg = e.to_string();
            control_db
                .set_tenant_domain_status(
                    &domain_id,
                    DomainStatus::Failed,
                    None,
                    Some(msg.as_str()),
                )
                .await?;
            Ok(DomainStatus::Failed)
        }
    }
}

// ── Background sweep helper ───────────────────────────────────────────────────

/// Statistics returned by one sweep run.
#[derive(Debug, Default)]
pub struct SweepStats {
    pub processed: usize,
    pub verified: usize,
    pub failed: usize,
}

/// Run one verification sweep over `pending_verification` and `failed` rows,
/// capped at `limit` rows (oldest-first). Reuses [`verify_domain`].
pub async fn run_one_sweep(
    resolver: &dyn DnsResolver,
    control_db: &ControlDb,
    limit: usize,
) -> Result<SweepStats, SaasError> {
    use crate::domains::DomainId as Did;
    use uuid::Uuid;

    let rows = control_db.list_domains_for_sweep(limit as i64).await?;
    let mut stats = SweepStats::default();

    for row in &rows {
        let domain_id = match Uuid::from_slice(&row.id) {
            Ok(u) => Did::from(u),
            Err(_) => {
                tracing::warn!("skipping domain row with undecodable UUID in sweep");
                continue;
            }
        };
        let tenant_id = match Uuid::from_slice(&row.tenant_id) {
            Ok(u) => TenantId::from(u),
            Err(_) => {
                tracing::warn!("skipping domain row with undecodable tenant UUID in sweep");
                continue;
            }
        };

        stats.processed += 1;
        match verify_domain(
            resolver,
            control_db,
            domain_id,
            tenant_id,
            &row.domain,
            &row.dns_target,
        )
        .await
        {
            Ok(DomainStatus::Verified) => stats.verified += 1,
            Ok(_) => stats.failed += 1,
            Err(e) => {
                tracing::warn!(domain = %row.domain, error = %e, "sweep: DB error on domain");
                stats.failed += 1;
            }
        }
    }

    Ok(stats)
}

// ── Tests ─────────────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;
    use crate::control_db::ControlDb;
    use crate::control_db::tests::test_pool;
    use crate::domains::DomainId;
    use uuid::Uuid;

    // ─── Helper: build a seeded DB with one pending domain ───────────────────

    async fn setup(slug: &str, domain: &str) -> (ControlDb, DomainId, TenantId) {
        let pool = test_pool().await;
        let db = ControlDb::new(pool).await.unwrap();

        // Seed a tenant.
        let plan_id: Vec<u8> = sqlx::query_scalar("SELECT id FROM tenant_plans LIMIT 1")
            .fetch_one(db.pool())
            .await
            .unwrap();
        let tid_uuid = Uuid::now_v7();
        sqlx::query(
            "INSERT INTO tenants (id, name, slug, owner_email, plan_id, status, db_path) \
             VALUES (?, ?, ?, ?, ?, 'active', ?)",
        )
        .bind(tid_uuid.as_bytes().as_ref())
        .bind(slug)
        .bind(slug)
        .bind(format!("{slug}@example.com"))
        .bind(&plan_id)
        .bind(format!("{slug}.db"))
        .execute(db.pool())
        .await
        .unwrap();

        let tenant_id = TenantId::from(tid_uuid);
        let row = db
            .create_tenant_domain(&tenant_id, domain, "custom.allowthem.io")
            .await
            .unwrap();
        let domain_id = DomainId::from(Uuid::from_slice(&row.id).unwrap());

        (db, domain_id, tenant_id)
    }

    // ─── verify_domain tests ─────────────────────────────────────────────────

    #[tokio::test]
    async fn verify_domain_marks_verified_when_chain_contains_target() {
        let (db, domain_id, tenant_id) = setup("vf-a", "auth.example.com").await;
        let resolver = MockDnsResolver::new();
        resolver
            .queue("auth.example.com", Ok(vec!["custom.allowthem.io".into()]))
            .await;

        let status = verify_domain(
            &resolver,
            &db,
            domain_id,
            tenant_id,
            "auth.example.com",
            "custom.allowthem.io",
        )
        .await
        .unwrap();

        assert_eq!(status, DomainStatus::Verified);
        let row = db
            .get_tenant_domain_scoped(&domain_id, &tenant_id)
            .await
            .unwrap()
            .unwrap();
        assert_eq!(row.status, DomainStatus::Verified);
        assert!(row.verified_at.is_some());
        assert!(row.last_error.is_none());
    }

    #[tokio::test]
    async fn verify_domain_marks_verified_when_target_has_trailing_dot() {
        let (db, domain_id, tenant_id) = setup("vf-b", "auth.example.com").await;
        let resolver = MockDnsResolver::new();
        // Chain hop ends with a trailing dot (FQDN).
        resolver
            .queue("auth.example.com", Ok(vec!["custom.allowthem.io.".into()]))
            .await;

        let status = verify_domain(
            &resolver,
            &db,
            domain_id,
            tenant_id,
            "auth.example.com",
            "custom.allowthem.io",
        )
        .await
        .unwrap();

        assert_eq!(status, DomainStatus::Verified);
    }

    #[tokio::test]
    async fn verify_domain_marks_verified_when_target_is_not_last_hop() {
        // Target appears mid-chain (CDN adds more hops after it).
        let (db, domain_id, tenant_id) = setup("vf-c", "auth.example.com").await;
        let resolver = MockDnsResolver::new();
        resolver
            .queue(
                "auth.example.com",
                Ok(vec![
                    "custom.allowthem.io".into(),
                    "cdn-edge.fastly.net".into(),
                ]),
            )
            .await;

        let status = verify_domain(
            &resolver,
            &db,
            domain_id,
            tenant_id,
            "auth.example.com",
            "custom.allowthem.io",
        )
        .await
        .unwrap();

        assert_eq!(status, DomainStatus::Verified);
    }

    #[tokio::test]
    async fn verify_domain_marks_failed_when_no_cname() {
        let (db, domain_id, tenant_id) = setup("vf-d", "auth.example.com").await;
        let resolver = MockDnsResolver::new();
        resolver
            .queue("auth.example.com", Err(DnsError::NotFound))
            .await;

        let status = verify_domain(
            &resolver,
            &db,
            domain_id,
            tenant_id,
            "auth.example.com",
            "custom.allowthem.io",
        )
        .await
        .unwrap();

        assert_eq!(status, DomainStatus::Failed);
        let row = db
            .get_tenant_domain_scoped(&domain_id, &tenant_id)
            .await
            .unwrap()
            .unwrap();
        assert_eq!(row.status, DomainStatus::Failed);
        assert!(row.last_error.as_deref().unwrap().contains("no CNAME"));
    }

    #[tokio::test]
    async fn verify_domain_marks_failed_when_chain_has_no_matching_hop() {
        let (db, domain_id, tenant_id) = setup("vf-e", "auth.example.com").await;
        let resolver = MockDnsResolver::new();
        resolver
            .queue(
                "auth.example.com",
                Ok(vec!["other-provider.example.net".into()]),
            )
            .await;

        let status = verify_domain(
            &resolver,
            &db,
            domain_id,
            tenant_id,
            "auth.example.com",
            "custom.allowthem.io",
        )
        .await
        .unwrap();

        assert_eq!(status, DomainStatus::Failed);
        let row = db
            .get_tenant_domain_scoped(&domain_id, &tenant_id)
            .await
            .unwrap()
            .unwrap();
        assert!(
            row.last_error
                .as_deref()
                .unwrap()
                .contains("does not contain")
        );
    }

    #[tokio::test]
    async fn verify_domain_propagates_db_errors_through_saas_error() {
        // Force a DB write failure by closing the pool.
        let (db, domain_id, tenant_id) = setup("vf-f", "auth.example.com").await;
        db.pool().close().await;

        let resolver = MockDnsResolver::new();
        resolver
            .queue("auth.example.com", Ok(vec!["custom.allowthem.io".into()]))
            .await;

        let result = verify_domain(
            &resolver,
            &db,
            domain_id,
            tenant_id,
            "auth.example.com",
            "custom.allowthem.io",
        )
        .await;

        assert!(result.is_err(), "expected DB error to propagate");
    }

    // ─── MockDnsResolver hop-cap test ────────────────────────────────────────

    #[tokio::test]
    async fn lookup_cname_chain_caps_at_8_hops() {
        // Queue 9 hops — MockDnsResolver will return them one at a time.
        // The resolver should stop at 8.
        let resolver = MockDnsResolver::new();
        for i in 0..9_usize {
            let host = if i == 0 {
                "start.example.com".to_owned()
            } else {
                format!("hop{i}.example.com")
            };
            let next = format!("hop{}.example.com", i + 1);
            resolver.queue(&host, Ok(vec![next])).await;
        }

        let chain = resolver
            .lookup_cname_chain("start.example.com")
            .await
            .unwrap();
        // MockDnsResolver always pops one entry per call. With the cap at 8,
        // "start" + hops 1..8 → 8 entries in the chain.
        assert!(
            chain.len() <= 8,
            "chain length {} exceeds hop cap",
            chain.len()
        );
    }

    // ─── run_one_sweep tests ─────────────────────────────────────────────────

    #[tokio::test]
    async fn sweep_processes_pending_then_verified_disappears_from_pool() {
        let (db, domain_id, tenant_id) = setup("sw-a", "auth.example.com").await;
        let resolver = std::sync::Arc::new(MockDnsResolver::new());
        resolver
            .queue("auth.example.com", Ok(vec!["custom.allowthem.io".into()]))
            .await;

        let stats = run_one_sweep(&*resolver, &db, 100).await.unwrap();
        assert_eq!(stats.verified, 1);
        assert_eq!(stats.processed, 1);

        // Row should now be Verified, so a second sweep sees nothing.
        let _ = (domain_id, tenant_id); // used to seed
        let stats2 = run_one_sweep(&*resolver, &db, 100).await.unwrap();
        assert_eq!(stats2.processed, 0);
    }

    #[tokio::test]
    async fn sweep_caps_at_limit() {
        let pool = test_pool().await;
        let db = ControlDb::new(pool).await.unwrap();
        let plan_id: Vec<u8> = sqlx::query_scalar("SELECT id FROM tenant_plans LIMIT 1")
            .fetch_one(db.pool())
            .await
            .unwrap();

        // Seed 5 domains for one tenant.
        let tid_uuid = Uuid::now_v7();
        sqlx::query(
            "INSERT INTO tenants (id, name, slug, owner_email, plan_id, status, db_path) \
             VALUES (?, 'Cap', 'sw-cap', 'cap@example.com', ?, 'active', 'cap.db')",
        )
        .bind(tid_uuid.as_bytes().as_ref())
        .bind(&plan_id)
        .execute(db.pool())
        .await
        .unwrap();
        let tenant_id = TenantId::from(tid_uuid);

        for i in 0..5 {
            db.create_tenant_domain(
                &tenant_id,
                &format!("d{i}.example.com"),
                "custom.allowthem.io",
            )
            .await
            .unwrap();
        }

        let resolver = MockDnsResolver::new();
        let stats = run_one_sweep(&resolver, &db, 3).await.unwrap();
        assert_eq!(stats.processed, 3, "sweep should respect limit");
    }

    #[tokio::test]
    async fn sweep_failed_row_can_be_reprocessed_in_next_sweep() {
        let (db, domain_id, tenant_id) = setup("sw-b", "auth.example.com").await;
        let resolver = MockDnsResolver::new();

        // First sweep: wrong chain → Failed.
        resolver
            .queue("auth.example.com", Ok(vec!["wrong.example.net".into()]))
            .await;
        let s1 = run_one_sweep(&resolver, &db, 100).await.unwrap();
        assert_eq!(s1.failed, 1);

        // Second sweep: correct chain → Verified.
        resolver
            .queue("auth.example.com", Ok(vec!["custom.allowthem.io".into()]))
            .await;
        let s2 = run_one_sweep(&resolver, &db, 100).await.unwrap();
        assert_eq!(s2.verified, 1);

        let row = db
            .get_tenant_domain_scoped(&domain_id, &tenant_id)
            .await
            .unwrap()
            .unwrap();
        assert_eq!(row.status, DomainStatus::Verified);
    }
}