dns-orchestrator-provider 0.1.2

DNS provider abstraction library for multiple cloud platforms (Cloudflare, Aliyun, DNSPod, Huaweicloud)
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
//! Cloudflare `DnsProvider` trait implementation

use std::fmt::Write;

use async_trait::async_trait;
use serde::Deserialize;

use crate::error::{ProviderError, Result};
use crate::providers::common::{full_name_to_relative, relative_to_full_name};
use crate::traits::{DnsProvider, ErrorContext, ProviderErrorMapper};
use crate::types::{
    CreateDnsRecordRequest, DnsRecord, DomainStatus, FieldType, PaginatedResponse,
    PaginationParams, ProviderCredentialField, ProviderDomain, ProviderFeatures, ProviderLimits,
    ProviderMetadata, ProviderType, RecordData, RecordQueryParams, UpdateDnsRecordRequest,
};

use super::{
    CloudflareCaaData, CloudflareDnsRecord, CloudflareProvider, CloudflareSrvData, CloudflareZone,
    MAX_PAGE_SIZE_RECORDS, MAX_PAGE_SIZE_ZONES,
};

impl CloudflareProvider {
    /// Get domain name information (use cache first)
    async fn get_domain_cached(&self, domain_id: &str) -> Result<ProviderDomain> {
        if let Some(domain) = self.domain_cache.get(domain_id) {
            return Ok(domain);
        }
        let domain = self.get_domain(domain_id).await?;
        self.domain_cache.insert(domain_id, &domain);
        Ok(domain)
    }

    /// Convert Cloudflare zone to `ProviderDomain`
    /// Cloudflare status: active, pending, initializing, moved
    pub(crate) fn zone_to_domain(zone: CloudflareZone) -> ProviderDomain {
        let status = match zone.status.as_str() {
            "active" => DomainStatus::Active,
            "pending" | "initializing" => DomainStatus::Pending,
            "moved" => DomainStatus::Paused,
            _ => DomainStatus::Unknown,
        };

        ProviderDomain {
            id: zone.id,
            name: zone.name,
            provider: ProviderType::Cloudflare,
            status,
            record_count: None,
        }
    }

    /// Convert Cloudflare records to `DnsRecord`
    pub(crate) fn cf_record_to_dns_record(
        &self,
        cf_record: CloudflareDnsRecord,
        zone_id: &str,
        zone_name: &str,
    ) -> Result<DnsRecord> {
        let CloudflareDnsRecord {
            id,
            record_type,
            name,
            content,
            ttl,
            priority,
            proxied,
            created_on,
            modified_on,
            data,
        } = cf_record;

        let record_data = self.parse_record_data(&record_type, content, priority, data)?;

        Ok(DnsRecord {
            id,
            domain_id: zone_id.to_string(),
            name: full_name_to_relative(&name, zone_name),
            ttl,
            data: record_data,
            proxied,
            created_at: created_on.and_then(|s| {
                chrono::DateTime::parse_from_rfc3339(&s)
                    .ok()
                    .map(|dt| dt.with_timezone(&chrono::Utc))
            }),
            updated_at: modified_on.and_then(|s| {
                chrono::DateTime::parse_from_rfc3339(&s)
                    .ok()
                    .map(|dt| dt.with_timezone(&chrono::Utc))
            }),
        })
    }

