aptove-bridge 0.1.6

A secure bridge between ACP agents and clients
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
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
use anyhow::{Context, Result};
use reqwest::{Client, header};
use serde::{Deserialize, Serialize};
use tracing::{debug, info, warn};

const CLOUDFLARE_API_BASE: &str = "https://api.cloudflare.com/client/v4";

/// Cloudflare API client for Zero Trust operations
pub struct CloudflareClient {
    client: Client,
    #[allow(dead_code)]
    api_token: String,
    account_id: String,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct Tunnel {
    pub id: String,
    pub name: String,
    #[serde(default)]
    pub secret: String,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct AccessApplication {
    pub id: String,
    pub name: String,
    pub domain: String,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct ServiceToken {
    pub client_id: String,
    pub client_secret: String,
}

#[derive(Debug, Deserialize)]
struct CloudflareResponse {
    #[serde(default)]
    result: serde_json::Value,
    success: bool,
    #[serde(default)]
    errors: Vec<CloudflareError>,
    #[allow(dead_code)]
    #[serde(default)]
    messages: Vec<String>,
}

impl CloudflareResponse {
    fn into_result<T: serde::de::DeserializeOwned>(self) -> anyhow::Result<T> {
        serde_json::from_value(self.result).context("Failed to deserialize API result")
    }
}

#[derive(Debug, Deserialize)]
struct CloudflareError {
    code: i32,
    message: String,
}

impl CloudflareClient {
    /// Create a new Cloudflare API client
    pub fn new(api_token: String, account_id: String) -> Self {
        let mut headers = header::HeaderMap::new();
        headers.insert(
            header::AUTHORIZATION,
            header::HeaderValue::from_str(&format!("Bearer {}", api_token))
                .expect("Invalid API token format"),
        );
        headers.insert(
            header::CONTENT_TYPE,
            header::HeaderValue::from_static("application/json"),
        );

        let client = Client::builder()
            .default_headers(headers)
            .build()
            .expect("Failed to build HTTP client");

        Self {
            client,
            api_token,
            account_id,
        }
    }

    /// Create or retrieve existing tunnel
    pub async fn create_or_get_tunnel(&self, name: &str) -> Result<Tunnel> {
        // First, check if tunnel already exists
        let list_url = format!(
            "{}/accounts/{}/cfd_tunnel",
            CLOUDFLARE_API_BASE, self.account_id
        );

        let response: CloudflareResponse = self
            .client
            .get(&list_url)
            .send()
            .await
            .context("Failed to list tunnels")?
            .json()
            .await
            .context("Failed to parse tunnel list response")?;

        if response.success {
            let tunnels: Vec<Tunnel> = response.into_result().unwrap_or_default();
            if let Some(mut existing) = tunnels.into_iter().find(|t| t.name == name) {
                debug!("Found existing tunnel: {}", existing.id);
                // Recover the secret from the local credentials file (the API never returns it)
                if let Ok(dir) = get_cloudflared_dir() {
                    let creds_path = dir.join(format!("{}.json", existing.id));
                    if let Ok(content) = std::fs::read_to_string(&creds_path) {
                        if let Ok(json) = serde_json::from_str::<serde_json::Value>(&content) {
                            if let Some(s) = json.get("TunnelSecret").and_then(|v| v.as_str()) {
                                if !s.is_empty() {
                                    existing.secret = s.to_string();
                                    return Ok(existing);
                                }
                            }
                        }
                    }
                }
                // Secret is lost — delete this tunnel and fall through to create a new one
                warn!("Tunnel secret is lost for '{}'. Deleting and recreating...", existing.id);
                let _ = self.delete_tunnel(&existing.id).await;
            }
        }

        // Create new tunnel
        debug!("Creating new tunnel: {}", name);
        let create_url = format!(
            "{}/accounts/{}/cfd_tunnel",
            CLOUDFLARE_API_BASE, self.account_id
        );

        let tunnel_secret = self.generate_tunnel_secret();
        let payload = serde_json::json!({
            "name": name,
            "tunnel_secret": tunnel_secret,
        });

        let response: CloudflareResponse = self
            .client
            .post(&create_url)
            .json(&payload)
            .send()
            .await
            .context("Failed to create tunnel")?
            .json()
            .await
            .context("Failed to parse tunnel creation response")?;

        if !response.success {
            anyhow::bail!("Failed to create tunnel: {:?}", response.errors);
        }

        let mut tunnel: Tunnel = response.into_result().context("No tunnel returned after creation")?;
        tunnel.secret = tunnel_secret;
        Ok(tunnel)
    }

    /// Create DNS CNAME record for tunnel
    pub async fn create_dns_record(
        &self,
        zone_name: &str,
        subdomain: &str,
        tunnel_id: &str,
    ) -> Result<()> {
        // Get zone ID from zone name
        let zones_url = format!("{}/zones?name={}", CLOUDFLARE_API_BASE, zone_name);
        
        let zones_response: CloudflareResponse = self
            .client
            .get(&zones_url)
            .send()
            .await
            .context("Failed to fetch zone information")?
            .json()
            .await
            .context("Failed to parse zones response")?;

        #[derive(Deserialize)]
        struct Zone {
            id: String,
        }

        let zones: Vec<Zone> = zones_response.into_result().context("Zone not found")?;
        let zone_id = zones.into_iter().next().context("Zone not found")?.id;

        // Create DNS record
        let dns_url = format!("{}/zones/{}/dns_records", CLOUDFLARE_API_BASE, zone_id);
        let tunnel_cname = format!("{}.cfargotunnel.com", tunnel_id);
        
        let payload = serde_json::json!({
            "type": "CNAME",
            "name": subdomain,
            "content": tunnel_cname,
            "ttl": 1,
            "proxied": true,
        });

        let response: CloudflareResponse = self
            .client
            .post(&dns_url)
            .json(&payload)
            .send()
            .await
            .context("Failed to create DNS record")?
            .json()
            .await
            .context("Failed to parse DNS creation response")?;

        if !response.success {
            // Error 81053/81057: record with that name already exists — update it instead
            if response.errors.iter().any(|e| e.code == 81053 || e.code == 81057) {
                warn!("DNS record already exists, updating to point to current tunnel...");
                let full_hostname = format!("{}.{}", subdomain, zone_name);
                return self.update_dns_record(&zone_id, &full_hostname, &tunnel_cname).await;
            }
            anyhow::bail!("Failed to create DNS record: {:?}", response.errors);
        }

        Ok(())
    }

    /// Find and update an existing DNS CNAME record by name.
    async fn update_dns_record(&self, zone_id: &str, subdomain: &str, content: &str) -> Result<()> {
        #[derive(Deserialize)]
        struct DnsRecord {
            id: String,
        }

        let list_url = format!(
            "{}/zones/{}/dns_records?name={}&type=CNAME",
            CLOUDFLARE_API_BASE, zone_id, subdomain
        );

        let list_response: CloudflareResponse = self
            .client
            .get(&list_url)
            .send()
            .await
            .context("Failed to list DNS records")?
            .json()
            .await
            .context("Failed to parse DNS records list")?;

        let records: Vec<DnsRecord> = list_response.into_result().context("Failed to parse DNS record list")?;
        let record_id = records.into_iter().next()
            .context("DNS record not found for update")?
            .id;

        let update_url = format!("{}/zones/{}/dns_records/{}", CLOUDFLARE_API_BASE, zone_id, record_id);
        let payload = serde_json::json!({
            "type": "CNAME",
            "name": subdomain,
            "content": content,
            "ttl": 1,
            "proxied": true,
        });

        let response: CloudflareResponse = self
            .client
            .put(&update_url)
            .json(&payload)
            .send()
            .await
            .context("Failed to update DNS record")?
            .json()
            .await
            .context("Failed to parse DNS update response")?;

        if !response.success {
            anyhow::bail!("Failed to update DNS record: {:?}", response.errors);
        }

        info!("✅ DNS record updated to point to current tunnel");
        Ok(())
    }

    /// Create Zero Trust Access Application
    pub async fn create_access_application(&self, hostname: &str) -> Result<AccessApplication> {
        let url = format!(
            "{}/accounts/{}/access/apps",
            CLOUDFLARE_API_BASE, self.account_id
        );

        let payload = serde_json::json!({
            "name": format!("ACP Bridge - {}", hostname),
            "domain": hostname,
            "type": "self_hosted",
            "session_duration": "24h",
            "allowed_idps": [],
            "auto_redirect_to_identity": false,
        });

        let response: CloudflareResponse = self
            .client
            .post(&url)
            .json(&payload)
            .send()
            .await
            .context("Failed to create Access Application")?
            .json()
            .await
            .context("Failed to parse Access Application response")?;

        if !response.success || response.result.is_null() {
            warn!("Access Application creation failed, checking for existing app...");
            let app = self.find_access_application(hostname).await?;
            // Policy may already exist; ignore errors from duplicate policy creation
            let _ = self.create_service_auth_policy(&app.id, hostname).await;
            return Ok(app);
        }

        let app: AccessApplication = response.into_result().context("Failed to parse Access Application")?;
        // Create Service Auth policy
        self.create_service_auth_policy(&app.id, hostname).await?;
        Ok(app)
    }

    /// Find an existing Access Application by hostname.
    async fn find_access_application(&self, hostname: &str) -> Result<AccessApplication> {
        let url = format!(
            "{}/accounts/{}/access/apps",
            CLOUDFLARE_API_BASE, self.account_id
        );

        let response: CloudflareResponse = self
            .client
            .get(&url)
            .send()
            .await
            .context("Failed to list Access Applications")?
            .json()
            .await
            .context("Failed to parse Access Applications list")?;

        let apps: Vec<AccessApplication> = response.into_result().unwrap_or_default();
        apps.into_iter()
            .find(|app| app.domain == hostname)
            .with_context(|| format!("No Access Application found for hostname: {}", hostname))
    }

    /// Create Service Auth policy for the application
    async fn create_service_auth_policy(&self, app_id: &str, hostname: &str) -> Result<()> {
        let url = format!(
            "{}/accounts/{}/access/apps/{}/policies",
            CLOUDFLARE_API_BASE, self.account_id, app_id
        );

        let payload = serde_json::json!({
            "name": format!("Service Auth - {}", hostname),
            "decision": "non_identity",
            "include": [{
                "any_valid_service_token": {}
            }],
            "precedence": 1,
        });

        let response: CloudflareResponse = self
            .client
            .post(&url)
            .json(&payload)
            .send()
            .await
            .context("Failed to create Service Auth policy")?
            .json()
            .await
            .context("Failed to parse policy response")?;

        if !response.success {
            // Ignore "already exists" type errors — policy from a previous run is fine
            let already_exists = response.errors.iter().any(|e| {
                e.message.contains("already exists") || e.message.contains("duplicate")
            });
            if already_exists {
                warn!("Service Auth policy already exists, skipping...");
                return Ok(());
            }
            anyhow::bail!("Failed to create Service Auth policy: {:?}", response.errors);
        }

        Ok(())
    }

    /// Generate a Service Token for mobile authentication
    pub async fn create_service_token(&self, name: &str) -> Result<ServiceToken> {
        let url = format!(
            "{}/accounts/{}/access/service_tokens",
            CLOUDFLARE_API_BASE, self.account_id
        );
        let token_name = format!("Mobile Client - {}", name);

        let payload = serde_json::json!({
            "name": token_name,
            "duration": "8760h", // 1 year
        });

        let response: CloudflareResponse = self
            .client
            .post(&url)
            .json(&payload)
            .send()
            .await
            .context("Failed to create Service Token")?
            .json()
            .await
            .context("Failed to parse Service Token response")?;

        if !response.success || response.result.is_null() {
            // Auth errors can't be resolved by deleting and retrying — surface immediately.
            if response.errors.iter().any(|e| e.code == 10000) {
                anyhow::bail!(
                    "Cloudflare authentication error creating Service Token (code 10000). \
                     Ensure your API token has 'Access: Service Tokens: Edit' permission. \
                     Errors: {:?}",
                    response.errors
                );
            }

            warn!(
                "Service Token creation failed ({}), deleting existing token and retrying...",
                response.errors.iter().map(|e| format!("{}: {}", e.code, e.message)).collect::<Vec<_>>().join(", ")
            );
            self.delete_service_token_by_name(&token_name).await?;

            let retry: CloudflareResponse = self
                .client
                .post(&url)
                .json(&payload)
                .send()
                .await
                .context("Failed to create Service Token (retry)")?
                .json()
                .await
                .context("Failed to parse Service Token response (retry)")?;

            if !retry.success {
                anyhow::bail!("Failed to create Service Token: {:?}", retry.errors);
            }
            return retry.into_result().context("No Service Token returned after retry");
        }

        response.into_result().context("No Service Token returned")
    }

    /// List service tokens and delete the one matching `name`.
    async fn delete_service_token_by_name(&self, name: &str) -> Result<()> {
        #[derive(Deserialize)]
        struct TokenInfo {
            id: String,
            name: String,
        }

        let list_url = format!(
            "{}/accounts/{}/access/service_tokens",
            CLOUDFLARE_API_BASE, self.account_id
        );

        let list: CloudflareResponse = self
            .client
            .get(&list_url)
            .send()
            .await
            .context("Failed to list Service Tokens")?
            .json()
            .await
            .context("Failed to parse Service Token list")?;

        let tokens: Vec<TokenInfo> = list.into_result().unwrap_or_default();
        for token in tokens {
            if token.name == name {
                let delete_url = format!(
                    "{}/accounts/{}/access/service_tokens/{}",
                    CLOUDFLARE_API_BASE, self.account_id, token.id
                );
                self.client
                    .delete(&delete_url)
                    .send()
                    .await
                    .context("Failed to delete existing Service Token")?;
                info!("🗑️  Deleted existing Service Token '{}'", name);
                return Ok(());
            }
        }
        Ok(()) // not found — that's fine, proceed to create
    }

    /// Configure tunnel ingress rules
    pub async fn configure_tunnel_ingress(
        &self,
        tunnel_id: &str,
        hostname: &str,
        local_port: u16,
    ) -> Result<()> {
        let url = format!(
            "{}/accounts/{}/cfd_tunnel/{}/configurations",
            CLOUDFLARE_API_BASE, self.account_id, tunnel_id
        );

        let payload = serde_json::json!({
            "config": {
                "ingress": [
                    {
                        "hostname": hostname,
                        "service": format!("http://localhost:{}", local_port),
                    },
                    {
                        "service": "http_status:404",
                    }
                ],
            }
        });

        let response: CloudflareResponse = self
            .client
            .put(&url)
            .json(&payload)
            .send()
            .await
            .context("Failed to configure tunnel ingress")?
            .json()
            .await
            .context("Failed to parse ingress configuration response")?;

        if !response.success {
            anyhow::bail!("Failed to configure tunnel ingress: {:?}", response.errors);
        }

        Ok(())
    }

    /// Delete a tunnel by ID
    async fn delete_tunnel(&self, tunnel_id: &str) -> Result<()> {
        let url = format!(
            "{}/accounts/{}/cfd_tunnel/{}",
            CLOUDFLARE_API_BASE, self.account_id, tunnel_id
        );
        let response: CloudflareResponse = self
            .client
            .delete(&url)
            .send()
            .await
            .context("Failed to delete tunnel")?
            .json()
            .await
            .context("Failed to parse tunnel delete response")?;
        if !response.success {
            anyhow::bail!("Failed to delete tunnel: {:?}", response.errors);
        }
        Ok(())
    }

    /// Generate a secure tunnel secret
    fn generate_tunnel_secret(&self) -> String {
        use base64::{engine::general_purpose, Engine as _};
        let random_bytes: Vec<u8> = (0..32).map(|_| rand::random::<u8>()).collect();
        general_purpose::STANDARD.encode(random_bytes)
    }

    /// Get the account ID for this client
    #[allow(dead_code)]
    pub fn account_id(&self) -> &str {
        &self.account_id
    }
}

/// Write the cloudflared tunnel credentials JSON file to ~/.cloudflared/<tunnel-id>.json.
/// This file is required by `cloudflared tunnel run` to authenticate to Cloudflare.
pub fn write_credentials_file(
    account_id: &str,
    tunnel_id: &str,
    tunnel_secret: &str,
) -> Result<std::path::PathBuf> {
    let cloudflared_dir = get_cloudflared_dir()?;
    std::fs::create_dir_all(&cloudflared_dir)
        .context("Failed to create ~/.cloudflared directory")?;

    let credentials_path = cloudflared_dir.join(format!("{}.json", tunnel_id));
    let credentials = serde_json::json!({
        "AccountTag": account_id,
        "TunnelSecret": tunnel_secret,
        "TunnelID": tunnel_id,
    });
    std::fs::write(&credentials_path, serde_json::to_string_pretty(&credentials)?)
        .context("Failed to write tunnel credentials file")?;

    #[cfg(unix)]
    {
        use std::os::unix::fs::PermissionsExt;
        let perms = std::fs::Permissions::from_mode(0o600);
        std::fs::set_permissions(&credentials_path, perms)?;
    }

    Ok(credentials_path)
}

/// Write the cloudflared config.yml to ~/.cloudflared/config.yml.
/// This configures which tunnel to run and the ingress rules.
pub fn write_cloudflared_config(
    tunnel_id: &str,
    credentials_path: &std::path::Path,
    hostname: &str,
    local_port: u16,
) -> Result<std::path::PathBuf> {
    let cloudflared_dir = get_cloudflared_dir()?;
    std::fs::create_dir_all(&cloudflared_dir)
        .context("Failed to create ~/.cloudflared directory")?;

    let config_path = cloudflared_dir.join("config.yml");
    let credentials_str = credentials_path.to_string_lossy();
    let config_content = format!(
        "tunnel: {tunnel_id}\ncredentials-file: {credentials_str}\n\ningress:\n  - hostname: {hostname}\n    service: http://localhost:{local_port}\n  - service: http_status:404\n"
    );
    std::fs::write(&config_path, &config_content)
        .context("Failed to write cloudflared config.yml")?;

    Ok(config_path)
}

/// Return the path to the cloudflared config YAML (does not check existence).
pub fn cloudflared_config_path() -> Result<std::path::PathBuf> {
    Ok(get_cloudflared_dir()?.join("config.yml"))
}

/// Return the path to the cloudflared credentials file for a given tunnel ID.
#[allow(dead_code)]
pub fn cloudflared_credentials_path(tunnel_id: &str) -> Result<std::path::PathBuf> {
    Ok(get_cloudflared_dir()?.join(format!("{}.json", tunnel_id)))
}

fn get_cloudflared_dir() -> Result<std::path::PathBuf> {
    let home = std::env::var("HOME")
        .or_else(|_| std::env::var("USERPROFILE"))
        .context("Cannot determine home directory (HOME not set)")?;
    Ok(std::path::PathBuf::from(home).join(".cloudflared"))
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::fs;
    use tempfile::TempDir;

    fn fake_cloudflared_dir(tmp: &TempDir) -> std::path::PathBuf {
        let dir = tmp.path().join(".cloudflared");
        fs::create_dir_all(&dir).unwrap();
        dir
    }

    #[test]
    fn credentials_file_has_required_fields() {
        let tmp = TempDir::new().unwrap();
        // Override HOME so write_credentials_file uses tmp dir
        std::env::set_var("HOME", tmp.path().to_str().unwrap());

        let path = write_credentials_file("acct123", "tunnel-abc", "secret-base64==").unwrap();
        assert!(path.exists(), "credentials file should be created");

        let content = fs::read_to_string(&path).unwrap();
        let json: serde_json::Value = serde_json::from_str(&content).unwrap();
        assert_eq!(json["AccountTag"], "acct123");
        assert_eq!(json["TunnelSecret"], "secret-base64==");
        assert_eq!(json["TunnelID"], "tunnel-abc");
    }

    #[test]
    fn config_yml_has_correct_sections() {
        let tmp = TempDir::new().unwrap();
        std::env::set_var("HOME", tmp.path().to_str().unwrap());

        let creds_path = fake_cloudflared_dir(&tmp).join("tunnel-abc.json");
        fs::write(&creds_path, "{}").unwrap();

        let config_path = write_cloudflared_config(
            "tunnel-abc",
            &creds_path,
            "agent.example.com",
            8080,
        )
        .unwrap();

        let content = fs::read_to_string(&config_path).unwrap();
        assert!(content.contains("tunnel: tunnel-abc"), "should have tunnel ID");
        assert!(content.contains("credentials-file:"), "should have credentials-file");
        assert!(content.contains("hostname: agent.example.com"), "should have hostname");
        assert!(content.contains("http://localhost:8080"), "should have local port");
        assert!(content.contains("http_status:404"), "should have fallback rule");
    }
}