purple-ssh 1.27.0

Manage SSH configs and launch connections from the terminal. TUI host manager with search, tags, tunnels, command snippets, password management (keychain, 1Password, Bitwarden, pass, Vault), cloud sync (AWS EC2, DigitalOcean, Vultr, Linode, Hetzner, UpCloud, Proxmox VE, Scaleway, GCP), self-update and round-trip fidelity for ~/.ssh/config.
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
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
use std::collections::{HashMap, HashSet};
use std::sync::atomic::{AtomicBool, Ordering};

use hmac::{Hmac, Mac};
use sha2::{Digest, Sha256};

use super::{Provider, ProviderError, ProviderHost};

pub struct Aws {
    pub regions: Vec<String>,
    pub profile: String,
}

/// All commonly available AWS regions with display names.
/// Single source of truth. AWS_REGION_GROUPS references slices of this array.
pub const AWS_REGIONS: &[(&str, &str)] = &[
    // Americas (0..8)
    ("us-east-1", "N. Virginia"),
    ("us-east-2", "Ohio"),
    ("us-west-1", "N. California"),
    ("us-west-2", "Oregon"),
    ("ca-central-1", "Canada Central"),
    ("ca-west-1", "Canada West"),
    ("mx-central-1", "Mexico Central"),
    ("sa-east-1", "Sao Paulo"),
    // Europe (8..16)
    ("eu-west-1", "Ireland"),
    ("eu-west-2", "London"),
    ("eu-west-3", "Paris"),
    ("eu-central-1", "Frankfurt"),
    ("eu-central-2", "Zurich"),
    ("eu-south-1", "Milan"),
    ("eu-south-2", "Spain"),
    ("eu-north-1", "Stockholm"),
    // Asia Pacific (16..30)
    ("ap-northeast-1", "Tokyo"),
    ("ap-northeast-2", "Seoul"),
    ("ap-northeast-3", "Osaka"),
    ("ap-southeast-1", "Singapore"),
    ("ap-southeast-2", "Sydney"),
    ("ap-southeast-3", "Jakarta"),
    ("ap-southeast-4", "Melbourne"),
    ("ap-southeast-5", "Malaysia"),
    ("ap-southeast-6", "New Zealand"),
    ("ap-southeast-7", "Thailand"),
    ("ap-east-1", "Hong Kong"),
    ("ap-east-2", "Taipei"),
    ("ap-south-1", "Mumbai"),
    ("ap-south-2", "Hyderabad"),
    // Middle East / Africa (30..34)
    ("me-south-1", "Bahrain"),
    ("me-central-1", "UAE"),
    ("il-central-1", "Tel Aviv"),
    ("af-south-1", "Cape Town"),
];

/// Region group labels with start..end indices into AWS_REGIONS.
pub const AWS_REGION_GROUPS: &[(&str, usize, usize)] = &[
    ("Americas", 0, 8),
    ("Europe", 8, 16),
    ("Asia Pacific", 16, 30),
    ("Middle East / Africa", 30, 34),
];

// --- Credentials ---

struct AwsCredentials {
    access_key: String,
    secret_key: String,
}

fn resolve_credentials(token: &str, profile: &str) -> Result<AwsCredentials, ProviderError> {
    // Profile takes priority: read from ~/.aws/credentials
    if !profile.is_empty() {
        return read_credentials_file(profile);
    }
    // Token field: ACCESS_KEY_ID:SECRET_ACCESS_KEY
    if let Some((ak, sk)) = token.split_once(':') {
        if !ak.is_empty() && !sk.is_empty() {
            return Ok(AwsCredentials {
                access_key: ak.to_string(),
                secret_key: sk.to_string(),
            });
        }
    }
    // Environment variables
    if let (Ok(ak), Ok(sk)) = (
        std::env::var("AWS_ACCESS_KEY_ID"),
        std::env::var("AWS_SECRET_ACCESS_KEY"),
    ) {
        if !ak.is_empty() && !sk.is_empty() {
            return Ok(AwsCredentials {
                access_key: ak,
                secret_key: sk,
            });
        }
    }
    Err(ProviderError::AuthFailed)
}

/// Parse AWS credentials from INI content (testable without filesystem).
fn parse_credentials(content: &str, profile: &str) -> Option<AwsCredentials> {
    let header = format!("[{}]", profile);
    let mut in_section = false;
    let mut access_key = String::new();
    let mut secret_key = String::new();

    for line in content.lines() {
        let trimmed = line.trim();
        if trimmed.starts_with('[') {
            in_section = trimmed == header;
            continue;
        }
        if !in_section {
            continue;
        }
        if let Some((key, value)) = trimmed.split_once('=') {
            match key.trim() {
                "aws_access_key_id" => access_key = value.trim().to_string(),
                "aws_secret_access_key" => secret_key = value.trim().to_string(),
                _ => {}
            }
        }
    }

    if access_key.is_empty() || secret_key.is_empty() {
        None
    } else {
        Some(AwsCredentials {
            access_key,
            secret_key,
        })
    }
}

