aws-ssm-bridge 0.2.0

Rust library implementing AWS Systems Manager Session Manager protocol
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
//! Type-safe AWS SSM document definitions
//!
//! This module provides strongly-typed representations of AWS SSM Session Manager
//! documents and their parameters, eliminating magic strings and enabling
//! compile-time validation.
//!
//! # AWS SSM Session Manager Documents
//!
//! AWS SSM Session Manager supports exactly **4 built-in documents**:
//!
//! | Document | Rust Type | Purpose |
//! |----------|-----------|---------|
//! | (none) | [`ShellSession`] | Standard interactive shell |
//! | `AWS-StartPortForwardingSession` | [`PortForwardingSession`] | Port forward to instance |
//! | `AWS-StartPortForwardingSessionToRemoteHost` | [`PortForwardingToRemoteHost`] | Port forward through instance |
//! | `AWS-StartInteractiveCommand` | [`InteractiveCommand`] | Execute specific commands |
//! | `AWS-StartSSHSession` | [`SshSession`] | SSH over Session Manager |
//!
//! **Note:** Non-interactive commands are handled by AWS Run Command (a different
//! service), not Session Manager. Documents like `AWS-RunShellScript` are for
//! Run Command, not sessions.
//!
//! # Design Philosophy
//!
//! Instead of using `HashMap<String, Vec<String>>` with magic keys like
//! `"portNumber"`, we define proper Rust types that encode the valid
//! parameters for each document.
//!
//! # Example
//!
//! ```rust,no_run
//! use aws_ssm_bridge::documents::{PortForwardingSession, SsmDocument};
//!
//! // Type-safe port forwarding configuration
//! let doc = PortForwardingSession::new(3306);
//!
//! // With optional local port
//! let doc = PortForwardingSession::builder()
//!     .remote_port(3306)
//!     .local_port(13306)
//!     .build();
//! ```

use crate::protocol::SessionType;
use std::collections::HashMap;

// =============================================================================
// Document Trait
// =============================================================================

/// Trait for SSM document types
///
/// Each document type implements this trait to provide:
/// - The document name
/// - The session type
/// - Parameters as a HashMap (for AWS API compatibility)
pub trait SsmDocument: Send + Sync {
    /// The AWS SSM document name (e.g., "AWS-StartPortForwardingSession")
    fn document_name(&self) -> &'static str;

    /// The session type for this document
    fn session_type(&self) -> SessionType;

    /// Convert parameters to HashMap for AWS API
    fn parameters(&self) -> HashMap<String, Vec<String>>;
}

// =============================================================================
// Port Forwarding Document
// =============================================================================

/// AWS-StartPortForwardingSession document
///
/// Forwards a remote port on an EC2 instance to a local port.
///
/// # Example
///
/// ```rust
/// use aws_ssm_bridge::documents::PortForwardingSession;
///
/// // Simple: just remote port
/// let doc = PortForwardingSession::new(3306);
///
/// // With local port specified
/// let doc = PortForwardingSession::builder()
///     .remote_port(3306)
///     .local_port(13306)
///     .build();
/// ```
#[derive(Debug, Clone)]
pub struct PortForwardingSession {
    /// Remote port on the EC2 instance
    pub remote_port: u16,
    /// Optional local port (if not specified, a random port is used)
    pub local_port: Option<u16>,
}

impl PortForwardingSession {
    /// Document name constant
    pub const DOCUMENT_NAME: &'static str = "AWS-StartPortForwardingSession";

    /// Create a new port forwarding session for the given remote port
    pub fn new(remote_port: u16) -> Self {
        Self {
            remote_port,
            local_port: None,
        }
    }

    /// Create a builder for more complex configurations
    pub fn builder() -> PortForwardingSessionBuilder {
        PortForwardingSessionBuilder::default()
    }
}

impl SsmDocument for PortForwardingSession {
    fn document_name(&self) -> &'static str {
        Self::DOCUMENT_NAME
    }

    fn session_type(&self) -> SessionType {
        SessionType::Port
    }

    fn parameters(&self) -> HashMap<String, Vec<String>> {
        let mut params = HashMap::new();
        params.insert("portNumber".to_string(), vec![self.remote_port.to_string()]);
        if let Some(local) = self.local_port {
            params.insert("localPortNumber".to_string(), vec![local.to_string()]);
        }
        params
    }
}

/// Builder for PortForwardingSession
#[derive(Debug, Default)]
pub struct PortForwardingSessionBuilder {
    remote_port: Option<u16>,
    local_port: Option<u16>,
}

