fix(rds): honor restore IAM flags and reject malformed booleans (#5473)

* fix(rds): reject malformed restore flags

* fix(rds): disable IAM authentication by default on restore

* fix(rds): honor IAM settings on instance snapshot restore
This commit is contained in:
David Phillips authored and GitHub committed 2026-10-11 23:25:34 -04:00
1 parent dd6bf4cfcb
commit 03a0dfefdd
10 files changed
+489 -15

No files matched your search

@@ -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("<IAMDatabaseAuthenticationEnabled>" + expected
+ "</IAMDatabaseAuthenticationEnabled>"));
rds("DescribeDBClusters").formParam("DBClusterIdentifier", target).post("/").then().statusCode(200)
.body(containsString("<IAMDatabaseAuthenticationEnabled>" + expected
+ "</IAMDatabaseAuthenticationEnabled>"));
rds("DescribeDBClusters").formParam("DBClusterIdentifier", source).post("/").then().statusCode(200)
.body(containsString("<IAMDatabaseAuthenticationEnabled>true</IAMDatabaseAuthenticationEnabled>"));
} finally {
for (String id : new String[]{target, source}) {
rds("DeleteDBCluster").formParam("DBClusterIdentifier", id)
.formParam("SkipFinalSnapshot", "true").post("/");
}
rds("DeleteDBClusterSnapshot").formParam("DBClusterSnapshotIdentifier", snapshot).post("/");
}
}
}
@@ -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(
"<IAMDatabaseAuthenticationEnabled>" + expected + "</IAMDatabaseAuthenticationEnabled>"));
rds("DescribeDBInstances").formParam("DBInstanceIdentifier", target).post("/").then()
.statusCode(200).body(containsString("<IAMDatabaseAuthenticationEnabled>" + expected
+ "</IAMDatabaseAuthenticationEnabled>"));
}
rds("DescribeDBInstances").formParam("DBInstanceIdentifier", source).post("/").then()
.statusCode(200).body(containsString("<IAMDatabaseAuthenticationEnabled>" + sourceEnabled
+ "</IAMDatabaseAuthenticationEnabled>"));
} finally {
for (String id : new String[]{target, source}) {
rds("DeleteDBInstance").formParam("DBInstanceIdentifier", id)
.formParam("SkipFinalSnapshot", true).post("/");
}
rds("DeleteDBSnapshot").formParam("DBSnapshotIdentifier", snapshot).post("/");
}
}
}
@@ -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<String, String> 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("<Code>InvalidParameterValue</Code>"));
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<String, String> 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<String, String> 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<String, String> 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<String, String> p = params();
@@ -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");
@@ -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");
}
}