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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
// 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::collections::BTreeSet;
use std::fmt;
use std::fs::{self, File};
use std::io::Write;
use std::net::{Ipv4Addr, SocketAddr};
use std::os::unix::fs::PermissionsExt;
use std::sync::atomic::{AtomicU16, Ordering};
use std::sync::{Arc, Mutex};
use std::time::Duration;

use anyhow::bail;
use itertools::Itertools;
use mz_ore::error::ErrorExt;
use mz_ore::task::{self, AbortOnDropHandle};
use openssh::{ForwardType, Session};
use rand::rngs::StdRng;
use rand::{Rng, SeedableRng};
use serde::{Deserialize, Serialize};
use tokio::time;
use tracing::{info, warn};

use crate::keys::SshKeyPair;

// TODO(benesch): allow configuring the following connection parameters via
// server configuration parameters.

pub const DEFAULT_CHECK_INTERVAL: Duration = Duration::from_secs(30);
pub const DEFAULT_CONNECT_TIMEOUT: Duration = Duration::from_secs(30);

/// TCP idle timeouts of 30s are common in the wild. An idle timeout of 10s
/// is comfortably beneath that threshold without being overly chatty.
pub const DEFAULT_KEEPALIVES_IDLE: Duration = Duration::from_secs(10);

/// Configuration of Ssh session and tunnel timeouts.
#[derive(Copy, Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct SshTimeoutConfig {
    /// How often to check whether the SSH session is still alive.
    pub check_interval: Duration,
    /// The timeout to use when establishing the connection to the SSH server.
    pub connect_timeout: Duration,
    /// The idle time after which the SSH control leader process should send a
    /// keepalive packet to the SSH server to determine whether the server is
    /// still alive.
    pub keepalives_idle: Duration,
}

impl Default for SshTimeoutConfig {
    fn default() -> SshTimeoutConfig {
        SshTimeoutConfig {
            check_interval: DEFAULT_CHECK_INTERVAL,
            connect_timeout: DEFAULT_CONNECT_TIMEOUT,
            keepalives_idle: DEFAULT_KEEPALIVES_IDLE,
        }
    }
}

/// Specifies an SSH tunnel.
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct SshTunnelConfig {
    /// The hostname/IP of the SSH bastion server.
    /// If multiple hosts are specified, they are tried in order.
    pub host: BTreeSet<String>,
    /// The port to connect to.
    pub port: u16,
    /// The name of the user to connect as.
    pub user: String,
    /// The SSH key pair to authenticate with.
    pub key_pair: SshKeyPair,
}

impl fmt::Debug for SshTunnelConfig {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("Tunnel")
            .field("host", &self.host)
            .field("port", &self.port)
            .field("user", &self.user)
            // Omit keys from debug output.
            .finish()
    }
}

impl fmt::Display for SshTunnelConfig {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "{}@{}:{}",
            self.user,
            self.host.iter().join(","),
            self.port
        )
    }
}

/// The status of a running SSH tunnel.
#[derive(Clone, Debug)]
pub enum SshTunnelStatus {
    /// The SSH tunnel is healthy.
    Running,
    /// The SSH tunnel is broken, with the given error message.
    Errored(String),
}

impl SshTunnelConfig {
    /// Establishes a connection to the specified host and port via the
    /// configured SSH tunnel.
    ///
    /// Returns a handle to the SSH tunnel. The SSH tunnel is automatically shut
    /// down when the handle is dropped.
    pub async fn connect(
        &self,
        remote_host: &str,
        remote_port: u16,
        timeout_config: SshTimeoutConfig,
    ) -> Result<SshTunnelHandle, anyhow::Error> {
        let tunnel_id = format!("{}:{} via {}", remote_host, remote_port, self);

        // N.B.
        //
        // We could probably move this into the look and use the above channel to report this
        // initial connection error, but this is simpler and easier to read!
        info!(%tunnel_id, "connecting to ssh tunnel");
        let mut session = match connect(self, timeout_config).await {
            Ok(s) => s,
            Err(e) => {
                warn!(%tunnel_id, "failed to connect to ssh tunnel: {}", e.display_with_causes());
                return Err(e);
            }
        };
        let local_port = match port_forward(&session, remote_host, remote_port).await {
            Ok(local_port) => local_port,
            Err(e) => {
                warn!(%tunnel_id, "failed to forward port through ssh tunnel: {}", e.display_with_causes());
                return Err(e);
            }
        };
        info!(%tunnel_id, %local_port, "connected to ssh tunnel");
        let local_port = Arc::new(AtomicU16::new(local_port));
        let status = Arc::new(Mutex::new(SshTunnelStatus::Running));

        let join_handle = task::spawn(|| format!("ssh_session_{remote_host}:{remote_port}"), {
            let config = self.clone();
            let remote_host = remote_host.to_string();
            let local_port = Arc::clone(&local_port);
            let status = Arc::clone(&status);
            async move {
                scopeguard::defer! {
                    info!(%tunnel_id, "terminating ssh tunnel");
                }
                let mut interval = time::interval(timeout_config.check_interval);
                // Just in case checking takes a long time.
                interval.set_missed_tick_behavior(time::MissedTickBehavior::Skip);
                // The first tick happens immediately.
                interval.tick().await;
                loop {
                    interval.tick().await;
                    if let Err(e) = session.check().await {
                        warn!(%tunnel_id, "ssh tunnel unhealthy: {}", e.display_with_causes());
                        let s = match connect(&config, timeout_config).await {
                            Ok(s) => s,
                            Err(e) => {
                                warn!(%tunnel_id, "reconnection to ssh tunnel failed: {}", e.display_with_causes());
                                *status.lock().expect("poisoned") =
                                    SshTunnelStatus::Errored(e.to_string_with_causes());
                                continue;
                            }
                        };
                        let lp = match port_forward(&s, &remote_host, remote_port).await {
                            Ok(lp) => lp,
                            Err(e) => {
                                warn!(%tunnel_id, "reconnection to ssh tunnel failed: {}", e.display_with_causes());
                                *status.lock().expect("poisoned") =
                                    SshTunnelStatus::Errored(e.to_string_with_causes());
                                continue;
                            }
                        };
                        session = s;
                        local_port.store(lp, Ordering::SeqCst);
                        *status.lock().expect("poisoned") = SshTunnelStatus::Running;
                    }
                }
            }
        });

        Ok(SshTunnelHandle {
            local_port,
            status,
            _join_handle: join_handle.abort_on_drop(),
        })
    }

