From 1a3a7f14dda98478df196ca1a971d599179ae469 Mon Sep 17 00:00:00 2001 From: Stas Kelvich Date: Sat, 15 Apr 2023 01:15:22 +0300 Subject: [PATCH] works now, few fixes --- Cargo.lock | 33 +++++++++++++++++++------------- Cargo.toml | 1 + proxy/Cargo.toml | 1 + proxy/src/http/websocket.rs | 38 ++++++++++++++++++++++++++----------- 4 files changed, 49 insertions(+), 24 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index a18f4490da..9f77612b31 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -297,7 +297,7 @@ dependencies = [ "http", "http-body", "lazy_static", - "percent-encoding", + "percent-encoding 2.2.0", "pin-project-lite", "tracing", ] @@ -401,7 +401,7 @@ dependencies = [ "hex", "http", "once_cell", - "percent-encoding", + "percent-encoding 2.2.0", "regex", "ring", "time", @@ -522,7 +522,7 @@ dependencies = [ "http-body", "hyper", "once_cell", - "percent-encoding", + "percent-encoding 2.2.0", "pin-project-lite", "pin-utils", "tokio", @@ -544,7 +544,7 @@ dependencies = [ "http-body", "hyper", "once_cell", - "percent-encoding", + "percent-encoding 2.2.0", "pin-project-lite", "pin-utils", "tracing", @@ -684,7 +684,7 @@ dependencies = [ "matchit", "memchr", "mime", - "percent-encoding", + "percent-encoding 2.2.0", "pin-project-lite", "rustversion", "serde", @@ -1580,7 +1580,7 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a9c384f161156f5260c24a097c56119f9be8c798586aecc13afbcbe7b7e26bf8" dependencies = [ - "percent-encoding", + "percent-encoding 2.2.0", ] [[package]] @@ -2584,7 +2584,7 @@ dependencies = [ "futures-util", "once_cell", "opentelemetry_api", - "percent-encoding", + "percent-encoding 2.2.0", "rand", "thiserror", "tokio", @@ -2749,6 +2749,12 @@ dependencies = [ "base64 0.13.1", ] +[[package]] +name = "percent-encoding" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "31010dd2e1ac33d5b46a5b413495239882813e0369f8ed8a5e266f173602f831" + [[package]] name = "percent-encoding" version = "2.2.0" @@ -3112,6 +3118,7 @@ dependencies = [ "once_cell", "opentelemetry", "parking_lot", + "percent-encoding 1.0.1", "pin-project-lite", "postgres_backend", "pq_proto", @@ -3316,7 +3323,7 @@ dependencies = [ "mime", "mime_guess", "once_cell", - "percent-encoding", + "percent-encoding 2.2.0", "pin-project-lite", "rustls 0.20.8", "rustls-pemfile", @@ -3389,7 +3396,7 @@ dependencies = [ "http", "hyper", "lazy_static", - "percent-encoding", + "percent-encoding 2.2.0", "regex", ] @@ -4332,7 +4339,7 @@ dependencies = [ "futures-util", "log", "parking_lot", - "percent-encoding", + "percent-encoding 2.2.0", "phf", "pin-project-lite", "postgres-protocol", @@ -4480,7 +4487,7 @@ dependencies = [ "http-body", "hyper", "hyper-timeout", - "percent-encoding", + "percent-encoding 2.2.0", "pin-project", "prost", "prost-derive", @@ -4512,7 +4519,7 @@ dependencies = [ "http-body", "hyper", "hyper-timeout", - "percent-encoding", + "percent-encoding 2.2.0", "pin-project", "prost", "rustls-native-certs", @@ -4822,7 +4829,7 @@ checksum = "0d68c799ae75762b8c3fe375feb6600ef5602c883c5d21eb51c09f22b83c4643" dependencies = [ "form_urlencoded", "idna", - "percent-encoding", + "percent-encoding 2.2.0", "serde", ] diff --git a/Cargo.toml b/Cargo.toml index 0b545e6190..90271aedc2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -117,6 +117,7 @@ uuid = { version = "1.2", features = ["v4", "serde"] } walkdir = "2.3.2" webpki-roots = "0.23" x509-parser = "0.15" +percent-encoding = "1.0" ## TODO replace this with tracing env_logger = "0.10" diff --git a/proxy/Cargo.toml b/proxy/Cargo.toml index 9d702b29c3..e3907da1f0 100644 --- a/proxy/Cargo.toml +++ b/proxy/Cargo.toml @@ -65,6 +65,7 @@ x509-parser.workspace = true workspace_hack.workspace = true tokio-util.workspace = true +percent-encoding.workspace = true [dev-dependencies] rcgen.workspace = true diff --git a/proxy/src/http/websocket.rs b/proxy/src/http/websocket.rs index bd97f18209..c5ab0045d0 100644 --- a/proxy/src/http/websocket.rs +++ b/proxy/src/http/websocket.rs @@ -31,6 +31,7 @@ use tokio_util::sync::CancellationToken; use tracing::{error, info, info_span, warn, Instrument}; use url::form_urlencoded; use utils::http::{error::ApiError, json::json_response}; +use percent_encoding::percent_decode; // TODO: use `std::sync::Exclusive` once it's stabilized. // Tracking issue: https://github.com/rust-lang/rust/issues/98407. @@ -232,7 +233,11 @@ async fn handle_sql( .map(|s| s.to_string()) .ok_or(anyhow::anyhow!("missing host header"))?; - let params = StartupMessageParams::new([]); + let params = StartupMessageParams::new([ + ("user", username.as_str()), + ("database", dbname.as_str()), + ("application_name", "proxy_http_sql"), + ]); let tls = config.tls_config.as_ref(); let common_names = tls.and_then(|tls| tls.common_names.clone()); let creds = config @@ -256,15 +261,19 @@ async fn handle_sql( } }; + let conn_string = &format!( + "host={} port={} user={} password={} dbname={}", + host, + conf.get_ports().first().expect("no port"), + username, + password, + dbname + ); + + info!("!!!! connecting to: {}", conn_string); + let (client, connection) = tokio_postgres::connect( - &format!( - "host={} port={} user={} password={} dbname={}", - host, - conf.get_ports().first().expect("no port"), - username, - password, - dbname - ), + conn_string, tokio_postgres::NoTls, ) .await?; @@ -275,14 +284,21 @@ async fn handle_sql( } }); + let sql = percent_decode(sql.as_bytes()).decode_utf8()?.to_string(); + info!("!!!! query: '{}'", sql); + let rows: Result, anyhow::Error> = client - .query(sql, &[]) + .query(&sql, &[]) .await? .into_iter() .map(postgres_row_to_json_value) .collect(); - Ok(serde_json::to_string(&rows?)?) + let rows = rows?; + + info!("!!!! n_rows: {}", rows.len()); + + Ok(serde_json::to_string(&rows)?) } pub async fn task_main(