support_kit/
ssh.rs

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
use std::{future::Future, sync::Arc, time::Duration};

use async_trait::async_trait;
use russh::{client, keys, ChannelMsg};
use tokio::io::AsyncWriteExt;

use crate::{Deployment, SshError};

struct SshHostConfig {
    address: String,
    port: u16,
    user: String,
    key: String,
}

impl SshHostConfig {
    pub fn new(address: String, port: u16, user: String, key: String) -> Self {
        Self {
            address,
            port,
            user,
            key,
        }
    }
}

pub struct SshHost {
    _config: SshHostConfig,
    session: SshSession,
}

impl SshHost {
    pub async fn connect(
        address: impl AsRef<str>,
        port: u16,
        user: impl AsRef<str>,
        key: impl AsRef<str>,
    ) -> Result<Self, SshError> {
        let config = SshHostConfig::new(
            address.as_ref().into(),
            port,
            user.as_ref().into(),
            key.as_ref().into(),
        );
        Ok(Self {
            session: SshSession::connect(&config).await?,
            _config: config,
        })
    }

    pub async fn run_cmd<T>(&self, cmd: Vec<T>) -> Result<(), SshError>
    where
        T: AsRef<str>,
    {
        self.session.run_cmd(cmd).await
    }
}

struct SshSession {
    connection: client::Handle<SshConnection>,
}

impl SshSession {
    pub async fn connect(host: &SshHostConfig) -> Result<Self, SshError> {
        let config = Arc::new(client::Config {
            inactivity_timeout: Some(Duration::from_secs(5)),
            ..<_>::default()
        });

        let mut session =
            client::connect(config, (host.address.as_ref(), host.port), SshConnection).await?;

        let key_pair = keys::load_secret_key(&host.key, None)?;

        let auth_res = session
            .authenticate_publickey(&host.user, Arc::new(key_pair))
            .await?;

        if !auth_res {
            return Err(SshError::AuthenticationFailed);
        }

        tracing::debug!("ssh session established: {address}", address = host.address);

        Ok(SshSession {
            connection: session,
        })
    }

    pub async fn run_cmd<T>(&self, command: Vec<T>) -> Result<(), SshError>
    where
        T: AsRef<str>,
    {
        let mut channel = self.connection.channel_open_session().await?;
        let command = command
            .into_iter()
            .map(|x| shell_escape::escape(x.as_ref().to_owned().into()))
            .collect::<Vec<_>>()
            .join(" ");

        channel.exec(true, command).await?;

        let mut code = None;
        let mut stdout = tokio::io::stdout();

        loop {
            // There's an event available on the session channel
            let Some(msg) = channel.wait().await else {
                break;
            };
            match msg {
                // Write data to the terminal
                ChannelMsg::Data { ref data } => {
                    stdout.write_all(data).await?;
                    stdout.flush().await?;
                }
                // The command has returned an exit code
                ChannelMsg::ExitStatus { exit_status } => {
                    code = Some(exit_status);
                    // cannot leave the loop immediately, there might still be more data to receive
                }
                other => {
                    tracing::debug!("channel message: {:?}", other);
                }
            }
        }

        // Wait for the channel to close
        channel.close().await?;

        // report code

        if let Some(code) = code {
            println!("Exit code: {}", code);
        }

        Ok(())
    }
}

struct SshConnection;

// the methods are async w/ async_trait, so that should be imported if you want to use them
#[async_trait]
impl client::Handler for SshConnection {
    type Error = russh::Error;
    async fn check_server_key(
        &mut self,
        _server_public_key: &keys::key::PublicKey,
    ) -> Result<bool, Self::Error> {
        Ok(true)
    }
}

pub struct SshControl;

impl SshControl {
    pub async fn on_hosts<Func, Fut>(
        deployment: &Deployment,
        callback_fn: Func,
    ) -> Result<(), SshError>
    where
        Func: Fn(SshHost) -> Fut,
        Fut: Future<Output = Result<(), SshError>>,
    {
        for host in deployment.hosts.clone() {
            let connection = SshHost::connect(
                host.address,
                host.port.unwrap_or(22),
                host.user.unwrap_or("root".into()),
                host.auth.unwrap_or("~/.ssh/id_rsa".into()),
            )
            .await?;

            callback_fn(connection).await?;
        }

        Ok(())
    }
}