regent-sdk 0.9.1

Multi-paradigm configuration management system as a library
Documentation
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
use bytes::Bytes;
use russh::Disconnect;
use russh::Preferred;
use russh::client::AuthResult;
use russh::client::{Config, Handle, Handler};
use russh::keys::key::PrivateKeyWithHashAlg;
use russh::keys::{load_secret_key, ssh_key};
use russh::{Channel, ChannelMsg};
use serde::Deserialize;
use serde::Serialize;
use std::io::Cursor;
use std::path::PathBuf;
use std::sync::Arc;
use tokio::time::{Duration, timeout};
#[allow(unused)]
use tracing::{debug, error, info, trace, warn};

use crate::command::CommandResult;
use crate::error::RegentError;
use crate::hosts::handlers::HostHandler;
use crate::hosts::handlers::final_command;
use crate::hosts::handlers::localhost::WhichUser;
use crate::hosts::privilege::Credentials;
use crate::hosts::privilege::LoginKey;
use crate::hosts::privilege::LoginKeyRef;
use crate::hosts::privilege::Privilege;
// use crate::secrets::SecretProvider;
use crate::secrets::SecretReference;

// #[derive(Clone)]
// pub struct Ssh2HostHandler {
//     auth: Ssh2AuthMethod,
//     session: Handle<Ssh2Client>,
// }

pub enum Ssh2HostHandler {
    NotConnected(Ssh2AuthMethod),
    Connected(Ssh2AuthMethod, Handle<Ssh2Client>),
}

struct Ssh2Client {}

impl Handler for Ssh2Client {
    type Error = russh::Error;

    async fn check_server_key(
        &mut self,
        _server_public_key: &ssh_key::PublicKey,
    ) -> Result<bool, Self::Error> {
        Ok(true)
    }
}

impl Clone for Ssh2HostHandler {
    fn clone(&self) -> Self {
        match self {
            Ssh2HostHandler::NotConnected(auth) => Ssh2HostHandler::NotConnected(auth.clone()),
            Ssh2HostHandler::Connected(auth, _) => {
                // When cloning a connected handler, return a new NotConnected handler
                // The caller will need to reconnect
                Ssh2HostHandler::NotConnected(auth.clone())
            }
        }
    }
}

impl std::fmt::Debug for Ssh2HostHandler {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Ssh2HostHandler::NotConnected(auth) => {
                f.debug_tuple("NotConnected").field(auth).finish()
            }
            Ssh2HostHandler::Connected(auth, _) => f
                .debug_tuple("Connected")
                .field(auth)
                .field(&"*SESSION HANDLE*")
                .finish(),
        }
    }
}

