|
| 1 | +use super::extract_endpoint; |
| 2 | +use crate::{ |
| 3 | + local_connector::{Connector, UnsealFuture, Unsealed}, |
| 4 | + unseal, |
| 5 | +}; |
| 6 | +use futures::{channel::mpsc, FutureExt, Stream}; |
| 7 | +use proto_flow::{ |
| 8 | + capture::{Request, Response}, |
| 9 | + runtime::CaptureRequestExt, |
| 10 | +}; |
| 11 | + |
| 12 | +fn unseal(mut request: Request) -> Result<UnsealFuture<Request>, Request> { |
| 13 | + if !matches!( |
| 14 | + request, |
| 15 | + Request { spec: Some(_), .. } |
| 16 | + | Request { |
| 17 | + discover: Some(_), |
| 18 | + .. |
| 19 | + } |
| 20 | + | Request { |
| 21 | + validate: Some(_), |
| 22 | + .. |
| 23 | + } |
| 24 | + | Request { apply: Some(_), .. } |
| 25 | + | Request { open: Some(_), .. } |
| 26 | + ) { |
| 27 | + return Err(request); // Not an unseal-able request. |
| 28 | + }; |
| 29 | + |
| 30 | + Ok(async move { |
| 31 | + let (endpoint, config_json) = extract_endpoint(&mut request)?; |
| 32 | + |
| 33 | + let models::CaptureEndpoint::Local(models::LocalConfig { |
| 34 | + command, |
| 35 | + config: sealed_config, |
| 36 | + env, |
| 37 | + protobuf, |
| 38 | + }) = endpoint else { |
| 39 | + anyhow::bail!("task connector type has changed and is no longer an image") |
| 40 | + }; |
| 41 | + |
| 42 | + *config_json = unseal::decrypt_sops(&sealed_config).await?.to_string(); |
| 43 | + |
| 44 | + let log_level = match request.get_internal() { |
| 45 | + Ok(CaptureRequestExt { |
| 46 | + labels: Some(labels), |
| 47 | + .. |
| 48 | + }) => Some(labels.log_level()), |
| 49 | + _ => None, |
| 50 | + }; |
| 51 | + |
| 52 | + Ok(Unsealed { |
| 53 | + command, |
| 54 | + env, |
| 55 | + log_level, |
| 56 | + protobuf, |
| 57 | + request, |
| 58 | + }) |
| 59 | + } |
| 60 | + .boxed()) |
| 61 | +} |
| 62 | + |
| 63 | +pub fn connector<L, R>(log_handler: L, request_rx: R) -> mpsc::Receiver<tonic::Result<Response>> |
| 64 | +where |
| 65 | + L: Fn(&ops::Log) + Clone + Send + Sync + 'static, |
| 66 | + R: Stream<Item = tonic::Result<Request>> + Send + Unpin + 'static, |
| 67 | +{ |
| 68 | + let (connector, response_rx) = Connector::new(log_handler, request_rx, unseal); |
| 69 | + tokio::spawn(async move { connector.run().await }); |
| 70 | + response_rx |
| 71 | +} |
0 commit comments