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