mitoxide-ssh 0.1.0

SSH transport layer for Mitoxide
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
//! Transport abstraction and implementations

use async_trait::async_trait;
use crate::{Connection, TransportError};
use std::collections::HashMap;
use std::path::PathBuf;
use std::process::Stdio;
use tokio::process::{Child, Command};
use tracing::{debug, info};

/// Transport abstraction for different connection types
#[async_trait]
pub trait Transport: Send + Sync {
    /// Connect to the remote host
    async fn connect(&mut self) -> Result<Connection, TransportError>;
    
    /// Bootstrap the agent on the remote host
    async fn bootstrap_agent(&mut self, agent_binary: &[u8]) -> Result<(), TransportError>;
    
    /// Get connection information
    fn connection_info(&self) -> ConnectionInfo;
    
    /// Test connectivity to the remote host
    async fn test_connection(&mut self) -> Result<(), TransportError>;
}

/// Connection information
#[derive(Debug, Clone)]
pub struct ConnectionInfo {
    /// Remote hostname or IP
    pub host: String,
    /// Remote port
    pub port: u16,
    /// Username
    pub username: String,
    /// Connection type
    pub transport_type: TransportType,
}

/// Transport type enumeration
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum TransportType {
    /// SSH with subprocess
    SshSubprocess,
    /// SSH with libssh2
    SshLibssh2,
    /// Local process (for testing)
    Local,
}

/// SSH configuration
#[derive(Debug, Clone)]
pub struct SshConfig {
    /// Remote hostname or IP
    pub host: String,
    /// Remote port (default: 22)
    pub port: u16,
    /// Username
    pub username: String,
    /// SSH key path
    pub key_path: Option<PathBuf>,
    /// SSH options
    pub options: HashMap<String, String>,
    /// Connection timeout in seconds
    pub connect_timeout: u64,
    /// Command timeout in seconds
    pub command_timeout: u64,
}

impl Default for SshConfig {
    fn default() -> Self {
        Self {
            host: "localhost".to_string(),
            port: 22,
            username: "root".to_string(),
            key_path: None,
            options: HashMap::new(),
            connect_timeout: 30,
            command_timeout: 300,
        }
    }
}

/// SSH stdio transport implementation using subprocess
pub struct StdioTransport {
    /// SSH configuration
    config: SshConfig,
    /// Active SSH process
    ssh_process: Option<Child>,
    /// Connection state
    connected: bool,
}

impl StdioTransport {
    /// Create a new stdio transport
    pub fn new(config: SshConfig) -> Self {
        Self {
            config,
            ssh_process: None,
            connected: false,
        }
    }
    
    /// Build SSH command arguments
    fn build_ssh_args(&self) -> Vec<String> {
        let mut args = vec![
            "-o".to_string(), "BatchMode=yes".to_string(),
            "-o".to_string(), "StrictHostKeyChecking=no".to_string(),
            "-o".to_string(), format!("ConnectTimeout={}", self.config.connect_timeout),
            "-p".to_string(), self.config.port.to_string(),
        ];
        
        // Add SSH key if specified
        if let Some(key_path) = &self.config.key_path {
            args.push("-i".to_string());
            args.push(key_path.to_string_lossy().to_string());
        }
        
        // Add custom options
        for (key, value) in &self.config.options {
            args.push("-o".to_string());
            args.push(format!("{}={}", key, value));
        }
        
        // Add target
        args.push(format!("{}@{}", self.config.username, self.config.host));
        
        args
    }
    
    /// Execute a command over SSH
    async fn execute_command(&mut self, command: &str) -> Result<String, TransportError> {
        let mut ssh_args = self.build_ssh_args();
        ssh_args.push(command.to_string());
        
        debug!("Executing SSH command: ssh {}", ssh_args.join(" "));
        
        let output = Command::new("ssh")
            .args(&ssh_args)
            .output()
            .await
            .map_err(|e| TransportError::Connection(format!("Failed to execute SSH: {}", e)))?;
        
        if !output.status.success() {
            let stderr = String::from_utf8_lossy(&output.stderr);
            return Err(TransportError::Connection(format!("SSH command failed: {}", stderr)));
        }
        
        Ok(String::from_utf8_lossy(&output.stdout).to_string())
    }
    
    /// Start an interactive SSH session
    async fn start_interactive_session(&mut self) -> Result<Child, TransportError> {
        let ssh_args = self.build_ssh_args();
        
        debug!("Starting interactive SSH session: ssh {}", ssh_args.join(" "));
        
        let child = Command::new("ssh")
            .args(&ssh_args)
            .stdin(Stdio::piped())
            .stdout(Stdio::piped())
            .stderr(Stdio::piped())
            .spawn()
            .map_err(|e| TransportError::Connection(format!("Failed to start SSH: {}", e)))?;
        
        Ok(child)
    }
}

