Skip to main content

hyprstream_core/account/
mod.rs

1//! Account identity — host-form `did:web` account zone, DNS interface, and
2//! DNS-01 wildcard TLS (epic #1158, Phase A3 / ticket #1162).
3//!
4//! This module owns the deployment-facing configuration surface for the account
5//! zone and the **interfaces** (not implementations) for the two product-layer
6//! concerns above it: managed DNS and ACME DNS-01 wildcard issuance.
7//!
8//! # What hyprstream owns vs. what the product layer owns
9//!
10//! | Concern | Owner |
11//! |---|---|
12//! | Account-zone apex name (no default — sane-degrade) | hyprstream (`config`) |
13//! | Wildcard shape `*.<apex>` (exactly one label — the A2 seam) | hyprstream (`zone`) |
14//! | Renewal schedule + expiry alarm (total-outage surface) | hyprstream (`renewal`) |
15//! | Sane-degrade when unconfigured | hyprstream (`config`, `tls`) |
16//! | Publishing DNS-01 TXT + wildcard A/AAAA records | **product layer** (`dns::DnsProvider`) |
17//! | Driving the ACME directory for DNS-01 issuance | **product layer** (`tls::WildcardCertIssuer`) |
18//!
19//! The account **label grammar** (LDH, NFKC, confusable rejection, reserved
20//! list, never-reuse) is owned by A2 (`#1160`); B1 enforces it at mint time.
21//! This crate only guarantees the wildcard covers exactly one label.
22//!
23//! # No PowerDNS, anywhere
24//!
25//! hyprstream must not require a specific DNS server. If it did, self-hosters
26//! could not run it. Managed DNS is a product layer above a protocol that does
27//! not depend on it — the same posture the epic takes for managed DNS.
28
29pub mod config;
30pub mod dns;
31pub mod renewal;
32pub mod tls;
33pub mod zone;
34
35pub use config::{AccountHttpConfig, AccountZoneConfig};
36pub use dns::{DnsError, DnsProvider, UnconfiguredDnsProvider};
37pub use renewal::{AlarmThresholds, CertHealth, CertHealthMonitor, CertRenewer};
38pub use tls::{CertHandle, IssuanceError, IssuedCert, NullWildcardCertIssuer, WildcardCertIssuer};
39pub use zone::{AccountZone, AccountZoneError};
40
41use std::net::{Ipv4Addr, Ipv6Addr};
42
43/// Publish the wildcard A/AAAA records for the delegated account zone, if the
44/// deployment configured targets and wired a DNS backend.
45///
46/// The wildcard covers **exactly one** label (see [`zone::AccountZone`]). Both
47/// `v4`/`v6` are optional — an IPv6-only deployment publishes AAAA only.
48///
49/// This is a convenience entry point over [`DnsProvider`]; it logs each
50/// publication and a clear message when a target is configured but no backend
51/// is wired (sane-degrade, not a panic).
52pub async fn publish_wildcard_records(
53    zone: &AccountZone,
54    dns: &dyn DnsProvider,
55    v4: Option<Ipv4Addr>,
56    v6: Option<Ipv6Addr>,
57    ttl: u32,
58) -> anyhow::Result<()> {
59    if let Some(v4) = v4 {
60        if let Err(e) = dns.publish_wildcard_a(zone, v4, ttl).await {
61            tracing::warn!(
62                zone = %zone.apex(),
63                backend = dns.backend(),
64                error = %e,
65                "failed to publish wildcard A for account zone"
66            );
67            anyhow::bail!(
68                "wildcard A publication for {zone} via {} failed: {e}",
69                dns.backend()
70            );
71        }
72        tracing::info!(zone = %zone.apex(), target = %v4, "published wildcard A for account zone");
73    }
74    if let Some(v6) = v6 {
75        if let Err(e) = dns.publish_wildcard_aaaa(zone, v6, ttl).await {
76            tracing::warn!(
77                zone = %zone.apex(),
78                backend = dns.backend(),
79                error = %e,
80                "failed to publish wildcard AAAA for account zone"
81            );
82            anyhow::bail!(
83                "wildcard AAAA publication for {zone} via {} failed: {e}",
84                dns.backend()
85            );
86        }
87        tracing::info!(zone = %zone.apex(), target = %v6, "published wildcard AAAA for account zone");
88    }
89    Ok(())
90}
91
92#[cfg(test)]
93#[allow(clippy::unwrap_used)]
94mod tests {
95    use super::*;
96
97    #[tokio::test]
98    async fn publish_records_fails_loudly_on_unconfigured() {
99        let z = AccountZone::new("acct.example.com").unwrap();
100        let dns = UnconfiguredDnsProvider::new();
101        let err =
102            publish_wildcard_records(&z, &dns, Some("203.0.113.10".parse().unwrap()), None, 300)
103                .await
104                .unwrap_err();
105        let msg = err.to_string();
106        assert!(msg.contains("wildcard A"), "{msg}");
107    }
108
109    #[tokio::test]
110    async fn publish_records_noop_when_no_targets() {
111        let z = AccountZone::new("acct.example.com").unwrap();
112        let dns = UnconfiguredDnsProvider::new();
113        // No targets configured → no publication attempted → no error even on
114        // an unconfigured backend (operator may publish records out-of-band).
115        publish_wildcard_records(&z, &dns, None, None, 300)
116            .await
117            .unwrap();
118    }
119}