diff --git a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/RdsControlPlaneTest.java b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/RdsControlPlaneTest.java index 3bbc75878f..72919c5f54 100644 --- a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/RdsControlPlaneTest.java +++ b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/RdsControlPlaneTest.java @@ -32,6 +32,7 @@ import software.amazon.awssdk.services.rds.model.DescribeDbSubnetGroupsResponse; import software.amazon.awssdk.services.rds.model.DescribeOptionGroupsResponse; import software.amazon.awssdk.services.rds.model.DescribeOrderableDbInstanceOptionsResponse; import software.amazon.awssdk.services.rds.model.InvalidOptionGroupStateException; +import software.amazon.awssdk.services.rds.model.InvalidRestoreException; import software.amazon.awssdk.services.rds.model.ListTagsForResourceResponse; import software.amazon.awssdk.services.rds.model.ModifyDbClusterResponse; import software.amazon.awssdk.services.rds.model.ModifyDbProxyResponse; @@ -44,6 +45,7 @@ import software.amazon.awssdk.services.rds.model.RdsException; import software.amazon.awssdk.services.rds.model.RegisterDbProxyTargetsResponse; import software.amazon.awssdk.services.rds.model.Tag; +import java.time.Instant; import java.util.List; import java.util.logging.Level; import java.util.logging.Logger; @@ -611,6 +613,69 @@ class RdsControlPlaneTest { } } + @Test + @DisplayName("Point in time restore of an instance and a cluster, and the restorable times") + void sdkRestoresAnInstanceAndAClusterToAPointInTime() { + String instanceName = TestFixtures.uniqueName("rds-pitr-db"); + String restoredInstance = instanceName + "-restored"; + String clusterName = TestFixtures.uniqueName("rds-pitr-cluster"); + String restoredCluster = clusterName + "-restored"; + try { + createDbInstance(rds, instanceName, "pitr-secret"); + DBInstance source = rds.describeDBInstances(b -> b.dbInstanceIdentifier(instanceName)) + .dbInstances().get(0); + assertThat(source.latestRestorableTime()).isNotNull(); + + DBInstance restored = rds.restoreDBInstanceToPointInTime(b -> b + .sourceDBInstanceIdentifier(instanceName) + .targetDBInstanceIdentifier(restoredInstance) + .useLatestRestorableTime(true) + .dbInstanceClass("db.t3.small")) + .dbInstance(); + assertThat(restored.dbInstanceIdentifier()).isEqualTo(restoredInstance); + assertThat(restored.dbInstanceClass()).isEqualTo("db.t3.small"); + assertThat(restored.dbName()).isEqualTo("app"); + + assertThatThrownBy(() -> rds.restoreDBInstanceToPointInTime(b -> b + .sourceDBInstanceIdentifier(instanceName) + .targetDBInstanceIdentifier(instanceName + "-early") + // Before the source existed, so outside its restorable window. + .restoreTime(Instant.now().minusSeconds(3 * 86400)))) + .isInstanceOf(InvalidRestoreException.class); + + rds.createDBCluster(b -> b + .dbClusterIdentifier(clusterName) + .engine("aurora-postgresql") + .engineVersion("16.3") + .masterUsername("admin") + .masterUserPassword("pitr-secret") + .databaseName("app")); + DBCluster sourceCluster = rds.describeDBClusters(b -> b.dbClusterIdentifier(clusterName)) + .dbClusters().get(0); + assertThat(sourceCluster.earliestRestorableTime()).isNotNull(); + assertThat(sourceCluster.latestRestorableTime()).isAfterOrEqualTo(sourceCluster.earliestRestorableTime()); + + DBCluster restoredFromTime = rds.restoreDBClusterToPointInTime(b -> b + .dbClusterIdentifier(restoredCluster) + .sourceDBClusterIdentifier(sourceCluster.dbClusterArn()) + .restoreToTime(sourceCluster.earliestRestorableTime()) + .restoreType("copy-on-write")) + .dbCluster(); + assertThat(restoredFromTime.dbClusterIdentifier()).isEqualTo(restoredCluster); + assertThat(restoredFromTime.databaseName()).isEqualTo("app"); + } finally { + deleteDbInstance(rds, restoredInstance); + deleteDbInstance(rds, instanceName); + for (String name : List.of(restoredCluster, clusterName)) { + try { + rds.deleteDBCluster(b -> b.dbClusterIdentifier(name).skipFinalSnapshot(true)); + } catch (Exception e) { + LOG.log(Level.WARNING, "Failed to clean up RDS cluster " + name, e); + } + } + } + } + private static CreateDbProxyResponse createIamProxy(RdsClient client, String name, List vpcSubnetIds) { return client.createDBProxy(b -> b .dbProxyName(name) diff --git a/docs/services/rds.md b/docs/services/rds.md index 6fc369a02c..d1ebcdc2d3 100644 --- a/docs/services/rds.md +++ b/docs/services/rds.md @@ -97,6 +97,7 @@ RDS Data API (`rds-data`) is documented separately because it uses REST JSON rou | `CopyDBSnapshot` | Copy an available DB snapshot, optionally copying tags or overriding the option group and KMS key | | `ModifyDBSnapshot` | Change the engine version or option group of an available manual DB snapshot | | `RestoreDBInstanceFromDBSnapshot` | Create a new DB instance from a snapshot | +| `RestoreDBInstanceToPointInTime` | Create a new DB instance from a source instance at a time in its restorable window; see [Point in time restore](#point-in-time-restore) | | `DescribeDBSnapshots` | List DB instance snapshots | | `DescribeDBSnapshotAttributes` | Return a snapshot's `restore` attribute (accounts authorized to copy/restore it) | | `ModifyDBSnapshotAttribute` | Add or remove accounts authorized to copy/restore a snapshot | @@ -114,6 +115,7 @@ RDS Data API (`rds-data`) is documented separately because it uses REST JSON rou | `DeleteDBClusterSnapshot` | Delete an available cluster snapshot and its data; the response carries `Status` `deleted` | | `CopyDBClusterSnapshot` | Copy an available cluster snapshot (by identifier or same-region ARN) to a new manual one with its data; `CopyTags` and `Tags`; the copy reports `SourceDBClusterSnapshotArn` | | `RestoreDBClusterFromSnapshot` | Create a cluster from a cluster snapshot's settings and data; `Engine` must match the snapshot's | +| `RestoreDBClusterToPointInTime` | Create a cluster from a source cluster at a time in its restorable window; see [Point in time restore](#point-in-time-restore) | | `DescribeDBClusterSnapshotAttributes` | Return the `restore` attribute of a cluster snapshot | | `ModifyDBClusterSnapshotAttribute` | Add or remove `restore` values (account ids or `all`) on a cluster snapshot | | `DescribeGlobalClusters` | List the account's global clusters with their primary and secondary members; see [Global clusters](#global-clusters) | @@ -209,8 +211,8 @@ they belong to, as the model requires. An omitted `Enabled` activates the subscr RDS requires the Docker socket and port range exposure. For private registry authentication and other Docker settings see [Docker Configuration](../configuration/docker.md). -`CreateDBInstance`, `CreateDBCluster`, `RestoreDBInstanceFromDBSnapshot`, and -`RestoreDBClusterFromSnapshot` honor a requested `Port` only within the configured +`CreateDBInstance`, `CreateDBCluster`, `RestoreDBInstanceFromDBSnapshot`, +`RestoreDBClusterFromSnapshot` and the two point in time restores honor a requested `Port` only within the configured RDS proxy range. A port outside that range causes Floci to assign the next free port and return it in the endpoint. A port inside the range that is already in use is rejected. This differs from AWS because Floci's local proxy must use a published @@ -526,6 +528,34 @@ same-region replicas; a cross-region replica keeps its link with the replication `terminated` until it is promoted or deleted, which is what AWS does for PostgreSQL. Deleting a replica drops it from its source's list. +## Point in time restore + +`RestoreDBInstanceToPointInTime` and `RestoreDBClusterToPointInTime` check the request as AWS does: +exactly one of `RestoreTime` (`RestoreToTime` for a cluster) and `UseLatestRestorableTime`, and a +time inside the source's restorable window, or `InvalidRestoreFault`. The window is what the describe +calls report. An instance's runs from the later of its creation and the start of its backup +retention period up to `LatestRestorableTime` on `DescribeDBInstances`; an instance with +`BackupRetentionPeriod` 0 reports no `LatestRestorableTime` and is refused with +`PointInTimeRestoreNotEnabled`. Floci does not model a cluster's backup retention, so a cluster's +window runs from its creation (`EarliestRestorableTime`) to `LatestRestorableTime` on +`DescribeDBClusters`. + +An instance source is named by `SourceDBInstanceIdentifier` or `SourceDbiResourceId`, a cluster +source by `SourceDBClusterIdentifier` (identifier or ARN) or `SourceDbClusterResourceId`. Restoring +from retained automated backups (`SourceDBInstanceAutomatedBackupsArn`, used for a deleted instance) +is not supported, since Floci keeps no automated backups, and answers +`DBInstanceAutomatedBackupNotFound`. A restored instance takes the source's engine, version, +credentials, database name, storage and encryption, and its instance class unless the request names +one; everything else is the request's or the default a new instance gets. A restored cluster takes +the source's engine, version, credentials, database name and encryption, and has no instances until +they are added with `CreateDBInstance`, as on AWS. + +Floci keeps no transaction log, so it cannot rebuild a database as it was at an earlier time. The +target is seeded from a `pg_dumpall` of the source taken when the request arrives, the mechanism read +replicas use, so the restored data is the source's latest state whatever time in the window is +asked for; this is also why `LatestRestorableTime` is always the current time. As with +`CreateDBSnapshot`, only PostgreSQL sources are accepted. + ## Global clusters `CreateGlobalCluster` creates an Aurora global database (`aurora-mysql` or `aurora-postgresql`), 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 01eba60703..d0552c3e38 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 @@ -30,6 +30,7 @@ import io.github.hectorvent.floci.services.rds.model.GlobalCluster; import io.github.hectorvent.floci.services.rds.model.GlobalClusterMember; import io.github.hectorvent.floci.services.rds.model.OptionGroup; import io.github.hectorvent.floci.services.rds.model.OptionGroupOption; +import io.github.hectorvent.floci.services.rds.model.PointInTimeRestoreRequest; import io.github.hectorvent.floci.services.rds.model.RdsEvent; import io.github.hectorvent.floci.services.rds.model.ReadReplicaRequest; import jakarta.enterprise.context.ApplicationScoped; @@ -38,6 +39,7 @@ import jakarta.ws.rs.core.MultivaluedMap; import jakarta.ws.rs.core.Response; import org.jboss.logging.Logger; +import java.time.Instant; import java.util.ArrayList; import java.util.Collection; import java.util.LinkedHashMap; @@ -131,6 +133,7 @@ public class RdsQueryHandler { case "CopyDBSnapshot" -> handleCopyDbSnapshot(params, region); case "ModifyDBSnapshot" -> handleModifyDbSnapshot(params, region); case "RestoreDBInstanceFromDBSnapshot" -> handleRestoreDbInstanceFromDbSnapshot(params, region); + case "RestoreDBInstanceToPointInTime" -> handleRestoreDbInstanceToPointInTime(params, region); case "DescribeDBSnapshots" -> handleDescribeDbSnapshots(params, region); case "DescribeDBSnapshotAttributes" -> handleDescribeDbSnapshotAttributes(params, region); case "ModifyDBSnapshotAttribute" -> handleModifyDbSnapshotAttribute(params, region); @@ -148,6 +151,7 @@ public class RdsQueryHandler { case "DeleteDBClusterSnapshot" -> handleDeleteDbClusterSnapshot(params, region); case "CopyDBClusterSnapshot" -> handleCopyDbClusterSnapshot(params, region); case "RestoreDBClusterFromSnapshot" -> handleRestoreDbClusterFromSnapshot(params, region); + case "RestoreDBClusterToPointInTime" -> handleRestoreDbClusterToPointInTime(params, region); case "DescribeDBClusterSnapshotAttributes" -> handleDescribeDbClusterSnapshotAttributes(params, region); case "ModifyDBClusterSnapshotAttribute" -> handleModifyDbClusterSnapshotAttribute(params, region); case "DescribeGlobalClusters" -> handleDescribeGlobalClusters(params); @@ -2203,6 +2207,63 @@ public class RdsQueryHandler { dbClusterSnapshotXml(copy))).build(); } + private Response handleRestoreDbInstanceToPointInTime(MultivaluedMap params, String region) { + try { + PointInTimeRestoreRequest request = new PointInTimeRestoreRequest( + params.getFirst("TargetDBInstanceIdentifier"), + params.getFirst("SourceDBInstanceIdentifier"), + params.getFirst("SourceDbiResourceId"), + params.getFirst("SourceDBInstanceAutomatedBackupsArn"), + parseOptionalInstant(params.getFirst("RestoreTime")), + parseOptionalBoolean(params, "UseLatestRestorableTime"), + params.getFirst("DBInstanceClass"), + parseIntegerParam(params, "Port"), + params.getFirst("AvailabilityZone"), + parseOptionalBoolean(params, "MultiAZ"), + parseOptionalBoolean(params, "AutoMinorVersionUpgrade"), + params.getFirst("DBName"), + params.getFirst("OptionGroupName"), + params.getFirst("DBParameterGroupName"), + parseOptionalBoolean(params, "PubliclyAccessible"), + params.getFirst("DBSubnetGroupName"), + hasMemberKeys(params, "VpcSecurityGroupIds") ? vpcSecurityGroupIds(params) : null, + parseOptionalBoolean(params, "CopyTagsToSnapshot"), + parseOptionalBoolean(params, "EnableIAMDatabaseAuthentication"), + parseIntegerParam(params, "BackupRetentionPeriod"), + params.getFirst("PreferredBackupWindow"), + parseOptionalBoolean(params, "DeletionProtection"), + parseTags(params)); + DbInstance instance = service.restoreDbInstanceToPointInTime(request, region); + return Response.ok(AwsQueryResponse.envelope( + "RestoreDBInstanceToPointInTime", AwsNamespaces.RDS, dbInstanceXml(instance))).build(); + } catch (AwsException e) { + return AwsQueryResponse.error(e.getErrorCode(), e.getMessage(), AwsNamespaces.RDS, e.getHttpStatus()); + } + } + + private Response handleRestoreDbClusterToPointInTime(MultivaluedMap params, String region) { + String clusterId = params.getFirst("DBClusterIdentifier"); + Response missing = firstMissingParam("DBClusterIdentifier", clusterId); + if (missing != null) { + return missing; + } + try { + DbCluster cluster = service.restoreDbClusterToPointInTime(clusterId, + params.getFirst("SourceDBClusterIdentifier"), params.getFirst("SourceDbClusterResourceId"), + params.getFirst("RestoreType"), + parseOptionalInstant(params.getFirst("RestoreToTime")), + parseOptionalBoolean(params, "UseLatestRestorableTime"), + optionalInt(params.getFirst("Port")), params.getFirst("DBSubnetGroupName"), + params.getFirst("DBClusterParameterGroupName"), + optionalBoolean(params.getFirst("EnableIAMDatabaseAuthentication")), + params.getFirst("EngineMode"), parseTags(params), region); + return Response.ok(AwsQueryResponse.envelope("RestoreDBClusterToPointInTime", AwsNamespaces.RDS, + dbClusterXml(cluster))).build(); + } catch (AwsException e) { + return AwsQueryResponse.error(e.getErrorCode(), e.getMessage(), AwsNamespaces.RDS, e.getHttpStatus()); + } + } + private Response handleRestoreDbClusterFromSnapshot(MultivaluedMap params, String region) { String clusterId = params.getFirst("DBClusterIdentifier"); String snapshotId = params.getFirst("SnapshotIdentifier"); @@ -2386,6 +2447,10 @@ public class RdsQueryHandler { .elem("MonitoringInterval", i.getMonitoringInterval()) .elem("PerformanceInsightsEnabled", i.isPerformanceInsightsEnabled()) .elem("EngineLifecycleSupport", i.getEngineLifecycleSupport()); + if (i.getBackupRetentionPeriod() > 0) { + // Point in time restore copies the source's current data, so the latest restorable time is now. + xml.elem("LatestRestorableTime", Instant.now().toString()); + } if (i.getMonitoringRoleArn() != null && !i.getMonitoringRoleArn().isBlank()) { xml.elem("MonitoringRoleArn", i.getMonitoringRoleArn()); } @@ -2569,6 +2634,11 @@ public class RdsQueryHandler { if (readerEp != null) { xml.elem("ReaderEndpoint", readerEp.address()); } + if (c.getCreatedAt() != null) { + // The restorable window runs from creation to now; see RestoreDBClusterToPointInTime. + xml.elem("EarliestRestorableTime", c.getCreatedAt().toString()) + .elem("LatestRestorableTime", Instant.now().toString()); + } xml.elem("IAMDatabaseAuthenticationEnabled", c.isIamDatabaseAuthenticationEnabled()) .elem("MultiAZ", c.isMultiAz()) .elem("StorageEncrypted", c.isStorageEncrypted()) 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 28fba514b2..cb1905140a 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 @@ -46,6 +46,7 @@ import io.github.hectorvent.floci.services.rds.model.DbProxy; import io.github.hectorvent.floci.services.rds.model.DbProxyAuth; import io.github.hectorvent.floci.services.rds.model.DbProxyTarget; import io.github.hectorvent.floci.services.rds.model.DbProxyTargetGroup; +import io.github.hectorvent.floci.services.rds.model.PointInTimeRestoreRequest; import io.github.hectorvent.floci.services.rds.model.RdsEvent; import io.github.hectorvent.floci.services.rds.model.ReadReplicaRequest; import io.github.hectorvent.floci.services.rds.model.DbSnapshot; @@ -1180,6 +1181,188 @@ public class RdsService implements Resettable, ResourceProvider { return instance; } + // ── Point in time restore ───────────────────────────────────────────────── + + // The RDS rule for a PostgreSQL DBName: a letter, then letters, digits or underscores, 63 at most. + private static final Pattern POSTGRES_DB_NAME = Pattern.compile("[A-Za-z][A-Za-z0-9_]{0,62}"); + + /** + * The earliest time a DB instance can be restored to: the later of its creation and the start + * of its backup retention period. Floci restores from the source's current data (see + * {@link #restoreDbInstanceToPointInTime}), so the latest restorable time is always now. + */ + public static Instant earliestRestorableTime(DbInstance instance, Instant now) { + Instant retentionStart = now.minus(Duration.ofDays(instance.getBackupRetentionPeriod())); + Instant created = instance.getCreatedAt(); + return created != null && created.isAfter(retentionStart) ? created : retentionStart; + } + + /** + * Creates a new DB instance from a source instance as of a time in its restorable window. + * + *

Floci keeps no transaction log, so it cannot rebuild the source as it was at an earlier + * time. The requested time is checked against the window as AWS checks it, and the new + * instance is then seeded from a dump of the source taken now, the mechanism read replicas + * use. The restored data is therefore the source's latest state for any time in the window. + */ + public DbInstance restoreDbInstanceToPointInTime(PointInTimeRestoreRequest request, String region) { + String effectiveRegion = effectiveRegion(region); + String targetId = request.targetDbInstanceIdentifier(); + if (targetId == null || targetId.isBlank()) { + throw new AwsException("InvalidParameterValue", "TargetDBInstanceIdentifier is required.", 400); + } + DbInstance source = resolvePointInTimeSource(request, effectiveRegion); + String sourceId = source.getDbInstanceIdentifier(); + if (source.getDbClusterIdentifier() != null && !source.getDbClusterIdentifier().isBlank()) { + throw new AwsException("InvalidParameterCombination", + "DB instance " + sourceId + " belongs to DB cluster " + source.getDbClusterIdentifier() + + ". Use RestoreDBClusterToPointInTime to restore the cluster.", 400); + } + if (source.getStatus() != DbInstanceStatus.AVAILABLE) { + throw new AwsException("InvalidDBInstanceState", + "DB instance " + sourceId + " is not in available state.", 400); + } + if (source.getBackupRetentionPeriod() <= 0) { + throw new AwsException("PointInTimeRestoreNotEnabled", + "Point-in-time restore is not enabled for DB instance " + sourceId + + " because its backup retention period is 0.", 400); + } + Instant now = Instant.now(); + requireRestorableTime(request.restoreTime(), request.useLatestRestorableTime(), + earliestRestorableTime(source, now), now, "RestoreTime"); + if (source.getEngine() != DatabaseEngine.POSTGRES) { + // CreateDBSnapshot draws the same line: the copy of the source is pg_dumpall based. + throw new AwsException("InvalidDBInstanceState", + "Operation RestoreDBInstanceToPointInTime is not supported for engine " + + source.getEngine() + ".", 400); + } + + String dbName = firstNonBlank(request.dbName(), source.getDbName()); + // The database the source's dump carries: its DBName, or, without one, the database the + // image names after the master user. + String sourceDatabase = firstNonBlank(source.getDbName(), + firstNonBlank(source.getMasterUsername(), "postgres")); + boolean renameDatabase = !isBlank(request.dbName()) && !request.dbName().equals(sourceDatabase); + if (renameDatabase && !POSTGRES_DB_NAME.matcher(request.dbName()).matches()) { + throw new AwsException("InvalidParameterValue", + "DBName must begin with a letter and contain only letters, digits and underscores, " + + "up to 63 characters.", 400); + } + + String engineParam = source.getEngineIdentifier() != null + ? source.getEngineIdentifier() : source.getEngine().name().toLowerCase(Locale.ROOT); + // Encryption follows the source, as it does for a snapshot restore; backups and windows + // not in the request take the defaults a new instance gets. + DbInstanceSettings settings = new DbInstanceSettings( + source.isStorageEncrypted() ? Boolean.TRUE : null, + source.isStorageEncrypted() ? source.getKmsKeyId() : null, + request.backupRetentionPeriod(), request.preferredBackupWindow(), null, + request.copyTagsToSnapshot()); + DbInstance instance = createDbInstance(targetId, engineParam, source.getEngineVersion(), + source.getMasterUsername(), source.getMasterPassword(), dbName, + firstNonBlank(request.dbInstanceClass(), source.getDbInstanceClass()), + source.getAllocatedStorage(), + Boolean.TRUE.equals(request.iamDatabaseAuthenticationEnabled()), + request.dbParameterGroupName(), request.dbSubnetGroupName(), null, + request.availabilityZone(), Boolean.TRUE.equals(request.multiAz()), false, null, + request.tags() != null ? request.tags() : Map.of(), + request.vpcSecurityGroupIds() != null ? request.vpcSecurityGroupIds() : List.of(), + request.optionGroupName(), effectiveRegion, + request.autoMinorVersionUpgrade() == null || request.autoMinorVersionUpgrade(), + // Deletion protection is turned on only once the copy succeeds, so a failed copy can + // still remove the target it created. + settings, request.publiclyAccessible(), request.port(), null); + + if (!config.services().rds().mock()) { + try { + // Without both containers there is nothing to copy from or into, and reporting the + // restore as done would hand back an instance without the source's data. + if (source.getContainerId() == null || instance.getContainerId() == null) { + throw new IllegalStateException("the source or target database container is not running"); + } + String sqlDump = containerManager.createPostgresSnapshot( + source.getContainerId(), source.getMasterUsername()); + containerManager.restorePostgresSnapshot( + instance.getContainerId(), instance.getMasterUsername(), sqlDump); + if (renameDatabase) { + // The dump brings the source's database under its own name. + containerManager.renamePostgresDatabase(instance.getContainerId(), + instance.getMasterUsername(), sourceDatabase, dbName); + } + } catch (Exception e) { + try { + deleteDbInstance(targetId, effectiveRegion); + } catch (RuntimeException cleanupError) { + e.addSuppressed(cleanupError); + } + AwsException failure = new AwsException("InvalidDBInstanceState", + "Failed to restore DB instance " + sourceId + " to " + targetId + ": " + + e.getMessage(), 400); + failure.initCause(e); + throw failure; + } + } + if (Boolean.TRUE.equals(request.deletionProtection())) { + instance.setDeletionProtection(true); + putInstanceForScope(currentAccountId(), effectiveRegion, targetId, instance); + } + LOG.infov("DB instance {0} restored to point in time as {1}", sourceId, targetId); + return instance; + } + + /** + * The source of a point in time restore. A live instance can be named by identifier or by its + * DbiResourceId; an automated backups ARN names retained backups, which Floci does not keep. + */ + private DbInstance resolvePointInTimeSource(PointInTimeRestoreRequest request, String region) { + String byId = request.sourceDbInstanceIdentifier(); + String byResourceId = request.sourceDbiResourceId(); + String byBackupsArn = request.sourceDbInstanceAutomatedBackupsArn(); + int named = (isBlank(byId) ? 0 : 1) + (isBlank(byResourceId) ? 0 : 1) + (isBlank(byBackupsArn) ? 0 : 1); + if (named != 1) { + throw new AwsException("InvalidParameterCombination", + "Specify exactly one of SourceDBInstanceIdentifier, SourceDbiResourceId and " + + "SourceDBInstanceAutomatedBackupsArn.", 400); + } + if (!isBlank(byId)) { + return getDbInstance(byId, region); + } + if (!isBlank(byResourceId)) { + return listDbInstancesByDbiResourceIds(List.of(byResourceId), region).stream() + .findFirst() + .orElseThrow(() -> new AwsException("DBInstanceNotFound", + "DB instance with resource ID " + byResourceId + " not found.", 404)); + } + throw new AwsException("DBInstanceAutomatedBackupNotFound", + "Automated backups " + byBackupsArn + " not found.", 404); + } + + /** + * Exactly one of a restore time and the latest restorable time must be asked for, and a + * restore time must lie in the window from {@code earliest} to {@code latest}. + */ + private static void requireRestorableTime(Instant restoreTime, Boolean useLatest, + Instant earliest, Instant latest, String timeParam) { + boolean latestRequested = Boolean.TRUE.equals(useLatest); + if (restoreTime != null && latestRequested) { + throw new AwsException("InvalidParameterCombination", + timeParam + " cannot be specified when UseLatestRestorableTime is true.", 400); + } + if (restoreTime == null && !latestRequested) { + throw new AwsException("InvalidParameterCombination", + "Specify either " + timeParam + " or UseLatestRestorableTime.", 400); + } + if (restoreTime != null && (restoreTime.isBefore(earliest) || restoreTime.isAfter(latest))) { + throw new AwsException("InvalidRestoreFault", + "The restore time " + restoreTime + " is outside the restorable window from " + + earliest + " to " + latest + ".", 400); + } + } + + private static boolean isBlank(String value) { + return value == null || value.isBlank(); + } + // ── Read replicas ───────────────────────────────────────────────────────── static final String READ_REPLICATION_REPLICATING = "replicating"; @@ -1792,6 +1975,93 @@ public class RdsService implements Resettable, ResourceProvider { return cluster; } + /** + * Creates a new DB cluster from a source cluster as of a time in its restorable window, from + * the cluster's creation to now. Floci does not model a cluster's backup retention period, so + * the window opens at creation. As for an instance, the data is the source's state when the + * request arrives (see {@link #restoreDbInstanceToPointInTime}). Like a cluster snapshot + * restore, this creates the cluster only; instances are added with CreateDBInstance. + */ + public DbCluster restoreDbClusterToPointInTime(String clusterId, String sourceClusterRef, + String sourceClusterResourceId, String restoreType, + Instant restoreToTime, Boolean useLatestRestorableTime, + Integer port, String dbSubnetGroupName, String parameterGroupName, + Boolean iamEnabled, String engineMode, + Map tags, String region) { + String effectiveRegion = effectiveRegion(region); + String accountId = currentAccountId(); + if (isBlank(clusterId)) { + throw new AwsException("InvalidParameterValue", "DBClusterIdentifier is required.", 400); + } + if (isBlank(sourceClusterRef) == isBlank(sourceClusterResourceId)) { + throw new AwsException("InvalidParameterCombination", + "Specify exactly one of SourceDBClusterIdentifier and SourceDbClusterResourceId.", 400); + } + if (restoreType != null && !"full-copy".equals(restoreType) && !"copy-on-write".equals(restoreType)) { + throw new AwsException("InvalidParameterValue", + "RestoreType must be full-copy or copy-on-write.", 400); + } + // SourceDBClusterIdentifier takes the cluster's identifier or its ARN (API reference). + DbCluster source = !isBlank(sourceClusterRef) + ? getDbCluster(clusterIdentifierFromArnOrName(sourceClusterRef, effectiveRegion), effectiveRegion) + : listDbClusters(null, effectiveRegion).stream() + .filter(c -> sourceClusterResourceId.equals(c.getDbClusterResourceId())) + .findFirst() + .orElseThrow(() -> new AwsException("DBClusterNotFoundFault", + "DB cluster with resource ID " + sourceClusterResourceId + " not found.", 404)); + String sourceId = source.getDbClusterIdentifier(); + if (source.getStatus() != null && source.getStatus() != DbInstanceStatus.AVAILABLE) { + throw new AwsException("InvalidDBClusterStateFault", + "DB cluster " + sourceId + " is not in available state.", 400); + } + Instant now = Instant.now(); + Instant earliest = source.getCreatedAt() != null ? source.getCreatedAt() : now; + requireRestorableTime(restoreToTime, useLatestRestorableTime, earliest, now, "RestoreToTime"); + if (source.getEngine() != DatabaseEngine.POSTGRES) { + throw new AwsException("InvalidDBClusterStateFault", + "Operation RestoreDBClusterToPointInTime is not supported for engine " + + source.getEngine() + ".", 400); + } + + String engineParam = source.getEngineIdentifier() != null + ? 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(), + parameterGroupName, dbSubnetGroupName, null, false, effectiveRegion, + null, null, null, false, null, + !isBlank(engineMode) ? engineMode : source.getEngineMode(), + source.isStorageEncrypted(), port); + if (tags != null && !tags.isEmpty()) { + cluster.getTags().putAll(tags); + putClusterForScope(accountId, effectiveRegion, clusterId, cluster); + } + if (!config.services().rds().mock()) { + try { + if (source.getContainerId() == null || cluster.getContainerId() == null) { + throw new IllegalStateException("the source or target database container is not running"); + } + String sqlDump = containerManager.createPostgresSnapshot( + source.getContainerId(), source.getMasterUsername()); + containerManager.restorePostgresSnapshot( + cluster.getContainerId(), cluster.getMasterUsername(), sqlDump); + } catch (Exception e) { + try { + deleteDbCluster(clusterId, effectiveRegion); + } catch (Exception cleanupError) { + e.addSuppressed(cleanupError); + } + AwsException failure = new AwsException("InvalidDBClusterStateFault", + "Failed to restore DB cluster " + sourceId + " to " + clusterId + ": " + + e.getMessage(), 400); + failure.initCause(e); + throw failure; + } + } + LOG.infov("DB cluster {0} restored to point in time as {1}", sourceId, clusterId); + return cluster; + } + private DbClusterSnapshot clusterSnapshotOf(DbCluster cluster, String snapshotId, String region) { DbClusterSnapshot snapshot = new DbClusterSnapshot(); snapshot.setDbClusterSnapshotIdentifier(snapshotId); @@ -1868,6 +2138,27 @@ public class RdsService implements Resettable, ResourceProvider { } } + /** A cluster named by identifier or by ARN; an ARN for another account or Region names no cluster here. */ + private String clusterIdentifierFromArnOrName(String source, String region) { + if (!source.startsWith("arn:")) { + return source; + } + AwsArnUtils.Arn arn; + try { + arn = AwsArnUtils.parse(source); + } catch (IllegalArgumentException malformed) { + throw new AwsException("InvalidParameterValue", "Invalid DB cluster identifier: " + source, 400); + } + String resource = arn.resource(); + if (!"rds".equals(arn.service()) || !resource.startsWith("cluster:")) { + throw new AwsException("InvalidParameterValue", "Invalid DB cluster identifier: " + source, 400); + } + if (!region.equals(arn.region()) || !currentAccountId().equals(arn.accountId())) { + throw new AwsException("DBClusterNotFoundFault", "DB cluster " + source + " not found.", 404); + } + return resource.substring("cluster:".length()); + } + private String clusterSnapshotIdentifierFromArnOrName(String source, String region) { if (source == null || source.isBlank()) { throw new AwsException("InvalidParameterValue", "A DB cluster snapshot identifier is required.", 400); diff --git a/src/main/java/io/github/hectorvent/floci/services/rds/container/RdsContainerManager.java b/src/main/java/io/github/hectorvent/floci/services/rds/container/RdsContainerManager.java index 756a4554e0..009b7fbb41 100644 --- a/src/main/java/io/github/hectorvent/floci/services/rds/container/RdsContainerManager.java +++ b/src/main/java/io/github/hectorvent/floci/services/rds/container/RdsContainerManager.java @@ -947,6 +947,34 @@ public class RdsContainerManager implements RdsBackendGate, Resettable { } } + /** + * Gives a restored database the name a point in time restore asked for. {@code from} is the + * database the source's dump brought over: its DBName, or the master user's name when it had + * none, which is how the image names its default database. The rename runs connected to + * template1, so it also works when {@code from} is {@code postgres}. + */ + public void renamePostgresDatabase(String containerId, String masterUsername, String from, String to) { + String effectiveUser = (masterUsername != null && !masterUsername.isBlank()) ? masterUsername : "postgres"; + String sql = "ALTER DATABASE " + quotePostgresIdentifier(from) + " RENAME TO " + quotePostgresIdentifier(to); + String[] cmd = {"psql", "-v", "ON_ERROR_STOP=1", "-U", effectiveUser, "-d", "template1", "-c", sql}; + try (Lease _ = holdContainer(containerId)) { + ContainerExec.Result result = execInContainer(containerId, cmd, 60); + if (result.exitCode() != 0) { + throw new RuntimeException("Renaming database " + from + " to " + to + " failed with exit code " + + result.exitCode() + ": " + result.stderr()); + } + } catch (RuntimeException e) { + throw e; + } catch (Exception e) { + throw new RuntimeException("Failed to rename database " + from + " to " + to, e); + } + } + + /** A PostgreSQL quoted identifier: wrapped in double quotes, with each embedded one doubled. */ + static String quotePostgresIdentifier(String name) { + return "\"" + name.replace("\"", "\"\"") + "\""; + } + static String postgresRestoreScript() { return """ #!/bin/sh diff --git a/src/main/java/io/github/hectorvent/floci/services/rds/model/PointInTimeRestoreRequest.java b/src/main/java/io/github/hectorvent/floci/services/rds/model/PointInTimeRestoreRequest.java new file mode 100644 index 0000000000..7ba2adba11 --- /dev/null +++ b/src/main/java/io/github/hectorvent/floci/services/rds/model/PointInTimeRestoreRequest.java @@ -0,0 +1,39 @@ +package io.github.hectorvent.floci.services.rds.model; + +import io.quarkus.runtime.annotations.RegisterForReflection; + +import java.time.Instant; +import java.util.List; +import java.util.Map; + +/** + * The RestoreDBInstanceToPointInTime request as the service consumes it. The source is named by + * identifier, DbiResourceId or automated backups ARN; the time by RestoreTime or + * UseLatestRestorableTime. A null override means the API reference's default: the source's + * instance class, and the Region's defaults for everything else. + */ +@RegisterForReflection +public record PointInTimeRestoreRequest(String targetDbInstanceIdentifier, + String sourceDbInstanceIdentifier, + String sourceDbiResourceId, + String sourceDbInstanceAutomatedBackupsArn, + Instant restoreTime, + Boolean useLatestRestorableTime, + String dbInstanceClass, + Integer port, + String availabilityZone, + Boolean multiAz, + Boolean autoMinorVersionUpgrade, + String dbName, + String optionGroupName, + String dbParameterGroupName, + Boolean publiclyAccessible, + String dbSubnetGroupName, + List vpcSecurityGroupIds, + Boolean copyTagsToSnapshot, + Boolean iamDatabaseAuthenticationEnabled, + Integer backupRetentionPeriod, + String preferredBackupWindow, + Boolean deletionProtection, + Map tags) { +} diff --git a/src/test/java/io/github/hectorvent/floci/services/rds/RdsPointInTimeRestoreIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/rds/RdsPointInTimeRestoreIntegrationTest.java new file mode 100644 index 0000000000..5d3d284c7c --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/rds/RdsPointInTimeRestoreIntegrationTest.java @@ -0,0 +1,398 @@ +package io.github.hectorvent.floci.services.rds; + +import com.github.dockerjava.api.DockerClient; +import com.github.dockerjava.api.model.Frame; +import com.github.dockerjava.api.model.StreamType; +import com.github.dockerjava.core.command.ExecStartResultCallback; +import io.github.hectorvent.floci.services.rds.model.DbCluster; +import io.github.hectorvent.floci.services.rds.model.DbInstance; +import io.quarkus.test.junit.QuarkusTest; +import io.restassured.path.xml.XmlPath; +import io.restassured.specification.RequestSpecification; +import jakarta.inject.Inject; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Tag; +import org.junit.jupiter.api.Test; + +import java.io.ByteArrayOutputStream; +import java.io.Closeable; +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.time.Duration; +import java.time.Instant; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; + +import static io.restassured.RestAssured.given; +import static io.restassured.http.ContentType.URLENC; +import static org.hamcrest.Matchers.containsString; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assumptions.assumeTrue; + +/** + * RestoreDBInstanceToPointInTime and RestoreDBClusterToPointInTime against real Postgres + * containers: a row in the source comes back from the restored target, the describe calls report + * the restorable window, and each refusal carries the API reference's error. + */ +@QuarkusTest +@Tag("docker") +class RdsPointInTimeRestoreIntegrationTest { + + private static final String AUTH = + "AWS4-HMAC-SHA256 Credential=test/20261002/us-east-1/rds/aws4_request, " + + "SignedHeaders=content-type;host, Signature=test"; + + @Inject + DockerClient dockerClient; + + @Inject + RdsService rdsService; + + @BeforeEach + void checkDocker() { + try { + dockerClient.pingCmd().exec(); + } catch (Exception e) { + assumeTrue(false, "Docker is not available: " + e.getMessage()); + } + } + + private static RequestSpecification query(String action) { + return given().header("Authorization", AUTH) + .contentType(URLENC) + .formParam("Action", action) + .formParam("Version", "2014-10-31"); + } + + @Test + void instanceRestoresToPointInTimeWithTheSourcesData() throws Exception { + String suffix = Long.toString(System.nanoTime(), 36); + String source = "pitr-src-" + suffix; + String latest = "pitr-latest-" + suffix; + String timed = "pitr-timed-" + suffix; + + query("CreateDBInstance") + .formParam("DBInstanceIdentifier", source) + .formParam("Engine", "postgres") + .formParam("MasterUsername", "admin") + .formParam("MasterUserPassword", "password123") + .formParam("DBName", "appdb") + .formParam("DBInstanceClass", "db.t3.micro") + .formParam("AllocatedStorage", "20") + .when().post("/").then().statusCode(200); + waitForDb(source); + DbInstance sourceInstance = rdsService.getDbInstance(source); + psql(sourceInstance.getContainerId(), "CREATE TABLE marker(v text); INSERT INTO marker VALUES ('source-row');"); + + XmlPath described = query("DescribeDBInstances").formParam("DBInstanceIdentifier", source) + .when().post("/").then().statusCode(200).extract().xmlPath(); + String instancePath = "DescribeDBInstancesResponse.DescribeDBInstancesResult.DBInstances.DBInstance."; + Instant latestRestorable = Instant.parse(described.getString(instancePath + "LatestRestorableTime")); + assertTrue(!latestRestorable.isBefore(sourceInstance.getCreatedAt().minusSeconds(1))); + String dbiResourceId = described.getString(instancePath + "DbiResourceId"); + + query("RestoreDBInstanceToPointInTime") + .formParam("SourceDBInstanceIdentifier", source) + .formParam("TargetDBInstanceIdentifier", latest) + .formParam("UseLatestRestorableTime", "true") + .formParam("DBInstanceClass", "db.t3.small") + .formParam("Tags.Tag.1.Key", "env") + .formParam("Tags.Tag.1.Value", "restore") + .when().post("/").then() + .statusCode(200) + .body(containsString("")) + .body(containsString("" + latest + "")) + .body(containsString("postgres")) + .body(containsString("appdb")) + .body(containsString("admin")) + .body(containsString("db.t3.small")); + waitForDb(latest); + assertEquals("source-row", psql(rdsService.getDbInstance(latest).getContainerId(), + "SELECT v FROM marker;").trim()); + + // A time inside the window, named through the source's DbiResourceId. The copied database + // takes the requested DBName, and protection comes on once the copy is done. + query("RestoreDBInstanceToPointInTime") + .formParam("SourceDbiResourceId", dbiResourceId) + .formParam("TargetDBInstanceIdentifier", timed) + .formParam("RestoreTime", Instant.now().minusSeconds(1).toString()) + .formParam("DBName", "otherdb") + .formParam("DeletionProtection", "true") + .when().post("/").then().statusCode(200) + .body(containsString("db.t3.micro")) + .body(containsString("otherdb")) + .body(containsString("true")); + waitForDb(timed); + assertEquals("source-row", psql(rdsService.getDbInstance(timed).getContainerId(), "otherdb", + "SELECT v FROM marker;").trim()); + query("RestoreDBInstanceToPointInTime") + .formParam("SourceDBInstanceIdentifier", source) + .formParam("TargetDBInstanceIdentifier", "pitr-x-" + suffix) + .formParam("UseLatestRestorableTime", "true") + .formParam("DBName", "1bad-name") + .when().post("/").then().statusCode(400) + .body(containsString("InvalidParameterValue")); + + expectInstanceError(source, latest, "UseLatestRestorableTime", "true", "DBInstanceAlreadyExists"); + expectInstanceError(source, "pitr-x-" + suffix, "RestoreTime", + Instant.now().minus(Duration.ofDays(2)).toString(), "InvalidRestoreFault"); + expectInstanceError(source, "pitr-x-" + suffix, "RestoreTime", + Instant.now().plus(Duration.ofHours(1)).toString(), "InvalidRestoreFault"); + expectInstanceError("pitr-missing-" + suffix, "pitr-x-" + suffix, "UseLatestRestorableTime", "true", + "DBInstanceNotFound"); + query("RestoreDBInstanceToPointInTime") + .formParam("SourceDBInstanceIdentifier", source) + .formParam("TargetDBInstanceIdentifier", "pitr-x-" + suffix) + .formParam("UseLatestRestorableTime", "true") + .formParam("RestoreTime", Instant.now().toString()) + .when().post("/").then().statusCode(400) + .body(containsString("InvalidParameterCombination")); + query("RestoreDBInstanceToPointInTime") + .formParam("SourceDBInstanceIdentifier", source) + .formParam("TargetDBInstanceIdentifier", "pitr-x-" + suffix) + .when().post("/").then().statusCode(400) + .body(containsString("InvalidParameterCombination")); + query("RestoreDBInstanceToPointInTime") + .formParam("SourceDBInstanceAutomatedBackupsArn", + "arn:aws:rds:us-east-1:000000000000:auto-backup:ab-missing") + .formParam("TargetDBInstanceIdentifier", "pitr-x-" + suffix) + .formParam("UseLatestRestorableTime", "true") + .when().post("/").then().statusCode(404) + .body(containsString("DBInstanceAutomatedBackupNotFound")); + + // With backups off there is no restorable window, and no LatestRestorableTime. + query("ModifyDBInstance") + .formParam("DBInstanceIdentifier", source) + .formParam("BackupRetentionPeriod", "0") + .formParam("ApplyImmediately", "true") + .when().post("/").then().statusCode(200); + waitForDb(source); + String withoutBackups = query("DescribeDBInstances").formParam("DBInstanceIdentifier", source) + .when().post("/").then().statusCode(200).extract().asString(); + assertTrue(!withoutBackups.contains("")); + expectInstanceError(source, "pitr-x-" + suffix, "UseLatestRestorableTime", "true", + "PointInTimeRestoreNotEnabled"); + + query("ModifyDBInstance") + .formParam("DBInstanceIdentifier", timed) + .formParam("DeletionProtection", "false") + .formParam("ApplyImmediately", "true") + .when().post("/").then().statusCode(200); + for (String id : new String[] {timed, latest, source}) { + query("DeleteDBInstance").formParam("DBInstanceIdentifier", id) + .when().post("/").then().statusCode(200); + } + } + + @Test + void sourceWithoutDbNameRestoresItsDefaultDatabaseUnderTheRequestedName() throws Exception { + String suffix = Long.toString(System.nanoTime(), 36); + String source = "pitr-nodb-" + suffix; + String restored = "pitr-named-" + suffix; + + // Without DBName the image names the default database after the master user. + query("CreateDBInstance") + .formParam("DBInstanceIdentifier", source) + .formParam("Engine", "postgres") + .formParam("MasterUsername", "admin") + .formParam("MasterUserPassword", "password123") + .formParam("DBInstanceClass", "db.t3.micro") + .formParam("AllocatedStorage", "20") + .when().post("/").then().statusCode(200); + waitForDb(source); + psql(rdsService.getDbInstance(source).getContainerId(), "admin", + "CREATE TABLE marker(v text); INSERT INTO marker VALUES ('default-db-row');"); + + query("RestoreDBInstanceToPointInTime") + .formParam("SourceDBInstanceIdentifier", source) + .formParam("TargetDBInstanceIdentifier", restored) + .formParam("UseLatestRestorableTime", "true") + .formParam("DBName", "named") + .when().post("/").then().statusCode(200) + .body(containsString("named")); + waitForDb(restored); + assertEquals("default-db-row", psql(rdsService.getDbInstance(restored).getContainerId(), "named", + "SELECT v FROM marker;").trim()); + + for (String id : new String[] {restored, source}) { + query("DeleteDBInstance").formParam("DBInstanceIdentifier", id) + .when().post("/").then().statusCode(200); + } + } + + @Test + void clusterRestoresToPointInTimeWithTheSourcesData() throws Exception { + String suffix = Long.toString(System.nanoTime(), 36); + String source = "pitr-csrc-" + suffix; + String restored = "pitr-crest-" + suffix; + String byResourceId = "pitr-cres-" + suffix; + + query("CreateDBCluster") + .formParam("DBClusterIdentifier", source) + .formParam("Engine", "aurora-postgresql") + .formParam("EngineVersion", "16.3") + .formParam("MasterUsername", "admin") + .formParam("MasterUserPassword", "password123") + .formParam("DatabaseName", "appdb") + .when().post("/").then().statusCode(200); + waitForCluster(source); + DbCluster sourceCluster = rdsService.getDbCluster(source); + psql(sourceCluster.getContainerId(), "CREATE TABLE marker(v text); INSERT INTO marker VALUES ('cluster-row');"); + + XmlPath described = query("DescribeDBClusters").formParam("DBClusterIdentifier", source) + .when().post("/").then().statusCode(200).extract().xmlPath(); + String clusterPath = "DescribeDBClustersResponse.DescribeDBClustersResult.DBClusters.DBCluster."; + Instant earliest = Instant.parse(described.getString(clusterPath + "EarliestRestorableTime")); + Instant latest = Instant.parse(described.getString(clusterPath + "LatestRestorableTime")); + assertEquals(sourceCluster.getCreatedAt(), earliest); + assertTrue(!latest.isBefore(earliest.minusSeconds(1))); + String clusterArn = described.getString(clusterPath + "DBClusterArn"); + String resourceId = described.getString(clusterPath + "DbClusterResourceId"); + + // The window's earliest time, with the source named by ARN. + query("RestoreDBClusterToPointInTime") + .formParam("DBClusterIdentifier", restored) + .formParam("SourceDBClusterIdentifier", clusterArn) + .formParam("RestoreToTime", earliest.toString()) + .formParam("RestoreType", "copy-on-write") + .when().post("/").then() + .statusCode(200) + .body(containsString("")) + .body(containsString("" + restored + "")) + .body(containsString("aurora-postgresql")) + .body(containsString("appdb")); + waitForCluster(restored); + assertEquals("cluster-row", psql(rdsService.getDbCluster(restored).getContainerId(), + "SELECT v FROM marker;").trim()); + + query("RestoreDBClusterToPointInTime") + .formParam("DBClusterIdentifier", byResourceId) + .formParam("SourceDbClusterResourceId", resourceId) + .formParam("UseLatestRestorableTime", "true") + .when().post("/").then().statusCode(200); + waitForCluster(byResourceId); + + expectClusterError(source, restored, "UseLatestRestorableTime", "true", "DBClusterAlreadyExistsFault"); + expectClusterError(source, "pitr-cx-" + suffix, "RestoreToTime", + earliest.minusSeconds(60).toString(), "InvalidRestoreFault"); + expectClusterError("pitr-cmissing-" + suffix, "pitr-cx-" + suffix, "UseLatestRestorableTime", "true", + "DBClusterNotFoundFault"); + query("RestoreDBClusterToPointInTime") + .formParam("DBClusterIdentifier", "pitr-cx-" + suffix) + .formParam("SourceDBClusterIdentifier", source) + .formParam("UseLatestRestorableTime", "true") + .formParam("RestoreType", "snapshot") + .when().post("/").then().statusCode(400) + .body(containsString("InvalidParameterValue")); + query("RestoreDBClusterToPointInTime") + .formParam("DBClusterIdentifier", "pitr-cx-" + suffix) + .formParam("SourceDBClusterIdentifier", source) + .formParam("SourceDbClusterResourceId", resourceId) + .formParam("UseLatestRestorableTime", "true") + .when().post("/").then().statusCode(400) + .body(containsString("InvalidParameterCombination")); + + for (String id : new String[] {byResourceId, restored, source}) { + query("DeleteDBCluster").formParam("DBClusterIdentifier", id).formParam("SkipFinalSnapshot", "true") + .when().post("/").then().statusCode(200); + } + } + + private void expectInstanceError(String source, String target, String timeParam, String timeValue, String code) { + query("RestoreDBInstanceToPointInTime") + .formParam("SourceDBInstanceIdentifier", source) + .formParam("TargetDBInstanceIdentifier", target) + .formParam(timeParam, timeValue) + .when().post("/").then() + .body(containsString("" + code + "")); + } + + private void expectClusterError(String source, String target, String timeParam, String timeValue, String code) { + query("RestoreDBClusterToPointInTime") + .formParam("DBClusterIdentifier", target) + .formParam("SourceDBClusterIdentifier", source) + .formParam(timeParam, timeValue) + .when().post("/").then() + .body(containsString("" + code + "")); + } + + private void waitForDb(String dbId) throws Exception { + for (int i = 0; i < 60; i++) { + String status = query("DescribeDBInstances") + .formParam("DBInstanceIdentifier", dbId) + .when().post("/").then() + .extract().xmlPath().getString( + "DescribeDBInstancesResponse.DescribeDBInstancesResult.DBInstances.DBInstance.DBInstanceStatus"); + if ("available".equals(status)) { + return; + } + Thread.sleep(1000); + } + throw new RuntimeException("DB instance " + dbId + " did not become available."); + } + + private void waitForCluster(String clusterId) throws Exception { + for (int i = 0; i < 60; i++) { + String status = query("DescribeDBClusters") + .formParam("DBClusterIdentifier", clusterId) + .when().post("/").then() + .extract().xmlPath().getString( + "DescribeDBClustersResponse.DescribeDBClustersResult.DBClusters.DBCluster.Status"); + if ("available".equals(status)) { + return; + } + Thread.sleep(1000); + } + throw new RuntimeException("DB cluster " + clusterId + " did not become available."); + } + + private String psql(String containerId, String sql) throws Exception { + return psql(containerId, "appdb", sql); + } + + private String psql(String containerId, String database, String sql) throws Exception { + String[] command = {"psql", "-U", "admin", "-d", database, "-tAc", sql}; + String execId = dockerClient.execCreateCmd(containerId) + .withCmd(command) + .withAttachStdout(true) + .withAttachStderr(true) + .exec() + .getId(); + ByteArrayOutputStream output = new ByteArrayOutputStream(); + ByteArrayOutputStream errors = new ByteArrayOutputStream(); + CountDownLatch latch = new CountDownLatch(1); + Closeable callback = dockerClient.execStartCmd(execId).exec(new ExecStartResultCallback() { + @Override + public void onNext(Frame frame) { + try { + if (frame.getStreamType() == StreamType.STDOUT) { + output.write(frame.getPayload()); + } else if (frame.getStreamType() == StreamType.STDERR) { + errors.write(frame.getPayload()); + } + } catch (IOException e) { + throw new IllegalStateException("Failed to capture psql output", e); + } + } + + @Override + public void onComplete() { + latch.countDown(); + } + + @Override + public void onError(Throwable throwable) { + latch.countDown(); + } + }); + try { + assertTrue(latch.await(30, TimeUnit.SECONDS), "Timed out running psql"); + long exitCode = dockerClient.inspectExecCmd(execId).exec().getExitCodeLong(); + assertTrue(exitCode == 0, "psql failed with exit code " + exitCode + ": " + errors.toString(StandardCharsets.UTF_8)); + return output.toString(StandardCharsets.UTF_8); + } finally { + callback.close(); + } + } +} 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 9b6530fda0..e7f7ec3579 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 @@ -37,6 +37,7 @@ import io.github.hectorvent.floci.services.rds.model.GlobalCluster; import io.github.hectorvent.floci.services.rds.model.GlobalClusterMember; import io.github.hectorvent.floci.services.rds.model.OptionGroup; import io.github.hectorvent.floci.services.rds.model.OptionGroupOption; +import io.github.hectorvent.floci.services.rds.model.PointInTimeRestoreRequest; import io.github.hectorvent.floci.services.rds.model.RdsEvent; import io.github.hectorvent.floci.services.rds.model.ReadReplicaRequest; import io.github.hectorvent.floci.services.rds.proxy.RdsAuthProxy; @@ -2489,6 +2490,35 @@ class RdsServiceTest { () -> rdsService.startDbInstance("standalone")).getErrorCode()); } + @Test + void pointInTimeRestoreFailsAndCleansUpWhenTheTargetContainerDidNotStart() { + rdsService.createDbInstance("pitr-source", "postgres", "16", + "admin", "password", "dbname", "db.t3.micro", + 20, false, null, null, null); + rdsService.createDbCluster("pitr-cluster", "aurora-postgresql", "16.3", + "admin", "password", "dbname", false, null); + // The target's container fails to start, so there is nothing to copy the source into. + when(containerManager.tryStart(any(), any(), any(), any(), any(), any(), any(), any(), any())) + .thenReturn(null); + + AwsException instanceFault = assertThrows(AwsException.class, () -> rdsService.restoreDbInstanceToPointInTime( + new PointInTimeRestoreRequest("pitr-target", "pitr-source", null, null, null, true, + null, null, null, null, null, null, null, null, null, null, null, null, null, + null, null, null, Map.of()), + "us-east-1")); + assertEquals("InvalidDBInstanceState", instanceFault.getErrorCode()); + assertEquals("DBInstanceNotFound", assertThrows(AwsException.class, + () -> rdsService.getDbInstance("pitr-target")).getErrorCode()); + + AwsException clusterFault = assertThrows(AwsException.class, () -> rdsService.restoreDbClusterToPointInTime( + "pitr-cluster-target", "pitr-cluster", null, null, null, true, + null, null, null, null, null, Map.of(), "us-east-1")); + assertEquals("InvalidDBClusterStateFault", clusterFault.getErrorCode()); + assertEquals("DBClusterNotFoundFault", assertThrows(AwsException.class, + () -> rdsService.getDbCluster("pitr-cluster-target")).getErrorCode()); + verify(containerManager, never()).createPostgresSnapshot(any(), any()); + } + @Test void stopDbInstanceTakesTheRequestedSnapshotFirst() { rdsService.createDbInstance("standalone", "postgres", "16", diff --git a/src/test/java/io/github/hectorvent/floci/services/rds/container/RdsContainerManagerTest.java b/src/test/java/io/github/hectorvent/floci/services/rds/container/RdsContainerManagerTest.java index 96721ed75c..9cf940a767 100644 --- a/src/test/java/io/github/hectorvent/floci/services/rds/container/RdsContainerManagerTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/rds/container/RdsContainerManagerTest.java @@ -60,6 +60,12 @@ class RdsContainerManagerTest { @TempDir Path tempDir; + @Test + void quotePostgresIdentifierDoublesEmbeddedQuotes() { + assertEquals("\"appdb\"", RdsContainerManager.quotePostgresIdentifier("appdb")); + assertEquals("\"app\"\"db\"", RdsContainerManager.quotePostgresIdentifier("app\"db")); + } + @Test void mysql84UsesSupportedNativePasswordOptions() { assertEquals(