purple-ssh 3.31.0

Free, open-source terminal SSH manager and SSH config editor in Rust for macOS and Linux that keeps ~/.ssh/config in sync with 18 cloud providers, monitors live SSH tunnels and manages Docker and Podman containers fleet-wide. Spin up a VM, it shows up. Destroy it, it dims. Plus scp, Vault SSH certs and an MCP server for AI agents. MIT licensed.
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
use super::*;

use std::sync::atomic::AtomicBool;

/// Unwrap the error side without printing the Ok side. `AwsCredentials` has no
/// `Debug` on purpose, so `expect_err` would leak a secret into a panic if it
/// ever compiled.
fn err_of(result: Result<AwsCredentials, ProviderError>, what: &str) -> ProviderError {
    match result {
        Ok(_) => panic!("{}", what),
        Err(e) => e,
    }
}

fn base_creds() -> AwsCredentials {
    AwsCredentials {
        access_key: "AKIDEXAMPLE".to_string(),
        secret_key: "wJalrXUtnFEMI/K7MDENG+bPxRfiCYEXAMPLEKEY".to_string(),
        session_token: None,
    }
}

fn temp_creds() -> AwsCredentials {
    AwsCredentials {
        access_key: "ASIAEXAMPLE".to_string(),
        secret_key: "SECRET".to_string(),
        session_token: Some("SESSION".to_string()),
    }
}

fn step(role_arn: &str) -> RoleStep {
    RoleStep {
        profile: "prod".to_string(),
        role_arn: role_arn.to_string(),
        role_session_name: String::new(),
        external_id: String::new(),
        duration_seconds: String::new(),
    }
}

/// A success body in the shape AWS documents for AssumeRole.
fn success_body(access: &str, secret: &str, token: &str) -> String {
    format!(
        r#"<AssumeRoleResponse xmlns="https://sts.amazonaws.com/doc/2011-06-15/">
  <AssumeRoleResult>
    <AssumedRoleUser>
      <Arn>arn:aws:sts::123456789012:assumed-role/demo/purple</Arn>
      <AssumedRoleId>ARO123EXAMPLE123:purple</AssumedRoleId>
    </AssumedRoleUser>
    <Credentials>
      <AccessKeyId>{}</AccessKeyId>
      <SecretAccessKey>{}</SecretAccessKey>
      <SessionToken>{}</SessionToken>
      <Expiration>2026-11-09T13:34:41Z</Expiration>
    </Credentials>
    <PackedPolicySize>6</PackedPolicySize>
  </AssumeRoleResult>
  <ResponseMetadata>
    <RequestId>c6104cbe-af31-11e0-8154-cbc7ccf896c7</RequestId>
  </ResponseMetadata>
</AssumeRoleResponse>"#,
        access, secret, token
    )
}

// =========================================================================
// Endpoints
// =========================================================================

#[test]
fn the_endpoint_is_regional_so_the_scope_region_is_the_request_region() {
    assert_eq!(
        region_endpoint("eu-west-1"),
        "https://sts.eu-west-1.amazonaws.com"
    );
    assert_eq!(
        region_endpoint("us-east-1"),
        "https://sts.us-east-1.amazonaws.com"
    );
}

#[test]
fn govcloud_uses_the_commercial_suffix() {
    assert_eq!(
        region_endpoint("us-gov-west-1"),
        "https://sts.us-gov-west-1.amazonaws.com"
    );
}

// =========================================================================
// Session name
// =========================================================================

#[test]
fn an_absent_session_name_falls_back_to_the_default() {
    assert_eq!(session_name(&step("arn:r")), DEFAULT_ROLE_SESSION_NAME);
}

#[test]
fn a_valid_session_name_is_used_as_written() {
    let mut s = step("arn:r");
    s.role_session_name = "ci-build_7".to_string();
    assert_eq!(session_name(&s), "ci-build_7");
}

#[test]
fn an_illegal_session_name_is_replaced_rather_than_failing_the_sync() {
    let mut s = step("arn:r");
    s.role_session_name = "has spaces".to_string();
    assert_eq!(session_name(&s), DEFAULT_ROLE_SESSION_NAME);
}

#[test]
fn session_name_length_bounds_match_the_service() {
    assert!(!is_valid_session_name("a"));
    assert!(is_valid_session_name("ab"));
    assert!(is_valid_session_name(&"a".repeat(64)));
    assert!(!is_valid_session_name(&"a".repeat(65)));
    assert!(!is_valid_session_name(""));
}

#[test]
fn session_name_charset_matches_the_service() {
    assert!(is_valid_session_name("A_z0+=,.@-"));
    assert!(!is_valid_session_name("bad/slash"));
    assert!(!is_valid_session_name("bad:colon"));
}

