aws-lite-rs 0.1.1

Lightweight HTTP client for AWS APIs
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
//! AWS Identity and Access Management (IAM) API client.
//!
//! Thin wrapper over generated ops. All URL construction and HTTP methods
//! are in `ops::iam::IamOps`. This layer adds:
//! - Ergonomic method signatures
//! - Convenience parameters for common operations

use crate::{
    AwsHttpClient, Result,
    ops::iam::IamOps,
    types::iam::{
        AttachRolePolicyRequest, CreateServiceLinkedRoleRequest, CreateServiceLinkedRoleResponse,
        DeleteAccessKeyRequest, DeleteUserPolicyRequest, DetachRolePolicyRequest,
        DetachUserPolicyRequest, GenerateCredentialReportResponse, GetAccessKeyLastUsedRequest,
        GetAccessKeyLastUsedResponse, GetAccountPasswordPolicyResponse, GetAccountSummaryResponse,
        GetCredentialReportResponse, GetLoginProfileRequest, GetLoginProfileResponse,
        GetPolicyVersionRequest, GetPolicyVersionResponse, GetUserPolicyRequest,
        GetUserPolicyResponse, ListAccessKeysRequest, ListAccessKeysResponse,
        ListAttachedGroupPoliciesRequest, ListAttachedGroupPoliciesResponse,
        ListAttachedUserPoliciesRequest, ListAttachedUserPoliciesResponse,
        ListEntitiesForPolicyRequest, ListEntitiesForPolicyResponse, ListGroupsForUserRequest,
        ListGroupsForUserResponse, ListMFADevicesRequest, ListMFADevicesResponse,
        ListPoliciesRequest, ListPoliciesResponse, ListRolesRequest, ListRolesResponse,
        ListServerCertificatesRequest, ListServerCertificatesResponse, ListUserPoliciesRequest,
        ListUserPoliciesResponse, ListUsersRequest, ListUsersResponse,
        ListVirtualMFADevicesRequest, ListVirtualMFADevicesResponse, Policy,
        UpdateAccessKeyRequest, UpdateAccountPasswordPolicyRequest, VirtualMFADevice,
    },
};

/// Client for the AWS Identity and Access Management API.
pub struct IamClient<'a> {
    ops: IamOps<'a>,
}

impl<'a> IamClient<'a> {
    /// Create a new IAM API client.
    pub(crate) fn new(client: &'a AwsHttpClient) -> Self {
        Self {
            ops: IamOps::new(client),
        }
    }

    /// List all IAM users in the account.
    pub async fn list_users(&self) -> Result<ListUsersResponse> {
        let body = ListUsersRequest::default();
        self.ops.list_users(&body).await
    }

    /// List all managed policies attached to the specified IAM user.
    pub async fn list_attached_user_policies(
        &self,
        user_name: &str,
    ) -> Result<ListAttachedUserPoliciesResponse> {
        let body = ListAttachedUserPoliciesRequest {
            user_name: user_name.to_string(),
            ..Default::default()
        };
        self.ops.list_attached_user_policies(&body).await
    }

    /// Remove a managed policy from an IAM user.
    pub async fn detach_user_policy(&self, user_name: &str, policy_arn: &str) -> Result<()> {
        let body = DetachUserPolicyRequest {
            user_name: user_name.to_string(),
            policy_arn: policy_arn.to_string(),
        };
        self.ops.detach_user_policy(&body).await
    }

    /// Delete an access key pair for an IAM user.
    pub async fn delete_access_key(&self, user_name: &str, access_key_id: &str) -> Result<()> {
        let body = DeleteAccessKeyRequest {
            user_name: Some(user_name.to_string()),
            access_key_id: access_key_id.to_string(),
        };
        self.ops.delete_access_key(&body).await
    }

    /// List access keys for an IAM user.
    pub async fn list_access_keys(&self, user_name: &str) -> Result<ListAccessKeysResponse> {
        let body = ListAccessKeysRequest {
            user_name: Some(user_name.to_string()),
            ..Default::default()
        };
        self.ops.list_access_keys(&body).await
    }

    /// Retrieve information about when an access key was last used.
    pub async fn get_access_key_last_used(
        &self,
        access_key_id: &str,
    ) -> Result<GetAccessKeyLastUsedResponse> {
        let body = GetAccessKeyLastUsedRequest {
            access_key_id: access_key_id.to_string(),
        };
        self.ops.get_access_key_last_used(&body).await
    }

    /// Generate a credential report for the AWS account.
    pub async fn generate_credential_report(&self) -> Result<GenerateCredentialReportResponse> {
        self.ops.generate_credential_report().await
    }

    /// Retrieve a credential report for the AWS account.
    pub async fn get_credential_report(&self) -> Result<GetCredentialReportResponse> {
        self.ops.get_credential_report().await
    }

    /// Change the status of an access key from Active to Inactive, or vice versa.
    pub async fn update_access_key(
        &self,
        user_name: &str,
        access_key_id: &str,
        status: &str,
    ) -> Result<()> {
        let body = UpdateAccessKeyRequest {
            user_name: Some(user_name.to_string()),
            access_key_id: access_key_id.to_string(),
            status: status.to_string(),
        };
        self.ops.update_access_key(&body).await
    }

    /// List MFA devices for an IAM user.
    pub async fn list_mfa_devices(&self, user_name: &str) -> Result<ListMFADevicesResponse> {
        let body = ListMFADevicesRequest {
            user_name: Some(user_name.to_string()),
            ..Default::default()
        };
        self.ops.list_mfa_devices(&body).await
    }

