1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
// Copyright Materialize, Inc. and contributors. All rights reserved.
//
// Use of this software is governed by the Business Source License
// included in the LICENSE file.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0.

use std::pin::Pin;
use std::sync::{Arc, Mutex};
use std::task::{Context, Poll};

use async_trait::async_trait;
use derivative::Derivative;
use mz_ore::netio::AsyncReady;
use mz_server_core::TlsMode;
use tokio::io::{self, AsyncRead, AsyncWrite, Interest, ReadBuf, Ready};
use tokio_openssl::SslStream;
use tokio_postgres::error::SqlState;

use crate::ErrorResponse;

pub const CONN_UUID_KEY: &str = "mz_connection_uuid";
pub const MZ_FORWARDED_FOR_KEY: &str = "mz_forwarded_for";

#[derive(Debug)]
pub enum Conn<A> {
    Unencrypted(A),
    Ssl(SslStream<A>),
}

impl<A> Conn<A> {
    pub fn inner_mut(&mut self) -> &mut A {
        match self {
            Conn::Unencrypted(inner) => inner,
            Conn::Ssl(inner) => inner.get_mut(),
        }
    }

    /// Returns an error if tls_mode is incompatible with this connection's stream type.
    pub fn ensure_tls_compatibility(
        &self,
        tls_mode: &Option<TlsMode>,
    ) -> Result<(), ErrorResponse> {
        // Validate that the connection is compatible with the TLS mode.
        //
        // The match here explicitly spells out all cases to be resilient to
        // future changes to TlsMode.
        match (tls_mode, self) {
            (None, Conn::Unencrypted(_)) => (),
            (None, Conn::Ssl(_)) => unreachable!(),
            (Some(TlsMode::Allow), Conn::Unencrypted(_)) => (),
            (Some(TlsMode::Allow), Conn::Ssl(_)) => (),
            (Some(TlsMode::Require), Conn::Ssl(_)) => (),
            (Some(TlsMode::Require), Conn::Unencrypted(_)) => {
                return Err(ErrorResponse::fatal(
                    SqlState::SQLSERVER_REJECTED_ESTABLISHMENT_OF_SQLCONNECTION,
                    "TLS encryption is required",
                ));
            }
        }

        Ok(())
    }
}

impl<A> AsyncRead for Conn<A>
where
    A: AsyncRead + AsyncWrite + Unpin,
{
    fn poll_read(
        self: Pin<&mut Self>,
        cx: &mut Context,
        buf: &mut ReadBuf,
    ) -> Poll<io::Result<()>> {
        match self.get_mut() {
            Conn::Unencrypted(inner) => Pin::new(inner).poll_read(cx, buf),
            Conn::Ssl(inner) => Pin::new(inner).poll_read(cx, buf),
        }
    }
}

impl<A> AsyncWrite for Conn<A>
where
    A: AsyncRead + AsyncWrite + Unpin,
{
    fn poll_write(self: Pin<&mut Self>, cx: &mut Context, buf: &[u8]) -> Poll<io::Result<usize>> {
        match self.get_mut() {
            Conn::Unencrypted(inner) => Pin::new(inner).poll_write(cx, buf),
            Conn::Ssl(inner) => Pin::new(inner).poll_write(cx, buf),
        }
    }

    fn poll_flush(self: Pin<&mut Self>, cx: &mut Context) -> Poll<io::Result<()>> {
        match self.get_mut() {
            Conn::Unencrypted(inner) => Pin::new(inner).poll_flush(cx),
            Conn::Ssl(inner) => Pin::new(inner).poll_flush(cx),
        }
    }

    fn poll_shutdown(self: Pin<&mut Self>, cx: &mut Context) -> Poll<io::Result<()>> {
        match self.get_mut() {
            Conn::Unencrypted(inner) => Pin::new(inner).poll_shutdown(cx),
            Conn::Ssl(inner) => Pin::new(inner).poll_shutdown(cx),
        }
    }
}

#[async_trait]
impl<A> AsyncReady for Conn<A>
where
    A: AsyncRead + AsyncWrite + AsyncReady + Sync + Unpin,
{
    async fn ready(&self, interest: Interest) -> io::Result<Ready> {
        match self {
            Conn::Unencrypted(inner) => inner.ready(interest).await,
            Conn::Ssl(inner) => inner.ready(interest).await,
        }
    }
}