// =========================================================================
// Duration
// =========================================================================

#[test]
fn an_absent_duration_lets_the_service_default_apply() {
    assert_eq!(duration_seconds(&step("arn:r"), false), None);
}

#[test]
fn a_duration_inside_the_range_is_sent_as_written() {
    let mut s = step("arn:r");
    s.duration_seconds = "7200".to_string();
    assert_eq!(duration_seconds(&s, false), Some(7200));
}

#[test]
fn a_duration_outside_the_range_is_dropped() {
    let mut s = step("arn:r");
    s.duration_seconds = "60".to_string();
    assert_eq!(duration_seconds(&s, false), None);
    s.duration_seconds = "99999".to_string();
    assert_eq!(duration_seconds(&s, false), None);
}

#[test]
fn a_non_numeric_duration_is_dropped() {
    let mut s = step("arn:r");
    s.duration_seconds = "an hour".to_string();
    assert_eq!(duration_seconds(&s, false), None);
}

#[test]
fn chaining_caps_the_duration_at_one_hour() {
    // The literal, not the constant: AWS refuses a chained AssumeRole above
    // 3600 whatever the role's own maximum allows, so a wrong constant has to
    // fail here rather than only against the live service.
    let mut s = step("arn:r");
    s.duration_seconds = "43200".to_string();
    assert_eq!(duration_seconds(&s, true), Some(3600));
}

#[test]
fn chaining_leaves_a_shorter_duration_alone() {
    let mut s = step("arn:r");
    s.duration_seconds = "1800".to_string();
    assert_eq!(duration_seconds(&s, true), Some(1800));
}

// =========================================================================
// Response parsing
// =========================================================================

#[test]
fn credentials_are_read_out_of_a_documented_response() {
    let creds = parse_credentials(&success_body("ASIANEW", "NEWSECRET", "NEWTOKEN"))
        .expect("credentials present");
    assert_eq!(creds.access_key, "ASIANEW");
    assert_eq!(creds.secret_key, "NEWSECRET");
    assert_eq!(creds.session_token.as_deref(), Some("NEWTOKEN"));
}

#[test]
fn credentials_are_matched_by_name_not_position() {
    // GetSessionToken documents a different child order, so the parser must
    // not depend on it.
    let body = r#"<AssumeRoleResponse><AssumeRoleResult><Credentials>
      <SessionToken>TOK</SessionToken>
      <SecretAccessKey>SEC</SecretAccessKey>
      <Expiration>2026-01-01T00:00:00Z</Expiration>
      <AccessKeyId>AKI</AccessKeyId>
    </Credentials></AssumeRoleResult></AssumeRoleResponse>"#;
    let creds = parse_credentials(body).expect("credentials present");
    assert_eq!(creds.access_key, "AKI");
    assert_eq!(creds.secret_key, "SEC");
    assert_eq!(creds.session_token.as_deref(), Some("TOK"));
}

#[test]
fn a_wrapped_session_token_is_put_back_together() {
    // A session token is base64 and never holds whitespace, so the pieces
    // join straight up. A space where the wrap was would be signed into the
    // request and AWS would refuse it.
    let body = "<Credentials><AccessKeyId>AKI</AccessKeyId>\
        <SecretAccessKey>SEC</SecretAccessKey>\
        <SessionToken>AAA\n      BBB</SessionToken></Credentials>";
    let creds = parse_credentials(body).expect("credentials present");
    assert_eq!(creds.session_token.as_deref(), Some("AAABBB"));
}

#[test]
fn a_response_missing_a_field_is_not_credentials() {
    let body = "<Credentials><AccessKeyId>AKI</AccessKeyId></Credentials>";
    assert!(parse_credentials(body).is_none());
}

#[test]
fn an_empty_field_is_not_credentials() {
    let body = "<Credentials><AccessKeyId></AccessKeyId>\
        <SecretAccessKey>SEC</SecretAccessKey>\
        <SessionToken>TOK</SessionToken></Credentials>";
    assert!(parse_credentials(body).is_none());
}

#[test]
fn unknown_fields_do_not_break_parsing() {
    // PackedPolicySize is deprecated and SessionTokenSize is newer, so the
    // parser must tolerate both presence and absence.
    let body = "<Credentials><AccessKeyId>AKI</AccessKeyId>\
        <SecretAccessKey>SEC</SecretAccessKey>\
        <SessionToken>TOK</SessionToken>\
        <Expiration>2026-01-01T00:00:00Z</Expiration>\
        <SessionTokenSize>42</SessionTokenSize></Credentials>";
    assert!(parse_credentials(body).is_some());
}

