blueprint-manager-bridge 0.2.0-alpha.12

Bridge for Blueprint manager to service communication
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
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
use crate::VSOCK_PORT;
use crate::blueprint_manager_bridge_server::{
    BlueprintManagerBridge, BlueprintManagerBridgeServer,
};
use crate::{
    AddOwnerToServiceRequest, Error, PortRequest, PortResponse,
    RegisterBlueprintServiceProxyRequest, RemoveOwnerFromServiceRequest,
    UnregisterBlueprintServiceProxyRequest, UpdateBlueprintServiceTlsProfileRequest,
};
use blueprint_auth::{
    db::RocksDb,
    models::{ServiceModel, ServiceOwnerModel},
    types::ServiceId,
};
use blueprint_core::{error, info, warn};
use std::path::PathBuf;
use std::sync::Arc;
use tokio::net::UnixListener;
use tokio::sync::{Mutex, oneshot};
use tokio::task::JoinHandle;
use tonic::codegen::tokio_stream::wrappers::UnixListenerStream;
use tonic::{Request, Response, transport::Server};

/// Handle to a running bridge
///
/// Dropping this handle will shut down the bridge and clean up any registered service.
pub struct BridgeHandle {
    sock_path: PathBuf,
    handle: JoinHandle<Result<(), Error>>,
    db: RocksDb,
    registered_service_id: Arc<std::sync::Mutex<Option<u64>>>,
}

impl BridgeHandle {
    pub fn shutdown(self) {}
}

impl Drop for BridgeHandle {
    fn drop(&mut self) {
        // Clean up any registered service from the database
        if let Some(service_id) = self
            .registered_service_id
            .lock()
            .unwrap_or_else(|e| e.into_inner())
            .take()
        {
            let sid = ServiceId(service_id, 0);
            if let Err(e) = ServiceModel::delete(sid, &self.db) {
                warn!("Failed to clean up service {sid} on bridge drop: {e}");
            } else {
                info!("Cleaned up service {sid} registration on bridge shutdown");
            }
        }
        let _ = std::fs::remove_file(&self.sock_path);
        self.handle.abort();
    }
}

/// Manager <-> Service bridge
pub struct Bridge {
    runtime_dir: PathBuf,
    service_name: String,
    db: RocksDb,
    no_vm: bool,
    expected_service_id: Option<u64>,
}

impl Bridge {
    #[must_use]
    pub fn new(
        runtime_dir: PathBuf,
        service_name: String,
        db: RocksDb,
        no_vm: bool,
        expected_service_id: Option<u64>,
    ) -> Self {
        Self {
            runtime_dir,
            service_name,
            db,
            no_vm,
            expected_service_id,
        }
    }

    /// The *base* bridge socket
    ///
    /// For native services, this is the only socket that exists.
    ///
    /// For sandboxed services, this is the base path. When a guest connects to the bridge, it will
    /// be through the socket at `<base>_VSOCK_PORT`. See [`Self::guest_socket_path()`].
    #[must_use]
    pub fn base_socket_path(&self) -> PathBuf {
        let sock_name = format!("{}.sock", self.service_name);
        self.runtime_dir.join(sock_name)
    }

    /// The socket path on the *host* for guest connections
    ///
    /// This socket is only created for sandboxed services. It will **not** exist for native services.
    #[must_use]
    pub fn guest_socket_path(&self) -> PathBuf {
        let sock_name = format!("{}.sock_{VSOCK_PORT}", self.service_name);
        self.runtime_dir.join(sock_name)
    }