    /// Parse Cloudflare record as `RecordData`
    fn parse_record_data(
        &self,
        record_type: &str,
        content: String,
        priority: Option<u16>,
        data: Option<serde_json::Value>,
    ) -> Result<RecordData> {
        match record_type {
            "A" => Ok(RecordData::A { address: content }),
            "AAAA" => Ok(RecordData::AAAA { address: content }),
            "CNAME" => Ok(RecordData::CNAME { target: content }),
            "MX" => Ok(RecordData::MX {
                priority: priority
                    .ok_or_else(|| self.parse_error("MX record missing priority field"))?,
                exchange: content,
            }),
            "TXT" => Ok(RecordData::TXT { text: content }),
            "NS" => Ok(RecordData::NS {
                nameserver: content,
            }),
            "SRV" => {
                // SRV records use the data field
                if let Some(data) = data {
                    let srv: CloudflareSrvData = serde_json::from_value(data)
                        .map_err(|e| self.parse_error(format!("Failed to parse SRV data: {e}")))?;
                    Ok(RecordData::SRV {
                        priority: srv.priority,
                        weight: srv.weight,
                        port: srv.port,
                        target: srv.target,
                    })
                } else {
                    // Fallback: Try to parse from content
                    let parts: Vec<&str> = content.split_whitespace().collect();
                    if parts.len() >= 3 {
                        Ok(RecordData::SRV {
                            priority: priority.ok_or_else(|| {
                                self.parse_error("SRV record missing priority field")
                            })?,
                            weight: parts[0].parse().map_err(|_| {
                                self.parse_error(format!("Invalid SRV weight: '{}'", parts[0]))
                            })?,
                            port: parts[1].parse().map_err(|_| {
                                self.parse_error(format!("Invalid SRV port: '{}'", parts[1]))
                            })?,
                            target: parts[2].to_string(),
                        })
                    } else {
                        Err(self.parse_error(format!(
                            "Invalid SRV record format: expected 'weight port target', got '{content}'"
                        )))
                    }
                }
            }
            "CAA" => {
                // CAA records use the data field
                if let Some(data) = data {
                    let caa: CloudflareCaaData = serde_json::from_value(data)
                        .map_err(|e| self.parse_error(format!("Failed to parse CAA data: {e}")))?;
                    Ok(RecordData::CAA {
                        flags: caa.flags,
                        tag: caa.tag,
                        value: caa.value,
                    })
                } else {
                    // Fallback: Try to parse "flags tag value" from content
                    let parts: Vec<&str> = content.splitn(3, ' ').collect();
                    if parts.len() >= 3 {
                        Ok(RecordData::CAA {
                            flags: parts[0].parse().map_err(|_| {
                                self.parse_error(format!("Invalid CAA flags: '{}'", parts[0]))
                            })?,
                            tag: parts[1].to_string(),
                            value: parts[2].trim_matches('"').to_string(),
                        })
                    } else {
                        Err(self.parse_error(format!(
                            "Invalid CAA record format: expected 'flags tag value', got '{content}'"
                        )))
                    }
                }
            }
            _ => Err(ProviderError::UnsupportedRecordType {
                provider: self.provider_name().to_string(),
                record_type: record_type.to_string(),
            }),
        }
    }

    /// Convert `RecordData` to Cloudflare API request body
    fn build_create_body(
        full_name: &str,
        ttl: u32,
        data: &RecordData,
        proxied: Option<bool>,
    ) -> serde_json::Value {
        match data {
            RecordData::A { address } => serde_json::json!({
                "type": "A",
                "name": full_name,
                "content": address,
                "ttl": ttl,
                "proxied": proxied,
            }),
            RecordData::AAAA { address } => serde_json::json!({
                "type": "AAAA",
                "name": full_name,
                "content": address,
                "ttl": ttl,
                "proxied": proxied,
            }),
            RecordData::CNAME { target } => serde_json::json!({
                "type": "CNAME",
                "name": full_name,
                "content": target,
                "ttl": ttl,
                "proxied": proxied,
            }),
            RecordData::MX { priority, exchange } => serde_json::json!({
                "type": "MX",
                "name": full_name,
                "content": exchange,
                "ttl": ttl,
                "priority": priority,
            }),
            RecordData::TXT { text } => serde_json::json!({
                "type": "TXT",
                "name": full_name,
                "content": text,
                "ttl": ttl,
            }),
            RecordData::NS { nameserver } => serde_json::json!({
                "type": "NS",
                "name": full_name,
                "content": nameserver,
                "ttl": ttl,
            }),
            RecordData::SRV {
                priority,
                weight,
                port,
                target,
            } => serde_json::json!({
                "type": "SRV",
                "name": full_name,
                "ttl": ttl,
                "data": {
                    "priority": priority,
                    "weight": weight,
                    "port": port,
                    "target": target,
                }
            }),
            RecordData::CAA { flags, tag, value } => serde_json::json!({
                "type": "CAA",
                "name": full_name,
                "ttl": ttl,
                "data": {
                    "flags": flags,
                    "tag": tag,
                    "value": value,
                }
            }),
        }
    }
}

