fix(ecr): validate lifecycle policy documents before mutation (#7611)

This commit is contained in:
Andre Kurait authored and GitHub committed 2026-11-05 13:54:02 -04:00
1 parent d4a65d477b
commit f719d6af56
7 files changed
+223 -12

No files matched your search

@@ -59,6 +59,10 @@ public class EcrCfnProvisioner implements CfnResourceProvisioner {
String mutability = ctx.resolveOptional(props, "ImageTagMutability");
Map<String, String> tags = parseCfnTags(props != null ? props.get("Tags") : null, ctx);
String region = ctx.region();
String lifecyclePolicy = lifecyclePolicyText(props, ctx);
if (lifecyclePolicy != null) {
EcrService.validateLifecyclePolicyDocument(lifecyclePolicy);
}
// provision is also the update path. Only the repository this resource already owns is
// reconciled in place; a replacing update derives a different name and creates, and a name
@@ -70,7 +74,6 @@ public class EcrCfnProvisioner implements CfnResourceProvisioner {
// Both policies are updatable: a template that declares one sets it, and on the update
// path a template that no longer declares one deletes it, tolerating "none set".
String lifecyclePolicy = lifecyclePolicyText(props, ctx);
if (lifecyclePolicy != null) {
ecrService.putLifecyclePolicy(repoName, null, lifecyclePolicy, region);
} else if (reusing) {
@@ -125,7 +128,7 @@ public class EcrCfnProvisioner implements CfnResourceProvisioner {
}
JsonNode lp = ctx.engine().resolveNode(props.get("LifecyclePolicy"));
String policyText = lp.path("LifecyclePolicyText").asText(null);
return policyText == null || policyText.isEmpty() ? null : policyText;
return policyText;
}
/** {@code RepositoryPolicyText} as a JSON string, or null when the template declares none. */
@@ -1675,12 +1675,36 @@ public class EcrService implements ResourceProvider {
}
public Repository putLifecyclePolicy(String repoName, String registryId, String policyText, String region) {
validateLifecyclePolicyDocument(policyText);
Repository repo = requireRepo(repoName, registryId, region);
repo.setLifecyclePolicyText(policyText);
repoStore.put(key(region, repo.getRegistryId(), repoName), repo);
return repo;
}
public static void validateLifecyclePolicyDocument(String text) {
JsonNode policy;
try {
policy = text == null ? null : SINGLE_JSON_VALUE.readTree(text);
} catch (JsonProcessingException e) {
throw new AwsException("InvalidParameterException", "Lifecycle policy must be a JSON object", 400);
}
JsonNode rules = policy == null || !policy.isObject() ? null : policy.get("rules");
if (rules == null || !rules.isArray() || rules.isEmpty()) {
throw new AwsException("InvalidParameterException",
"Lifecycle policy must contain a nonempty rules array", 400);
}
Set<Integer> priorities = new LinkedHashSet<>();
for (JsonNode rule : rules) {
JsonNode priority = rule.get("rulePriority");
if (priority == null || !priority.isIntegralNumber() || !priority.canConvertToInt()
|| priority.intValue() <= 0 || !priorities.add(priority.intValue())) {
throw new AwsException("InvalidParameterException",
"Lifecycle rule priorities must be unique positive integers", 400);
}
}
}
public Repository getLifecyclePolicy(String repoName, String registryId, String region) {
Repository repo = requireRepo(repoName, registryId, region);
if (repo.getLifecyclePolicyText() == null) {