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

@@ -19,6 +19,7 @@ import io.floci.aws.core.common.StoredResourceArns;
import io.floci.aws.core.resource.ExplorerResource;
import io.floci.aws.core.resource.ResourceProvider;
import io.floci.aws.core.resource.SupportedResourceType;
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.core.storage.StorageFactory;
@@ -57,6 +58,7 @@ public class SsmService implements ResourceProvider, StoredResourceArns {
private static final Logger LOG = Logger.getLogger(SsmService.class);
private static final String PUBLIC_PARAMETER_PREFIX = "/aws/service/";
private static final String DOCUMENT_PREFIX = "document/";
private static final String ECS_AL2023_RECOMMENDED =
"/aws/service/ecs/optimized-ami/amazon-linux-2023/recommended";
private static final String ECS_AL2023_ARM64_RECOMMENDED =
@@ -1509,6 +1511,51 @@ public class SsmService implements ResourceProvider, StoredResourceArns {
// raises InvalidDocument instead of silently minting or reporting share state for
// a document that does not exist.
/**
* The accounts the document of that name in the owner's account and Region is shared with; empty when there is no
* such document.
*/
List<String> documentSharedWith(String ownerAccountId, String name, String region) {
String key = regionKey(region, name);
if (AccountAwareStorageBackend.readInAccount(documentStore, ownerAccountId, key).isEmpty()) {
return List.of();
}
return AccountAwareStorageBackend.readInAccount(documentPermissionStore, ownerAccountId, key)
.map(List::copyOf).orElse(List.of());
}
/**
* The document an association runs: the caller's by its name or ARN, or, by its ARN, one another account shared
* with the caller. A document of another account not shared with the caller does not exist for it.
*/
private SsmDocument associatedDocument(String nameOrArn, String region) {
if (!AwsArnUtils.looksLikeArn(nameOrArn)) {
return getDocument(nameOrArn, region);
}
AwsArnUtils.Arn arn;
try {
arn = AwsArnUtils.parse(nameOrArn);
} catch (IllegalArgumentException e) {
LOG.debugv("Failed to parse document name as ARN: {0}", nameOrArn);
return getDocument(nameOrArn, region);
}
if (!"ssm".equals(arn.service()) || !arn.resource().startsWith(DOCUMENT_PREFIX)
|| !arn.region().equals(region) || !arn.partition().equals(regionResolver.partitionForRegion(region))) {
return getDocument(nameOrArn, region);
}
String name = arn.resource().substring(DOCUMENT_PREFIX.length());
String caller = regionResolver.getAccountId();
if (arn.accountId().equals(caller)) {
return getDocument(name, region);
}
if (!documentSharedWith(arn.accountId(), name, region).contains(caller)) {
throw new AwsException("InvalidDocument", "Document " + nameOrArn + " does not exist.", 400);
}
return AccountAwareStorageBackend.readInAccount(documentStore, arn.accountId(), regionKey(region, name))
.orElseThrow(() -> new AwsException("InvalidDocument", "Document " + nameOrArn + " does not exist.",
400));
}
public SsmDocument getDocument(String name, String region) {
SsmDocument document = documentStore.get(regionKey(region, name))
.orElseThrow(() -> new AwsException("InvalidDocument",
@@ -1728,7 +1775,7 @@ public class SsmService implements ResourceProvider, StoredResourceArns {
Map<String, String> tags,
String region) {
validateTagKeys(tags);
SsmDocument document = getDocument(name, region);
SsmDocument document = associatedDocument(name, region);
validateDocumentVersion(document, documentVersion);
if (instanceId != null && !instanceId.isBlank()) {
@@ -1795,7 +1842,7 @@ public class SsmService implements ResourceProvider, StoredResourceArns {
"The specified association does not exist.", 400));
if (documentVersion != null) {
validateDocumentVersion(getDocument(association.getName(), region), documentVersion);
validateDocumentVersion(associatedDocument(association.getName(), region), documentVersion);
}
if (associationName != null) association.setAssociationName(associationName);
@@ -2056,6 +2103,20 @@ public class SsmService implements ResourceProvider, StoredResourceArns {
return "ssm";
}
/** A document another account owns, which {@code CreateAssociation} runs from that account when it is shared. */
@Override
public boolean reachedInItsAccount(String arn, String accountId) {
if (!AwsArnUtils.isArnFor(arn, "ssm")) {
return false;
}
AwsArnUtils.Arn parsed = AwsArnUtils.parse(arn);
return !parsed.accountId().isEmpty() && !parsed.accountId().equals(accountId)
&& parsed.resource().startsWith(DOCUMENT_PREFIX)
&& parsed.partition().equals(regionResolver.partitionForRegion(parsed.region()))
&& AccountAwareStorageBackend.readInAccount(documentStore, parsed.accountId(),
regionKey(parsed.region(), parsed.resource().substring(DOCUMENT_PREFIX.length()))).isPresent();
}
/**
* The {@code parameter/<name>} ARN a legacy {@code parameter<name>} ARN of the caller's partition, Region and
* account names, which the tagging operations act on as {@link #parameterTagName} reads it; empty for any other