dnsync 0.2.1

DNS Sync and Control with MCP
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
//! UniFi implementations of the vendor-neutral DNS service traits.
//!
//! UniFi DNS policies are site-scoped, not zone-scoped, so dnsync derives
//! logical zones by suffix matching. The integration exposes:
//!   - `list_records`  → GET /sites/{siteId}/dns/policies (paginated)
//!   - `add_record`    → POST /sites/{siteId}/dns/policies
//!   - `delete_record` → list, match by domain+type+value, DELETE by id
//!
//! Zones, cache, access lists, stats, settings, and zone import/export are
//! unsupported and return `Error::unsupported`. `FORWARD_DOMAIN` policies
//! are surfaced as provider-specific metadata in listings but cannot be
//! created or deleted through the record API.

use serde_json::Value;
use tracing::instrument;

use crate::control_plane::config::VendorKind;
use crate::core::dns::capabilities::VendorCapabilities;
use crate::core::dns::logs::{LogLine, LogsOptions, LogsRead};
use crate::core::dns::records::RecordData;
use crate::core::dns::responses::{ListRecordsResponse, ZoneInfo, ZoneRecord};
use crate::core::dns::service::{
    AccessListRead, AccessListWrite, CacheRead, CacheWrite, DnsVendor, ListRecordsOptions,
    RecordWrite, SettingsRead, StatsRead, ZoneExport, ZoneImport, ZoneRead, ZoneWrite,
};
use crate::core::error::{Error, Result};

use super::client::UnifiClient;
use super::mapping::{
    domain_matches_zone, policy_matches_delete_params, policy_to_zone_record,
    record_data_to_unifi_body,
};

// ─── DnsVendor ────────────────────────────────────────────────────────────────

impl DnsVendor for UnifiClient {
    fn kind(&self) -> VendorKind {
        VendorKind::Unifi
    }

    fn capabilities(&self) -> VendorCapabilities {
        VendorCapabilities {
            zones: false,
            records: true,
            cache: false,
            access_lists: false,
            // `get_settings` returns the controller's visible site list so
            // users can discover their site name/UUID without leaving the CLI.
            settings: true,
            zone_import: false,
            zone_export: false,
            logs: false,
        }
    }
}

// ─── ZoneRead ─────────────────────────────────────────────────────────────────

impl ZoneRead for UnifiClient {
    /// UniFi exposes no zone abstraction — there is nothing to list. Returning
    /// `unsupported` lets the trait surface that clearly rather than faking a
    /// synthetic zone list.
    async fn list_zones(&self, _page: u32, _per_page: u32) -> Result<Value> {
        Err(Error::unsupported("UniFi", "zone listing"))
    }

    #[instrument(
        skip(self, _options),
        fields(vendor = "unifi", operation = "list_records")
    )]
    async fn list_records<'a>(
        &'a self,
        domain: &'a str,
        zone: Option<&'a str>,
        _options: ListRecordsOptions,
    ) -> Result<ListRecordsResponse> {
        // Resolve the site first so a misconfigured site name fails with the
        // friendly site-not-found error instead of a misleading 404 from the
        // DNS policy endpoint.
        let site_id = self.resolve_site_id().await?.to_string();
        let policies = self.list_all_dns_policies(None).await?;

        let zone_label = zone
            .map(ToOwned::to_owned)
            .unwrap_or_else(|| domain.to_string());

        let records: Vec<ZoneRecord> = policies
            .iter()
            .filter(|p| domain_matches_zone(&p.domain, &zone_label))
            .map(|p| policy_to_zone_record(p, &zone_label))
            .collect();

        let zone_info = ZoneInfo {
            id: Some(site_id),
            name: zone_label,
            zone_type: "UniFi/Site".to_string(),
            disabled: false,
            dnssec_status: None,
        };

        Ok(ListRecordsResponse::single(zone_info, records))
    }
}

// ─── ZoneWrite (unsupported — UniFi has no zone model) ───────────────────────