fn read_credentials_file(profile: &str) -> Result<AwsCredentials, ProviderError> {
    let path = dirs::home_dir()
        .ok_or(ProviderError::AuthFailed)?
        .join(".aws")
        .join("credentials");
    let content = std::fs::read_to_string(&path)
        .map_err(|_| ProviderError::AuthFailed)?;
    parse_credentials(&content, profile).ok_or(ProviderError::AuthFailed)
}

// --- SigV4 signing ---

fn hex_encode(bytes: &[u8]) -> String {
    bytes.iter().map(|b| format!("{:02x}", b)).collect()
}

fn sha256_hash(data: &[u8]) -> Vec<u8> {
    let mut hasher = Sha256::new();
    hasher.update(data);
    hasher.finalize().to_vec()
}

fn hmac_sha256(key: &[u8], data: &[u8]) -> Vec<u8> {
    let mut mac = Hmac::<Sha256>::new_from_slice(key).expect("HMAC accepts any key size");
    mac.update(data);
    mac.finalize().into_bytes().to_vec()
}

/// RFC 3986 URI encoding.
fn uri_encode(s: &str) -> String {
    let mut result = String::with_capacity(s.len());
    for byte in s.bytes() {
        match byte {
            b'A'..=b'Z' | b'a'..=b'z' | b'0'..=b'9' | b'-' | b'_' | b'.' | b'~' => {
                result.push(byte as char);
            }
            _ => {
                result.push_str(&format!("%{:02X}", byte));
            }
        }
    }
    result
}

/// Format epoch seconds as (timestamp, datestamp) for SigV4.
fn format_utc(epoch_secs: u64) -> (String, String) {
    let secs_per_day = 86400u64;
    let mut remaining_days = epoch_secs / secs_per_day;
    let day_secs = epoch_secs % secs_per_day;
    let hours = day_secs / 3600;
    let minutes = (day_secs % 3600) / 60;
    let seconds = day_secs % 60;

    let mut year = 1970u64;
    loop {
        let leap = year % 4 == 0 && (year % 100 != 0 || year % 400 == 0);
        let days_in_year = if leap { 366 } else { 365 };
        if remaining_days < days_in_year {
            break;
        }
        remaining_days -= days_in_year;
        year += 1;
    }

    let leap = year % 4 == 0 && (year % 100 != 0 || year % 400 == 0);
    let days_per_month: [u64; 12] = [
        31,
        if leap { 29 } else { 28 },
        31, 30, 31, 30, 31, 31, 30, 31, 30, 31,
    ];
    let mut month = 0usize;
    while month < 12 && remaining_days >= days_per_month[month] {
        remaining_days -= days_per_month[month];
        month += 1;
    }
    let day = remaining_days + 1;

    let timestamp = format!(
        "{:04}{:02}{:02}T{:02}{:02}{:02}Z",
        year,
        month + 1,
        day,
        hours,
        minutes,
        seconds,
    );
    let datestamp = format!("{:04}{:02}{:02}", year, month + 1, day);
    (timestamp, datestamp)
}

/// Build the SigV4 Authorization header value.
fn sign_request(
    creds: &AwsCredentials,
    region: &str,
    host: &str,
    query_string: &str,
    timestamp: &str,
    datestamp: &str,
) -> String {
    let payload_hash = hex_encode(&sha256_hash(b""));
    let canonical_headers = format!("host:{}\nx-amz-date:{}\n", host, timestamp);
    let signed_headers = "host;x-amz-date";

    let canonical_request = format!(
        "GET\n/\n{}\n{}\n{}\n{}",
        query_string, canonical_headers, signed_headers, payload_hash
    );

    let scope = format!("{}/{}/ec2/aws4_request", datestamp, region);
    let string_to_sign = format!(
        "AWS4-HMAC-SHA256\n{}\n{}\n{}",
        timestamp,
        scope,
        hex_encode(&sha256_hash(canonical_request.as_bytes())),
    );

    let k_date = hmac_sha256(
        format!("AWS4{}", creds.secret_key).as_bytes(),
        datestamp.as_bytes(),
    );
    let k_region = hmac_sha256(&k_date, region.as_bytes());
    let k_service = hmac_sha256(&k_region, b"ec2");
    let k_signing = hmac_sha256(&k_service, b"aws4_request");
    let signature = hex_encode(&hmac_sha256(&k_signing, string_to_sign.as_bytes()));

    format!(
        "AWS4-HMAC-SHA256 Credential={}/{}, SignedHeaders={}, Signature={}",
        creds.access_key, scope, signed_headers, signature
    )
}

// --- XML response structs ---

/// Generic wrapper for AWS XML lists that use repeated <item> elements.
#[derive(serde::Deserialize, Debug)]
#[serde(bound(deserialize = "T: serde::Deserialize<'de>"))]
struct ItemList<T> {
    #[serde(rename = "item", default = "Vec::new")]
    item: Vec<T>,
}

impl<T> Default for ItemList<T> {
    fn default() -> Self {
        Self { item: Vec::new() }
    }
}

#[derive(serde::Deserialize, Debug)]
struct DescribeInstancesResponse {
    #[serde(rename = "reservationSet", default)]
    reservation_set: ItemList<Reservation>,
    #[serde(rename = "nextToken", default)]
    next_token: Option<String>,
}