    /// Validates the SSH configuration by establishing a connection to the intermediate SSH
    /// bastion host. It does not set up a port forwarding tunnel.
    pub async fn validate(&self, timeout_config: SshTimeoutConfig) -> Result<(), anyhow::Error> {
        connect(self, timeout_config).await?;
        Ok(())
    }
}

/// A handle to a running SSH tunnel.
#[derive(Debug)]
pub struct SshTunnelHandle {
    local_port: Arc<AtomicU16>,
    status: Arc<Mutex<SshTunnelStatus>>,
    _join_handle: AbortOnDropHandle<()>,
}

impl SshTunnelHandle {
    /// Returns the local address at which the SSH tunnel is listening.
    pub fn local_addr(&self) -> SocketAddr {
        let port = self.local_port.load(Ordering::SeqCst);
        // Force use of IPv4 loopback. Do not use the hostname `localhost`, as
        // that can resolve to IPv6, and the SSH tunnel is only listening for
        // IPv4 connections.
        SocketAddr::from((Ipv4Addr::LOCALHOST, port))
    }

    /// Returns the current status of the SSH tunnel.
    ///
    /// Note this status may be stale, as the health of the underlying SSH
    /// tunnel is only checked periodically.
    pub fn check_status(&self) -> SshTunnelStatus {
        self.status.lock().expect("poisoned").clone()
    }
}

async fn connect(
    config: &SshTunnelConfig,
    timeout_config: SshTimeoutConfig,
) -> Result<Session, anyhow::Error> {
    let tempdir = tempfile::Builder::new()
        .prefix("ssh-tunnel-key")
        .tempdir()?;
    let path = tempdir.path().join("key");
    let mut tempfile = File::create(&path)?;
    // Grant read and write permissions on the file.
    tempfile.set_permissions(std::fs::Permissions::from_mode(0o600))?;
    tempfile.write_all(config.key_pair.ssh_private_key().as_bytes())?;
    // Remove write permissions as soon as the key is written.
    // Mostly helpful to ensure the file is not accidentally overwritten.
    tempfile.set_permissions(std::fs::Permissions::from_mode(0o400))?;

    // Try connecting to each host in turn.
    let mut connect_err = None;
    for host in &config.host {
        // Bastion hosts (and therefore keys) tend to change, so we don't want
        // to lock ourselves into trusting only the first we see. In any case,
        // recording a known host would only last as long as the life of a
        // storage pod, so it doesn't offer any protection.
        match openssh::SessionBuilder::default()
            .known_hosts_check(openssh::KnownHosts::Accept)
            .user_known_hosts_file("/dev/null")
            .user(config.user.clone())
            .port(config.port)
            .keyfile(&path)
            .server_alive_interval(timeout_config.keepalives_idle)
            .connect_timeout(timeout_config.connect_timeout)
            .connect_mux(host.clone())
            .await
        {
            Ok(session) => {
                // Delete the private key for safety: since `ssh` still has an open
                // handle to it, it still has access to the key.
                drop(tempfile);
                fs::remove_file(&path)?;
                drop(tempdir);

                // Ensure session is healthy.
                session.check().await?;

                return Ok(session);
            }
            Err(err) => {
                connect_err = Some(err);
            }
        }
    }
    Err(connect_err
        .map(Into::into)
        .unwrap_or_else(|| anyhow::anyhow!("no hosts to connect to")))
}

async fn port_forward(session: &Session, host: &str, port: u16) -> Result<u16, anyhow::Error> {
    // Loop trying to find an open port.
    for _ in 0..50 {
        // Choose a dynamic port according to RFC 6335.
        let mut rng = StdRng::from_entropy();
        let local_port: u16 = rng.gen_range(49152..65535);

        // Force use of IPv4 loopback. Do not use the hostname `localhost`,
        // as that can resolve to IPv6, and the SSH tunnel is only listening
        // for IPv4 connections.
        let local = openssh::Socket::from((Ipv4Addr::LOCALHOST, local_port));
        let remote = openssh::Socket::new(host, port);

        match session
            .request_port_forward(ForwardType::Local, local, remote)
            .await
        {
            Ok(_) => return Ok(local_port),
            Err(err) => match err {
                openssh::Error::SshMux(openssh_mux_client::Error::RequestFailure(e))
                    if &*e == "Port forwarding failed" =>
                {
                    info!("port {local_port} already in use; testing another port");
                }
                _ => {
                    warn!("ssh connection failed: {}", err.display_with_causes());
                    bail!("failed to open SSH tunnel: {}", err.display_with_causes())
                }
            },
        };
    }
    // If we failed to find an open port after 50 attempts,
    // something is seriously wrong.
    bail!("failed to find an open port for SSH tunnel")
}