    /// Spawn the bridge instance
    ///
    /// # Errors
    ///
    /// * Unable to bind to the socket, possibly an issue with the [`HypervisorInstance`] startup.
    ///
    /// [`HypervisorInstance`]: https://docs.rs/blueprint-manager/latest/blueprint_manager/rt/struct.HypervisorInstance.html
    pub fn spawn(self) -> Result<(BridgeHandle, oneshot::Receiver<()>), Error> {
        let sock_path = if self.no_vm {
            self.base_socket_path()
        } else {
            self.guest_socket_path()
        };

        let _ = std::fs::remove_file(&sock_path);
        let listener = UnixListener::bind(&sock_path).map_err(|e| {
            error!(
                "Failed to bind bridge socket at {}: {e}",
                sock_path.display()
            );
            e
        })?;

        info!(
            "Bridge for service `{}` listening on {}",
            self.service_name,
            sock_path.display()
        );

        let (tx, rx) = oneshot::channel();
        let registered_service_id = Arc::new(std::sync::Mutex::new(None));
        let registered_service_id_clone = Arc::clone(&registered_service_id);
        let db_clone = self.db.clone();

        let handle = tokio::task::spawn(async move {
            Server::builder()
                .add_service(BlueprintManagerBridgeServer::new(
                    BridgeService::with_service_pinning(
                        tx,
                        self.db,
                        self.expected_service_id,
                        registered_service_id_clone,
                    ),
                ))
                .serve_with_incoming(UnixListenerStream::new(listener))
                .await
                .map_err(Error::from)
        });

        Ok((
            BridgeHandle {
                sock_path,
                handle,
                db: db_clone,
                registered_service_id,
            },
            rx,
        ))
    }
}

struct BridgeService {
    ready_tx: Arc<Mutex<Option<oneshot::Sender<()>>>>,
    db: RocksDb,
    expected_service_id: Option<u64>,
    registered_service_id: Arc<std::sync::Mutex<Option<u64>>>,
}

impl BridgeService {
    #[allow(dead_code)]
    fn new(tx: oneshot::Sender<()>, db: RocksDb) -> Self {
        Self {
            ready_tx: Arc::new(Mutex::new(Some(tx))),
            db,
            expected_service_id: None,
            registered_service_id: Arc::new(std::sync::Mutex::new(None)),
        }
    }

    fn with_service_pinning(
        tx: oneshot::Sender<()>,
        db: RocksDb,
        expected_service_id: Option<u64>,
        registered_service_id: Arc<std::sync::Mutex<Option<u64>>>,
    ) -> Self {
        Self {
            ready_tx: Arc::new(Mutex::new(Some(tx))),
            db,
            expected_service_id,
            registered_service_id,
        }
    }

    async fn signal_ready(&self) {
        if let Some(tx) = self.ready_tx.lock().await.take() {
            let _ = tx.send(());
        }
    }

    /// Verify that the given service_id is allowed for this bridge.
    ///
    /// Checks the BPM-provided expected_service_id constraint and the self-pinning constraint.
    fn verify_service_id(&self, service_id: u64) -> Result<(), tonic::Status> {
        // Check BPM-provided constraint
        if let Some(expected) = self.expected_service_id {
            if service_id != expected {
                return Err(tonic::Status::permission_denied(format!(
                    "Bridge is configured for service_id {expected}, got {service_id}"
                )));
            }
        }

        // Check self-pinning constraint
        let pinned = self
            .registered_service_id
            .lock()
            .unwrap_or_else(|e| e.into_inner());
        if let Some(pinned_id) = *pinned {
            if service_id != pinned_id {
                return Err(tonic::Status::permission_denied(format!(
                    "Bridge is pinned to service_id {pinned_id}, cannot operate on {service_id}"
                )));
            }
        }

        Ok(())
    }

    /// Verify that this bridge has previously registered a service and the given service_id matches.
    fn verify_registered_service_id(&self, service_id: u64) -> Result<(), tonic::Status> {
        self.verify_service_id(service_id)?;

        let pinned = self
            .registered_service_id
            .lock()
            .unwrap_or_else(|e| e.into_inner());
        if pinned.is_none() {
            return Err(tonic::Status::failed_precondition(
                "No service registered on this bridge",
            ));
        }

        Ok(())
    }
}

fn validate_tls_profile(profile: &blueprint_auth::models::TlsProfile) -> Result<(), tonic::Status> {
    if !profile.tls_enabled {
        return Ok(());
    }

    if profile.encrypted_server_cert.is_empty() || profile.encrypted_server_key.is_empty() {
        return Err(tonic::Status::invalid_argument(
            "Server certificate and key are required when TLS is enabled",
        ));
    }

    if profile.require_client_mtls && profile.encrypted_client_ca_bundle.is_empty() {
        return Err(tonic::Status::invalid_argument(
            "Client CA bundle is required when mutual TLS authentication is enabled",
        ));
    }

    Ok(())
}