    /// Get the login profile (console password) for an IAM user.
    pub async fn get_login_profile(&self, user_name: &str) -> Result<GetLoginProfileResponse> {
        let body = GetLoginProfileRequest {
            user_name: Some(user_name.to_string()),
        };
        self.ops.get_login_profile(&body).await
    }

    /// Get the account-level summary of IAM entity usage and quotas.
    pub async fn get_account_summary(&self) -> Result<GetAccountSummaryResponse> {
        self.ops.get_account_summary().await
    }

    /// Get the account password policy.
    pub async fn get_account_password_policy(&self) -> Result<GetAccountPasswordPolicyResponse> {
        self.ops.get_account_password_policy().await
    }

    /// Update the account password policy.
    pub async fn update_account_password_policy(
        &self,
        body: &UpdateAccountPasswordPolicyRequest,
    ) -> Result<()> {
        self.ops.update_account_password_policy(body).await
    }

    /// List all IAM roles in the account.
    pub async fn list_roles(&self) -> Result<ListRolesResponse> {
        let body = ListRolesRequest::default();
        self.ops.list_roles(&body).await
    }

    /// List inline policy names for an IAM user.
    pub async fn list_user_policies(&self, user_name: &str) -> Result<ListUserPoliciesResponse> {
        let body = ListUserPoliciesRequest {
            user_name: user_name.to_string(),
            ..Default::default()
        };
        self.ops.list_user_policies(&body).await
    }

    /// List the groups that an IAM user belongs to.
    pub async fn list_groups_for_user(&self, user_name: &str) -> Result<ListGroupsForUserResponse> {
        let body = ListGroupsForUserRequest {
            user_name: user_name.to_string(),
            ..Default::default()
        };
        self.ops.list_groups_for_user(&body).await
    }

    /// List all server certificates in the account.
    pub async fn list_server_certificates(&self) -> Result<ListServerCertificatesResponse> {
        let body = ListServerCertificatesRequest::default();
        self.ops.list_server_certificates(&body).await
    }

    /// Delete an inline policy from an IAM user.
    pub async fn delete_user_policy(&self, user_name: &str, policy_name: &str) -> Result<()> {
        let body = DeleteUserPolicyRequest {
            user_name: user_name.to_string(),
            policy_name: policy_name.to_string(),
        };
        self.ops.delete_user_policy(&body).await
    }

    /// Attach a managed policy to an IAM role.
    pub async fn attach_role_policy(&self, role_name: &str, policy_arn: &str) -> Result<()> {
        let body = AttachRolePolicyRequest {
            role_name: role_name.to_string(),
            policy_arn: policy_arn.to_string(),
        };
        self.ops.attach_role_policy(&body).await
    }

    /// Detach a managed policy from an IAM role.
    pub async fn detach_role_policy(&self, role_name: &str, policy_arn: &str) -> Result<()> {
        let body = DetachRolePolicyRequest {
            role_name: role_name.to_string(),
            policy_arn: policy_arn.to_string(),
        };
        self.ops.detach_role_policy(&body).await
    }

    /// Create a service-linked role for an AWS service.
    pub async fn create_service_linked_role(
        &self,
        aws_service_name: &str,
    ) -> Result<CreateServiceLinkedRoleResponse> {
        let body = CreateServiceLinkedRoleRequest {
            aws_service_name: aws_service_name.to_string(),
            ..Default::default()
        };
        self.ops.create_service_linked_role(&body).await
    }

    /// Retrieve an inline policy document embedded in an IAM user.
    pub async fn get_user_policy(
        &self,
        user_name: &str,
        policy_name: &str,
    ) -> Result<GetUserPolicyResponse> {
        let body = GetUserPolicyRequest {
            user_name: user_name.to_string(),
            policy_name: policy_name.to_string(),
        };
        self.ops.get_user_policy(&body).await
    }

    /// List all managed policies attached to an IAM group.
    pub async fn list_attached_group_policies(
        &self,
        group_name: &str,
    ) -> Result<ListAttachedGroupPoliciesResponse> {
        let body = ListAttachedGroupPoliciesRequest {
            group_name: group_name.to_string(),
            ..Default::default()
        };
        self.ops.list_attached_group_policies(&body).await
    }

    // ── Virtual MFA Devices ────────────────────────────────────────────

    /// Return the first page of virtual MFA devices.
    ///
    /// `assignment_status`: optional filter — `"Assigned"`, `"Unassigned"`, or
    /// `"Any"` (default when `None`).
    pub async fn list_virtual_mfa_devices(
        &self,
        assignment_status: Option<&str>,
    ) -> Result<ListVirtualMFADevicesResponse> {
        let body = ListVirtualMFADevicesRequest {
            assignment_status: assignment_status.map(str::to_string),
            ..Default::default()
        };
        self.ops.list_virtual_mfa_devices(&body).await
    }

    /// Return all virtual MFA devices in the account, following pagination.
    ///
    /// CIS 2.5: the root account should use a hardware MFA device, not a
    /// virtual one. Any `VirtualMFADevice` whose serial number contains
    /// `"root-account-mfa-device"` indicates virtual MFA on root.
    pub async fn list_all_virtual_mfa_devices(&self) -> Result<Vec<VirtualMFADevice>> {
        let mut all = Vec::new();
        let mut marker: Option<String> = None;
        loop {
            let body = ListVirtualMFADevicesRequest {
                marker: marker.clone(),
                ..Default::default()
            };
            let resp = self.ops.list_virtual_mfa_devices(&body).await?;
            all.extend(resp.virtual_mfa_devices);
            match resp.marker {
                Some(m) if !m.is_empty() && resp.is_truncated == Some(true) => {
                    marker = Some(m);
                }
                _ => break,
            }
        }
        Ok(all)
    }