impl ZoneWrite for UnifiClient {
    async fn create_zone<'a>(&'a self, _zone: &'a str, _zone_type: &'a str) -> Result<Value> {
        Err(Error::unsupported("UniFi", "zone creation"))
    }

    async fn delete_zone<'a>(&'a self, _zone: &'a str) -> Result<Value> {
        Err(Error::unsupported("UniFi", "zone deletion"))
    }

    async fn enable_zone<'a>(&'a self, _zone: &'a str) -> Result<Value> {
        Err(Error::unsupported("UniFi", "zone enable"))
    }

    async fn disable_zone<'a>(&'a self, _zone: &'a str) -> Result<Value> {
        Err(Error::unsupported("UniFi", "zone disable"))
    }
}

// ─── RecordWrite ──────────────────────────────────────────────────────────────

impl RecordWrite for UnifiClient {
    #[instrument(skip(self, record), fields(vendor = "unifi", operation = "add_record"))]
    async fn add_record<'a>(
        &'a self,
        zone: &'a str,
        domain: &'a str,
        ttl: u32,
        record: &'a RecordData,
    ) -> Result<Value> {
        let fqdn = resolve_fqdn(domain, zone);
        let body = record_data_to_unifi_body(&fqdn, ttl, true, record)?;
        let created = self.create_dns_policy(&body).await?;
        serde_json::to_value(created)
            .map_err(|e| Error::parse(format!("re-encoding UniFi create response: {e}")))
    }

    #[instrument(
        skip(self, type_params),
        fields(vendor = "unifi", operation = "delete_record")
    )]
    async fn delete_record<'a>(
        &'a self,
        zone: &'a str,
        domain: &'a str,
        type_params: &'a [(&'a str, String)],
    ) -> Result<Value> {
        let fqdn = resolve_fqdn(domain, zone);
        let policies = self.list_all_dns_policies(None).await?;

        let matched = policies
            .iter()
            .find(|p| policy_matches_delete_params(p, &fqdn, type_params))
            .ok_or_else(|| Error::Api {
                message: format!("no matching UniFi DNS policy found for '{fqdn}'"),
            })?;

        self.delete_dns_policy(&matched.id).await?;
        Ok(serde_json::json!({
            "id": matched.id,
            "domain": matched.domain,
            "type": matched.policy_type.as_str(),
            "deleted": true,
        }))
    }
}

/// Resolve a relative or absolute name within a zone into a UniFi FQDN.
///
/// A name is treated as already-qualified when it is the zone itself or
/// already sits below the zone (e.g. `"www.example.com"` inside zone
/// `"example.com"`). Multi-label relative names like `"a.b"` are appended to
/// the zone — UniFi DNS policies are flat FQDNs, so silently leaving a
/// relative dotted name unqualified would target the wrong domain.
fn resolve_fqdn(domain: &str, zone: &str) -> String {
    if domain == "@" {
        return zone.to_string();
    }
    let candidate = domain.trim_end_matches('.');
    let zone_lower = zone.to_ascii_lowercase();
    let cand_lower = candidate.to_ascii_lowercase();
    if cand_lower == zone_lower || cand_lower.ends_with(&format!(".{zone_lower}")) {
        candidate.to_string()
    } else {
        format!("{candidate}.{zone}")
    }
}

// ─── Unsupported operations ───────────────────────────────────────────────────

impl CacheRead for UnifiClient {
    async fn list_cache<'a>(&'a self, _domain: &'a str) -> Result<Value> {
        Err(Error::unsupported("UniFi", "cache listing"))
    }
}

impl CacheWrite for UnifiClient {
    async fn delete_cache_zone<'a>(&'a self, _domain: &'a str) -> Result<Value> {
        Err(Error::unsupported("UniFi", "cache zone deletion"))
    }

    async fn flush_cache(&self) -> Result<Value> {
        Err(Error::unsupported("UniFi", "cache flush"))
    }
}

impl StatsRead for UnifiClient {
    async fn get_stats<'a>(&'a self, _stats_type: &'a str) -> Result<Value> {
        Err(Error::unsupported("UniFi", "stats"))
    }
}

impl AccessListRead for UnifiClient {
    async fn list_blocked(&self) -> Result<Value> {
        Err(Error::unsupported("UniFi", "blocked list"))
    }