#[derive(serde::Deserialize, Debug)]
struct Reservation {
    #[serde(rename = "instancesSet", default)]
    instances_set: ItemList<Ec2Instance>,
}

#[derive(serde::Deserialize, Debug)]
struct Ec2Instance {
    #[serde(rename = "instanceId", default)]
    instance_id: String,
    #[serde(rename = "imageId", default)]
    image_id: String,
    #[serde(rename = "instanceState", default)]
    instance_state: InstanceState,
    #[serde(rename = "instanceType", default)]
    instance_type: String,
    #[serde(rename = "tagSet", default)]
    tag_set: ItemList<Ec2Tag>,
    #[serde(rename = "ipAddress", default)]
    ip_address: Option<String>,
}

#[derive(serde::Deserialize, Debug, Default)]
struct InstanceState {
    #[serde(default)]
    name: String,
}

#[derive(serde::Deserialize, Debug)]
struct Ec2Tag {
    #[serde(default)]
    key: String,
    #[serde(default)]
    value: String,
}

#[derive(serde::Deserialize, Debug)]
struct DescribeImagesResponse {
    #[serde(rename = "imagesSet", default)]
    images_set: ItemList<ImageInfo>,
}

#[derive(serde::Deserialize, Debug)]
struct ImageInfo {
    #[serde(rename = "imageId", default)]
    image_id: String,
    #[serde(default)]
    name: String,
}

// --- EC2 API ---

fn param(key: &str, value: &str) -> (String, String) {
    (key.to_string(), value.to_string())
}

/// Make a signed GET request to the EC2 API.
fn ec2_get(
    agent: &ureq::Agent,
    creds: &AwsCredentials,
    region: &str,
    params: Vec<(String, String)>,
) -> Result<String, ProviderError> {
    let host = format!("ec2.{}.amazonaws.com", region);
    let epoch = std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .unwrap_or_default()
        .as_secs();
    let (timestamp, datestamp) = format_utc(epoch);

    // Build sorted, URI-encoded query string (SigV4 requires sorted params)
    let mut sorted: Vec<(String, String)> = params
        .into_iter()
        .map(|(k, v)| (uri_encode(&k), uri_encode(&v)))
        .collect();
    sorted.sort();
    let query_string: String = sorted
        .iter()
        .map(|(k, v)| format!("{}={}", k, v))
        .collect::<Vec<_>>()
        .join("&");

    let auth = sign_request(creds, region, &host, &query_string, &timestamp, &datestamp);
    let url = format!("https://{}/?{}", host, query_string);

    let resp = agent
        .get(&url)
        .set("Authorization", &auth)
        .set("x-amz-date", &timestamp)
        .call()
        .map_err(super::map_ureq_error)?;

    resp.into_string()
        .map_err(|e| ProviderError::Parse(e.to_string()))
}

/// Fetch all non-terminated instances in a region (handles pagination).
fn describe_instances(
    agent: &ureq::Agent,
    creds: &AwsCredentials,
    region: &str,
    cancel: &AtomicBool,
) -> Result<Vec<Ec2Instance>, ProviderError> {
    let mut all = Vec::new();
    let mut next_token: Option<String> = None;
    let mut page = 0usize;

    loop {
        page += 1;
        if page > 500 {
            break;
        }
        if cancel.load(Ordering::Relaxed) {
            return Err(ProviderError::Cancelled);
        }

        let mut params = vec![
            param("Action", "DescribeInstances"),
            param("Version", "2016-11-15"),
        ];
        if let Some(ref token) = next_token {
            params.push(param("NextToken", token));
        }

        let body = ec2_get(agent, creds, region, params)?;
        let resp: DescribeInstancesResponse = quick_xml::de::from_str(&body)
            .map_err(|e| ProviderError::Parse(format!("{}: {}", region, e)))?;

        for reservation in resp.reservation_set.item {
            for instance in reservation.instances_set.item {
                if instance.instance_state.name != "terminated"
                    && instance.instance_state.name != "shutting-down"
                {
                    all.push(instance);
                }
            }
        }

        match resp.next_token {
            Some(t) if !t.is_empty() => next_token = Some(t),
            _ => break,
        }
    }

    Ok(all)
}

/// Maximum AMI IDs per DescribeImages request to stay within AWS query limits.
const AMI_BATCH_SIZE: usize = 100;

/// Fetch AMI ID to name mapping (best effort, returns empty map on failure).
/// Batches requests to stay within AWS API limits.
fn fetch_image_names(
    agent: &ureq::Agent,
    creds: &AwsCredentials,
    region: &str,
    image_ids: &[String],
) -> Result<HashMap<String, String>, ProviderError> {
    if image_ids.is_empty() {
        return Ok(HashMap::new());
    }

    let mut map = HashMap::new();
    for chunk in image_ids.chunks(AMI_BATCH_SIZE) {
        let mut params = vec![
            param("Action", "DescribeImages"),
            param("Version", "2016-11-15"),
        ];
        for (i, id) in chunk.iter().enumerate() {
            params.push(param(&format!("ImageId.{}", i + 1), id));
        }

        let body = ec2_get(agent, creds, region, params)?;
        let resp: DescribeImagesResponse = quick_xml::de::from_str(&body)
            .map_err(|e| ProviderError::Parse(format!("{}: {}", region, e)))?;

        for image in resp.images_set.item {
            if !image.name.is_empty() {
                map.insert(image.image_id, image.name);
            }
        }
    }
    Ok(map)
}