#[tonic::async_trait]
impl BlueprintManagerBridge for BridgeService {
    async fn ping(&self, _req: Request<()>) -> Result<Response<()>, tonic::Status> {
        self.signal_ready().await;
        Ok(Response::new(()))
    }

    #[allow(clippy::cast_possible_truncation)]
    async fn request_port(
        &self,
        req: Request<PortRequest>,
    ) -> Result<Response<PortResponse>, tonic::Status> {
        let PortRequest { preferred_port, .. } = req.into_inner();

        let port = allocate_host_port(preferred_port as u16).await?;

        Ok(Response::new(PortResponse {
            port: u32::from(port),
        }))
    }

    async fn register_blueprint_service_proxy(
        &self,
        req: Request<RegisterBlueprintServiceProxyRequest>,
    ) -> Result<Response<()>, tonic::Status> {
        let RegisterBlueprintServiceProxyRequest {
            service_id,
            api_key_prefix,
            upstream_url,
            owners,
            tls_profile,
        } = req.into_inner();

        // Verify this bridge is allowed to operate on this service_id
        self.verify_service_id(service_id)?;

        let db = &self.db;

        // Convert protobuf owners to ServiceOwnerModel
        let service_owners: Vec<ServiceOwnerModel> = owners
            .into_iter()
            .map(|owner| ServiceOwnerModel {
                key_type: owner.key_type,
                key_bytes: owner.key_bytes,
            })
            .collect();

        let tls_profile: Option<blueprint_auth::models::TlsProfile> = tls_profile.map(Into::into);
        if let Some(ref profile) = tls_profile {
            validate_tls_profile(profile)?;
        }

        let service = ServiceModel {
            api_key_prefix,
            owners: service_owners,
            upstream_url,
            tls_profile,
        };

        let service_id = ServiceId(service_id, 0);

        // First-time registration uses an atomic check-then-write to prevent
        // cross-bridge hijacking races. Updates (bridge already pinned) use plain save.
        let is_update = self
            .registered_service_id
            .lock()
            .unwrap_or_else(|e| e.into_inner())
            .is_some();

        if is_update {
            service.save(service_id, db).map_err(|e| {
                error!("Failed to save service to database: {e}");
                tonic::Status::internal(format!("Database error: {e}"))
            })?;
        } else {
            let saved = service.save_if_absent(service_id, db).map_err(|e| {
                error!("Failed to save service to database: {e}");
                tonic::Status::internal(format!("Database error: {e}"))
            })?;
            if !saved {
                return Err(tonic::Status::already_exists(format!(
                    "Service {service_id} is already registered by another bridge"
                )));
            }
        }

        // Pin this bridge to the registered service
        {
            let mut pinned = self
                .registered_service_id
                .lock()
                .unwrap_or_else(|e| e.into_inner());
            *pinned = Some(service_id.0);
        }

        info!("Registered service proxy with ID: {}", service_id);
        Ok(Response::new(()))
    }

    async fn unregister_blueprint_service_proxy(
        &self,
        req: Request<UnregisterBlueprintServiceProxyRequest>,
    ) -> Result<Response<()>, tonic::Status> {
        let UnregisterBlueprintServiceProxyRequest { service_id } = req.into_inner();

        // Verify this bridge owns this service
        self.verify_registered_service_id(service_id)?;

        let db = &self.db;

        let service_id = ServiceId(service_id, 0);

        // Delete from database
        ServiceModel::delete(service_id, db).map_err(|e| {
            error!("Failed to delete service {} from database: {e}", service_id);
            tonic::Status::internal(format!("Database error: {e}"))
        })?;

        // Clear the pin
        {
            let mut pinned = self
                .registered_service_id
                .lock()
                .unwrap_or_else(|e| e.into_inner());
            *pinned = None;
        }

        info!("Unregistered service proxy with ID: {}", service_id);
        Ok(Response::new(()))
    }