// =========================================================================
// Error parsing
// =========================================================================

#[test]
fn an_error_body_yields_code_and_message() {
    let body = r#"<ErrorResponse xmlns="https://sts.amazonaws.com/doc/2011-06-15/">
  <Error>
    <Type>Sender</Type>
    <Code>AccessDenied</Code>
    <Message>User is not authorized to perform sts:AssumeRole</Message>
  </Error>
  <RequestId>abc</RequestId>
</ErrorResponse>"#;
    assert_eq!(
        parse_error(body).as_deref(),
        Some("AccessDenied: User is not authorized to perform sts:AssumeRole")
    );
}

#[test]
fn an_error_without_a_message_still_yields_the_code() {
    let body = "<ErrorResponse><Error><Code>ExpiredTokenException</Code></Error></ErrorResponse>";
    assert_eq!(parse_error(body).as_deref(), Some("ExpiredTokenException"));
}

#[test]
fn a_success_body_is_not_an_error() {
    assert!(parse_error(&success_body("A", "B", "C")).is_none());
}

#[test]
fn a_very_long_error_message_is_cut() {
    let body = format!(
        "<Error><Code>X</Code><Message>{}</Message></Error>",
        "y".repeat(MAX_ERROR_MESSAGE * 2)
    );
    let parsed = parse_error(&body).expect("error parsed");
    assert_eq!(parsed.chars().count(), MAX_ERROR_MESSAGE);
}

#[test]
fn an_error_message_wrapped_across_lines_is_collapsed() {
    let body = "<Error><Code>X</Code><Message>one\n    two</Message></Error>";
    assert_eq!(parse_error(body).as_deref(), Some("X: one two"));
}

// =========================================================================
// HTTP roundtrip
// =========================================================================

#[test]
fn assume_chain_signs_and_returns_the_new_credentials() {
    let mut server = mockito::Server::new();
    let mock = server
        .mock("GET", "/")
        .match_query(mockito::Matcher::AllOf(vec![
            mockito::Matcher::UrlEncoded("Action".into(), "AssumeRole".into()),
            mockito::Matcher::UrlEncoded("Version".into(), STS_API_VERSION.into()),
            mockito::Matcher::UrlEncoded("RoleArn".into(), "arn:aws:iam::1:role/Admin".into()),
            mockito::Matcher::UrlEncoded(
                "RoleSessionName".into(),
                DEFAULT_ROLE_SESSION_NAME.into(),
            ),
        ]))
        .match_header(
            "Authorization",
            mockito::Matcher::Regex("sts/aws4_request".into()),
        )
        .with_status(200)
        .with_body(success_body("ASIANEW", "NEWSECRET", "NEWTOKEN"))
        .create();

    let url = server.url();
    let creds = assume_chain_with_endpoint(
        &super::super::http_agent(),
        base_creds(),
        &[step("arn:aws:iam::1:role/Admin")],
        "eu-west-1",
        &AtomicBool::new(false),
        |_| url.clone(),
    )
    .expect("assume succeeds");

    mock.assert();
    assert_eq!(creds.access_key, "ASIANEW");
    assert_eq!(creds.session_token.as_deref(), Some("NEWTOKEN"));
}

#[test]
fn an_empty_chain_returns_the_base_untouched() {
    let creds = assume_chain_with_endpoint(
        &super::super::http_agent(),
        base_creds(),
        &[],
        "eu-west-1",
        &AtomicBool::new(false),
        |_| "http://127.0.0.1:1".to_string(),
    )
    .expect("no roles means no call");
    assert_eq!(creds.access_key, "AKIDEXAMPLE");
}

#[test]
fn a_temporary_base_sends_its_session_token() {
    let mut server = mockito::Server::new();
    let mock = server
        .mock("GET", "/")
        .match_query(mockito::Matcher::Any)
        .match_header("x-amz-security-token", "SESSION")
        .with_status(200)
        .with_body(success_body("ASIANEW", "NEWSECRET", "NEWTOKEN"))
        .create();

    let url = server.url();
    assume_chain_with_endpoint(
        &super::super::http_agent(),
        temp_creds(),
        &[step("arn:r")],
        "eu-west-1",
        &AtomicBool::new(false),
        |_| url.clone(),
    )
    .expect("assume succeeds");
    mock.assert();
}

#[test]
fn a_two_step_chain_makes_two_calls_and_keeps_the_last_credentials() {
    let mut server = mockito::Server::new();
    let mock = server
        .mock("GET", "/")
        .match_query(mockito::Matcher::Any)
        .with_status(200)
        .with_body(success_body("ASIAFINAL", "FINALSECRET", "FINALTOKEN"))
        .expect(2)
        .create();

    let url = server.url();
    let creds = assume_chain_with_endpoint(
        &super::super::http_agent(),
        base_creds(),
        &[step("arn:inner"), step("arn:outer")],
        "eu-west-1",
        &AtomicBool::new(false),
        |_| url.clone(),
    )
    .expect("assume succeeds");

    mock.assert();
    assert_eq!(creds.access_key, "ASIAFINAL");
}