impl HostHandler for Ssh2HostHandler {
    async fn connect(&mut self, endpoint: &str) -> Result<(), RegentError> {
        // Check whether a session is already enabled or not (init() might have already been called
        // on this host)
        if self.is_connected().await {
            return Ok(());
        }

        let address_and_port: Vec<&str> = endpoint.split(':').collect();
        if address_and_port.is_empty() {
            return Err(RegentError::FailedInitialization(
                "empty address".to_string(),
            ));
        }

        let address = address_and_port[0];
        let ssh_port: u16 = match address_and_port.get(1) {
            Some(port) => match port.parse::<u16>() {
                Ok(p) => p,
                Err(e) => {
                    return Err(RegentError::FailedInitialization(format!(
                        "invalid port: {}",
                        e
                    )));
                }
            },
            None => 22,
        };

        let addrs = format!("{}:{}", address, ssh_port);

        // Create russh configuration
        let config = Arc::new(Config {
            keepalive_interval: Some(Duration::from_secs(5)),
            keepalive_max: 3,
            nodelay: true,
            window_size: 2097152,       // 2 MB
            maximum_packet_size: 32768, // 32 KB
            channel_buffer_size: 128,
            inactivity_timeout: None,
            preferred: Preferred::default(),
            ..Default::default()
        });

        // Connect using russh
        let client_handler = Ssh2Client {};
        let connection_timeout = Duration::from_secs(10);
        let mut handle = match timeout(
            connection_timeout,
            russh::client::connect(config, addrs, client_handler),
        )
        .await
        {
            Ok(connection_result) => match connection_result {
                Ok(h) => h,
                Err(e) => {
                    return Err(RegentError::FailedTcpBinding(format!("{:?}", e)));
                }
            },
            Err(_details) => {
                return Err(RegentError::FailureToEstablishConnection(format!(
                    "Connection timeout elapsed ({} ms)",
                    connection_timeout.as_millis()
                )));
            }
        };

        // Authenticate based on the auth method
        if let Ssh2HostHandler::NotConnected(auth_method) = self {
            match auth_method {
                Ssh2AuthMethod::UsernamePassword(credentials) => {
                    match handle
                        .authenticate_password(credentials.username(), credentials.password())
                        .await
                    {
                        Ok(auth_result) => match auth_result {
                            AuthResult::Success => {
                                *self = Ssh2HostHandler::Connected(auth_method.clone(), handle);
                                return Ok(());
                            }
                            AuthResult::Failure {
                                remaining_methods,
                                partial_success,
                            } => {
                                return Err(RegentError::FailedInitialization(
                                    "Password authentication failed".to_string(),
                                ));
                            }
                        },
                        Err(e) => {
                            return Err(RegentError::FailedInitialization(format!(
                                "Password authentication error: {:?}",
                                e
                            )));
                        }
                    }
                }
                Ssh2AuthMethod::Key(login_key) => {
                    // Load the private key from the string
                    match load_secret_key(login_key.key(), None) {
                        Ok(key_pair) => {
                            let best_hash = match handle.best_supported_rsa_hash().await {
                                Ok(h) => h.flatten(),
                                Err(e) => {
                                    return Err(RegentError::FailedInitialization(format!(
                                        "Failed to get supported hash: {:?}",
                                        e
                                    )));
                                }
                            };
                            let key_with_alg =
                                PrivateKeyWithHashAlg::new(Arc::new(key_pair), best_hash);
                            match handle
                                .authenticate_publickey(login_key.username(), key_with_alg)
                                .await
                            {
                                Ok(auth_result) => match auth_result {
                                    AuthResult::Success => {
                                        *self =
                                            Ssh2HostHandler::Connected(auth_method.clone(), handle);
                                        return Ok(());
                                    }
                                    AuthResult::Failure {
                                        remaining_methods,
                                        partial_success,
                                    } => {
                                        return Err(RegentError::FailedInitialization(
                                            "Public key authentication failed".to_string(),
                                        ));
                                    }
                                },
                                Err(e) => {
                                    return Err(RegentError::FailedInitialization(format!(
                                        "Public key authentication error: {:?}",
                                        e
                                    )));
                                }
                            }
                        }
                        Err(error_details) => {
                            return Err(RegentError::FailedInitialization(format!(
                                "Failed to load key: {:?}",
                                error_details
                            )));
                        }
                    }
                }
            }
        } else {
            Ok(())
        }
    }

    async fn is_connected(&mut self) -> bool {
        match self {
            Ssh2HostHandler::NotConnected(_) => false,
            Ssh2HostHandler::Connected(auth_method, session) => match session.send_ping().await {
                Ok(()) => true,
                Err(_error_details) => {
                    *self = Ssh2HostHandler::NotConnected(auth_method.clone());
                    false
                }
            },
        }
    }

    async fn disconnect(&mut self) -> Result<(), RegentError> {
        match self {
            Ssh2HostHandler::NotConnected(_) => Ok(()),
            Ssh2HostHandler::Connected(auth_method, session) => {
                match session
                    .disconnect(Disconnect::ByApplication, "regent", "English")
                    .await
                {
                    Ok(()) => {
                        *self = Ssh2HostHandler::NotConnected(auth_method.clone());
                        Ok(())
                    }
                    Err(error_details) => Err(RegentError::ProblemWithHostConnection(format!(
                        "{:?}",
                        error_details
                    ))),
                }
            }
        }
    }

    async fn is_this_command_available(
        &mut self,
        command: &str,
        privilege: &Privilege,
    ) -> Result<bool, RegentError> {
        let check_cmd_content = format!("command -v {}", command);
        let check_cmd_result = self
            .run_command(check_cmd_content.as_str(), privilege)
            .await;

        match check_cmd_result {
            Ok(cmd_result) => {
                if cmd_result.return_code == 0 {
                    Ok(true)
                } else {
                    Ok(false)
                }
            }
            Err(e) => {
                return Err(RegentError::FailureToRunCommand(format!("{:?}", e)));
            }
        }
    }