    async fn add_owner_to_service(
        &self,
        req: Request<AddOwnerToServiceRequest>,
    ) -> Result<Response<()>, tonic::Status> {
        let AddOwnerToServiceRequest {
            service_id,
            owner_to_add,
        } = req.into_inner();

        // Verify this bridge owns this service
        self.verify_registered_service_id(service_id)?;

        let db = &self.db;

        let service_id = ServiceId(service_id, 0);
        let new_owner_proto = owner_to_add.ok_or_else(|| {
            error!(
                "Owner is missing in AddOwnerToServiceRequest for service ID: {}",
                service_id
            );
            tonic::Status::invalid_argument("owner_to_add is required")
        })?;

        // Load existing service
        let mut service = ServiceModel::find_by_id(service_id, db)
            .map_err(|e| {
                error!("Failed to load service {} from database: {e}", service_id);
                tonic::Status::internal(format!("Database error: {e}"))
            })?
            .ok_or_else(|| {
                error!("Service {} not found for add_owner_to_service", service_id);
                tonic::Status::not_found(format!("Service {} not found", service_id))
            })?;

        // Convert protobuf owner to ServiceOwnerModel
        let new_owner = ServiceOwnerModel {
            key_type: new_owner_proto.key_type,
            key_bytes: new_owner_proto.key_bytes,
        };

        // Add owner if not already present
        if service.owners.contains(&new_owner) {
            info!("Owner already exists for service ID: {service_id}");
        } else {
            service.owners.push(new_owner);
            // Save updated service
            service.save(service_id, db).map_err(|e| {
                error!("Failed to save updated service {service_id} to database: {e}",);
                tonic::Status::internal(format!("Database error: {e}"))
            })?;
            info!("Added owner to service ID: {service_id}");
        }

        Ok(Response::new(()))
    }

    async fn remove_owner_from_service(
        &self,
        req: Request<RemoveOwnerFromServiceRequest>,
    ) -> Result<Response<()>, tonic::Status> {
        let RemoveOwnerFromServiceRequest {
            service_id,
            owner_to_remove,
        } = req.into_inner();

        // Verify this bridge owns this service
        self.verify_registered_service_id(service_id)?;

        let db = &self.db;

        let service_id = ServiceId(service_id, 0);
        let owner_to_remove_proto = owner_to_remove.ok_or_else(|| {
            error!(
                "Owner is missing in RemoveOwnerFromServiceRequest for service ID: {}",
                service_id
            );
            tonic::Status::invalid_argument("owner_to_remove is required")
        })?;

        // Load existing service
        let mut service = ServiceModel::find_by_id(service_id, db)
            .map_err(|e| {
                error!("Failed to load service {} from database: {e}", service_id);
                tonic::Status::internal(format!("Database error: {e}"))
            })?
            .ok_or_else(|| {
                error!(
                    "Service {} not found for remove_owner_from_service",
                    service_id
                );
                tonic::Status::not_found(format!("Service {} not found", service_id))
            })?;

        // Convert protobuf owner to ServiceOwnerModel
        let owner_to_remove = ServiceOwnerModel {
            key_type: owner_to_remove_proto.key_type,
            key_bytes: owner_to_remove_proto.key_bytes,
        };

        // Remove owner
        let initial_len = service.owners.len();
        service.owners.retain(|o| o != &owner_to_remove);

        if service.owners.len() < initial_len {
            // Save updated service
            service.save(service_id, db).map_err(|e| {
                error!(
                    "Failed to save updated service {} to database: {e}",
                    service_id
                );
                tonic::Status::internal(format!("Database error: {e}"))
            })?;
            info!("Removed owner from service ID: {}", service_id);
        } else {
            info!(
                "Owner not found for service ID: {}, nothing to remove",
                service_id
            );
        }

        Ok(Response::new(()))
    }

    async fn update_blueprint_service_tls_profile(
        &self,
        req: Request<UpdateBlueprintServiceTlsProfileRequest>,
    ) -> Result<Response<()>, tonic::Status> {
        let UpdateBlueprintServiceTlsProfileRequest {
            service_id,
            tls_profile,
        } = req.into_inner();

        // Verify this bridge owns this service
        self.verify_registered_service_id(service_id)?;

        let db = &self.db;
        let service_id = ServiceId(service_id, 0);

        // Load existing service
        let mut service = ServiceModel::find_by_id(service_id, db)
            .map_err(|e| {
                error!("Failed to load service {} from database: {e}", service_id);
                tonic::Status::internal(format!("Database error: {e}"))
            })?
            .ok_or_else(|| {
                error!(
                    "Service {} not found for update_blueprint_service_tls_profile",
                    service_id
                );
                tonic::Status::not_found(format!("Service {} not found", service_id))
            })?;

        let new_tls_profile: Option<blueprint_auth::models::TlsProfile> =
            tls_profile.map(Into::into);
        if let Some(ref profile) = new_tls_profile {
            validate_tls_profile(profile)?;
        }

        service.tls_profile = new_tls_profile;

        // Save updated service
        service.save(service_id, db).map_err(|e| {
            error!(
                "Failed to save updated service {} TLS profile to database: {e}",
                service_id
            );
            tonic::Status::internal(format!("Database error: {e}"))
        })?;

        info!("Updated TLS profile for service ID: {}", service_id);
        Ok(Response::new(()))
    }
}

