1pub(crate) use crate::application::json_input::{
2 json_bool_field, json_f32_field, json_string_field, json_usize_field,
3};
4pub(crate) use crate::application::{
5 AdminUseCases, CatalogUseCases, CreateEdgeInput, CreateEntityOutput, CreateNodeGraphLinkInput,
6 CreateNodeInput, CreateNodeTableLinkInput, CreateRowInput, CreateVectorInput,
7 DeleteEntityInput, EntityUseCases, ExecuteQueryInput, ExplainQueryInput, GraphCentralityInput,
8 GraphClusteringInput, GraphCommunitiesInput, GraphComponentsInput, GraphCyclesInput,
9 GraphHitsInput, GraphNeighborhoodInput, GraphPersonalizedPageRankInput, GraphShortestPathInput,
10 GraphTopologicalSortInput, GraphTraversalInput, GraphUseCases, InspectNativeArtifactInput,
11 NativeUseCases, PatchEntityInput, QueryUseCases, SearchHybridInput, SearchIvfInput,
12 SearchSimilarInput, SearchTextInput,
13};
14use std::collections::BTreeMap;
15use std::pin::Pin;
16use std::sync::Arc;
17use std::time::{SystemTime, UNIX_EPOCH};
18
19use crate::api::{RedDBOptions, RedDBResult};
20use crate::auth::middleware::{check_permission, AuthResult, AuthSource};
21use crate::auth::store::AuthStore;
22use crate::auth::Role;
23use crate::health::{HealthProvider, HealthState};
24use crate::json::{
25 from_str as json_from_str, to_string as json_to_string, Map, Value as JsonValue,
26};
27use crate::runtime::{
28 RedDBRuntime, RuntimeFilter, RuntimeFilterValue, RuntimeGraphCentralityAlgorithm,
29 RuntimeGraphCentralityResult, RuntimeGraphClusteringResult, RuntimeGraphCommunityAlgorithm,
30 RuntimeGraphCommunityResult, RuntimeGraphComponentsMode, RuntimeGraphComponentsResult,
31 RuntimeGraphCyclesResult, RuntimeGraphDirection, RuntimeGraphHitsResult,
32 RuntimeGraphNeighborhoodResult, RuntimeGraphPathAlgorithm, RuntimeGraphPathResult,
33 RuntimeGraphPattern, RuntimeGraphProjection, RuntimeGraphTopologicalSortResult,
34 RuntimeGraphTraversalResult, RuntimeGraphTraversalStrategy, RuntimeIvfSearchResult,
35 RuntimeQueryResult, RuntimeQueryWeights, RuntimeStats, ScanPage,
36};
37use crate::storage::schema::Value;
38use crate::storage::unified::devx::refs::{NodeRef, TableRef};
39use crate::storage::unified::{Metadata, MetadataValue};
40use crate::storage::{EntityData, EntityId, UnifiedEntity};
41use tokio_stream::wrappers::TcpListenerStream;
42use tonic::metadata::MetadataMap;
43use tonic::{Request, Response, Status};
44
45pub use reddb_grpc_proto as proto;
51
52use proto::red_db_server::{RedDb, RedDbServer};
53use proto::{
54 ask_stream_event, AskAnswerToken, AskReply, AskRequest, AskSources, AskStreamEvent,
55 BatchInsertChunk, BatchInsertReply, BatchQueryReply, BatchQueryRequest, BulkEntityReply,
56 Citation, CollectionRequest, CollectionsReply, DeleteEntityRequest, DeploymentProfileRequest,
57 Empty, EntityReply, ExecutePreparedRequest, ExportRequest, GraphProjectionUpsertRequest,
58 HealthReply, IndexNameRequest, IndexToggleRequest, JsonBulkCreateRequest, JsonCreateRequest,
59 JsonPayloadRequest, KvWatchEvent, KvWatchRequest, ManifestRequest, OperationReply,
60 PayloadReply, PrepareQueryReply, PrepareQueryRequest, QueryReply, QueryRequest, QueryValue,
61 ScanEntity, ScanReply, ScanRequest, StatsReply, TopologyReply, TopologyRequest,
62 UpdateEntityRequest, Validation, ValidationItem,
63};
64
65mod control_support;
66mod entity_ops;
67mod input_support;
68pub(crate) mod scan_json;
69
70use self::control_support::*;
71use self::entity_ops::*;
72use self::input_support::*;
73use self::scan_json::*;
74
75#[derive(Debug, Clone)]
76pub struct GrpcServerOptions {
77 pub bind_addr: String,
78 pub tls: Option<GrpcTlsOptions>,
83}
84
85#[derive(Debug, Clone)]
91pub struct GrpcTlsOptions {
92 pub cert_pem: Vec<u8>,
94 pub key_pem: Vec<u8>,
96 pub client_ca_pem: Option<Vec<u8>>,
101}
102
103impl GrpcTlsOptions {
104 pub fn to_tonic_config(
108 &self,
109 ) -> Result<tonic::transport::ServerTlsConfig, Box<dyn std::error::Error>> {
110 let identity = tonic::transport::Identity::from_pem(&self.cert_pem, &self.key_pem);
111 let mut cfg = tonic::transport::ServerTlsConfig::new().identity(identity);
112 if let Some(ca_pem) = &self.client_ca_pem {
113 cfg = cfg.client_ca_root(tonic::transport::Certificate::from_pem(ca_pem));
114 }
115 Ok(cfg)
116 }
117}
118
119impl Default for GrpcServerOptions {
120 fn default() -> Self {
121 Self {
122 bind_addr: "127.0.0.1:5555".to_string(),
123 tls: None,
124 }
125 }
126}
127
128#[derive(Clone)]
129pub struct RedDBGrpcServer {
130 runtime: RedDBRuntime,
131 options: GrpcServerOptions,
132 auth_store: Arc<AuthStore>,
133 oauth_validator: Option<Arc<crate::auth::OAuthValidator>>,
139}
140
141impl RedDBGrpcServer {
142 pub fn new(runtime: RedDBRuntime) -> Self {
143 let auth_config = crate::auth::AuthConfig::default();
144 let auth_store = Arc::new(AuthStore::new(auth_config));
145 Self::with_options(runtime, GrpcServerOptions::default(), auth_store)
146 }
147
148 pub fn from_database_options(
149 db_options: RedDBOptions,
150 options: GrpcServerOptions,
151 ) -> RedDBResult<Self> {
152 let runtime = RedDBRuntime::with_options(db_options.clone())?;
154
155 let auth_store = if db_options.auth.vault_enabled {
156 let pager = runtime.db().store().pager().cloned().ok_or_else(|| {
160 crate::api::RedDBError::Internal(
161 "vault requires a paged database (persistent mode)".into(),
162 )
163 })?;
164 let store = AuthStore::with_vault(db_options.auth.clone(), pager, None)
165 .map_err(|e| crate::api::RedDBError::Internal(e.to_string()))?;
166 Arc::new(store)
167 } else {
168 Arc::new(AuthStore::new(db_options.auth.clone()))
169 };
170 auth_store.bootstrap_from_env();
171 Ok(Self::with_options(runtime, options, auth_store))
172 }
173
174 pub fn with_options(
175 runtime: RedDBRuntime,
176 options: GrpcServerOptions,
177 auth_store: Arc<AuthStore>,
178 ) -> Self {
179 runtime.set_auth_store(Arc::clone(&auth_store));
182 Self {
183 runtime,
184 options,
185 auth_store,
186 oauth_validator: None,
187 }
188 }
189
190 pub fn with_oauth_validator(mut self, validator: Arc<crate::auth::OAuthValidator>) -> Self {
196 self.oauth_validator = Some(validator);
197 self
198 }
199
200 pub fn oauth_validator(&self) -> Option<&Arc<crate::auth::OAuthValidator>> {
202 self.oauth_validator.as_ref()
203 }
204
205 pub fn runtime(&self) -> &RedDBRuntime {
206 &self.runtime
207 }
208
209 pub fn options(&self) -> &GrpcServerOptions {
210 &self.options
211 }
212
213 pub fn auth_store(&self) -> &Arc<AuthStore> {
214 &self.auth_store
215 }
216
217 fn grpc_runtime(&self) -> GrpcRuntime {
218 GrpcRuntime {
219 runtime: self.runtime.clone(),
220 auth_store: self.auth_store.clone(),
221 prepared_registry: PreparedStatementRegistry::new(),
222 oauth_validator: self.oauth_validator.clone(),
223 }
224 }
225
226 pub async fn serve(&self) -> Result<(), Box<dyn std::error::Error>> {
227 let addr = self.options.bind_addr.parse()?;
228 let mut builder = tonic::transport::Server::builder();
229 if let Some(tls) = &self.options.tls {
230 log_grpc_tls_identity(tls);
233 builder = builder.tls_config(tls.to_tonic_config()?)?;
234 }
235 builder
236 .add_service(Self::configured_service(self.grpc_runtime()))
237 .serve(addr)
238 .await?;
239 Ok(())
240 }
241
242 pub async fn serve_on(
243 &self,
244 listener: std::net::TcpListener,
245 ) -> Result<(), Box<dyn std::error::Error>> {
246 listener.set_nonblocking(true)?;
247 let listener = tokio::net::TcpListener::from_std(listener)?;
248 let incoming = TcpListenerStream::new(listener);
249 let mut builder = tonic::transport::Server::builder();
250 if let Some(tls) = &self.options.tls {
251 log_grpc_tls_identity(tls);
252 builder = builder.tls_config(tls.to_tonic_config()?)?;
253 }
254 builder
255 .add_service(Self::configured_service(self.grpc_runtime()))
256 .serve_with_incoming(incoming)
257 .await?;
258 Ok(())
259 }
260
261 fn configured_service(runtime: GrpcRuntime) -> RedDbServer<GrpcRuntime> {
262 use tonic::codec::CompressionEncoding;
266 RedDbServer::new(runtime)
267 .max_decoding_message_size(256 * 1024 * 1024)
268 .max_encoding_message_size(256 * 1024 * 1024)
269 .accept_compressed(CompressionEncoding::Zstd)
270 .accept_compressed(CompressionEncoding::Gzip)
271 .send_compressed(CompressionEncoding::Zstd)
272 }
273}
274
275struct GrpcPreparedStatement {
277 shape: std::sync::Arc<crate::storage::query::ast::QueryExpr>,
278 parameter_count: usize,
279 created_at: std::time::Instant,
280}
281
282struct PreparedStatementRegistry {
285 map: parking_lot::RwLock<std::collections::HashMap<u64, GrpcPreparedStatement>>,
287 next_id: std::sync::atomic::AtomicU64,
288 get_count: std::sync::atomic::AtomicU64,
289}
290
291impl PreparedStatementRegistry {
292 fn new() -> Arc<Self> {
293 Arc::new(Self {
294 map: parking_lot::RwLock::new(std::collections::HashMap::new()),
295 next_id: std::sync::atomic::AtomicU64::new(1),
296 get_count: std::sync::atomic::AtomicU64::new(0),
297 })
298 }
299
300 fn prepare(&self, shape: crate::storage::query::ast::QueryExpr, parameter_count: usize) -> u64 {
301 use std::sync::atomic::Ordering;
302 let id = self.next_id.fetch_add(1, Ordering::Relaxed);
303 let mut map = self.map.write();
304 self.evict_old_locked(&mut map);
305 map.insert(
306 id,
307 GrpcPreparedStatement {
308 shape: std::sync::Arc::new(shape),
310 parameter_count,
311 created_at: std::time::Instant::now(),
312 },
313 );
314 id
315 }
316
317 fn get_shape_and_count(
318 &self,
319 id: u64,
320 ) -> Option<(std::sync::Arc<crate::storage::query::ast::QueryExpr>, usize)> {
321 let get_count = self
324 .get_count
325 .fetch_add(1, std::sync::atomic::Ordering::Relaxed)
326 + 1;
327 if get_count.is_multiple_of(256) {
328 let mut map = self.map.write();
329 self.evict_old_locked(&mut map);
330 }
331 let map = self.map.read();
332 map.get(&id)
333 .map(|s| (std::sync::Arc::clone(&s.shape), s.parameter_count))
334 }
335
336 fn evict_old_locked(&self, map: &mut std::collections::HashMap<u64, GrpcPreparedStatement>) {
337 let threshold = std::time::Duration::from_secs(3600);
338 map.retain(|_, v| v.created_at.elapsed() < threshold);
339 }
340}
341
342#[derive(Clone)]
343struct GrpcRuntime {
344 runtime: RedDBRuntime,
345 auth_store: Arc<AuthStore>,
346 prepared_registry: Arc<PreparedStatementRegistry>,
347 oauth_validator: Option<Arc<crate::auth::OAuthValidator>>,
351}
352
353impl GrpcRuntime {
354 fn admin_use_cases(&self) -> AdminUseCases<'_, RedDBRuntime> {
355 AdminUseCases::new(&self.runtime)
356 }
357
358 fn catalog_use_cases(&self) -> CatalogUseCases<'_, RedDBRuntime> {
359 CatalogUseCases::new(&self.runtime)
360 }
361
362 fn query_use_cases(&self) -> QueryUseCases<'_, RedDBRuntime> {
363 QueryUseCases::new(&self.runtime)
364 }
365
366 fn entity_use_cases(&self) -> EntityUseCases<'_, RedDBRuntime> {
367 EntityUseCases::new(&self.runtime)
368 }
369
370 fn graph_use_cases(&self) -> GraphUseCases<'_, RedDBRuntime> {
371 GraphUseCases::new(&self.runtime)
372 }
373
374 fn native_use_cases(&self) -> NativeUseCases<'_, RedDBRuntime> {
375 NativeUseCases::new(&self.runtime)
376 }
377}
378
379fn grpc_query_value_to_schema_value(value: QueryValue) -> Result<Value, Status> {
380 use proto::query_value::Kind;
381
382 match value
383 .kind
384 .ok_or_else(|| Status::invalid_argument("missing query param value"))?
385 {
386 Kind::NullValue(_) => Ok(Value::Null),
387 Kind::BoolValue(value) => Ok(Value::Boolean(value)),
388 Kind::IntValue(value) => Ok(Value::Integer(value)),
389 Kind::FloatValue(value) => Ok(Value::Float(value)),
390 Kind::TextValue(value) => Ok(Value::Text(std::sync::Arc::from(value))),
391 Kind::BytesValue(value) => Ok(Value::Blob(value)),
392 Kind::VectorValue(value) => Ok(Value::Vector(value.values)),
393 Kind::JsonValue(value) => {
394 let parsed = json_from_str::<JsonValue>(&value)
395 .map_err(|e| Status::invalid_argument(format!("json param parse error: {e}")))?;
396 let encoded = json_to_string(&parsed)
397 .map_err(|e| Status::invalid_argument(format!("json param encode error: {e}")))?;
398 Ok(Value::Json(encoded.into_bytes()))
399 }
400 Kind::TimestampValue(value) => Ok(Value::Timestamp(value)),
401 Kind::UuidValue(value) => {
402 let bytes: [u8; 16] = value.try_into().map_err(|value: Vec<u8>| {
403 Status::invalid_argument(format!(
404 "uuid param must be 16 bytes, got {}",
405 value.len()
406 ))
407 })?;
408 Ok(Value::Uuid(bytes))
409 }
410 }
411}
412
413fn execute_grpc_query_with_optional_params(
414 runtime: &RedDBRuntime,
415 query: String,
416 params: Vec<QueryValue>,
417) -> Result<RuntimeQueryResult, Status> {
418 if query.trim().is_empty() {
419 return Err(Status::invalid_argument("query field cannot be empty"));
420 }
421
422 if params.is_empty() {
423 return runtime.execute_query(&query).map_err(to_status);
424 }
425
426 let binds = params
427 .into_iter()
428 .map(grpc_query_value_to_schema_value)
429 .collect::<Result<Vec<_>, _>>()?;
430 let parsed = crate::storage::query::modes::parse_multi(&query)
431 .map_err(|e| Status::invalid_argument(format!("parse error: {e}")))?;
432 let bound = crate::storage::query::user_params::bind(&parsed, &binds)
433 .map_err(|e| Status::invalid_argument(format!("bind error: {e}")))?;
434 runtime.execute_query_expr(bound).map_err(to_status)
435}
436
437#[cfg(test)]
438mod grpc_query_value_tests {
439 use super::*;
440 use proto::query_value::Kind;
441
442 #[test]
443 fn grpc_query_value_maps_to_schema_value_variants() {
444 let cases = vec![
445 (
446 QueryValue {
447 kind: Some(Kind::NullValue(proto::QueryNull {})),
448 },
449 Value::Null,
450 ),
451 (
452 QueryValue {
453 kind: Some(Kind::BoolValue(true)),
454 },
455 Value::Boolean(true),
456 ),
457 (
458 QueryValue {
459 kind: Some(Kind::IntValue(42)),
460 },
461 Value::Integer(42),
462 ),
463 (
464 QueryValue {
465 kind: Some(Kind::FloatValue(1.5)),
466 },
467 Value::Float(1.5),
468 ),
469 (
470 QueryValue {
471 kind: Some(Kind::BytesValue(vec![0, 1, 2])),
472 },
473 Value::Blob(vec![0, 1, 2]),
474 ),
475 (
476 QueryValue {
477 kind: Some(Kind::VectorValue(proto::QueryVector {
478 values: vec![0.25, 0.5],
479 })),
480 },
481 Value::Vector(vec![0.25, 0.5]),
482 ),
483 (
484 QueryValue {
485 kind: Some(Kind::TimestampValue(1_779_999_000)),
486 },
487 Value::Timestamp(1_779_999_000),
488 ),
489 (
490 QueryValue {
491 kind: Some(Kind::UuidValue(vec![0x11; 16])),
492 },
493 Value::Uuid([0x11; 16]),
494 ),
495 ];
496
497 for (input, expected) in cases {
498 assert_eq!(grpc_query_value_to_schema_value(input).unwrap(), expected);
499 }
500
501 assert_eq!(
502 grpc_query_value_to_schema_value(QueryValue {
503 kind: Some(Kind::TextValue("alice".into())),
504 })
505 .unwrap(),
506 Value::Text(std::sync::Arc::from("alice"))
507 );
508 assert_eq!(
509 grpc_query_value_to_schema_value(QueryValue {
510 kind: Some(Kind::JsonValue("{\"role\":\"admin\"}".into())),
511 })
512 .unwrap(),
513 Value::Json(b"{\"role\":\"admin\"}".to_vec())
514 );
515 }
516
517 #[test]
518 fn grpc_query_value_rejects_missing_kind_and_bad_uuid() {
519 assert!(grpc_query_value_to_schema_value(QueryValue { kind: None }).is_err());
520 assert!(grpc_query_value_to_schema_value(QueryValue {
521 kind: Some(Kind::UuidValue(vec![0; 15])),
522 })
523 .is_err());
524 }
525
526 #[test]
527 fn grpc_query_rejects_empty_query_before_runtime_parse() {
528 let runtime =
529 RedDBRuntime::with_options(crate::api::RedDBOptions::in_memory()).expect("runtime");
530 let err = execute_grpc_query_with_optional_params(&runtime, " ".to_string(), Vec::new())
531 .expect_err("empty query should fail");
532
533 assert_eq!(err.code(), tonic::Code::InvalidArgument);
534 assert_eq!(err.message(), "query field cannot be empty");
535 }
536
537 #[test]
538 fn grpc_query_params_are_bound_before_execution() {
539 let runtime =
540 RedDBRuntime::with_options(crate::api::RedDBOptions::in_memory()).expect("runtime");
541 seed_grpc_param_table(&runtime);
542
543 let result = execute_grpc_query_with_optional_params(
544 &runtime,
545 "SELECT id, name FROM p WHERE id = $1 AND name = $2".to_string(),
546 grpc_param_values(),
547 )
548 .expect("parameterized query");
549
550 assert_eq!(result.result.records.len(), 1);
551 }
552
553 #[tokio::test]
554 async fn grpc_query_rpc_binds_query_request_params() {
555 let runtime =
556 RedDBRuntime::with_options(crate::api::RedDBOptions::in_memory()).expect("runtime");
557 seed_grpc_param_table(&runtime);
558 let service = GrpcRuntime {
559 runtime,
560 auth_store: Arc::new(AuthStore::new(crate::auth::AuthConfig::default())),
561 prepared_registry: PreparedStatementRegistry::new(),
562 oauth_validator: None,
563 };
564
565 let reply = RedDb::query(
566 &service,
567 Request::new(QueryRequest {
568 query: "SELECT id, name FROM p WHERE id = $1 AND name = $2".to_string(),
569 entity_types: Vec::new(),
570 capabilities: Vec::new(),
571 params: grpc_param_values(),
572 }),
573 )
574 .await
575 .expect("query rpc")
576 .into_inner();
577
578 assert_eq!(reply.record_count, 1);
579 assert!(reply.result_json.contains("Alice"), "{}", reply.result_json);
580 assert!(!reply.result_json.contains("Bob"), "{}", reply.result_json);
581 }
582
583 fn seed_grpc_param_table(runtime: &RedDBRuntime) {
584 runtime
585 .execute_query("CREATE TABLE p (id INTEGER, name TEXT)")
586 .expect("create table");
587 runtime
588 .execute_query("INSERT INTO p (id, name) VALUES (1, 'Alice')")
589 .expect("insert alice");
590 runtime
591 .execute_query("INSERT INTO p (id, name) VALUES (2, 'Bob')")
592 .expect("insert bob");
593 }
594
595 fn grpc_param_values() -> Vec<QueryValue> {
596 vec![
597 QueryValue {
598 kind: Some(Kind::IntValue(1)),
599 },
600 QueryValue {
601 kind: Some(Kind::TextValue("Alice".to_string())),
602 },
603 ]
604 }
605}
606
607#[cfg(test)]
608mod grpc_ask_query_reply_tests {
609 use super::*;
610 use crate::storage::query::modes::QueryMode;
611 use crate::storage::query::unified::{UnifiedRecord, UnifiedResult};
612 use crate::storage::schema::Value as SchemaValue;
613
614 fn ask_runtime_result() -> RuntimeQueryResult {
615 let mut result = UnifiedResult::with_columns(vec![
616 "answer".into(),
617 "provider".into(),
618 "model".into(),
619 "mode".into(),
620 "retry_count".into(),
621 "prompt_tokens".into(),
622 "completion_tokens".into(),
623 "sources_flat".into(),
624 "citations".into(),
625 "validation".into(),
626 ]);
627 let mut record = UnifiedRecord::new();
628 record.set("answer", SchemaValue::text("Deploy failed [^1]."));
629 record.set("provider", SchemaValue::text("openai"));
630 record.set("model", SchemaValue::text("gpt-4o-mini"));
631 record.set("mode", SchemaValue::text("strict"));
632 record.set("retry_count", SchemaValue::Integer(0));
633 record.set("prompt_tokens", SchemaValue::Integer(11));
634 record.set("completion_tokens", SchemaValue::Integer(7));
635 record.set(
636 "sources_flat",
637 SchemaValue::Json(
638 br#"[{"urn":"urn:reddb:row:deployments:1","kind":"row","collection":"deployments","id":"1"}]"#.to_vec(),
639 ),
640 );
641 record.set(
642 "citations",
643 SchemaValue::Json(br#"[{"marker":1,"urn":"urn:reddb:row:deployments:1"}]"#.to_vec()),
644 );
645 record.set(
646 "validation",
647 SchemaValue::Json(br#"{"ok":true,"warnings":[],"errors":[]}"#.to_vec()),
648 );
649 result.push(record);
650
651 RuntimeQueryResult {
652 query: "ASK 'why did deploy fail?'".to_string(),
653 mode: QueryMode::Sql,
654 statement: "ask",
655 engine: "runtime-ai",
656 result,
657 affected_rows: 0,
658 statement_type: "select",
659 bookmark: None,
660 }
661 }
662
663 #[test]
664 fn query_reply_ask_result_json_uses_full_canonical_schema() {
665 let reply = query_reply(ask_runtime_result(), &None, &None);
666 let json: crate::json::Value =
667 crate::json::from_str(&reply.result_json).expect("valid ask json");
668
669 assert_eq!(
670 json.get("answer").and_then(crate::json::Value::as_str),
671 Some("Deploy failed [^1].")
672 );
673 assert_eq!(
674 json.get("cache_hit").and_then(crate::json::Value::as_bool),
675 Some(false)
676 );
677 assert_eq!(
678 json.get("cost_usd").and_then(crate::json::Value::as_f64),
679 Some(0.0)
680 );
681 assert_eq!(
682 json.get("mode").and_then(crate::json::Value::as_str),
683 Some("strict")
684 );
685 assert_eq!(
686 json.get("retry_count").and_then(crate::json::Value::as_u64),
687 Some(0)
688 );
689 assert!(
690 json.get("records").is_none(),
691 "ASK must not be row-wrapped: {}",
692 reply.result_json
693 );
694 assert!(
695 json.get("sources_flat")
696 .and_then(crate::json::Value::as_array)
697 .is_some_and(|sources| sources.len() == 1
698 && sources[0]
699 .get("payload")
700 .and_then(crate::json::Value::as_str)
701 .is_some()),
702 "sources_flat must be parsed with payload fallback: {}",
703 reply.result_json
704 );
705 assert!(
706 json.get("citations")
707 .and_then(crate::json::Value::as_array)
708 .is_some_and(|citations| citations.len() == 1),
709 "citations must be parsed: {}",
710 reply.result_json
711 );
712 assert_eq!(
713 json.get("validation")
714 .and_then(|v| v.get("ok"))
715 .and_then(crate::json::Value::as_bool),
716 Some(true)
717 );
718 }
719
720 #[test]
721 fn query_reply_non_ask_answer_column_keeps_row_shape() {
722 let mut result = UnifiedResult::with_columns(vec!["answer".into()]);
723 let mut record = UnifiedRecord::new();
724 record.set("answer", SchemaValue::text("plain select"));
725 result.push(record);
726
727 let reply = query_reply(
728 RuntimeQueryResult {
729 query: "SELECT 'plain select' AS answer".to_string(),
730 mode: QueryMode::Sql,
731 statement: "select",
732 engine: "runtime-sql",
733 result,
734 affected_rows: 0,
735 statement_type: "select",
736 bookmark: None,
737 },
738 &None,
739 &None,
740 );
741 let json: crate::json::Value =
742 crate::json::from_str(&reply.result_json).expect("valid query json");
743
744 assert!(
745 json.get("records").is_some(),
746 "non-ASK must stay row-wrapped"
747 );
748 assert!(
749 json.get("answer").is_none(),
750 "non-ASK must not use ASK envelope"
751 );
752 }
753}
754
755fn log_grpc_tls_identity(tls: &GrpcTlsOptions) {
758 use sha2::{Digest, Sha256};
759 let cert_fp = {
760 let mut h = Sha256::new();
761 h.update(&tls.cert_pem);
762 let digest = h.finalize();
763 let mut buf = String::with_capacity(64);
766 for b in digest.iter() {
767 buf.push_str(&format!("{b:02x}"));
768 }
769 buf
770 };
771 tracing::info!(
772 target: "reddb::security",
773 transport = "grpc",
774 cert_sha256 = %cert_fp,
775 mtls = tls.client_ca_pem.is_some(),
776 "gRPC TLS identity loaded"
777 );
778}
779
780include!("grpc/service_impl.rs");