/// Extract Name tag value and user tags from an instance's tag set.
/// Filters out aws:* tags. Returns (name, tags) where tags are values only.
fn extract_tags(tag_set: &[Ec2Tag]) -> (String, Vec<String>) {
    let mut name = String::new();
    let mut tags = Vec::new();
    for tag in tag_set {
        if tag.key == "Name" {
            name = tag.value.clone();
        } else if !tag.key.starts_with("aws:") && !tag.value.is_empty() {
            tags.push(tag.value.clone());
        }
    }
    tags.sort();
    (name, tags)
}

// --- Provider trait ---

impl Provider for Aws {
    fn name(&self) -> &str {
        "aws"
    }

    fn short_label(&self) -> &str {
        "aws"
    }

    fn fetch_hosts_cancellable(
        &self,
        token: &str,
        cancel: &AtomicBool,
    ) -> Result<Vec<ProviderHost>, ProviderError> {
        self.fetch_hosts_with_progress(token, cancel, &|_| {})
    }

    fn fetch_hosts_with_progress(
        &self,
        token: &str,
        cancel: &AtomicBool,
        progress: &dyn Fn(&str),
    ) -> Result<Vec<ProviderHost>, ProviderError> {
        if self.regions.is_empty() {
            return Err(ProviderError::Http(
                "No AWS regions configured. Add regions in the provider settings.".to_string(),
            ));
        }

        let valid_codes: HashSet<&str> = AWS_REGIONS.iter().map(|(c, _)| *c).collect();
        for region in &self.regions {
            if !valid_codes.contains(region.as_str()) {
                return Err(ProviderError::Http(format!(
                    "Unknown AWS region '{}'. Check your provider settings.",
                    region
                )));
            }
        }

        let creds = resolve_credentials(token, &self.profile)?;
        let agent = super::http_agent();
        let total_regions = self.regions.len();
        let mut all_hosts = Vec::new();
        let mut failed_regions = 0usize;

        for (i, region) in self.regions.iter().enumerate() {
            if cancel.load(Ordering::Relaxed) {
                return Err(ProviderError::Cancelled);
            }

            progress(&format!("Fetching {} ({}/{})...", region, i + 1, total_regions));

            let instances = match describe_instances(&agent, &creds, region, cancel) {
                Ok(instances) => instances,
                Err(ProviderError::Cancelled) => return Err(ProviderError::Cancelled),
                Err(ProviderError::AuthFailed) => return Err(ProviderError::AuthFailed),
                Err(ProviderError::RateLimited) => return Err(ProviderError::RateLimited),
                Err(_) => {
                    failed_regions += 1;
                    continue;
                }
            };

            // Collect unique AMI IDs for OS metadata lookup
            let ami_ids: Vec<String> = {
                let mut set = HashSet::new();
                for inst in &instances {
                    if !inst.image_id.is_empty() {
                        set.insert(inst.image_id.clone());
                    }
                }
                set.into_iter().collect()
            };

            // Fetch AMI names (best effort)
            let ami_names = if !ami_ids.is_empty() {
                progress(&format!("Resolving AMIs for {}...", region));
                fetch_image_names(&agent, &creds, region, &ami_ids).unwrap_or_default()
            } else {
                HashMap::new()
            };

            for instance in instances {
                let ip = match instance.ip_address {
                    Some(ref ip) if !ip.is_empty() => ip.clone(),
                    _ => continue,
                };

                let (name, tags) = extract_tags(&instance.tag_set.item);
                let name = if name.is_empty() {
                    instance.instance_id.clone()
                } else {
                    name
                };

                let mut metadata = Vec::new();
                metadata.push(("region".to_string(), region.clone()));
                if !instance.instance_type.is_empty() {
                    metadata.push(("plan".to_string(), instance.instance_type.clone()));
                }
                if let Some(os_name) = ami_names.get(&instance.image_id) {
                    metadata.push(("os".to_string(), os_name.clone()));
                }
                if !instance.instance_state.name.is_empty() {
                    metadata.push(("status".to_string(), instance.instance_state.name.clone()));
                }

                all_hosts.push(ProviderHost {
                    server_id: instance.instance_id,
                    name,
                    ip,
                    tags,
                    metadata,
                });
            }
        }

        // Summary
        let mut parts = vec![format!("{} instances", all_hosts.len())];
        if failed_regions > 0 {
            parts.push(format!(
                "{} of {} regions failed",
                failed_regions, total_regions
            ));
        }
        progress(&parts.join(", "));

        if failed_regions > 0 {
            if all_hosts.is_empty() {
                return Err(ProviderError::Http(format!(
                    "All {} regions failed. Check your credentials and region configuration.",
                    total_regions,
                )));
            }
            return Err(ProviderError::PartialResult {
                hosts: all_hosts,
                failures: failed_regions,
                total: total_regions,
            });
        }

        Ok(all_hosts)
    }
}

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

    // =========================================================================
    // format_utc
    // =========================================================================

    #[test]
    fn test_format_utc_epoch_zero() {
        let (ts, ds) = format_utc(0);
        assert_eq!(ts, "19700101T000000Z");
        assert_eq!(ds, "19700101");
    }

    #[test]
    fn test_format_utc_known_date() {
        // 2024-01-15 12:30:45 UTC = 1705321845
        let (ts, ds) = format_utc(1705321845);
        assert_eq!(ts, "20240115T123045Z");
        assert_eq!(ds, "20240115");
    }

    #[test]
    fn test_format_utc_leap_year() {
        // 2024-02-29 00:00:00 UTC = 1709164800
        let (ts, ds) = format_utc(1709164800);
        assert_eq!(ts, "20240229T000000Z");
        assert_eq!(ds, "20240229");
    }

    #[test]
    fn test_format_utc_end_of_year() {
        // 2023-12-31 23:59:59 UTC = 1704067199
        let (ts, ds) = format_utc(1704067199);
        assert_eq!(ts, "20231231T235959Z");
        assert_eq!(ds, "20231231");
    }

    #[test]
    fn test_format_utc_year_2000() {
        // 2000-03-01 00:00:00 UTC = 951868800
        let (ts, ds) = format_utc(951868800);
        assert_eq!(ts, "20000301T000000Z");
        assert_eq!(ds, "20000301");
    }

    // =========================================================================
    // uri_encode
    // =========================================================================

    #[test]
    fn test_uri_encode_passthrough() {
        assert_eq!(uri_encode("abc123-_.~"), "abc123-_.~");
    }

    #[test]
    fn test_uri_encode_special_chars() {
        assert_eq!(uri_encode("hello world"), "hello%20world");
        assert_eq!(uri_encode("a=b&c"), "a%3Db%26c");
        assert_eq!(uri_encode("/path"), "%2Fpath");
    }

    #[test]
    fn test_uri_encode_empty() {
        assert_eq!(uri_encode(""), "");
    }

    // =========================================================================
    // hex_encode
    // =========================================================================

    #[test]
    fn test_hex_encode() {
        assert_eq!(hex_encode(&[0x00, 0xff, 0xab]), "00ffab");
        assert_eq!(hex_encode(&[]), "");
    }

    // =========================================================================
    // sha256_hash
    // =========================================================================

    #[test]
    fn test_sha256_empty() {
        let hash = hex_encode(&sha256_hash(b""));
        assert_eq!(
            hash,
            "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
        );
    }

    #[test]
    fn test_sha256_known() {
        let hash = hex_encode(&sha256_hash(b"hello"));
        assert_eq!(
            hash,
            "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824"
        );
    }

    // =========================================================================
    // hmac_sha256
    // =========================================================================

    #[test]
    fn test_hmac_sha256_known() {
        // HMAC-SHA256("key", "message") is a well-known test vector
        let result = hex_encode(&hmac_sha256(b"key", b"The quick brown fox jumps over the lazy dog"));
        assert_eq!(
            result,
            "f7bc83f430538424b13298e6aa6fb143ef4d59a14946175997479dbc2d1a3cd8"
        );
    }

    // =========================================================================
    // sign_request (SigV4)
    // =========================================================================

    #[test]
    fn test_sign_request_format() {
        let creds = AwsCredentials {
            access_key: "AKIDEXAMPLE".to_string(),
            secret_key: "wJalrXUtnFEMI/K7MDENG+bPxRfiCYEXAMPLEKEY".to_string(),
        };
        let auth = sign_request(
            &creds,
            "us-east-1",
            "ec2.us-east-1.amazonaws.com",
            "Action=DescribeInstances&Version=2016-11-15",
            "20150830T123600Z",
            "20150830",
        );
        assert!(auth.starts_with("AWS4-HMAC-SHA256 Credential=AKIDEXAMPLE/20150830/us-east-1/ec2/aws4_request, SignedHeaders=host;x-amz-date, Signature="));
        // Signature should be a 64-char hex string
        let sig = auth.rsplit("Signature=").next().unwrap();
        assert_eq!(sig.len(), 64);
        assert!(sig.chars().all(|c| c.is_ascii_hexdigit()));
    }

    #[test]
    fn test_sign_request_deterministic() {
        let creds = AwsCredentials {
            access_key: "AK".to_string(),
            secret_key: "SK".to_string(),
        };
        let a = sign_request(&creds, "us-east-1", "ec2.us-east-1.amazonaws.com", "Action=DescribeInstances", "20240101T000000Z", "20240101");
        let b = sign_request(&creds, "us-east-1", "ec2.us-east-1.amazonaws.com", "Action=DescribeInstances", "20240101T000000Z", "20240101");
        assert_eq!(a, b);
    }

    #[test]
    fn test_sign_request_different_regions() {
        let creds = AwsCredentials {
            access_key: "AK".to_string(),
            secret_key: "SK".to_string(),
        };
        let a = sign_request(&creds, "us-east-1", "ec2.us-east-1.amazonaws.com", "Action=DescribeInstances", "20240101T000000Z", "20240101");
        let b = sign_request(&creds, "eu-west-1", "ec2.eu-west-1.amazonaws.com", "Action=DescribeInstances", "20240101T000000Z", "20240101");
        assert_ne!(a, b);
    }

    // =========================================================================
    // parse_credentials
    // =========================================================================

    #[test]
    fn test_parse_credentials_default_profile() {
        let content = "[default]\naws_access_key_id = AKID123\naws_secret_access_key = SECRET456\n";
        let creds = parse_credentials(content, "default").unwrap();
        assert_eq!(creds.access_key, "AKID123");
        assert_eq!(creds.secret_key, "SECRET456");
    }

    #[test]
    fn test_parse_credentials_named_profile() {
        let content = "[default]\naws_access_key_id = DEFAULT\naws_secret_access_key = DEFSECRET\n\n[prod]\naws_access_key_id = PRODAK\naws_secret_access_key = PRODSK\n";
        let creds = parse_credentials(content, "prod").unwrap();
        assert_eq!(creds.access_key, "PRODAK");
        assert_eq!(creds.secret_key, "PRODSK");
    }

    #[test]
    fn test_parse_credentials_missing_profile() {
        let content = "[default]\naws_access_key_id = AK\naws_secret_access_key = SK\n";
        assert!(parse_credentials(content, "nonexistent").is_none());
    }

    #[test]
    fn test_parse_credentials_incomplete_profile() {
        let content = "[incomplete]\naws_access_key_id = AK\n";
        assert!(parse_credentials(content, "incomplete").is_none());
    }

    #[test]
    fn test_parse_credentials_whitespace_handling() {
        let content = "[default]\n  aws_access_key_id  =  AKID  \n  aws_secret_access_key  =  SECRET  \n";
        let creds = parse_credentials(content, "default").unwrap();
        assert_eq!(creds.access_key, "AKID");
        assert_eq!(creds.secret_key, "SECRET");
    }

    #[test]
    fn test_parse_credentials_extra_keys_ignored() {
        let content = "[default]\naws_access_key_id = AK\naws_secret_access_key = SK\naws_session_token = TOKEN\nregion = us-east-1\n";
        let creds = parse_credentials(content, "default").unwrap();
        assert_eq!(creds.access_key, "AK");
        assert_eq!(creds.secret_key, "SK");
    }

    #[test]
    fn test_parse_credentials_empty_content() {
        assert!(parse_credentials("", "default").is_none());
    }

    // =========================================================================
    // resolve_credentials (token parsing)
    // =========================================================================

    #[test]
    fn test_resolve_credentials_token_format() {
        let creds = resolve_credentials("AKID:SECRET", "").unwrap();
        assert_eq!(creds.access_key, "AKID");
        assert_eq!(creds.secret_key, "SECRET");
    }

    #[test]
    fn test_resolve_credentials_empty_parts() {
        // Empty access key
        assert!(resolve_credentials(":SECRET", "").is_err());
        // Empty secret key
        assert!(resolve_credentials("AKID:", "").is_err());
    }

    #[test]
    fn test_resolve_credentials_no_colon() {
        // No colon in token: split_once fails, falls through to env vars
        // Token-only (no colon) should not produce valid credentials from token path
        let result = resolve_credentials("just-a-token", "");
        // Result depends on env vars. Verify token path was skipped by
        // confirming credentials (if any) don't contain the raw token string.
        if let Ok(ref creds) = result {
            assert_ne!(creds.access_key, "just-a-token");
            assert_ne!(creds.secret_key, "just-a-token");
        }
    }

    // =========================================================================
    // XML parsing: DescribeInstances
    // =========================================================================

    #[test]
    fn test_parse_describe_instances_basic() {
        let xml = r#"<?xml version="1.0" encoding="UTF-8"?>
<DescribeInstancesResponse xmlns="http://ec2.amazonaws.com/doc/2016-11-15/">
    <requestId>abc123</requestId>
    <reservationSet>
        <item>
            <reservationId>r-12345</reservationId>
            <instancesSet>
                <item>
                    <instanceId>i-abc123</instanceId>
                    <imageId>ami-12345</imageId>
                    <instanceState><name>running</name></instanceState>
                    <instanceType>t3.micro</instanceType>
                    <ipAddress>1.2.3.4</ipAddress>
                    <placement><availabilityZone>us-east-1a</availabilityZone></placement>
                    <tagSet>
                        <item><key>Name</key><value>web-01</value></item>
                        <item><key>Environment</key><value>prod</value></item>
                    </tagSet>
                </item>
            </instancesSet>
        </item>
    </reservationSet>
</DescribeInstancesResponse>"#;

        let resp: DescribeInstancesResponse = quick_xml::de::from_str(xml).unwrap();
        assert_eq!(resp.reservation_set.item.len(), 1);
        let instance = &resp.reservation_set.item[0].instances_set.item[0];
        assert_eq!(instance.instance_id, "i-abc123");
        assert_eq!(instance.image_id, "ami-12345");
        assert_eq!(instance.instance_state.name, "running");
        assert_eq!(instance.instance_type, "t3.micro");
        assert_eq!(instance.ip_address.as_deref(), Some("1.2.3.4"));
        assert_eq!(instance.tag_set.item.len(), 2);
    }

    #[test]
    fn test_parse_describe_instances_no_public_ip() {
        let xml = r#"<DescribeInstancesResponse xmlns="http://ec2.amazonaws.com/doc/2016-11-15/">
    <reservationSet>
        <item>
            <instancesSet>
                <item>
                    <instanceId>i-noip</instanceId>
                    <instanceState><name>running</name></instanceState>
                    <tagSet/>
                </item>
            </instancesSet>
        </item>
    </reservationSet>
</DescribeInstancesResponse>"#;

        let resp: DescribeInstancesResponse = quick_xml::de::from_str(xml).unwrap();
        let instance = &resp.reservation_set.item[0].instances_set.item[0];
        assert!(instance.ip_address.is_none());
    }

    #[test]
    fn test_parse_describe_instances_empty() {
        let xml = r#"<DescribeInstancesResponse xmlns="http://ec2.amazonaws.com/doc/2016-11-15/">
    <reservationSet/>
</DescribeInstancesResponse>"#;

        let resp: DescribeInstancesResponse = quick_xml::de::from_str(xml).unwrap();
        assert!(resp.reservation_set.item.is_empty());
    }

    #[test]
    fn test_parse_describe_instances_with_next_token() {
        let xml = r#"<DescribeInstancesResponse xmlns="http://ec2.amazonaws.com/doc/2016-11-15/">
    <reservationSet/>
    <nextToken>eyJ0b2tlbiI6ICJ0ZXN0In0=</nextToken>
</DescribeInstancesResponse>"#;

        let resp: DescribeInstancesResponse = quick_xml::de::from_str(xml).unwrap();
        assert_eq!(
            resp.next_token.as_deref(),
            Some("eyJ0b2tlbiI6ICJ0ZXN0In0=")
        );
    }

    #[test]
    fn test_parse_describe_instances_multiple_reservations() {
        let xml = r#"<DescribeInstancesResponse xmlns="http://ec2.amazonaws.com/doc/2016-11-15/">
    <reservationSet>
        <item>
            <instancesSet>
                <item>
                    <instanceId>i-001</instanceId>
                    <instanceState><name>running</name></instanceState>
                    <ipAddress>1.1.1.1</ipAddress>
                </item>
            </instancesSet>
        </item>
        <item>
            <instancesSet>
                <item>
                    <instanceId>i-002</instanceId>
                    <instanceState><name>running</name></instanceState>
                    <ipAddress>2.2.2.2</ipAddress>
                </item>
            </instancesSet>
        </item>
    </reservationSet>
</DescribeInstancesResponse>"#;

        let resp: DescribeInstancesResponse = quick_xml::de::from_str(xml).unwrap();
        assert_eq!(resp.reservation_set.item.len(), 2);
        assert_eq!(
            resp.reservation_set.item[0].instances_set.item[0].instance_id,
            "i-001"
        );
        assert_eq!(
            resp.reservation_set.item[1].instances_set.item[0].instance_id,
            "i-002"
        );
    }

    // =========================================================================
    // XML parsing: DescribeImages
    // =========================================================================

    #[test]
    fn test_parse_describe_images() {
        let xml = r#"<DescribeImagesResponse xmlns="http://ec2.amazonaws.com/doc/2016-11-15/">
    <imagesSet>
        <item>
            <imageId>ami-12345</imageId>
            <name>ubuntu/images/hvm-ssd/ubuntu-jammy-22.04-amd64-server-20240101</name>
        </item>
        <item>
            <imageId>ami-67890</imageId>
            <name>amzn2-ami-hvm-2.0.20240101.0-x86_64-gp2</name>
        </item>
    </imagesSet>
</DescribeImagesResponse>"#;

        let resp: DescribeImagesResponse = quick_xml::de::from_str(xml).unwrap();
        assert_eq!(resp.images_set.item.len(), 2);
        assert_eq!(resp.images_set.item[0].image_id, "ami-12345");
        assert!(resp.images_set.item[0].name.contains("ubuntu"));
        assert_eq!(resp.images_set.item[1].image_id, "ami-67890");
    }

    #[test]
    fn test_parse_describe_images_empty() {
        let xml = r#"<DescribeImagesResponse xmlns="http://ec2.amazonaws.com/doc/2016-11-15/">
    <imagesSet/>
</DescribeImagesResponse>"#;

        let resp: DescribeImagesResponse = quick_xml::de::from_str(xml).unwrap();
        assert!(resp.images_set.item.is_empty());
    }

    // =========================================================================
    // extract_tags
    // =========================================================================

    #[test]
    fn test_extract_tags_name_and_values() {
        let tags = vec![
            Ec2Tag { key: "Name".to_string(), value: "web-01".to_string() },
            Ec2Tag { key: "Environment".to_string(), value: "prod".to_string() },
            Ec2Tag { key: "Team".to_string(), value: "backend".to_string() },
        ];
        let (name, extracted) = extract_tags(&tags);
        assert_eq!(name, "web-01");
        assert_eq!(extracted, vec!["backend", "prod"]); // sorted
    }

    #[test]
    fn test_extract_tags_filters_aws_prefix() {
        let tags = vec![
            Ec2Tag { key: "Name".to_string(), value: "srv".to_string() },
            Ec2Tag { key: "aws:cloudformation:stack-name".to_string(), value: "my-stack".to_string() },
            Ec2Tag { key: "aws:autoscaling:groupName".to_string(), value: "my-asg".to_string() },
            Ec2Tag { key: "custom".to_string(), value: "val".to_string() },
        ];
        let (name, extracted) = extract_tags(&tags);
        assert_eq!(name, "srv");
        assert_eq!(extracted, vec!["val"]);
    }

    #[test]
    fn test_extract_tags_no_name() {
        let tags = vec![
            Ec2Tag { key: "Environment".to_string(), value: "dev".to_string() },
        ];
        let (name, extracted) = extract_tags(&tags);
        assert!(name.is_empty());
        assert_eq!(extracted, vec!["dev"]);
    }

    #[test]
    fn test_extract_tags_empty_value_skipped() {
        let tags = vec![
            Ec2Tag { key: "flag".to_string(), value: "".to_string() },
        ];
        let (_, extracted) = extract_tags(&tags);
        assert!(extracted.is_empty());
    }

    #[test]
    fn test_extract_tags_empty() {
        let (name, tags) = extract_tags(&[]);
        assert!(name.is_empty());
        assert!(tags.is_empty());
    }

    // =========================================================================
    // AWS_REGIONS constant
    // =========================================================================

    #[test]
    fn test_aws_regions_not_empty() {
        assert!(AWS_REGIONS.len() >= 20);
    }

    #[test]
    fn test_aws_region_groups_cover_all_regions() {
        let total: usize = AWS_REGION_GROUPS.iter().map(|&(_, s, e)| e - s).sum();
        assert_eq!(total, AWS_REGIONS.len());
        // Verify groups are contiguous and non-overlapping
        let mut expected_start = 0;
        for &(_, start, end) in AWS_REGION_GROUPS {
            assert_eq!(start, expected_start, "Gap or overlap in region groups");
            assert!(end > start, "Empty region group");
            expected_start = end;
        }
        assert_eq!(expected_start, AWS_REGIONS.len());
    }

    #[test]
    fn test_aws_regions_no_duplicates() {
        let mut seen = HashSet::new();
        for (code, _) in AWS_REGIONS {
            assert!(seen.insert(code), "Duplicate region: {}", code);
        }
    }

    #[test]
    fn test_aws_regions_contains_common() {
        let codes: Vec<&str> = AWS_REGIONS.iter().map(|(c, _)| *c).collect();
        assert!(codes.contains(&"us-east-1"));
        assert!(codes.contains(&"eu-west-1"));
        assert!(codes.contains(&"ap-northeast-1"));
    }

    // =========================================================================
    // Provider trait
    // =========================================================================

    #[test]
    fn test_aws_provider_name() {
        let aws = Aws {
            regions: vec![],
            profile: String::new(),
        };
        assert_eq!(aws.name(), "aws");
        assert_eq!(aws.short_label(), "aws");
    }

    #[test]
    fn test_aws_no_regions_error() {
        let aws = Aws {
            regions: vec![],
            profile: String::new(),
        };
        let result = aws.fetch_hosts("fake");
        match result {
            Err(ProviderError::Http(msg)) => assert!(msg.contains("No AWS regions")),
            other => panic!("Expected Http error, got: {:?}", other),
        }
    }

    // =========================================================================
    // param helper
    // =========================================================================

    #[test]
    fn test_param_helper() {
        let (k, v) = param("Action", "DescribeInstances");
        assert_eq!(k, "Action");
        assert_eq!(v, "DescribeInstances");
    }

    // =========================================================================
    // Region validation
    // =========================================================================

    #[test]
    fn test_aws_invalid_region_error() {
        let aws = Aws {
            regions: vec!["xx-invalid-1".to_string()],
            profile: String::new(),
        };
        let result = aws.fetch_hosts("AKID:SECRET");
        match result {
            Err(ProviderError::Http(msg)) => assert!(msg.contains("Unknown AWS region")),
            other => panic!("Expected Http error for invalid region, got: {:?}", other),
        }
    }

    #[test]
    fn test_aws_mixed_valid_invalid_region_error() {
        let aws = Aws {
            regions: vec!["us-east-1".to_string(), "xx-fake-9".to_string()],
            profile: String::new(),
        };
        let result = aws.fetch_hosts("AKID:SECRET");
        match result {
            Err(ProviderError::Http(msg)) => assert!(msg.contains("xx-fake-9")),
            other => panic!("Expected Http error for invalid region, got: {:?}", other),
        }
    }

    // =========================================================================
    // Profile credential errors return AuthFailed
    // =========================================================================

    #[test]
    fn test_resolve_credentials_bad_profile_returns_auth_failed() {
        // Non-existent profile should return AuthFailed (not Http)
        let result = read_credentials_file("nonexistent-profile-xyz");
        assert!(matches!(result, Err(ProviderError::AuthFailed)));
    }

    // =========================================================================
    // AMI batch constant
    // =========================================================================

    #[test]
    fn test_ami_batch_size_is_reasonable() {
        assert!(AMI_BATCH_SIZE > 0);
        assert!(AMI_BATCH_SIZE <= 200);
    }
}