// TODO: Actually allocate a port to the VM
#[expect(clippy::unused_async, reason = "This isn't actually setup yet")]
async fn allocate_host_port(hint: u16) -> Result<u16, tonic::Status> {
    // Try the caller's preferred port first, but fall back to an OS-assigned free
    // port when it is already bound. Co-located services on the same host commonly
    // hint the same base port (e.g. several ai-agent-sandbox operators all default
    // to 9090); without this fallback the 2nd+ service fails port allocation with
    // "Address already in use" and never boots. Binding `:0` lets the OS pick a
    // distinct free port so every co-located service gets a bindable port.
    let listener = std::net::TcpListener::bind(format!("0.0.0.0:{hint}"))
        .or_else(|_| std::net::TcpListener::bind("0.0.0.0:0"))
        .map_err(|e| tonic::Status::unavailable(e.to_string()))?;
    let port = listener
        .local_addr()
        .expect("Should have a local address")
        .port();
    drop(listener);
    Ok(port)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::TlsProfileConfig;
    use blueprint_auth::db::{RocksDb, RocksDbConfig};
    use tempfile::tempdir;

    #[tokio::test]
    async fn test_tls_profile_validation_server_tls_required() {
        let tmp_dir = tempdir().unwrap();
        let db = RocksDb::open(tmp_dir.path(), &RocksDbConfig::default()).unwrap();
        let service = BridgeService::new(tokio::sync::oneshot::channel().0, db);

        // Test valid server TLS configuration
        let valid_request = tonic::Request::new(RegisterBlueprintServiceProxyRequest {
            service_id: 1,
            api_key_prefix: "test".to_string(),
            upstream_url: "http://localhost:8080".to_string(),
            owners: vec![],
            tls_profile: Some(TlsProfileConfig {
                tls_enabled: true,
                require_client_mtls: false,
                encrypted_server_cert: b"cert".to_vec(),
                encrypted_server_key: b"key".to_vec(),
                encrypted_client_ca_bundle: b"ca".to_vec(),
                encrypted_upstream_ca_bundle: Vec::new(),
                encrypted_upstream_client_cert: Vec::new(),
                encrypted_upstream_client_key: Vec::new(),
                client_cert_ttl_hours: 24,
                sni: None,
                subject_alt_name_template: None,
                allowed_dns_names: Vec::new(),
            }),
        });

        let result = service
            .register_blueprint_service_proxy(valid_request)
            .await;
        assert!(result.is_ok());

        // Test that client CA bundle is optional when client mTLS is disabled
        let optional_ca_request = tonic::Request::new(RegisterBlueprintServiceProxyRequest {
            service_id: 1,
            api_key_prefix: "test".to_string(),
            upstream_url: "http://localhost:8080".to_string(),
            owners: vec![],
            tls_profile: Some(TlsProfileConfig {
                tls_enabled: true,
                require_client_mtls: false,
                encrypted_server_cert: b"cert".to_vec(),
                encrypted_server_key: b"key".to_vec(),
                encrypted_client_ca_bundle: Vec::new(),
                encrypted_upstream_ca_bundle: Vec::new(),
                encrypted_upstream_client_cert: Vec::new(),
                encrypted_upstream_client_key: Vec::new(),
                client_cert_ttl_hours: 24,
                sni: None,
                subject_alt_name_template: None,
                allowed_dns_names: Vec::new(),
            }),
        });

        let result = service
            .register_blueprint_service_proxy(optional_ca_request)
            .await;
        assert!(result.is_ok());

        // Test invalid server TLS configuration (missing cert)
        let invalid_request = tonic::Request::new(RegisterBlueprintServiceProxyRequest {
            service_id: 1,
            api_key_prefix: "test".to_string(),
            upstream_url: "http://localhost:8080".to_string(),
            owners: vec![],
            tls_profile: Some(TlsProfileConfig {
                tls_enabled: true,
                require_client_mtls: false,
                encrypted_server_cert: Vec::new(), // Missing
                encrypted_server_key: b"key".to_vec(),
                encrypted_client_ca_bundle: b"ca".to_vec(),
                encrypted_upstream_ca_bundle: Vec::new(),
                encrypted_upstream_client_cert: Vec::new(),
                encrypted_upstream_client_key: Vec::new(),
                client_cert_ttl_hours: 24,
                sni: None,
                subject_alt_name_template: None,
                allowed_dns_names: Vec::new(),
            }),
        });

        let result = service
            .register_blueprint_service_proxy(invalid_request)
            .await;
        assert!(result.is_err());
        assert_eq!(
            result.as_ref().unwrap_err().code(),
            tonic::Code::InvalidArgument
        );
        assert_eq!(
            result.as_ref().unwrap_err().message(),
            "Server certificate and key are required when TLS is enabled"
        );

        // Test invalid server TLS configuration (missing key)
        let invalid_request = tonic::Request::new(RegisterBlueprintServiceProxyRequest {
            service_id: 1,
            api_key_prefix: "test".to_string(),
            upstream_url: "http://localhost:8080".to_string(),
            owners: vec![],
            tls_profile: Some(TlsProfileConfig {
                tls_enabled: true,
                require_client_mtls: false,
                encrypted_server_cert: b"cert".to_vec(),
                encrypted_server_key: Vec::new(), // Missing
                encrypted_client_ca_bundle: b"ca".to_vec(),
                encrypted_upstream_ca_bundle: Vec::new(),
                encrypted_upstream_client_cert: Vec::new(),
                encrypted_upstream_client_key: Vec::new(),
                client_cert_ttl_hours: 24,
                sni: None,
                subject_alt_name_template: None,
                allowed_dns_names: Vec::new(),
            }),
        });

        let result = service
            .register_blueprint_service_proxy(invalid_request)
            .await;
        assert!(result.is_err());
        assert_eq!(
            result.as_ref().unwrap_err().code(),
            tonic::Code::InvalidArgument
        );
        assert_eq!(
            result.as_ref().unwrap_err().message(),
            "Server certificate and key are required when TLS is enabled"
        );
    }

    #[tokio::test]
    async fn test_tls_profile_validation_client_mtls_required() {
        let tmp_dir = tempdir().unwrap();
        let db = RocksDb::open(tmp_dir.path(), &RocksDbConfig::default()).unwrap();
        let service = BridgeService::new(tokio::sync::oneshot::channel().0, db);

        // Test valid client mTLS configuration
        let valid_request = tonic::Request::new(RegisterBlueprintServiceProxyRequest {
            service_id: 1,
            api_key_prefix: "test".to_string(),
            upstream_url: "http://localhost:8080".to_string(),
            owners: vec![],
            tls_profile: Some(TlsProfileConfig {
                tls_enabled: true,
                require_client_mtls: true,
                encrypted_server_cert: b"cert".to_vec(),
                encrypted_server_key: b"key".to_vec(),
                encrypted_client_ca_bundle: b"client_ca".to_vec(),
                encrypted_upstream_ca_bundle: Vec::new(),
                encrypted_upstream_client_cert: Vec::new(),
                encrypted_upstream_client_key: Vec::new(),
                client_cert_ttl_hours: 24,
                sni: None,
                subject_alt_name_template: None,
                allowed_dns_names: Vec::new(),
            }),
        });

        let result = service
            .register_blueprint_service_proxy(valid_request)
            .await;
        assert!(result.is_ok());

        // Test invalid client mTLS configuration (missing client CA)
        let invalid_request = tonic::Request::new(RegisterBlueprintServiceProxyRequest {
            service_id: 1,
            api_key_prefix: "test".to_string(),
            upstream_url: "http://localhost:8080".to_string(),
            owners: vec![],
            tls_profile: Some(TlsProfileConfig {
                tls_enabled: true,
                require_client_mtls: true,
                encrypted_server_cert: b"cert".to_vec(),
                encrypted_server_key: b"key".to_vec(),
                encrypted_client_ca_bundle: Vec::new(), // Missing
                encrypted_upstream_ca_bundle: Vec::new(),
                encrypted_upstream_client_cert: Vec::new(),
                encrypted_upstream_client_key: Vec::new(),
                client_cert_ttl_hours: 24,
                sni: None,
                subject_alt_name_template: None,
                allowed_dns_names: Vec::new(),
            }),
        });

        let result = service
            .register_blueprint_service_proxy(invalid_request)
            .await;
        assert!(result.is_err());
        assert_eq!(
            result.as_ref().unwrap_err().code(),
            tonic::Code::InvalidArgument
        );
        assert_eq!(
            result.as_ref().unwrap_err().message(),
            "Client CA bundle is required when mutual TLS authentication is enabled"
        );
    }

    #[tokio::test]
    async fn test_tls_profile_update_validation() {
        let tmp_dir = tempdir().unwrap();
        let db = RocksDb::open(tmp_dir.path(), &RocksDbConfig::default()).unwrap();
        let service = BridgeService::new(tokio::sync::oneshot::channel().0, db.clone());

        // First register a service without TLS
        let register_request = tonic::Request::new(RegisterBlueprintServiceProxyRequest {
            service_id: 1,
            api_key_prefix: "test".to_string(),
            upstream_url: "http://localhost:8080".to_string(),
            owners: vec![],
            tls_profile: None,
        });

        let result = service
            .register_blueprint_service_proxy(register_request)
            .await;
        assert!(result.is_ok());

        // Test updating to valid TLS configuration without client CA when mTLS is disabled
        let valid_update_request = tonic::Request::new(UpdateBlueprintServiceTlsProfileRequest {
            service_id: 1,
            tls_profile: Some(TlsProfileConfig {
                tls_enabled: true,
                require_client_mtls: false,
                encrypted_server_cert: b"cert".to_vec(),
                encrypted_server_key: b"key".to_vec(),
                encrypted_client_ca_bundle: Vec::new(),
                encrypted_upstream_ca_bundle: Vec::new(),
                encrypted_upstream_client_cert: Vec::new(),
                encrypted_upstream_client_key: Vec::new(),
                client_cert_ttl_hours: 24,
                sni: Some("example.com".to_string()),
                subject_alt_name_template: None,
                allowed_dns_names: Vec::new(),
            }),
        });

        let result = service
            .update_blueprint_service_tls_profile(valid_update_request)
            .await;
        assert!(result.is_ok());

        // Test updating to invalid TLS configuration
        let invalid_update_request = tonic::Request::new(UpdateBlueprintServiceTlsProfileRequest {
            service_id: 1,
            tls_profile: Some(TlsProfileConfig {
                tls_enabled: true,
                require_client_mtls: true,
                encrypted_server_cert: b"cert".to_vec(),
                encrypted_server_key: b"key".to_vec(),
                encrypted_client_ca_bundle: Vec::new(), // Missing for client mTLS
                encrypted_upstream_ca_bundle: Vec::new(),
                encrypted_upstream_client_cert: Vec::new(),
                encrypted_upstream_client_key: Vec::new(),
                client_cert_ttl_hours: 24,
                sni: None,
                subject_alt_name_template: None,
                allowed_dns_names: Vec::new(),
            }),
        });

        let result = service
            .update_blueprint_service_tls_profile(invalid_update_request)
            .await;
        assert!(result.is_err());
        assert_eq!(result.unwrap_err().code(), tonic::Code::InvalidArgument);
    }

    #[tokio::test]
    async fn test_tls_profile_disable_tls() {
        let tmp_dir = tempdir().unwrap();
        let db = RocksDb::open(tmp_dir.path(), &RocksDbConfig::default()).unwrap();
        let service = BridgeService::new(tokio::sync::oneshot::channel().0, db.clone());

        // First register a service with TLS
        let register_request = tonic::Request::new(RegisterBlueprintServiceProxyRequest {
            service_id: 1,
            api_key_prefix: "test".to_string(),
            upstream_url: "http://localhost:8080".to_string(),
            owners: vec![],
            tls_profile: Some(TlsProfileConfig {
                tls_enabled: true,
                require_client_mtls: false,
                encrypted_server_cert: b"cert".to_vec(),
                encrypted_server_key: b"key".to_vec(),
                encrypted_client_ca_bundle: b"ca".to_vec(),
                encrypted_upstream_ca_bundle: Vec::new(),
                encrypted_upstream_client_cert: Vec::new(),
                encrypted_upstream_client_key: Vec::new(),
                client_cert_ttl_hours: 24,
                sni: None,
                subject_alt_name_template: None,
                allowed_dns_names: Vec::new(),
            }),
        });

        let result = service
            .register_blueprint_service_proxy(register_request)
            .await;
        assert!(result.is_ok());

        // Test disabling TLS by setting TLS profile to None
        let disable_request = tonic::Request::new(UpdateBlueprintServiceTlsProfileRequest {
            service_id: 1,
            tls_profile: None,
        });

        let result = service
            .update_blueprint_service_tls_profile(disable_request)
            .await;
        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn test_service_id_pinning() {
        let tmp_dir = tempdir().unwrap();
        let db = RocksDb::open(tmp_dir.path(), &RocksDbConfig::default()).unwrap();
        let service = BridgeService::new(tokio::sync::oneshot::channel().0, db);

        // Register service_id=1
        let request = tonic::Request::new(RegisterBlueprintServiceProxyRequest {
            service_id: 1,
            api_key_prefix: "test".to_string(),
            upstream_url: "http://localhost:8080".to_string(),
            owners: vec![],
            tls_profile: None,
        });
        assert!(
            service
                .register_blueprint_service_proxy(request)
                .await
                .is_ok()
        );

        // Try to register service_id=2 on the same bridge - should be rejected (pinned to 1)
        let request = tonic::Request::new(RegisterBlueprintServiceProxyRequest {
            service_id: 2,
            api_key_prefix: "test".to_string(),
            upstream_url: "http://localhost:9090".to_string(),
            owners: vec![],
            tls_profile: None,
        });
        let result = service.register_blueprint_service_proxy(request).await;
        assert!(result.is_err());
        assert_eq!(result.unwrap_err().code(), tonic::Code::PermissionDenied);
    }

    #[tokio::test]
    async fn test_cross_bridge_hijack_protection() {
        let tmp_dir = tempdir().unwrap();
        let db = RocksDb::open(tmp_dir.path(), &RocksDbConfig::default()).unwrap();

        // Bridge A registers service_id=1
        let bridge_a = BridgeService::new(tokio::sync::oneshot::channel().0, db.clone());
        let request = tonic::Request::new(RegisterBlueprintServiceProxyRequest {
            service_id: 1,
            api_key_prefix: "test".to_string(),
            upstream_url: "http://localhost:8080".to_string(),
            owners: vec![],
            tls_profile: None,
        });
        assert!(
            bridge_a
                .register_blueprint_service_proxy(request)
                .await
                .is_ok()
        );

        // Bridge B (different instance, same DB) tries to register service_id=1 - should be rejected
        let bridge_b = BridgeService::new(tokio::sync::oneshot::channel().0, db);
        let request = tonic::Request::new(RegisterBlueprintServiceProxyRequest {
            service_id: 1,
            api_key_prefix: "evil".to_string(),
            upstream_url: "http://attacker:9090".to_string(),
            owners: vec![],
            tls_profile: None,
        });
        let result = bridge_b.register_blueprint_service_proxy(request).await;
        assert!(result.is_err());
        assert_eq!(result.unwrap_err().code(), tonic::Code::AlreadyExists);
    }

    #[tokio::test]
    async fn test_unregister_requires_registration() {
        let tmp_dir = tempdir().unwrap();
        let db = RocksDb::open(tmp_dir.path(), &RocksDbConfig::default()).unwrap();
        let service = BridgeService::new(tokio::sync::oneshot::channel().0, db);

        // Try to unregister without having registered - should fail
        let request = tonic::Request::new(UnregisterBlueprintServiceProxyRequest { service_id: 1 });
        let result = service.unregister_blueprint_service_proxy(request).await;
        assert!(result.is_err());
        assert_eq!(result.unwrap_err().code(), tonic::Code::FailedPrecondition);
    }
}