    // ── Managed Policies ───────────────────────────────────────────────

    /// Return the first page of IAM policies.
    ///
    /// `scope`: `"Local"` (customer-managed), `"AWS"` (AWS-managed), or
    /// `"All"` (default when `None`).
    pub async fn list_policies(
        &self,
        scope: Option<&str>,
        marker: Option<&str>,
    ) -> Result<ListPoliciesResponse> {
        let body = ListPoliciesRequest {
            scope: scope.map(str::to_string),
            marker: marker.map(str::to_string),
            ..Default::default()
        };
        self.ops.list_policies(&body).await
    }

    /// Return all IAM policies for the given scope, following pagination.
    ///
    /// CIS 2.15: pass `scope = Some("Local")` to enumerate customer-managed
    /// policies, then call `get_policy_version` on each to inspect for
    /// unrestricted `"*:*"` statements.
    pub async fn list_all_policies(&self, scope: Option<&str>) -> Result<Vec<Policy>> {
        let mut all = Vec::new();
        let mut marker: Option<String> = None;
        loop {
            let body = ListPoliciesRequest {
                scope: scope.map(str::to_string),
                marker: marker.clone(),
                ..Default::default()
            };
            let resp = self.ops.list_policies(&body).await?;
            all.extend(resp.policies);
            match resp.marker {
                Some(m) if !m.is_empty() && resp.is_truncated == Some(true) => {
                    marker = Some(m);
                }
                _ => break,
            }
        }
        Ok(all)
    }

    /// Retrieve a specific version of a managed policy.
    ///
    /// The `document` field in the response is URL-encoded JSON. Decode it
    /// with `urlencoding::decode` before parsing.
    ///
    /// CIS 2.15: inspect the document for statements allowing `"*"` actions
    /// on `"*"` resources to detect policies with full admin access.
    pub async fn get_policy_version(
        &self,
        policy_arn: &str,
        version_id: &str,
    ) -> Result<GetPolicyVersionResponse> {
        let body = GetPolicyVersionRequest {
            policy_arn: policy_arn.to_string(),
            version_id: version_id.to_string(),
        };
        self.ops.get_policy_version(&body).await
    }

    // ── Entities For Policy ────────────────────────────────────────────

    /// Return the first page of entities (groups, users, roles) attached to a
    /// managed policy.
    pub async fn list_entities_for_policy(
        &self,
        policy_arn: &str,
    ) -> Result<ListEntitiesForPolicyResponse> {
        let body = ListEntitiesForPolicyRequest {
            policy_arn: policy_arn.to_string(),
            ..Default::default()
        };
        self.ops.list_entities_for_policy(&body).await
    }

    /// Return all entities attached to a managed policy, following pagination.
    ///
    /// Merges `PolicyGroups`, `PolicyUsers`, and `PolicyRoles` across all
    /// pages into a single response.
    ///
    /// CIS 2.16: verify at least one entity is attached to `AWSSupportAccess`.
    /// CIS 2.21: verify no entity is attached to `AWSCloudShellFullAccess`
    /// (or only approved entities).
    pub async fn list_all_entities_for_policy(
        &self,
        policy_arn: &str,
    ) -> Result<ListEntitiesForPolicyResponse> {
        let mut groups = Vec::new();
        let mut users = Vec::new();
        let mut roles = Vec::new();
        let mut marker: Option<String> = None;
        loop {
            let body = ListEntitiesForPolicyRequest {
                policy_arn: policy_arn.to_string(),
                marker: marker.clone(),
                ..Default::default()
            };
            let resp = self.ops.list_entities_for_policy(&body).await?;
            groups.extend(resp.policy_groups);
            users.extend(resp.policy_users);
            roles.extend(resp.policy_roles);
            match resp.marker {
                Some(m) if !m.is_empty() && resp.is_truncated == Some(true) => {
                    marker = Some(m);
                }
                _ => break,
            }
        }
        Ok(ListEntitiesForPolicyResponse {
            policy_groups: groups,
            policy_users: users,
            policy_roles: roles,
            is_truncated: Some(false),
            marker: None,
        })
    }
}

#[cfg(test)]
mod tests {
    use crate::AwsHttpClient;
    use crate::mock_client::MockClient;
    use crate::test_support::iam_mock_helpers::IamMockHelpers;

    fn xml_envelope(action: &str, inner: &str) -> Vec<u8> {
        format!("<{action}Response><{action}Result>{inner}</{action}Result></{action}Response>")
            .into_bytes()
    }

    #[tokio::test]
    async fn list_users_returns_parsed_users() {
        let mut mock = MockClient::new();
        mock.expect_list_users().returning_bytes(xml_envelope(
            "ListUsers",
            "<Users>\
               <member>\
                 <Arn>arn:aws:iam::123456789012:user/alice</Arn>\
                 <UserName>alice</UserName>\
                 <CreateDate>2024-01-15T10:30:00Z</CreateDate>\
               </member>\
             </Users>",
        ));

        let client = AwsHttpClient::from_mock(mock);
        let response = client.iam().list_users().await.unwrap();

        assert_eq!(response.users.len(), 1);
        assert_eq!(response.users[0].user_name, "alice");
        assert_eq!(
            response.users[0].arn,
            "arn:aws:iam::123456789012:user/alice"
        );
        assert_eq!(response.users[0].create_date, "2024-01-15T10:30:00Z");
    }

