Line data Source code
1 : use std::sync::Arc;
2 :
3 : use futures::TryFutureExt;
4 : use tokio::io::{AsyncRead, AsyncWrite, AsyncWriteExt};
5 : use tokio_util::sync::CancellationToken;
6 : use tracing::{debug, error, info, Instrument};
7 :
8 : use crate::auth::backend::ConsoleRedirectBackend;
9 : use crate::cancellation::{CancellationHandlerMain, CancellationHandlerMainInternal};
10 : use crate::config::{ProxyConfig, ProxyProtocolV2};
11 : use crate::context::RequestContext;
12 : use crate::error::ReportableError;
13 : use crate::metrics::{Metrics, NumClientConnectionsGuard};
14 : use crate::protocol2::{read_proxy_protocol, ConnectHeader, ConnectionInfo};
15 : use crate::proxy::connect_compute::{connect_to_compute, TcpMechanism};
16 : use crate::proxy::handshake::{handshake, HandshakeData};
17 : use crate::proxy::passthrough::ProxyPassthrough;
18 : use crate::proxy::{
19 : prepare_client_connection, run_until_cancelled, ClientRequestError, ErrorSource,
20 : };
21 :
22 0 : pub async fn task_main(
23 0 : config: &'static ProxyConfig,
24 0 : backend: &'static ConsoleRedirectBackend,
25 0 : listener: tokio::net::TcpListener,
26 0 : cancellation_token: CancellationToken,
27 0 : cancellation_handler: Arc<CancellationHandlerMain>,
28 0 : ) -> anyhow::Result<()> {
29 0 : scopeguard::defer! {
30 0 : info!("proxy has shut down");
31 0 : }
32 0 :
33 0 : // When set for the server socket, the keepalive setting
34 0 : // will be inherited by all accepted client sockets.
35 0 : socket2::SockRef::from(&listener).set_keepalive(true)?;
36 :
37 0 : let connections = tokio_util::task::task_tracker::TaskTracker::new();
38 :
39 0 : while let Some(accept_result) =
40 0 : run_until_cancelled(listener.accept(), &cancellation_token).await
41 : {
42 0 : let (socket, peer_addr) = accept_result?;
43 :
44 0 : let conn_gauge = Metrics::get()
45 0 : .proxy
46 0 : .client_connections
47 0 : .guard(crate::metrics::Protocol::Tcp);
48 0 :
49 0 : let session_id = uuid::Uuid::new_v4();
50 0 : let cancellation_handler = Arc::clone(&cancellation_handler);
51 0 :
52 0 : debug!(protocol = "tcp", %session_id, "accepted new TCP connection");
53 :
54 0 : connections.spawn(async move {
55 0 : let (socket, peer_addr) = match read_proxy_protocol(socket).await {
56 0 : Err(e) => {
57 0 : error!("per-client task finished with an error: {e:#}");
58 0 : return;
59 : }
60 : // our load balancers will not send any more data. let's just exit immediately
61 0 : Ok((_socket, ConnectHeader::Local)) => {
62 0 : debug!("healthcheck received");
63 0 : return;
64 : }
65 0 : Ok((_socket, ConnectHeader::Missing)) if config.proxy_protocol_v2 == ProxyProtocolV2::Required => {
66 0 : error!("missing required proxy protocol header");
67 0 : return;
68 : }
69 0 : Ok((_socket, ConnectHeader::Proxy(_))) if config.proxy_protocol_v2 == ProxyProtocolV2::Rejected => {
70 0 : error!("proxy protocol header not supported");
71 0 : return;
72 : }
73 0 : Ok((socket, ConnectHeader::Proxy(info))) => (socket, info),
74 0 : Ok((socket, ConnectHeader::Missing)) => (socket, ConnectionInfo{ addr: peer_addr, extra: None }),
75 : };
76 :
77 0 : match socket.inner.set_nodelay(true) {
78 0 : Ok(()) => {}
79 0 : Err(e) => {
80 0 : error!("per-client task finished with an error: failed to set socket option: {e:#}");
81 0 : return;
82 : }
83 : };
84 :
85 0 : let ctx = RequestContext::new(
86 0 : session_id,
87 0 : peer_addr,
88 0 : crate::metrics::Protocol::Tcp,
89 0 : &config.region,
90 0 : );
91 0 : let span = ctx.span();
92 0 :
93 0 : let startup = Box::pin(
94 0 : handle_client(
95 0 : config,
96 0 : backend,
97 0 : &ctx,
98 0 : cancellation_handler,
99 0 : socket,
100 0 : conn_gauge,
101 0 : )
102 0 : .instrument(span.clone()),
103 0 : );
104 0 : let res = startup.await;
105 :
106 0 : match res {
107 0 : Err(e) => {
108 0 : // todo: log and push to ctx the error kind
109 0 : ctx.set_error_kind(e.get_error_kind());
110 0 : error!(parent: &span, "per-client task finished with an error: {e:#}");
111 : }
112 0 : Ok(None) => {
113 0 : ctx.set_success();
114 0 : }
115 0 : Ok(Some(p)) => {
116 0 : ctx.set_success();
117 0 : ctx.log_connect();
118 0 : match p.proxy_pass().instrument(span.clone()).await {
119 0 : Ok(()) => {}
120 0 : Err(ErrorSource::Client(e)) => {
121 0 : error!(parent: &span, "per-client task finished with an IO error from the client: {e:#}");
122 : }
123 0 : Err(ErrorSource::Compute(e)) => {
124 0 : error!(parent: &span, "per-client task finished with an IO error from the compute: {e:#}");
125 : }
126 : }
127 : }
128 : }
129 0 : });
130 : }
131 :
132 0 : connections.close();
133 0 : drop(listener);
134 0 :
135 0 : // Drain connections
136 0 : connections.wait().await;
137 :
138 0 : Ok(())
139 0 : }
140 :
141 0 : pub(crate) async fn handle_client<S: AsyncRead + AsyncWrite + Unpin>(
142 0 : config: &'static ProxyConfig,
143 0 : backend: &'static ConsoleRedirectBackend,
144 0 : ctx: &RequestContext,
145 0 : cancellation_handler: Arc<CancellationHandlerMain>,
146 0 : stream: S,
147 0 : conn_gauge: NumClientConnectionsGuard<'static>,
148 0 : ) -> Result<Option<ProxyPassthrough<CancellationHandlerMainInternal, S>>, ClientRequestError> {
149 0 : debug!(
150 0 : protocol = %ctx.protocol(),
151 0 : "handling interactive connection from client"
152 : );
153 :
154 0 : let metrics = &Metrics::get().proxy;
155 0 : let proto = ctx.protocol();
156 0 : let request_gauge = metrics.connection_requests.guard(proto);
157 0 :
158 0 : let tls = config.tls_config.as_ref();
159 0 : let record_handshake_error = !ctx.has_private_peer_addr();
160 0 : let pause = ctx.latency_timer_pause(crate::metrics::Waiting::Client);
161 0 : let do_handshake = handshake(ctx, stream, tls, record_handshake_error);
162 :
163 0 : let (mut stream, params) =
164 0 : match tokio::time::timeout(config.handshake_timeout, do_handshake).await?? {
165 0 : HandshakeData::Startup(stream, params) => (stream, params),
166 0 : HandshakeData::Cancel(cancel_key_data) => {
167 0 : return Ok(cancellation_handler
168 0 : .cancel_session(
169 0 : cancel_key_data,
170 0 : ctx.session_id(),
171 0 : &ctx.peer_addr(),
172 0 : config.authentication_config.ip_allowlist_check_enabled,
173 0 : )
174 0 : .await
175 0 : .map(|()| None)?)
176 : }
177 : };
178 0 : drop(pause);
179 0 :
180 0 : ctx.set_db_options(params.clone());
181 :
182 0 : let (user_info, ip_allowlist) = match backend
183 0 : .authenticate(ctx, &config.authentication_config, &mut stream)
184 0 : .await
185 : {
186 0 : Ok(auth_result) => auth_result,
187 0 : Err(e) => {
188 0 : return stream.throw_error(e).await?;
189 : }
190 : };
191 :
192 0 : let mut node = connect_to_compute(
193 0 : ctx,
194 0 : &TcpMechanism {
195 0 : params: ¶ms,
196 0 : locks: &config.connect_compute_locks,
197 0 : },
198 0 : &user_info,
199 0 : config.allow_self_signed_compute,
200 0 : config.wake_compute_retry_config,
201 0 : config.connect_to_compute_retry_config,
202 0 : )
203 0 : .or_else(|e| stream.throw_error(e))
204 0 : .await?;
205 :
206 0 : node.cancel_closure
207 0 : .set_ip_allowlist(ip_allowlist.unwrap_or_default());
208 0 : let session = cancellation_handler.get_session();
209 0 : prepare_client_connection(&node, &session, &mut stream).await?;
210 :
211 : // Before proxy passing, forward to compute whatever data is left in the
212 : // PqStream input buffer. Normally there is none, but our serverless npm
213 : // driver in pipeline mode sends startup, password and first query
214 : // immediately after opening the connection.
215 0 : let (stream, read_buf) = stream.into_inner();
216 0 : node.stream.write_all(&read_buf).await?;
217 :
218 0 : Ok(Some(ProxyPassthrough {
219 0 : client: stream,
220 0 : aux: node.aux.clone(),
221 0 : compute: node,
222 0 : _req: request_gauge,
223 0 : _conn: conn_gauge,
224 0 : _cancel: session,
225 0 : }))
226 0 : }
|