diff --git a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/IamTest.java b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/IamTest.java index 4e10d34337..c160346ea4 100644 --- a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/IamTest.java +++ b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/IamTest.java @@ -35,6 +35,8 @@ import software.amazon.awssdk.services.iam.model.GetInstanceProfileRequest; import software.amazon.awssdk.services.iam.model.GetInstanceProfileResponse; import software.amazon.awssdk.services.iam.model.GetPolicyRequest; import software.amazon.awssdk.services.iam.model.GetPolicyResponse; +import software.amazon.awssdk.services.iam.model.GetPolicyVersionRequest; +import software.amazon.awssdk.services.iam.model.GetPolicyVersionResponse; import software.amazon.awssdk.services.iam.model.GetRoleRequest; import software.amazon.awssdk.services.iam.model.GetRoleResponse; import software.amazon.awssdk.services.iam.model.GetRolePolicyRequest; @@ -58,6 +60,7 @@ import software.amazon.awssdk.services.iam.model.ListUserTagsRequest; import software.amazon.awssdk.services.iam.model.ListUserTagsResponse; import software.amazon.awssdk.services.iam.model.ListUsersResponse; import software.amazon.awssdk.services.iam.model.NoSuchEntityException; +import software.amazon.awssdk.services.iam.model.PolicyScopeType; import software.amazon.awssdk.services.iam.model.PutRolePolicyRequest; import software.amazon.awssdk.services.iam.model.RemoveRoleFromInstanceProfileRequest; import software.amazon.awssdk.services.iam.model.RemoveUserFromGroupRequest; @@ -67,6 +70,10 @@ import software.amazon.awssdk.services.iam.model.TagUserRequest; import software.amazon.awssdk.services.iam.model.UntagUserRequest; import software.amazon.awssdk.services.iam.model.UpdateAccessKeyRequest; +import java.net.URLDecoder; +import java.nio.charset.StandardCharsets; +import java.util.List; + import static org.assertj.core.api.Assertions.*; @DisplayName("IAM Identity and Access Management") @@ -79,6 +86,11 @@ class IamTest { private static final String ROLE_NAME = "sdk-test-role"; private static final String POLICY_NAME = "sdk-test-policy"; private static final String INSTANCE_PROFILE_NAME = "sdk-test-profile"; + private static final String AWS_MANAGED_POLICY_PREFIX = "arn:aws:iam::aws:policy/"; + private static final String ADMIN_POLICY_ARN = AWS_MANAGED_POLICY_PREFIX + "AdministratorAccess"; + private static final String READ_ONLY_POLICY_ARN = AWS_MANAGED_POLICY_PREFIX + "ReadOnlyAccess"; + private static final String LAMBDA_BASIC_POLICY_ARN = + AWS_MANAGED_POLICY_PREFIX + "service-role/AWSLambdaBasicExecutionRole"; private static final String TRUST_POLICY = "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\"," + "\"Principal\":{\"Service\":\"lambda.amazonaws.com\"},\"Action\":\"sts:AssumeRole\"}]}"; private static final String POLICY_DOCUMENT = "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\"," @@ -131,6 +143,12 @@ class IamTest { iam.deletePolicy(DeletePolicyRequest.builder().policyArn(policyArn).build()); } catch (Exception ignored) {} } + try { + iam.detachUserPolicy(DetachUserPolicyRequest.builder() + .userName(USER_NAME).policyArn(READ_ONLY_POLICY_ARN).build()); + } catch (Exception ignored) { + // The policy may not have been attached if the test failed before reaching cleanup. + } try { iam.removeUserFromGroup(RemoveUserFromGroupRequest.builder() .groupName(GROUP_NAME).userName(USER_NAME).build()); @@ -524,4 +542,80 @@ class IamTest { .userName("nonexistent-user-xyz").build())) .isInstanceOf(NoSuchEntityException.class); } + + // ── AWS managed policies ─────────────────────────────────────────── + // The catalog is parsed while the native image is built and lives in its + // heap, so only a run against a native binary proves the documents survived. + + @Test + @Order(33) + void getAwsManagedPolicy() { + GetPolicyResponse response = iam.getPolicy(GetPolicyRequest.builder().policyArn(ADMIN_POLICY_ARN).build()); + + assertThat(response.policy().policyName()).isEqualTo("AdministratorAccess"); + assertThat(response.policy().arn()).isEqualTo(ADMIN_POLICY_ARN); + assertThat(response.policy().path()).isEqualTo("/"); + assertThat(response.policy().defaultVersionId()).matches("v[1-9][0-9]*"); + assertThat(response.policy().isAttachable()).isTrue(); + } + + @Test + @Order(34) + void getAwsManagedPolicyWithServiceRolePath() { + GetPolicyResponse response = + iam.getPolicy(GetPolicyRequest.builder().policyArn(LAMBDA_BASIC_POLICY_ARN).build()); + + assertThat(response.policy().policyName()).isEqualTo("AWSLambdaBasicExecutionRole"); + assertThat(response.policy().path()).isEqualTo("/service-role/"); + } + + @Test + @Order(35) + void getAwsManagedPolicyVersionDocument() { + String versionId = iam.getPolicy(GetPolicyRequest.builder().policyArn(ADMIN_POLICY_ARN).build()) + .policy().defaultVersionId(); + GetPolicyVersionResponse response = iam.getPolicyVersion(GetPolicyVersionRequest.builder() + .policyArn(ADMIN_POLICY_ARN).versionId(versionId).build()); + + assertThat(response.policyVersion().versionId()).isEqualTo(versionId); + assertThat(response.policyVersion().isDefaultVersion()).isTrue(); + String document = URLDecoder.decode(response.policyVersion().document(), StandardCharsets.UTF_8); + assertThat(document).startsWith("{").contains("\"Statement\"").contains("\"Effect\""); + } + + @Test + @Order(36) + void listAwsManagedPolicies() { + List names = iam.listPoliciesPaginator(request -> request.scope(PolicyScopeType.AWS)) + .policies().stream().map(policy -> policy.policyName()).toList(); + + assertThat(names).hasSizeGreaterThan(1000) + .contains("AdministratorAccess", "ReadOnlyAccess", "AWSLambdaBasicExecutionRole"); + } + + @Test + @Order(37) + void attachAwsManagedPolicyToUser() { + iam.attachUserPolicy(AttachUserPolicyRequest.builder() + .userName(USER_NAME).policyArn(READ_ONLY_POLICY_ARN).build()); + + ListAttachedUserPoliciesResponse response = iam.listAttachedUserPolicies(ListAttachedUserPoliciesRequest.builder() + .userName(USER_NAME).build()); + + assertThat(response.attachedPolicies()) + .anyMatch(p -> READ_ONLY_POLICY_ARN.equals(p.policyArn()) + && "ReadOnlyAccess".equals(p.policyName())); + + iam.detachUserPolicy(DetachUserPolicyRequest.builder() + .userName(USER_NAME).policyArn(READ_ONLY_POLICY_ARN).build()); + } + + @Test + @Order(38) + void attachUnknownAwsManagedPolicyThrows() { + assertThatThrownBy(() -> iam.attachUserPolicy(AttachUserPolicyRequest.builder() + .userName(USER_NAME) + .policyArn(AWS_MANAGED_POLICY_PREFIX + "NoSuchManagedPolicyXyz").build())) + .isInstanceOf(NoSuchEntityException.class); + } } diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index d9b6392561..ef94a2b86d 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -38,20 +38,20 @@ quarkus: add-all-charsets: true resources: # The Pricing service reads its bundled snapshot, EC2 reads its image - # catalog (ec2/image-catalog.yaml), IAM reads its AWS managed-policy - # catalog (iam/managed-policies.yaml, iam/managed-policy-documents.json and - # iam/managed-policy-versions.json), AppSync's GraphQL parser reads its + # catalog (ec2/image-catalog.yaml), AppSync's GraphQL parser reads its # message bundles, and API Gateway's Velocity engine reads its default - # properties at runtime via - # ClassLoader#getResourceAsStream. Quarkus's static resource analysis - # cannot detect these dynamically-built paths, so they are registered - # explicitly to ensure GraalVM includes every file in the native image. + # properties at runtime via ClassLoader#getResourceAsStream. Quarkus's + # static resource analysis cannot detect these dynamically-built paths, + # so they are registered explicitly to ensure GraalVM includes every + # file in the native image. + # The IAM managed-policy catalog (iam/*) is not listed on purpose. + # AwsManagedPolicies parses it while the image is built and the parsed + # catalog lives in the image heap, so the 3.5 MB of source files would + # only be a second copy of the same data. includes: - pricing-snapshots/** - ui/*.html - ec2/*.yaml - - iam/*.yaml - - iam/*.json - i18n/*.properties - org/apache/velocity/runtime/defaults/*.properties # DriverManager finds the JDBC drivers through ServiceLoader, and the MySQL