/// Metadata about a user that is required to allocate a [`ConnectionHandle`].
#[derive(Debug, Clone, Copy)]
pub struct UserMetadata {
    pub is_admin: bool,
    pub should_limit_connections: bool,
}

#[derive(Debug, Clone)]
pub struct ConnectionCounter {
    inner: Arc<Mutex<ConnectionCounterInner>>,
}

impl ConnectionCounter {
    /// Returns a [`ConnectionHandle`] which must be kept alive for the entire duration of the
    /// external connection.
    ///
    /// Dropping the [`ConnectionHandle`] decrements the connection count.
    pub fn allocate_connection(
        &self,
        metadata: impl Into<UserMetadata>,
    ) -> Result<Option<ConnectionHandle>, ConnectionError> {
        let mut inner = self.inner.lock().expect("environmentd panicked");
        let metadata = metadata.into();

        if !metadata.should_limit_connections {
            return Ok(None);
        }

        if (metadata.is_admin && inner.reserved_remaining() > 0)
            || inner.non_reserved_remaining() > 0
        {
            inner.inc_connection_count();
            Ok(Some(self.create_handle()))
        } else {
            Err(ConnectionError::TooManyConnections {
                current: inner.current,
                limit: inner.limit,
            })
        }
    }

    /// Updates the maximum number of connections we allow.
    pub fn update_limit(&self, new_limit: u64) {
        let mut inner = self.inner.lock().expect("environmentd panicked");
        inner.limit = new_limit;
    }

    /// Updates the number of connections we reserve for superusers.
    pub fn update_superuser_reserved(&self, new_reserve: u64) {
        let mut inner = self.inner.lock().expect("environmentd panicked");
        inner.superuser_reserved = new_reserve;
    }

    fn create_handle(&self) -> ConnectionHandle {
        let inner = Arc::clone(&self.inner);
        let decrement_fn = Box::new(move || {
            let mut inner = inner.lock().expect("environmentd panicked");
            inner.dec_connection_count();
        });

        ConnectionHandle {
            decrement_fn: Some(decrement_fn),
        }
    }
}

impl Default for ConnectionCounter {
    fn default() -> Self {
        let inner = ConnectionCounterInner::new(10, 3);
        ConnectionCounter {
            inner: Arc::new(Mutex::new(inner)),
        }
    }
}

#[derive(Debug)]
pub struct ConnectionCounterInner {
    /// Current number of connections.
    current: u64,
    /// Total number of connections allowed.
    limit: u64,
    /// Number of connections in `limit` we'll reserve for superusers.
    superuser_reserved: u64,
}

impl ConnectionCounterInner {
    fn new(limit: u64, superuser_reserved: u64) -> Self {
        assert!(superuser_reserved < limit);
        ConnectionCounterInner {
            current: 0,
            limit,
            superuser_reserved,
        }
    }

    fn inc_connection_count(&mut self) {
        self.current += 1;
    }

    fn dec_connection_count(&mut self) {
        self.current -= 1;
    }

    /// The number of connections still available to superusers.
    fn reserved_remaining(&self) -> u64 {
        // Use a saturating sub in case the limit is reduced below the number
        // of current connections.
        self.limit.saturating_sub(self.current)
    }

    /// The number of connections available to non-superusers.
    fn non_reserved_remaining(&self) -> u64 {
        // This ensures that at least a few connections remain for superusers.
        let limit = self.limit.saturating_sub(self.superuser_reserved);
        // Use a saturating sub in case the limit is reduced below the number
        // of current connections.
        limit.saturating_sub(self.current)
    }
}

/// Handle to an open connection, allows us to maintain a count of all connections.
///
/// When Drop-ed decrements the count of open connections.
#[derive(Derivative)]
#[derivative(Debug)]
pub struct ConnectionHandle {
    #[derivative(Debug = "ignore")]
    decrement_fn: Option<Box<dyn FnOnce() -> () + Send + Sync>>,
}

impl Drop for ConnectionHandle {
    fn drop(&mut self) {
        match self.decrement_fn.take() {
            Some(decrement_fn) => (decrement_fn)(),
            None => tracing::error!("ConnectionHandle dropped twice!?"),
        }
    }
}

#[derive(Debug)]
pub enum ConnectionError {
    /// There were too many connections
    TooManyConnections { current: u64, limit: u64 },
}