    async fn run_command(
        &mut self,
        command: &str,
        privilege: &Privilege,
    ) -> Result<CommandResult, RegentError> {
        match self {
            Ssh2HostHandler::NotConnected(_auth_method) => {
                return Err(RegentError::NotConnectedToHost);
            }
            Ssh2HostHandler::Connected(_auth_method, session_handle) => {
                match session_handle.channel_open_session().await {
                    Ok(mut channel) => {
                        let final_command =
                            final_command(command, privilege, &WhichUser::CurrentUser);

                        if let Err(details) = channel.exec(true, final_command).await {
                            return Err(RegentError::FailureToRunCommand(format!("{:?}", details)));
                        }

                        let mut return_code = 1;
                        let mut command_output = String::new();

                        loop {
                            // There's an event available on the session channel
                            let Some(msg) = channel.wait().await else {
                                break;
                            };
                            match msg {
                                // Write data to the buffer
                                ChannelMsg::Data { ref data } => {
                                    command_output.push_str(&String::from_utf8_lossy(data));
                                }
                                ChannelMsg::ExitStatus { exit_status } => {
                                    return_code = exit_status;
                                    // cannot leave the loop immediately, there might still be more data to receive
                                }
                                _ => {}
                            }
                        }

                        Ok(CommandResult {
                            return_code: return_code.into(),
                            stdout: command_output,
                            stderr: String::new(),
                        })
                    }
                    Err(e) => Err(RegentError::FailureToEstablishConnection(e.to_string())),
                }
            }
        }
    }

    #[cfg(feature = "windows")]
    async fn run_windows_command(&mut self, command: &str) -> Result<CommandResult, RegentError> {
        match self {
            Ssh2HostHandler::NotConnected(_auth_method) => {
                return Err(RegentError::NotConnectedToHost);
            }
            Ssh2HostHandler::Connected(_auth_method, session_handle) => {
                match session_handle.channel_open_session().await {
                    Ok(mut channel) => {
                        let final_command = format!("cmd /C {}", command);

                        if let Err(details) = channel.exec(true, final_command).await {
                            return Err(RegentError::FailureToRunCommand(format!("{:?}", details)));
                        }

                        let mut return_code = 1;
                        let mut command_output = String::new();

                        loop {
                            // There's an event available on the session channel
                            let Some(msg) = channel.wait().await else {
                                break;
                            };
                            match msg {
                                // Write data to the buffer
                                ChannelMsg::Data { ref data } => {
                                    command_output.push_str(&String::from_utf8_lossy(data));
                                }
                                ChannelMsg::ExitStatus { exit_status } => {
                                    return_code = exit_status;
                                    // cannot leave the loop immediately, there might still be more data to receive
                                }
                                _ => {}
                            }
                        }

                        Ok(CommandResult {
                            return_code: return_code.into(),
                            stdout: command_output,
                            stderr: String::new(),
                        })
                    }

                    Err(e) => Err(RegentError::FailureToEstablishConnection(e.to_string())),
                }
            }
        }
    }

