diff --git a/src/main/java/io/floci/aws/services/cloudformation/provisioners/ElastiCacheCfnProvisioner.java b/src/main/java/io/floci/aws/services/cloudformation/provisioners/ElastiCacheCfnProvisioner.java index a26d20201b..73dd16d24d 100644 --- a/src/main/java/io/floci/aws/services/cloudformation/provisioners/ElastiCacheCfnProvisioner.java +++ b/src/main/java/io/floci/aws/services/cloudformation/provisioners/ElastiCacheCfnProvisioner.java @@ -4,6 +4,7 @@ import io.floci.aws.core.common.AwsException; import io.floci.aws.core.common.JacksonConfig; import io.floci.aws.core.common.JsonNodes; import io.floci.aws.services.cloudformation.model.StackResource; +import io.floci.aws.services.cloudformation.provisioners.ProvisionContext.NamedChange; import io.floci.aws.services.elasticache.ElastiCacheMemcachedService; import io.floci.aws.services.elasticache.ElastiCacheService; import io.floci.aws.services.elasticache.UserAuthentication; @@ -28,6 +29,7 @@ import java.util.List; import java.util.Locale; import java.util.Map; import java.util.Set; +import java.util.function.UnaryOperator; /** * Provisions AWS::ElastiCache::CacheCluster, ReplicationGroup, SubnetGroup, User and UserGroup. A cache cluster needs @@ -482,11 +484,13 @@ public class ElastiCacheCfnProvisioner implements CfnResourceProvisioner { throw new AwsException("ValidationException", "CacheSubnetGroupName exceeds maximum length of " + SUBNET_GROUP_NAME_MAX_LENGTH, 400); } - String id = ctx.stablePhysicalName(explicitName, r.getLogicalId(), SUBNET_GROUP_NAME_MAX_LENGTH, true); + NamedChange change = ctx.changes(SUBNET_GROUP, props, explicitName, r.getLogicalId(), + SUBNET_GROUP_NAME_MAX_LENGTH, true, UnaryOperator.identity()); + String id = change.name(); List subnetIds = ctx.resolveStringList(props, "SubnetIds"); CacheSubnetGroup group; - if (ctx.reusesPriorEntity(id)) { + if (ctx.isUpdate() && !change.replaces()) { List existing = elastiCacheService.describeCacheSubnetGroups(id); if (!existing.isEmpty()) { CacheSubnetGroup prior = existing.getFirst(); @@ -524,12 +528,16 @@ public class ElastiCacheCfnProvisioner implements CfnResourceProvisioner { LOG.warnv("ElastiCache user {0}: Tags are ignored, the emulator does not tag users", userId); } + // UserName is create-only; the engine refuses its change under the same UserId. + NamedChange change = ctx.changes(USER, props, userId, r.getLogicalId(), 0, false, + UnaryOperator.identity()); + userId = change.name(); ElastiCacheUser user; - if (ctx.reusesPriorEntity(userId)) { + if (ctx.isUpdate() && !change.replaces()) { ElastiCacheUser existing = elastiCacheService.getUser(userId); + // The stored user is checked too, for an update where the engine had no prior values to compare. if (!userName.equals(existing.getUserName())) { - throw new AwsException("InvalidParameterValue", - "UserName of user " + userId + " is create-only; use a new UserId to change it", 400); + throw CfnExceptions.customNamedReplacement(userId); } UserSnapshot snapshot = new UserSnapshot( existing.getUserId(), @@ -572,8 +580,12 @@ public class ElastiCacheCfnProvisioner implements CfnResourceProvisioner { LOG.warnv("ElastiCache user group {0}: Tags are ignored, the emulator does not tag user groups", groupId); } + // The service stores the id in lowercase, so the prior physical id only matches in that form. + NamedChange change = ctx.changes(USER_GROUP, props, groupId, r.getLogicalId(), 0, true, + s -> s.toLowerCase(Locale.ROOT)); + groupId = change.name(); ElastiCacheUserGroup group; - if (ctx.reusesPriorEntity(groupId)) { + if (ctx.isUpdate() && !change.replaces()) { ElastiCacheUserGroup existing = elastiCacheService.getUserGroup(groupId); UserGroupSnapshot snapshot = new UserGroupSnapshot( existing.getUserGroupId(), diff --git a/src/test/java/io/floci/aws/services/cloudformation/CfnReplacementIntegrationTest.java b/src/test/java/io/floci/aws/services/cloudformation/CfnReplacementIntegrationTest.java index a5457cfbf8..2fb535f17b 100644 --- a/src/test/java/io/floci/aws/services/cloudformation/CfnReplacementIntegrationTest.java +++ b/src/test/java/io/floci/aws/services/cloudformation/CfnReplacementIntegrationTest.java @@ -178,6 +178,12 @@ class CfnReplacementIntegrationTest { + " \"HostedZoneConfig\": {\"Comment\": \"second\"}", "False", false, null), SUBNET_GROUP_REPLACED, + createOnlyRow("ElastiCache subnet group CacheSubnetGroupName removed", + json(vpcAndSubnet("10.74")).replaceAll(",$", ""), "", "AWS::ElastiCache::SubnetGroup", + "\"CacheSubnetGroupName\": \"repl-sng-named\", \"Description\": \"example\"," + + " \"SubnetIds\": [{\"Ref\": \"Subnet\"}]", + "\"Description\": \"example\", \"SubnetIds\": [{\"Ref\": \"Subnet\"}]", + "True", false, null), createOnlyRow("SQS queue explicit QueueName removed", "", "", SQS_TYPE, "\"QueueName\": \"repl-q-removed\"", "\"VisibilityTimeout\": 30", "True", false, null), new Row("SQS queue QueueName dropped by Fn::If choosing AWS::NoValue, beside an updatable change", diff --git a/src/test/java/io/floci/aws/services/cloudformation/provisioners/ElastiCacheCfnProvisionerTest.java b/src/test/java/io/floci/aws/services/cloudformation/provisioners/ElastiCacheCfnProvisionerTest.java index fe4baeff20..5c3b005e56 100644 --- a/src/test/java/io/floci/aws/services/cloudformation/provisioners/ElastiCacheCfnProvisionerTest.java +++ b/src/test/java/io/floci/aws/services/cloudformation/provisioners/ElastiCacheCfnProvisionerTest.java @@ -639,15 +639,21 @@ class ElastiCacheCfnProvisionerTest { verify(cache).setUserGroupMembers(eq("team"), eq(List.of("d", "new")), eq("redis")); } + /** UserName is "Update requires: Replacement", and a custom-named replacement under the same UserId is refused. */ @Test - void userNameChangeIsRejectedBecauseItIsCreateOnly() throws Exception { - ElastiCacheUser existing = new ElastiCacheUser("app", "old-name", AuthMode.NO_AUTH, List.of(), "on ~* +@all", - "redis", "active", Instant.now()); - when(cache.getUser("app")).thenReturn(existing); + void userNameChangeUnderTheSameUserIdIsRefusedAsACustomNamedReplacement() throws Exception { + when(cache.getUser("app")).thenReturn(new ElastiCacheUser("app", "old-name", AuthMode.NO_AUTH, List.of(), + "on ~* +@all", "redis", "active", Instant.now())); StackResource r = resource("AWS::ElastiCache::User", "AppUser"); + ObjectNode renamed = props("{\"UserId\":\"app\",\"UserName\":\"new-name\",\"Engine\":\"redis\"}"); + ProvisionContext update = ctx("app", "{\"UserId\":\"app\",\"UserName\":\"old-name\",\"Engine\":\"redis\"}"); - assertThrows(AwsException.class, () -> provisioner.provision(r, - props("{\"UserId\":\"app\",\"UserName\":\"new-name\",\"Engine\":\"redis\"}"), ctx("app"))); + for (ProvisionContext context : List.of(update, ctx("app"))) { + AwsException e = assertThrows(AwsException.class, () -> provisioner.provision(r, renamed, context)); + + assertEquals("ValidationError", e.getErrorCode()); + assertTrue(e.getMessage().contains("custom-named"), e.getMessage()); + } verify(cache, never()).modifyUser(any(), any(), any(), any(), any(), any()); } diff --git a/src/test/resources/cloudformation/change-exempt.tsv b/src/test/resources/cloudformation/change-exempt.tsv index 052ebcd9c7..b1f88348f5 100644 --- a/src/test/resources/cloudformation/change-exempt.tsv +++ b/src/test/resources/cloudformation/change-exempt.tsv @@ -81,9 +81,6 @@ AWS::EFS::AccessPoint update not yet on the engine's changes (#6606) AWS::EFS::FileSystem update not yet on the engine's changes (#6606) AWS::EFS::MountTarget update not yet on the engine's changes (#6606) AWS::ElastiCache::ServerlessCache prior entity found by reusesPriorEntity; moves to the engine's decision with the create-only work -AWS::ElastiCache::SubnetGroup prior entity found by reusesPriorEntity; moves to the engine's decision with the create-only work -AWS::ElastiCache::User prior entity found by reusesPriorEntity; moves to the engine's decision with the create-only work -AWS::ElastiCache::UserGroup prior entity found by reusesPriorEntity; moves to the engine's decision with the create-only work AWS::ElasticLoadBalancingV2::Listener update not yet on the engine's changes (#6606) AWS::ElasticLoadBalancingV2::ListenerCertificate update not yet on the engine's changes (#6606) AWS::ElasticLoadBalancingV2::ListenerRule update not yet on the engine's changes (#6606)