diff --git a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/RdsClusterRestoreIamTest.java b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/RdsClusterRestoreIamTest.java new file mode 100644 index 0000000000..e75c1d687d --- /dev/null +++ b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/RdsClusterRestoreIamTest.java @@ -0,0 +1,79 @@ +package com.floci.test; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; +import software.amazon.awssdk.services.rds.RdsClient; +import software.amazon.awssdk.services.rds.model.DBCluster; +import software.amazon.awssdk.services.rds.model.RdsException; + +import java.util.logging.Level; +import java.util.logging.Logger; + +import static org.assertj.core.api.Assertions.assertThat; + +@DisplayName("Cluster restore IAM authentication defaults") +class RdsClusterRestoreIamTest { + private static final Logger LOG = Logger.getLogger(RdsClusterRestoreIamTest.class.getName()); + + @ParameterizedTest + @ValueSource(strings = {"snapshot", "point"}) + @DisplayName("Omitted IAM authentication is disabled and explicit choices are preserved") + void restoredIamDefaultsToDisabled(String path) { + String source = TestFixtures.uniqueName("restore-iam"); + String snapshot = source + "-snap"; + try (RdsClient rds = TestFixtures.rdsClient()) { + try { + rds.createDBCluster(b -> b.dbClusterIdentifier(source).engine("aurora-postgresql") + .masterUsername("admin").masterUserPassword("Password123!") + .enableIAMDatabaseAuthentication(true)); + rds.createDBClusterSnapshot(b -> b.dbClusterIdentifier(source).dbClusterSnapshotIdentifier(snapshot)); + for (Boolean flag : new Boolean[]{null, false, true}) { + String target = source + "-" + (flag == null ? "omitted" : flag.toString()); + try { + DBCluster restored; + if ("snapshot".equals(path)) { + restored = rds.restoreDBClusterFromSnapshot(b -> { + b.dbClusterIdentifier(target).snapshotIdentifier(snapshot).engine("aurora-postgresql"); + if (flag != null) { + b.enableIAMDatabaseAuthentication(flag); + } + }).dbCluster(); + } else { + restored = rds.restoreDBClusterToPointInTime(b -> { + b.dbClusterIdentifier(target).sourceDBClusterIdentifier(source) + .useLatestRestorableTime(true); + if (flag != null) { + b.enableIAMDatabaseAuthentication(flag); + } + }).dbCluster(); + } + boolean expected = Boolean.TRUE.equals(flag); + assertThat(restored.iamDatabaseAuthenticationEnabled()).isEqualTo(expected); + assertThat(rds.describeDBClusters(b -> b.dbClusterIdentifier(target)).dbClusters() + .get(0).iamDatabaseAuthenticationEnabled()).isEqualTo(expected); + assertThat(rds.describeDBClusters(b -> b.dbClusterIdentifier(source)).dbClusters() + .get(0).iamDatabaseAuthenticationEnabled()).isTrue(); + } finally { + deleteCluster(rds, target); + } + } + } finally { + try { + rds.deleteDBClusterSnapshot(b -> b.dbClusterSnapshotIdentifier(snapshot)); + } catch (RdsException e) { + LOG.log(Level.WARNING, "Could not clean up snapshot " + snapshot, e); + } + deleteCluster(rds, source); + } + } + } + + private static void deleteCluster(RdsClient rds, String identifier) { + try { + rds.deleteDBCluster(b -> b.dbClusterIdentifier(identifier).skipFinalSnapshot(true)); + } catch (RdsException e) { + LOG.log(Level.WARNING, "Could not clean up cluster " + identifier, e); + } + } +} diff --git a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/RdsInstanceSnapshotIamTest.java b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/RdsInstanceSnapshotIamTest.java new file mode 100644 index 0000000000..efcfae4256 --- /dev/null +++ b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/RdsInstanceSnapshotIamTest.java @@ -0,0 +1,69 @@ +package com.floci.test; + +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; +import software.amazon.awssdk.services.rds.RdsClient; +import software.amazon.awssdk.services.rds.model.DBInstance; +import software.amazon.awssdk.services.rds.model.RdsException; + +import java.util.logging.Level; +import java.util.logging.Logger; + +import static org.assertj.core.api.Assertions.assertThat; + +class RdsInstanceSnapshotIamTest { + private static final Logger LOG = Logger.getLogger(RdsInstanceSnapshotIamTest.class.getName()); + + @ParameterizedTest + @ValueSource(booleans = {false, true}) + void restoredIamUsesExplicitFlagOrDefaultsFalse(boolean sourceEnabled) { + String source = TestFixtures.uniqueName("snapshot-iam"); + String snapshot = source + "-snap"; + try (RdsClient rds = TestFixtures.rdsClient()) { + try { + rds.createDBInstance(b -> b.dbInstanceIdentifier(source).engine("postgres") + .dbInstanceClass("db.t3.micro").allocatedStorage(20) + .masterUsername("admin").masterUserPassword("Password123!") + .enableIAMDatabaseAuthentication(sourceEnabled)); + rds.createDBSnapshot(b -> b.dbInstanceIdentifier(source).dbSnapshotIdentifier(snapshot)); + for (Boolean flag : new Boolean[]{null, false, true}) { + String target = source + "-" + (flag == null ? "omitted" : flag.toString()); + try { + DBInstance restored = rds.restoreDBInstanceFromDBSnapshot(b -> { + b.dbInstanceIdentifier(target).dbSnapshotIdentifier(snapshot); + if (flag != null) { + b.enableIAMDatabaseAuthentication(flag); + } + if (Boolean.TRUE.equals(flag)) { + b.port(5433); + } + }).dbInstance(); + boolean expected = Boolean.TRUE.equals(flag); + assertThat(restored.iamDatabaseAuthenticationEnabled()).isEqualTo(expected); + assertThat(rds.describeDBInstances(b -> b.dbInstanceIdentifier(target)).dbInstances() + .get(0).iamDatabaseAuthenticationEnabled()).isEqualTo(expected); + assertThat(rds.describeDBInstances(b -> b.dbInstanceIdentifier(source)).dbInstances() + .get(0).iamDatabaseAuthenticationEnabled()).isEqualTo(sourceEnabled); + } finally { + deleteInstance(rds, target); + } + } + } finally { + try { + rds.deleteDBSnapshot(b -> b.dbSnapshotIdentifier(snapshot)); + } catch (RdsException e) { + LOG.log(Level.WARNING, "Could not clean up snapshot " + snapshot, e); + } + deleteInstance(rds, source); + } + } + } + + private static void deleteInstance(RdsClient rds, String identifier) { + try { + rds.deleteDBInstance(b -> b.dbInstanceIdentifier(identifier).skipFinalSnapshot(true)); + } catch (RdsException e) { + LOG.log(Level.WARNING, "Could not clean up instance " + identifier, e); + } + } +} diff --git a/docs/services/rds.md b/docs/services/rds.md index ae518ba0da..ac12605ce8 100644 --- a/docs/services/rds.md +++ b/docs/services/rds.md @@ -190,9 +190,12 @@ including empty strings, return `InvalidParameterValue` before changing resource | `CopyDBClusterSnapshot` | `CopyTags` | Defaults to `false`; explicit request tags still apply | | `CreateEventSubscription` | `Enabled` | Defaults to `true` | | `ModifyEventSubscription` | `Enabled` | Preserves the current value | +| `RestoreDBInstanceFromDBSnapshot` | `MultiAZ`, `EnableIAMDatabaseAuthentication` | Defaults to `false`, regardless of the snapshot setting | +| `RestoreDBClusterFromSnapshot`, `RestoreDBClusterToPointInTime` | `EnableIAMDatabaseAuthentication` | Defaults to `false`, regardless of the source setting | Malformed reset flags leave saved parameter overrides unchanged. Malformed event-subscription flags leave the subscription and its topic, source type and event categories unchanged. +Malformed restore flags are rejected before starting an instance or cluster restore. ## Event notification subscriptions diff --git a/src/main/java/io/github/hectorvent/floci/services/rds/RdsQueryHandler.java b/src/main/java/io/github/hectorvent/floci/services/rds/RdsQueryHandler.java index f5c8435e5c..33980af5c5 100644 --- a/src/main/java/io/github/hectorvent/floci/services/rds/RdsQueryHandler.java +++ b/src/main/java/io/github/hectorvent/floci/services/rds/RdsQueryHandler.java @@ -1697,8 +1697,7 @@ public class RdsQueryHandler { } String dbInstanceClass = params.getFirst("DBInstanceClass"); String availabilityZone = params.getFirst("AvailabilityZone"); - String multiAzStr = params.getFirst("MultiAZ"); - boolean multiAz = multiAzStr != null && Boolean.parseBoolean(multiAzStr); + boolean multiAz = Boolean.TRUE.equals(parseOptionalBoolean(params, "MultiAZ")); String dbSubnetGroupName = params.getFirst("DBSubnetGroupName"); List vpcSecurityGroupIds = new ArrayList<>(); @@ -1712,13 +1711,10 @@ public class RdsQueryHandler { try { Integer requestedPort = parseIntegerParam(params, "Port"); - DbInstance instance = requestedPort == null - ? service.restoreDbInstanceFromDbSnapshot(instanceId, snapshotId, - dbInstanceClass, availabilityZone, multiAz, dbSubnetGroupName, - vpcSecurityGroupIds, tags, region) - : service.restoreDbInstanceFromDbSnapshot(instanceId, snapshotId, - dbInstanceClass, availabilityZone, multiAz, dbSubnetGroupName, - vpcSecurityGroupIds, tags, region, requestedPort); + DbInstance instance = service.restoreDbInstanceFromDbSnapshot(instanceId, snapshotId, + dbInstanceClass, availabilityZone, multiAz, dbSubnetGroupName, + vpcSecurityGroupIds, tags, region, requestedPort, + parseOptionalBoolean(params, "EnableIAMDatabaseAuthentication")); String result = dbInstanceXml(instance); return Response.ok(AwsQueryResponse.envelope("RestoreDBInstanceFromDBSnapshot", AwsNamespaces.RDS, result)).build(); } catch (AwsException e) { @@ -2354,7 +2350,7 @@ public class RdsQueryHandler { parseOptionalBoolean(params, "UseLatestRestorableTime"), optionalInt(params.getFirst("Port")), params.getFirst("DBSubnetGroupName"), params.getFirst("DBClusterParameterGroupName"), - optionalBoolean(params.getFirst("EnableIAMDatabaseAuthentication")), + parseOptionalBoolean(params, "EnableIAMDatabaseAuthentication"), params.getFirst("EngineMode"), parseTags(params), region); return Response.ok(AwsQueryResponse.envelope("RestoreDBClusterToPointInTime", AwsNamespaces.RDS, dbClusterXml(cluster))).build(); @@ -2379,7 +2375,7 @@ public class RdsQueryHandler { params.getFirst("DatabaseName"), params.getFirst("DBSubnetGroupName"), params.getFirst("DBClusterParameterGroupName"), params.getFirst("AvailabilityZones.member.1"), - optionalBoolean(params.getFirst("EnableIAMDatabaseAuthentication")), + parseOptionalBoolean(params, "EnableIAMDatabaseAuthentication"), params.getFirst("EngineMode"), parseTags(params), region); return Response.ok(AwsQueryResponse.envelope("RestoreDBClusterFromSnapshot", AwsNamespaces.RDS, dbClusterXml(cluster))).build(); 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 04b4cfcc5b..010e1ca5e7 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 @@ -1154,6 +1154,15 @@ public class RdsService implements Resettable, ResourceProvider { List vpcSecurityGroupIds, Map tags, String region, Integer requestedPort) { + return restoreDbInstanceFromDbSnapshot(instanceId, snapshotId, dbInstanceClass, availabilityZone, + multiAz, dbSubnetGroupName, vpcSecurityGroupIds, tags, region, requestedPort, null); + } + + public DbInstance restoreDbInstanceFromDbSnapshot(String instanceId, String snapshotId, String dbInstanceClass, + String availabilityZone, boolean multiAz, String dbSubnetGroupName, + List vpcSecurityGroupIds, + Map tags, String region, + Integer requestedPort, Boolean iamEnabled) { String effectiveRegion = effectiveRegion(region); DbSnapshot snapshot = Optional.ofNullable(findSnapshotForScope(currentAccountId(), effectiveRegion, snapshotId)) .orElseThrow(() -> new AwsException("DBSnapshotNotFound", "DBSnapshot " + snapshotId + " not found.", 404)); @@ -1170,7 +1179,7 @@ public class RdsService implements Resettable, ResourceProvider { snapshot.isStorageEncrypted(), snapshot.getKmsKeyId(), null, null, null, null); DbInstance instance = createDbInstance(instanceId, snapshot.getEngine().name().toLowerCase(), snapshot.getEngineVersion(), snapshot.getMasterUsername(), snapshot.getMasterPassword(), - snapshot.getDbName(), targetClass, snapshot.getAllocatedStorage(), snapshot.isIamDatabaseAuthenticationEnabled(), + snapshot.getDbName(), targetClass, snapshot.getAllocatedStorage(), Boolean.TRUE.equals(iamEnabled), null, dbSubnetGroupName, null, availabilityZone, multiAz, false, null, tags, vpcSecurityGroupIds, null, effectiveRegion, true, restoreSettings, null, requestedPort); @@ -1959,7 +1968,7 @@ public class RdsService implements Resettable, ResourceProvider { engineVersion != null && !engineVersion.isBlank() ? engineVersion : snapshot.getEngineVersion(), snapshot.getMasterUsername(), snapshot.getMasterPassword(), databaseName != null && !databaseName.isBlank() ? databaseName : snapshot.getDatabaseName(), - iamEnabled != null ? iamEnabled : snapshot.isIamDatabaseAuthenticationEnabled(), + Boolean.TRUE.equals(iamEnabled), parameterGroupName, dbSubnetGroupName, availabilityZone, false, effectiveRegion, null, null, null, false, null, engineMode != null && !engineMode.isBlank() ? engineMode : snapshot.getEngineMode(), @@ -2038,7 +2047,7 @@ public class RdsService implements Resettable, ResourceProvider { ? source.getEngineIdentifier() : source.getEngine().name().toLowerCase(Locale.ROOT); DbCluster cluster = createDbClusterWithPort(clusterId, engineParam, source.getEngineVersion(), source.getMasterUsername(), source.getMasterPassword(), source.getDatabaseName(), - iamEnabled != null ? iamEnabled : source.isIamDatabaseAuthenticationEnabled(), + Boolean.TRUE.equals(iamEnabled), parameterGroupName, dbSubnetGroupName, null, false, effectiveRegion, null, null, null, false, null, !isBlank(engineMode) ? engineMode : source.getEngineMode(), diff --git a/src/test/java/io/github/hectorvent/floci/services/rds/RdsClusterRestoreIamIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/rds/RdsClusterRestoreIamIntegrationTest.java new file mode 100644 index 0000000000..065aa7a23a --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/rds/RdsClusterRestoreIamIntegrationTest.java @@ -0,0 +1,61 @@ +package io.github.hectorvent.floci.services.rds; + +import io.github.hectorvent.floci.testing.RdsMockProfile; +import io.quarkus.test.junit.QuarkusTest; +import io.quarkus.test.junit.TestProfile; +import io.restassured.specification.RequestSpecification; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; + +import java.util.UUID; + +import static io.github.hectorvent.floci.testing.RdsRequests.rds; +import static org.hamcrest.Matchers.containsString; + +@QuarkusTest +@TestProfile(RdsMockProfile.class) +class RdsClusterRestoreIamIntegrationTest { + @ParameterizedTest + @CsvSource({"snapshot, , false", "snapshot, false, false", "snapshot, true, true", + "point, , false", "point, false, false", "point, true, true"}) + void restoredIamFlagDefaultsToFalse(String path, String flag, boolean expected) { + String source = "iam-source-" + UUID.randomUUID(); + String snapshot = source + "-snap"; + String target = source + "-target"; + try { + rds("CreateDBCluster").formParam("DBClusterIdentifier", source) + .formParam("Engine", "aurora-postgresql").formParam("MasterUsername", "admin") + .formParam("MasterUserPassword", "password123") + .formParam("EnableIAMDatabaseAuthentication", "true") + .post("/").then().statusCode(200); + rds("CreateDBClusterSnapshot").formParam("DBClusterIdentifier", source) + .formParam("DBClusterSnapshotIdentifier", snapshot).post("/").then().statusCode(200); + RequestSpecification request; + if ("snapshot".equals(path)) { + request = rds("RestoreDBClusterFromSnapshot").formParam("SnapshotIdentifier", snapshot) + .formParam("Engine", "aurora-postgresql"); + } else { + request = rds("RestoreDBClusterToPointInTime").formParam("SourceDBClusterIdentifier", source) + .formParam("UseLatestRestorableTime", "true"); + } + request.formParam("DBClusterIdentifier", target); + if (flag != null) { + request.formParam("EnableIAMDatabaseAuthentication", flag); + } + request.post("/").then().statusCode(200) + .body(containsString("" + expected + + "")); + rds("DescribeDBClusters").formParam("DBClusterIdentifier", target).post("/").then().statusCode(200) + .body(containsString("" + expected + + "")); + rds("DescribeDBClusters").formParam("DBClusterIdentifier", source).post("/").then().statusCode(200) + .body(containsString("true")); + } finally { + for (String id : new String[]{target, source}) { + rds("DeleteDBCluster").formParam("DBClusterIdentifier", id) + .formParam("SkipFinalSnapshot", "true").post("/"); + } + rds("DeleteDBClusterSnapshot").formParam("DBClusterSnapshotIdentifier", snapshot).post("/"); + } + } +} diff --git a/src/test/java/io/github/hectorvent/floci/services/rds/RdsInstanceSnapshotIamIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/rds/RdsInstanceSnapshotIamIntegrationTest.java new file mode 100644 index 0000000000..55611f3d27 --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/rds/RdsInstanceSnapshotIamIntegrationTest.java @@ -0,0 +1,70 @@ +package io.github.hectorvent.floci.services.rds; + +import io.github.hectorvent.floci.testing.RdsMockProfile; +import io.quarkus.test.junit.QuarkusTest; +import io.quarkus.test.junit.TestProfile; +import io.restassured.specification.RequestSpecification; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; + +import java.util.UUID; + +import static io.github.hectorvent.floci.testing.RdsRequests.rds; +import static org.hamcrest.Matchers.containsString; + +@QuarkusTest +@TestProfile(RdsMockProfile.class) +class RdsInstanceSnapshotIamIntegrationTest { + @ParameterizedTest + @CsvSource({"true, , false", "true, FaLsE, false", "true, TrUe, true", + "false, , false", "false, FaLsE, false", "false, TrUe, true"}) + void restoreUsesRequestedIamFlagOrDefaultsFalse(boolean sourceEnabled, String flag, boolean expected) { + restore(sourceEnabled, flag, false, expected); + } + + @ParameterizedTest + @CsvSource({"true, yes", "true, ''", "true, not-a-boolean", + "false, yes", "false, ''", "false, not-a-boolean"}) + void malformedIamFlagDoesNotCreateInstance(boolean sourceEnabled, String flag) { + restore(sourceEnabled, flag, true, false); + } + + private static void restore(boolean sourceEnabled, String flag, boolean invalid, boolean expected) { + String source = "instance-iam-" + UUID.randomUUID(); + String snapshot = source + "-snap"; + String target = source + "-target"; + try { + rds("CreateDBInstance").formParam("DBInstanceIdentifier", source).formParam("Engine", "postgres") + .formParam("DBInstanceClass", "db.t3.micro").formParam("MasterUsername", "admin") + .formParam("MasterUserPassword", "Password123!").formParam("AllocatedStorage", 20) + .formParam("EnableIAMDatabaseAuthentication", sourceEnabled).post("/").then().statusCode(200); + rds("CreateDBSnapshot").formParam("DBInstanceIdentifier", source) + .formParam("DBSnapshotIdentifier", snapshot).post("/").then().statusCode(200); + RequestSpecification request = rds("RestoreDBInstanceFromDBSnapshot") + .formParam("DBInstanceIdentifier", target).formParam("DBSnapshotIdentifier", snapshot); + if (flag != null) { + request.formParam("EnableIAMDatabaseAuthentication", flag); + } + if (invalid) { + request.post("/").then().statusCode(400).body(containsString("InvalidParameterValue")); + rds("DescribeDBInstances").formParam("DBInstanceIdentifier", target) + .post("/").then().statusCode(404).body(containsString("DBInstanceNotFound")); + } else { + request.post("/").then().statusCode(200).body(containsString( + "" + expected + "")); + rds("DescribeDBInstances").formParam("DBInstanceIdentifier", target).post("/").then() + .statusCode(200).body(containsString("" + expected + + "")); + } + rds("DescribeDBInstances").formParam("DBInstanceIdentifier", source).post("/").then() + .statusCode(200).body(containsString("" + sourceEnabled + + "")); + } finally { + for (String id : new String[]{target, source}) { + rds("DeleteDBInstance").formParam("DBInstanceIdentifier", id) + .formParam("SkipFinalSnapshot", true).post("/"); + } + rds("DeleteDBSnapshot").formParam("DBSnapshotIdentifier", snapshot).post("/"); + } + } +} diff --git a/src/test/java/io/github/hectorvent/floci/services/rds/RdsQueryHandlerTest.java b/src/test/java/io/github/hectorvent/floci/services/rds/RdsQueryHandlerTest.java index 89ddde06d6..84d3187565 100644 --- a/src/test/java/io/github/hectorvent/floci/services/rds/RdsQueryHandlerTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/rds/RdsQueryHandlerTest.java @@ -2469,10 +2469,122 @@ class RdsQueryHandlerTest { eq(List.of("222222222222")), eq(List.of("111111111111")), isNull()); } + @ParameterizedTest + @CsvSource({ + "RestoreDBInstanceFromDBSnapshot, MultiAZ", + "RestoreDBInstanceFromDBSnapshot, EnableIAMDatabaseAuthentication", + "RestoreDBClusterFromSnapshot, EnableIAMDatabaseAuthentication", + "RestoreDBClusterToPointInTime, EnableIAMDatabaseAuthentication" + }) + void restoreBooleanFlagsRejectMalformedValuesBeforeServiceCalls(String action, String parameter) { + when(service.restoreDbInstanceFromDbSnapshot(any(), any(), any(), any(), anyBoolean(), + any(), any(), any(), any(), any(), any())).thenReturn(makeInstance("restored")); + when(service.restoreDbClusterFromSnapshot(any(), any(), any(), any(), any(), any(), + any(), any(), any(), any(), any(), any(), any())).thenReturn(makeCluster("restored")); + when(service.restoreDbClusterToPointInTime(any(), any(), any(), any(), any(), any(), + any(), any(), any(), any(), any(), any(), any())).thenReturn(makeCluster("restored")); + for (String invalid : List.of("yes", "", "not-a-boolean")) { + MultivaluedMap p = params(); + p.putSingle("DBInstanceIdentifier", "restored"); + p.putSingle("DBSnapshotIdentifier", "source"); + p.putSingle("DBClusterIdentifier", "restored"); + p.putSingle("SnapshotIdentifier", "source"); + p.putSingle("SourceDBClusterIdentifier", "source"); + p.putSingle("Engine", "aurora-postgresql"); + p.putSingle("UseLatestRestorableTime", "true"); + p.putSingle(parameter, invalid); + + Response response = handler.handle(action, p); + + assertEquals(400, response.getStatus()); + assertTrue(((String) response.getEntity()).contains("InvalidParameterValue")); + assertTrue(((String) response.getEntity()).contains(parameter + " must be true or false.")); + verifyNoInteractions(service); + } + } + + @ParameterizedTest + @NullSource + @ValueSource(strings = {"true", "false", "TrUe", "FaLsE"}) + void restoreInstanceSnapshotPreservesMultiAzValuesAndDefault(String value) { + boolean enabled = Boolean.parseBoolean(value); + when(service.restoreDbInstanceFromDbSnapshot(eq("restored"), eq("source"), isNull(), isNull(), + eq(enabled), isNull(), eq(List.of()), eq(Map.of()), isNull(), isNull(), isNull())) + .thenReturn(makeInstance("restored")); + when(service.restoreDbInstanceFromDbSnapshot(eq("restored"), eq("source"), isNull(), isNull(), + eq(enabled), isNull(), eq(List.of()), eq(Map.of()), isNull(), eq(5433), isNull())) + .thenReturn(makeInstance("restored")); + MultivaluedMap p = params(); + p.putSingle("DBInstanceIdentifier", "restored"); + p.putSingle("DBSnapshotIdentifier", "source"); + if (value != null) { + p.putSingle("MultiAZ", value); + } + + assertEquals(200, handler.handle("RestoreDBInstanceFromDBSnapshot", p).getStatus()); + verify(service).restoreDbInstanceFromDbSnapshot("restored", "source", null, null, + enabled, null, List.of(), Map.of(), null, null, null); + p.putSingle("Port", "5433"); + assertEquals(200, handler.handle("RestoreDBInstanceFromDBSnapshot", p).getStatus()); + verify(service).restoreDbInstanceFromDbSnapshot("restored", "source", null, null, + enabled, null, List.of(), Map.of(), null, 5433, null); + } + + @ParameterizedTest + @NullSource + @ValueSource(strings = {"true", "false", "TrUe", "FaLsE"}) + void restoreInstanceSnapshotForwardsIamFlagWithAndWithoutPort(String value) { + Boolean enabled = value == null ? null : Boolean.parseBoolean(value); + when(service.restoreDbInstanceFromDbSnapshot(any(), any(), any(), any(), anyBoolean(), + any(), any(), any(), any(), any(), eq(enabled))).thenReturn(makeInstance("restored")); + MultivaluedMap p = params(); + p.putSingle("DBInstanceIdentifier", "restored"); + p.putSingle("DBSnapshotIdentifier", "source"); + if (value != null) { + p.putSingle("EnableIAMDatabaseAuthentication", value); + } + assertEquals(200, handler.handle("RestoreDBInstanceFromDBSnapshot", p).getStatus()); + verify(service).restoreDbInstanceFromDbSnapshot("restored", "source", null, null, + false, null, List.of(), Map.of(), null, null, enabled); + p.putSingle("Port", "5433"); + assertEquals(200, handler.handle("RestoreDBInstanceFromDBSnapshot", p).getStatus()); + verify(service).restoreDbInstanceFromDbSnapshot("restored", "source", null, null, + false, null, List.of(), Map.of(), null, 5433, enabled); + } + + @ParameterizedTest + @NullSource + @ValueSource(strings = {"true", "false", "TrUe", "FaLsE"}) + void restoreClusterActionsPreserveIamValuesAndOmission(String value) { + Boolean enabled = value == null ? null : Boolean.parseBoolean(value); + when(service.restoreDbClusterFromSnapshot(eq("restored"), eq("snapshot"), eq("aurora-postgresql"), + isNull(), isNull(), isNull(), isNull(), isNull(), isNull(), eq(enabled), isNull(), + eq(Map.of()), isNull())).thenReturn(makeCluster("restored")); + when(service.restoreDbClusterToPointInTime(eq("restored"), eq("source"), isNull(), isNull(), + isNull(), eq(true), isNull(), isNull(), isNull(), eq(enabled), isNull(), + eq(Map.of()), isNull())).thenReturn(makeCluster("restored")); + MultivaluedMap p = params(); + p.putSingle("DBClusterIdentifier", "restored"); + p.putSingle("SnapshotIdentifier", "snapshot"); + p.putSingle("Engine", "aurora-postgresql"); + if (value != null) { + p.putSingle("EnableIAMDatabaseAuthentication", value); + } + + assertEquals(200, handler.handle("RestoreDBClusterFromSnapshot", p).getStatus()); + verify(service).restoreDbClusterFromSnapshot("restored", "snapshot", "aurora-postgresql", + null, null, null, null, null, null, enabled, null, Map.of(), null); + p.putSingle("SourceDBClusterIdentifier", "source"); + p.putSingle("UseLatestRestorableTime", "true"); + assertEquals(200, handler.handle("RestoreDBClusterToPointInTime", p).getStatus()); + verify(service).restoreDbClusterToPointInTime("restored", "source", null, null, null, true, + null, null, null, enabled, null, Map.of(), null); + } + @Test void restoreDbInstanceFromDbSnapshot_success() { DbInstance instance = makeInstance("mydb"); - when(service.restoreDbInstanceFromDbSnapshot(eq("mydb"), eq("mysnap"), eq("db.t3.large"), eq("us-east-1a"), eq(true), eq("my-subnets"), eq(List.of("sg-123")), eq(Map.of("Env", "Prod")), isNull())) + when(service.restoreDbInstanceFromDbSnapshot(eq("mydb"), eq("mysnap"), eq("db.t3.large"), eq("us-east-1a"), eq(true), eq("my-subnets"), eq(List.of("sg-123")), eq(Map.of("Env", "Prod")), isNull(), isNull(), isNull())) .thenReturn(instance); MultivaluedMap p = params(); 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 c2031581f0..411cd87228 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 @@ -3954,6 +3954,36 @@ class RdsServiceTest { verify(containerManager).createPostgresSnapshot(any(), eq("admin")); } + @ParameterizedTest + @CsvSource({"true, , false, ", "true, false, false, ", "true, true, true, ", + "false, , false, ", "false, false, false, ", "false, true, true, ", + "true, , false, 5433", "true, false, false, 5433", "true, true, true, 5433", + "false, , false, 5433", "false, false, false, 5433", "false, true, true, 5433"}) + void instanceSnapshotRestoreIamDefaultsToDisabled(boolean sourceEnabled, Boolean requested, + boolean expected, Integer port) { + DbInstance source = rdsService.createDbInstance("iam-source", "postgres", "16.3", + "admin", "password", "dbname", "db.t3.micro", 20, sourceEnabled, + null, null, null); + when(containerManager.createPostgresSnapshot(any(), eq("admin"))).thenReturn("DUMP"); + DbSnapshot snapshot = rdsService.createDbSnapshot("iam-snapshot", "iam-source"); + clearInvocations(proxyManager); + + DbInstance restored = rdsService.restoreDbInstanceFromDbSnapshot("iam-restored", "iam-snapshot", + null, null, false, null, List.of(), Map.of(), "us-east-1", port, requested); + + assertEquals(expected, restored.isIamDatabaseAuthenticationEnabled()); + assertEquals(expected, rdsService.getDbInstance("iam-restored").isIamDatabaseAuthenticationEnabled()); + assertEquals(sourceEnabled, source.isIamDatabaseAuthenticationEnabled()); + assertEquals(sourceEnabled, snapshot.isIamDatabaseAuthenticationEnabled()); + verify(proxyManager).startProxy(any(), any(), eq(expected), anyInt(), any(), anyInt(), + any(), any(), any(), any(), any(), any()); + if (requested == null) { + DbInstance legacy = rdsService.restoreDbInstanceFromDbSnapshot("iam-legacy", "iam-snapshot", + null, null, false, null, List.of(), Map.of(), "us-east-1"); + assertFalse(legacy.isIamDatabaseAuthenticationEnabled()); + } + } + @Test void createCopyAndRestoreDbSnapshotPreserveEncryptionWithoutExplicitKmsKey() { DbInstance sourceInstance = rdsService.createDbInstance("mydb", "postgres", "13", @@ -4267,6 +4297,35 @@ class RdsServiceTest { () -> rdsService.describeDbClusterSnapshots("absent", null, null)).getErrorCode()); } + @ParameterizedTest + @CsvSource({ + "snapshot, true, , false", "snapshot, true, false, false", "snapshot, true, true, true", + "snapshot, false, , false", "snapshot, false, false, false", "snapshot, false, true, true", + "point, true, , false", "point, true, false, false", "point, true, true, true", + "point, false, , false", "point, false, false, false", "point, false, true, true" + }) + void clusterRestoreIamDefaultsToDisabled(String path, boolean sourceEnabled, Boolean requested, + boolean expected) { + DbCluster source = rdsService.createDbCluster("iam-source", "aurora-postgresql", "16.3", + "admin", "password", "dbname", sourceEnabled, null); + when(containerManager.createPostgresSnapshot(any(), eq("admin"))).thenReturn("CLUSTER_DUMP"); + DbClusterSnapshot snapshot = rdsService.createDbClusterSnapshot("iam-snapshot", "iam-source", null); + clearInvocations(proxyManager); + + DbCluster restored = "snapshot".equals(path) + ? rdsService.restoreDbClusterFromSnapshot("iam-restored", "iam-snapshot", "aurora-postgresql", + null, null, null, null, null, null, requested, null, null, "us-east-1") + : rdsService.restoreDbClusterToPointInTime("iam-restored", "iam-source", null, null, + null, true, null, null, null, requested, null, null, "us-east-1"); + + assertEquals(expected, restored.isIamDatabaseAuthenticationEnabled()); + assertEquals(expected, rdsService.getDbCluster("iam-restored").isIamDatabaseAuthenticationEnabled()); + assertEquals(sourceEnabled, source.isIamDatabaseAuthenticationEnabled()); + assertEquals(sourceEnabled, snapshot.isIamDatabaseAuthenticationEnabled()); + verify(proxyManager).startProxy(any(), any(), eq(expected), anyInt(), any(), anyInt(), + any(), any(), any(), any(), any(), any()); + } + @Test void clusterSnapshotCanBeCopiedRestoredAndDeleted() { DbClusterSnapshot source = clusterSnapshotOfNewCluster("csnap"); diff --git a/src/test/java/io/github/hectorvent/floci/testing/RdsRequests.java b/src/test/java/io/github/hectorvent/floci/testing/RdsRequests.java new file mode 100644 index 0000000000..ba7c1b7c78 --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/testing/RdsRequests.java @@ -0,0 +1,16 @@ +package io.github.hectorvent.floci.testing; + +import io.restassured.specification.RequestSpecification; + +import static io.restassured.RestAssured.given; +import static io.restassured.http.ContentType.URLENC; + +public final class RdsRequests { + private RdsRequests() {} + + public static RequestSpecification rds(String action) { + return given().header("Authorization", + "AWS4-HMAC-SHA256 Credential=test/20261011/us-east-1/rds/aws4_request") + .contentType(URLENC).formParam("Action", action).formParam("Version", "2014-10-31"); + } +}