    async fn list_allowed(&self) -> Result<Value> {
        Err(Error::unsupported("UniFi", "allowed list"))
    }
}

impl AccessListWrite for UnifiClient {
    async fn add_blocked<'a>(&'a self, _domain: &'a str) -> Result<Value> {
        Err(Error::unsupported("UniFi", "add blocked"))
    }

    async fn delete_blocked<'a>(&'a self, _domain: &'a str) -> Result<Value> {
        Err(Error::unsupported("UniFi", "delete blocked"))
    }

    async fn add_allowed<'a>(&'a self, _domain: &'a str) -> Result<Value> {
        Err(Error::unsupported("UniFi", "add allowed"))
    }

    async fn delete_allowed<'a>(&'a self, _domain: &'a str) -> Result<Value> {
        Err(Error::unsupported("UniFi", "delete allowed"))
    }
}

impl ZoneImport for UnifiClient {
    async fn import_zone_file<'a>(
        &'a self,
        _zone: &'a str,
        _file_name: String,
        _file_bytes: Vec<u8>,
        _overwrite: bool,
        _overwrite_zone: bool,
        _overwrite_soa_serial: bool,
    ) -> Result<Value> {
        Err(Error::unsupported("UniFi", "zone import"))
    }
}

impl ZoneExport for UnifiClient {
    async fn export_zone_file<'a>(&'a self, _zone: &'a str) -> Result<String> {
        Err(Error::unsupported("UniFi", "zone export"))
    }
}

impl LogsRead for UnifiClient {
    async fn get_logs(&self, _options: LogsOptions) -> Result<Vec<LogLine>> {
        Err(Error::unsupported("UniFi", "logs"))
    }
}

impl SettingsRead for UnifiClient {
    /// Returns the list of UniFi sites accessible to this API key, plus the
    /// configured site label and whether it resolves to a known site. Use
    /// this to discover the human-readable site name to put in `org_id`.
    #[instrument(skip(self), fields(vendor = "unifi", operation = "get_settings"))]
    async fn get_settings(&self) -> Result<Value> {
        let sites = self.list_all_sites().await?;
        let configured = self.site();
        let resolved = super::responses::match_site(&sites, configured).map(|s| s.id.clone());
        Ok(serde_json::json!({
            "configuredSite": configured,
            "resolvedSiteId": resolved,
            "sites": sites,
        }))
    }
}

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

#[cfg(test)]
mod tests {
    use super::*;
    use crate::core::secret::ApiToken;

    fn make_client() -> UnifiClient {
        UnifiClient::new(
            "https://unifi.local/proxy/network/integration/v1".to_string(),
            ApiToken::new("test-token"),
            "11111111-1111-1111-1111-111111111111".to_string(),
        )
        .unwrap()
    }

    // ── kind / capabilities ──────────────────────────────────────────────────

    #[test]
    fn kind_returns_unifi() {
        assert_eq!(make_client().kind(), VendorKind::Unifi);
    }

    #[test]
    fn capabilities_advertise_records_and_settings() {
        let caps = make_client().capabilities();
        assert!(!caps.zones);
        assert!(caps.records);
        assert!(!caps.cache);
        assert!(!caps.access_lists);
        // `get_settings` exposes the site list for UUID discovery.
        assert!(caps.settings);
        assert!(!caps.zone_import);
        assert!(!caps.zone_export);
    }

    // ── unsupported operations ───────────────────────────────────────────────

    macro_rules! assert_unsupported {
        ($call:expr) => {
            match $call.await.unwrap_err() {
                Error::Unsupported { vendor, .. } => assert_eq!(vendor, "UniFi"),
                other => panic!("expected Unsupported, got {other:?}"),
            }
        };
    }

    #[tokio::test]
    async fn list_zones_is_unsupported() {
        assert_unsupported!(make_client().list_zones(0, 25));
    }

    #[tokio::test]
    async fn create_zone_is_unsupported() {
        assert_unsupported!(make_client().create_zone("example.com", "Primary"));
    }

