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.
This commit is contained in:
Vũ Thế Huy authored and GitHub committed 2026-10-12 21:47:33 -04:00
1 parent f50437efba
commit b318794fa2
5 files changed
+24 -12

No files matched your search

@@ -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;
}
@@ -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 -> {
}
}
@@ -1806,7 +1806,7 @@ public class ElastiCacheService implements ResourceProvider {
public ElastiCacheUser createUser(String userId, String userName, AuthMode authMode,
List<String> 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<ElastiCacheUser> 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);
@@ -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");
@@ -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");