    #[tokio::test]
    async fn list_users_handles_empty_response() {
        let mut mock = MockClient::new();
        mock.expect_list_users()
            .returning_bytes(xml_envelope("ListUsers", "<Users/>"));

        let client = AwsHttpClient::from_mock(mock);
        let response = client.iam().list_users().await.unwrap();
        assert!(response.users.is_empty());
    }

    #[tokio::test]
    async fn list_attached_user_policies_returns_policies() {
        let mut mock = MockClient::new();
        mock.expect_list_attached_user_policies()
            .returning_bytes(xml_envelope(
                "ListAttachedUserPolicies",
                "<AttachedPolicies>\
                   <member>\
                     <PolicyArn>arn:aws:iam::aws:policy/ReadOnlyAccess</PolicyArn>\
                     <PolicyName>ReadOnlyAccess</PolicyName>\
                   </member>\
                 </AttachedPolicies>",
            ));

        let client = AwsHttpClient::from_mock(mock);
        let response = client
            .iam()
            .list_attached_user_policies("alice")
            .await
            .unwrap();

        assert_eq!(response.attached_policies.len(), 1);
        assert_eq!(
            response.attached_policies[0].policy_arn.as_deref(),
            Some("arn:aws:iam::aws:policy/ReadOnlyAccess")
        );
        assert_eq!(
            response.attached_policies[0].policy_name.as_deref(),
            Some("ReadOnlyAccess")
        );
    }

