mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
feat(cloudformation): provision ElastiCache replication groups (#8448)
CloudFormation now provisions AWS::ElastiCache::ReplicationGroup instead of completing the stack with no cache behind it. The group is created through the ElastiCache service, Ref returns its id, and Fn::GetAtt exposes the configuration endpoint for a cluster-mode group or the primary, reader and per-node read endpoints otherwise. A create-only change replaces the group, an explicit ReplicationGroupId that stays the same is refused, and an update that reuses the group warns only about the properties it ignores. Optional booleans in the ElastiCache, IoT authorizer, API Gateway API key and StackSet provisioners are read through ProvisionContext.resolveOptionalBoolean, which refuses a value that is neither true nor false instead of reading it as false. Closes #8447
This commit is contained in:
1 parent
3fa20af859
commit
9e05c07bfc
16 files changed
+512
-34
No files matched your search
@@ -17,9 +17,13 @@ import software.amazon.awssdk.services.elasticache.ElastiCacheClient;
|
||||
import software.amazon.awssdk.services.elasticache.model.CacheCluster;
|
||||
import software.amazon.awssdk.services.elasticache.model.CacheSubnetGroupNotFoundException;
|
||||
import software.amazon.awssdk.services.elasticache.model.ElastiCacheException;
|
||||
import software.amazon.awssdk.services.elasticache.model.Endpoint;
|
||||
import software.amazon.awssdk.services.elasticache.model.ReplicationGroup;
|
||||
import software.amazon.awssdk.services.elasticache.model.ReplicationGroupNotFoundException;
|
||||
import software.amazon.awssdk.services.elasticache.model.UserGroupNotFoundException;
|
||||
|
||||
import java.net.URI;
|
||||
import java.util.List;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
@@ -215,6 +219,25 @@ class ElastiCacheCfnIntegrationTest {
|
||||
CfnStackWaits.awaitStackDeleted(stack);
|
||||
}
|
||||
|
||||
@Test
|
||||
void userGroupCanListTheBuiltInDefaultUser() {
|
||||
String stack = "elasticache-cfn-builtin-member-it";
|
||||
cloudFormation(stack, "CreateStack", """
|
||||
{"Resources": {"UserGroup": {"Type": "AWS::ElastiCache::UserGroup",
|
||||
"Properties": {"UserGroupId": "cfn-it-builtin-members", "Engine": "redis",
|
||||
"UserIds": ["default"]}}}}
|
||||
""");
|
||||
describeStacks(stack, "CREATE_COMPLETE");
|
||||
|
||||
try (ElastiCacheClient client = client()) {
|
||||
assertEquals(List.of("default"), client.describeUserGroups(r -> r.userGroupId("cfn-it-builtin-members"))
|
||||
.userGroups().getFirst().userIds());
|
||||
}
|
||||
|
||||
cloudFormation(stack, "DeleteStack", null);
|
||||
CfnStackWaits.awaitStackDeleted(stack);
|
||||
}
|
||||
|
||||
@Test
|
||||
void cacheClusterExposesEndpoints() throws Exception {
|
||||
Assumptions.assumeTrue(DockerAvailability.isAvailable(), "Docker is required for the cache data plane");
|
||||
@@ -237,6 +260,68 @@ class ElastiCacheCfnIntegrationTest {
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
void replicationGroupWithBuiltInDefaultUserExposesEndpoints() throws Exception {
|
||||
Assumptions.assumeTrue(DockerAvailability.isAvailable(), "Docker is required for the cache data plane");
|
||||
String stack = "elasticache-cfn-replication-group-it";
|
||||
// the property shapes a Serverless Framework template sends: a numeric EngineVersion, string scalars
|
||||
cloudFormation(stack, "CreateStack", """
|
||||
{
|
||||
"Resources": {
|
||||
"Vpc": {"Type": "AWS::EC2::VPC", "Properties": {"CidrBlock": "10.74.0.0/16"}},
|
||||
"Subnet": {"Type": "AWS::EC2::Subnet",
|
||||
"Properties": {"VpcId": {"Ref": "Vpc"}, "CidrBlock": "10.74.0.0/24"}},
|
||||
"SecurityGroup": {"Type": "AWS::EC2::SecurityGroup",
|
||||
"Properties": {"GroupDescription": "cache", "VpcId": {"Ref": "Vpc"}}},
|
||||
"SubnetGroup": {"Type": "AWS::ElastiCache::SubnetGroup",
|
||||
"Properties": {"Description": "example", "SubnetIds": [{"Ref": "Subnet"}]}},
|
||||
"AppUser": {"Type": "AWS::ElastiCache::User",
|
||||
"Properties": {"UserId": "cfn-it-rg-app", "UserName": "cfn-it-rg-app", "Engine": "redis",
|
||||
"AccessString": "on ~* +@all",
|
||||
"AuthenticationMode": {"Type": "password",
|
||||
"Passwords": ["example-password-0123456789"]}}},
|
||||
"UserGroup": {"Type": "AWS::ElastiCache::UserGroup",
|
||||
"Properties": {"UserGroupId": "cfn-it-rg-users", "Engine": "redis",
|
||||
"UserIds": ["default", {"Ref": "AppUser"}]}},
|
||||
"Redis": {"Type": "AWS::ElastiCache::ReplicationGroup",
|
||||
"Properties": {"ReplicationGroupId": "cfn-it-rg", "ReplicationGroupDescription": "cache",
|
||||
"Engine": "redis", "EngineVersion": 7.1, "Port": 6379,
|
||||
"CacheNodeType": "cache.t3.micro", "NumCacheClusters": "1",
|
||||
"AutomaticFailoverEnabled": "false",
|
||||
"SecurityGroupIds": [{"Ref": "SecurityGroup"}],
|
||||
"CacheSubnetGroupName": {"Ref": "SubnetGroup"},
|
||||
"UserGroupIds": [{"Ref": "UserGroup"}],
|
||||
"TransitEncryptionEnabled": true}}
|
||||
},
|
||||
"Outputs": {
|
||||
"Ref": {"Value": {"Ref": "Redis"}},
|
||||
"Address": {"Value": {"Fn::GetAtt": ["Redis", "PrimaryEndPoint.Address"]}},
|
||||
"Port": {"Value": {"Fn::GetAtt": ["Redis", "PrimaryEndPoint.Port"]}},
|
||||
"ReadPorts": {"Value": {"Fn::Join": [",", {"Fn::GetAtt": ["Redis", "ReadEndPoint.Ports.List"]}]}}
|
||||
}
|
||||
}
|
||||
""");
|
||||
String stacks = describeStacks(stack, "CREATE_COMPLETE");
|
||||
|
||||
assertEquals("cfn-it-rg", outputValue(stacks, "Ref"));
|
||||
try (ElastiCacheClient client = client()) {
|
||||
ReplicationGroup group = client.describeReplicationGroups(r -> r.replicationGroupId("cfn-it-rg"))
|
||||
.replicationGroups().getFirst();
|
||||
Endpoint primary = group.nodeGroups().getFirst().primaryEndpoint();
|
||||
assertEquals(primary.address(), outputValue(stacks, "Address"));
|
||||
assertEquals(String.valueOf(primary.port()), outputValue(stacks, "Port"));
|
||||
assertEquals(String.valueOf(primary.port()), outputValue(stacks, "ReadPorts"));
|
||||
assertEquals(List.of("cfn-it-rg-users"), group.userGroupIds());
|
||||
assertTrue(group.transitEncryptionEnabled());
|
||||
|
||||
cloudFormation(stack, "DeleteStack", null);
|
||||
CfnStackWaits.awaitStackDeleted(stack);
|
||||
|
||||
assertThrows(ReplicationGroupNotFoundException.class,
|
||||
() -> client.describeReplicationGroups(r -> r.replicationGroupId("cfn-it-rg")));
|
||||
}
|
||||
}
|
||||
|
||||
private static void cloudFormation(String stack, String action, String templateBody) {
|
||||
RequestSpecification request = given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
|
||||
+146
@@ -15,9 +15,13 @@ import io.floci.aws.services.elasticache.model.CacheSubnetGroup;
|
||||
import io.floci.aws.services.elasticache.model.ElastiCacheUser;
|
||||
import io.floci.aws.services.elasticache.model.ElastiCacheUserGroup;
|
||||
import io.floci.aws.services.elasticache.model.Endpoint;
|
||||
import io.floci.aws.services.elasticache.model.ReplicationGroup;
|
||||
import io.floci.aws.services.elasticache.model.ReplicationGroupStatus;
|
||||
import io.floci.aws.testutil.LogCapture;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
|
||||
import java.text.MessageFormat;
|
||||
import java.time.Instant;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
@@ -105,6 +109,148 @@ class ElastiCacheCfnProvisionerTest {
|
||||
verify(memcached, never()).createCacheCluster(any());
|
||||
}
|
||||
|
||||
private ReplicationGroup replicationGroup(String id, int numCacheClusters) {
|
||||
ReplicationGroup group = new ReplicationGroup(id, "d", ReplicationGroupStatus.AVAILABLE, AuthMode.NO_AUTH,
|
||||
new Endpoint("localhost", 6420), Instant.now(), 6420);
|
||||
group.setNumCacheClusters(numCacheClusters);
|
||||
return group;
|
||||
}
|
||||
|
||||
@Test
|
||||
void replicationGroupTakesLooseScalarsAndExposesItsEndpoints() throws Exception {
|
||||
ReplicationGroup created = replicationGroup("app-redis", 2);
|
||||
when(cache.createReplicationGroup(any(ElastiCacheService.CreateReplicationGroupRequest.class)))
|
||||
.thenReturn(created);
|
||||
when(cache.memberCacheClusters(created)).thenReturn(List.of(
|
||||
new ElastiCacheService.MemberCacheCluster(created, "app-redis-001", 6420, true),
|
||||
new ElastiCacheService.MemberCacheCluster(created, "app-redis-002", 6420, false)));
|
||||
StackResource r = resource("AWS::ElastiCache::ReplicationGroup", "Redis");
|
||||
|
||||
// EngineVersion as a number, NumCacheClusters and AutomaticFailoverEnabled as strings
|
||||
provisioner.provision(r, props("""
|
||||
{"ReplicationGroupId":"app-redis","ReplicationGroupDescription":"d","Engine":"redis",
|
||||
"EngineVersion":7.1,"Port":6379,"CacheNodeType":"cache.t3.micro","NumCacheClusters":"2",
|
||||
"AutomaticFailoverEnabled":"false","SecurityGroupIds":["sg-1"],"CacheSubnetGroupName":"sng",
|
||||
"UserGroupIds":["users"],"TransitEncryptionEnabled":true}
|
||||
"""), ctx(null));
|
||||
|
||||
ArgumentCaptor<ElastiCacheService.CreateReplicationGroupRequest> request =
|
||||
ArgumentCaptor.forClass(ElastiCacheService.CreateReplicationGroupRequest.class);
|
||||
verify(cache).createReplicationGroup(request.capture());
|
||||
ElastiCacheService.CreateReplicationGroupRequest sent = request.getValue();
|
||||
assertEquals("app-redis", sent.replicationGroupId());
|
||||
assertEquals("7.1", sent.engineVersion());
|
||||
assertEquals(2, sent.numCacheClusters());
|
||||
assertEquals(false, sent.automaticFailoverEnabled());
|
||||
assertEquals(6379, sent.port());
|
||||
assertEquals(true, sent.settings().transitEncryptionEnabled());
|
||||
assertEquals(List.of("users"), sent.userGroupIds());
|
||||
assertEquals(List.of("sg-1"), sent.securityGroupIds());
|
||||
assertEquals("sng", sent.cacheSubnetGroupName());
|
||||
assertEquals(AuthMode.NO_AUTH, sent.authMode());
|
||||
|
||||
assertEquals("app-redis", r.getPhysicalId());
|
||||
assertEquals("localhost", r.getAttributes().get("PrimaryEndPoint.Address"));
|
||||
assertEquals("6420", r.getAttributes().get("PrimaryEndPoint.Port"));
|
||||
assertEquals("localhost", r.getAttributes().get("ReaderEndPoint.Address"));
|
||||
assertEquals("6420", r.getAttributes().get("ReaderEndPoint.Port"));
|
||||
assertEquals("localhost,localhost", r.getAttributes().get("ReadEndPoint.Addresses"));
|
||||
assertEquals("6420,6420", r.getAttributes().get("ReadEndPoint.Ports"));
|
||||
assertEquals(mapper.readTree("[\"6420\",\"6420\"]"), provisioner.attributeValue(
|
||||
"AWS::ElastiCache::ReplicationGroup", "ReadEndPoint.Ports.List",
|
||||
r.getAttributes().get("ReadEndPoint.Ports.List")));
|
||||
assertNull(r.getAttributes().get("ConfigurationEndPoint.Address"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void clusterModeReplicationGroupExposesOnlyTheConfigurationEndpoint() throws Exception {
|
||||
ReplicationGroup created = replicationGroup("sharded", 1);
|
||||
created.setClusterEnabled(true);
|
||||
when(cache.createReplicationGroup(any(ElastiCacheService.CreateReplicationGroupRequest.class)))
|
||||
.thenReturn(created);
|
||||
StackResource r = resource("AWS::ElastiCache::ReplicationGroup", "Redis");
|
||||
|
||||
provisioner.provision(r, props("""
|
||||
{"ReplicationGroupId":"sharded","ReplicationGroupDescription":"d","ClusterMode":"enabled",
|
||||
"NumNodeGroups":2}
|
||||
"""), ctx(null));
|
||||
|
||||
assertEquals("6420", r.getAttributes().get("ConfigurationEndPoint.Port"));
|
||||
assertNull(r.getAttributes().get("PrimaryEndPoint.Address"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void malformedReplicationGroupBooleanIsRejected() throws Exception {
|
||||
StackResource r = resource("AWS::ElastiCache::ReplicationGroup", "Redis");
|
||||
assertThrows(AwsException.class, () -> provisioner.provision(r, props("""
|
||||
{"ReplicationGroupDescription":"d","AutomaticFailoverEnabled":"yes"}
|
||||
"""), ctx(null)));
|
||||
verify(cache, never()).createReplicationGroup(any(ElastiCacheService.CreateReplicationGroupRequest.class));
|
||||
}
|
||||
|
||||
private List<String> ignoredChangeWarnings(StackResource r, String props, String priorProps) {
|
||||
return LogCapture.capture(ElastiCacheCfnProvisioner.class, () -> {
|
||||
try {
|
||||
provisioner.provision(r, props(props), ctx("app-redis", priorProps));
|
||||
} catch (Exception e) {
|
||||
throw new IllegalStateException(e);
|
||||
}
|
||||
}).stream()
|
||||
.map(record -> MessageFormat.format(record.getMessage(), record.getParameters()))
|
||||
.filter(message -> message.contains("in-place modification is not supported"))
|
||||
.toList();
|
||||
}
|
||||
|
||||
@Test
|
||||
void replicationGroupUpdateWithSameIdNamesTheChangesItIgnores() throws Exception {
|
||||
ReplicationGroup existing = replicationGroup("app-redis", 1);
|
||||
existing.setCacheNodeType("cache.t3.micro");
|
||||
when(cache.getReplicationGroup("app-redis")).thenReturn(existing);
|
||||
StackResource r = resource("AWS::ElastiCache::ReplicationGroup", "Redis");
|
||||
|
||||
List<String> warnings = ignoredChangeWarnings(r, """
|
||||
{"ReplicationGroupId":"app-redis","ReplicationGroupDescription":"changed","CacheNodeType":"cache.t3.small",
|
||||
"NumCacheClusters":"1"}
|
||||
""", """
|
||||
{"ReplicationGroupId":"app-redis","ReplicationGroupDescription":"d","CacheNodeType":"cache.t3.micro",
|
||||
"NumCacheClusters":"1"}
|
||||
""");
|
||||
|
||||
verify(cache, never()).createReplicationGroup(any(ElastiCacheService.CreateReplicationGroupRequest.class));
|
||||
assertEquals("app-redis", r.getPhysicalId());
|
||||
assertEquals("6420", r.getAttributes().get("PrimaryEndPoint.Port"));
|
||||
assertEquals(List.of("ElastiCache replication group app-redis: in-place modification is not supported, "
|
||||
+ "changes to ReplicationGroupDescription, CacheNodeType are ignored"), warnings);
|
||||
}
|
||||
|
||||
@Test
|
||||
void replicationGroupUpdateWithoutChangesWarnsNothing() throws Exception {
|
||||
ReplicationGroup existing = replicationGroup("app-redis", 1);
|
||||
existing.setCacheNodeType("cache.t3.micro");
|
||||
existing.setEngineVersion("7.1");
|
||||
existing.getUserGroupIds().add("users");
|
||||
when(cache.getReplicationGroup("app-redis")).thenReturn(existing);
|
||||
String template = """
|
||||
{"ReplicationGroupId":"app-redis","ReplicationGroupDescription":"d","CacheNodeType":"cache.t3.micro",
|
||||
"EngineVersion":7.1,"NumCacheClusters":"1","UserGroupIds":["Users"],"Tags":[{"Key":"a","Value":"b"}]}
|
||||
""";
|
||||
|
||||
List<String> warnings = ignoredChangeWarnings(resource("AWS::ElastiCache::ReplicationGroup", "Redis"),
|
||||
template, template);
|
||||
|
||||
assertEquals(List.of(), warnings);
|
||||
}
|
||||
|
||||
@Test
|
||||
void replicationGroupDeleteToleratesAGroupAlreadyGone() {
|
||||
doThrow(new AwsException("ReplicationGroupNotFoundFault", "gone", 404))
|
||||
.when(cache).deleteReplicationGroup("app-redis");
|
||||
|
||||
provisioner.delete("AWS::ElastiCache::ReplicationGroup", "app-redis", "us-east-1");
|
||||
|
||||
verify(cache).deleteReplicationGroup("app-redis");
|
||||
}
|
||||
|
||||
@Test
|
||||
void memcachedCacheClusterGoesToMemcachedServiceWithConfigurationEndpoint() throws Exception {
|
||||
CacheCluster created = new CacheCluster("mc", CacheClusterStatus.AVAILABLE, "memcached", "1.6",
|
||||
|
||||
+1
-1
@@ -271,7 +271,7 @@ class IotAuthorizerCfnProvisionerTest {
|
||||
AwsException error = assertThrows(AwsException.class, () -> update(resource, properties));
|
||||
|
||||
assertEquals("ValidationError", error.getErrorCode());
|
||||
assertEquals("AWS::IoT::Authorizer EnableCachingForHttp must be true or false, got ", error.getMessage());
|
||||
assertEquals("Value of property EnableCachingForHttp must be a boolean.", error.getMessage());
|
||||
verify(service, never()).updateAuthorizer(anyString(), any(), anyString());
|
||||
assertTrue(authorizers.get("auth").isEnableCachingForHttp());
|
||||
}
|
||||
|
||||
+30
@@ -291,6 +291,36 @@ class ProvisionContextTest {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void resolveOptionalBooleanReadsABooleanAndRefusesAnythingElse() {
|
||||
ProvisionContext ctx = context(null);
|
||||
JsonNode flag = props("{\"Ref\": \"Flag\"}");
|
||||
when(engine.resolve(flag)).thenReturn("TRUE");
|
||||
|
||||
assertEquals(true, ctx.resolveOptionalBoolean(props("{\"On\": true}"), "On"), "a boolean");
|
||||
assertEquals(false, ctx.resolveOptionalBoolean(props("{\"On\": \"false\"}"), "On"), "a boolean string");
|
||||
assertEquals(false, ctx.resolveOptionalBoolean(props("{\"On\": \" False \"}"), "On"),
|
||||
"any case, surrounding blanks trimmed");
|
||||
assertEquals(true, ctx.resolveOptionalBoolean(props("{\"On\": {\"Ref\": \"Flag\"}}"), "On"),
|
||||
"an intrinsic's result");
|
||||
assertNull(ctx.resolveOptionalBoolean(props("{}"), "On"), "absent");
|
||||
assertNull(ctx.resolveOptionalBoolean(props("{\"On\": null}"), "On"), "explicitly null");
|
||||
assertNull(ctx.resolveOptionalBoolean(null, "On"), "no properties at all");
|
||||
assertNull(ctx.resolveOptionalBoolean(props("{\"On\": {\"Ref\": \"AWS::NoValue\"}}"), "On"),
|
||||
"an intrinsic that resolves to empty, as AWS::NoValue does");
|
||||
JsonNode whitespace = props("{\"Ref\": \"Blank\"}");
|
||||
when(engine.resolve(whitespace)).thenReturn(" ");
|
||||
AwsException blank = assertThrows(AwsException.class,
|
||||
() -> ctx.resolveOptionalBoolean(props("{\"On\": {\"Ref\": \"Blank\"}}"), "On"));
|
||||
assertEquals("Value of property On must be a boolean.", blank.getMessage(), "a whitespace intrinsic result");
|
||||
for (String literal : List.of("\"\"", "\"yes\"", "1", "0")) {
|
||||
AwsException e = assertThrows(AwsException.class,
|
||||
() -> ctx.resolveOptionalBoolean(props("{\"On\": " + literal + "}"), "On"), literal);
|
||||
assertEquals("ValidationError", e.getErrorCode(), literal);
|
||||
assertEquals("Value of property On must be a boolean.", e.getMessage(), literal);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void stablePhysicalNamePrefersTheTemplatesName() {
|
||||
assertEquals("chosen",
|
||||
|
||||
Reference in new issue
Block a user