    #[tokio::test]
    async fn delete_zone_is_unsupported() {
        assert_unsupported!(make_client().delete_zone("example.com"));
    }

    #[tokio::test]
    async fn enable_zone_is_unsupported() {
        assert_unsupported!(make_client().enable_zone("example.com"));
    }

    #[tokio::test]
    async fn disable_zone_is_unsupported() {
        assert_unsupported!(make_client().disable_zone("example.com"));
    }

    #[tokio::test]
    async fn list_cache_is_unsupported() {
        assert_unsupported!(make_client().list_cache("example.com"));
    }

    #[tokio::test]
    async fn delete_cache_zone_is_unsupported() {
        assert_unsupported!(make_client().delete_cache_zone("example.com"));
    }

    #[tokio::test]
    async fn flush_cache_is_unsupported() {
        assert_unsupported!(make_client().flush_cache());
    }

    #[tokio::test]
    async fn get_stats_is_unsupported() {
        assert_unsupported!(make_client().get_stats("last7days"));
    }

    #[tokio::test]
    async fn list_blocked_is_unsupported() {
        assert_unsupported!(make_client().list_blocked());
    }

    #[tokio::test]
    async fn list_allowed_is_unsupported() {
        assert_unsupported!(make_client().list_allowed());
    }

    #[tokio::test]
    async fn add_blocked_is_unsupported() {
        assert_unsupported!(make_client().add_blocked("evil.example.com"));
    }

    #[tokio::test]
    async fn delete_blocked_is_unsupported() {
        assert_unsupported!(make_client().delete_blocked("evil.example.com"));
    }

    #[tokio::test]
    async fn add_allowed_is_unsupported() {
        assert_unsupported!(make_client().add_allowed("ok.example.com"));
    }

    #[tokio::test]
    async fn delete_allowed_is_unsupported() {
        assert_unsupported!(make_client().delete_allowed("ok.example.com"));
    }

    #[tokio::test]
    async fn import_zone_file_is_unsupported() {
        assert_unsupported!(make_client().import_zone_file(
            "example.com",
            "zone.txt".to_string(),
            vec![],
            true,
            false,
            false,
        ));
    }

    #[tokio::test]
    async fn export_zone_file_is_unsupported() {
        assert_unsupported!(make_client().export_zone_file("example.com"));
    }

    // ── resolve_fqdn ─────────────────────────────────────────────────────────

    #[test]
    fn at_resolves_to_zone() {
        assert_eq!(resolve_fqdn("@", "example.com"), "example.com");
    }

    #[test]
    fn relative_label_joins_with_zone() {
        assert_eq!(resolve_fqdn("www", "example.com"), "www.example.com");
    }

    #[test]
    fn absolute_fqdn_is_kept() {
        assert_eq!(
            resolve_fqdn("www.example.com", "example.com"),
            "www.example.com"
        );
    }

    #[test]
    fn trailing_dot_is_stripped() {
        assert_eq!(
            resolve_fqdn("www.example.com.", "example.com"),
            "www.example.com"
        );
    }

    #[test]
    fn relative_dotted_label_is_appended_to_zone() {
        assert_eq!(resolve_fqdn("a.b", "example.com"), "a.b.example.com");
    }

    #[test]
    fn unrelated_fqdn_is_still_appended_to_zone() {
        // A name that is not under the zone is treated as relative and
        // appended — UniFi has no concept of cross-zone references.
        assert_eq!(resolve_fqdn("other.net", "example.com"), "other.net.example.com");
    }

    // ── add_record rejects unsupported types pre-flight ─────────────────────

    #[tokio::test]
    async fn add_record_rejects_unsupported_type_without_network_call() {
        let client = make_client();
        let err = client
            .add_record(
                "example.com",
                "@",
                300,
                &RecordData::Ns {
                    nameserver: "ns1.example.com".into(),
                    glue: None,
                },
            )
            .await
            .unwrap_err();
        // Should be Unsupported, not Network — mapping rejects before any HTTP.
        assert!(matches!(
            err,
            Error::Unsupported {
                vendor: "UniFi",
                ..
            }
        ));
    }
}