mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
Add RDS provisioning support (#1253)
* feat(rds): add subnet groups and orderable options * feat(rds): support subnet group modification * feat(rds): return managed master user secrets * fix(rds): preserve instance tags * fix(rds): honor requested engine versions * fix(rds): return routable proxy endpoints * fix(rds): accept PostgreSQL SSL negotiation * fix(rds): initialize PostgreSQL IAM role * fix(rds): include PostgreSQL 18 orderable option * fix(rds): address provisioning support review feedback
This commit is contained in:
1 parent
59a2659e41
commit
22a50b3c80
13 files changed
+1329
-58
No files matched your search
@@ -7,6 +7,7 @@ import io.github.hectorvent.floci.services.rds.model.DbClusterParameterGroup;
|
||||
import io.github.hectorvent.floci.services.rds.model.DbInstance;
|
||||
import io.github.hectorvent.floci.services.rds.model.DbInstanceStatus;
|
||||
import io.github.hectorvent.floci.services.rds.model.DbParameterGroup;
|
||||
import io.github.hectorvent.floci.services.rds.model.DbSubnetGroup;
|
||||
import jakarta.ws.rs.core.MultivaluedHashMap;
|
||||
import jakarta.ws.rs.core.MultivaluedMap;
|
||||
import jakarta.ws.rs.core.Response;
|
||||
@@ -129,6 +130,115 @@ class RdsQueryHandlerTest {
|
||||
verify(service).listDbInstances(null);
|
||||
}
|
||||
|
||||
@Test
|
||||
void describeDbInstances_usesStoredDbSubnetGroup() {
|
||||
DbInstance instance = makeInstance("mydb");
|
||||
instance.setDbSubnetGroupName("sample-db-subnets");
|
||||
when(service.listDbInstances(null)).thenReturn(List.of(instance));
|
||||
when(service.getDbSubnetGroup("sample-db-subnets")).thenReturn(new DbSubnetGroup(
|
||||
"sample-db-subnets", "test subnets", "vpc-123", List.of("subnet-aaa", "subnet-bbb")));
|
||||
|
||||
Response response = handler.handle("DescribeDBInstances", params());
|
||||
|
||||
String body = (String) response.getEntity();
|
||||
assertTrue(body.contains("<DBSubnetGroupName>sample-db-subnets</DBSubnetGroupName>"));
|
||||
assertTrue(body.contains("<SubnetIdentifier>subnet-aaa</SubnetIdentifier>"));
|
||||
assertTrue(body.contains("<SubnetIdentifier>subnet-bbb</SubnetIdentifier>"));
|
||||
assertFalse(body.contains("<SubnetIdentifier>subnet-00000000</SubnetIdentifier>"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void describeDbInstances_includesTagList() {
|
||||
DbInstance instance = makeInstance("mydb");
|
||||
instance.setTags(java.util.Map.of("example:ClusterId", "cluster-a", "Name", "mydb"));
|
||||
when(service.listDbInstances(null)).thenReturn(List.of(instance));
|
||||
|
||||
Response response = handler.handle("DescribeDBInstances", params());
|
||||
|
||||
String body = (String) response.getEntity();
|
||||
assertTrue(body.contains("<TagList>"));
|
||||
assertTrue(body.contains("<Key>example:ClusterId</Key>"));
|
||||
assertTrue(body.contains("<Value>cluster-a</Value>"));
|
||||
assertTrue(body.contains("<Key>Name</Key>"));
|
||||
assertTrue(body.contains("<Value>mydb</Value>"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void createDbInstance_passesCreateTagsToService() {
|
||||
DbInstance instance = makeInstance("mydb");
|
||||
when(service.createDbInstance(eq("mydb"), eq("postgres"), eq("16.3"),
|
||||
eq(null), eq(null), eq(null), eq("db.t3.micro"),
|
||||
eq(20), eq(false), eq(null), eq(null), eq(null), eq(false), eq(null),
|
||||
eq(java.util.Map.of("example:ClusterId", "cluster-a", "Name", "mydb"))))
|
||||
.thenReturn(instance);
|
||||
|
||||
MultivaluedMap<String, String> p = params();
|
||||
p.add("DBInstanceIdentifier", "mydb");
|
||||
p.add("Engine", "postgres");
|
||||
p.add("Tags.member.1.Key", "example:ClusterId");
|
||||
p.add("Tags.member.1.Value", "cluster-a");
|
||||
p.add("Tags.member.2.Key", "Name");
|
||||
p.add("Tags.member.2.Value", "mydb");
|
||||
handler.handle("CreateDBInstance", p);
|
||||
|
||||
verify(service).createDbInstance("mydb", "postgres", "16.3",
|
||||
null, null, null, "db.t3.micro", 20, false, null, null, null, false, null,
|
||||
java.util.Map.of("example:ClusterId", "cluster-a", "Name", "mydb"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void listTagsForResource_returnsStoredTags() {
|
||||
when(service.listTagsForResource("arn:aws:rds:us-east-1:000000000000:db:mydb"))
|
||||
.thenReturn(java.util.Map.of("Name", "mydb"));
|
||||
|
||||
MultivaluedMap<String, String> p = params();
|
||||
p.add("ResourceName", "arn:aws:rds:us-east-1:000000000000:db:mydb");
|
||||
Response response = handler.handle("ListTagsForResource", p);
|
||||
|
||||
String body = (String) response.getEntity();
|
||||
assertTrue(body.contains("<TagList>"));
|
||||
assertTrue(body.contains("<Key>Name</Key>"));
|
||||
assertTrue(body.contains("<Value>mydb</Value>"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void addAndRemoveTagsForResource_passThrough() {
|
||||
MultivaluedMap<String, String> add = params();
|
||||
add.add("ResourceName", "arn:aws:rds:us-east-1:000000000000:db:mydb");
|
||||
add.add("Tags.member.1.Key", "Name");
|
||||
add.add("Tags.member.1.Value", "mydb");
|
||||
handler.handle("AddTagsToResource", add);
|
||||
|
||||
verify(service).addTagsToResource("arn:aws:rds:us-east-1:000000000000:db:mydb", java.util.Map.of("Name", "mydb"));
|
||||
|
||||
MultivaluedMap<String, String> remove = params();
|
||||
remove.add("ResourceName", "arn:aws:rds:us-east-1:000000000000:db:mydb");
|
||||
remove.add("TagKeys.member.1", "Name");
|
||||
handler.handle("RemoveTagsFromResource", remove);
|
||||
|
||||
verify(service).removeTagsFromResource("arn:aws:rds:us-east-1:000000000000:db:mydb", List.of("Name"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void describeOrderableDbInstanceOptions_usesServiceCatalog() {
|
||||
when(service.describeOrderableDbInstanceOptions("postgres", "16.3", "db.t4g.medium"))
|
||||
.thenReturn(List.of(java.util.Map.of(
|
||||
"engine", "postgres",
|
||||
"engineVersion", "16.3",
|
||||
"dbInstanceClass", "db.t4g.medium")));
|
||||
|
||||
MultivaluedMap<String, String> p = params();
|
||||
p.add("Engine", "postgres");
|
||||
p.add("EngineVersion", "16.3");
|
||||
p.add("DBInstanceClass", "db.t4g.medium");
|
||||
Response response = handler.handle("DescribeOrderableDBInstanceOptions", p);
|
||||
|
||||
String body = (String) response.getEntity();
|
||||
assertEquals(200, response.getStatus());
|
||||
assertTrue(body.contains("<OrderableDBInstanceOption>"));
|
||||
assertTrue(body.contains("<DBInstanceClass>db.t4g.medium</DBInstanceClass>"));
|
||||
}
|
||||
|
||||
// ──────────────────────────── DBParameterGroups XML tag ──────────────────────
|
||||
|
||||
@Test
|
||||
@@ -148,7 +258,7 @@ class RdsQueryHandlerTest {
|
||||
DbInstance instance = makeInstance("mydb");
|
||||
when(service.createDbInstance(eq("mydb"), eq("postgres"), eq("16.3"),
|
||||
eq("admin"), eq("secret"), eq("dbname"), eq("db.t3.micro"),
|
||||
eq(20), eq(false), eq(null), eq(null)))
|
||||
eq(20), eq(false), eq(null), eq(null), eq(null), eq(false), eq(null), eq(java.util.Map.of())))
|
||||
.thenReturn(instance);
|
||||
|
||||
MultivaluedMap<String, String> p = params();
|
||||
@@ -161,7 +271,36 @@ class RdsQueryHandlerTest {
|
||||
handler.handle("CreateDBInstance", p);
|
||||
|
||||
verify(service).createDbInstance("mydb", "postgres", "16.3",
|
||||
"admin", "secret", "dbname", "db.t3.micro", 20, false, null, null);
|
||||
"admin", "secret", "dbname", "db.t3.micro", 20, false, null, null, null, false, null, java.util.Map.of());
|
||||
}
|
||||
|
||||
@Test
|
||||
void createDbInstancePassesManagedMasterUserSecretOptions() {
|
||||
DbInstance instance = makeInstance("mydb");
|
||||
instance.setMasterUserSecretArn("arn:aws:secretsmanager:us-east-1:000000000000:secret:rds!db-123456");
|
||||
instance.setMasterUserSecretStatus("active");
|
||||
instance.setMasterUserSecretKmsKeyId("kms-key-1");
|
||||
when(service.createDbInstance(eq("mydb"), eq("postgres"), eq("16.3"),
|
||||
eq("admin"), eq(null), eq("dbname"), eq("db.t3.micro"),
|
||||
eq(20), eq(false), eq(null), eq(null), eq(null), eq(true), eq("kms-key-1"), eq(java.util.Map.of())))
|
||||
.thenReturn(instance);
|
||||
|
||||
MultivaluedMap<String, String> p = params();
|
||||
p.add("DBInstanceIdentifier", "mydb");
|
||||
p.add("Engine", "postgres");
|
||||
p.add("MasterUsername", "admin");
|
||||
p.add("DBName", "dbname");
|
||||
p.add("ManageMasterUserPassword", "true");
|
||||
p.add("MasterUserSecretKmsKeyId", "kms-key-1");
|
||||
Response response = handler.handle("CreateDBInstance", p);
|
||||
|
||||
String body = (String) response.getEntity();
|
||||
assertTrue(body.contains("<MasterUserSecret>"));
|
||||
assertTrue(body.contains("<SecretArn>arn:aws:secretsmanager:us-east-1:000000000000:secret:rds!db-123456</SecretArn>"));
|
||||
assertTrue(body.contains("<SecretStatus>active</SecretStatus>"));
|
||||
assertTrue(body.contains("<KmsKeyId>kms-key-1</KmsKeyId>"));
|
||||
verify(service).createDbInstance("mydb", "postgres", "16.3",
|
||||
"admin", null, "dbname", "db.t3.micro", 20, false, null, null, null, true, "kms-key-1", java.util.Map.of());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -171,7 +310,7 @@ class RdsQueryHandlerTest {
|
||||
// AwsException wrapping into a 400 query error.
|
||||
when(service.createDbInstance(eq("mydb"), eq("oracle"), eq("1.0"),
|
||||
eq(null), eq(null), eq(null), eq("db.t3.micro"),
|
||||
eq(20), eq(false), eq(null), eq(null)))
|
||||
eq(20), eq(false), eq(null), eq(null), eq(null), eq(false), eq(null), eq(java.util.Map.of())))
|
||||
.thenThrow(new AwsException("InvalidParameterValue",
|
||||
"Unsupported engine: oracle. Supported: postgres, mysql, mariadb.", 400));
|
||||
|
||||
@@ -184,6 +323,51 @@ class RdsQueryHandlerTest {
|
||||
assertTrue(((String) response.getEntity()).contains("InvalidParameterValue"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void createDbSubnetGroup_passesSubnetMembersToService() {
|
||||
when(service.createDbSubnetGroup("sample-db-subnets", "test", List.of("subnet-aaa", "subnet-bbb")))
|
||||
.thenReturn(new DbSubnetGroup(
|
||||
"sample-db-subnets", "test", "vpc-123", List.of("subnet-aaa", "subnet-bbb")));
|
||||
|
||||
MultivaluedMap<String, String> p = params();
|
||||
p.add("DBSubnetGroupName", "sample-db-subnets");
|
||||
p.add("DBSubnetGroupDescription", "test");
|
||||
p.add("SubnetIds.SubnetIdentifier.1", "subnet-aaa");
|
||||
p.add("SubnetIds.SubnetIdentifier.2", "subnet-bbb");
|
||||
Response response = handler.handle("CreateDBSubnetGroup", p);
|
||||
|
||||
verify(service).createDbSubnetGroup("sample-db-subnets", "test", List.of("subnet-aaa", "subnet-bbb"));
|
||||
String body = (String) response.getEntity();
|
||||
assertEquals(200, response.getStatus());
|
||||
assertTrue(body.contains("<DBSubnetGroupName>sample-db-subnets</DBSubnetGroupName>"));
|
||||
assertTrue(body.contains("<Subnets><Subnet>"));
|
||||
assertFalse(body.contains("<Subnets><member>"));
|
||||
assertTrue(body.contains("<SubnetIdentifier>subnet-aaa</SubnetIdentifier>"));
|
||||
assertTrue(body.contains("<SubnetIdentifier>subnet-bbb</SubnetIdentifier>"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void modifyDbSubnetGroup_passesSubnetMembersToService() {
|
||||
when(service.modifyDbSubnetGroup("sample-db-subnets", List.of("subnet-new-a", "subnet-new-b")))
|
||||
.thenReturn(new DbSubnetGroup(
|
||||
"sample-db-subnets", "test", "vpc-123", List.of("subnet-new-a", "subnet-new-b")));
|
||||
|
||||
MultivaluedMap<String, String> p = params();
|
||||
p.add("DBSubnetGroupName", "sample-db-subnets");
|
||||
p.add("SubnetIds.SubnetIdentifier.1", "subnet-new-a");
|
||||
p.add("SubnetIds.SubnetIdentifier.2", "subnet-new-b");
|
||||
Response response = handler.handle("ModifyDBSubnetGroup", p);
|
||||
|
||||
verify(service).modifyDbSubnetGroup("sample-db-subnets", List.of("subnet-new-a", "subnet-new-b"));
|
||||
String body = (String) response.getEntity();
|
||||
assertEquals(200, response.getStatus());
|
||||
assertTrue(body.contains("<DBSubnetGroupName>sample-db-subnets</DBSubnetGroupName>"));
|
||||
assertTrue(body.contains("<Subnets><Subnet>"));
|
||||
assertFalse(body.contains("<Subnets><member>"));
|
||||
assertTrue(body.contains("<SubnetIdentifier>subnet-new-a</SubnetIdentifier>"));
|
||||
assertTrue(body.contains("<SubnetIdentifier>subnet-new-b</SubnetIdentifier>"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void modifyDbParameterGroup_ignoresParametersWithoutValue() {
|
||||
DbParameterGroup group = new DbParameterGroup("pg1", "postgres15", "test group");
|
||||
|
||||
@@ -3,6 +3,7 @@ package io.github.hectorvent.floci.services.rds;
|
||||
import io.github.hectorvent.floci.config.EmulatorConfig;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.common.RegionResolver;
|
||||
import io.github.hectorvent.floci.core.common.docker.DockerHostResolver;
|
||||
import io.github.hectorvent.floci.core.storage.InMemoryStorage;
|
||||
import io.github.hectorvent.floci.core.storage.StorageBackend;
|
||||
import io.github.hectorvent.floci.services.rds.model.DatabaseEngine;
|
||||
@@ -12,16 +13,23 @@ import io.github.hectorvent.floci.services.rds.container.RdsContainerHandle;
|
||||
import io.github.hectorvent.floci.services.rds.container.RdsContainerManager;
|
||||
import io.github.hectorvent.floci.services.rds.model.DbInstance;
|
||||
import io.github.hectorvent.floci.services.rds.model.DbParameterGroup;
|
||||
import io.github.hectorvent.floci.services.rds.model.DbSubnetGroup;
|
||||
import io.github.hectorvent.floci.services.rds.proxy.RdsProxyManager;
|
||||
import io.github.hectorvent.floci.services.secretsmanager.SecretsManagerService;
|
||||
import io.github.hectorvent.floci.services.secretsmanager.model.Secret;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyBoolean;
|
||||
import static org.mockito.ArgumentMatchers.anyInt;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@@ -46,6 +54,9 @@ class RdsServiceTest {
|
||||
when(servicesConfig.rds()).thenReturn(rdsConfig);
|
||||
when(rdsConfig.proxyBasePort()).thenReturn(7000);
|
||||
when(rdsConfig.proxyMaxPort()).thenReturn(7099);
|
||||
when(rdsConfig.defaultPostgresImage()).thenReturn("postgres:16-alpine");
|
||||
when(rdsConfig.defaultMysqlImage()).thenReturn("mysql:8.0");
|
||||
when(rdsConfig.defaultMariadbImage()).thenReturn("mariadb:11");
|
||||
|
||||
rdsService = newService(containerManager, proxyManager,
|
||||
new InMemoryStorage<>(), new InMemoryStorage<>(),
|
||||
@@ -59,7 +70,7 @@ class RdsServiceTest {
|
||||
void createDbInstanceGeneratesMissingFields() {
|
||||
DbInstance instance = rdsService.createDbInstance("mydb", "postgres", "13",
|
||||
"admin", "password", "dbname", "db.t3.micro",
|
||||
20, false, null, null);
|
||||
20, false, null, null, null);
|
||||
|
||||
assertEquals("mydb", instance.getDbInstanceIdentifier());
|
||||
assertNotNull(instance.getDbiResourceId());
|
||||
@@ -67,11 +78,98 @@ class RdsServiceTest {
|
||||
assertEquals("arn:aws:rds:us-east-1:123456789012:db:mydb", instance.getDbInstanceArn());
|
||||
}
|
||||
|
||||
@Test
|
||||
void postgresImageUsesRequestedEngineVersionAndDefaultFlavor() {
|
||||
assertEquals("postgres:18.1-alpine",
|
||||
RdsService.imageForRequestedVersion("postgres:16-alpine", "18.1"));
|
||||
assertEquals("example.com/library/postgres:18.1-alpine",
|
||||
RdsService.imageForRequestedVersion("example.com/library/postgres:16-alpine", "18.1"));
|
||||
assertEquals("postgres:18.1",
|
||||
RdsService.imageForRequestedVersion("postgres", "18.1"));
|
||||
assertEquals("postgres:18.1-alpine",
|
||||
RdsService.imageForRequestedVersion("postgres:16-alpine", "18.1-alpine"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void createDbInstanceStartsContainerWithRequestedEngineVersionImage() {
|
||||
rdsService.createDbInstance("mydb", "postgres", "18.1",
|
||||
"admin", "password", "dbname", "db.t3.micro",
|
||||
20, false, null, null, null);
|
||||
|
||||
verify(containerManager).start(eq("mydb"), any(), eq(DatabaseEngine.POSTGRES),
|
||||
eq("postgres:18.1-alpine"), eq("admin"), eq("password"), eq("dbname"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void dbInstanceTagsRoundTripAndMutateByArn() {
|
||||
DbInstance instance = rdsService.createDbInstance("mydb", "postgres", "13",
|
||||
"admin", "password", "dbname", "db.t3.micro",
|
||||
20, false, null, null, null, false, null,
|
||||
java.util.Map.of("example:ClusterId", "cluster-a"));
|
||||
|
||||
assertEquals(java.util.Map.of("example:ClusterId", "cluster-a"),
|
||||
rdsService.listTagsForResource(instance.getDbInstanceArn()));
|
||||
|
||||
rdsService.addTagsToResource(instance.getDbInstanceArn(), java.util.Map.of("Name", "mydb"));
|
||||
assertEquals(java.util.Map.of("example:ClusterId", "cluster-a", "Name", "mydb"),
|
||||
rdsService.listTagsForResource(instance.getDbInstanceArn()));
|
||||
|
||||
rdsService.removeTagsFromResource(instance.getDbInstanceArn(), java.util.List.of("Name"));
|
||||
assertEquals(java.util.Map.of("example:ClusterId", "cluster-a"),
|
||||
rdsService.listTagsForResource(instance.getDbInstanceArn()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void dbInstanceEndpointUsesResolvedProxyHost() {
|
||||
DockerHostResolver dockerHostResolver = mock(DockerHostResolver.class);
|
||||
when(dockerHostResolver.resolve()).thenReturn("floci.local");
|
||||
RdsService service = new RdsService(containerManager, proxyManager, regionResolver, config,
|
||||
new InMemoryStorage<>(), new InMemoryStorage<>(), new InMemoryStorage<>(),
|
||||
new InMemoryStorage<>(), new InMemoryStorage<>(), null, dockerHostResolver);
|
||||
|
||||
DbInstance instance = service.createDbInstance("mydb", "postgres", "13",
|
||||
"admin", "password", "dbname", "db.t3.micro",
|
||||
20, false, null, null, null);
|
||||
|
||||
assertEquals("floci.local", instance.getEndpoint().address());
|
||||
}
|
||||
|
||||
@Test
|
||||
void createDbInstanceWithManagedMasterPasswordCreatesSecret() {
|
||||
SecretsManagerService secretsManager = mock(SecretsManagerService.class);
|
||||
Secret secret = new Secret();
|
||||
secret.setArn("arn:aws:secretsmanager:us-east-1:123456789012:secret:rds!db-secret");
|
||||
when(secretsManager.createSecret(any(), any(), eq(null), any(), eq("kms-key-1"), eq(null), eq("us-east-1")))
|
||||
.thenReturn(secret);
|
||||
RdsService service = newService(containerManager, proxyManager,
|
||||
new InMemoryStorage<>(), new InMemoryStorage<>(),
|
||||
new InMemoryStorage<>(), new InMemoryStorage<>(),
|
||||
secretsManager);
|
||||
|
||||
DbInstance instance = service.createDbInstance("mydb", "postgres", "13",
|
||||
"admin", null, "dbname", "db.t3.micro",
|
||||
20, true, null, null, null, true, "kms-key-1");
|
||||
|
||||
assertEquals("arn:aws:secretsmanager:us-east-1:123456789012:secret:rds!db-secret", instance.getMasterUserSecretArn());
|
||||
assertEquals("active", instance.getMasterUserSecretStatus());
|
||||
assertEquals("kms-key-1", instance.getMasterUserSecretKmsKeyId());
|
||||
assertNotNull(instance.getMasterPassword());
|
||||
assertTrue(instance.getMasterPassword().startsWith("floci-"));
|
||||
|
||||
ArgumentCaptor<String> secretName = ArgumentCaptor.forClass(String.class);
|
||||
ArgumentCaptor<String> secretString = ArgumentCaptor.forClass(String.class);
|
||||
verify(secretsManager).createSecret(secretName.capture(), secretString.capture(), eq(null), any(), eq("kms-key-1"), eq(null), eq("us-east-1"));
|
||||
assertTrue(secretName.getValue().startsWith("rds!db-"));
|
||||
assertTrue(secretString.getValue().contains("\"username\":\"admin\""));
|
||||
assertTrue(secretString.getValue().contains("\"password\":\"" + instance.getMasterPassword() + "\""));
|
||||
assertTrue(secretString.getValue().contains("\"dbInstanceIdentifier\":\"mydb\""));
|
||||
}
|
||||
|
||||
@Test
|
||||
void listDbInstancesIsCaseInsensitive() {
|
||||
rdsService.createDbInstance("mydb", "postgres", "13",
|
||||
"admin", "password", "dbname", "db.t3.micro",
|
||||
20, false, null, null);
|
||||
20, false, null, null, null);
|
||||
|
||||
Collection<DbInstance> result = rdsService.listDbInstances("MYDB");
|
||||
assertEquals(1, result.size());
|
||||
@@ -91,9 +189,9 @@ class RdsServiceTest {
|
||||
void modifyDbInstanceBlankPasswordDoesNotOverwriteExistingPassword() {
|
||||
rdsService.createDbInstance("mydb", "postgres", "13",
|
||||
"admin", "original-password", "dbname", "db.t3.micro",
|
||||
20, false, null, null);
|
||||
20, false, null, null, null);
|
||||
|
||||
DbInstance modified = rdsService.modifyDbInstance("mydb", " ", null);
|
||||
DbInstance modified = rdsService.modifyDbInstance("mydb", " ", null, null);
|
||||
|
||||
assertEquals("original-password", modified.getMasterPassword());
|
||||
assertFalse(modified.isIamDatabaseAuthenticationEnabled());
|
||||
@@ -103,14 +201,63 @@ class RdsServiceTest {
|
||||
void modifyDbInstanceCanToggleIamWithoutChangingPassword() {
|
||||
rdsService.createDbInstance("mydb", "postgres", "13",
|
||||
"admin", "original-password", "dbname", "db.t3.micro",
|
||||
20, false, null, null);
|
||||
20, false, null, null, null);
|
||||
|
||||
DbInstance modified = rdsService.modifyDbInstance("mydb", null, true);
|
||||
DbInstance modified = rdsService.modifyDbInstance("mydb", null, true, null);
|
||||
|
||||
assertEquals("original-password", modified.getMasterPassword());
|
||||
assertTrue(modified.isIamDatabaseAuthenticationEnabled());
|
||||
}
|
||||
|
||||
@Test
|
||||
void modifyDbInstanceRejectsMissingDbSubnetGroup() {
|
||||
rdsService.createDbInstance("mydb", "postgres", "13",
|
||||
"admin", "original-password", "dbname", "db.t3.micro",
|
||||
20, false, null, null, null);
|
||||
|
||||
AwsException exception = assertThrows(AwsException.class,
|
||||
() -> rdsService.modifyDbInstance("mydb", null, null, "missing-subnet-group"));
|
||||
|
||||
assertEquals("DBSubnetGroupNotFoundFault", exception.getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void dbSubnetGroupRoundTrip() {
|
||||
DbSubnetGroup group = rdsService.createDbSubnetGroup(
|
||||
"sample-db-subnets", "test", java.util.List.of("subnet-aaa", "subnet-bbb"));
|
||||
|
||||
assertEquals("sample-db-subnets", group.getDbSubnetGroupName());
|
||||
assertEquals(java.util.List.of("subnet-aaa", "subnet-bbb"), group.getSubnetIds());
|
||||
assertEquals(1, rdsService.listDbSubnetGroups("sample-db-subnets").size());
|
||||
|
||||
rdsService.deleteDbSubnetGroup("sample-db-subnets");
|
||||
assertTrue(rdsService.listDbSubnetGroups("sample-db-subnets").isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void createDbInstanceRejectsMissingDbSubnetGroupBeforeStartingRuntime() {
|
||||
AwsException exception = assertThrows(AwsException.class, () ->
|
||||
rdsService.createDbInstance("mydb", "postgres", "13",
|
||||
"admin", "password", "dbname", "db.t3.micro",
|
||||
20, false, null, "missing-subnet-group", null));
|
||||
|
||||
assertEquals("DBSubnetGroupNotFoundFault", exception.getErrorCode());
|
||||
verify(containerManager, never()).start(any(), any(), any(), any(), any(), any(), any());
|
||||
verify(proxyManager, never()).startProxy(any(), any(), anyBoolean(), anyInt(),
|
||||
any(), anyInt(), any(), any(), any(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void describeOrderableDbInstanceOptionsFiltersByEngineVersionAndClass() {
|
||||
var result = rdsService.describeOrderableDbInstanceOptions(
|
||||
"postgres", "18.1", "db.t3.micro");
|
||||
|
||||
assertEquals(1, result.size());
|
||||
assertEquals("postgres", result.getFirst().get("engine"));
|
||||
assertEquals("18.1", result.getFirst().get("engineVersion"));
|
||||
assertEquals("db.t3.micro", result.getFirst().get("dbInstanceClass"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void deleteDbClusterFailsWhenMembersRemain() {
|
||||
DbCluster cluster = rdsService.createDbCluster("cluster1", "postgres", "13",
|
||||
@@ -190,7 +337,7 @@ class RdsServiceTest {
|
||||
instances, clusters, parameterGroups, clusterParameterGroups);
|
||||
DbInstance created = initialService.createDbInstance("mydb", "postgres", "16.3",
|
||||
"admin", "secret", "app", "db.t3.micro",
|
||||
20, false, null, null);
|
||||
20, false, null, null, null);
|
||||
|
||||
String persistedVolumeId = created.getVolumeId();
|
||||
int persistedProxyPort = created.getProxyPort();
|
||||
@@ -214,7 +361,7 @@ class RdsServiceTest {
|
||||
assertEquals(15432, restored.getContainerPort());
|
||||
|
||||
verify(restoredContainerManager).start(eq("mydb"), eq(persistedVolumeId),
|
||||
eq(DatabaseEngine.POSTGRES), any(), eq("admin"), eq("secret"), eq("app"));
|
||||
eq(DatabaseEngine.POSTGRES), eq("postgres:16.3-alpine"), eq("admin"), eq("secret"), eq("app"));
|
||||
verify(restoredProxyManager).startProxy(eq("mydb"), eq(DatabaseEngine.POSTGRES),
|
||||
eq(false), eq(persistedProxyPort), eq("127.0.0.1"), eq(15432),
|
||||
eq("admin"), eq("secret"), eq("app"), any());
|
||||
@@ -236,7 +383,7 @@ class RdsServiceTest {
|
||||
"admin", "secret", "app", false, null);
|
||||
DbInstance member = initialService.createDbInstance("member1", "aurora-postgresql", "16.3",
|
||||
"admin", "secret", "app", "db.t3.medium",
|
||||
20, false, null, "cluster1");
|
||||
20, false, null, null, "cluster1");
|
||||
|
||||
RdsContainerManager restoredContainerManager = mock(RdsContainerManager.class);
|
||||
RdsProxyManager restoredProxyManager = mock(RdsProxyManager.class);
|
||||
@@ -259,7 +406,7 @@ class RdsServiceTest {
|
||||
assertEquals(15432, restoredMember.getContainerPort());
|
||||
|
||||
verify(restoredContainerManager).start(eq("cluster1"), eq(cluster.getVolumeId()),
|
||||
eq(DatabaseEngine.POSTGRES), any(), eq("admin"), eq("secret"), eq("app"));
|
||||
eq(DatabaseEngine.POSTGRES), eq("postgres:16.3-alpine"), eq("admin"), eq("secret"), eq("app"));
|
||||
verify(restoredProxyManager).startProxy(eq("cluster1"), eq(DatabaseEngine.POSTGRES),
|
||||
eq(false), eq(cluster.getProxyPort()), eq("127.0.0.1"), eq(15432),
|
||||
eq("admin"), eq("secret"), eq("app"), any());
|
||||
@@ -274,7 +421,17 @@ class RdsServiceTest {
|
||||
StorageBackend<String, DbCluster> clusters,
|
||||
StorageBackend<String, DbParameterGroup> parameterGroups,
|
||||
StorageBackend<String, DbClusterParameterGroup> clusterParameterGroups) {
|
||||
return newService(containerManager, proxyManager, instances, clusters, parameterGroups, clusterParameterGroups, null);
|
||||
}
|
||||
|
||||
private RdsService newService(RdsContainerManager containerManager,
|
||||
RdsProxyManager proxyManager,
|
||||
StorageBackend<String, DbInstance> instances,
|
||||
StorageBackend<String, DbCluster> clusters,
|
||||
StorageBackend<String, DbParameterGroup> parameterGroups,
|
||||
StorageBackend<String, DbClusterParameterGroup> clusterParameterGroups,
|
||||
SecretsManagerService secretsManager) {
|
||||
return new RdsService(containerManager, proxyManager, regionResolver, config,
|
||||
instances, clusters, parameterGroups, clusterParameterGroups);
|
||||
instances, clusters, parameterGroups, clusterParameterGroups, new InMemoryStorage<>(), secretsManager);
|
||||
}
|
||||
}
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
package io.github.hectorvent.floci.services.rds.container;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
class RdsContainerManagerTest {
|
||||
|
||||
@Test
|
||||
void postgresInitSqlCreatesRdsIamRoleWhenMissing() {
|
||||
String sql = RdsContainerManager.postgresIamRoleInitSql();
|
||||
|
||||
assertTrue(sql.contains("pg_roles"));
|
||||
assertTrue(sql.contains("rolname = 'rds_iam'"));
|
||||
assertTrue(sql.contains("CREATE ROLE rds_iam"));
|
||||
}
|
||||
}
|
||||
+86
@@ -0,0 +1,86 @@
|
||||
package io.github.hectorvent.floci.services.rds.proxy;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import javax.net.ssl.SSLContext;
|
||||
import javax.net.ssl.SSLSocket;
|
||||
import javax.net.ssl.TrustManager;
|
||||
import javax.net.ssl.X509TrustManager;
|
||||
import java.io.DataInputStream;
|
||||
import java.io.DataOutputStream;
|
||||
import java.net.ServerSocket;
|
||||
import java.net.Socket;
|
||||
import java.security.SecureRandom;
|
||||
import java.security.cert.X509Certificate;
|
||||
import java.util.concurrent.ArrayBlockingQueue;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
class PostgresProtocolHandlerTest {
|
||||
|
||||
private static final int SSL_REQUEST_CODE = 80877103;
|
||||
|
||||
@Test
|
||||
void acceptsPostgresSslRequestAndUpgradesSocket() throws Exception {
|
||||
ArrayBlockingQueue<Integer> serverRead = new ArrayBlockingQueue<>(1);
|
||||
|
||||
try (ServerSocket server = new ServerSocket(0)) {
|
||||
Thread.ofVirtual().start(() -> {
|
||||
try (Socket accepted = server.accept()) {
|
||||
DataInputStream in = new DataInputStream(accepted.getInputStream());
|
||||
DataOutputStream out = new DataOutputStream(accepted.getOutputStream());
|
||||
assertEquals(8, in.readInt());
|
||||
assertEquals(SSL_REQUEST_CODE, in.readInt());
|
||||
|
||||
out.writeByte('S');
|
||||
out.flush();
|
||||
Socket sslSocket = PostgresProtocolHandler.acceptSsl(accepted);
|
||||
serverRead.add(sslSocket.getInputStream().read());
|
||||
sslSocket.getOutputStream().write(99);
|
||||
sslSocket.getOutputStream().flush();
|
||||
sslSocket.close();
|
||||
} catch (Exception e) {
|
||||
serverRead.add(-1);
|
||||
}
|
||||
});
|
||||
|
||||
try (Socket rawClient = new Socket("localhost", server.getLocalPort())) {
|
||||
DataOutputStream out = new DataOutputStream(rawClient.getOutputStream());
|
||||
DataInputStream in = new DataInputStream(rawClient.getInputStream());
|
||||
out.writeInt(8);
|
||||
out.writeInt(SSL_REQUEST_CODE);
|
||||
out.flush();
|
||||
assertEquals('S', in.readUnsignedByte());
|
||||
|
||||
SSLSocket sslClient = (SSLSocket) trustAllContext().getSocketFactory()
|
||||
.createSocket(rawClient, "localhost", server.getLocalPort(), true);
|
||||
sslClient.setUseClientMode(true);
|
||||
sslClient.startHandshake();
|
||||
sslClient.getOutputStream().write(42);
|
||||
sslClient.getOutputStream().flush();
|
||||
assertEquals(99, sslClient.getInputStream().read());
|
||||
sslClient.close();
|
||||
}
|
||||
}
|
||||
|
||||
assertEquals(42, serverRead.poll(5, TimeUnit.SECONDS));
|
||||
}
|
||||
|
||||
private static SSLContext trustAllContext() throws Exception {
|
||||
SSLContext context = SSLContext.getInstance("TLS");
|
||||
context.init(null, new TrustManager[] {new X509TrustManager() {
|
||||
@Override
|
||||
public void checkClientTrusted(X509Certificate[] chain, String authType) {}
|
||||
|
||||
@Override
|
||||
public void checkServerTrusted(X509Certificate[] chain, String authType) {}
|
||||
|
||||
@Override
|
||||
public X509Certificate[] getAcceptedIssuers() {
|
||||
return new X509Certificate[0];
|
||||
}
|
||||
}}, new SecureRandom());
|
||||
return context;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user