Skip to content

armgit / floci

Public Imported from GitHub
0

Light, fluffy, and always free - The AWS Local Emulator alternative

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

Vũ Thế Huy173ff45b1291
diff --git a/src/main/java/io/github/hectorvent/floci/services/elasticache/ElastiCacheService.java b/src/main/java/io/github/hectorvent/floci/services/elasticache/ElastiCacheService.java
index 36d214471a..a6e103dd40 100644
--- a/src/main/java/io/github/hectorvent/floci/services/elasticache/ElastiCacheService.java
+++ b/src/main/java/io/github/hectorvent/floci/services/elasticache/ElastiCacheService.java
@@ -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);
}
}
diff --git a/src/main/java/io/github/hectorvent/floci/services/elasticache/proxy/ElastiCacheAuthProxy.java b/src/main/java/io/github/hectorvent/floci/services/elasticache/proxy/ElastiCacheAuthProxy.java
index cf15694e55..0ca14c4c94 100644
--- a/src/main/java/io/github/hectorvent/floci/services/elasticache/proxy/ElastiCacheAuthProxy.java
+++ b/src/main/java/io/github/hectorvent/floci/services/elasticache/proxy/ElastiCacheAuthProxy.java
@@ -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);
+ }
}
/**
diff --git a/src/test/java/io/github/hectorvent/floci/services/elasticache/ElastiCacheIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/elasticache/ElastiCacheIntegrationTest.java
index 0f37622dd9..ef7b2a24f7 100644
--- a/src/test/java/io/github/hectorvent/floci/services/elasticache/ElastiCacheIntegrationTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/elasticache/ElastiCacheIntegrationTest.java
@@ -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");
-
- // 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);
+ .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");
+ }
- // Clean up the cross-group
- given()
- .formParam("Action", "DeleteReplicationGroup")
- .formParam("ReplicationGroupId", CROSS_GROUP_ID)
- .header("Authorization", AUTH_HEADER)
- .when()
- .post("/")
- .then()
- .statusCode(200);
+ // 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");
+
+ // 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