From b318794fa2687da057ce19b0dbac4dda0cebeaac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C5=A9=20Th=E1=BA=BF=20Huy?= <31652864+vuthehuyht@users.noreply.github.com> Date: Tue, 13 Oct 2026 08:47:33 +0700 Subject: [PATCH] fix(elasticache): use the AWS wire codes UserNotFound and UserAlreadyExists (#5452) * fix(elasticache): use the AWS wire codes UserNotFound and UserAlreadyExists * fix(elasticache): tolerate UserNotFound on CFN user delete and fix SDK test assertions The CloudFormation user delete still tolerated the old UserNotFoundFault code, so deleting a stack whose user was already gone would report DELETE_FAILED. The SDK compatibility test also asserted UserAlreadyExistsException on the duplicate replication group test instead of the duplicate user test. --- .../test/java/com/floci/test/ElastiCacheTest.java | 10 ++++------ .../provisioners/ElastiCacheCfnProvisioner.java | 2 +- .../services/elasticache/ElastiCacheService.java | 8 ++++---- .../ElastiCacheCfnProvisionerTest.java | 2 +- .../elasticache/ElastiCacheServiceTest.java | 14 ++++++++++++++ 5 files changed, 24 insertions(+), 12 deletions(-) diff --git a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/ElastiCacheTest.java b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/ElastiCacheTest.java index 995af422b2..57ca6f16ee 100644 --- a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/ElastiCacheTest.java +++ b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/ElastiCacheTest.java @@ -31,7 +31,9 @@ import software.amazon.awssdk.services.elasticache.model.ModifyReplicationGroupR import software.amazon.awssdk.services.elasticache.model.ModifyUserRequest; import software.amazon.awssdk.services.elasticache.model.ModifyUserResponse; import software.amazon.awssdk.services.elasticache.model.ReplicationGroup; +import software.amazon.awssdk.services.elasticache.model.UserAlreadyExistsException; import software.amazon.awssdk.services.elasticache.model.UserGroupNotFoundException; +import software.amazon.awssdk.services.elasticache.model.UserNotFoundException; import java.io.IOException; import java.io.InputStream; @@ -193,8 +195,6 @@ class ElastiCacheTest { void createDuplicateUserThrows409() { requireUser(); - // Floci returns generic error code, SDK maps to ElastiCacheException - // rather than UserAlreadyExistsException (pre-existing deviation) assertThatThrownBy(() -> elasticache.createUser(CreateUserRequest.builder() .userId(userId) .userName(userName) @@ -205,7 +205,7 @@ class ElastiCacheTest { .passwords("user-password-1") .build()) .build())) - .isInstanceOf(ElastiCacheException.class) + .isInstanceOf(UserAlreadyExistsException.class) .hasMessageContaining("already exists"); } @@ -276,12 +276,10 @@ class ElastiCacheTest { elasticache.deleteUser(DeleteUserRequest.builder().userId(userId).build()); - // Floci returns generic error code, SDK maps to ElastiCacheException - // rather than UserNotFoundException (pre-existing deviation) assertThatThrownBy(() -> elasticache.describeUsers(DescribeUsersRequest.builder() .userId(userId) .build())) - .isInstanceOf(ElastiCacheException.class) + .isInstanceOf(UserNotFoundException.class) .hasMessageContaining("not found"); userCreated = false; } diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/ElastiCacheCfnProvisioner.java b/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/ElastiCacheCfnProvisioner.java index bcea43b5c6..e312e2d5a2 100644 --- a/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/ElastiCacheCfnProvisioner.java +++ b/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/ElastiCacheCfnProvisioner.java @@ -455,7 +455,7 @@ public class ElastiCacheCfnProvisioner implements CfnResourceProvisioner { case USER_GROUP -> CfnDeletes.safeDelete("ElastiCache user group", physicalId, () -> elastiCacheService.deleteUserGroup(physicalId), "UserGroupNotFound"); case USER -> CfnDeletes.safeDelete("ElastiCache user", physicalId, - () -> elastiCacheService.deleteUser(physicalId), "UserNotFoundFault"); + () -> elastiCacheService.deleteUser(physicalId), "UserNotFound"); default -> { } } 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 d1fb908dfa..35a7c75bae 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 @@ -1806,7 +1806,7 @@ public class ElastiCacheService implements ResourceProvider { public ElastiCacheUser createUser(String userId, String userName, AuthMode authMode, List passwords, String accessString, String engine) { if (users.get(userId).isPresent()) { - throw new AwsException("UserAlreadyExistsFault", + throw new AwsException("UserAlreadyExists", "User " + userId + " already exists.", 400); } @@ -1827,7 +1827,7 @@ public class ElastiCacheService implements ResourceProvider { public ElastiCacheUser getUser(String userId) { return users.get(userId).orElseThrow(() -> - new AwsException("UserNotFoundFault", "User " + userId + " not found.", 404)); + new AwsException("UserNotFound", "User " + userId + " not found.", 404)); } public Collection listUsers(String filterUserId, String filterEngine) { @@ -1835,7 +1835,7 @@ public class ElastiCacheService implements ResourceProvider { if (filterUserId != null && !filterUserId.isBlank()) { return users.get(filterUserId) .map(List::of) - .orElseThrow(() -> new AwsException("UserNotFoundFault", + .orElseThrow(() -> new AwsException("UserNotFound", "User " + filterUserId + " not found.", 404)); } if (filterEngine != null && !filterEngine.isBlank()) { @@ -1890,7 +1890,7 @@ public class ElastiCacheService implements ResourceProvider { public void deleteUser(String userId) { if (users.get(userId).isEmpty()) { - throw new AwsException("UserNotFoundFault", "User " + userId + " not found.", 404); + throw new AwsException("UserNotFound", "User " + userId + " not found.", 404); } synchronized (userGroupLock) { users.delete(userId); diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/ElastiCacheCfnProvisionerTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/ElastiCacheCfnProvisionerTest.java index 6bc112f2fc..e3ff0bc0c9 100644 --- a/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/ElastiCacheCfnProvisionerTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/ElastiCacheCfnProvisionerTest.java @@ -679,7 +679,7 @@ class ElastiCacheCfnProvisionerTest { @Test void deleteToleratesAlreadyDeletedButPropagatesOtherFailures() { - doThrow(new AwsException("UserNotFoundFault", "gone", 404)) + doThrow(new AwsException("UserNotFound", "gone", 404)) .when(cache).deleteUser("gone"); provisioner.delete("AWS::ElastiCache::User", "gone", "us-east-1"); diff --git a/src/test/java/io/github/hectorvent/floci/services/elasticache/ElastiCacheServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/elasticache/ElastiCacheServiceTest.java index cc8688c806..c3c933090c 100644 --- a/src/test/java/io/github/hectorvent/floci/services/elasticache/ElastiCacheServiceTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/elasticache/ElastiCacheServiceTest.java @@ -464,6 +464,20 @@ class ElastiCacheServiceTest { assertEquals(Set.of("valkey-user-id"), service.getUserGroup("vk-group").getUserIds()); } + @Test + void userErrorsUseTheBotocoreWireCodesWithoutTheFaultSuffix() { + service.createUser("dupe-user", "dupe", AuthMode.PASSWORD, List.of("pw"), "on ~* +@all", "redis"); + + AwsException duplicate = assertThrows(AwsException.class, () -> service.createUser( + "dupe-user", "dupe", AuthMode.PASSWORD, List.of("pw"), "on ~* +@all", "redis")); + assertEquals("UserAlreadyExists", duplicate.getErrorCode()); + assertEquals(400, duplicate.getHttpStatus()); + assertEquals("UserNotFound", assertThrows(AwsException.class, () -> service.getUser("absent")).getErrorCode()); + assertEquals("UserNotFound", + assertThrows(AwsException.class, () -> service.listUsers("absent", null)).getErrorCode()); + assertEquals("UserNotFound", assertThrows(AwsException.class, () -> service.deleteUser("absent")).getErrorCode()); + } + @Test void singleArgAuthFallsBackToGroupAuthToken() { service.createReplicationGroup("grp", "test", AuthMode.PASSWORD, "group-token", "us-east-1");