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 : // todo: race condition?
618 0 : // we're unlikely to change the common names.
619 0 : config.tls_config.load().as_deref(),
620 0 : )?;
621 0 : info!(
622 0 : user = conn_info.conn_info.user_info.user.as_str(),
623 0 : "credentials"
624 : );
625 :
626 0 : match conn_info.auth {
627 0 : AuthData::Jwt(jwt) if config.authentication_config.is_auth_broker => {
628 0 : handle_auth_broker_inner(ctx, request, conn_info.conn_info, jwt, backend).await
629 : }
630 0 : auth => {
631 0 : handle_db_inner(
632 0 : cancel,
633 0 : config,
634 0 : ctx,
635 0 : request,
636 0 : conn_info.conn_info,
637 0 : auth,
638 0 : backend,
639 0 : )
640 0 : .await
641 : }
642 : }
643 0 : }
644 :
645 0 : async fn handle_db_inner(
646 0 : cancel: CancellationToken,
647 0 : config: &'static ProxyConfig,
648 0 : ctx: &RequestContext,
649 0 : request: Request<Incoming>,
650 0 : conn_info: ConnInfo,
651 0 : auth: AuthData,
652 0 : backend: Arc<PoolingBackend>,
653 0 : ) -> Result<Response<BoxBody<Bytes, hyper::Error>>, SqlOverHttpError> {
654 0 : //
655 0 : // Determine the destination and connection params
656 0 : //
657 0 : let headers = request.headers();
658 :
659 : // Allow connection pooling only if explicitly requested
660 : // or if we have decided that http pool is no longer opt-in
661 0 : let allow_pool = !config.http_config.pool_options.opt_in
662 0 : || headers.get(&ALLOW_POOL) == Some(&HEADER_VALUE_TRUE);
663 :
664 0 : let parsed_headers = HttpHeaders::try_parse(headers)?;
665 :
666 0 : let fetch_and_process_request = Box::pin(
667 0 : async {
668 0 : let body = read_body_with_limit(
669 0 : request.into_body(),
670 0 : config.http_config.max_request_size_bytes,
671 0 : )
672 0 : .await?;
673 :
674 0 : Metrics::get()
675 0 : .proxy
676 0 : .http_conn_content_length_bytes
677 0 : .observe(HttpDirection::Request, body.len() as f64);
678 0 :
679 0 : debug!(length = body.len(), "request payload read");
680 0 : let payload: Payload = serde_json::from_slice(&body)?;
681 0 : Ok::<Payload, ReadPayloadError>(payload) // Adjust error type accordingly
682 0 : }
683 0 : .map_err(SqlOverHttpError::from),
684 0 : );
685 0 :
686 0 : let authenticate_and_connect = Box::pin(
687 0 : async {
688 0 : let keys = match auth {
689 0 : AuthData::Password(pw) => backend
690 0 : .authenticate_with_password(ctx, &conn_info.user_info, &pw)
691 0 : .await
692 0 : .map_err(HttpConnError::AuthError)?,
693 0 : AuthData::Jwt(jwt) => backend
694 0 : .authenticate_with_jwt(ctx, &conn_info.user_info, jwt)
695 0 : .await
696 0 : .map_err(HttpConnError::AuthError)?,
697 : };
698 :
699 0 : let client = match keys.keys {
700 0 : ComputeCredentialKeys::JwtPayload(payload)
701 0 : if backend.auth_backend.is_local_proxy() =>
702 : {
703 0 : let mut client = backend.connect_to_local_postgres(ctx, conn_info).await?;
704 0 : let (cli_inner, _dsc) = client.client_inner();
705 0 : cli_inner.set_jwt_session(&payload).await?;
706 0 : Client::Local(client)
707 : }
708 : _ => {
709 0 : let client = backend
710 0 : .connect_to_compute(ctx, conn_info, keys, !allow_pool)
711 0 : .await?;
712 0 : Client::Remote(client)
713 : }
714 : };
715 :
716 : // not strictly necessary to mark success here,
717 : // but it's just insurance for if we forget it somewhere else
718 0 : ctx.success();
719 0 : Ok::<_, SqlOverHttpError>(client)
720 0 : }
721 0 : .map_err(SqlOverHttpError::from),
722 0 : );
723 :
724 0 : let (payload, mut client) = match run_until_cancelled(
725 0 : // Run both operations in parallel
726 0 : try_join(
727 0 : pin!(fetch_and_process_request),
728 0 : pin!(authenticate_and_connect),
729 0 : ),
730 0 : &cancel,
731 0 : )
732 0 : .await
733 : {
734 0 : Some(result) => result?,
735 0 : None => return Err(SqlOverHttpError::Cancelled(SqlOverHttpCancel::Connect)),
736 : };
737 :
738 0 : let mut response = Response::builder()
739 0 : .status(StatusCode::OK)
740 0 : .header(header::CONTENT_TYPE, "application/json");
741 :
742 : // Now execute the query and return the result.
743 0 : let json_output = match payload {
744 0 : Payload::Single(stmt) => {
745 0 : stmt.process(&config.http_config, cancel, &mut client, parsed_headers)
746 0 : .await?
747 : }
748 0 : Payload::Batch(statements) => {
749 0 : if parsed_headers.txn_read_only {
750 0 : response = response.header(TXN_READ_ONLY.clone(), &HEADER_VALUE_TRUE);
751 0 : }
752 0 : if parsed_headers.txn_deferrable {
753 0 : response = response.header(TXN_DEFERRABLE.clone(), &HEADER_VALUE_TRUE);
754 0 : }
755 0 : if let Some(txn_isolation_level) = parsed_headers
756 0 : .txn_isolation_level
757 0 : .and_then(map_isolation_level_to_headers)
758 0 : {
759 0 : response = response.header(TXN_ISOLATION_LEVEL.clone(), txn_isolation_level);
760 0 : }
761 :
762 0 : statements
763 0 : .process(&config.http_config, cancel, &mut client, parsed_headers)
764 0 : .await?
765 : }
766 : };
767 :
768 0 : let metrics = client.metrics(TrafficDirection::Egress, ctx);
769 0 :
770 0 : let len = json_output.len();
771 0 : let response = response
772 0 : .body(
773 0 : Full::new(Bytes::from(json_output))
774 0 : .map_err(|x| match x {})
775 0 : .boxed(),
776 0 : )
777 0 : // only fails if invalid status code or invalid header/values are given.
778 0 : // these are not user configurable so it cannot fail dynamically
779 0 : .expect("building response payload should not fail");
780 0 :
781 0 : // count the egress bytes - we miss the TLS and header overhead but oh well...
782 0 : // moving this later in the stack is going to be a lot of effort and ehhhh
783 0 : metrics.record_egress(len as u64);
784 0 : Metrics::get()
785 0 : .proxy
786 0 : .http_conn_content_length_bytes
787 0 : .observe(HttpDirection::Response, len as f64);
788 0 :
789 0 : Ok(response)
790 0 : }
791 :
792 : static HEADERS_TO_FORWARD: &[&HeaderName] = &[
793 : &AUTHORIZATION,
794 : &CONN_STRING,
795 : &RAW_TEXT_OUTPUT,
796 : &ARRAY_MODE,
797 : &TXN_ISOLATION_LEVEL,
798 : &TXN_READ_ONLY,
799 : &TXN_DEFERRABLE,
800 : ];
801 :
802 0 : pub(crate) fn uuid_to_header_value(id: Uuid) -> HeaderValue {
803 0 : let mut uuid = [0; uuid::fmt::Hyphenated::LENGTH];
804 0 : HeaderValue::from_str(id.as_hyphenated().encode_lower(&mut uuid[..]))
805 0 : .expect("uuid hyphenated format should be all valid header characters")
806 0 : }
807 :
808 0 : async fn handle_auth_broker_inner(
809 0 : ctx: &RequestContext,
810 0 : request: Request<Incoming>,
811 0 : conn_info: ConnInfo,
812 0 : jwt: String,
813 0 : backend: Arc<PoolingBackend>,
814 0 : ) -> Result<Response<BoxBody<Bytes, hyper::Error>>, SqlOverHttpError> {
815 0 : backend
816 0 : .authenticate_with_jwt(ctx, &conn_info.user_info, jwt)
817 0 : .await
818 0 : .map_err(HttpConnError::from)?;
819 :
820 0 : let mut client = backend.connect_to_local_proxy(ctx, conn_info).await?;
821 :
822 0 : let local_proxy_uri = ::http::Uri::from_static("http://proxy.local/sql");
823 0 :
824 0 : let (mut parts, body) = request.into_parts();
825 0 : let mut req = Request::builder().method(Method::POST).uri(local_proxy_uri);
826 :
827 : // todo(conradludgate): maybe auth-broker should parse these and re-serialize
828 : // these instead just to ensure they remain normalised.
829 0 : for &h in HEADERS_TO_FORWARD {
830 0 : if let Some(hv) = parts.headers.remove(h) {
831 0 : req = req.header(h, hv);
832 0 : }
833 : }
834 0 : req = req.header(&NEON_REQUEST_ID, uuid_to_header_value(ctx.session_id()));
835 0 :
836 0 : let req = req
837 0 : .body(body)
838 0 : .expect("all headers and params received via hyper should be valid for request");
839 0 :
840 0 : // todo: map body to count egress
841 0 : let _metrics = client.metrics(TrafficDirection::Egress, ctx);
842 0 :
843 0 : Ok(client
844 0 : .inner
845 0 : .inner
846 0 : .send_request(req)
847 0 : .await
848 0 : .map_err(LocalProxyConnError::from)
849 0 : .map_err(HttpConnError::from)?
850 0 : .map(|b| b.boxed()))
851 0 : }
852 :
853 : impl QueryData {
854 0 : async fn process(
855 0 : self,
856 0 : config: &'static HttpConfig,
857 0 : cancel: CancellationToken,
858 0 : client: &mut Client,
859 0 : parsed_headers: HttpHeaders,
860 0 : ) -> Result<String, SqlOverHttpError> {
861 0 : let (inner, mut discard) = client.inner();
862 0 : let cancel_token = inner.cancel_token();
863 :
864 0 : let res = match select(
865 0 : pin!(query_to_json(
866 0 : config,
867 0 : &mut *inner,
868 0 : self,
869 0 : &mut 0,
870 0 : parsed_headers
871 0 : )),
872 0 : pin!(cancel.cancelled()),
873 0 : )
874 0 : .await
875 : {
876 : // The query successfully completed.
877 0 : Either::Left((Ok((status, results)), __not_yet_cancelled)) => {
878 0 : discard.check_idle(status);
879 0 :
880 0 : let json_output =
881 0 : serde_json::to_string(&results).expect("json serialization should not fail");
882 0 : Ok(json_output)
883 : }
884 : // The query failed with an error
885 0 : Either::Left((Err(e), __not_yet_cancelled)) => {
886 0 : discard.discard();
887 0 : return Err(e);
888 : }
889 : // The query was cancelled.
890 0 : Either::Right((_cancelled, query)) => {
891 0 : tracing::info!("cancelling query");
892 0 : if let Err(err) = cancel_token.cancel_query(NoTls).await {
893 0 : tracing::warn!(?err, "could not cancel query");
894 0 : }
895 : // wait for the query cancellation
896 0 : match time::timeout(time::Duration::from_millis(100), query).await {
897 : // query successed before it was cancelled.
898 0 : Ok(Ok((status, results))) => {
899 0 : discard.check_idle(status);
900 0 :
901 0 : let json_output = serde_json::to_string(&results)
902 0 : .expect("json serialization should not fail");
903 0 : Ok(json_output)
904 : }
905 : // query failed or was cancelled.
906 0 : Ok(Err(error)) => {
907 0 : let db_error = match &error {
908 : SqlOverHttpError::ConnectCompute(
909 0 : HttpConnError::PostgresConnectionError(e),
910 : )
911 0 : | SqlOverHttpError::Postgres(e) => e.as_db_error(),
912 0 : _ => None,
913 : };
914 :
915 : // if errored for some other reason, it might not be safe to return
916 0 : if !db_error.is_some_and(|e| *e.code() == SqlState::QUERY_CANCELED) {
917 0 : discard.discard();
918 0 : }
919 :
920 0 : Err(SqlOverHttpError::Cancelled(SqlOverHttpCancel::Postgres))
921 : }
922 0 : Err(_timeout) => {
923 0 : discard.discard();
924 0 : Err(SqlOverHttpError::Cancelled(SqlOverHttpCancel::Postgres))
925 : }
926 : }
927 : }
928 : };
929 0 : res
930 0 : }
931 : }
932 :
933 : impl BatchQueryData {
934 0 : async fn process(
935 0 : self,
936 0 : config: &'static HttpConfig,
937 0 : cancel: CancellationToken,
938 0 : client: &mut Client,
939 0 : parsed_headers: HttpHeaders,
940 0 : ) -> Result<String, SqlOverHttpError> {
941 0 : info!("starting transaction");
942 0 : let (inner, mut discard) = client.inner();
943 0 : let cancel_token = inner.cancel_token();
944 0 : let mut builder = inner.build_transaction();
945 0 : if let Some(isolation_level) = parsed_headers.txn_isolation_level {
946 0 : builder = builder.isolation_level(isolation_level);
947 0 : }
948 0 : if parsed_headers.txn_read_only {
949 0 : builder = builder.read_only(true);
950 0 : }
951 0 : if parsed_headers.txn_deferrable {
952 0 : builder = builder.deferrable(true);
953 0 : }
954 :
955 0 : let mut transaction = builder
956 0 : .start()
957 0 : .await
958 0 : .inspect_err(|_| {
959 0 : // if we cannot start a transaction, we should return immediately
960 0 : // and not return to the pool. connection is clearly broken
961 0 : discard.discard();
962 0 : })
963 0 : .map_err(SqlOverHttpError::Postgres)?;
964 :
965 0 : let json_output = match query_batch(
966 0 : config,
967 0 : cancel.child_token(),
968 0 : &mut transaction,
969 0 : self,
970 0 : parsed_headers,
971 0 : )
972 0 : .await
973 : {
974 0 : Ok(json_output) => {
975 0 : info!("commit");
976 0 : let status = transaction
977 0 : .commit()
978 0 : .await
979 0 : .inspect_err(|_| {
980 0 : // if we cannot commit - for now don't return connection to pool
981 0 : // TODO: get a query status from the error
982 0 : discard.discard();
983 0 : })
984 0 : .map_err(SqlOverHttpError::Postgres)?;
985 0 : discard.check_idle(status);
986 0 : json_output
987 : }
988 : Err(SqlOverHttpError::Cancelled(_)) => {
989 0 : if let Err(err) = cancel_token.cancel_query(NoTls).await {
990 0 : tracing::warn!(?err, "could not cancel query");
991 0 : }
992 : // TODO: after cancelling, wait to see if we can get a status. maybe the connection is still safe.
993 0 : discard.discard();
994 0 :
995 0 : return Err(SqlOverHttpError::Cancelled(SqlOverHttpCancel::Postgres));
996 : }
997 0 : Err(err) => {
998 0 : info!("rollback");
999 0 : let status = transaction
1000 0 : .rollback()
1001 0 : .await
1002 0 : .inspect_err(|_| {
1003 0 : // if we cannot rollback - for now don't return connection to pool
1004 0 : // TODO: get a query status from the error
1005 0 : discard.discard();
1006 0 : })
1007 0 : .map_err(SqlOverHttpError::Postgres)?;
1008 0 : discard.check_idle(status);
1009 0 : return Err(err);
1010 : }
1011 : };
1012 :
1013 0 : Ok(json_output)
1014 0 : }
1015 : }
1016 :
1017 0 : async fn query_batch(
1018 0 : config: &'static HttpConfig,
1019 0 : cancel: CancellationToken,
1020 0 : transaction: &mut Transaction<'_>,
1021 0 : queries: BatchQueryData,
1022 0 : parsed_headers: HttpHeaders,
1023 0 : ) -> Result<String, SqlOverHttpError> {
1024 0 : let mut results = Vec::with_capacity(queries.queries.len());
1025 0 : let mut current_size = 0;
1026 0 : for stmt in queries.queries {
1027 0 : let query = pin!(query_to_json(
1028 0 : config,
1029 0 : transaction,
1030 0 : stmt,
1031 0 : &mut current_size,
1032 0 : parsed_headers,
1033 0 : ));
1034 0 : let cancelled = pin!(cancel.cancelled());
1035 0 : let res = select(query, cancelled).await;
1036 0 : match res {
1037 : // TODO: maybe we should check that the transaction bit is set here
1038 0 : Either::Left((Ok((_, values)), _cancelled)) => {
1039 0 : results.push(values);
1040 0 : }
1041 0 : Either::Left((Err(e), _cancelled)) => {
1042 0 : return Err(e);
1043 : }
1044 0 : Either::Right((_cancelled, _)) => {
1045 0 : return Err(SqlOverHttpError::Cancelled(SqlOverHttpCancel::Postgres));
1046 : }
1047 : }
1048 : }
1049 :
1050 0 : let results = json!({ "results": results });
1051 0 : let json_output = serde_json::to_string(&results).expect("json serialization should not fail");
1052 0 :
1053 0 : Ok(json_output)
1054 0 : }
1055 :
1056 0 : async fn query_to_json<T: GenericClient>(
1057 0 : config: &'static HttpConfig,
1058 0 : client: &mut T,
1059 0 : data: QueryData,
1060 0 : current_size: &mut usize,
1061 0 : parsed_headers: HttpHeaders,
1062 0 : ) -> Result<(ReadyForQueryStatus, impl Serialize + use<T>), SqlOverHttpError> {
1063 0 : let query_start = Instant::now();
1064 0 :
1065 0 : let query_params = data.params;
1066 0 : let mut row_stream = std::pin::pin!(
1067 0 : client
1068 0 : .query_raw_txt(&data.query, query_params)
1069 0 : .await
1070 0 : .map_err(SqlOverHttpError::Postgres)?
1071 : );
1072 0 : let query_acknowledged = Instant::now();
1073 0 :
1074 0 : // Manually drain the stream into a vector to leave row_stream hanging
1075 0 : // around to get a command tag. Also check that the response is not too
1076 0 : // big.
1077 0 : let mut rows: Vec<postgres_client::Row> = Vec::new();
1078 0 : while let Some(row) = row_stream.next().await {
1079 0 : let row = row.map_err(SqlOverHttpError::Postgres)?;
1080 0 : *current_size += row.body_len();
1081 0 : rows.push(row);
1082 0 : // we don't have a streaming response support yet so this is to prevent OOM
1083 0 : // from a malicious query (eg a cross join)
1084 0 : if *current_size > config.max_response_size_bytes {
1085 0 : return Err(SqlOverHttpError::ResponseTooLarge(
1086 0 : config.max_response_size_bytes,
1087 0 : ));
1088 0 : }
1089 : }
1090 :
1091 0 : let query_resp_end = Instant::now();
1092 0 : let ready = row_stream.ready_status();
1093 0 :
1094 0 : // grab the command tag and number of rows affected
1095 0 : let command_tag = row_stream.command_tag().unwrap_or_default();
1096 0 : let mut command_tag_split = command_tag.split(' ');
1097 0 : let command_tag_name = command_tag_split.next().unwrap_or_default();
1098 0 : let command_tag_count = if command_tag_name == "INSERT" {
1099 : // INSERT returns OID first and then number of rows
1100 0 : command_tag_split.nth(1)
1101 : } else {
1102 : // other commands return number of rows (if any)
1103 0 : command_tag_split.next()
1104 : }
1105 0 : .and_then(|s| s.parse::<i64>().ok());
1106 0 :
1107 0 : info!(
1108 0 : rows = rows.len(),
1109 0 : ?ready,
1110 0 : command_tag,
1111 0 : acknowledgement = ?(query_acknowledged - query_start),
1112 0 : response = ?(query_resp_end - query_start),
1113 0 : "finished executing query"
1114 : );
1115 :
1116 0 : let columns_len = row_stream.columns().len();
1117 0 : let mut fields = Vec::with_capacity(columns_len);
1118 0 : let mut columns = Vec::with_capacity(columns_len);
1119 :
1120 0 : for c in row_stream.columns() {
1121 0 : fields.push(json!({
1122 0 : "name": c.name().to_owned(),
1123 0 : "dataTypeID": c.type_().oid(),
1124 0 : "tableID": c.table_oid(),
1125 0 : "columnID": c.column_id(),
1126 0 : "dataTypeSize": c.type_size(),
1127 0 : "dataTypeModifier": c.type_modifier(),
1128 0 : "format": "text",
1129 0 : }));
1130 0 :
1131 0 : match client.get_type(c.type_oid()).await {
1132 0 : Ok(t) => columns.push(t),
1133 0 : Err(err) => {
1134 0 : tracing::warn!(?err, "unable to query type information");
1135 0 : return Err(SqlOverHttpError::InternalPostgres(err));
1136 : }
1137 : }
1138 : }
1139 :
1140 0 : let array_mode = data.array_mode.unwrap_or(parsed_headers.default_array_mode);
1141 :
1142 : // convert rows to JSON
1143 0 : let rows = rows
1144 0 : .iter()
1145 0 : .map(|row| pg_text_row_to_json(row, &columns, parsed_headers.raw_output, array_mode))
1146 0 : .collect::<Result<Vec<_>, _>>()?;
1147 :
1148 : // Resulting JSON format is based on the format of node-postgres result.
1149 0 : let results = json!({
1150 0 : "command": command_tag_name.to_string(),
1151 0 : "rowCount": command_tag_count,
1152 0 : "rows": rows,
1153 0 : "fields": fields,
1154 0 : "rowAsArray": array_mode,
1155 0 : });
1156 0 :
1157 0 : Ok((ready, results))
1158 0 : }
1159 :
1160 : enum Client {
1161 : Remote(conn_pool_lib::Client<postgres_client::Client>),
1162 : Local(conn_pool_lib::Client<postgres_client::Client>),
1163 : }
1164 :
1165 : enum Discard<'a> {
1166 : Remote(conn_pool_lib::Discard<'a, postgres_client::Client>),
1167 : Local(conn_pool_lib::Discard<'a, postgres_client::Client>),
1168 : }
1169 :
1170 : impl Client {
1171 0 : fn metrics(&self, direction: TrafficDirection, ctx: &RequestContext) -> Arc<MetricCounter> {
1172 0 : match self {
1173 0 : Client::Remote(client) => client.metrics(direction, ctx),
1174 0 : Client::Local(local_client) => local_client.metrics(direction, ctx),
1175 : }
1176 0 : }
1177 :
1178 0 : fn inner(&mut self) -> (&mut postgres_client::Client, Discard<'_>) {
1179 0 : match self {
1180 0 : Client::Remote(client) => {
1181 0 : let (c, d) = client.inner();
1182 0 : (c, Discard::Remote(d))
1183 : }
1184 0 : Client::Local(local_client) => {
1185 0 : let (c, d) = local_client.inner();
1186 0 : (c, Discard::Local(d))
1187 : }
1188 : }
1189 0 : }
1190 : }
1191 :
1192 : impl Discard<'_> {
1193 0 : fn check_idle(&mut self, status: ReadyForQueryStatus) {
1194 0 : match self {
1195 0 : Discard::Remote(discard) => discard.check_idle(status),
1196 0 : Discard::Local(discard) => discard.check_idle(status),
1197 : }
1198 0 : }
1199 0 : fn discard(&mut self) {
1200 0 : match self {
1201 0 : Discard::Remote(discard) => discard.discard(),
1202 0 : Discard::Local(discard) => discard.discard(),
1203 : }
1204 0 : }
1205 : }
1206 :
1207 : #[cfg(test)]
1208 : #[expect(clippy::unwrap_used)]
1209 : mod tests {
1210 : use super::*;
1211 :
1212 : #[test]
1213 1 : fn test_payload() {
1214 1 : let payload = "{\"query\":\"SELECT * FROM users WHERE name = ?\",\"params\":[\"test\"],\"arrayMode\":true}";
1215 1 : let deserialized_payload: Payload = serde_json::from_str(payload).unwrap();
1216 1 :
1217 1 : match deserialized_payload {
1218 : Payload::Single(QueryData {
1219 1 : query,
1220 1 : params,
1221 1 : array_mode,
1222 1 : }) => {
1223 1 : assert_eq!(query, "SELECT * FROM users WHERE name = ?");
1224 1 : assert_eq!(params, vec![Some(String::from("test"))]);
1225 1 : assert!(array_mode.unwrap());
1226 : }
1227 : Payload::Batch(_) => {
1228 0 : panic!("deserialization failed: case with single query, one param, and array mode")
1229 : }
1230 : }
1231 :
1232 1 : let payload = "{\"queries\":[{\"query\":\"SELECT * FROM users0 WHERE name = ?\",\"params\":[\"test0\"], \"arrayMode\":false},{\"query\":\"SELECT * FROM users1 WHERE name = ?\",\"params\":[\"test1\"],\"arrayMode\":true}]}";
1233 1 : let deserialized_payload: Payload = serde_json::from_str(payload).unwrap();
1234 1 :
1235 1 : match deserialized_payload {
1236 1 : Payload::Batch(BatchQueryData { queries }) => {
1237 1 : assert_eq!(queries.len(), 2);
1238 2 : for (i, query) in queries.into_iter().enumerate() {
1239 2 : assert_eq!(
1240 2 : query.query,
1241 2 : format!("SELECT * FROM users{i} WHERE name = ?")
1242 2 : );
1243 2 : assert_eq!(query.params, vec![Some(format!("test{i}"))]);
1244 2 : assert_eq!(query.array_mode.unwrap(), i > 0);
1245 : }
1246 : }
1247 0 : Payload::Single(_) => panic!("deserialization failed: case with multiple queries"),
1248 : }
1249 :
1250 1 : let payload = "{\"query\":\"SELECT 1\"}";
1251 1 : let deserialized_payload: Payload = serde_json::from_str(payload).unwrap();
1252 1 :
1253 1 : match deserialized_payload {
1254 : Payload::Single(QueryData {
1255 1 : query,
1256 1 : params,
1257 1 : array_mode,
1258 1 : }) => {
1259 1 : assert_eq!(query, "SELECT 1");
1260 1 : assert_eq!(params, vec![]);
1261 1 : assert!(array_mode.is_none());
1262 : }
1263 0 : Payload::Batch(_) => panic!("deserialization failed: case with only one query"),
1264 : }
1265 1 : }
1266 : }
|