#[async_trait]
impl Transport for StdioTransport {
    async fn connect(&mut self) -> Result<Connection, TransportError> {
        if self.connected {
            return Ok(Connection::new(self.ssh_process.take()));
        }
        
        info!("Connecting to {}@{}:{}", self.config.username, self.config.host, self.config.port);
        
        // Test basic connectivity first
        self.test_connection().await?;
        
        // Start interactive session
        let child = self.start_interactive_session().await?;
        self.ssh_process = Some(child);
        self.connected = true;
        
        info!("Successfully connected to {}@{}", self.config.username, self.config.host);
        Ok(Connection::new(self.ssh_process.take()))
    }
    
    async fn bootstrap_agent(&mut self, agent_binary: &[u8]) -> Result<(), TransportError> {
        info!("Bootstrapping agent on {}@{}", self.config.username, self.config.host);
        
        // Detect platform
        let platform_info = self.execute_command("uname -m && uname -s").await?;
        debug!("Remote platform: {}", platform_info.trim());
        
        // Try to use memfd_create for in-memory execution (Linux only)
        let bootstrap_script = format!(
            r#"
            set -e
            
            # Try memfd_create approach first (Linux)
            if command -v python3 >/dev/null 2>&1; then
                python3 -c "
import os, sys
try:
    import ctypes
    libc = ctypes.CDLL('libc.so.6')
    fd = libc.syscall(319, b'mitoxide-agent', 1)  # memfd_create
    if fd >= 0:
        os.write(fd, sys.stdin.buffer.read())
        os.fexecve(fd, ['/proc/self/fd/%d' % fd], os.environ)
except:
    pass
# Fallback to temp file
import tempfile
with tempfile.NamedTemporaryFile(delete=False) as f:
    f.write(sys.stdin.buffer.read())
    f.flush()
    os.chmod(f.name, 0o755)
    os.execv(f.name, [f.name])
"
            elif [ -d /tmp ] && [ -w /tmp ]; then
                # Fallback to /tmp
                AGENT_PATH="/tmp/mitoxide-agent-$$"
                cat > "$AGENT_PATH"
                chmod +x "$AGENT_PATH"
                exec "$AGENT_PATH"
                rm -f "$AGENT_PATH" 2>/dev/null || true
            else
                echo "No suitable location for agent bootstrap" >&2
                exit 1
            fi
            "#
        );
        
        // Send agent binary through stdin
        let mut ssh_args = self.build_ssh_args();
        ssh_args.push("bash".to_string());
        
        let mut child = Command::new("ssh")
            .args(&ssh_args)
            .stdin(Stdio::piped())
            .stdout(Stdio::piped())
            .stderr(Stdio::piped())
            .spawn()
            .map_err(|e| TransportError::Bootstrap(format!("Failed to start SSH for bootstrap: {}", e)))?;
        
        // Send bootstrap script and agent binary
        if let Some(stdin) = child.stdin.as_mut() {
            use tokio::io::AsyncWriteExt;
            
            stdin.write_all(bootstrap_script.as_bytes()).await
                .map_err(|e| TransportError::Bootstrap(format!("Failed to write bootstrap script: {}", e)))?;
            
            stdin.write_all(agent_binary).await
                .map_err(|e| TransportError::Bootstrap(format!("Failed to write agent binary: {}", e)))?;
            
            stdin.shutdown().await
                .map_err(|e| TransportError::Bootstrap(format!("Failed to close stdin: {}", e)))?;
        }
        
        // Wait for bootstrap to complete
        let output = child.wait_with_output().await
            .map_err(|e| TransportError::Bootstrap(format!("Bootstrap process failed: {}", e)))?;
        
        if !output.status.success() {
            let stderr = String::from_utf8_lossy(&output.stderr);
            return Err(TransportError::Bootstrap(format!("Agent bootstrap failed: {}", stderr)));
        }
        
        info!("Agent successfully bootstrapped on {}@{}", self.config.username, self.config.host);
        Ok(())
    }
    
    fn connection_info(&self) -> ConnectionInfo {
        ConnectionInfo {
            host: self.config.host.clone(),
            port: self.config.port,
            username: self.config.username.clone(),
            transport_type: TransportType::SshSubprocess,
        }
    }
    