#[test]
fn a_refused_role_names_the_role_and_the_service_code() {
    // 403 with the body STS actually sends. The status has to be taken as
    // data, or the HTTP client drops the body and the sentence naming the
    // principal, the role and the condition goes with it.
    let mut server = mockito::Server::new();
    let _mock = server
        .mock("GET", "/")
        .match_query(mockito::Matcher::Any)
        .with_status(403)
        .with_body(
            "<ErrorResponse><Error><Type>Sender</Type><Code>AccessDenied</Code><Message>User: arn:aws:iam::111122223333:user/eric is not authorized to perform: sts:AssumeRole on resource: arn:aws:iam::1:role/Admin</Message></Error></ErrorResponse>",
        )
        .create();

    let url = server.url();
    let err = err_of(
        assume_chain_with_endpoint(
            &super::super::http_agent(),
            base_creds(),
            &[step("arn:aws:iam::1:role/Admin")],
            "eu-west-1",
            &AtomicBool::new(false),
            |_| url.clone(),
        ),
        "a refused role must fail",
    );
    let msg = err.to_string();
    assert!(
        msg.contains("arn:aws:iam::1:role/Admin"),
        "role not named: {msg}"
    );
    assert!(msg.contains("AccessDenied"), "code not carried: {msg}");
    assert!(
        msg.contains("is not authorized to perform: sts:AssumeRole"),
        "the service's own sentence was dropped with the body: {msg}"
    );
}

#[test]
fn an_http_error_status_still_names_the_role() {
    let mut server = mockito::Server::new();
    let _mock = server
        .mock("GET", "/")
        .match_query(mockito::Matcher::Any)
        .with_status(403)
        .with_body("<ErrorResponse/>")
        .create();

    let url = server.url();
    let err = err_of(
        assume_chain_with_endpoint(
            &super::super::http_agent(),
            base_creds(),
            &[step("arn:aws:iam::1:role/Admin")],
            "eu-west-1",
            &AtomicBool::new(false),
            |_| url.clone(),
        ),
        "a 403 must fail",
    );
    assert!(err.to_string().contains("arn:aws:iam::1:role/Admin"));
}

#[test]
fn a_success_body_without_credentials_is_reported_as_unparsable() {
    let mut server = mockito::Server::new();
    let _mock = server
        .mock("GET", "/")
        .match_query(mockito::Matcher::Any)
        .with_status(200)
        .with_body("<AssumeRoleResponse><AssumeRoleResult/></AssumeRoleResponse>")
        .create();

    let url = server.url();
    let err = err_of(
        assume_chain_with_endpoint(
            &super::super::http_agent(),
            base_creds(),
            &[step("arn:r")],
            "eu-west-1",
            &AtomicBool::new(false),
            |_| url.clone(),
        ),
        "no credentials must fail",
    );
    assert!(matches!(err, ProviderError::Parse(_)));
}

#[test]
fn a_cancelled_sync_stops_before_the_first_call() {
    let err = err_of(
        assume_chain_with_endpoint(
            &super::super::http_agent(),
            base_creds(),
            &[step("arn:r")],
            "eu-west-1",
            &AtomicBool::new(true),
            |_| "http://127.0.0.1:1".to_string(),
        ),
        "cancel must stop it",
    );
    assert!(matches!(err, ProviderError::Cancelled));
}

#[test]
fn a_throttled_assume_role_is_reported_as_rate_limiting() {
    // STS answers 429 with a normal error body, so the status has to be read
    // before the body or throttling would surface as a refused role.
    let mut server = mockito::Server::new();
    let _mock = server
        .mock("GET", "/")
        .match_query(mockito::Matcher::Any)
        .with_status(429)
        .with_body(
            "<ErrorResponse><Error><Code>Throttling</Code><Message>Rate exceeded</Message></Error></ErrorResponse>",
        )
        .create();

    let url = server.url();
    let err = err_of(
        assume_chain_with_endpoint(
            &super::super::http_agent(),
            base_creds(),
            &[step("arn:aws:iam::1:role/Admin")],
            "eu-west-1",
            &AtomicBool::new(false),
            |_| url.clone(),
        ),
        "429 must fail",
    );
    assert!(
        matches!(err, ProviderError::RateLimited),
        "expected RateLimited, got {err:?}"
    );
}