    async fn get_file(&mut self, path: PathBuf) -> Result<Vec<u8>, RegentError> {
        match self {
            Ssh2HostHandler::NotConnected(_auth_method) => {
                return Err(RegentError::NotConnectedToHost);
            }
            Ssh2HostHandler::Connected(_auth_method, session_handle) => {
                match session_handle.channel_open_session().await {
                    Ok(mut channel) => {
                        // 1. SCP request in "source" mode (-f)
                        let cmd = format!("scp -f \"{}\"", path.display());
                        if let Err(e) = channel.exec(true, cmd).await {
                            return Err(RegentError::FailedToGetFile(format!(
                                "Russh exec error: {:?}",
                                e
                            )));
                        }

                        // Stream buffer to isolate the text header from the binary content
                        let mut stream_buffer = Vec::new();

                        // 2. Initialization: Send a NULL byte to indicate that the client is ready
                        if let Err(e) = channel.data(Cursor::new(&[0x00][..])).await {
                            return Err(RegentError::FailedToGetFile(format!(
                                "Russh data transfer error: {:?}",
                                e
                            )));
                        }

                        // 3. Read and parse the header (until \n)
                        let mut header_line = String::new();
                        loop {
                            if stream_buffer.is_empty() {
                                match fetch_next_chunk(&mut channel).await {
                                    Ok(chunk) => stream_buffer.extend_from_slice(chunk.as_ref()),
                                    Err(e) => return Err(e),
                                }
                            }

                            let chunk = stream_buffer.remove(0);
                            if chunk == b'\n' {
                                break;
                            }
                            header_line.push(chunk as char);
                        }

                        // 4. Validate the received header type (\x01 = Warning, \x02 = Fatal Error)
                        if header_line.starts_with('\x01') || header_line.starts_with('\x02') {
                            return Err(RegentError::FailedToGetFile(header_line[1..].to_string()));
                        }
                        if !header_line.starts_with('C') {
                            return Err(RegentError::FailedToGetFile(format!(
                                "Invalid SCP file header: {}",
                                header_line
                            )));
                        }

                        // 5. Extract the size and name of the original file
                        let parts: Vec<&str> = header_line[1..].splitn(3, ' ').collect();
                        if parts.len() < 3 {
                            return Err(RegentError::FailedToGetFile(
                                "Malformed SCP header format".to_string(),
                            ));
                        }

                        let file_size: usize;
                        if let Ok(size) = parts[0].parse() {
                            file_size = size;
                        } else {
                            return Err(RegentError::FailedToGetFile(
                                "Invalid file size integer in header".to_string(),
                            ));
                        }
                        let filename = parts[1].to_string();

                        // 6. Send ACK to validate the header and start the binary stream
                        if let Err(e) = channel.data(Cursor::new(&[0x00][..])).await {
                            return Err(RegentError::FailedToGetFile(format!(
                                "Russh data transfer error: {:?}",
                                e
                            )));
                        }

                        // 7. Allocate the final memory vector
                        let mut file_payload = Vec::with_capacity(file_size);

                        // If file bytes were already in the buffer during header parsing
                        if !stream_buffer.is_empty() {
                            let buffered_take = std::cmp::min(stream_buffer.len(), file_size);
                            file_payload.extend(stream_buffer.drain(..buffered_take));
                        }

                        // Main network loop: fill the Vec until reaching `file_size`
                        while file_payload.len() < file_size {
                            let remaining_needed = file_size - file_payload.len();

                            let mut chunk;
                            match fetch_next_chunk(&mut channel).await {
                                Ok(c) => chunk = c,
                                Err(e) => return Err(e),
                            }

                            if chunk.len() <= remaining_needed {
                                file_payload.extend_from_slice(&chunk);
                            } else {
                                // The network block exceeds the file size (it contains the final ACK token)
                                let excess = chunk.split_off(remaining_needed);
                                file_payload.extend_from_slice(&chunk);
                                stream_buffer.extend_from_slice(&excess); // Keep the excess for final verification
                            }
                        }

                        // 8. Verify the final status byte sent by the server (\x00 expected)
                        if stream_buffer.is_empty() {
                            match fetch_next_chunk(&mut channel).await {
                                Ok(chunk) => stream_buffer.extend_from_slice(chunk.as_ref()),
                                Err(e) => return Err(e),
                            }
                        }

                        let end_ack = stream_buffer.remove(0);
                        if end_ack != 0x00 {
                            return Err(RegentError::FailedToGetFile(format!(
                                "Unexpected end transfer token status byte: {}",
                                end_ack
                            )));
                        }

                        // 9. Send the final ACK to close the protocol
                        if let Err(e) = channel.data(Cursor::new(&[0x00][..])).await {
                            return Err(RegentError::FailedToGetFile(format!(
                                "Russh data transfer error: {:?}",
                                e
                            )));
                        }
                        if let Err(e) = channel.eof().await {
                            return Err(RegentError::FailedToGetFile(format!(
                                "Russh channel EOF error: {:?}",
                                e
                            )));
                        }

                        Ok(file_payload)
                    }
                    Err(e) => Err(RegentError::FailureToEstablishConnection(e.to_string())),
                }
            }
        }
    }
}