    async fn test_connection(&mut self) -> Result<(), TransportError> {
        debug!("Testing connection to {}@{}", self.config.username, self.config.host);
        
        // Simple connectivity test
        let result = self.execute_command("echo 'connection_test'").await?;
        
        if !result.trim().contains("connection_test") {
            return Err(TransportError::Connection("Connection test failed".to_string()));
        }
        
        debug!("Connection test successful");
        Ok(())
    }
}

impl Drop for StdioTransport {
    fn drop(&mut self) {
        if let Some(mut child) = self.ssh_process.take() {
            // Try to kill the SSH process gracefully
            let _ = child.start_kill();
        }
    }
}

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

    
    #[test]
    fn test_ssh_config_default() {
        let config = SshConfig::default();
        assert_eq!(config.host, "localhost");
        assert_eq!(config.port, 22);
        assert_eq!(config.username, "root");
        assert_eq!(config.connect_timeout, 30);
        assert_eq!(config.command_timeout, 300);
    }
    
    #[test]
    fn test_stdio_transport_creation() {
        let config = SshConfig::default();
        let transport = StdioTransport::new(config.clone());
        
        let info = transport.connection_info();
        assert_eq!(info.host, config.host);
        assert_eq!(info.port, config.port);
        assert_eq!(info.username, config.username);
        assert_eq!(info.transport_type, TransportType::SshSubprocess);
    }
    
    #[test]
    fn test_ssh_args_building() {
        let mut config = SshConfig::default();
        config.host = "example.com".to_string();
        config.port = 2222;
        config.username = "testuser".to_string();
        config.key_path = Some(PathBuf::from("/path/to/key"));
        config.options.insert("ServerAliveInterval".to_string(), "60".to_string());
        
        let transport = StdioTransport::new(config);
        let args = transport.build_ssh_args();
        
        assert!(args.contains(&"-p".to_string()));
        assert!(args.contains(&"2222".to_string()));
        assert!(args.contains(&"-i".to_string()));
        assert!(args.contains(&"/path/to/key".to_string()));
        assert!(args.contains(&"-o".to_string()));
        assert!(args.contains(&"ServerAliveInterval=60".to_string()));
        assert!(args.contains(&"testuser@example.com".to_string()));
    }
    
    #[test]
    fn test_connection_info() {
        let config = SshConfig {
            host: "test.example.com".to_string(),
            port: 2222,
            username: "testuser".to_string(),
            ..Default::default()
        };
        
        let transport = StdioTransport::new(config);
        let info = transport.connection_info();
        
        assert_eq!(info.host, "test.example.com");
        assert_eq!(info.port, 2222);
        assert_eq!(info.username, "testuser");
        assert_eq!(info.transport_type, TransportType::SshSubprocess);
    }
    
    // Mock transport for testing
    #[cfg(test)]
    pub struct MockTransport {
        should_fail: bool,
        connection_info: ConnectionInfo,
    }
    
    #[cfg(test)]
    impl MockTransport {
        pub fn new(should_fail: bool) -> Self {
            Self {
                should_fail,
                connection_info: ConnectionInfo {
                    host: "mock.example.com".to_string(),
                    port: 22,
                    username: "mockuser".to_string(),
                    transport_type: TransportType::Local,
                },
            }
        }
    }
    
    #[cfg(test)]
    #[async_trait]
    impl Transport for MockTransport {
        async fn connect(&mut self) -> Result<Connection, TransportError> {
            if self.should_fail {
                Err(TransportError::Connection("Mock connection failed".to_string()))
            } else {
                Ok(Connection::new(None))
            }
        }
        
        async fn bootstrap_agent(&mut self, _agent_binary: &[u8]) -> Result<(), TransportError> {
            if self.should_fail {
                Err(TransportError::Bootstrap("Mock bootstrap failed".to_string()))
            } else {
                Ok(())
            }
        }
        
        fn connection_info(&self) -> ConnectionInfo {
            self.connection_info.clone()
        }
        
        async fn test_connection(&mut self) -> Result<(), TransportError> {
            if self.should_fail {
                Err(TransportError::Connection("Mock test failed".to_string()))
            } else {
                Ok(())
            }
        }
    }
    
    #[tokio::test]
    async fn test_mock_transport_success() {
        let mut transport = MockTransport::new(false);
        
        assert!(transport.test_connection().await.is_ok());
        assert!(transport.connect().await.is_ok());
        assert!(transport.bootstrap_agent(b"test").await.is_ok());
    }
    
    #[tokio::test]
    async fn test_mock_transport_failure() {
        let mut transport = MockTransport::new(true);
        
        assert!(transport.test_connection().await.is_err());
        assert!(transport.connect().await.is_err());
        assert!(transport.bootstrap_agent(b"test").await.is_err());
    }
}