LCOV - code coverage report
Current view: top level - proxy/src/serverless - sql_over_http.rs (source / functions) Coverage Total Hit
Test: 6df3fc19ec669bcfbbf9aba41d1338898d24eaa0.info Lines: 5.5 % 838 46
Test Date: 2025-03-12 18:28:53 Functions: 3.9 % 103 4

            Line data    Source code
       1              : use std::pin::pin;
       2              : use std::sync::Arc;
       3              : 
       4              : use bytes::Bytes;
       5              : use futures::future::{Either, select, try_join};
       6              : use futures::{StreamExt, TryFutureExt};
       7              : use http::Method;
       8              : use http::header::AUTHORIZATION;
       9              : use http_body_util::combinators::BoxBody;
      10              : use http_body_util::{BodyExt, Full};
      11              : use http_utils::error::ApiError;
      12              : use hyper::body::Incoming;
      13              : use hyper::http::{HeaderName, HeaderValue};
      14              : use hyper::{HeaderMap, Request, Response, StatusCode, header};
      15              : use indexmap::IndexMap;
      16              : use postgres_client::error::{DbError, ErrorPosition, SqlState};
      17              : use postgres_client::{GenericClient, IsolationLevel, NoTls, ReadyForQueryStatus, Transaction};
      18              : use pq_proto::StartupMessageParamsBuilder;
      19              : use serde::Serialize;
      20              : use serde_json::Value;
      21              : use serde_json::value::RawValue;
      22              : use tokio::time::{self, Instant};
      23              : use tokio_util::sync::CancellationToken;
      24              : use tracing::{debug, error, info};
      25              : use typed_json::json;
      26              : use url::Url;
      27              : use uuid::Uuid;
      28              : 
      29              : use super::backend::{LocalProxyConnError, PoolingBackend};
      30              : use super::conn_pool::{AuthData, ConnInfoWithAuth};
      31              : use super::conn_pool_lib::{self, ConnInfo};
      32              : use super::error::HttpCodeError;
      33              : use super::http_util::json_response;
      34              : use super::json::{JsonConversionError, json_to_pg_text, pg_text_row_to_json};
      35              : use crate::auth::backend::{ComputeCredentialKeys, ComputeUserInfo};
      36              : use crate::auth::{ComputeUserInfoParseError, endpoint_sni};
      37              : use crate::config::{AuthenticationConfig, HttpConfig, ProxyConfig, TlsConfig};
      38              : use crate::context::RequestContext;
      39              : use crate::error::{ErrorKind, ReportableError, UserFacingError};
      40              : use crate::http::{ReadBodyError, read_body_with_limit};
      41              : use crate::metrics::{HttpDirection, Metrics};
      42              : use crate::proxy::{NeonOptions, run_until_cancelled};
      43              : use crate::serverless::backend::HttpConnError;
      44              : use crate::types::{DbName, RoleName};
      45              : use crate::usage_metrics::{MetricCounter, MetricCounterRecorder, TrafficDirection};
      46              : 
      47           11 : #[derive(serde::Deserialize)]
      48              : #[serde(rename_all = "camelCase")]
      49              : struct QueryData {
      50              :     query: String,
      51              :     #[serde(deserialize_with = "bytes_to_pg_text")]
      52              :     #[serde(default)]
      53              :     params: Vec<Option<String>>,
      54              :     #[serde(default)]
      55              :     array_mode: Option<bool>,
      56              : }
      57              : 
      58            1 : #[derive(serde::Deserialize)]
      59              : struct BatchQueryData {
      60              :     queries: Vec<QueryData>,
      61              : }
      62              : 
      63              : #[derive(serde::Deserialize)]
      64              : #[serde(untagged)]
      65              : enum Payload {
      66              :     Single(QueryData),
      67              :     Batch(BatchQueryData),
      68              : }
      69              : 
      70              : pub(super) static NEON_REQUEST_ID: HeaderName = HeaderName::from_static("neon-request-id");
      71              : 
      72              : static CONN_STRING: HeaderName = HeaderName::from_static("neon-connection-string");
      73              : static RAW_TEXT_OUTPUT: HeaderName = HeaderName::from_static("neon-raw-text-output");
      74              : static ARRAY_MODE: HeaderName = HeaderName::from_static("neon-array-mode");
      75              : static ALLOW_POOL: HeaderName = HeaderName::from_static("neon-pool-opt-in");
      76              : static TXN_ISOLATION_LEVEL: HeaderName = HeaderName::from_static("neon-batch-isolation-level");
      77              : static TXN_READ_ONLY: HeaderName = HeaderName::from_static("neon-batch-read-only");
      78              : static TXN_DEFERRABLE: HeaderName = HeaderName::from_static("neon-batch-deferrable");
      79              : 
      80              : static HEADER_VALUE_TRUE: HeaderValue = HeaderValue::from_static("true");
      81              : 
      82            3 : fn bytes_to_pg_text<'de, D>(deserializer: D) -> Result<Vec<Option<String>>, D::Error>
      83            3 : where
      84            3 :     D: serde::de::Deserializer<'de>,
      85            3 : {
      86              :     // TODO: consider avoiding the allocation here.
      87            3 :     let json: Vec<Value> = serde::de::Deserialize::deserialize(deserializer)?;
      88            3 :     Ok(json_to_pg_text(json))
      89            3 : }
      90              : 
      91              : #[derive(Debug, thiserror::Error)]
      92              : pub(crate) enum ConnInfoError {
      93              :     #[error("invalid header: {0}")]
      94              :     InvalidHeader(&'static HeaderName),
      95              :     #[error("invalid connection string: {0}")]
      96              :     UrlParseError(#[from] url::ParseError),
      97              :     #[error("incorrect scheme")]
      98              :     IncorrectScheme,
      99              :     #[error("missing database name")]
     100              :     MissingDbName,
     101              :     #[error("invalid database name")]
     102              :     InvalidDbName,
     103              :     #[error("missing username")]
     104              :     MissingUsername,
     105              :     #[error("invalid username: {0}")]
     106              :     InvalidUsername(#[from] std::string::FromUtf8Error),
     107              :     #[error("missing authentication credentials: {0}")]
     108              :     MissingCredentials(Credentials),
     109              :     #[error("missing hostname")]
     110              :     MissingHostname,
     111              :     #[error("invalid hostname: {0}")]
     112              :     InvalidEndpoint(#[from] ComputeUserInfoParseError),
     113              :     #[error("malformed endpoint")]
     114              :     MalformedEndpoint,
     115              : }
     116              : 
     117              : #[derive(Debug, thiserror::Error)]
     118              : pub(crate) enum Credentials {
     119              :     #[error("required password")]
     120              :     Password,
     121              :     #[error("required authorization bearer token in JWT format")]
     122              :     BearerJwt,
     123              : }
     124              : 
     125              : impl ReportableError for ConnInfoError {
     126            0 :     fn get_error_kind(&self) -> ErrorKind {
     127            0 :         ErrorKind::User
     128            0 :     }
     129              : }
     130              : 
     131              : impl UserFacingError for ConnInfoError {
     132            0 :     fn to_string_client(&self) -> String {
     133            0 :         self.to_string()
     134            0 :     }
     135              : }
     136              : 
     137            0 : fn get_conn_info(
     138            0 :     config: &'static AuthenticationConfig,
     139            0 :     ctx: &RequestContext,
     140            0 :     headers: &HeaderMap,
     141            0 :     tls: Option<&TlsConfig>,
     142            0 : ) -> Result<ConnInfoWithAuth, ConnInfoError> {
     143            0 :     let connection_string = headers
     144            0 :         .get(&CONN_STRING)
     145            0 :         .ok_or(ConnInfoError::InvalidHeader(&CONN_STRING))?
     146            0 :         .to_str()
     147            0 :         .map_err(|_| ConnInfoError::InvalidHeader(&CONN_STRING))?;
     148              : 
     149            0 :     let connection_url = Url::parse(connection_string)?;
     150              : 
     151            0 :     let protocol = connection_url.scheme();
     152            0 :     if protocol != "postgres" && protocol != "postgresql" {
     153            0 :         return Err(ConnInfoError::IncorrectScheme);
     154            0 :     }
     155              : 
     156            0 :     let mut url_path = connection_url
     157            0 :         .path_segments()
     158            0 :         .ok_or(ConnInfoError::MissingDbName)?;
     159              : 
     160            0 :     let dbname: DbName =
     161            0 :         urlencoding::decode(url_path.next().ok_or(ConnInfoError::InvalidDbName)?)?.into();
     162            0 :     ctx.set_dbname(dbname.clone());
     163              : 
     164            0 :     let username = RoleName::from(urlencoding::decode(connection_url.username())?);
     165            0 :     if username.is_empty() {
     166            0 :         return Err(ConnInfoError::MissingUsername);
     167            0 :     }
     168            0 :     ctx.set_user(username.clone());
     169              : 
     170            0 :     let auth = if let Some(auth) = headers.get(&AUTHORIZATION) {
     171            0 :         if !config.accept_jwts {
     172            0 :             return Err(ConnInfoError::MissingCredentials(Credentials::Password));
     173            0 :         }
     174              : 
     175            0 :         let auth = auth
     176            0 :             .to_str()
     177            0 :             .map_err(|_| ConnInfoError::InvalidHeader(&AUTHORIZATION))?;
     178              :         AuthData::Jwt(
     179            0 :             auth.strip_prefix("Bearer ")
     180            0 :                 .ok_or(ConnInfoError::MissingCredentials(Credentials::BearerJwt))?
     181            0 :                 .into(),
     182              :         )
     183            0 :     } else if let Some(pass) = connection_url.password() {
     184              :         // wrong credentials provided
     185            0 :         if config.accept_jwts {
     186            0 :             return Err(ConnInfoError::MissingCredentials(Credentials::BearerJwt));
     187            0 :         }
     188            0 : 
     189            0 :         AuthData::Password(match urlencoding::decode_binary(pass.as_bytes()) {
     190            0 :             std::borrow::Cow::Borrowed(b) => b.into(),
     191            0 :             std::borrow::Cow::Owned(b) => b.into(),
     192              :         })
     193            0 :     } else if config.accept_jwts {
     194            0 :         return Err(ConnInfoError::MissingCredentials(Credentials::BearerJwt));
     195              :     } else {
     196            0 :         return Err(ConnInfoError::MissingCredentials(Credentials::Password));
     197              :     };
     198              : 
     199            0 :     let endpoint = match connection_url.host() {
     200            0 :         Some(url::Host::Domain(hostname)) => {
     201            0 :             if let Some(tls) = tls {
     202            0 :                 endpoint_sni(hostname, &tls.common_names)?
     203            0 :                     .ok_or(ConnInfoError::MalformedEndpoint)?
     204              :             } else {
     205            0 :                 hostname
     206            0 :                     .split_once('.')
     207            0 :                     .map_or(hostname, |(prefix, _)| prefix)
     208            0 :                     .into()
     209              :             }
     210              :         }
     211              :         Some(url::Host::Ipv4(_) | url::Host::Ipv6(_)) | None => {
     212            0 :             return Err(ConnInfoError::MissingHostname);
     213              :         }
     214              :     };
     215            0 :     ctx.set_endpoint_id(endpoint.clone());
     216            0 : 
     217            0 :     let pairs = connection_url.query_pairs();
     218            0 : 
     219            0 :     let mut options = Option::None;
     220            0 : 
     221            0 :     let mut params = StartupMessageParamsBuilder::default();
     222            0 :     params.insert("user", &username);
     223            0 :     params.insert("database", &dbname);
     224            0 :     for (key, value) in pairs {
     225            0 :         params.insert(&key, &value);
     226            0 :         if key == "options" {
     227            0 :             options = Some(NeonOptions::parse_options_raw(&value));
     228            0 :         }
     229              :     }
     230              : 
     231            0 :     ctx.set_user_agent(
     232            0 :         headers
     233            0 :             .get(hyper::header::USER_AGENT)
     234            0 :             .and_then(|h| h.to_str().ok())
     235            0 :             .map(Into::into),
     236            0 :     );
     237            0 : 
     238            0 :     let user_info = ComputeUserInfo {
     239            0 :         endpoint,
     240            0 :         user: username,
     241            0 :         options: options.unwrap_or_default(),
     242            0 :     };
     243            0 : 
     244            0 :     let conn_info = ConnInfo { user_info, dbname };
     245            0 :     Ok(ConnInfoWithAuth { conn_info, auth })
     246            0 : }
     247              : 
     248            0 : pub(crate) async fn handle(
     249            0 :     config: &'static ProxyConfig,
     250            0 :     ctx: RequestContext,
     251            0 :     request: Request<Incoming>,
     252            0 :     backend: Arc<PoolingBackend>,
     253            0 :     cancel: CancellationToken,
     254            0 : ) -> Result<Response<BoxBody<Bytes, hyper::Error>>, ApiError> {
     255            0 :     let result = handle_inner(cancel, config, &ctx, request, backend).await;
     256              : 
     257            0 :     let mut response = match result {
     258            0 :         Ok(r) => {
     259            0 :             ctx.set_success();
     260            0 : 
     261            0 :             // Handling the error response from local proxy here
     262            0 :             if config.authentication_config.is_auth_broker && r.status().is_server_error() {
     263            0 :                 let status = r.status();
     264              : 
     265            0 :                 let body_bytes = r
     266            0 :                     .collect()
     267            0 :                     .await
     268            0 :                     .map_err(|e| {
     269            0 :                         ApiError::InternalServerError(anyhow::Error::msg(format!(
     270            0 :                             "could not collect http body: {e}"
     271            0 :                         )))
     272            0 :                     })?
     273            0 :                     .to_bytes();
     274              : 
     275            0 :                 if let Ok(mut json_map) =
     276            0 :                     serde_json::from_slice::<IndexMap<&str, &RawValue>>(&body_bytes)
     277              :                 {
     278            0 :                     let message = json_map.get("message");
     279            0 :                     if let Some(message) = message {
     280            0 :                         let msg: String = match serde_json::from_str(message.get()) {
     281            0 :                             Ok(msg) => msg,
     282              :                             Err(_) => {
     283            0 :                                 "Unable to parse the response message from server".to_string()
     284              :                             }
     285              :                         };
     286              : 
     287            0 :                         error!("Error response from local_proxy: {status} {msg}");
     288              : 
     289            0 :                         json_map.retain(|key, _| !key.starts_with("neon:")); // remove all the neon-related keys
     290            0 : 
     291            0 :                         let resp_json = serde_json::to_string(&json_map)
     292            0 :                             .unwrap_or("failed to serialize the response message".to_string());
     293            0 : 
     294            0 :                         return json_response(status, resp_json);
     295            0 :                     }
     296            0 :                 }
     297              : 
     298            0 :                 error!("Unable to parse the response message from local_proxy");
     299            0 :                 return json_response(
     300            0 :                     status,
     301            0 :                     json!({ "message": "Unable to parse the response message from server".to_string() }),
     302            0 :                 );
     303            0 :             }
     304            0 :             r
     305              :         }
     306            0 :         Err(e @ SqlOverHttpError::Cancelled(_)) => {
     307            0 :             let error_kind = e.get_error_kind();
     308            0 :             ctx.set_error_kind(error_kind);
     309            0 : 
     310            0 :             let message = "Query cancelled, connection was terminated";
     311            0 : 
     312            0 :             tracing::info!(
     313            0 :                 kind=error_kind.to_metric_label(),
     314            0 :                 error=%e,
     315            0 :                 msg=message,
     316            0 :                 "forwarding error to user"
     317              :             );
     318              : 
     319            0 :             json_response(
     320            0 :                 StatusCode::BAD_REQUEST,
     321            0 :                 json!({ "message": message, "code": SqlState::PROTOCOL_VIOLATION.code() }),
     322            0 :             )?
     323              :         }
     324            0 :         Err(e) => {
     325            0 :             let error_kind = e.get_error_kind();
     326            0 :             ctx.set_error_kind(error_kind);
     327            0 : 
     328            0 :             let mut message = e.to_string_client();
     329            0 :             let db_error = match &e {
     330            0 :                 SqlOverHttpError::ConnectCompute(HttpConnError::PostgresConnectionError(e))
     331            0 :                 | SqlOverHttpError::Postgres(e) => e.as_db_error(),
     332            0 :                 _ => None,
     333              :             };
     334            0 :             fn get<'a, T: Default>(db: Option<&'a DbError>, x: impl FnOnce(&'a DbError) -> T) -> T {
     335            0 :                 db.map(x).unwrap_or_default()
     336            0 :             }
     337              : 
     338            0 :             if let Some(db_error) = db_error {
     339            0 :                 db_error.message().clone_into(&mut message);
     340            0 :             }
     341              : 
     342            0 :             let position = db_error.and_then(|db| db.position());
     343            0 :             let (position, internal_position, internal_query) = match position {
     344            0 :                 Some(ErrorPosition::Original(position)) => (Some(position.to_string()), None, None),
     345            0 :                 Some(ErrorPosition::Internal { position, query }) => {
     346            0 :                     (None, Some(position.to_string()), Some(query.clone()))
     347              :                 }
     348            0 :                 None => (None, None, None),
     349              :             };
     350              : 
     351            0 :             let code = get(db_error, |db| db.code().code());
     352            0 :             let severity = get(db_error, |db| db.severity());
     353            0 :             let detail = get(db_error, |db| db.detail());
     354            0 :             let hint = get(db_error, |db| db.hint());
     355            0 :             let where_ = get(db_error, |db| db.where_());
     356            0 :             let table = get(db_error, |db| db.table());
     357            0 :             let column = get(db_error, |db| db.column());
     358            0 :             let schema = get(db_error, |db| db.schema());
     359            0 :             let datatype = get(db_error, |db| db.datatype());
     360            0 :             let constraint = get(db_error, |db| db.constraint());
     361            0 :             let file = get(db_error, |db| db.file());
     362            0 :             let line = get(db_error, |db| db.line().map(|l| l.to_string()));
     363            0 :             let routine = get(db_error, |db| db.routine());
     364            0 : 
     365            0 :             tracing::info!(
     366            0 :                 kind=error_kind.to_metric_label(),
     367            0 :                 error=%e,
     368            0 :                 msg=message,
     369            0 :                 "forwarding error to user"
     370              :             );
     371              : 
     372            0 :             json_response(
     373            0 :                 e.get_http_status_code(),
     374            0 :                 json!({
     375            0 :                     "message": message,
     376            0 :                     "code": code,
     377            0 :                     "detail": detail,
     378            0 :                     "hint": hint,
     379            0 :                     "position": position,
     380            0 :                     "internalPosition": internal_position,
     381            0 :                     "internalQuery": internal_query,
     382            0 :                     "severity": severity,
     383            0 :                     "where": where_,
     384            0 :                     "table": table,
     385            0 :                     "column": column,
     386            0 :                     "schema": schema,
     387            0 :                     "dataType": datatype,
     388            0 :                     "constraint": constraint,
     389            0 :                     "file": file,
     390            0 :                     "line": line,
     391            0 :                     "routine": routine,
     392            0 :                 }),
     393            0 :             )?
     394              :         }
     395              :     };
     396              : 
     397            0 :     response
     398            0 :         .headers_mut()
     399            0 :         .insert("Access-Control-Allow-Origin", HeaderValue::from_static("*"));
     400            0 :     Ok(response)
     401            0 : }
     402              : 
     403              : #[derive(Debug, thiserror::Error)]
     404              : pub(crate) enum SqlOverHttpError {
     405              :     #[error("{0}")]
     406              :     ReadPayload(#[from] ReadPayloadError),
     407              :     #[error("{0}")]
     408              :     ConnectCompute(#[from] HttpConnError),
     409              :     #[error("{0}")]
     410              :     ConnInfo(#[from] ConnInfoError),
     411              :     #[error("response is too large (max is {0} bytes)")]
     412              :     ResponseTooLarge(usize),
     413              :     #[error("invalid isolation level")]
     414              :     InvalidIsolationLevel,
     415              :     /// for queries our customers choose to run
     416              :     #[error("{0}")]
     417              :     Postgres(#[source] postgres_client::Error),
     418              :     /// for queries we choose to run
     419              :     #[error("{0}")]
     420              :     InternalPostgres(#[source] postgres_client::Error),
     421              :     #[error("{0}")]
     422              :     JsonConversion(#[from] JsonConversionError),
     423              :     #[error("{0}")]
     424              :     Cancelled(SqlOverHttpCancel),
     425              : }
     426              : 
     427              : impl ReportableError for SqlOverHttpError {
     428            0 :     fn get_error_kind(&self) -> ErrorKind {
     429            0 :         match self {
     430            0 :             SqlOverHttpError::ReadPayload(e) => e.get_error_kind(),
     431            0 :             SqlOverHttpError::ConnectCompute(e) => e.get_error_kind(),
     432            0 :             SqlOverHttpError::ConnInfo(e) => e.get_error_kind(),
     433            0 :             SqlOverHttpError::ResponseTooLarge(_) => ErrorKind::User,
     434            0 :             SqlOverHttpError::InvalidIsolationLevel => ErrorKind::User,
     435            0 :             SqlOverHttpError::Postgres(p) => p.get_error_kind(),
     436            0 :             SqlOverHttpError::InternalPostgres(p) => {
     437            0 :                 if p.as_db_error().is_some() {
     438            0 :                     ErrorKind::Service
     439              :                 } else {
     440            0 :                     ErrorKind::Compute
     441              :                 }
     442              :             }
     443            0 :             SqlOverHttpError::JsonConversion(_) => ErrorKind::Postgres,
     444            0 :             SqlOverHttpError::Cancelled(c) => c.get_error_kind(),
     445              :         }
     446            0 :     }
     447              : }
     448              : 
     449              : impl UserFacingError for SqlOverHttpError {
     450            0 :     fn to_string_client(&self) -> String {
     451            0 :         match self {
     452            0 :             SqlOverHttpError::ReadPayload(p) => p.to_string(),
     453            0 :             SqlOverHttpError::ConnectCompute(c) => c.to_string_client(),
     454            0 :             SqlOverHttpError::ConnInfo(c) => c.to_string_client(),
     455            0 :             SqlOverHttpError::ResponseTooLarge(_) => self.to_string(),
     456            0 :             SqlOverHttpError::InvalidIsolationLevel => self.to_string(),
     457            0 :             SqlOverHttpError::Postgres(p) => p.to_string(),
     458            0 :             SqlOverHttpError::InternalPostgres(p) => p.to_string(),
     459            0 :             SqlOverHttpError::JsonConversion(_) => "could not parse postgres response".to_string(),
     460            0 :             SqlOverHttpError::Cancelled(_) => self.to_string(),
     461              :         }
     462            0 :     }
     463              : }
     464              : 
     465              : impl HttpCodeError for SqlOverHttpError {
     466            0 :     fn get_http_status_code(&self) -> StatusCode {
     467            0 :         match self {
     468            0 :             SqlOverHttpError::ReadPayload(e) => e.get_http_status_code(),
     469            0 :             SqlOverHttpError::ConnectCompute(h) => match h.get_error_kind() {
     470            0 :                 ErrorKind::User => StatusCode::BAD_REQUEST,
     471            0 :                 _ => StatusCode::INTERNAL_SERVER_ERROR,
     472              :             },
     473            0 :             SqlOverHttpError::ConnInfo(_) => StatusCode::BAD_REQUEST,
     474            0 :             SqlOverHttpError::ResponseTooLarge(_) => StatusCode::INSUFFICIENT_STORAGE,
     475            0 :             SqlOverHttpError::InvalidIsolationLevel => StatusCode::BAD_REQUEST,
     476            0 :             SqlOverHttpError::Postgres(_) => StatusCode::BAD_REQUEST,
     477            0 :             SqlOverHttpError::InternalPostgres(_) => StatusCode::INTERNAL_SERVER_ERROR,
     478            0 :             SqlOverHttpError::JsonConversion(_) => StatusCode::INTERNAL_SERVER_ERROR,
     479            0 :             SqlOverHttpError::Cancelled(_) => StatusCode::INTERNAL_SERVER_ERROR,
     480              :         }
     481            0 :     }
     482              : }
     483              : 
     484              : #[derive(Debug, thiserror::Error)]
     485              : pub(crate) enum ReadPayloadError {
     486              :     #[error("could not read the HTTP request body: {0}")]
     487              :     Read(#[from] hyper::Error),
     488              :     #[error("request is too large (max is {limit} bytes)")]
     489              :     BodyTooLarge { limit: usize },
     490              :     #[error("could not parse the HTTP request body: {0}")]
     491              :     Parse(#[from] serde_json::Error),
     492              : }
     493              : 
     494              : impl From<ReadBodyError<hyper::Error>> for ReadPayloadError {
     495            0 :     fn from(value: ReadBodyError<hyper::Error>) -> Self {
     496            0 :         match value {
     497            0 :             ReadBodyError::BodyTooLarge { limit } => Self::BodyTooLarge { limit },
     498            0 :             ReadBodyError::Read(e) => Self::Read(e),
     499              :         }
     500            0 :     }
     501              : }
     502              : 
     503              : impl ReportableError for ReadPayloadError {
     504            0 :     fn get_error_kind(&self) -> ErrorKind {
     505            0 :         match self {
     506            0 :             ReadPayloadError::Read(_) => ErrorKind::ClientDisconnect,
     507            0 :             ReadPayloadError::BodyTooLarge { .. } => ErrorKind::User,
     508            0 :             ReadPayloadError::Parse(_) => ErrorKind::User,
     509              :         }
     510            0 :     }
     511              : }
     512              : 
     513              : impl HttpCodeError for ReadPayloadError {
     514            0 :     fn get_http_status_code(&self) -> StatusCode {
     515            0 :         match self {
     516            0 :             ReadPayloadError::Read(_) => StatusCode::BAD_REQUEST,
     517            0 :             ReadPayloadError::BodyTooLarge { .. } => StatusCode::PAYLOAD_TOO_LARGE,
     518            0 :             ReadPayloadError::Parse(_) => StatusCode::BAD_REQUEST,
     519              :         }
     520            0 :     }
     521              : }
     522              : 
     523              : #[derive(Debug, thiserror::Error)]
     524              : pub(crate) enum SqlOverHttpCancel {
     525              :     #[error("query was cancelled")]
     526              :     Postgres,
     527              :     #[error("query was cancelled while stuck trying to connect to the database")]
     528              :     Connect,
     529              : }
     530              : 
     531              : impl ReportableError for SqlOverHttpCancel {
     532            0 :     fn get_error_kind(&self) -> ErrorKind {
     533            0 :         match self {
     534            0 :             SqlOverHttpCancel::Postgres => ErrorKind::ClientDisconnect,
     535            0 :             SqlOverHttpCancel::Connect => ErrorKind::ClientDisconnect,
     536              :         }
     537            0 :     }
     538              : }
     539              : 
     540              : #[derive(Clone, Copy, Debug)]
     541              : struct HttpHeaders {
     542              :     raw_output: bool,
     543              :     default_array_mode: bool,
     544              :     txn_isolation_level: Option<IsolationLevel>,
     545              :     txn_read_only: bool,
     546              :     txn_deferrable: bool,
     547              : }
     548              : 
     549              : impl HttpHeaders {
     550            0 :     fn try_parse(headers: &hyper::http::HeaderMap) -> Result<Self, SqlOverHttpError> {
     551            0 :         // Determine the output options. Default behaviour is 'false'. Anything that is not
     552            0 :         // strictly 'true' assumed to be false.
     553            0 :         let raw_output = headers.get(&RAW_TEXT_OUTPUT) == Some(&HEADER_VALUE_TRUE);
     554            0 :         let default_array_mode = headers.get(&ARRAY_MODE) == Some(&HEADER_VALUE_TRUE);
     555              : 
     556              :         // isolation level, read only and deferrable
     557            0 :         let txn_isolation_level = match headers.get(&TXN_ISOLATION_LEVEL) {
     558            0 :             Some(x) => Some(
     559            0 :                 map_header_to_isolation_level(x).ok_or(SqlOverHttpError::InvalidIsolationLevel)?,
     560              :             ),
     561            0 :             None => None,
     562              :         };
     563              : 
     564            0 :         let txn_read_only = headers.get(&TXN_READ_ONLY) == Some(&HEADER_VALUE_TRUE);
     565            0 :         let txn_deferrable = headers.get(&TXN_DEFERRABLE) == Some(&HEADER_VALUE_TRUE);
     566            0 : 
     567            0 :         Ok(Self {
     568            0 :             raw_output,
     569            0 :             default_array_mode,
     570            0 :             txn_isolation_level,
     571            0 :             txn_read_only,
     572            0 :             txn_deferrable,
     573            0 :         })
     574            0 :     }
     575              : }
     576              : 
     577            0 : fn map_header_to_isolation_level(level: &HeaderValue) -> Option<IsolationLevel> {
     578            0 :     match level.as_bytes() {
     579            0 :         b"Serializable" => Some(IsolationLevel::Serializable),
     580            0 :         b"ReadUncommitted" => Some(IsolationLevel::ReadUncommitted),
     581            0 :         b"ReadCommitted" => Some(IsolationLevel::ReadCommitted),
     582            0 :         b"RepeatableRead" => Some(IsolationLevel::RepeatableRead),
     583            0 :         _ => None,
     584              :     }
     585            0 : }
     586              : 
     587            0 : fn map_isolation_level_to_headers(level: IsolationLevel) -> Option<HeaderValue> {
     588            0 :     match level {
     589            0 :         IsolationLevel::ReadUncommitted => Some(HeaderValue::from_static("ReadUncommitted")),
     590            0 :         IsolationLevel::ReadCommitted => Some(HeaderValue::from_static("ReadCommitted")),
     591            0 :         IsolationLevel::RepeatableRead => Some(HeaderValue::from_static("RepeatableRead")),
     592            0 :         IsolationLevel::Serializable => Some(HeaderValue::from_static("Serializable")),
     593            0 :         _ => None,
     594              :     }
     595            0 : }
     596              : 
     597            0 : async fn handle_inner(
     598            0 :     cancel: CancellationToken,
     599            0 :     config: &'static ProxyConfig,
     600            0 :     ctx: &RequestContext,
     601            0 :     request: Request<Incoming>,
     602            0 :     backend: Arc<PoolingBackend>,
     603            0 : ) -> Result<Response<BoxBody<Bytes, hyper::Error>>, SqlOverHttpError> {
     604            0 :     let _requeset_gauge = Metrics::get()
     605            0 :         .proxy
     606            0 :         .connection_requests
     607            0 :         .guard(ctx.protocol());
     608            0 :     info!(
     609            0 :         protocol = %ctx.protocol(),
     610            0 :         "handling interactive connection from client"
     611              :     );
     612              : 
     613            0 :     let conn_info = get_conn_info(
     614            0 :         &config.authentication_config,
     615            0 :         ctx,
     616            0 :         request.headers(),
     617            0 :         config.tls_config.as_ref(),
     618            0 :     )?;
     619            0 :     info!(
     620            0 :         user = conn_info.conn_info.user_info.user.as_str(),
     621            0 :         "credentials"
     622              :     );
     623              : 
     624            0 :     match conn_info.auth {
     625            0 :         AuthData::Jwt(jwt) if config.authentication_config.is_auth_broker => {
     626            0 :             handle_auth_broker_inner(ctx, request, conn_info.conn_info, jwt, backend).await
     627              :         }
     628            0 :         auth => {
     629            0 :             handle_db_inner(
     630            0 :                 cancel,
     631            0 :                 config,
     632            0 :                 ctx,
     633            0 :                 request,
     634            0 :                 conn_info.conn_info,
     635            0 :                 auth,
     636            0 :                 backend,
     637            0 :             )
     638            0 :             .await
     639              :         }
     640              :     }
     641            0 : }
     642              : 
     643            0 : async fn handle_db_inner(
     644            0 :     cancel: CancellationToken,
     645            0 :     config: &'static ProxyConfig,
     646            0 :     ctx: &RequestContext,
     647            0 :     request: Request<Incoming>,
     648            0 :     conn_info: ConnInfo,
     649            0 :     auth: AuthData,
     650            0 :     backend: Arc<PoolingBackend>,
     651            0 : ) -> Result<Response<BoxBody<Bytes, hyper::Error>>, SqlOverHttpError> {
     652            0 :     //
     653            0 :     // Determine the destination and connection params
     654            0 :     //
     655            0 :     let headers = request.headers();
     656              : 
     657              :     // Allow connection pooling only if explicitly requested
     658              :     // or if we have decided that http pool is no longer opt-in
     659            0 :     let allow_pool = !config.http_config.pool_options.opt_in
     660            0 :         || headers.get(&ALLOW_POOL) == Some(&HEADER_VALUE_TRUE);
     661              : 
     662            0 :     let parsed_headers = HttpHeaders::try_parse(headers)?;
     663              : 
     664            0 :     let fetch_and_process_request = Box::pin(
     665            0 :         async {
     666            0 :             let body = read_body_with_limit(
     667            0 :                 request.into_body(),
     668            0 :                 config.http_config.max_request_size_bytes,
     669            0 :             )
     670            0 :             .await?;
     671              : 
     672            0 :             Metrics::get()
     673            0 :                 .proxy
     674            0 :                 .http_conn_content_length_bytes
     675            0 :                 .observe(HttpDirection::Request, body.len() as f64);
     676            0 : 
     677            0 :             debug!(length = body.len(), "request payload read");
     678            0 :             let payload: Payload = serde_json::from_slice(&body)?;
     679            0 :             Ok::<Payload, ReadPayloadError>(payload) // Adjust error type accordingly
     680            0 :         }
     681            0 :         .map_err(SqlOverHttpError::from),
     682            0 :     );
     683            0 : 
     684            0 :     let authenticate_and_connect = Box::pin(
     685            0 :         async {
     686            0 :             let keys = match auth {
     687            0 :                 AuthData::Password(pw) => backend
     688            0 :                     .authenticate_with_password(ctx, &conn_info.user_info, &pw)
     689            0 :                     .await
     690            0 :                     .map_err(HttpConnError::AuthError)?,
     691            0 :                 AuthData::Jwt(jwt) => backend
     692            0 :                     .authenticate_with_jwt(ctx, &conn_info.user_info, jwt)
     693            0 :                     .await
     694            0 :                     .map_err(HttpConnError::AuthError)?,
     695              :             };
     696              : 
     697            0 :             let client = match keys.keys {
     698            0 :                 ComputeCredentialKeys::JwtPayload(payload)
     699            0 :                     if backend.auth_backend.is_local_proxy() =>
     700              :                 {
     701            0 :                     let mut client = backend.connect_to_local_postgres(ctx, conn_info).await?;
     702            0 :                     let (cli_inner, _dsc) = client.client_inner();
     703            0 :                     cli_inner.set_jwt_session(&payload).await?;
     704            0 :                     Client::Local(client)
     705              :                 }
     706              :                 _ => {
     707            0 :                     let client = backend
     708            0 :                         .connect_to_compute(ctx, conn_info, keys, !allow_pool)
     709            0 :                         .await?;
     710            0 :                     Client::Remote(client)
     711              :                 }
     712              :             };
     713              : 
     714              :             // not strictly necessary to mark success here,
     715              :             // but it's just insurance for if we forget it somewhere else
     716            0 :             ctx.success();
     717            0 :             Ok::<_, SqlOverHttpError>(client)
     718            0 :         }
     719            0 :         .map_err(SqlOverHttpError::from),
     720            0 :     );
     721              : 
     722            0 :     let (payload, mut client) = match run_until_cancelled(
     723            0 :         // Run both operations in parallel
     724            0 :         try_join(
     725            0 :             pin!(fetch_and_process_request),
     726            0 :             pin!(authenticate_and_connect),
     727            0 :         ),
     728            0 :         &cancel,
     729            0 :     )
     730            0 :     .await
     731              :     {
     732            0 :         Some(result) => result?,
     733            0 :         None => return Err(SqlOverHttpError::Cancelled(SqlOverHttpCancel::Connect)),
     734              :     };
     735              : 
     736            0 :     let mut response = Response::builder()
     737            0 :         .status(StatusCode::OK)
     738            0 :         .header(header::CONTENT_TYPE, "application/json");
     739              : 
     740              :     // Now execute the query and return the result.
     741            0 :     let json_output = match payload {
     742            0 :         Payload::Single(stmt) => {
     743            0 :             stmt.process(&config.http_config, cancel, &mut client, parsed_headers)
     744            0 :                 .await?
     745              :         }
     746            0 :         Payload::Batch(statements) => {
     747            0 :             if parsed_headers.txn_read_only {
     748            0 :                 response = response.header(TXN_READ_ONLY.clone(), &HEADER_VALUE_TRUE);
     749            0 :             }
     750            0 :             if parsed_headers.txn_deferrable {
     751            0 :                 response = response.header(TXN_DEFERRABLE.clone(), &HEADER_VALUE_TRUE);
     752            0 :             }
     753            0 :             if let Some(txn_isolation_level) = parsed_headers
     754            0 :                 .txn_isolation_level
     755            0 :                 .and_then(map_isolation_level_to_headers)
     756            0 :             {
     757            0 :                 response = response.header(TXN_ISOLATION_LEVEL.clone(), txn_isolation_level);
     758            0 :             }
     759              : 
     760            0 :             statements
     761            0 :                 .process(&config.http_config, cancel, &mut client, parsed_headers)
     762            0 :                 .await?
     763              :         }
     764              :     };
     765              : 
     766            0 :     let metrics = client.metrics(TrafficDirection::Egress, ctx);
     767            0 : 
     768            0 :     let len = json_output.len();
     769            0 :     let response = response
     770            0 :         .body(
     771            0 :             Full::new(Bytes::from(json_output))
     772            0 :                 .map_err(|x| match x {})
     773            0 :                 .boxed(),
     774            0 :         )
     775            0 :         // only fails if invalid status code or invalid header/values are given.
     776            0 :         // these are not user configurable so it cannot fail dynamically
     777            0 :         .expect("building response payload should not fail");
     778            0 : 
     779            0 :     // count the egress bytes - we miss the TLS and header overhead but oh well...
     780            0 :     // moving this later in the stack is going to be a lot of effort and ehhhh
     781            0 :     metrics.record_egress(len as u64);
     782            0 :     Metrics::get()
     783            0 :         .proxy
     784            0 :         .http_conn_content_length_bytes
     785            0 :         .observe(HttpDirection::Response, len as f64);
     786            0 : 
     787            0 :     Ok(response)
     788            0 : }
     789              : 
     790              : static HEADERS_TO_FORWARD: &[&HeaderName] = &[
     791              :     &AUTHORIZATION,
     792              :     &CONN_STRING,
     793              :     &RAW_TEXT_OUTPUT,
     794              :     &ARRAY_MODE,
     795              :     &TXN_ISOLATION_LEVEL,
     796              :     &TXN_READ_ONLY,
     797              :     &TXN_DEFERRABLE,
     798              : ];
     799              : 
     800            0 : pub(crate) fn uuid_to_header_value(id: Uuid) -> HeaderValue {
     801            0 :     let mut uuid = [0; uuid::fmt::Hyphenated::LENGTH];
     802            0 :     HeaderValue::from_str(id.as_hyphenated().encode_lower(&mut uuid[..]))
     803            0 :         .expect("uuid hyphenated format should be all valid header characters")
     804            0 : }
     805              : 
     806            0 : async fn handle_auth_broker_inner(
     807            0 :     ctx: &RequestContext,
     808            0 :     request: Request<Incoming>,
     809            0 :     conn_info: ConnInfo,
     810            0 :     jwt: String,
     811            0 :     backend: Arc<PoolingBackend>,
     812            0 : ) -> Result<Response<BoxBody<Bytes, hyper::Error>>, SqlOverHttpError> {
     813            0 :     backend
     814            0 :         .authenticate_with_jwt(ctx, &conn_info.user_info, jwt)
     815            0 :         .await
     816            0 :         .map_err(HttpConnError::from)?;
     817              : 
     818            0 :     let mut client = backend.connect_to_local_proxy(ctx, conn_info).await?;
     819              : 
     820            0 :     let local_proxy_uri = ::http::Uri::from_static("http://proxy.local/sql");
     821            0 : 
     822            0 :     let (mut parts, body) = request.into_parts();
     823            0 :     let mut req = Request::builder().method(Method::POST).uri(local_proxy_uri);
     824              : 
     825              :     // todo(conradludgate): maybe auth-broker should parse these and re-serialize
     826              :     // these instead just to ensure they remain normalised.
     827            0 :     for &h in HEADERS_TO_FORWARD {
     828            0 :         if let Some(hv) = parts.headers.remove(h) {
     829            0 :             req = req.header(h, hv);
     830            0 :         }
     831              :     }
     832            0 :     req = req.header(&NEON_REQUEST_ID, uuid_to_header_value(ctx.session_id()));
     833            0 : 
     834            0 :     let req = req
     835            0 :         .body(body)
     836            0 :         .expect("all headers and params received via hyper should be valid for request");
     837            0 : 
     838            0 :     // todo: map body to count egress
     839            0 :     let _metrics = client.metrics(TrafficDirection::Egress, ctx);
     840            0 : 
     841            0 :     Ok(client
     842            0 :         .inner
     843            0 :         .inner
     844            0 :         .send_request(req)
     845            0 :         .await
     846            0 :         .map_err(LocalProxyConnError::from)
     847            0 :         .map_err(HttpConnError::from)?
     848            0 :         .map(|b| b.boxed()))
     849            0 : }
     850              : 
     851              : impl QueryData {
     852            0 :     async fn process(
     853            0 :         self,
     854            0 :         config: &'static HttpConfig,
     855            0 :         cancel: CancellationToken,
     856            0 :         client: &mut Client,
     857            0 :         parsed_headers: HttpHeaders,
     858            0 :     ) -> Result<String, SqlOverHttpError> {
     859            0 :         let (inner, mut discard) = client.inner();
     860            0 :         let cancel_token = inner.cancel_token();
     861              : 
     862            0 :         let res = match select(
     863            0 :             pin!(query_to_json(
     864            0 :                 config,
     865            0 :                 &mut *inner,
     866            0 :                 self,
     867            0 :                 &mut 0,
     868            0 :                 parsed_headers
     869            0 :             )),
     870            0 :             pin!(cancel.cancelled()),
     871            0 :         )
     872            0 :         .await
     873              :         {
     874              :             // The query successfully completed.
     875            0 :             Either::Left((Ok((status, results)), __not_yet_cancelled)) => {
     876            0 :                 discard.check_idle(status);
     877            0 : 
     878            0 :                 let json_output =
     879            0 :                     serde_json::to_string(&results).expect("json serialization should not fail");
     880            0 :                 Ok(json_output)
     881              :             }
     882              :             // The query failed with an error
     883            0 :             Either::Left((Err(e), __not_yet_cancelled)) => {
     884            0 :                 discard.discard();
     885            0 :                 return Err(e);
     886              :             }
     887              :             // The query was cancelled.
     888            0 :             Either::Right((_cancelled, query)) => {
     889            0 :                 tracing::info!("cancelling query");
     890            0 :                 if let Err(err) = cancel_token.cancel_query(NoTls).await {
     891            0 :                     tracing::warn!(?err, "could not cancel query");
     892            0 :                 }
     893              :                 // wait for the query cancellation
     894            0 :                 match time::timeout(time::Duration::from_millis(100), query).await {
     895              :                     // query successed before it was cancelled.
     896            0 :                     Ok(Ok((status, results))) => {
     897            0 :                         discard.check_idle(status);
     898            0 : 
     899            0 :                         let json_output = serde_json::to_string(&results)
     900            0 :                             .expect("json serialization should not fail");
     901            0 :                         Ok(json_output)
     902              :                     }
     903              :                     // query failed or was cancelled.
     904            0 :                     Ok(Err(error)) => {
     905            0 :                         let db_error = match &error {
     906              :                             SqlOverHttpError::ConnectCompute(
     907            0 :                                 HttpConnError::PostgresConnectionError(e),
     908              :                             )
     909            0 :                             | SqlOverHttpError::Postgres(e) => e.as_db_error(),
     910            0 :                             _ => None,
     911              :                         };
     912              : 
     913              :                         // if errored for some other reason, it might not be safe to return
     914            0 :                         if !db_error.is_some_and(|e| *e.code() == SqlState::QUERY_CANCELED) {
     915            0 :                             discard.discard();
     916            0 :                         }
     917              : 
     918            0 :                         Err(SqlOverHttpError::Cancelled(SqlOverHttpCancel::Postgres))
     919              :                     }
     920            0 :                     Err(_timeout) => {
     921            0 :                         discard.discard();
     922            0 :                         Err(SqlOverHttpError::Cancelled(SqlOverHttpCancel::Postgres))
     923              :                     }
     924              :                 }
     925              :             }
     926              :         };
     927            0 :         res
     928            0 :     }
     929              : }
     930              : 
     931              : impl BatchQueryData {
     932            0 :     async fn process(
     933            0 :         self,
     934            0 :         config: &'static HttpConfig,
     935            0 :         cancel: CancellationToken,
     936            0 :         client: &mut Client,
     937            0 :         parsed_headers: HttpHeaders,
     938            0 :     ) -> Result<String, SqlOverHttpError> {
     939            0 :         info!("starting transaction");
     940            0 :         let (inner, mut discard) = client.inner();
     941            0 :         let cancel_token = inner.cancel_token();
     942            0 :         let mut builder = inner.build_transaction();
     943            0 :         if let Some(isolation_level) = parsed_headers.txn_isolation_level {
     944            0 :             builder = builder.isolation_level(isolation_level);
     945            0 :         }
     946            0 :         if parsed_headers.txn_read_only {
     947            0 :             builder = builder.read_only(true);
     948            0 :         }
     949            0 :         if parsed_headers.txn_deferrable {
     950            0 :             builder = builder.deferrable(true);
     951            0 :         }
     952              : 
     953            0 :         let mut transaction = builder
     954            0 :             .start()
     955            0 :             .await
     956            0 :             .inspect_err(|_| {
     957            0 :                 // if we cannot start a transaction, we should return immediately
     958            0 :                 // and not return to the pool. connection is clearly broken
     959            0 :                 discard.discard();
     960            0 :             })
     961            0 :             .map_err(SqlOverHttpError::Postgres)?;
     962              : 
     963            0 :         let json_output = match query_batch(
     964            0 :             config,
     965            0 :             cancel.child_token(),
     966            0 :             &mut transaction,
     967            0 :             self,
     968            0 :             parsed_headers,
     969            0 :         )
     970            0 :         .await
     971              :         {
     972            0 :             Ok(json_output) => {
     973            0 :                 info!("commit");
     974            0 :                 let status = transaction
     975            0 :                     .commit()
     976            0 :                     .await
     977            0 :                     .inspect_err(|_| {
     978            0 :                         // if we cannot commit - for now don't return connection to pool
     979            0 :                         // TODO: get a query status from the error
     980            0 :                         discard.discard();
     981            0 :                     })
     982            0 :                     .map_err(SqlOverHttpError::Postgres)?;
     983            0 :                 discard.check_idle(status);
     984            0 :                 json_output
     985              :             }
     986              :             Err(SqlOverHttpError::Cancelled(_)) => {
     987            0 :                 if let Err(err) = cancel_token.cancel_query(NoTls).await {
     988            0 :                     tracing::warn!(?err, "could not cancel query");
     989            0 :                 }
     990              :                 // TODO: after cancelling, wait to see if we can get a status. maybe the connection is still safe.
     991            0 :                 discard.discard();
     992            0 : 
     993            0 :                 return Err(SqlOverHttpError::Cancelled(SqlOverHttpCancel::Postgres));
     994              :             }
     995            0 :             Err(err) => {
     996            0 :                 info!("rollback");
     997            0 :                 let status = transaction
     998            0 :                     .rollback()
     999            0 :                     .await
    1000            0 :                     .inspect_err(|_| {
    1001            0 :                         // if we cannot rollback - for now don't return connection to pool
    1002            0 :                         // TODO: get a query status from the error
    1003            0 :                         discard.discard();
    1004            0 :                     })
    1005            0 :                     .map_err(SqlOverHttpError::Postgres)?;
    1006            0 :                 discard.check_idle(status);
    1007            0 :                 return Err(err);
    1008              :             }
    1009              :         };
    1010              : 
    1011            0 :         Ok(json_output)
    1012            0 :     }
    1013              : }
    1014              : 
    1015            0 : async fn query_batch(
    1016            0 :     config: &'static HttpConfig,
    1017            0 :     cancel: CancellationToken,
    1018            0 :     transaction: &mut Transaction<'_>,
    1019            0 :     queries: BatchQueryData,
    1020            0 :     parsed_headers: HttpHeaders,
    1021            0 : ) -> Result<String, SqlOverHttpError> {
    1022            0 :     let mut results = Vec::with_capacity(queries.queries.len());
    1023            0 :     let mut current_size = 0;
    1024            0 :     for stmt in queries.queries {
    1025            0 :         let query = pin!(query_to_json(
    1026            0 :             config,
    1027            0 :             transaction,
    1028            0 :             stmt,
    1029            0 :             &mut current_size,
    1030            0 :             parsed_headers,
    1031            0 :         ));
    1032            0 :         let cancelled = pin!(cancel.cancelled());
    1033            0 :         let res = select(query, cancelled).await;
    1034            0 :         match res {
    1035              :             // TODO: maybe we should check that the transaction bit is set here
    1036            0 :             Either::Left((Ok((_, values)), _cancelled)) => {
    1037            0 :                 results.push(values);
    1038            0 :             }
    1039            0 :             Either::Left((Err(e), _cancelled)) => {
    1040            0 :                 return Err(e);
    1041              :             }
    1042            0 :             Either::Right((_cancelled, _)) => {
    1043            0 :                 return Err(SqlOverHttpError::Cancelled(SqlOverHttpCancel::Postgres));
    1044              :             }
    1045              :         }
    1046              :     }
    1047              : 
    1048            0 :     let results = json!({ "results": results });
    1049            0 :     let json_output = serde_json::to_string(&results).expect("json serialization should not fail");
    1050            0 : 
    1051            0 :     Ok(json_output)
    1052            0 : }
    1053              : 
    1054            0 : async fn query_to_json<T: GenericClient>(
    1055            0 :     config: &'static HttpConfig,
    1056            0 :     client: &mut T,
    1057            0 :     data: QueryData,
    1058            0 :     current_size: &mut usize,
    1059            0 :     parsed_headers: HttpHeaders,
    1060            0 : ) -> Result<(ReadyForQueryStatus, impl Serialize + use<T>), SqlOverHttpError> {
    1061            0 :     let query_start = Instant::now();
    1062            0 : 
    1063            0 :     let query_params = data.params;
    1064            0 :     let mut row_stream = std::pin::pin!(
    1065            0 :         client
    1066            0 :             .query_raw_txt(&data.query, query_params)
    1067            0 :             .await
    1068            0 :             .map_err(SqlOverHttpError::Postgres)?
    1069              :     );
    1070            0 :     let query_acknowledged = Instant::now();
    1071            0 : 
    1072            0 :     // Manually drain the stream into a vector to leave row_stream hanging
    1073            0 :     // around to get a command tag. Also check that the response is not too
    1074            0 :     // big.
    1075            0 :     let mut rows: Vec<postgres_client::Row> = Vec::new();
    1076            0 :     while let Some(row) = row_stream.next().await {
    1077            0 :         let row = row.map_err(SqlOverHttpError::Postgres)?;
    1078            0 :         *current_size += row.body_len();
    1079            0 :         rows.push(row);
    1080            0 :         // we don't have a streaming response support yet so this is to prevent OOM
    1081            0 :         // from a malicious query (eg a cross join)
    1082            0 :         if *current_size > config.max_response_size_bytes {
    1083            0 :             return Err(SqlOverHttpError::ResponseTooLarge(
    1084            0 :                 config.max_response_size_bytes,
    1085            0 :             ));
    1086            0 :         }
    1087              :     }
    1088              : 
    1089            0 :     let query_resp_end = Instant::now();
    1090            0 :     let ready = row_stream.ready_status();
    1091            0 : 
    1092            0 :     // grab the command tag and number of rows affected
    1093            0 :     let command_tag = row_stream.command_tag().unwrap_or_default();
    1094            0 :     let mut command_tag_split = command_tag.split(' ');
    1095            0 :     let command_tag_name = command_tag_split.next().unwrap_or_default();
    1096            0 :     let command_tag_count = if command_tag_name == "INSERT" {
    1097              :         // INSERT returns OID first and then number of rows
    1098            0 :         command_tag_split.nth(1)
    1099              :     } else {
    1100              :         // other commands return number of rows (if any)
    1101            0 :         command_tag_split.next()
    1102              :     }
    1103            0 :     .and_then(|s| s.parse::<i64>().ok());
    1104            0 : 
    1105            0 :     info!(
    1106            0 :         rows = rows.len(),
    1107            0 :         ?ready,
    1108            0 :         command_tag,
    1109            0 :         acknowledgement = ?(query_acknowledged - query_start),
    1110            0 :         response = ?(query_resp_end - query_start),
    1111            0 :         "finished executing query"
    1112              :     );
    1113              : 
    1114            0 :     let columns_len = row_stream.columns().len();
    1115            0 :     let mut fields = Vec::with_capacity(columns_len);
    1116            0 :     let mut columns = Vec::with_capacity(columns_len);
    1117              : 
    1118            0 :     for c in row_stream.columns() {
    1119            0 :         fields.push(json!({
    1120            0 :             "name": c.name().to_owned(),
    1121            0 :             "dataTypeID": c.type_().oid(),
    1122            0 :             "tableID": c.table_oid(),
    1123            0 :             "columnID": c.column_id(),
    1124            0 :             "dataTypeSize": c.type_size(),
    1125            0 :             "dataTypeModifier": c.type_modifier(),
    1126            0 :             "format": "text",
    1127            0 :         }));
    1128            0 : 
    1129            0 :         match client.get_type(c.type_oid()).await {
    1130            0 :             Ok(t) => columns.push(t),
    1131            0 :             Err(err) => {
    1132            0 :                 tracing::warn!(?err, "unable to query type information");
    1133            0 :                 return Err(SqlOverHttpError::InternalPostgres(err));
    1134              :             }
    1135              :         }
    1136              :     }
    1137              : 
    1138            0 :     let array_mode = data.array_mode.unwrap_or(parsed_headers.default_array_mode);
    1139              : 
    1140              :     // convert rows to JSON
    1141            0 :     let rows = rows
    1142            0 :         .iter()
    1143            0 :         .map(|row| pg_text_row_to_json(row, &columns, parsed_headers.raw_output, array_mode))
    1144            0 :         .collect::<Result<Vec<_>, _>>()?;
    1145              : 
    1146              :     // Resulting JSON format is based on the format of node-postgres result.
    1147            0 :     let results = json!({
    1148            0 :         "command": command_tag_name.to_string(),
    1149            0 :         "rowCount": command_tag_count,
    1150            0 :         "rows": rows,
    1151            0 :         "fields": fields,
    1152            0 :         "rowAsArray": array_mode,
    1153            0 :     });
    1154            0 : 
    1155            0 :     Ok((ready, results))
    1156            0 : }
    1157              : 
    1158              : enum Client {
    1159              :     Remote(conn_pool_lib::Client<postgres_client::Client>),
    1160              :     Local(conn_pool_lib::Client<postgres_client::Client>),
    1161              : }
    1162              : 
    1163              : enum Discard<'a> {
    1164              :     Remote(conn_pool_lib::Discard<'a, postgres_client::Client>),
    1165              :     Local(conn_pool_lib::Discard<'a, postgres_client::Client>),
    1166              : }
    1167              : 
    1168              : impl Client {
    1169            0 :     fn metrics(&self, direction: TrafficDirection, ctx: &RequestContext) -> Arc<MetricCounter> {
    1170            0 :         match self {
    1171            0 :             Client::Remote(client) => client.metrics(direction, ctx),
    1172            0 :             Client::Local(local_client) => local_client.metrics(direction, ctx),
    1173              :         }
    1174            0 :     }
    1175              : 
    1176            0 :     fn inner(&mut self) -> (&mut postgres_client::Client, Discard<'_>) {
    1177            0 :         match self {
    1178            0 :             Client::Remote(client) => {
    1179            0 :                 let (c, d) = client.inner();
    1180            0 :                 (c, Discard::Remote(d))
    1181              :             }
    1182            0 :             Client::Local(local_client) => {
    1183            0 :                 let (c, d) = local_client.inner();
    1184            0 :                 (c, Discard::Local(d))
    1185              :             }
    1186              :         }
    1187            0 :     }
    1188              : }
    1189              : 
    1190              : impl Discard<'_> {
    1191            0 :     fn check_idle(&mut self, status: ReadyForQueryStatus) {
    1192            0 :         match self {
    1193            0 :             Discard::Remote(discard) => discard.check_idle(status),
    1194            0 :             Discard::Local(discard) => discard.check_idle(status),
    1195              :         }
    1196            0 :     }
    1197            0 :     fn discard(&mut self) {
    1198            0 :         match self {
    1199            0 :             Discard::Remote(discard) => discard.discard(),
    1200            0 :             Discard::Local(discard) => discard.discard(),
    1201              :         }
    1202            0 :     }
    1203              : }
    1204              : 
    1205              : #[cfg(test)]
    1206              : #[expect(clippy::unwrap_used)]
    1207              : mod tests {
    1208              :     use super::*;
    1209              : 
    1210              :     #[test]
    1211            1 :     fn test_payload() {
    1212            1 :         let payload = "{\"query\":\"SELECT * FROM users WHERE name = ?\",\"params\":[\"test\"],\"arrayMode\":true}";
    1213            1 :         let deserialized_payload: Payload = serde_json::from_str(payload).unwrap();
    1214            1 : 
    1215            1 :         match deserialized_payload {
    1216              :             Payload::Single(QueryData {
    1217            1 :                 query,
    1218            1 :                 params,
    1219            1 :                 array_mode,
    1220            1 :             }) => {
    1221            1 :                 assert_eq!(query, "SELECT * FROM users WHERE name = ?");
    1222            1 :                 assert_eq!(params, vec![Some(String::from("test"))]);
    1223            1 :                 assert!(array_mode.unwrap());
    1224              :             }
    1225              :             Payload::Batch(_) => {
    1226            0 :                 panic!("deserialization failed: case with single query, one param, and array mode")
    1227              :             }
    1228              :         }
    1229              : 
    1230            1 :         let payload = "{\"queries\":[{\"query\":\"SELECT * FROM users0 WHERE name = ?\",\"params\":[\"test0\"], \"arrayMode\":false},{\"query\":\"SELECT * FROM users1 WHERE name = ?\",\"params\":[\"test1\"],\"arrayMode\":true}]}";
    1231            1 :         let deserialized_payload: Payload = serde_json::from_str(payload).unwrap();
    1232            1 : 
    1233            1 :         match deserialized_payload {
    1234            1 :             Payload::Batch(BatchQueryData { queries }) => {
    1235            1 :                 assert_eq!(queries.len(), 2);
    1236            2 :                 for (i, query) in queries.into_iter().enumerate() {
    1237            2 :                     assert_eq!(
    1238            2 :                         query.query,
    1239            2 :                         format!("SELECT * FROM users{i} WHERE name = ?")
    1240            2 :                     );
    1241            2 :                     assert_eq!(query.params, vec![Some(format!("test{i}"))]);
    1242            2 :                     assert_eq!(query.array_mode.unwrap(), i > 0);
    1243              :                 }
    1244              :             }
    1245            0 :             Payload::Single(_) => panic!("deserialization failed: case with multiple queries"),
    1246              :         }
    1247              : 
    1248            1 :         let payload = "{\"query\":\"SELECT 1\"}";
    1249            1 :         let deserialized_payload: Payload = serde_json::from_str(payload).unwrap();
    1250            1 : 
    1251            1 :         match deserialized_payload {
    1252              :             Payload::Single(QueryData {
    1253            1 :                 query,
    1254            1 :                 params,
    1255            1 :                 array_mode,
    1256            1 :             }) => {
    1257            1 :                 assert_eq!(query, "SELECT 1");
    1258            1 :                 assert_eq!(params, vec![]);
    1259            1 :                 assert!(array_mode.is_none());
    1260              :             }
    1261            0 :             Payload::Batch(_) => panic!("deserialization failed: case with only one query"),
    1262              :         }
    1263            1 :     }
    1264              : }
        

Generated by: LCOV version 2.1-beta