fix(ssm): normalize parameter resource ARNs (#5261)

* fix(ssm): normalize parameter resource ARNs

A parameter's ARN is now arn:<partition>:ssm:<region>:<account>:parameter/<name
without leading slash> for both top-level names and paths, matching AWS. Before,
a top-level name such as foo got parameterfoo.

SsmService.parameterResource(name) is the single formula, used by PutParameter
and by the AWS::SSM::Parameter provisioner, so the CloudFormation Arn attribute
equals the GetParameter ARN.

Tag operations and the internal getParameter(nameOrArn, region) read resolve an
ARN to the stored name and, for parameter/x when only x is stored, fall back to x,
so a top-level parameter is found by its ARN, also with a :version or :label
selector. ECS task secrets pass valueFrom to
that read whole instead of stripping only ":parameter". Legacy parameterfoo ARNs
still resolve.

* test(cloudformation): share the CloudFormation Query request builder

Six CloudFormation integration tests each carried a private copy of the
same Query request builder, as cfn or request. CfnRequests.cfn in testing
replaces them, as RdsRequests.rds does for RDS. A null stack name leaves
StackName out, which the ContinueUpdateRollback validation test needs.
This commit is contained in:
Mario Toffia authored and GitHub committed 2026-10-19 09:46:50 -04:00
1 parent e75b15809f
commit c145fa104c
16 files changed
+369 -139

No files matched your search

@@ -84,7 +84,8 @@ public class SsmCfnProvisioner implements CfnResourceProvisioner {
r.getAttributes().put("Name", name);
r.getAttributes().put("Type", type);
r.getAttributes().put("Value", value);
r.getAttributes().put("Arn", parameterArn(name, ctx));
r.getAttributes().put("Arn", AwsArnUtils.Arn.of("ssm", ctx.region(), ctx.accountId(),
SsmService.parameterResource(name)).toString());
// Tag keys cannot contain a comma, so the sorted keys join losslessly.
if (tags.isEmpty()) {
r.getAttributes().remove(SSM_TEMPLATE_TAG_KEYS_ATTR);
@@ -161,12 +162,6 @@ public class SsmCfnProvisioner implements CfnResourceProvisioner {
return keys == null || keys.isEmpty() ? List.of() : Arrays.asList(keys.split(","));
}
/** AWS's form is {@code parameter/<name>} whether or not the name starts with a slash. */
private static String parameterArn(String name, ProvisionContext ctx) {
String path = name.startsWith("/") ? name : "/" + name;
return AwsArnUtils.Arn.of("ssm", ctx.region(), ctx.accountId(), "parameter" + path).toString();
}
@Override
public void delete(String resourceType, String physicalId, String region) {
// ParameterNotFound is SSM's own code for a parameter that is already gone.
@@ -2053,8 +2053,7 @@ public class EcsContainerManager {
selector.versionId(), selector.versionStage(), secretRegion);
value = secret == null ? null : secret.getSecretString();
} else {
String parameterName = ssmParameterName(valueFrom);
Parameter parameter = ssmService.getParameter(parameterName, secretRegion);
Parameter parameter = ssmService.getParameter(valueFrom, secretRegion);
value = parameter == null ? null : parameter.getValue();
}
} catch (AwsException e) {
@@ -2100,16 +2099,6 @@ public class EcsContainerManager {
return taskRegion;
}
private String ssmParameterName(String valueFrom) {
if (AwsArnUtils.isArnFor(valueFrom, "ssm")) {
int parameterMarker = valueFrom.indexOf(":parameter");
if (parameterMarker >= 0) {
return valueFrom.substring(parameterMarker + ":parameter".length());
}
}
return valueFrom;
}
/**
* Resolves the host address at which a running task container is reachable from the
* Floci process — used to register the container as an ELBv2 target.
@@ -298,7 +298,7 @@ public class SsmService implements ResourceProvider {
// AWS never moves an Advanced parameter back to Standard behind an overwrite that omits the tier
// or asks for Intelligent-Tiering.
parameter.setTier(tier != null ? tier : existing != null ? tierOf(existing) : "Standard");
parameter.setArn(regionResolver.buildArn("ssm", region, "parameter" + name));
parameter.setArn(regionResolver.buildArn("ssm", region, parameterResource(name)));
parameter.setLastModifiedDate(Instant.now());
if (existing != null && existing.getTags() != null) {
@@ -314,9 +314,14 @@ public class SsmService implements ResourceProvider {
return version;
}
/** Reads on behalf of ECS task secrets, CodeBuild and CloudFormation ssm-secure, which decrypt. */
public Parameter getParameter(String name, String region) {
return getParameter(name, true, region);
/** AWS's form is {@code parameter/<name>} whether or not the name starts with a slash. */
public static String parameterResource(String name) {
return "parameter" + (name.startsWith("/") ? name : "/" + name);
}
/** Reads on behalf of ECS task secrets, CodeBuild and CloudFormation ssm-secure, which decrypt and may pass an ARN. */
public Parameter getParameter(String nameOrArn, String region) {
return getParameter(parameterName(nameOrArn, region), true, region);
}
public Parameter getParameter(String name, boolean withDecryption, String region) {
@@ -527,7 +532,7 @@ public class SsmService implements ResourceProvider {
}
return imageCatalog.findByPublicParameterName(name).map(image -> {
Parameter parameter = new Parameter(name, image.imageId, "String");
parameter.setArn(AwsArnUtils.Arn.of("ssm", region, "", "parameter" + name).toString());
parameter.setArn(AwsArnUtils.Arn.of("ssm", region, "", parameterResource(name)).toString());
parameter.setLastModifiedDate(Instant.parse(image.creationDate));
return parameter;
});
@@ -898,8 +903,7 @@ public class SsmService implements ResourceProvider {
public void addTagsToResource(String resourceId, Map<String, String> tags, String region) {
validateTagKeys(tags);
String normalizedId = normalizeResourceId(resourceId);
String storageKey = regionKey(region, normalizedId);
String storageKey = regionKey(region, parameterName(resourceId, region));
Parameter param = parameterStore.get(storageKey)
.orElseThrow(() -> new AwsException("InvalidResourceId",
"Resource " + resourceId + " not found.", 400));
@@ -939,8 +943,7 @@ public class SsmService implements ResourceProvider {
}
public Map<String, String> listTagsForResource(String resourceId, String region) {
String normalizedId = normalizeResourceId(resourceId);
String storageKey = regionKey(region, normalizedId);
String storageKey = regionKey(region, parameterName(resourceId, region));
Parameter param = parameterStore.get(storageKey)
.orElseThrow(() -> new AwsException("InvalidResourceId",
"Resource " + resourceId + " not found.", 400));
@@ -948,8 +951,7 @@ public class SsmService implements ResourceProvider {
}
public void removeTagsFromResource(String resourceId, List<String> tagKeys, String region) {
String normalizedId = normalizeResourceId(resourceId);
String storageKey = regionKey(region, normalizedId);
String storageKey = regionKey(region, parameterName(resourceId, region));
Parameter param = parameterStore.get(storageKey)
.orElseThrow(() -> new AwsException("InvalidResourceId",
"Resource " + resourceId + " not found.", 400));
@@ -963,26 +965,31 @@ public class SsmService implements ResourceProvider {
LOG.debugv("Removed tags from parameter: {0}", resourceId);
}
private static String normalizeResourceId(String resourceId) {
if (resourceId != null && resourceId.startsWith("arn:")) {
/**
* The stored name a parameter name or ARN addresses. The ARN carries a leading slash the stored name may
* lack, so {@code parameter/foo} falls back to {@code foo} when only {@code foo} is stored. A version or
* label selector after the last colon is kept, but the fallback checks the name without it.
*/
private String parameterName(String nameOrArn, String region) {
if (nameOrArn != null && nameOrArn.startsWith("arn:")) {
try {
AwsArnUtils.Arn arn = AwsArnUtils.parse(resourceId);
if ("ssm".equals(arn.service())) {
String resource = arn.resource();
if (resource.startsWith("parameter/")) {
return resource.substring("parameter".length());
}
if (resource.startsWith("parameter")) {
return resource.substring("parameter".length());
AwsArnUtils.Arn arn = AwsArnUtils.parse(nameOrArn);
if ("ssm".equals(arn.service()) && arn.resource().startsWith("parameter")) {
String name = arn.resource().substring("parameter".length());
int separator = name.lastIndexOf(':');
String baseName = separator > 0 ? name.substring(0, separator) : name;
if (baseName.startsWith("/") && parameterStore.get(regionKey(region, baseName)).isEmpty()
&& parameterStore.get(regionKey(region, baseName.substring(1))).isPresent()) {
return name.substring(1);
}
return name;
}
} catch (IllegalArgumentException e) {
// Not a valid ARN; fall through and use resourceId directly so callers
// querying parameterStore produce the standard InvalidResourceId error.
LOG.debugv("Failed to parse resourceId as ARN: {0}", resourceId);
// Not a valid ARN; use it as a name so the caller reports its usual not-found error.
LOG.debugv("Failed to parse resourceId as ARN: {0}", nameOrArn);
}
}
return resourceId;
return nameOrArn;
}
// ──────────────────────── Documents and Share Permissions ────────────────