From 0a8b6396ac4d78873344ac6c3fb70d8a2c173734 Mon Sep 17 00:00:00 2001 From: Niels Kootstra <545768+nkootstra@users.noreply.github.com> Date: Sat, 14 Nov 2026 22:22:41 +0100 Subject: [PATCH] test(ssm): check CreateAssociation on the document it runs (#8230) Under IAM enforcement, CreateAssociation is checked on the document its Name gives as well as the association it makes, which the generated request-resource table has done since #8203. An enforcement test now shows a user allowed on one document can associate it and is refused for another, and a request-resource test shows a shared document given by its ARN is checked on that ARN. The SSM docs note the check. --- docs/services/ssm.md | 3 +- .../aws/core/common/RequestResourcesTest.java | 10 +++ ...ociationIamEnforcementIntegrationTest.java | 72 +++++++++++++++++++ 3 files changed, 84 insertions(+), 1 deletion(-) create mode 100644 src/test/java/io/floci/aws/services/ssm/SsmCreateAssociationIamEnforcementIntegrationTest.java diff --git a/docs/services/ssm.md b/docs/services/ssm.md index b9388385bb..1dd665ca5a 100644 --- a/docs/services/ssm.md +++ b/docs/services/ssm.md @@ -32,7 +32,8 @@ the `DocumentDescription` of `CreateDocument`, `UpdateDocument` and `DescribeDocument`, and in each `ListDocuments` entry. With IAM enforcement on, the tag calls are checked against the resource their `ResourceType` and `ResourceId` name: `document/`, `association/` or `parameter/` -without its leading `/`. `CreateDocument` is checked against `document/`. +without its leading `/`. `CreateDocument` is checked against `document/`, and `CreateAssociation` against the +association and the `document/` its `Name` gives. `GetParameters` returns successful parameters alphabetically by `Name`, preserving the selected versions and labels. diff --git a/src/test/java/io/floci/aws/core/common/RequestResourcesTest.java b/src/test/java/io/floci/aws/core/common/RequestResourcesTest.java index 1b3d3c4453..aeb2bc0a7e 100644 --- a/src/test/java/io/floci/aws/core/common/RequestResourcesTest.java +++ b/src/test/java/io/floci/aws/core/common/RequestResourcesTest.java @@ -184,6 +184,16 @@ class RequestResourcesTest { "000000000000", null)); } + @Test + void anAssociationOnASharedDocumentGivenByItsArnIsCheckedOnThatArn() throws Exception { + String shared = "arn:aws:ssm:us-east-1:100000008230:document/shared"; + List arns = RequestResources.ofBody("ssm:CreateAssociation", + new ObjectMapper().readTree("{\"Name\":\"" + shared + "\"}"), "aws", "us-east-1", "000000000000", + null); + assertTrue(arns.contains(shared), arns.toString()); + assertFalse(arns.stream().anyMatch(arn -> arn.contains("document/arn:")), arns.toString()); + } + /** * DynamoDB is built by hand, but the table reads {@code ListTables}: AWS was measured to check it on the literal * {@code table/*} of the request's partition, Region and account, whatever table its request names, though the diff --git a/src/test/java/io/floci/aws/services/ssm/SsmCreateAssociationIamEnforcementIntegrationTest.java b/src/test/java/io/floci/aws/services/ssm/SsmCreateAssociationIamEnforcementIntegrationTest.java new file mode 100644 index 0000000000..33904867ed --- /dev/null +++ b/src/test/java/io/floci/aws/services/ssm/SsmCreateAssociationIamEnforcementIntegrationTest.java @@ -0,0 +1,72 @@ +package io.floci.aws.services.ssm; + +import io.floci.aws.testing.AwsTestClients; +import io.floci.aws.testing.EnforcementFixtures; +import io.floci.aws.testing.IamEnforcementProfile; +import io.floci.aws.testing.PartitionCleanup; +import io.quarkus.test.junit.QuarkusTest; +import io.quarkus.test.junit.TestProfile; +import io.restassured.RestAssured; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; +import software.amazon.awssdk.regions.Region; +import software.amazon.awssdk.services.ssm.SsmClient; +import software.amazon.awssdk.services.ssm.model.DocumentType; +import software.amazon.awssdk.services.ssm.model.SsmException; + +import java.util.UUID; + +import static io.floci.aws.testing.EnforcementFixtures.auth; +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +/** Creating an association is checked against the document it runs. */ +@QuarkusTest +@TestProfile(IamEnforcementProfile.class) +class SsmCreateAssociationIamEnforcementIntegrationTest { + + private static final String ACCOUNT = "000000000000"; + private static final String CONTENT = "{\"schemaVersion\":\"2.2\",\"description\":\"d\",\"mainSteps\":[{" + + "\"action\":\"aws:runShellScript\",\"name\":\"s\",\"inputs\":{\"runCommand\":[\"echo hi\"]}}]}"; + + @RegisterExtension + final PartitionCleanup cleanup = new PartitionCleanup(); + + @Test + void anAssociationIsCreatedOnlyForADocumentThePolicyNames() { + String suffix = UUID.randomUUID().toString().substring(0, 8); + String allowedDocument = "iam-assoc-allowed-" + suffix; + String deniedDocument = "iam-assoc-denied-" + suffix; + try (SsmClient root = AwsTestClients.create(SsmClient.builder(), ACCOUNT, Region.US_EAST_1)) { + try { + createDocument(root, allowedDocument); + createDocument(root, deniedDocument); + String userName = "ssm-assoc-" + suffix; + EnforcementFixtures.UserCredentials credentials = EnforcementFixtures.createUser( + cleanup, RestAssured.port, auth(ACCOUNT, "iam"), userName); + EnforcementFixtures.putUserPolicy(RestAssured.port, auth(ACCOUNT, "iam"), userName, "p", """ + {"Version":"2012-10-17","Statement":[{"Effect":"Allow","Action":"ssm:CreateAssociation", + "Resource":["arn:aws:ssm:*:*:association/*","arn:aws:ssm:*:*:document/%s"]}]}""" + .formatted(allowedDocument)); + try (SsmClient user = AwsTestClients.create(SsmClient.builder(), credentials.accessKeyId(), + Region.US_EAST_1)) { + assertDoesNotThrow(() -> associate(user, allowedDocument)); + SsmException denied = assertThrows(SsmException.class, () -> associate(user, deniedDocument)); + assertEquals("AccessDeniedException", denied.awsErrorDetails().errorCode()); + } + } finally { + root.deleteDocument(r -> r.name(allowedDocument).force(true)); + root.deleteDocument(r -> r.name(deniedDocument).force(true)); + } + } + } + + private static void createDocument(SsmClient root, String name) { + root.createDocument(r -> r.name(name).documentType(DocumentType.COMMAND).content(CONTENT)); + } + + private static void associate(SsmClient ssm, String document) { + ssm.createAssociation(r -> r.name(document).targets(t -> t.key("InstanceIds").values("i-0123456789abcdef0"))); + } +}