impl PortForwardingSessionBuilder {
    /// Set the remote port (required)
    pub fn remote_port(mut self, port: u16) -> Self {
        self.remote_port = Some(port);
        self
    }

    /// Set the local port (optional)
    pub fn local_port(mut self, port: u16) -> Self {
        self.local_port = Some(port);
        self
    }

    /// Build the document configuration
    ///
    /// # Panics
    ///
    /// Panics if remote_port is not set. Use `try_build()` for fallible construction.
    pub fn build(self) -> PortForwardingSession {
        self.try_build().expect("remote_port is required")
    }

    /// Try to build the document configuration
    pub fn try_build(self) -> Option<PortForwardingSession> {
        Some(PortForwardingSession {
            remote_port: self.remote_port?,
            local_port: self.local_port,
        })
    }
}

// =============================================================================
// Port Forwarding to Remote Host Document
// =============================================================================

/// AWS-StartPortForwardingSessionToRemoteHost document
///
/// Forwards a port through an EC2 instance to a remote host (e.g., RDS).
///
/// # Example
///
/// ```rust
/// use aws_ssm_bridge::documents::PortForwardingToRemoteHost;
///
/// // Forward to an RDS instance through a bastion
/// let doc = PortForwardingToRemoteHost::new("mydb.cluster-xxx.us-east-1.rds.amazonaws.com", 3306);
/// ```
#[derive(Debug, Clone)]
pub struct PortForwardingToRemoteHost {
    /// Remote host to connect to (hostname or IP)
    pub host: String,
    /// Port on the remote host
    pub remote_port: u16,
    /// Optional local port
    pub local_port: Option<u16>,
}

impl PortForwardingToRemoteHost {
    /// Document name constant
    pub const DOCUMENT_NAME: &'static str = "AWS-StartPortForwardingSessionToRemoteHost";

    /// Create a new port forwarding session to a remote host
    pub fn new(host: impl Into<String>, remote_port: u16) -> Self {
        Self {
            host: host.into(),
            remote_port,
            local_port: None,
        }
    }

    /// Set the local port
    pub fn with_local_port(mut self, port: u16) -> Self {
        self.local_port = Some(port);
        self
    }
}

impl SsmDocument for PortForwardingToRemoteHost {
    fn document_name(&self) -> &'static str {
        Self::DOCUMENT_NAME
    }

    fn session_type(&self) -> SessionType {
        SessionType::Port
    }

    fn parameters(&self) -> HashMap<String, Vec<String>> {
        let mut params = HashMap::new();
        params.insert("host".to_string(), vec![self.host.clone()]);
        params.insert("portNumber".to_string(), vec![self.remote_port.to_string()]);
        if let Some(local) = self.local_port {
            params.insert("localPortNumber".to_string(), vec![local.to_string()]);
        }
        params
    }
}

// =============================================================================
// Interactive Shell Document
// =============================================================================

/// Standard interactive shell session (no custom document)
///
/// This is the default session type - a simple shell session.
#[derive(Debug, Clone, Default)]
pub struct ShellSession;

impl ShellSession {
    /// Create a new shell session
    pub fn new() -> Self {
        Self
    }
}

impl SsmDocument for ShellSession {
    fn document_name(&self) -> &'static str {
        // Shell sessions don't require a document name
        ""
    }

    fn session_type(&self) -> SessionType {
        SessionType::StandardStream
    }

    fn parameters(&self) -> HashMap<String, Vec<String>> {
        HashMap::new()
    }
}

// =============================================================================
// Interactive Command Document
// =============================================================================

/// AWS-StartInteractiveCommand document
///
/// Execute commands in an interactive shell session with specified commands.
/// Unlike a plain shell session, this runs specific commands.
///
/// # Example
///
/// ```rust
/// use aws_ssm_bridge::documents::InteractiveCommand;
///
/// // Run a single command
/// let doc = InteractiveCommand::new("top");
///
/// // Run multiple commands
/// let doc = InteractiveCommand::with_commands(vec!["cd /var/log".into(), "tail -f syslog".into()]);
/// ```
#[derive(Debug, Clone)]
pub struct InteractiveCommand {
    /// Commands to execute
    pub commands: Vec<String>,
}

impl InteractiveCommand {
    /// Document name constant
    pub const DOCUMENT_NAME: &'static str = "AWS-StartInteractiveCommand";

    /// Create with a single command
    pub fn new(command: impl Into<String>) -> Self {
        Self {
            commands: vec![command.into()],
        }
    }

    /// Create with multiple commands
    pub fn with_commands(commands: Vec<String>) -> Self {
        Self { commands }
    }
}