#[async_trait]
impl DnsProvider for CloudflareProvider {
    fn id(&self) -> &'static str {
        "cloudflare"
    }

    fn metadata() -> ProviderMetadata {
        ProviderMetadata {
            id: ProviderType::Cloudflare,
            name: "Cloudflare".to_string(),
            description: "Global CDN and DNS service provider".to_string(),
            required_fields: vec![ProviderCredentialField {
                key: "apiToken".to_string(),
                label: "API Token".to_string(),
                field_type: FieldType::Password,
                placeholder: Some("Enter Cloudflare API Token".to_string()),
                help_text: Some(
                    "Create at Cloudflare Dashboard -> My Profile -> API Tokens".to_string(),
                ),
            }],
            features: ProviderFeatures { proxy: true },
            limits: ProviderLimits {
                max_page_size_domains: 50,
                max_page_size_records: 5000,
            },
        }
    }

    async fn validate_credentials(&self) -> Result<bool> {
        #[derive(Deserialize)]
        struct VerifyResponse {
            status: String,
        }

        match self
            .get::<VerifyResponse>("/user/tokens/verify", ErrorContext::default())
            .await
        {
            Ok(resp) => Ok(resp.status == "active"),
            Err(
                ProviderError::InvalidCredentials { .. } | ProviderError::PermissionDenied { .. },
            ) => Ok(false),
            Err(e) => Err(e),
        }
    }

    async fn list_domains(
        &self,
        params: &PaginationParams,
    ) -> Result<PaginatedResponse<ProviderDomain>> {
        let params = params.validated(MAX_PAGE_SIZE_ZONES);
        let (zones, total_count): (Vec<CloudflareZone>, u32) = self
            .get_paginated("/zones", &params, ErrorContext::default())
            .await?;
        let domains = zones.into_iter().map(Self::zone_to_domain).collect();
        Ok(PaginatedResponse::new(
            domains,
            params.page,
            params.page_size,
            total_count,
        ))
    }

    async fn get_domain(&self, domain_id: &str) -> Result<ProviderDomain> {
        let ctx = ErrorContext {
            domain: Some(domain_id.to_string()),
            ..Default::default()
        };
        let zone: CloudflareZone = self.get(&format!("/zones/{domain_id}"), ctx).await?;
        Ok(Self::zone_to_domain(zone))
    }

    async fn list_records(
        &self,
        domain_id: &str,
        params: &RecordQueryParams,
    ) -> Result<PaginatedResponse<DnsRecord>> {
        let params = params.validated(MAX_PAGE_SIZE_RECORDS);
        let ctx = ErrorContext {
            domain: Some(domain_id.to_string()),
            ..Default::default()
        };

        // Get zone information first to get domain name (use cache)
        let domain_info = self.get_domain_cached(domain_id).await?;
        let zone_name = domain_info.name;

        // Build query URL, including search parameters
        let mut url = format!(
            "/zones/{}/dns_records?page={}&per_page={}",
            domain_id,
            params.page,
            params.page_size.min(MAX_PAGE_SIZE_RECORDS)
        );

        // Add search keywords (only search record names)
        if let Some(ref keyword) = params.keyword
            && !keyword.is_empty()
        {
            let _ = write!(url, "&name.contains={}", urlencoding::encode(keyword));
        }

        // Add record type filter
        if let Some(ref record_type) = params.record_type {
            let type_str = crate::providers::common::record_type_to_string(record_type);
            let _ = write!(url, "&type={}", urlencoding::encode(type_str));
        }

        let (cf_records, total_count) = self.get_records(&url, ctx).await?;

        let records: Vec<DnsRecord> = cf_records
            .into_iter()
            .filter_map(
                |r| match self.cf_record_to_dns_record(r, domain_id, &zone_name) {
                    Ok(record) => Some(record),
                    Err(ProviderError::UnsupportedRecordType { .. }) => None,
                    Err(e) => {
                        log::warn!("[cloudflare] Skipping record due to parse error: {e}");
                        None
                    }
                },
            )
            .collect();

        Ok(PaginatedResponse::new(
            records,
            params.page,
            params.page_size,
            total_count,
        ))
    }

    async fn create_record(&self, req: &CreateDnsRecordRequest) -> Result<DnsRecord> {
        let ctx = ErrorContext {
            record_name: Some(req.name.clone()),
            domain: Some(req.domain_id.clone()),
            ..Default::default()
        };

        // Get zone information (using cache)
        let domain_info = self.get_domain_cached(&req.domain_id).await?;
        let zone_name = domain_info.name;

        let full_name = relative_to_full_name(&req.name, &zone_name);
        let body = Self::build_create_body(&full_name, req.ttl, &req.data, req.proxied);

        let cf_record: CloudflareDnsRecord = self
            .post_json(&format!("/zones/{}/dns_records", req.domain_id), body, ctx)
            .await?;

        self.cf_record_to_dns_record(cf_record, &req.domain_id, &zone_name)
    }

    async fn update_record(
        &self,
        record_id: &str,
        req: &UpdateDnsRecordRequest,
    ) -> Result<DnsRecord> {
        let ctx = ErrorContext {
            record_name: Some(req.name.clone()),
            record_id: Some(record_id.to_string()),
            domain: Some(req.domain_id.clone()),
        };

        // Get zone information (using cache)
        let domain_info = self.get_domain_cached(&req.domain_id).await?;
        let zone_name = domain_info.name;

        let full_name = relative_to_full_name(&req.name, &zone_name);
        let body = Self::build_create_body(&full_name, req.ttl, &req.data, req.proxied);

        let cf_record: CloudflareDnsRecord = self
            .patch_json(
                &format!("/zones/{}/dns_records/{}", req.domain_id, record_id),
                body,
                ctx,
            )
            .await?;

        self.cf_record_to_dns_record(cf_record, &req.domain_id, &zone_name)
    }

    async fn delete_record(&self, record_id: &str, domain_id: &str) -> Result<()> {
        let ctx = ErrorContext {
            record_id: Some(record_id.to_string()),
            domain: Some(domain_id.to_string()),
            ..Default::default()
        };
        self.delete(&format!("/zones/{domain_id}/dns_records/{record_id}"), ctx)
            .await
    }
}