mirror of
https://github.com/floci-io/floci.git
synced 2026-11-24 06:20:24 -05:00
fix(rds): store and return the encryption and backup settings given on create (#2617)
* fix(rds): store and return the encryption and backup settings given on create DescribeDBInstances never returned StorageEncrypted, KmsKeyId, BackupRetentionPeriod or CopyTagsToSnapshot, and wrote both windows as constants. Store the six values from CreateDBInstance, apply the backup settings and windows from ModifyDBInstance, and emit them from the instance XML. A record persisted without them reads as AWS defaults. KmsKeyId is resolved against the KMS store in the request's region — a key ARN, key id, alias ARN or alias name — and stored as the key ARN, as a live account reports it; a key that does not exist or is not enabled is KMSKeyNotAccessibleFault. Validation follows a live account: KmsKeyId needs StorageEncrypted, window times are checked with AWS's wording, windows are at least 30 minutes and may not overlap; the retention period is not range-checked since AWS accepted 40 days. Closes #2611 * fix(rds): check window overlap against the windows that will be in effect A window given alone was not checked at all: the request's other window was null, so validate() skipped the overlap check although the instance kept its stored counterpart on modify and got a default on create. Check the pair that results from the request — on modify the stored other window, on create the default, swapped for an alternate default when the given window overlaps it, since AWS picks a random window clear of the given one. Verified on a live account: a modify overlapping the stored window is refused either way, replacing both at once checks only the new pair. * fix(rds): detect window overlap across the week boundary in both directions A Sunday backup window running past midnight lies beyond the end of the week, so comparing it with a Monday maintenance window needs the maintenance interval shifted forward as well as back. * fix(rds): derive the default counterpart window from the given one's end A fixed alternate default cannot be clear of every window a request can give — a 00:00-07:00 backup window covers it. When the window given on create overlaps the usual default, pair it with a 30-minute window that starts the minute the given one ends, which is clear of a daily window by construction; the overlap check stays as the backstop. * fix(rds): refuse a window given alone that leaves no room for the other, in AWS's words The derived counterpart is clear of any window that leaves a 30-minute gap (swept over every start and length in DbInstanceSettingsTest); a window longer than that has no clear counterpart, and a live account refuses it: "The specified backup window overlaps all available default maintenance windows. Shrink the backup window or specify a non-overlapping maintenance window." (mirrored for maintenance), and a maintenance window of 24 hours or more is "Maintenance window must be less than 24 hours." * refactor(rds): share the window checks with BackupWindows
This commit is contained in:
1 parent
46d2f4e70f
commit
5033ace772
10 files changed
+1067
-28
No files matched your search
@@ -12,6 +12,7 @@ import io.github.hectorvent.floci.services.rds.model.DbEndpoint;
|
||||
import io.github.hectorvent.floci.services.docdb.DocDbQueryHandler;
|
||||
import io.github.hectorvent.floci.services.neptune.NeptuneQueryHandler;
|
||||
import io.github.hectorvent.floci.services.rds.model.DbInstance;
|
||||
import io.github.hectorvent.floci.services.rds.model.DbInstanceSettings;
|
||||
import io.github.hectorvent.floci.services.rds.model.DbInstanceStatus;
|
||||
import io.github.hectorvent.floci.services.rds.model.DbParameterGroup;
|
||||
import io.github.hectorvent.floci.services.rds.model.DbProxy;
|
||||
@@ -156,12 +157,13 @@ public class RdsQueryHandler {
|
||||
}
|
||||
|
||||
try {
|
||||
DbInstanceSettings settings = instanceSettings(params);
|
||||
List<String> vpcSecurityGroupIds = vpcSecurityGroupIds(params);
|
||||
DbInstance instance = service.createDbInstance(id, engine, engineVersion, masterUsername,
|
||||
masterPassword, dbName, dbInstanceClass, allocatedStorage, iamEnabled,
|
||||
paramGroupName, dbSubnetGroupName, dbClusterIdentifier, availabilityZone, multiAz,
|
||||
manageMasterUserPassword, masterUserSecretKmsKeyId, tags, vpcSecurityGroupIds,
|
||||
optionGroupName, region, autoMinorVersionUpgrade);
|
||||
optionGroupName, region, autoMinorVersionUpgrade, settings);
|
||||
String result = dbInstanceXml(instance);
|
||||
return Response.ok(AwsQueryResponse.envelope("CreateDBInstance", AwsNamespaces.RDS, result)).build();
|
||||
} catch (AwsException e) {
|
||||
@@ -250,10 +252,11 @@ public class RdsQueryHandler {
|
||||
Boolean autoMinorVersionUpgrade = autoMinorVersionUpgradeStr != null
|
||||
? Boolean.parseBoolean(autoMinorVersionUpgradeStr) : null;
|
||||
try {
|
||||
DbInstanceSettings settings = instanceSettings(params, false);
|
||||
List<String> vpcSecurityGroupIds = vpcSecurityGroupIds(params);
|
||||
DbInstance instance = service.modifyDbInstance(
|
||||
id, newPassword, iamEnabled, dbSubnetGroupName,
|
||||
vpcSecurityGroupIds, optionGroupName, region, autoMinorVersionUpgrade);
|
||||
vpcSecurityGroupIds, optionGroupName, region, autoMinorVersionUpgrade, settings);
|
||||
String result = dbInstanceXml(instance);
|
||||
return Response.ok(AwsQueryResponse.envelope("ModifyDBInstance", AwsNamespaces.RDS, result)).build();
|
||||
} catch (AwsException e) {
|
||||
@@ -261,6 +264,41 @@ public class RdsQueryHandler {
|
||||
}
|
||||
}
|
||||
|
||||
private static DbInstanceSettings instanceSettings(MultivaluedMap<String, String> params) {
|
||||
return instanceSettings(params, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* ModifyDBInstance has no StorageEncrypted or KmsKeyId in its request shape — encryption is
|
||||
* fixed at create — so a modify reads only the backup settings and the windows.
|
||||
*/
|
||||
private static DbInstanceSettings instanceSettings(MultivaluedMap<String, String> params,
|
||||
boolean includeEncryption) {
|
||||
return new DbInstanceSettings(
|
||||
includeEncryption ? optionalBoolean(params.getFirst("StorageEncrypted")) : null,
|
||||
includeEncryption ? params.getFirst("KmsKeyId") : null,
|
||||
optionalInt(params.getFirst("BackupRetentionPeriod")),
|
||||
params.getFirst("PreferredBackupWindow"),
|
||||
params.getFirst("PreferredMaintenanceWindow"),
|
||||
optionalBoolean(params.getFirst("CopyTagsToSnapshot")));
|
||||
}
|
||||
|
||||
private static Boolean optionalBoolean(String value) {
|
||||
return value == null ? null : Boolean.parseBoolean(value);
|
||||
}
|
||||
|
||||
private static Integer optionalInt(String value) {
|
||||
if (value == null || value.isBlank()) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
return Integer.parseInt(value.trim());
|
||||
} catch (NumberFormatException e) {
|
||||
throw new AwsException("InvalidParameterValue",
|
||||
"Value " + value + " is not a valid integer.", 400);
|
||||
}
|
||||
}
|
||||
|
||||
private Response handleDescribeOrderableDbInstanceOptions(MultivaluedMap<String, String> params) {
|
||||
Collection<Map<String, String>> options = service.describeOrderableDbInstanceOptions(
|
||||
params.getFirst("Engine"),
|
||||
@@ -1264,14 +1302,22 @@ public class RdsQueryHandler {
|
||||
.elem("StorageType", "gp2")
|
||||
.elem("PubliclyAccessible", false)
|
||||
.elem("AvailabilityZone", i.getAvailabilityZone() != null ? i.getAvailabilityZone() : config.defaultAvailabilityZone())
|
||||
.elem("PreferredMaintenanceWindow", "mon:00:00-mon:03:00")
|
||||
.elem("PreferredBackupWindow", "04:00-06:00")
|
||||
.elem("PreferredMaintenanceWindow", i.getPreferredMaintenanceWindow() != null
|
||||
? i.getPreferredMaintenanceWindow() : DbInstanceSettings.DEFAULT_MAINTENANCE_WINDOW)
|
||||
.elem("PreferredBackupWindow", i.getPreferredBackupWindow() != null
|
||||
? i.getPreferredBackupWindow() : DbInstanceSettings.DEFAULT_BACKUP_WINDOW)
|
||||
.elem("BackupRetentionPeriod", i.getBackupRetentionPeriod())
|
||||
.elem("StorageEncrypted", i.isStorageEncrypted())
|
||||
.elem("CopyTagsToSnapshot", i.isCopyTagsToSnapshot())
|
||||
.raw(vpcSecurityGroupsXml(i))
|
||||
.raw(dbParameterGroupsXml(i))
|
||||
.raw(optionGroupMembershipsXml(i))
|
||||
.raw(dbSubnetGroupXml(dbSubnetGroupForInstance(i)))
|
||||
.elem("DbiResourceId", i.getDbiResourceId())
|
||||
.elem("DBInstanceArn", i.getDbInstanceArn());
|
||||
if (i.getKmsKeyId() != null && !i.getKmsKeyId().isBlank()) {
|
||||
xml.elem("KmsKeyId", i.getKmsKeyId());
|
||||
}
|
||||
if (i.getMasterUserSecretArn() != null && !i.getMasterUserSecretArn().isBlank()) {
|
||||
xml.start("MasterUserSecret")
|
||||
.elem("SecretArn", i.getMasterUserSecretArn())
|
||||
|
||||
@@ -26,6 +26,9 @@ import io.github.hectorvent.floci.services.rds.model.DbCluster;
|
||||
import io.github.hectorvent.floci.services.rds.model.DbClusterParameterGroup;
|
||||
import io.github.hectorvent.floci.services.rds.model.DbEndpoint;
|
||||
import io.github.hectorvent.floci.services.rds.model.DbInstance;
|
||||
import io.github.hectorvent.floci.services.kms.KmsService;
|
||||
import io.github.hectorvent.floci.services.kms.model.KmsKey;
|
||||
import io.github.hectorvent.floci.services.rds.model.DbInstanceSettings;
|
||||
import io.github.hectorvent.floci.services.rds.model.DbInstanceStatus;
|
||||
import io.github.hectorvent.floci.services.rds.model.DbParameterGroup;
|
||||
import io.github.hectorvent.floci.services.rds.model.DbProxy;
|
||||
@@ -147,6 +150,7 @@ public class RdsService implements Resettable, ResourceProvider {
|
||||
private final RegionResolver regionResolver;
|
||||
private final EmulatorConfig config;
|
||||
private final SecretsManagerService secretsManagerService;
|
||||
private final KmsService kmsService;
|
||||
private final DockerHostResolver dockerHostResolver;
|
||||
private final CurrentContainerNetworkResolver currentContainerNetworkResolver;
|
||||
private final ResourceGroupsTaggingService taggingService;
|
||||
@@ -182,13 +186,15 @@ public class RdsService implements Resettable, ResourceProvider {
|
||||
SecretsManagerService secretsManagerService,
|
||||
DockerHostResolver dockerHostResolver,
|
||||
CurrentContainerNetworkResolver currentContainerNetworkResolver,
|
||||
ResourceGroupsTaggingService taggingService) {
|
||||
ResourceGroupsTaggingService taggingService,
|
||||
KmsService kmsService) {
|
||||
this.containerManager = containerManager;
|
||||
this.proxyManager = proxyManager;
|
||||
this.ec2Service = ec2Service;
|
||||
this.regionResolver = regionResolver;
|
||||
this.config = config;
|
||||
this.secretsManagerService = secretsManagerService;
|
||||
this.kmsService = kmsService;
|
||||
this.dockerHostResolver = dockerHostResolver;
|
||||
this.currentContainerNetworkResolver = currentContainerNetworkResolver;
|
||||
this.taggingService = taggingService;
|
||||
@@ -300,7 +306,7 @@ public class RdsService implements Resettable, ResourceProvider {
|
||||
this(containerManager, proxyManager, ec2Service, regionResolver, config,
|
||||
instances, clusters, parameterGroups, clusterParameterGroups, subnetGroups,
|
||||
secretsManagerService, dockerHostResolver, currentContainerNetworkResolver,
|
||||
proxies, proxyTargetGroups, new InMemoryStorage<>(), null);
|
||||
proxies, proxyTargetGroups, new InMemoryStorage<>(), null, null);
|
||||
}
|
||||
|
||||
RdsService(RdsContainerManager containerManager,
|
||||
@@ -319,13 +325,15 @@ public class RdsService implements Resettable, ResourceProvider {
|
||||
StorageBackend<String, DbProxy> proxies,
|
||||
StorageBackend<String, DbProxyTargetGroup> proxyTargetGroups,
|
||||
StorageBackend<String, OptionGroup> optionGroups,
|
||||
ResourceGroupsTaggingService taggingService) {
|
||||
ResourceGroupsTaggingService taggingService,
|
||||
KmsService kmsService) {
|
||||
this.containerManager = containerManager;
|
||||
this.proxyManager = proxyManager;
|
||||
this.ec2Service = ec2Service;
|
||||
this.regionResolver = regionResolver;
|
||||
this.config = config;
|
||||
this.secretsManagerService = secretsManagerService;
|
||||
this.kmsService = kmsService;
|
||||
this.dockerHostResolver = dockerHostResolver;
|
||||
this.currentContainerNetworkResolver = currentContainerNetworkResolver;
|
||||
this.instances = instances;
|
||||
@@ -484,6 +492,28 @@ public class RdsService implements Resettable, ResourceProvider {
|
||||
String optionGroupName,
|
||||
String region,
|
||||
boolean autoMinorVersionUpgrade) {
|
||||
return createDbInstance(id, engineParam, engineVersion, masterUsername, masterPassword,
|
||||
dbName, dbInstanceClass, allocatedStorage, iamEnabled, paramGroupName,
|
||||
dbSubnetGroupName, dbClusterIdentifier, availabilityZone, multiAz,
|
||||
manageMasterUserPassword, masterUserSecretKmsKeyId, tags, vpcSecurityGroupIds,
|
||||
optionGroupName, region, autoMinorVersionUpgrade, DbInstanceSettings.defaults());
|
||||
}
|
||||
|
||||
public DbInstance createDbInstance(String id, String engineParam, String engineVersion,
|
||||
String masterUsername, String masterPassword,
|
||||
String dbName, String dbInstanceClass,
|
||||
int allocatedStorage, boolean iamEnabled,
|
||||
String paramGroupName, String dbSubnetGroupName,
|
||||
String dbClusterIdentifier, String availabilityZone,
|
||||
boolean multiAz, boolean manageMasterUserPassword,
|
||||
String masterUserSecretKmsKeyId,
|
||||
Map<String, String> tags,
|
||||
List<String> vpcSecurityGroupIds,
|
||||
String optionGroupName,
|
||||
String region,
|
||||
boolean autoMinorVersionUpgrade,
|
||||
DbInstanceSettings settings) {
|
||||
validateInstanceSettings(settings);
|
||||
String provisioningKey = "instance:" + currentAccountId() + ":"
|
||||
+ dbResourceKey(effectiveRegion(region), id);
|
||||
if (!provisioningIds.add(provisioningKey)) {
|
||||
@@ -495,7 +525,8 @@ public class RdsService implements Resettable, ResourceProvider {
|
||||
masterPassword, dbName, dbInstanceClass, allocatedStorage, iamEnabled,
|
||||
paramGroupName, dbSubnetGroupName, dbClusterIdentifier, availabilityZone,
|
||||
multiAz, manageMasterUserPassword, masterUserSecretKmsKeyId, tags,
|
||||
vpcSecurityGroupIds, optionGroupName, region, autoMinorVersionUpgrade);
|
||||
vpcSecurityGroupIds, optionGroupName, region, autoMinorVersionUpgrade,
|
||||
settings);
|
||||
} finally {
|
||||
provisioningIds.remove(provisioningKey);
|
||||
}
|
||||
@@ -513,7 +544,8 @@ public class RdsService implements Resettable, ResourceProvider {
|
||||
List<String> vpcSecurityGroupIds,
|
||||
String optionGroupName,
|
||||
String region,
|
||||
boolean autoMinorVersionUpgrade) {
|
||||
boolean autoMinorVersionUpgrade,
|
||||
DbInstanceSettings settings) {
|
||||
String effectiveRegion = effectiveRegion(region);
|
||||
String dbiResourceId = "db-" + java.util.UUID.randomUUID().toString()
|
||||
.replace("-", "").substring(0, 24).toUpperCase();
|
||||
@@ -531,6 +563,9 @@ public class RdsService implements Resettable, ResourceProvider {
|
||||
validateInstanceParameterGroup(
|
||||
paramGroupName, engineParam, engineVersion, effectiveRegion);
|
||||
validateInstanceOptionGroup(optionGroupName, engineParam, engineVersion, effectiveRegion);
|
||||
// resolved with the other validations, before a port is taken or a container started
|
||||
DbInstanceSettings resolvedSettings = withEffectiveWindows(settings, null)
|
||||
.withKmsKeyId(resolveKmsKeyArn(settings.kmsKeyId(), effectiveRegion));
|
||||
boolean mock = config.services().rds().mock();
|
||||
// Always reserve a unique port (even in mock) so endpoints stay distinct and usedPorts
|
||||
// is consistent; mock mode only skips starting the container and auth proxy.
|
||||
@@ -621,6 +656,7 @@ public class RdsService implements Resettable, ResourceProvider {
|
||||
instance.setSubnetAvailabilityZones(placement.subnetAvailabilityZones());
|
||||
instance.setAutoMinorVersionUpgrade(autoMinorVersionUpgrade);
|
||||
instance.setEngineIdentifier(engineIdentifier);
|
||||
resolvedSettings.applyTo(instance);
|
||||
|
||||
instance.setDbiResourceId(dbiResourceId);
|
||||
instance.setDbInstanceArn(dbInstanceArn);
|
||||
@@ -1019,8 +1055,22 @@ public class RdsService implements Resettable, ResourceProvider {
|
||||
String id, String newPassword, Boolean iamEnabled,
|
||||
String dbSubnetGroupName, List<String> vpcSecurityGroupIds,
|
||||
String optionGroupName, String region, Boolean autoMinorVersionUpgrade) {
|
||||
return modifyDbInstance(id, newPassword, iamEnabled, dbSubnetGroupName,
|
||||
vpcSecurityGroupIds, optionGroupName, region, autoMinorVersionUpgrade,
|
||||
DbInstanceSettings.unchanged());
|
||||
}
|
||||
|
||||
// synchronized like the tag and delete paths: an unguarded read-modify-write here could
|
||||
// write an instance back after deleteDbInstance removed it
|
||||
public synchronized DbInstance modifyDbInstance(
|
||||
String id, String newPassword, Boolean iamEnabled,
|
||||
String dbSubnetGroupName, List<String> vpcSecurityGroupIds,
|
||||
String optionGroupName, String region, Boolean autoMinorVersionUpgrade,
|
||||
DbInstanceSettings settings) {
|
||||
validateInstanceSettings(settings);
|
||||
String effectiveRegion = effectiveRegion(region);
|
||||
DbInstance instance = getDbInstance(id, effectiveRegion);
|
||||
DbInstanceSettings effective = withEffectiveWindows(settings, instance);
|
||||
instance.setStatus(DbInstanceStatus.AVAILABLE);
|
||||
if (optionGroupName != null && !optionGroupName.isBlank()) {
|
||||
validateInstanceOptionGroup(optionGroupName,
|
||||
@@ -1044,11 +1094,96 @@ public class RdsService implements Resettable, ResourceProvider {
|
||||
if (autoMinorVersionUpgrade != null) {
|
||||
instance.setAutoMinorVersionUpgrade(autoMinorVersionUpgrade);
|
||||
}
|
||||
effective.applyTo(instance);
|
||||
putInstanceForScope(currentAccountId(), effectiveRegion, id, instance);
|
||||
LOG.infov("DB instance {0} modified", id);
|
||||
return instance;
|
||||
}
|
||||
|
||||
private static void validateInstanceSettings(DbInstanceSettings settings) {
|
||||
settings.validate();
|
||||
}
|
||||
|
||||
/**
|
||||
* The windows that will be in effect after the request, checked against each other the way a
|
||||
* live account checks them. On create ({@code current} null) a window given alone is paired
|
||||
* with the default, or with a window starting where the given one ends when the default would
|
||||
* overlap it — AWS picks a random window clear of the given one. On modify the counterpart is
|
||||
* the instance's.
|
||||
*/
|
||||
private static DbInstanceSettings withEffectiveWindows(DbInstanceSettings settings, DbInstance current) {
|
||||
String backup = settings.preferredBackupWindow();
|
||||
String maintenance = settings.preferredMaintenanceWindow();
|
||||
boolean backupGiven = backup != null;
|
||||
boolean maintenanceGiven = maintenance != null;
|
||||
if (current == null) {
|
||||
if (!backupGiven) {
|
||||
backup = maintenanceGiven && DbInstanceSettings.windowsOverlap(
|
||||
DbInstanceSettings.DEFAULT_BACKUP_WINDOW, maintenance)
|
||||
? DbInstanceSettings.backupWindowAfter(maintenance)
|
||||
: DbInstanceSettings.DEFAULT_BACKUP_WINDOW;
|
||||
}
|
||||
if (!maintenanceGiven) {
|
||||
maintenance = backupGiven && DbInstanceSettings.windowsOverlap(
|
||||
backup, DbInstanceSettings.DEFAULT_MAINTENANCE_WINDOW)
|
||||
? DbInstanceSettings.maintenanceWindowAfter(backup)
|
||||
: DbInstanceSettings.DEFAULT_MAINTENANCE_WINDOW;
|
||||
}
|
||||
// a derived window is clear unless the given one leaves no 30-minute gap at all
|
||||
if (DbInstanceSettings.windowsOverlap(backup, maintenance)) {
|
||||
if (!maintenanceGiven) {
|
||||
throw DbInstanceSettings.noRoomForMaintenanceWindow();
|
||||
}
|
||||
if (!backupGiven) {
|
||||
throw DbInstanceSettings.noRoomForBackupWindow();
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (!backupGiven) {
|
||||
backup = current.getPreferredBackupWindow() != null
|
||||
? current.getPreferredBackupWindow() : DbInstanceSettings.DEFAULT_BACKUP_WINDOW;
|
||||
}
|
||||
if (!maintenanceGiven) {
|
||||
maintenance = current.getPreferredMaintenanceWindow() != null
|
||||
? current.getPreferredMaintenanceWindow() : DbInstanceSettings.DEFAULT_MAINTENANCE_WINDOW;
|
||||
}
|
||||
}
|
||||
if (DbInstanceSettings.windowsOverlap(backup, maintenance)) {
|
||||
throw DbInstanceSettings.overlappingWindows();
|
||||
}
|
||||
return new DbInstanceSettings(settings.storageEncrypted(), settings.kmsKeyId(),
|
||||
settings.backupRetentionPeriod(), backup, maintenance, settings.copyTagsToSnapshot());
|
||||
}
|
||||
|
||||
/**
|
||||
* A live account takes the key as an ARN, a key id, an alias ARN or an alias name and reports
|
||||
* the key ARN on the instance; a key it cannot use is one fault whatever the reason.
|
||||
*/
|
||||
private String resolveKmsKeyArn(String kmsKeyId, String region) {
|
||||
if (kmsKeyId == null || kmsKeyId.isBlank()) {
|
||||
return null;
|
||||
}
|
||||
if (kmsService == null) {
|
||||
throw new IllegalStateException("RdsService was built without a KmsService; "
|
||||
+ "a KmsKeyId cannot be resolved");
|
||||
}
|
||||
KmsKey key;
|
||||
try {
|
||||
key = kmsService.describeKey(kmsKeyId, region);
|
||||
} catch (AwsException e) {
|
||||
throw kmsKeyNotAccessible(kmsKeyId);
|
||||
}
|
||||
if (!key.isEnabled() || "PendingDeletion".equals(key.getKeyState())) {
|
||||
throw kmsKeyNotAccessible(kmsKeyId);
|
||||
}
|
||||
return key.getArn();
|
||||
}
|
||||
|
||||
private static AwsException kmsKeyNotAccessible(String kmsKeyId) {
|
||||
return new AwsException("KMSKeyNotAccessibleFault", "The specified KMS key [" + kmsKeyId
|
||||
+ "] does not exist, is not enabled or you do not have permissions to access it.", 400);
|
||||
}
|
||||
|
||||
public List<Map<String, String>> describeOrderableDbInstanceOptions(String engine,
|
||||
String engineVersion,
|
||||
String dbInstanceClass) {
|
||||
|
||||
@@ -36,6 +36,14 @@ public class DbInstance {
|
||||
// AWS defaults this to true when CreateDBInstance omits it (minor engine upgrades are
|
||||
// applied automatically unless explicitly opted out).
|
||||
private boolean autoMinorVersionUpgrade = true;
|
||||
private boolean storageEncrypted;
|
||||
private String kmsKeyId;
|
||||
// AWS defaults to one day of automated backups when CreateDBInstance omits it; a record
|
||||
// persisted before the field existed reads the same way.
|
||||
private int backupRetentionPeriod = 1;
|
||||
private String preferredBackupWindow;
|
||||
private String preferredMaintenanceWindow;
|
||||
private boolean copyTagsToSnapshot;
|
||||
private Map<String, String> subnetAvailabilityZones = new LinkedHashMap<>();
|
||||
private String dbiResourceId;
|
||||
private String dbInstanceArn;
|
||||
@@ -167,6 +175,24 @@ public class DbInstance {
|
||||
public String getMasterUserSecretStatus() { return masterUserSecretStatus; }
|
||||
public void setMasterUserSecretStatus(String masterUserSecretStatus) { this.masterUserSecretStatus = masterUserSecretStatus; }
|
||||
|
||||
public boolean isStorageEncrypted() { return storageEncrypted; }
|
||||
public void setStorageEncrypted(boolean storageEncrypted) { this.storageEncrypted = storageEncrypted; }
|
||||
|
||||
public String getKmsKeyId() { return kmsKeyId; }
|
||||
public void setKmsKeyId(String kmsKeyId) { this.kmsKeyId = kmsKeyId; }
|
||||
|
||||
public int getBackupRetentionPeriod() { return backupRetentionPeriod; }
|
||||
public void setBackupRetentionPeriod(int backupRetentionPeriod) { this.backupRetentionPeriod = backupRetentionPeriod; }
|
||||
|
||||
public String getPreferredBackupWindow() { return preferredBackupWindow; }
|
||||
public void setPreferredBackupWindow(String preferredBackupWindow) { this.preferredBackupWindow = preferredBackupWindow; }
|
||||
|
||||
public String getPreferredMaintenanceWindow() { return preferredMaintenanceWindow; }
|
||||
public void setPreferredMaintenanceWindow(String preferredMaintenanceWindow) { this.preferredMaintenanceWindow = preferredMaintenanceWindow; }
|
||||
|
||||
public boolean isCopyTagsToSnapshot() { return copyTagsToSnapshot; }
|
||||
public void setCopyTagsToSnapshot(boolean copyTagsToSnapshot) { this.copyTagsToSnapshot = copyTagsToSnapshot; }
|
||||
|
||||
public String getMasterUserSecretKmsKeyId() { return masterUserSecretKmsKeyId; }
|
||||
public void setMasterUserSecretKmsKeyId(String masterUserSecretKmsKeyId) { this.masterUserSecretKmsKeyId = masterUserSecretKmsKeyId; }
|
||||
|
||||
|
||||
@@ -0,0 +1,112 @@
|
||||
package io.github.hectorvent.floci.services.rds.model;
|
||||
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.common.BackupWindows;
|
||||
import io.quarkus.runtime.annotations.RegisterForReflection;
|
||||
|
||||
|
||||
/**
|
||||
* The storage and backup settings of a DB instance as a request carries them: a null member is
|
||||
* one the request left out. On create that means the AWS default, on modify it means unchanged.
|
||||
*/
|
||||
@RegisterForReflection
|
||||
public record DbInstanceSettings(Boolean storageEncrypted,
|
||||
String kmsKeyId,
|
||||
Integer backupRetentionPeriod,
|
||||
String preferredBackupWindow,
|
||||
String preferredMaintenanceWindow,
|
||||
Boolean copyTagsToSnapshot) {
|
||||
|
||||
/** Where AWS picks a random 30-minute window, Floci picks these. */
|
||||
public static final String DEFAULT_BACKUP_WINDOW = BackupWindows.DEFAULT_BACKUP_WINDOW;
|
||||
public static final String DEFAULT_MAINTENANCE_WINDOW = BackupWindows.DEFAULT_MAINTENANCE_WINDOW;
|
||||
|
||||
public static DbInstanceSettings defaults() {
|
||||
return new DbInstanceSettings(null, null, null, null, null, null);
|
||||
}
|
||||
|
||||
public static DbInstanceSettings unchanged() {
|
||||
return defaults();
|
||||
}
|
||||
|
||||
/**
|
||||
* The per-parameter checks a live account applies, with its wording. The retention period is
|
||||
* not range-checked: AWS accepted 40 days on a postgres instance. Overlap between the windows
|
||||
* is checked by the service against the windows that will be in effect, since the counterpart
|
||||
* of a window given alone comes from the instance or from a default.
|
||||
*/
|
||||
public void validate() {
|
||||
if (kmsKeyId != null && !kmsKeyId.isBlank() && !Boolean.TRUE.equals(storageEncrypted)) {
|
||||
throw new AwsException("InvalidParameterCombination",
|
||||
"You must enable StorageEncrypted when you specify KmsKeyId", 400);
|
||||
}
|
||||
if (preferredBackupWindow != null) {
|
||||
BackupWindows.parseBackupWindow(preferredBackupWindow);
|
||||
}
|
||||
if (preferredMaintenanceWindow != null) {
|
||||
BackupWindows.parseMaintenanceWindow(preferredMaintenanceWindow);
|
||||
}
|
||||
}
|
||||
|
||||
/** Whether a daily backup window and a weekly maintenance window share any minute. */
|
||||
public static boolean windowsOverlap(String backupWindow, String maintenanceWindow) {
|
||||
return BackupWindows.overlap(backupWindow, maintenanceWindow);
|
||||
}
|
||||
|
||||
/**
|
||||
* A 30-minute maintenance window that starts the minute the given daily backup window ends,
|
||||
* so it is clear of it on every day — what AWS's random pick achieves when only the backup
|
||||
* window is given.
|
||||
*/
|
||||
public static String maintenanceWindowAfter(String backupWindow) {
|
||||
return BackupWindows.maintenanceWindowAfter(backupWindow);
|
||||
}
|
||||
|
||||
/** A 30-minute daily backup window starting the minute the given maintenance window ends. */
|
||||
public static String backupWindowAfter(String maintenanceWindow) {
|
||||
return BackupWindows.backupWindowAfter(maintenanceWindow);
|
||||
}
|
||||
|
||||
/** A window given alone that leaves no room for the other kind, in AWS's words. */
|
||||
public static AwsException noRoomForMaintenanceWindow() {
|
||||
return new AwsException("InvalidParameterValue", "The specified backup window overlaps all "
|
||||
+ "available default maintenance windows. Shrink the backup window or specify a "
|
||||
+ "non-overlapping maintenance window.", 400);
|
||||
}
|
||||
|
||||
public static AwsException noRoomForBackupWindow() {
|
||||
return new AwsException("InvalidParameterValue", "The specified maintenance window overlaps "
|
||||
+ "all available default backup windows. Shrink the maintenance window or specify a "
|
||||
+ "non-overlapping backup window.", 400);
|
||||
}
|
||||
|
||||
public static AwsException overlappingWindows() {
|
||||
return BackupWindows.overlapping();
|
||||
}
|
||||
|
||||
public DbInstanceSettings withKmsKeyId(String resolvedKmsKeyId) {
|
||||
return new DbInstanceSettings(storageEncrypted, resolvedKmsKeyId, backupRetentionPeriod,
|
||||
preferredBackupWindow, preferredMaintenanceWindow, copyTagsToSnapshot);
|
||||
}
|
||||
|
||||
public void applyTo(DbInstance instance) {
|
||||
if (storageEncrypted != null) {
|
||||
instance.setStorageEncrypted(storageEncrypted);
|
||||
}
|
||||
if (kmsKeyId != null && !kmsKeyId.isBlank()) {
|
||||
instance.setKmsKeyId(kmsKeyId);
|
||||
}
|
||||
if (backupRetentionPeriod != null) {
|
||||
instance.setBackupRetentionPeriod(backupRetentionPeriod);
|
||||
}
|
||||
if (preferredBackupWindow != null && !preferredBackupWindow.isBlank()) {
|
||||
instance.setPreferredBackupWindow(preferredBackupWindow);
|
||||
}
|
||||
if (preferredMaintenanceWindow != null && !preferredMaintenanceWindow.isBlank()) {
|
||||
instance.setPreferredMaintenanceWindow(preferredMaintenanceWindow.toLowerCase());
|
||||
}
|
||||
if (copyTagsToSnapshot != null) {
|
||||
instance.setCopyTagsToSnapshot(copyTagsToSnapshot);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user