Build an HTTPS channel for OIDC-authenticated gateways. Tries mTLS client certs for the transport layer when available (the server may still require them alongside the bearer token), falls back to CA-only or system roots.
(name: &str, endpoint: &str)
| 529 | /// may still require them alongside the bearer token), falls back to CA-only |
| 530 | /// or system roots. |
| 531 | async fn build_oidc_channel(name: &str, endpoint: &str) -> Result<Channel> { |
| 532 | let mtls_dir = gateway_mtls_dir(name); |
| 533 | |
| 534 | let tls_config = mtls_dir.as_ref().map_or_else( |
| 535 | || ClientTlsConfig::new().with_enabled_roots(), |
| 536 | |dir| { |
| 537 | let ca = std::fs::read(dir.join("ca.crt")).ok(); |
| 538 | let cert = std::fs::read(dir.join("tls.crt")).ok(); |
| 539 | let key = std::fs::read(dir.join("tls.key")).ok(); |
| 540 | |
| 541 | match (ca, cert, key) { |
| 542 | (Some(ca), Some(cert), Some(key)) => ClientTlsConfig::new() |
| 543 | .ca_certificate(Certificate::from_pem(ca)) |
| 544 | .identity(Identity::from_pem(cert, key)), |
| 545 | (Some(ca), _, _) => { |
| 546 | ClientTlsConfig::new().ca_certificate(Certificate::from_pem(ca)) |
| 547 | } |
| 548 | _ => ClientTlsConfig::new().with_enabled_roots(), |
| 549 | } |
| 550 | }, |
| 551 | ); |
| 552 | |
| 553 | Endpoint::from_shared(endpoint.to_string()) |
| 554 | .into_diagnostic()? |
| 555 | .connect_timeout(Duration::from_secs(10)) |
| 556 | .http2_keep_alive_interval(Duration::from_secs(10)) |
| 557 | .keep_alive_while_idle(true) |
| 558 | .tls_config(tls_config) |
| 559 | .into_diagnostic()? |
| 560 | .connect() |
| 561 | .await |
| 562 | .into_diagnostic() |
| 563 | } |
| 564 | |
| 565 | /// Build a gRPC channel using mTLS client certificates. |
| 566 | async fn build_mtls_channel(name: &str, endpoint: &str) -> Result<Channel> { |
no test coverage detected