Skip to main content

hyprstream_flight/
service.rs

1// tonic::Status is the idiomatic gRPC error type - boxing would break API
2#![allow(clippy::result_large_err)]
3
4use arrow_flight::{
5    encode::FlightDataEncoderBuilder,
6    error::FlightError,
7    flight_service_server::{FlightService, FlightServiceServer},
8    sql::{
9        server::FlightSqlService, ActionClosePreparedStatementRequest,
10        ActionCreatePreparedStatementRequest, ActionCreatePreparedStatementResult,
11        CommandPreparedStatementQuery, CommandStatementQuery, ProstMessageExt, SqlInfo,
12        TicketStatementQuery,
13    },
14    Action, FlightDescriptor, FlightEndpoint, FlightInfo, HandshakeRequest, HandshakeResponse,
15    Ticket,
16};
17use arrow_schema::Schema;
18use futures::{Stream, StreamExt, TryStreamExt};
19use hyprstream_metrics::query::QueryOrchestrator;
20use hyprstream_metrics::storage::{view::ViewDefinition, StorageBackend, StorageBackendType};
21use parking_lot::Mutex;
22use prost::Message;
23use serde::Deserialize;
24use serde_json;
25use std::pin::Pin;
26use std::sync::{atomic::AtomicU64, Arc};
27use tonic::{Request, Response, Status, Streaming};
28
29/// Transport-neutral failure from the embedding server's Flight PEP.
30#[derive(Debug, Clone, PartialEq, Eq)]
31pub enum FlightAuthError {
32    Unauthenticated(String),
33    Forbidden(String),
34    Internal(String),
35}
36
37impl FlightAuthError {
38    fn into_status(self) -> Status {
39        match self {
40            Self::Unauthenticated(message) => Status::unauthenticated(message),
41            Self::Forbidden(message) => Status::permission_denied(message),
42            Self::Internal(message) => Status::internal(message),
43        }
44    }
45}
46
47/// Authentication and tenant-policy boundary for the Flight data plane.
48///
49/// The implementation lives in the embedding binary so this leaf crate does
50/// not depend on Hyprstream's JWT or PolicyClient types.
51#[tonic::async_trait]
52pub trait FlightAuthorizer: Send + Sync {
53    async fn authorize(
54        &self,
55        authorization: Option<&str>,
56        resource: &str,
57        operation: &str,
58    ) -> Result<(), FlightAuthError>;
59}
60
61/// Fail-closed default used by standalone constructors.
62#[derive(Debug, Default)]
63pub struct DenyAllFlightAuthorizer;
64
65#[tonic::async_trait]
66impl FlightAuthorizer for DenyAllFlightAuthorizer {
67    async fn authorize(
68        &self,
69        _authorization: Option<&str>,
70        _resource: &str,
71        _operation: &str,
72    ) -> Result<(), FlightAuthError> {
73        Err(FlightAuthError::Unauthenticated(
74            "Flight authorization is not configured".to_owned(),
75        ))
76    }
77}
78
79#[derive(Debug, Deserialize)]
80struct CreateTableCmd {
81    name: String,
82    schema_bytes: Vec<u8>,
83}
84
85/// Command types for operations
86#[derive(Debug)]
87enum Command {
88    Table(TableCommand),
89    Sql(SqlCommand),
90}
91
92#[derive(Debug)]
93enum SqlCommand {
94    Execute(String),
95    Query(String),
96}
97
98impl Command {
99    fn from_json(cmd: &[u8]) -> Result<Self, Status> {
100        let value: serde_json::Value = serde_json::from_slice(cmd)
101            .map_err(|e| Status::invalid_argument(format!("Invalid JSON: {e}")))?;
102
103        match value.get("type").and_then(|t| t.as_str()) {
104            Some("table") => {
105                let cmd_bytes = serde_json::to_vec(&value["data"])
106                    .map_err(|e| Status::internal(format!("Failed to serialize command: {e}")))?;
107                let cmd = TableCommand::from_json(&cmd_bytes)?;
108                Ok(Command::Table(cmd))
109            }
110            Some("sql.execute") => {
111                let sql =
112                    String::from_utf8(value["data"].as_str().unwrap_or("").as_bytes().to_vec())
113                        .map_err(|e| {
114                            Status::invalid_argument(format!("Invalid SQL string: {e}"))
115                        })?;
116                Ok(Command::Sql(SqlCommand::Execute(sql)))
117            }
118            Some("sql.query") => {
119                let sql =
120                    String::from_utf8(value["data"].as_str().unwrap_or("").as_bytes().to_vec())
121                        .map_err(|e| {
122                            Status::invalid_argument(format!("Invalid SQL string: {e}"))
123                        })?;
124                Ok(Command::Sql(SqlCommand::Query(sql)))
125            }
126            _ => Err(Status::invalid_argument("Invalid command type")),
127        }
128    }
129}
130
131/// Command types for table and view operations
132#[derive(Debug)]
133enum TableCommand {
134    CreateTable {
135        name: String,
136        schema: Arc<Schema>,
137    },
138    CreateView {
139        name: String,
140        definition: ViewDefinition,
141    },
142    DropTable(String),
143    DropView(String),
144}
145
146impl TableCommand {
147    fn from_json(cmd: &[u8]) -> Result<Self, Status> {
148        let value: serde_json::Value = serde_json::from_slice(cmd)
149            .map_err(|e| Status::invalid_argument(format!("Invalid JSON: {e}")))?;
150
151        match value.get("type").and_then(|t| t.as_str()) {
152            Some("create_table") => {
153                let cmd: CreateTableCmd =
154                    serde_json::from_value(value["data"].clone()).map_err(|e| {
155                        Status::invalid_argument(format!("Invalid create table command: {e}"))
156                    })?;
157
158                let schema = arrow_ipc::reader::StreamReader::try_new(
159                    std::io::Cursor::new(&cmd.schema_bytes[..]),
160                    None,
161                )
162                .map_err(|e| Status::invalid_argument(format!("Invalid schema bytes: {e}")))?
163                .schema()
164                .clone();
165
166                Ok(TableCommand::CreateTable {
167                    name: cmd.name,
168                    schema,
169                })
170            }
171            Some("create_view") => {
172                let definition: ViewDefinition =
173                    serde_json::from_value(value["data"]["definition"].clone()).map_err(|e| {
174                        Status::invalid_argument(format!("Invalid view definition: {e}"))
175                    })?;
176                let name = value["data"]["name"]
177                    .as_str()
178                    .ok_or_else(|| Status::invalid_argument("Missing view name"))?
179                    .to_owned();
180                Ok(TableCommand::CreateView { name, definition })
181            }
182            Some("drop_table") => {
183                let name = value["data"]["name"]
184                    .as_str()
185                    .ok_or_else(|| Status::invalid_argument("Missing table name"))?;
186                Ok(TableCommand::DropTable(name.to_owned()))
187            }
188            Some("drop_view") => {
189                let name = value["data"]["name"]
190                    .as_str()
191                    .ok_or_else(|| Status::invalid_argument("Missing view name"))?;
192                Ok(TableCommand::DropView(name.to_owned()))
193            }
194            _ => Err(Status::invalid_argument("Invalid command type")),
195        }
196    }
197}
198
199/// Flight SQL server implementation
200#[derive(Clone)]
201pub struct FlightSqlServer {
202    backend: Arc<StorageBackendType>,
203    orchestrator: Arc<QueryOrchestrator>,
204    #[allow(dead_code)]
205    statement_counter: Arc<AtomicU64>,
206    prepared_statements: Arc<Mutex<Vec<(u64, String)>>>,
207    authorizer: Arc<dyn FlightAuthorizer>,
208    resource: String,
209}
210
211impl std::fmt::Debug for FlightSqlServer {
212    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
213        f.debug_struct("FlightSqlServer")
214            .field("resource", &self.resource)
215            .finish_non_exhaustive()
216    }
217}
218
219impl FlightSqlServer {
220    fn authorization<T>(request: &Request<T>) -> Option<String> {
221        request
222            .metadata()
223            .get("authorization")
224            .and_then(|value| value.to_str().ok())
225            .map(str::to_owned)
226    }
227
228    async fn authorize(&self, authorization: Option<&str>, operation: &str) -> Result<(), Status> {
229        self.authorizer
230            .authorize(authorization, &self.resource, operation)
231            .await
232            .map_err(FlightAuthError::into_status)
233    }
234}
235
236#[tonic::async_trait]
237impl FlightSqlService for FlightSqlServer {
238    type FlightService = Self;
239
240    async fn register_sql_info(&self, _id: i32, _info: &SqlInfo) {}
241
242    async fn do_handshake(
243        &self,
244        request: Request<Streaming<HandshakeRequest>>,
245    ) -> Result<
246        Response<Pin<Box<dyn Stream<Item = Result<HandshakeResponse, Status>> + Send>>>,
247        Status,
248    > {
249        let authorization = Self::authorization(&request);
250        self.authorize(authorization.as_deref(), "query").await?;
251        let mut stream = request.into_inner();
252        let response_stream = async_stream::try_stream! {
253            while let Some(request) = stream.next().await {
254                let request = request?;
255                yield HandshakeResponse {
256                    protocol_version: request.protocol_version,
257                    payload: request.payload,
258                };
259            }
260        };
261        Ok(Response::new(Box::pin(response_stream)))
262    }
263
264    async fn get_flight_info_statement(
265        &self,
266        query: CommandStatementQuery,
267        request: Request<FlightDescriptor>,
268    ) -> Result<Response<FlightInfo>, Status> {
269        let authorization = Self::authorization(&request);
270        self.authorize(authorization.as_deref(), "query").await?;
271        // Use QueryOrchestrator to prepare and get schema
272        let cached_stmt = self
273            .orchestrator
274            .prepare(&query.query)
275            .await
276            .map_err(|e| Status::internal(format!("Query preparation failed: {e}")))?;
277
278        let schema = cached_stmt.schema.clone();
279        let handle = cached_stmt.handle;
280
281        // Store SQL with handle for do_get (for compatibility with Flight SQL protocol)
282        {
283            let mut statements = self.prepared_statements.lock();
284            statements.push((handle, query.query));
285        }
286
287        // Create ticket with statement handle
288        let ticket = TicketStatementQuery {
289            statement_handle: handle.to_le_bytes().to_vec().into(),
290        };
291
292        let flight_descriptor = request.into_inner();
293
294        // Create FlightInfo with schema and endpoint
295        let info = FlightInfo::new()
296            .try_with_schema(schema.as_ref())
297            .map_err(|e| Status::internal(format!("Unable to serialize schema: {e}")))?
298            .with_descriptor(flight_descriptor)
299            .with_endpoint(FlightEndpoint::new().with_ticket(Ticket {
300                ticket: ticket.as_any().encode_to_vec().into(),
301            }))
302            .with_total_records(-1)
303            .with_total_bytes(-1)
304            .with_ordered(false);
305
306        Ok(Response::new(info))
307    }
308
309    async fn do_get_statement(
310        &self,
311        ticket: TicketStatementQuery,
312        request: Request<Ticket>,
313    ) -> Result<Response<<Self as FlightService>::DoGetStream>, Status> {
314        let authorization = Self::authorization(&request);
315        self.authorize(authorization.as_deref(), "query").await?;
316        // Get statement handle from ticket
317        let handle = u64::from_le_bytes(
318            ticket
319                .statement_handle
320                .to_vec()
321                .try_into()
322                .map_err(|_| Status::invalid_argument("Invalid statement handle"))?,
323        );
324
325        // Get cached statement from orchestrator
326        let cached_stmt = self
327            .orchestrator
328            .get_statement(handle)
329            .await
330            .ok_or_else(|| Status::invalid_argument("Statement handle not found"))?;
331
332        let schema = cached_stmt.schema.clone();
333
334        // Execute using the orchestrator's cached physical plan
335        let result_stream = self
336            .orchestrator
337            .execute(&cached_stmt)
338            .await
339            .map_err(|e| Status::internal(format!("Query execution failed: {e}")))?;
340
341        // Convert DataFusion stream to Flight stream
342        let stream = result_stream.map_err(|e| {
343            FlightError::ExternalError(Box::new(std::io::Error::other(e.to_string())))
344        });
345
346        let stream = FlightDataEncoderBuilder::new()
347            .with_schema(schema)
348            .build(stream)
349            .map_err(Status::from);
350
351        Ok(Response::new(Box::pin(stream)))
352    }
353
354    async fn get_flight_info_prepared_statement(
355        &self,
356        _cmd: CommandPreparedStatementQuery,
357        _request: Request<FlightDescriptor>,
358    ) -> Result<Response<FlightInfo>, Status> {
359        Err(Status::unimplemented(
360            "get_flight_info_prepared_statement not implemented",
361        ))
362    }
363
364    async fn do_get_prepared_statement(
365        &self,
366        _query: CommandPreparedStatementQuery,
367        _request: Request<Ticket>,
368    ) -> Result<Response<<Self as FlightService>::DoGetStream>, Status> {
369        Err(Status::unimplemented(
370            "do_get_prepared_statement not implemented",
371        ))
372    }
373
374    async fn do_action_create_prepared_statement(
375        &self,
376        _query: ActionCreatePreparedStatementRequest,
377        _request: Request<Action>,
378    ) -> Result<ActionCreatePreparedStatementResult, Status> {
379        Err(Status::unimplemented(
380            "do_action_create_prepared_statement not implemented",
381        ))
382    }
383
384    async fn do_action_close_prepared_statement(
385        &self,
386        _query: ActionClosePreparedStatementRequest,
387        _request: Request<Action>,
388    ) -> Result<(), Status> {
389        Err(Status::unimplemented(
390            "do_action_close_prepared_statement not implemented",
391        ))
392    }
393}
394
395impl FlightSqlServer {
396    pub async fn new(backend: StorageBackendType) -> Result<Self, Status> {
397        let backend = Arc::new(backend);
398
399        // Create query orchestrator with the storage backend
400        let orchestrator = QueryOrchestrator::new(backend.clone() as Arc<dyn StorageBackend>)
401            .await
402            .map_err(|e| Status::internal(format!("Failed to create query orchestrator: {e}")))?;
403
404        Ok(Self {
405            backend,
406            orchestrator: Arc::new(orchestrator),
407            statement_counter: Arc::new(AtomicU64::new(0)),
408            prepared_statements: Arc::new(Mutex::new(Vec::new())),
409            authorizer: Arc::new(DenyAllFlightAuthorizer),
410            resource: "flight:*".to_owned(),
411        })
412    }
413
414    /// Install the embedding server's JWT + tenant-policy authorizer.
415    pub fn with_authorizer(
416        mut self,
417        authorizer: Arc<dyn FlightAuthorizer>,
418        resource: impl Into<String>,
419    ) -> Self {
420        self.authorizer = authorizer;
421        self.resource = resource.into();
422        self
423    }
424
425    pub fn into_service(self) -> FlightServiceServer<Self> {
426        FlightServiceServer::new(self)
427    }
428
429    async fn handle_table_command(&self, cmd: TableCommand) -> Result<Vec<u8>, Status> {
430        match cmd {
431            TableCommand::CreateTable { name, schema } => {
432                self.backend.create_table(&name, &schema).await?;
433                Ok(vec![])
434            }
435            TableCommand::CreateView { name, definition } => {
436                self.backend.create_view(&name, definition).await?;
437                Ok(vec![])
438            }
439            TableCommand::DropTable(name) => {
440                self.backend.drop_table(&name).await?;
441                Ok(vec![])
442            }
443            TableCommand::DropView(name) => {
444                self.backend.drop_view(&name).await?;
445                Ok(vec![])
446            }
447        }
448    }
449
450    #[allow(dead_code)]
451    async fn handle_action(&self, request: Request<Action>) -> Result<Vec<u8>, Status> {
452        let action = request.into_inner();
453        match Command::from_json(&action.body)? {
454            Command::Table(cmd) => {
455                let result = self.handle_table_command(cmd).await;
456                // Refresh orchestrator's table registrations after schema changes
457                if result.is_ok() {
458                    let _ = self.orchestrator.refresh_tables().await;
459                }
460                result
461            }
462            Command::Sql(SqlCommand::Execute(sql)) => {
463                // Execute statement via orchestrator
464                self.orchestrator
465                    .query_collect(&sql)
466                    .await
467                    .map_err(|e| Status::internal(format!("Query execution failed: {e}")))?;
468                Ok(vec![])
469            }
470            Command::Sql(SqlCommand::Query(sql)) => {
471                // Prepare statement via orchestrator
472                let cached_stmt = self
473                    .orchestrator
474                    .prepare(&sql)
475                    .await
476                    .map_err(|e| Status::internal(format!("Query preparation failed: {e}")))?;
477
478                let handle = cached_stmt.handle;
479
480                // Store SQL with handle for do_get compatibility
481                {
482                    let mut statements = self.prepared_statements.lock();
483                    statements.push((handle, sql.clone()));
484                }
485
486                // Return handle as ticket for DoGet
487                Ok(handle.to_le_bytes().to_vec())
488            }
489        }
490    }
491
492    /// Get reference to the query orchestrator
493    pub fn orchestrator(&self) -> &Arc<QueryOrchestrator> {
494        &self.orchestrator
495    }
496}
497
498#[cfg(test)]
499mod authorization_tests {
500    use super::*;
501
502    #[tokio::test]
503    async fn standalone_flight_authorizer_fails_closed() {
504        let authorizer = DenyAllFlightAuthorizer;
505        assert_eq!(
506            authorizer
507                .authorize(Some("Bearer attacker"), "flight:*", "query")
508                .await,
509            Err(FlightAuthError::Unauthenticated(
510                "Flight authorization is not configured".to_owned()
511            ))
512        );
513    }
514}