    #[tokio::test]
    async fn detach_user_policy_succeeds() {
        let mut mock = MockClient::new();
        mock.expect_detach_user_policy()
            .returning_bytes(xml_envelope("DetachUserPolicy", ""));

        let client = AwsHttpClient::from_mock(mock);
        let result = client
            .iam()
            .detach_user_policy("alice", "arn:aws:iam::aws:policy/ReadOnlyAccess")
            .await;
        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn delete_access_key_succeeds() {
        let mut mock = MockClient::new();
        mock.expect_delete_access_key()
            .returning_bytes(xml_envelope("DeleteAccessKey", ""));

        let client = AwsHttpClient::from_mock(mock);
        let result = client
            .iam()
            .delete_access_key("alice", "AKIAIOSFODNN7EXAMPLE")
            .await;
        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn list_access_keys_returns_keys() {
        let mut mock = MockClient::new();
        mock.expect_list_access_keys().returning_bytes(xml_envelope(
            "ListAccessKeys",
            "<AccessKeyMetadata>\
               <member>\
                 <UserName>alice</UserName>\
                 <AccessKeyId>AKIAIOSFODNN7EXAMPLE</AccessKeyId>\
                 <Status>Active</Status>\
                 <CreateDate>2024-03-01T12:00:00Z</CreateDate>\
               </member>\
             </AccessKeyMetadata>",
        ));

        let client = AwsHttpClient::from_mock(mock);
        let response = client.iam().list_access_keys("alice").await.unwrap();

        assert_eq!(response.access_key_metadata.len(), 1);
        assert_eq!(
            response.access_key_metadata[0].access_key_id.as_deref(),
            Some("AKIAIOSFODNN7EXAMPLE")
        );
        assert_eq!(
            response.access_key_metadata[0].user_name.as_deref(),
            Some("alice")
        );
    }

    #[tokio::test]
    async fn get_access_key_last_used_returns_info() {
        let mut mock = MockClient::new();
        mock.expect_get_access_key_last_used()
            .returning_bytes(xml_envelope(
                "GetAccessKeyLastUsed",
                "<UserName>alice</UserName>\
                 <AccessKeyLastUsed>\
                   <ServiceName>s3</ServiceName>\
                   <Region>us-east-1</Region>\
                   <LastUsedDate>2024-06-15T08:00:00Z</LastUsedDate>\
                 </AccessKeyLastUsed>",
            ));

        let client = AwsHttpClient::from_mock(mock);
        let response = client
            .iam()
            .get_access_key_last_used("AKIAIOSFODNN7EXAMPLE")
            .await
            .unwrap();

        assert_eq!(response.user_name.as_deref(), Some("alice"));
        let last_used = response.access_key_last_used.unwrap();
        assert_eq!(last_used.service_name, "s3");
        assert_eq!(last_used.region, "us-east-1");
        assert_eq!(
            last_used.last_used_date.as_deref(),
            Some("2024-06-15T08:00:00Z")
        );
    }

    #[tokio::test]
    async fn generate_credential_report_returns_state() {
        let mut mock = MockClient::new();
        mock.expect_generate_credential_report()
            .returning_bytes(xml_envelope(
                "GenerateCredentialReport",
                "<State>STARTED</State>\
                 <Description>Starting report generation</Description>",
            ));

        let client = AwsHttpClient::from_mock(mock);
        let response = client.iam().generate_credential_report().await.unwrap();

        assert_eq!(
            response.state,
            Some(crate::types::iam::ReportStateType::Started)
        );
        assert_eq!(
            response.description.as_deref(),
            Some("Starting report generation")
        );
    }

    #[tokio::test]
    async fn get_credential_report_returns_content() {
        let mut mock = MockClient::new();
        mock.expect_get_credential_report()
            .returning_bytes(xml_envelope(
                "GetCredentialReport",
                "<Content>dXNlcixhcm4K</Content>\
                 <ReportFormat>text/csv</ReportFormat>\
                 <GeneratedTime>2024-06-15T10:00:00Z</GeneratedTime>",
            ));

        let client = AwsHttpClient::from_mock(mock);
        let response = client.iam().get_credential_report().await.unwrap();

        // Content is base64 decoded automatically by serde
        assert_eq!(response.content.as_deref(), Some("user,arn\n"));
        assert_eq!(
            response.report_format,
            Some(crate::types::iam::ReportFormatType::TextPercsv)
        );
        assert_eq!(
            response.generated_time.as_deref(),
            Some("2024-06-15T10:00:00Z")
        );
    }

    #[tokio::test]
    async fn update_access_key_succeeds() {
        let mut mock = MockClient::new();
        mock.expect_update_access_key()
            .returning_bytes(xml_envelope("UpdateAccessKey", ""));

        let client = AwsHttpClient::from_mock(mock);
        let result = client
            .iam()
            .update_access_key("alice", "AKIAIOSFODNN7EXAMPLE", "Inactive")
            .await;
        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn list_mfa_devices_returns_devices() {
        let mut mock = MockClient::new();
        mock.expect_list_mfa_devices().returning_bytes(xml_envelope(
            "ListMFADevices",
            "<MFADevices>\
               <member>\
                 <UserName>alice</UserName>\
                 <SerialNumber>arn:aws:iam::123456789012:mfa/alice</SerialNumber>\
                 <EnableDate>2024-01-15T10:00:00Z</EnableDate>\
               </member>\
             </MFADevices>",
        ));

        let client = AwsHttpClient::from_mock(mock);
        let response = client.iam().list_mfa_devices("alice").await.unwrap();

        assert_eq!(response.mfa_devices.len(), 1);
        assert_eq!(response.mfa_devices[0].user_name, "alice");
        assert_eq!(
            response.mfa_devices[0].serial_number,
            "arn:aws:iam::123456789012:mfa/alice"
        );
        assert_eq!(response.mfa_devices[0].enable_date, "2024-01-15T10:00:00Z");
    }

    #[tokio::test]
    async fn get_login_profile_returns_profile() {
        let mut mock = MockClient::new();
        mock.expect_get_login_profile()
            .returning_bytes(xml_envelope(
                "GetLoginProfile",
                "<LoginProfile>\
                   <UserName>alice</UserName>\
                   <CreateDate>2024-01-15T10:30:00Z</CreateDate>\
                   <PasswordResetRequired>false</PasswordResetRequired>\
                 </LoginProfile>",
            ));

        let client = AwsHttpClient::from_mock(mock);
        let response = client.iam().get_login_profile("alice").await.unwrap();

        assert_eq!(response.login_profile.user_name, "alice");
        assert_eq!(response.login_profile.create_date, "2024-01-15T10:30:00Z");
        assert_eq!(response.login_profile.password_reset_required, Some(false));
    }

    #[tokio::test]
    async fn get_account_summary_returns_map() {
        let mut mock = MockClient::new();
        mock.expect_get_account_summary()
            .returning_bytes(xml_envelope(
                "GetAccountSummary",
                "<SummaryMap>\
                   <entry><key>Users</key><value>5</value></entry>\
                   <entry><key>Roles</key><value>12</value></entry>\
                   <entry><key>AccountMFAEnabled</key><value>1</value></entry>\
                 </SummaryMap>",
            ));

        let client = AwsHttpClient::from_mock(mock);
        let response = client.iam().get_account_summary().await.unwrap();

        assert_eq!(response.summary_map.len(), 3);
        assert_eq!(response.summary_map.get("Users"), Some(&5));
        assert_eq!(response.summary_map.get("Roles"), Some(&12));
        assert_eq!(response.summary_map.get("AccountMFAEnabled"), Some(&1));
    }

    #[tokio::test]
    async fn get_account_password_policy_returns_policy() {
        let mut mock = MockClient::new();
        mock.expect_get_account_password_policy()
            .returning_bytes(xml_envelope(
                "GetAccountPasswordPolicy",
                "<PasswordPolicy>\
                   <MinimumPasswordLength>14</MinimumPasswordLength>\
                   <RequireSymbols>true</RequireSymbols>\
                   <RequireNumbers>true</RequireNumbers>\
                   <RequireUppercaseCharacters>true</RequireUppercaseCharacters>\
                   <RequireLowercaseCharacters>true</RequireLowercaseCharacters>\
                   <MaxPasswordAge>90</MaxPasswordAge>\
                 </PasswordPolicy>",
            ));

        let client = AwsHttpClient::from_mock(mock);
        let response = client.iam().get_account_password_policy().await.unwrap();

        let policy = &response.password_policy;
        assert_eq!(policy.minimum_password_length, Some(14));
        assert_eq!(policy.require_symbols, Some(true));
        assert_eq!(policy.require_numbers, Some(true));
        assert_eq!(policy.require_uppercase_characters, Some(true));
        assert_eq!(policy.require_lowercase_characters, Some(true));
        assert_eq!(policy.max_password_age, Some(90));
    }

    #[tokio::test]
    async fn update_account_password_policy_succeeds() {
        let mut mock = MockClient::new();
        mock.expect_update_account_password_policy()
            .returning_bytes(xml_envelope("UpdateAccountPasswordPolicy", ""));

        let client = AwsHttpClient::from_mock(mock);
        let body = crate::types::iam::UpdateAccountPasswordPolicyRequest {
            minimum_password_length: Some(14),
            ..Default::default()
        };
        let result = client.iam().update_account_password_policy(&body).await;
        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn list_roles_returns_roles() {
        let mut mock = MockClient::new();
        mock.expect_list_roles().returning_bytes(xml_envelope(
            "ListRoles",
            "<Roles>\
               <member>\
                 <RoleName>admin-role</RoleName>\
                 <Arn>arn:aws:iam::123456789012:role/admin-role</Arn>\
                 <CreateDate>2024-01-15T10:30:00Z</CreateDate>\
                 <Description>Admin role</Description>\
               </member>\
             </Roles>",
        ));

        let client = AwsHttpClient::from_mock(mock);
        let response = client.iam().list_roles().await.unwrap();

        assert_eq!(response.roles.len(), 1);
        assert_eq!(response.roles[0].role_name, "admin-role");
        assert_eq!(
            response.roles[0].arn,
            "arn:aws:iam::123456789012:role/admin-role"
        );
        assert_eq!(response.roles[0].create_date, "2024-01-15T10:30:00Z");
        assert_eq!(response.roles[0].description.as_deref(), Some("Admin role"));
    }

    #[tokio::test]
    async fn list_user_policies_returns_names() {
        let mut mock = MockClient::new();
        mock.expect_list_user_policies()
            .returning_bytes(xml_envelope(
                "ListUserPolicies",
                "<PolicyNames>\
                   <member>s3-read-policy</member>\
                   <member>ec2-describe-policy</member>\
                 </PolicyNames>",
            ));

        let client = AwsHttpClient::from_mock(mock);
        let response = client.iam().list_user_policies("alice").await.unwrap();

        assert_eq!(response.policy_names.len(), 2);
        assert_eq!(response.policy_names[0], "s3-read-policy");
        assert_eq!(response.policy_names[1], "ec2-describe-policy");
    }

    #[tokio::test]
    async fn list_groups_for_user_returns_groups() {
        let mut mock = MockClient::new();
        mock.expect_list_groups_for_user()
            .returning_bytes(xml_envelope(
                "ListGroupsForUser",
                "<Groups>\
                   <member>\
                     <GroupName>developers</GroupName>\
                     <Arn>arn:aws:iam::123456789012:group/developers</Arn>\
                     <CreateDate>2024-02-01T09:00:00Z</CreateDate>\
                   </member>\
                 </Groups>",
            ));

        let client = AwsHttpClient::from_mock(mock);
        let response = client.iam().list_groups_for_user("alice").await.unwrap();

        assert_eq!(response.groups.len(), 1);
        assert_eq!(response.groups[0].group_name, "developers");
        assert_eq!(
            response.groups[0].arn,
            "arn:aws:iam::123456789012:group/developers"
        );
        assert_eq!(response.groups[0].create_date, "2024-02-01T09:00:00Z");
    }

    #[tokio::test]
    async fn list_server_certificates_returns_certs() {
        let mut mock = MockClient::new();
        mock.expect_list_server_certificates()
            .returning_bytes(xml_envelope(
                "ListServerCertificates",
                "<ServerCertificateMetadataList>\
                   <member>\
                     <ServerCertificateName>my-cert</ServerCertificateName>\
                     <Arn>arn:aws:iam::123456789012:server-certificate/my-cert</Arn>\
                     <Expiration>2025-12-31T23:59:59Z</Expiration>\
                     <UploadDate>2024-01-01T00:00:00Z</UploadDate>\
                   </member>\
                 </ServerCertificateMetadataList>",
            ));

        let client = AwsHttpClient::from_mock(mock);
        let response = client.iam().list_server_certificates().await.unwrap();

        assert_eq!(response.server_certificate_metadata_list.len(), 1);
        let cert = &response.server_certificate_metadata_list[0];
        assert_eq!(cert.server_certificate_name, "my-cert");
        assert_eq!(
            cert.arn,
            "arn:aws:iam::123456789012:server-certificate/my-cert"
        );
        assert_eq!(cert.expiration.as_deref(), Some("2025-12-31T23:59:59Z"));
        assert_eq!(cert.upload_date.as_deref(), Some("2024-01-01T00:00:00Z"));
    }

    #[tokio::test]
    async fn delete_user_policy_succeeds() {
        let mut mock = MockClient::new();
        mock.expect_delete_user_policy()
            .returning_bytes(xml_envelope("DeleteUserPolicy", ""));

        let client = AwsHttpClient::from_mock(mock);
        let result = client.iam().delete_user_policy("alice", "my-policy").await;
        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn attach_role_policy_succeeds() {
        let mut mock = MockClient::new();
        mock.expect_attach_role_policy()
            .returning_bytes(xml_envelope("AttachRolePolicy", ""));

        let client = AwsHttpClient::from_mock(mock);
        let result = client
            .iam()
            .attach_role_policy("my-role", "arn:aws:iam::aws:policy/ReadOnlyAccess")
            .await;
        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn detach_role_policy_succeeds() {
        let mut mock = MockClient::new();
        mock.expect_detach_role_policy()
            .returning_bytes(xml_envelope("DetachRolePolicy", ""));

        let client = AwsHttpClient::from_mock(mock);
        let result = client
            .iam()
            .detach_role_policy("my-role", "arn:aws:iam::aws:policy/ReadOnlyAccess")
            .await;
        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn create_service_linked_role_returns_role() {
        let mut mock = MockClient::new();
        mock.expect_create_service_linked_role()
            .returning_bytes(xml_envelope(
                "CreateServiceLinkedRole",
                "<Role>\
                   <RoleName>AWSServiceRoleForElasticBeanstalk</RoleName>\
                   <Arn>arn:aws:iam::123456789012:role/aws-service-role/elasticbeanstalk.amazonaws.com/AWSServiceRoleForElasticBeanstalk</Arn>\
                   <CreateDate>2024-06-15T12:00:00Z</CreateDate>\
                   <Description>Service-linked role for Elastic Beanstalk</Description>\
                 </Role>",
            ));

        let client = AwsHttpClient::from_mock(mock);
        let response = client
            .iam()
            .create_service_linked_role("elasticbeanstalk.amazonaws.com")
            .await
            .unwrap();

        let role = response.role.unwrap();
        assert_eq!(role.role_name, "AWSServiceRoleForElasticBeanstalk");
        assert!(role.arn.contains("aws-service-role"));
        assert_eq!(role.create_date, "2024-06-15T12:00:00Z");
        assert_eq!(
            role.description.as_deref(),
            Some("Service-linked role for Elastic Beanstalk")
        );
    }

    #[tokio::test]
    async fn get_user_policy_returns_document() {
        let mut mock = MockClient::new();
        mock.expect_get_user_policy().returning_bytes(xml_envelope(
            "GetUserPolicy",
            "<UserName>alice</UserName>\
             <PolicyName>s3-read-policy</PolicyName>\
             <PolicyDocument>%7B%22Version%22%3A%222012-10-17%22%7D</PolicyDocument>",
        ));

        let client = AwsHttpClient::from_mock(mock);
        let response = client
            .iam()
            .get_user_policy("alice", "s3-read-policy")
            .await
            .unwrap();

        assert_eq!(response.user_name, "alice");
        assert_eq!(response.policy_name, "s3-read-policy");
        assert_eq!(
            response.policy_document,
            "%7B%22Version%22%3A%222012-10-17%22%7D"
        );
    }

    #[tokio::test]
    async fn list_attached_group_policies_returns_policies() {
        let mut mock = MockClient::new();
        mock.expect_list_attached_group_policies()
            .returning_bytes(xml_envelope(
                "ListAttachedGroupPolicies",
                "<AttachedPolicies>\
                   <member>\
                     <PolicyArn>arn:aws:iam::aws:policy/ReadOnlyAccess</PolicyArn>\
                     <PolicyName>ReadOnlyAccess</PolicyName>\
                   </member>\
                 </AttachedPolicies>",
            ));

        let client = AwsHttpClient::from_mock(mock);
        let response = client
            .iam()
            .list_attached_group_policies("developers")
            .await
            .unwrap();

        assert_eq!(response.attached_policies.len(), 1);
        assert_eq!(
            response.attached_policies[0].policy_arn.as_deref(),
            Some("arn:aws:iam::aws:policy/ReadOnlyAccess")
        );
        assert_eq!(
            response.attached_policies[0].policy_name.as_deref(),
            Some("ReadOnlyAccess")
        );
    }

    #[tokio::test]
    async fn list_attached_group_policies_handles_empty() {
        let mut mock = MockClient::new();
        mock.expect_list_attached_group_policies()
            .returning_bytes(xml_envelope(
                "ListAttachedGroupPolicies",
                "<AttachedPolicies/>",
            ));

        let client = AwsHttpClient::from_mock(mock);
        let response = client
            .iam()
            .list_attached_group_policies("empty-group")
            .await
            .unwrap();
        assert!(response.attached_policies.is_empty());
    }

    // ── Virtual MFA Devices ────────────────────────────────────────────

    #[tokio::test]
    async fn list_virtual_mfa_devices_returns_devices() {
        let mut mock = MockClient::new();
        mock.expect_list_virtual_mfa_devices()
            .returning_bytes(xml_envelope(
                "ListVirtualMFADevices",
                "<VirtualMFADevices>\
                   <member>\
                     <SerialNumber>arn:aws:iam::123456789012:mfa/root-account-mfa-device</SerialNumber>\
                     <EnableDate>2024-01-01T00:00:00Z</EnableDate>\
                   </member>\
                 </VirtualMFADevices>",
            ));

        let client = AwsHttpClient::from_mock(mock);
        let response = client.iam().list_virtual_mfa_devices(None).await.unwrap();

        assert_eq!(response.virtual_mfa_devices.len(), 1);
        assert_eq!(
            response.virtual_mfa_devices[0].serial_number,
            "arn:aws:iam::123456789012:mfa/root-account-mfa-device"
        );
        assert_eq!(
            response.virtual_mfa_devices[0].enable_date.as_deref(),
            Some("2024-01-01T00:00:00Z")
        );
    }

    #[tokio::test]
    async fn list_virtual_mfa_devices_handles_empty() {
        let mut mock = MockClient::new();
        mock.expect_list_virtual_mfa_devices()
            .returning_bytes(xml_envelope(
                "ListVirtualMFADevices",
                "<VirtualMFADevices/>",
            ));

        let client = AwsHttpClient::from_mock(mock);
        let response = client.iam().list_virtual_mfa_devices(None).await.unwrap();
        assert!(response.virtual_mfa_devices.is_empty());
    }

    // ── Managed Policies ───────────────────────────────────────────────

    #[tokio::test]
    async fn list_policies_returns_local_policies() {
        let mut mock = MockClient::new();
        mock.expect_list_policies().returning_bytes(xml_envelope(
            "ListPolicies",
            "<Policies>\
               <member>\
                 <PolicyName>FullAdminPolicy</PolicyName>\
                 <PolicyId>ANPA000000000EXAMPLE</PolicyId>\
                 <Arn>arn:aws:iam::123456789012:policy/FullAdminPolicy</Arn>\
                 <DefaultVersionId>v1</DefaultVersionId>\
                 <IsAttachable>true</IsAttachable>\
                 <CreateDate>2024-01-15T10:00:00Z</CreateDate>\
                 <UpdateDate>2024-01-15T10:00:00Z</UpdateDate>\
               </member>\
             </Policies>",
        ));

        let client = AwsHttpClient::from_mock(mock);
        let response = client
            .iam()
            .list_policies(Some("Local"), None)
            .await
            .unwrap();

        assert_eq!(response.policies.len(), 1);
        assert_eq!(
            response.policies[0].policy_name.as_deref(),
            Some("FullAdminPolicy")
        );
        assert_eq!(
            response.policies[0].arn.as_deref(),
            Some("arn:aws:iam::123456789012:policy/FullAdminPolicy")
        );
        assert_eq!(
            response.policies[0].default_version_id.as_deref(),
            Some("v1")
        );
        assert_eq!(response.policies[0].is_attachable, Some(true));
    }

    #[tokio::test]
    async fn list_policies_handles_empty() {
        let mut mock = MockClient::new();
        mock.expect_list_policies()
            .returning_bytes(xml_envelope("ListPolicies", "<Policies/>"));

        let client = AwsHttpClient::from_mock(mock);
        let response = client.iam().list_policies(None, None).await.unwrap();
        assert!(response.policies.is_empty());
    }

    #[tokio::test]
    async fn get_policy_version_returns_document() {
        let mut mock = MockClient::new();
        mock.expect_get_policy_version().returning_bytes(xml_envelope(
            "GetPolicyVersion",
            "<PolicyVersion>\
               <Document>%7B%22Version%22%3A%222012-10-17%22%2C%22Statement%22%3A%5B%7B%22Effect%22%3A%22Allow%22%2C%22Action%22%3A%22%2A%22%2C%22Resource%22%3A%22%2A%22%7D%5D%7D</Document>\
               <VersionId>v1</VersionId>\
               <IsDefaultVersion>true</IsDefaultVersion>\
               <CreateDate>2024-01-15T10:00:00Z</CreateDate>\
             </PolicyVersion>",
        ));

        let client = AwsHttpClient::from_mock(mock);
        let response = client
            .iam()
            .get_policy_version("arn:aws:iam::123456789012:policy/FullAdminPolicy", "v1")
            .await
            .unwrap();

        let version = response.policy_version.unwrap();
        assert_eq!(version.version_id.as_deref(), Some("v1"));
        assert_eq!(version.is_default_version, Some(true));
        // Document is URL-encoded JSON — just verify it's non-empty
        assert!(version.document.as_deref().unwrap_or("").contains("%7B"));
    }

    // ── Entities For Policy ────────────────────────────────────────────

    #[tokio::test]
    async fn list_entities_for_policy_returns_all_entity_types() {
        let mut mock = MockClient::new();
        mock.expect_list_entities_for_policy()
            .returning_bytes(xml_envelope(
                "ListEntitiesForPolicy",
                "<PolicyGroups>\
                   <member>\
                     <GroupName>SupportTeam</GroupName>\
                     <GroupId>AGPA000000000EXAMPLE</GroupId>\
                   </member>\
                 </PolicyGroups>\
                 <PolicyUsers>\
                   <member>\
                     <UserName>support-user</UserName>\
                     <UserId>AIDA000000000EXAMPLE</UserId>\
                   </member>\
                 </PolicyUsers>\
                 <PolicyRoles>\
                   <member>\
                     <RoleName>SupportRole</RoleName>\
                     <RoleId>AROA000000000EXAMPLE</RoleId>\
                   </member>\
                 </PolicyRoles>",
            ));

        let client = AwsHttpClient::from_mock(mock);
        let response = client
            .iam()
            .list_entities_for_policy("arn:aws:iam::aws:policy/AWSSupportAccess")
            .await
            .unwrap();

        assert_eq!(response.policy_groups.len(), 1);
        assert_eq!(
            response.policy_groups[0].group_name.as_deref(),
            Some("SupportTeam")
        );
        assert_eq!(response.policy_users.len(), 1);
        assert_eq!(
            response.policy_users[0].user_name.as_deref(),
            Some("support-user")
        );
        assert_eq!(response.policy_roles.len(), 1);
        assert_eq!(
            response.policy_roles[0].role_name.as_deref(),
            Some("SupportRole")
        );
    }

    #[tokio::test]
    async fn list_entities_for_policy_handles_empty() {
        let mut mock = MockClient::new();
        mock.expect_list_entities_for_policy()
            .returning_bytes(xml_envelope(
                "ListEntitiesForPolicy",
                "<PolicyGroups/><PolicyUsers/><PolicyRoles/>",
            ));

        let client = AwsHttpClient::from_mock(mock);
        let response = client
            .iam()
            .list_entities_for_policy("arn:aws:iam::aws:policy/AWSCloudShellFullAccess")
            .await
            .unwrap();
        assert!(response.policy_groups.is_empty());
        assert!(response.policy_users.is_empty());
        assert!(response.policy_roles.is_empty());
    }
}