fix(ssm): associate a document another account shared with the caller (#8646)

CreateAssociation now resolves a document ARN in another account to that account's document when its owner shared it with the caller through ModifyDocumentPermission, and answers InvalidDocument for one that is not shared, is of another partition or does not exist. UpdateAssociation checks a new DocumentVersion through the same lookup. With IAM enforcement on, reachedInItsAccount has the request checked on the owner's document ARN alone, so the caller's identity policy must allow that ARN.

Closes #8364
This commit is contained in:
Niels Kootstra authored and GitHub committed 2026-11-16 17:01:17 -04:00
1 parent e44f9137be
commit aef8416c4b
4 files changed
+230 -2

No files matched your search

@@ -4,6 +4,7 @@ import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.floci.aws.core.common.AwsException;
import io.floci.aws.core.common.RegionResolver;
import io.floci.aws.core.storage.AccountAwareStorageBackend;
import io.floci.aws.core.storage.InMemoryStorage;
import io.floci.aws.core.storage.StorageBackend;
import io.floci.aws.services.eventbridge.EventBridgeService;
@@ -1965,4 +1966,63 @@ class SsmServiceTest {
ssmService.describeParameters(List.of(filter), "us-east-1"));
assertEquals(errorCode, ex.getErrorCode());
}
private static final String CALLER = "000000000000";
private static final String OWNER = "100000008364";
@Test
void anAssociationOnASharedDocumentGivenByItsArnTakesANewDocumentVersion() {
AccountAwareStorageBackend<List<String>> permissions = AccountAwareStorageBackend.inMemory(CALLER);
AccountAwareStorageBackend<SsmDocument> documents = AccountAwareStorageBackend.inMemory(CALLER);
documents.putForAccount(OWNER, "us-east-1::shared", ownerDocument("shared"));
permissions.putForAccount(OWNER, "us-east-1::shared", List.of(CALLER));
SsmService service = sharingService(permissions, documents);
SsmAssociation association = service.createAssociation("arn:aws:ssm:us-east-1:" + OWNER + ":document/shared",
null, null, "i-0123456789abcdef0", null, null, null, "us-east-1");
SsmAssociation updated = service.updateAssociation(association.getAssociationId(), null, "1", null, null,
null, null, null, null, "us-east-1");
assertEquals("1", updated.getDocumentVersion());
}
@Test
void aSharedDocumentGivenByAnArnOfAnotherPartitionDoesNotExist() {
AccountAwareStorageBackend<List<String>> permissions = AccountAwareStorageBackend.inMemory(CALLER);
AccountAwareStorageBackend<SsmDocument> documents = AccountAwareStorageBackend.inMemory(CALLER);
documents.putForAccount(OWNER, "us-east-1::shared", ownerDocument("shared"));
permissions.putForAccount(OWNER, "us-east-1::shared", List.of(CALLER));
SsmService service = sharingService(permissions, documents);
String otherPartition = "arn:aws-cn:ssm:us-east-1:" + OWNER + ":document/shared";
AwsException e = assertThrows(AwsException.class, () -> service.createAssociation(otherPartition,
null, null, "i-0123456789abcdef0", null, null, null, "us-east-1"));
assertEquals("InvalidDocument", e.getErrorCode());
assertFalse(service.reachedInItsAccount(otherPartition, CALLER));
}
@Test
void aDocumentSharedBeforeStorageWasScopedByAccountIsStillShared() {
InMemoryStorage<String, List<String>> permissions = new InMemoryStorage<>();
InMemoryStorage<String, SsmDocument> documents = new InMemoryStorage<>();
permissions.put("us-east-1::legacy", List.of(OWNER));
documents.put("us-east-1::legacy", ownerDocument("legacy"));
SsmService service = sharingService(new AccountAwareStorageBackend<>(permissions, null, CALLER),
new AccountAwareStorageBackend<>(documents, null, CALLER));
assertEquals(List.of(OWNER), service.documentSharedWith(CALLER, "legacy", "us-east-1"));
}
private static SsmService sharingService(StorageBackend<String, List<String>> permissions,
StorageBackend<String, SsmDocument> documents) {
return new SsmService(new InMemoryStorage<>(), new InMemoryStorage<>(), permissions, documents,
new InMemoryStorage<>(), new InMemoryStorage<>(), 5, new RegionResolver("us-east-1", CALLER));
}
private static SsmDocument ownerDocument(String name) {
SsmDocument document = new SsmDocument(name, "{}", "Command");
document.setDocumentVersion(1);
document.setVersions(new LinkedHashMap<>(Map.of("1", "{}")));
return document;
}
}
@@ -0,0 +1,103 @@
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;
/** An account associates a document another account shared with it by giving the document's ARN. */
@QuarkusTest
@TestProfile(IamEnforcementProfile.class)
class SsmSharedDocumentAssociationIntegrationTest {
private static final String ACCOUNT = "000000000000";
private static final String OWNER = "100000008364";
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 aSharedDocumentIsAssociatedByItsArnAndAnUnsharedOneIsNot() {
String suffix = UUID.randomUUID().toString().substring(0, 8);
String shared = "shared-" + suffix;
String unshared = "unshared-" + suffix;
try (SsmClient owner = AwsTestClients.create(SsmClient.builder(), OWNER, Region.US_EAST_1)) {
try {
createDocument(owner, shared);
createDocument(owner, unshared);
owner.modifyDocumentPermission(r -> r.name(shared).permissionType("Share").accountIdsToAdd(ACCOUNT));
String userName = "ssm-shared-" + 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/*","%s","%s"]}]}"""
.formatted(arn(shared), arn(unshared)));
try (SsmClient user = AwsTestClients.create(SsmClient.builder(), credentials.accessKeyId(),
Region.US_EAST_1)) {
assertDoesNotThrow(() -> associate(user, arn(shared)));
SsmException missing = assertThrows(SsmException.class, () -> associate(user, arn(unshared)));
assertEquals("InvalidDocument", missing.awsErrorDetails().errorCode());
}
} finally {
owner.deleteDocument(r -> r.name(shared).force(true));
owner.deleteDocument(r -> r.name(unshared).force(true));
}
}
}
@Test
void aSharedDocumentIsNotAssociatedWithoutAnIdentityPolicyNamingIt() {
String suffix = UUID.randomUUID().toString().substring(0, 8);
String shared = "shared-ungranted-" + suffix;
try (SsmClient owner = AwsTestClients.create(SsmClient.builder(), OWNER, Region.US_EAST_1)) {
try {
createDocument(owner, shared);
owner.modifyDocumentPermission(r -> r.name(shared).permissionType("Share").accountIdsToAdd(ACCOUNT));
String userName = "ssm-ungranted-" + 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/*"}]}""");
try (SsmClient user = AwsTestClients.create(SsmClient.builder(), credentials.accessKeyId(),
Region.US_EAST_1)) {
SsmException denied = assertThrows(SsmException.class, () -> associate(user, arn(shared)));
assertEquals("AccessDeniedException", denied.awsErrorDetails().errorCode());
}
} finally {
owner.deleteDocument(r -> r.name(shared).force(true));
}
}
}
private static String arn(String document) {
return "arn:aws:ssm:us-east-1:" + OWNER + ":document/" + document;
}
private static void createDocument(SsmClient owner, String name) {
owner.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")));
}
}