feat(elasticache): add validation for cache parameter group existence in replication group operations (#2957)

Signed-off-by: Matheus André <matheusandr2@gmail.com>
This commit is contained in:
Matheus André authored and GitHub committed 2026-09-03 17:24:34 -04:00
1 parent df0e7bc79c
commit 6c9cc95d54
5 files changed
+127 -1

No files matched your search

+5
View File
@@ -113,6 +113,11 @@ than rejecting names a partial catalogue happens to be missing, which would refu
AWS accepts. `DescribeCacheParameters` returns those parameters; a request for `system` or
`engine-default` parameters returns none, and listings are unpaged.
A replication group that names a parameter group is refused with `CacheParameterGroupNotFound` when
no such group exists, and a parameter group still referenced by a replication group cannot be
deleted: `DeleteCacheParameterGroup` answers `InvalidCacheParameterGroupState` until that
replication group is gone.
## Configuration
| Variable | Default | Description |
@@ -144,6 +144,9 @@ public class ElastiCacheService implements ResourceProvider {
// resolved with the other validations, before a port is taken or a container started
ReplicationGroupSettings resolvedSettings =
settings.withKmsKeyId(resolveKmsKeyArn(settings.kmsKeyId(), request.region()));
if (request.cacheParameterGroupName() != null && !request.cacheParameterGroupName().isBlank()) {
requireParameterGroup(request.cacheParameterGroupName());
}
if (groups.get(groupId).isPresent()) {
throw new AwsException("ReplicationGroupAlreadyExistsFault",
"Replication group " + groupId + " already exists.", 400);
@@ -1143,11 +1146,22 @@ public class ElastiCacheService implements ResourceProvider {
throw new AwsException("CacheParameterGroupNotFound",
"CacheParameterGroupnot found: " + name, 404);
}
if (isParameterGroupInUse(name)) {
throw new AwsException("InvalidCacheParameterGroupState",
"One or more cache clusters are still members of this parameter group "
+ name + ", so the group cannot be deleted.", 400);
}
parameterGroups.delete(name);
}
LOG.infov("Deleted cache parameter group {0}", name);
}
/** Whether a stored replication group still references the parameter group by that name. */
private boolean isParameterGroupInUse(String name) {
return groups.scan(key -> true).stream()
.anyMatch(group -> name.equals(group.getCacheParameterGroupName()));
}
@Override
public List<ExplorerResource> getResources() {
List<ExplorerResource> resources = new ArrayList<>();
@@ -336,7 +336,7 @@ class ElastiCacheIntegrationTest {
equalTo(GROUP_ID + "-reused"));
}
private static boolean isDockerAvailable() {
static boolean isDockerAvailable() {
try {
Process process = new ProcessBuilder("docker", "version", "--format", "{{.Server.Version}}")
.redirectErrorStream(true)
@@ -1,6 +1,7 @@
package io.github.hectorvent.floci.services.elasticache;
import io.quarkus.test.junit.QuarkusTest;
import org.junit.jupiter.api.Assumptions;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
@@ -268,6 +269,75 @@ class ElastiCacheParameterGroupIntegrationTest {
.body(containsString("CacheParameterGroupnot found: absent-pg"));
}
@Test
@Order(4)
void aReplicationGroupCannotReferenceAnUnknownParameterGroup() {
// The lookup runs with the other validations, before any container is started, so the
// refusal carries the parameter group's own not-found rather than a provisioning failure.
query("CreateReplicationGroup")
.formParam("ReplicationGroupId", "pg-absent-rg")
.formParam("ReplicationGroupDescription", "references a missing group")
.formParam("CacheParameterGroupName", "absent-pg")
.when()
.post("/")
.then()
.statusCode(404)
.body(containsString("<Code>CacheParameterGroupNotFound</Code>"))
.body(containsString("CacheParameterGroup absent-pg not found."));
query("DescribeReplicationGroups")
.formParam("ReplicationGroupId", "pg-absent-rg")
.when()
.post("/")
.then()
.statusCode(404);
}
@Test
@Order(4)
void aParameterGroupInUseByAReplicationGroupCannotBeDeleted() {
// The replication group is a real one behind a container, so this case needs Docker
// where the rest of the class does not.
Assumptions.assumeTrue(ElastiCacheIntegrationTest.isDockerAvailable(),
"Docker daemon must be available to create a replication group");
String replicationGroupId = "pg-in-use-rg";
query("CreateReplicationGroup")
.formParam("ReplicationGroupId", replicationGroupId)
.formParam("ReplicationGroupDescription", "holds the parameter group in use")
.formParam("CacheParameterGroupName", GROUP)
.when()
.post("/")
.then()
.statusCode(200);
try {
query("DeleteCacheParameterGroup")
.formParam("CacheParameterGroupName", GROUP)
.when()
.post("/")
.then()
.statusCode(400)
.body(containsString("<Code>InvalidCacheParameterGroupState</Code>"))
.body(containsString("One or more cache clusters are still members of this parameter group "
+ GROUP + ", so the group cannot be deleted."));
query("DescribeCacheParameterGroups")
.formParam("CacheParameterGroupName", GROUP)
.when()
.post("/")
.then()
.statusCode(200)
.body(containsString("<CacheParameterGroupName>" + GROUP + "</CacheParameterGroupName>"));
} finally {
query("DeleteReplicationGroup")
.formParam("ReplicationGroupId", replicationGroupId)
.when()
.post("/")
.then()
.statusCode(200);
}
}
@Test
@Order(5)
void deleteRemovesItAndTheSecondDeleteReportsItMissing() {
@@ -677,4 +677,41 @@ class ElastiCacheServiceTest {
assertThrows(AwsException.class, () -> svc.getReplicationGroup("g1"),
"the deleted group must not come back from the modify");
}
private static ElastiCacheService.CreateReplicationGroupRequest requestWithParameterGroup(
String groupId, String parameterGroupName) {
return new ElastiCacheService.CreateReplicationGroupRequest(groupId, "test",
AuthMode.NO_AUTH, null, "us-east-1", null, null, null,
parameterGroupName, null, null, null, null,
null, null, null, ReplicationGroupSettings.defaults(), Map.of());
}
@Test
void createReferencingAnUnknownParameterGroupIsRefusedBeforeProvisioning() {
AwsException ex = assertThrows(AwsException.class,
() -> service.createReplicationGroup(requestWithParameterGroup("grp", "absent-pg")));
assertEquals("CacheParameterGroupNotFound", ex.getErrorCode());
assertEquals(404, ex.getHttpStatus());
verify(containerManager, never()).start(anyString(), anyString());
verify(containerManager, never()).start(anyString(), anyString(), any());
assertThrows(AwsException.class, () -> service.getReplicationGroup("grp"));
}
@Test
void aParameterGroupStillReferencedByAReplicationGroupCannotBeDeleted() {
service.createCacheParameterGroup("custom-pg", "redis7", "in use", Map.of());
service.createReplicationGroup(requestWithParameterGroup("grp", "custom-pg"));
AwsException ex = assertThrows(AwsException.class,
() -> service.deleteCacheParameterGroup("custom-pg"));
assertEquals("InvalidCacheParameterGroupState", ex.getErrorCode());
assertEquals(400, ex.getHttpStatus());
assertTrue(service.findParameterGroup("custom-pg").isPresent(),
"a refused delete must leave the group in place");
service.deleteReplicationGroup("grp");
service.deleteCacheParameterGroup("custom-pg");
assertTrue(service.findParameterGroup("custom-pg").isEmpty());
}
}