impl SsmDocument for InteractiveCommand {
    fn document_name(&self) -> &'static str {
        Self::DOCUMENT_NAME
    }

    fn session_type(&self) -> SessionType {
        SessionType::InteractiveCommands
    }

    fn parameters(&self) -> HashMap<String, Vec<String>> {
        let mut params = HashMap::new();
        params.insert("command".to_string(), self.commands.clone());
        params
    }
}

// =============================================================================
// SSH Session Document
// =============================================================================

/// AWS-StartSSHSession document
///
/// Start an SSH session through SSM (SSH over Session Manager).
/// This is used by `aws ssm start-session --document-name AWS-StartSSHSession`.
///
/// # Example
///
/// ```rust
/// use aws_ssm_bridge::documents::SshSession;
///
/// // Default SSH port (22)
/// let doc = SshSession::new();
///
/// // Custom SSH port
/// let doc = SshSession::with_port(2222);
/// ```
#[derive(Debug, Clone)]
pub struct SshSession {
    /// SSH port on the remote instance (default: 22)
    pub port: u16,
}

impl SshSession {
    /// Document name constant
    pub const DOCUMENT_NAME: &'static str = "AWS-StartSSHSession";

    /// Create a new SSH session with default port (22)
    pub fn new() -> Self {
        Self { port: 22 }
    }

    /// Create a new SSH session with custom port
    pub fn with_port(port: u16) -> Self {
        Self { port }
    }
}

impl Default for SshSession {
    fn default() -> Self {
        Self::new()
    }
}

impl SsmDocument for SshSession {
    fn document_name(&self) -> &'static str {
        Self::DOCUMENT_NAME
    }

    fn session_type(&self) -> SessionType {
        SessionType::Port
    }

    fn parameters(&self) -> HashMap<String, Vec<String>> {
        let mut params = HashMap::new();
        params.insert("portNumber".to_string(), vec![self.port.to_string()]);
        params
    }
}

// =============================================================================
// Tests
// =============================================================================

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

    #[test]
    fn test_port_forwarding_simple() {
        let doc = PortForwardingSession::new(3306);
        assert_eq!(doc.document_name(), "AWS-StartPortForwardingSession");
        assert_eq!(doc.session_type(), SessionType::Port);

        let params = doc.parameters();
        assert_eq!(params.get("portNumber"), Some(&vec!["3306".to_string()]));
        assert!(!params.contains_key("localPortNumber"));
    }

    #[test]
    fn test_port_forwarding_builder() {
        let doc = PortForwardingSession::builder()
            .remote_port(3306)
            .local_port(13306)
            .build();

        let params = doc.parameters();
        assert_eq!(params.get("portNumber"), Some(&vec!["3306".to_string()]));
        assert_eq!(
            params.get("localPortNumber"),
            Some(&vec!["13306".to_string()])
        );
    }

    #[test]
    fn test_port_forwarding_to_remote_host() {
        let doc =
            PortForwardingToRemoteHost::new("mydb.rds.amazonaws.com", 5432).with_local_port(15432);

        assert_eq!(
            doc.document_name(),
            "AWS-StartPortForwardingSessionToRemoteHost"
        );

        let params = doc.parameters();
        assert_eq!(
            params.get("host"),
            Some(&vec!["mydb.rds.amazonaws.com".to_string()])
        );
        assert_eq!(params.get("portNumber"), Some(&vec!["5432".to_string()]));
        assert_eq!(
            params.get("localPortNumber"),
            Some(&vec!["15432".to_string()])
        );
    }

    #[test]
    fn test_shell_session() {
        let doc = ShellSession::new();
        assert_eq!(doc.session_type(), SessionType::StandardStream);
        assert!(doc.parameters().is_empty());
    }

    #[test]
    fn test_interactive_command() {
        let doc = InteractiveCommand::new("top");
        assert_eq!(doc.document_name(), "AWS-StartInteractiveCommand");
        assert_eq!(doc.session_type(), SessionType::InteractiveCommands);

        let params = doc.parameters();
        assert_eq!(params.get("command"), Some(&vec!["top".to_string()]));
    }

    #[test]
    fn test_ssh_session() {
        let doc = SshSession::new();
        assert_eq!(doc.document_name(), "AWS-StartSSHSession");
        assert_eq!(doc.session_type(), SessionType::Port);

        let params = doc.parameters();
        assert_eq!(params.get("portNumber"), Some(&vec!["22".to_string()]));

        // Custom port
        let doc = SshSession::with_port(2222);
        let params = doc.parameters();
        assert_eq!(params.get("portNumber"), Some(&vec!["2222".to_string()]));
    }
}