// impl Ssh2HostHandler {
//     pub fn from(auth: Ssh2AuthMethod) -> Result<Ssh2HostHandler, RegentError> {
//         match Session::new() {
//             Ok(session) => Ok(Ssh2HostHandler { auth, session }),
//             Err(details) => Err(RegentError::ProblemWithHostConnection(format!(
//                 "Failed to create new SSH2 session : {:?}",
//                 details
//             ))),
//         }
//     }

//     pub fn username_password(
//         username: &str,
//         password: &str,
//     ) -> Result<Ssh2HostHandler, RegentError> {
//         match Ssh2HostHandler::from(Ssh2AuthMethod::UsernamePassword(Credentials::from(
//             username, password,
//         ))) {
//             Ok(ssh2_host_handler) => Ok(ssh2_host_handler),
//             Err(details) => Err(RegentError::ProblemWithHostConnection(format!(
//                 "Failed to create new Ssh2HostHandler : {:?}",
//                 details
//             ))),
//         }
//     }

//     pub fn key(username: &str, key: String) -> Result<Ssh2HostHandler, RegentError> {
//         match Ssh2HostHandler::from(Ssh2AuthMethod::Key(LoginKey::from(
//             username.to_string(),
//             key,
//         ))) {
//             Ok(ssh2_host_handler) => Ok(ssh2_host_handler),
//             Err(details) => Err(RegentError::ProblemWithHostConnection(format!(
//                 "Failed to create new Ssh2HostHandler : {:?}",
//                 details
//             ))),
//         }
//     }
// }

#[derive(Clone, Serialize, Deserialize)]
#[serde(rename_all = "PascalCase")]
pub enum Ssh2AuthMethod {
    UsernamePassword(Credentials),

    Key(LoginKey),
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "PascalCase")]
pub enum Ssh2AuthReference {
    UsernamePassword(SecretReference),
    Key(LoginKeyRef),
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "PascalCase")]
#[serde(deny_unknown_fields)]
pub struct Ssh2Auth {
    pub auth_method: Ssh2AuthReference,
}

impl Ssh2Auth {
    pub fn username_password(secret_reference: &str, secret_provider_name: Option<&str>) -> Self {
        match secret_provider_name {
            Some(name) => Self {
                auth_method: Ssh2AuthReference::UsernamePassword(SecretReference::from(
                    secret_reference,
                    Some(name.to_string()),
                )),
            },
            None => Self {
                auth_method: Ssh2AuthReference::UsernamePassword(SecretReference::from(
                    secret_reference,
                    None,
                )),
            },
        }
    }

    pub fn key(username: &str, key_secret_reference: SecretReference) -> Self {
        Self {
            auth_method: Ssh2AuthReference::Key(LoginKeyRef::from(
                username.to_string(),
                key_secret_reference,
            )),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_deserialize_username_password() {
        let yaml = r#"
            !UsernamePassword
              Username: "testuser"
              Password: "testpass"
        "#;
        let auth_method = yaml_serde::from_str::<Ssh2AuthMethod>(yaml);
        matches!(auth_method, Ok(Ssh2AuthMethod::UsernamePassword(_)));
    }

    #[test]
    fn test_deserialize_key_file() {
        let yaml = r#"
            !Key
              Username: testuser
              Key: /path/to/private/key
        "#;
        let auth_method = yaml_serde::from_str::<Ssh2AuthMethod>(yaml);
        matches!(auth_method, Ok(Ssh2AuthMethod::Key(_)));
    }
}

impl std::fmt::Debug for Ssh2AuthMethod {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        match self {
            Ssh2AuthMethod::UsernamePassword(creds) => {
                write!(
                    f,
                    "UsernamePassword(Credentials {{ username: {:?}, password: \"*REDACTED*\" }})",
                    creds.username()
                )
            }
            Ssh2AuthMethod::Key(login_key_path) => {
                write!(f, "Key(({:?}, *REDACTED*))", login_key_path.username())
            }
        }
    }
}

async fn fetch_next_chunk(channel: &mut Channel<russh::client::Msg>) -> Result<Bytes, RegentError> {
    while let Some(msg) = channel.wait().await {
        if let ChannelMsg::Data { data } = msg {
            if !data.is_empty() {
                return Ok(data);
            }
        }
    }
    Err(RegentError::FailedToGetFile(
        "Unexpected end of SSH channel stream".to_string(),
    ))
}