fix(elasticache): resolve order-dependent crossGroupAuthIsRejected test failure (#1991) (#3074)

* fix(elasticache): fix order-dependent crossGroupAuthIsRejected failure and handle proxy socket closing cleanly

* fix(elasticache): address review feedback on socket cleanup logging and test isolation

* fix(elasticache): use JBoss Logger and verify cleanup status in integration test
This commit is contained in:
Vũ Thế Huy authored and GitHub committed 2026-09-05 14:04:43 -04:00
1 parent 37faaa8abe
commit 173ff45b12
3 files changed
+73 -31

No files matched your search

@@ -248,11 +248,13 @@ public class ElastiCacheService implements ResourceProvider {
: 1);
applyCommonAttributes(group, request, resolvedSettings);
proxyManager.startProxy(groupId, authMode, proxyPort,
handle.getHost(), handle.getPort(),
(username, password) -> validatePassword(groupId, username, password));
synchronized (lockFor("rg:" + groupId)) {
groups.put(groupId, group);
proxyManager.startProxy(groupId, authMode, proxyPort,
handle.getHost(), handle.getPort(),
(username, password) -> validatePassword(groupId, username, password));
}
groups.put(groupId, group);
LOG.infov("Replication group {0} created, endpoint={1}:{2}", groupId, endpointHost, String.valueOf(proxyPort));
return group;
} catch (RuntimeException e) {
@@ -615,6 +617,7 @@ public class ElastiCacheService implements ResourceProvider {
} catch (RuntimeException e) {
LOG.warnv("Error stopping container for replication group {0}: {1}", groupId, e.getMessage());
} finally {
groups.delete(groupId);
releaseProxyPort(proxyPort);
}
}
@@ -212,8 +212,19 @@ public class ElastiCacheAuthProxy {
out.flush();
}
private static void closeQuietly(Socket s) {
try { s.close(); } catch (IOException ignored) {}
private void closeQuietly(Socket s) {
try {
if (!s.isClosed()) {
try {
s.shutdownOutput();
} catch (IOException e) {
LOG.debugv(e, "Error shutting down socket output for group {0}", groupId);
}
s.close();
}
} catch (IOException e) {
LOG.debugv(e, "Error closing socket for group {0}", groupId);
}
}
/**
@@ -17,6 +17,8 @@ import java.net.Socket;
import java.net.SocketTimeoutException;
import java.nio.charset.StandardCharsets;
import org.jboss.logging.Logger;
import static io.restassured.RestAssured.given;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.hamcrest.Matchers.equalTo;
@@ -26,6 +28,8 @@ import static org.hamcrest.Matchers.notNullValue;
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
class ElastiCacheIntegrationTest {
private static final Logger LOG = Logger.getLogger(ElastiCacheIntegrationTest.class);
private static final String AUTH_HEADER =
"AWS4-HMAC-SHA256 Credential=test/20260412/us-east-1/elasticache/aws4_request";
private static final String GROUP_ID = "it-ec-group";
@@ -201,34 +205,58 @@ class ElastiCacheIntegrationTest {
@Test
@Order(10)
void crossGroupAuthIsRejected() throws Exception {
// Create a second group and verify the user (associated with GROUP_ID only) cannot auth
crossGroupPort = given()
.formParam("Action", "CreateReplicationGroup")
.formParam("ReplicationGroupId", CROSS_GROUP_ID)
.formParam("ReplicationGroupDescription", "Cross-group isolation test")
.formParam("AuthToken", CROSS_GROUP_AUTH_TOKEN)
// Ensure user exists if this test is run in isolation
try {
given()
.formParam("Action", "CreateUser")
.formParam("UserId", USER_ID)
.formParam("UserName", USER_NAME)
.formParam("AuthenticationMode.Type", "password")
.formParam("AuthenticationMode.Passwords.member.1", INITIAL_PASSWORD)
.formParam("AccessString", "on ~* +@all")
.header("Authorization", AUTH_HEADER)
.when()
.post("/")
.then()
.statusCode(200)
.extract()
.xmlPath()
.getInt("CreateReplicationGroupResponse.CreateReplicationGroupResult.ReplicationGroup.ConfigurationEndpoint.Port");
.post("/");
} catch (Exception e) {
// User already created when tests run in full class order; tolerated in isolation
LOG.debugv(e, "User creation tolerated during crossGroupAuthIsRejected isolation setup");
}
// User associated with GROUP_ID should be rejected on CROSS_GROUP_ID
String reply = sendCommand(crossGroupPort, respArray("AUTH", USER_NAME, INITIAL_PASSWORD));
assertEquals("-ERR invalid username-password pair or user is disabled.\r\n", reply);
// Create a second group and verify the user (associated with GROUP_ID only) cannot auth
try {
crossGroupPort = given()
.formParam("Action", "CreateReplicationGroup")
.formParam("ReplicationGroupId", CROSS_GROUP_ID)
.formParam("ReplicationGroupDescription", "Cross-group isolation test")
.formParam("AuthToken", CROSS_GROUP_AUTH_TOKEN)
.header("Authorization", AUTH_HEADER)
.when()
.post("/")
.then()
.statusCode(200)
.extract()
.xmlPath()
.getInt("CreateReplicationGroupResponse.CreateReplicationGroupResult.ReplicationGroup.ConfigurationEndpoint.Port");
// Clean up the cross-group
given()
.formParam("Action", "DeleteReplicationGroup")
.formParam("ReplicationGroupId", CROSS_GROUP_ID)
.header("Authorization", AUTH_HEADER)
.when()
.post("/")
.then()
.statusCode(200);
// User associated with GROUP_ID should be rejected on CROSS_GROUP_ID
String reply = sendCommand(crossGroupPort, respArray("AUTH", USER_NAME, INITIAL_PASSWORD));
assertEquals("-ERR invalid username-password pair or user is disabled.\r\n", reply);
} finally {
// Clean up the cross-group without masking test assertions if creation/auth failed
try {
int status = given()
.formParam("Action", "DeleteReplicationGroup")
.formParam("ReplicationGroupId", CROSS_GROUP_ID)
.header("Authorization", AUTH_HEADER)
.when()
.post("/")
.getStatusCode();
if (status != 200 && status != 404) {
LOG.warnv("Cross-group cleanup returned unexpected status {0}", status);
}
} catch (Exception e) {
LOG.debugv(e, "Cross-group cleanup failed during teardown");
}
}
}
@Test