diff --git a/src/main/java/io/github/hectorvent/floci/services/rds/RdsService.java b/src/main/java/io/github/hectorvent/floci/services/rds/RdsService.java index de593e7a05..a4000239b9 100644 --- a/src/main/java/io/github/hectorvent/floci/services/rds/RdsService.java +++ b/src/main/java/io/github/hectorvent/floci/services/rds/RdsService.java @@ -661,7 +661,7 @@ public class RdsService implements Resettable, ResourceProvider { DbEndpoint endpoint = mock ? new DbEndpoint("localhost", proxyPort) : proxyEndpoint(proxyPort); DbInstance instance = new DbInstance(id, engine, engineVersion, masterUsername, masterPassword, - dbName, dbInstanceClass, allocatedStorage, DbInstanceStatus.AVAILABLE, + dbName, dbInstanceClass, allocatedStorage, DbInstanceStatus.CREATING, endpoint, iamEnabled, paramGroupName, dbClusterIdentifier, Instant.now(), proxyPort); instance.setOptionGroupName(optionGroupName); instance.setDbSubnetGroupName(dbSubnetGroupName); @@ -688,15 +688,26 @@ public class RdsService implements Resettable, ResourceProvider { attachManagedMasterUserSecret(instance, effectiveRegion, masterUserSecretKmsKeyId); } + String accountId = accountIdFromArn(instance.getDbInstanceArn()); + putInstanceForScope(accountId, effectiveRegion, id, instance); + if (!mock && hasBackend(backendHost, backendPort)) { - final String accountId = accountIdFromArn(instance.getDbInstanceArn()); final String instanceRegion = regionFromArn(instance.getDbInstanceArn()); - proxyManager.startProxy(rdsResourceRelayKey(instance.getDbInstanceArn(), id), - engine, iamEnabled, proxyPort, backendHost, backendPort, - instance.getEndpoint().address(), - masterUsername, masterPassword, dbName, - (user, pw) -> validateDbPasswordForScope( - accountId, instanceRegion, id, user, pw)); + try { + proxyManager.startProxy(rdsResourceRelayKey(instance.getDbInstanceArn(), id), + engine, iamEnabled, proxyPort, backendHost, backendPort, + instance.getEndpoint().address(), + masterUsername, masterPassword, dbName, + (user, pw) -> validateDbPasswordForScope( + accountId, instanceRegion, id, user, pw)); + } catch (RuntimeException | Error e) { + try { + deleteInstanceForScope(accountId, effectiveRegion, id); + } catch (RuntimeException rollbackFailure) { + e.addSuppressed(rollbackFailure); + } + throw e; + } } if (dbClusterIdentifier != null && !dbClusterIdentifier.isBlank()) { @@ -709,7 +720,8 @@ public class RdsService implements Resettable, ResourceProvider { } } - putInstanceForScope(currentAccountId(), effectiveRegion, id, instance); + instance.setStatus(DbInstanceStatus.AVAILABLE); + putInstanceForScope(accountId, effectiveRegion, id, instance); LOG.infov("DB instance {0} created, engine={1}, endpoint={2}:{3}", id, engine, endpoint.address(), String.valueOf(endpoint.port())); return instance; diff --git a/src/test/java/io/github/hectorvent/floci/services/rds/RdsServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/rds/RdsServiceTest.java index 098dd3a51a..a7f1d08d78 100644 --- a/src/test/java/io/github/hectorvent/floci/services/rds/RdsServiceTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/rds/RdsServiceTest.java @@ -171,6 +171,40 @@ class RdsServiceTest { assertEquals("arn:aws:rds:us-east-1:123456789012:db:mydb", instance.getDbInstanceArn()); } + @Test + void createDbInstanceMakesRequestedPasswordValidBeforeProxyStartsAcceptingConnections() { + doAnswer(invocation -> { + assertEquals(DbInstanceStatus.CREATING, + rdsService.getDbInstance("mypostgres").getStatus()); + RdsAuthProxy.MasterPasswordCheck passwordCheck = invocation.getArgument(10); + assertTrue(passwordCheck.validate("admin", "secret123")); + assertFalse(passwordCheck.validate("admin", "wrong")); + return null; + }).when(proxyManager).startProxy(any(), any(), anyBoolean(), anyInt(), any(), anyInt(), + any(), any(), any(), any(), any()); + + DbInstance instance = rdsService.createDbInstance("mypostgres", "postgres", "13", + "admin", "secret123", null, "db.t3.micro", + 20, false, null, null, null, null, false); + + assertEquals(DbInstanceStatus.AVAILABLE, instance.getStatus()); + assertEquals(DbInstanceStatus.AVAILABLE, + rdsService.getDbInstance("mypostgres").getStatus()); + } + + @Test + void createDbInstanceRemovesVisibleRecordWhenProxyStartupFails() { + doThrow(new IllegalStateException("proxy down")) + .when(proxyManager).startProxy(any(), any(), anyBoolean(), anyInt(), any(), anyInt(), + any(), any(), any(), any(), any()); + + assertThrows(IllegalStateException.class, () -> rdsService.createDbInstance( + "mypostgres", "postgres", "13", + "admin", "secret123", null, "db.t3.micro", + 20, false, null, null, null, null, false)); + assertThrows(AwsException.class, () -> rdsService.getDbInstance("mypostgres")); + } + @Test void createDbInstanceRejectsLegacyBareAuroraEngine() { // "aurora" (bare) is the retired Aurora MySQL 5.6 identifier; real AWS no