Light, fluffy, and always free - The AWS Local Emulator alternative
fix(docdb): store and return cluster and instance settings (#2651)
Michal Schreiberf019573bf164
diff --git a/compatibility-tests/sdk-test-awscli/test/rds.bats b/compatibility-tests/sdk-test-awscli/test/rds.batsindex ab339188d8..576ae4748d 100644--- a/compatibility-tests/sdk-test-awscli/test/rds.bats+++ b/compatibility-tests/sdk-test-awscli/test/rds.bats@@ -220,6 +220,40 @@ teardown() {aws_cmd ec2 delete-vpc --vpc-id "$VPC_ID" >/dev/null 2>&1 || true}+@test "docdb: cluster settings given on create are returned by describe" {+ CLUSTER_ID="bats-docdb-settings-$(unique_name)"+ run aws_cmd kms create-key --description "$CLUSTER_ID"+ assert_success+ KEY_ARN=$(json_get "$output" '.KeyMetadata.Arn')++ run aws_cmd docdb create-db-cluster --db-cluster-identifier "$CLUSTER_ID" \+ --engine docdb --master-username docdbadmin --master-user-password "secret99password" \+ --storage-encrypted --kms-key-id "$KEY_ARN" --backup-retention-period 5 \+ --preferred-backup-window 23:30-00:00 --preferred-maintenance-window sun:03:00-sun:04:00 \+ --tags "Key=Name,Value=$CLUSTER_ID"+ assert_success++ run aws_cmd docdb describe-db-clusters --db-cluster-identifier "$CLUSTER_ID" \+ --query 'DBClusters[0].[DBSubnetGroup,DBClusterParameterGroup,StorageEncrypted,KmsKeyId,BackupRetentionPeriod,PreferredBackupWindow,PreferredMaintenanceWindow,VpcSecurityGroups[0].VpcSecurityGroupId]'+ assert_success+ assert_output --partial '"default"'+ assert_output --partial '"default.docdb5.0"'+ assert_output --partial 'true'+ assert_output --partial "$KEY_ARN"+ assert_output --partial '5'+ assert_output --partial '"23:30-00:00"'+ assert_output --partial '"sun:03:00-sun:04:00"'+ assert_output --partial '"sg-'++ run aws_cmd docdb create-db-cluster --db-cluster-identifier "$CLUSTER_ID-x" \+ --engine docdb --master-username docdbadmin --master-user-password "secret99password" \+ --kms-key-id "$KEY_ARN"+ assert_failure+ assert_output --partial 'You cannot specify KMS key for unencrypted clusters.'++ aws_cmd docdb delete-db-cluster --db-cluster-identifier "$CLUSTER_ID" --skip-final-snapshot >/dev/null 2>&1 || true+}+@test "docdb: a DocumentDB cluster is listed by describe-db-clusters on both endpoints" {CLUSTER_ID="bats-docdb-list-$(unique_name)"aws_cmd docdb create-db-cluster --db-cluster-identifier "$CLUSTER_ID" \diff --git a/docs/services/docdb.md b/docs/services/docdb.mdindex 8a3866b18a..397541989c 100644--- a/docs/services/docdb.md+++ b/docs/services/docdb.md@@ -34,6 +34,17 @@ The management API shares the RDS Query endpoint (`POST /` with an `Action=` par| `RemoveTagsFromResource` | Remove tags by key from a cluster or instance |<!-- floci:actions:end -->+`CreateDBCluster` stores `DBSubnetGroupName`, `DBClusterParameterGroupName`, `VpcSecurityGroupIds`,+`StorageEncrypted`, `KmsKeyId` (resolved to the key ARN), `BackupRetentionPeriod`, both windows,+`Port`, `DeletionProtection` and `Tags`, and `DescribeDBClusters` returns them; `ModifyDBCluster`+changes the ones AWS lets change. References are checked as on AWS (the subnet group and cluster+parameter group are the RDS records, security groups are EC2's, the key must exist and be enabled),+and so are the windows (30-minute minimum, no overlap). Omitted values take the AWS defaults —+`default`, `default.docdb<family>`, the VPC's default security group, one day of backups — with+deterministic windows `04:00-06:00` / `mon:00:00-mon:03:00` where AWS picks random ones.+`CreateDBInstance` keeps `AutoMinorVersionUpgrade`, `PreferredMaintenanceWindow`,+`CopyTagsToSnapshot`, `PromotionTier` and `Tags`.+## Configuration| Variable | Default | Description |diff --git a/src/main/java/io/github/hectorvent/floci/core/common/BackupWindows.java b/src/main/java/io/github/hectorvent/floci/core/common/BackupWindows.javanew file mode 100644index 0000000000..7468228c0d--- /dev/null+++ b/src/main/java/io/github/hectorvent/floci/core/common/BackupWindows.java@@ -0,0 +1,138 @@+package io.github.hectorvent.floci.core.common;++import java.util.List;+import java.util.regex.Matcher;+import java.util.regex.Pattern;++/**+ * The daily backup window and weekly maintenance window the RDS family of services carries, with+ * the checks a live account applies to them and its wording: times in {@code hh24:mi}, days in+ * {@code ddd:hh24:mi}, at least 30 minutes each, and the two may not overlap.+ */+public final class BackupWindows {++ private static final Pattern TIME = Pattern.compile("^([01]\\d|2[0-3]):([0-5]\\d)$");+ private static final List<String> DAYS = List.of("mon", "tue", "wed", "thu", "fri", "sat", "sun");+ private static final int MINUTES_PER_DAY = 24 * 60;+ private static final int MINUTES_PER_WEEK = 7 * MINUTES_PER_DAY;+ private static final int MINIMUM_WINDOW_MINUTES = 30;++ /** Where AWS picks a random 30-minute window, Floci picks these. */+ public static final String DEFAULT_BACKUP_WINDOW = "04:00-06:00";+ public static final String DEFAULT_MAINTENANCE_WINDOW = "mon:00:00-mon:03:00";++ private BackupWindows() {+ }++ /** Validates a daily window and returns its start and end as minutes of the day. */+ public static int[] parseBackupWindow(String window) {+ String[] parts = window.split("-", -1);+ if (parts.length != 2) {+ throw invalidBackupTime(window);+ }+ int start = minuteOfDay(parts[0], () -> invalidBackupTime(parts[0]));+ int end = minuteOfDay(parts[1], () -> invalidBackupTime(parts[1]));+ if (end <= start) {+ end += MINUTES_PER_DAY;+ }+ if (end - start < MINIMUM_WINDOW_MINUTES) {+ throw new AwsException("InvalidParameterValue",+ "Backup window must be at least " + MINIMUM_WINDOW_MINUTES + " minutes.", 400);+ }+ return new int[] {start, end};+ }++ /** Validates a weekly window and returns its start and end as minutes of the week. */+ public static int[] parseMaintenanceWindow(String window) {+ String[] parts = window.split("-", -1);+ if (parts.length != 2) {+ throw invalidMaintenanceTime(window);+ }+ int start = minuteOfWeek(parts[0]);+ int end = minuteOfWeek(parts[1]);+ if (end <= start) {+ end += MINUTES_PER_WEEK;+ }+ if (end - start < MINIMUM_WINDOW_MINUTES) {+ throw new AwsException("InvalidParameterValue",+ "Maintenance window must be at least " + MINIMUM_WINDOW_MINUTES + " minutes.", 400);+ }+ if (end - start >= MINUTES_PER_DAY) {+ throw new AwsException("InvalidParameterValue",+ "Maintenance window must be less than 24 hours.", 400);+ }+ return new int[] {start, end};+ }++ public static boolean overlap(String backupWindow, String maintenanceWindow) {+ int[] backup = parseBackupWindow(backupWindow);+ int[] maintenance = parseMaintenanceWindow(maintenanceWindow);+ // the backup window recurs daily: lay it over each day and compare with the maintenance+ // window in this week and the neighbouring ones, since either may wrap the week boundary+ for (int day = 0; day < 7; day++) {+ int start = day * MINUTES_PER_DAY + backup[0];+ int end = day * MINUTES_PER_DAY + backup[1];+ for (int shift : new int[] {-MINUTES_PER_WEEK, 0, MINUTES_PER_WEEK}) {+ if (start < maintenance[1] - shift && maintenance[0] - shift < end) {+ return true;+ }+ }+ }+ return false;+ }++ public static AwsException overlapping() {+ return new AwsException("InvalidParameterValue",+ "The backup window and maintenance window must not overlap.", 400);+ }++ /** A 30-minute maintenance window starting the minute the given daily window ends. */+ public static String maintenanceWindowAfter(String backupWindow) {+ int end = parseBackupWindow(backupWindow)[1] % MINUTES_PER_DAY;+ return DAYS.get(0) + ":" + time(end) + "-" + DAYS.get((end + MINIMUM_WINDOW_MINUTES) / MINUTES_PER_DAY)+ + ":" + time(end + MINIMUM_WINDOW_MINUTES);+ }++ /** A 30-minute daily backup window starting the minute the given weekly window ends. */+ public static String backupWindowAfter(String maintenanceWindow) {+ int end = parseMaintenanceWindow(maintenanceWindow)[1] % MINUTES_PER_DAY;+ return time(end) + "-" + time(end + MINIMUM_WINDOW_MINUTES);+ }++ public static String lowerCase(String window) {+ return window == null ? null : window.toLowerCase();+ }++ private static int minuteOfWeek(String dayAndTime) {+ int colon = dayAndTime.indexOf(':');+ int day = colon < 0 ? -1 : DAYS.indexOf(dayAndTime.substring(0, colon).toLowerCase());+ if (day < 0) {+ throw invalidMaintenanceTime(dayAndTime);+ }+ return day * MINUTES_PER_DAY+ + minuteOfDay(dayAndTime.substring(colon + 1), () -> invalidMaintenanceTime(dayAndTime));+ }++ private static int minuteOfDay(String time, java.util.function.Supplier<AwsException> invalid) {+ Matcher m = TIME.matcher(time);+ if (!m.matches()) {+ throw invalid.get();+ }+ return Integer.parseInt(m.group(1)) * 60 + Integer.parseInt(m.group(2));+ }++ private static String time(int minuteOfDay) {+ int m = minuteOfDay % MINUTES_PER_DAY;+ return String.format("%02d:%02d", m / 60, m % 60);+ }++ private static AwsException invalidBackupTime(String time) {+ return new AwsException("InvalidParameterValue", "Invalid backup window time '" + time+ + "' specified. Should be specified as a time hh24:mi (24H Clock UTC). Example: 03:15", 400);+ }++ private static AwsException invalidMaintenanceTime(String time) {+ return new AwsException("InvalidParameterValue", "Invalid maintenance window time '" + time+ + "' specified. Should be specified as a time ddd:hh24:mi (24H Clock UTC). Example: Mon:00:15", 400);+ }+}diff --git a/src/main/java/io/github/hectorvent/floci/services/docdb/DocDbQueryHandler.java b/src/main/java/io/github/hectorvent/floci/services/docdb/DocDbQueryHandler.javaindex e0517172a2..940649502d 100644--- a/src/main/java/io/github/hectorvent/floci/services/docdb/DocDbQueryHandler.java+++ b/src/main/java/io/github/hectorvent/floci/services/docdb/DocDbQueryHandler.java@@ -5,7 +5,10 @@ import io.github.hectorvent.floci.core.common.AwsException;import io.github.hectorvent.floci.core.common.AwsNamespaces;import io.github.hectorvent.floci.core.common.AwsQueryResponse;import io.github.hectorvent.floci.core.common.XmlBuilder;+import io.github.hectorvent.floci.core.common.BackupWindows;import io.github.hectorvent.floci.services.docdb.model.DocDbCluster;+import io.github.hectorvent.floci.services.docdb.model.DocDbClusterSettings;+import io.github.hectorvent.floci.services.docdb.model.DocDbInstanceSettings;import io.github.hectorvent.floci.services.docdb.model.DocDbInstance;import jakarta.enterprise.context.ApplicationScoped;import jakarta.inject.Inject;@@ -76,13 +79,9 @@ public class DocDbQueryHandler {String masterPassword = params.getFirst("MasterUserPassword");boolean iamEnabled = "true".equalsIgnoreCase(params.getFirst("EnableIAMDatabaseAuthentication"));+ // Tags given at create are readable back on a live account; they go in with the record.DocDbCluster cluster = service.createDbCluster(id, engineVersion,- masterUsername, masterPassword, iamEnabled);- // Tags given at create are readable back on a live account, so they must not be dropped.- Map<String, String> tags = parseTags(params);- if (!tags.isEmpty()) {- service.addTagsToResource(cluster.getDbClusterArn(), tags);- }+ masterUsername, masterPassword, iamEnabled, clusterSettings(params, true), parseTags(params));return Response.ok(AwsQueryResponse.envelope("CreateDBCluster", AwsNamespaces.RDS,clusterXml(cluster))).build();}@@ -190,7 +189,7 @@ public class DocDbQueryHandler {String iamStr = params.getFirst("EnableIAMDatabaseAuthentication");Boolean iamEnabled = iamStr != null ? Boolean.parseBoolean(iamStr) : null;- DocDbCluster cluster = service.modifyDbCluster(id, engineVersion, iamEnabled);+ DocDbCluster cluster = service.modifyDbCluster(id, engineVersion, iamEnabled, clusterSettings(params, false));return Response.ok(AwsQueryResponse.envelope("ModifyDBCluster", AwsNamespaces.RDS,clusterXml(cluster))).build();}@@ -213,7 +212,7 @@ public class DocDbQueryHandler {boolean iamEnabled = "true".equalsIgnoreCase(params.getFirst("EnableIAMDatabaseAuthentication"));DocDbInstance instance = service.createDbInstance(id, dbClusterIdentifier,- dbInstanceClass, engineVersion, iamEnabled);+ dbInstanceClass, engineVersion, iamEnabled, instanceSettings(params), parseTags(params));return Response.ok(AwsQueryResponse.envelope("CreateDBInstance", AwsNamespaces.RDS,instanceXml(instance))).build();}@@ -264,11 +263,68 @@ public class DocDbQueryHandler {String iamStr = params.getFirst("EnableIAMDatabaseAuthentication");Boolean iamEnabled = iamStr != null ? Boolean.parseBoolean(iamStr) : null;- DocDbInstance instance = service.modifyDbInstance(id, dbInstanceClass, iamEnabled);+ DocDbInstance instance = service.modifyDbInstance(id, dbInstanceClass, iamEnabled, instanceSettings(params));return Response.ok(AwsQueryResponse.envelope("ModifyDBInstance", AwsNamespaces.RDS,instanceXml(instance))).build();}+ /**+ * The cluster settings a request carries. DBSubnetGroupName, StorageEncrypted and KmsKeyId are+ * fixed at create and not in the ModifyDBCluster shape, so a modify reads the rest only. Port+ * is not taken from the request: the port a cluster reports is the one it is reachable on.+ */+ private static DocDbClusterSettings clusterSettings(MultivaluedMap<String, String> params, boolean create) {+ List<String> securityGroups = memberList(params, "VpcSecurityGroupIds.VpcSecurityGroupId.");+ return new DocDbClusterSettings(+ create ? params.getFirst("DBSubnetGroupName") : null,+ params.getFirst("DBClusterParameterGroupName"),+ securityGroups.isEmpty() ? null : securityGroups,+ create ? optionalBoolean(params.getFirst("StorageEncrypted")) : null,+ create ? params.getFirst("KmsKeyId") : null,+ optionalInt(params.getFirst("BackupRetentionPeriod")),+ params.getFirst("PreferredBackupWindow"),+ params.getFirst("PreferredMaintenanceWindow"),+ optionalBoolean(params.getFirst("DeletionProtection")));+ }++ private static DocDbInstanceSettings instanceSettings(MultivaluedMap<String, String> params) {+ return new DocDbInstanceSettings(+ optionalBoolean(params.getFirst("AutoMinorVersionUpgrade")),+ params.getFirst("PreferredMaintenanceWindow"),+ optionalBoolean(params.getFirst("CopyTagsToSnapshot")),+ optionalInt(params.getFirst("PromotionTier")));+ }++ private static List<String> memberList(MultivaluedMap<String, String> params, String prefix) {+ List<String> values = new java.util.ArrayList<>();+ for (int i = 1; ; i++) {+ String value = params.getFirst(prefix + i);+ if (value == null) {+ break;+ }+ if (!value.isBlank()) {+ values.add(value);+ }+ }+ return values;+ }++ /** A live account reads any Query boolean that is not "false" as true. */+ private static Boolean optionalBoolean(String value) {+ return value == null ? null : !"false".equalsIgnoreCase(value.trim());+ }++ 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);+ }+ }+// ── XML builders ──────────────────────────────────────────────────────────private String clusterXml(DocDbCluster c) {@@ -287,11 +343,32 @@ public class DocDbQueryHandler {.elem("MasterUsername", c.getMasterUsername()).elem("IAMDatabaseAuthenticationEnabled", c.isIamDatabaseAuthenticationEnabled()).elem("MultiAZ", false)- .elem("StorageEncrypted", true)+ .elem("StorageEncrypted", c.isStorageEncrypted()).elem("AvailabilityZone", config.defaultAvailabilityZone())+ .elem("DBSubnetGroup", c.getDbSubnetGroupName() != null ? c.getDbSubnetGroupName() : "default")+ .elem("DBClusterParameterGroup", c.getDbClusterParameterGroupName() != null+ ? c.getDbClusterParameterGroupName()+ : "default." + DocDbService.parameterGroupFamily(c.getEngineVersion()))+ .elem("BackupRetentionPeriod", c.getBackupRetentionPeriod())+ .elem("PreferredBackupWindow", c.getPreferredBackupWindow() != null+ ? c.getPreferredBackupWindow() : BackupWindows.DEFAULT_BACKUP_WINDOW)+ .elem("PreferredMaintenanceWindow", c.getPreferredMaintenanceWindow() != null+ ? c.getPreferredMaintenanceWindow() : BackupWindows.DEFAULT_MAINTENANCE_WINDOW)+ .elem("DeletionProtection", c.isDeletionProtection()).elem("DbClusterResourceId", c.getDbClusterResourceId())- .elem("DBClusterArn", c.getDbClusterArn())- .start("DBClusterMembers");+ .elem("DBClusterArn", c.getDbClusterArn());+ if (c.getKmsKeyId() != null && !c.getKmsKeyId().isBlank()) {+ xml.elem("KmsKeyId", c.getKmsKeyId());+ }+ xml.start("VpcSecurityGroups");+ for (String groupId : c.getVpcSecurityGroupIds()) {+ xml.start("VpcSecurityGroupMembership")+ .elem("VpcSecurityGroupId", groupId)+ .elem("Status", "active")+ .end("VpcSecurityGroupMembership");+ }+ xml.end("VpcSecurityGroups")+ .start("DBClusterMembers");if (c.getDbClusterMembers() != null) {for (String memberId : c.getDbClusterMembers()) {xml.start("member")@@ -322,13 +399,27 @@ public class DocDbQueryHandler {.end("Endpoint").elem("IAMDatabaseAuthenticationEnabled", i.isIamDatabaseAuthenticationEnabled()).elem("MultiAZ", false)- .elem("StorageEncrypted", true)+ .elem("StorageEncrypted", storageEncryptedOf(i)).elem("AvailabilityZone", config.defaultAvailabilityZone())+ .elem("AutoMinorVersionUpgrade", i.isAutoMinorVersionUpgrade())+ .elem("PreferredMaintenanceWindow", i.getPreferredMaintenanceWindow() != null+ ? i.getPreferredMaintenanceWindow() : BackupWindows.DEFAULT_MAINTENANCE_WINDOW)+ .elem("CopyTagsToSnapshot", i.isCopyTagsToSnapshot())+ .elem("PromotionTier", i.getPromotionTier()).elem("DbiResourceId", i.getDbiResourceId()).elem("DBInstanceArn", i.getDbInstanceArn()).build();}+ /** An instance's encryption is its cluster's; a member of a cluster that is gone reads unencrypted. */+ private boolean storageEncryptedOf(DocDbInstance i) {+ try {+ return service.getDbCluster(i.getDbClusterIdentifier()).isStorageEncrypted();+ } catch (AwsException e) {+ return false;+ }+ }+private static String extractFilterValue(MultivaluedMap<String, String> params, String filterName) {for (int i = 1; ; i++) {String name = params.getFirst("Filters.Filter." + i + ".Name");diff --git a/src/main/java/io/github/hectorvent/floci/services/docdb/DocDbService.java b/src/main/java/io/github/hectorvent/floci/services/docdb/DocDbService.javaindex 9afe7c65e0..14a29d8a4b 100644--- a/src/main/java/io/github/hectorvent/floci/services/docdb/DocDbService.java+++ b/src/main/java/io/github/hectorvent/floci/services/docdb/DocDbService.java@@ -16,6 +16,14 @@ import jakarta.inject.Inject;import org.jboss.logging.Logger;import java.time.Instant;+import io.github.hectorvent.floci.core.common.BackupWindows;+import io.github.hectorvent.floci.services.docdb.model.DocDbClusterSettings;+import io.github.hectorvent.floci.services.docdb.model.DocDbInstanceSettings;+import io.github.hectorvent.floci.services.ec2.Ec2Service;+import io.github.hectorvent.floci.services.kms.KmsService;+import io.github.hectorvent.floci.services.kms.model.KmsKey;+import io.github.hectorvent.floci.services.rds.RdsService;+import io.github.hectorvent.floci.services.rds.model.DbClusterParameterGroup;import java.util.ArrayList;import java.util.Collection;import java.util.LinkedHashMap;@@ -39,6 +47,11 @@ public class DocDbService {private final EmulatorConfig config;private final RegionResolver regionResolver;private final DocDbContainerManager containerManager;+ // DocumentDB subnet groups and cluster parameter groups are the RDS records reached through+ // the rds scope; security groups are EC2's; a KMS key is resolved to its ARN as on AWS+ private final RdsService rdsService;+ private final Ec2Service ec2Service;+ private final KmsService kmsService;/*** One monitor per stored record, taken by everything that writes it. A tag update is a* read-modify-write, so without a lock shared with delete it can put a deleted cluster back.@@ -151,10 +164,16 @@ public class DocDbService {public DocDbService(EmulatorConfig config,RegionResolver regionResolver,DocDbContainerManager containerManager,- StorageFactory storageFactory) {+ StorageFactory storageFactory,+ RdsService rdsService,+ Ec2Service ec2Service,+ KmsService kmsService) {this.config = config;this.regionResolver = regionResolver;this.containerManager = containerManager;+ this.rdsService = rdsService;+ this.ec2Service = ec2Service;+ this.kmsService = kmsService;this.clusters = storageFactory.create("docdb", "docdb-clusters.json",new TypeReference<Map<String, DocDbCluster>>() {});this.instances = storageFactory.create("docdb", "docdb-instances.json",@@ -166,7 +185,19 @@ public class DocDbService {public DocDbCluster createDbCluster(String id, String engineVersion,String masterUsername, String masterPassword,boolean iamEnabled) {+ return createDbCluster(id, engineVersion, masterUsername, masterPassword, iamEnabled,+ DocDbClusterSettings.defaults(), Map.of());+ }++ public DocDbCluster createDbCluster(String id, String engineVersion,+ String masterUsername, String masterPassword,+ boolean iamEnabled, DocDbClusterSettings settings,+ Map<String, String> tags) {String region = regionResolver.getRegion();+ settings.validate();+ String effectiveEngineVersion = engineVersion != null ? engineVersion : ENGINE_VERSION_DEFAULT;+ // every reference checked and every default chosen before a container is started+ DocDbClusterSettings resolved = resolveClusterSettings(settings, null, effectiveEngineVersion, region);synchronized (lockFor("cluster:" + key(region, id))) {if (findCluster(region, id).isPresent()) {throw new AwsException("DBClusterAlreadyExistsFault",@@ -184,6 +215,10 @@ public class DocDbService {.replace("-", "").substring(0, 24).toUpperCase());cluster.setCreatedAt(Instant.now());cluster.setDbClusterMembers(new ArrayList<>());+ resolved.applyTo(cluster);+ if (tags != null && !tags.isEmpty()) {+ cluster.setTags(new LinkedHashMap<>(tags));+ }if (config.services().docdb().mock()) {LOG.infov("Creating DocDB cluster {0} in mock mode (no container)", id);@@ -221,13 +256,214 @@ public class DocDbService {}}+ /**+ * The settings that will be in effect after the request, with every reference checked the way+ * a live account checks it: the subnet group and cluster parameter group must exist (RDS+ * holds both), each security group must exist, a KMS key is resolved to its ARN. On create+ * ({@code current} null) an omitted value takes the AWS default — the default subnet group,+ * the family's default parameter group, the VPC's default security group, one day of backups+ * and windows clear of each other; on modify it keeps the cluster's.+ */+ private DocDbClusterSettings resolveClusterSettings(DocDbClusterSettings settings, DocDbCluster current,+ String engineVersion, String region) {+ boolean create = current == null;+ String subnetGroup = settings.dbSubnetGroupName();+ if (subnetGroup != null && !subnetGroup.isBlank()) {+ if (rdsService == null) {+ throw new IllegalStateException("DocDbService was built without an RdsService");+ }+ try {+ rdsService.getDbSubnetGroup(subnetGroup, region);+ } catch (AwsException e) {+ throw new AwsException("DBSubnetGroupNotFoundFault",+ "DB subnet group '" + subnetGroup + "' does not exist.", 404);+ }+ } else if (create) {+ subnetGroup = "default";+ }+ String parameterGroup = settings.dbClusterParameterGroupName();+ if (parameterGroup != null && !parameterGroup.isBlank()) {+ if (rdsService == null) {+ throw new IllegalStateException("DocDbService was built without an RdsService");+ }+ DbClusterParameterGroup group;+ try {+ group = rdsService.getDbClusterParameterGroup(parameterGroup, region);+ } catch (AwsException e) {+ throw new AwsException("DBClusterParameterGroupNotFound",+ "DBClusterParameterGroup not found: " + parameterGroup, 404);+ }+ requireFamily(group, engineVersion);+ } else if (create) {+ parameterGroup = "default." + parameterGroupFamily(engineVersion);+ } else if (current.getDbClusterParameterGroupName() != null+ && !parameterGroupFamily(engineVersion).equals(parameterGroupFamily(current.getEngineVersion()))) {+ // judged by what RDS holds for the group, never by its name: a custom group may be called anything+ if (rdsService != null && rdsService.isManagedClusterParameterGroup(current.getDbClusterParameterGroupName())) {+ // an engine version change moves a cluster on a default group to the new family's default+ parameterGroup = "default." + parameterGroupFamily(engineVersion);+ } else if (rdsService != null && !parameterGroupFamily(engineVersion).equals(+ currentParameterGroupFamily(current.getDbClusterParameterGroupName(), region))) {+ throw new AwsException("InvalidParameterCombination", "The current DB cluster parameter group "+ + current.getDbClusterParameterGroupName() + " is custom. You must explicitly specify a "+ + "new DB cluster parameter group, either default or custom, for the engine version upgrade.",+ 400);+ }+ }+ List<String> securityGroups = settings.vpcSecurityGroupIds();+ if (securityGroups != null && !securityGroups.isEmpty()) {+ if (ec2Service == null) {+ throw new IllegalStateException("DocDbService was built without an Ec2Service");+ }+ for (String groupId : securityGroups) {+ boolean known = ec2Service.describeSecurityGroups(region, List.of(groupId), null, Map.of())+ .stream().anyMatch(sg -> groupId.equals(sg.getGroupId()));+ if (!known) {+ throw new AwsException("InvalidParameterValue",+ "Invalid security group , groupId= " + groupId + ", groupName=.", 400);+ }+ }+ } else if (create) {+ securityGroups = List.of(Ec2Service.defaultSecurityGroupId(region));+ }+ String kmsKeyArn = resolveKmsKeyArn(settings.kmsKeyId(), region);+ String backup = settings.preferredBackupWindow();+ String maintenance = settings.preferredMaintenanceWindow();+ boolean backupGiven = backup != null;+ boolean maintenanceGiven = maintenance != null;+ if (create) {+ if (!backupGiven) {+ backup = maintenanceGiven && BackupWindows.overlap(BackupWindows.DEFAULT_BACKUP_WINDOW, maintenance)+ ? BackupWindows.backupWindowAfter(maintenance) : BackupWindows.DEFAULT_BACKUP_WINDOW;+ if (maintenanceGiven && BackupWindows.overlap(backup, maintenance)) {+ // the given window leaves no daily half hour free+ throw 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);+ }+ }+ if (!maintenanceGiven) {+ maintenance = backupGiven && BackupWindows.overlap(backup, BackupWindows.DEFAULT_MAINTENANCE_WINDOW)+ ? BackupWindows.maintenanceWindowAfter(backup) : BackupWindows.DEFAULT_MAINTENANCE_WINDOW;+ if (BackupWindows.overlap(backup, maintenance)) {+ throw 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);+ }+ }+ } else {+ if (!backupGiven) {+ backup = current.getPreferredBackupWindow() != null+ ? current.getPreferredBackupWindow() : BackupWindows.DEFAULT_BACKUP_WINDOW;+ }+ if (!maintenanceGiven) {+ maintenance = current.getPreferredMaintenanceWindow() != null+ ? current.getPreferredMaintenanceWindow() : BackupWindows.DEFAULT_MAINTENANCE_WINDOW;+ }+ }+ if (BackupWindows.overlap(backup, maintenance)) {+ throw BackupWindows.overlapping();+ }+ return new DocDbClusterSettings(subnetGroup, parameterGroup, securityGroups,+ settings.storageEncrypted(), kmsKeyArn, settings.backupRetentionPeriod(),+ backup, maintenance, settings.deletionProtection());+ }++ /** The family of the group a cluster is on; a group deleted since is judged by nothing. */+ private String currentParameterGroupFamily(String name, String region) {+ try {+ return rdsService.getDbClusterParameterGroup(name, region).getDbParameterGroupFamily();+ } catch (AwsException e) {+ return null;+ }+ }++ private static void requireFamily(DbClusterParameterGroup group, String engineVersion) {+ String family = parameterGroupFamily(engineVersion);+ if (group.getDbParameterGroupFamily() != null && !family.equals(group.getDbParameterGroupFamily())) {+ throw new AwsException("InvalidParameterCombination", "The Parameter Group "+ + group.getDbClusterParameterGroupName() + " with DBParameterGroupFamily "+ + group.getDbParameterGroupFamily() + " cannot be used for this instance. "+ + "Please use a Parameter Group with DBParameterGroupFamily " + family, 400);+ }+ }++ /** The parameter group family an engine version belongs to: {@code 5.0.0} is {@code docdb5.0}. */+ static String parameterGroupFamily(String engineVersion) {+ String version = engineVersion == null || engineVersion.isBlank() ? ENGINE_VERSION_DEFAULT : engineVersion;+ String[] parts = version.split("\\.");+ return "docdb" + parts[0] + "." + (parts.length > 1 ? parts[1] : "0");+ }++ /**+ * 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 cluster; 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("DocDbService 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 DocDbCluster getDbCluster(String id) {String region = regionResolver.getRegion();DocDbCluster cluster = findCluster(region, id).orElseThrow(() ->new AwsException("DBClusterNotFoundFault","DocDB cluster " + id + " not found.", 404));+ return filled(cluster, region);+ }++ /**+ * A cluster with what a record written by an earlier version lacks: its ARN, and the settings+ * a live account gives a cluster that names none — the default subnet group, the engine's+ * default parameter group, the VPC's default security group and the default windows.+ */+ private DocDbCluster filled(DocDbCluster cluster, String region) {+ String id = cluster.getDbClusterIdentifier();+ boolean changed = false;if (cluster.getDbClusterArn() == null || cluster.getDbClusterArn().isBlank()) {cluster.setDbClusterArn(legacyArn(region, "cluster:" + id));+ changed = true;+ }+ if (cluster.getDbSubnetGroupName() == null) {+ cluster.setDbSubnetGroupName("default");+ changed = true;+ }+ if (cluster.getDbClusterParameterGroupName() == null) {+ cluster.setDbClusterParameterGroupName("default." + parameterGroupFamily(cluster.getEngineVersion()));+ changed = true;+ }+ if (cluster.getVpcSecurityGroupIds() == null || cluster.getVpcSecurityGroupIds().isEmpty()) {+ cluster.setVpcSecurityGroupIds(List.of(Ec2Service.defaultSecurityGroupId(region)));+ changed = true;+ }+ if (cluster.getPreferredBackupWindow() == null) {+ cluster.setPreferredBackupWindow(BackupWindows.DEFAULT_BACKUP_WINDOW);+ changed = true;+ }+ if (cluster.getPreferredMaintenanceWindow() == null) {+ cluster.setPreferredMaintenanceWindow(BackupWindows.DEFAULT_MAINTENANCE_WINDOW);+ changed = true;+ }+ if (changed) {persistIfStillPresent(clusters, "cluster:" + key(region, id), key(region, id), cluster);}return cluster;@@ -314,24 +550,36 @@ public class DocDbService {// resolve a same-named local cluster.if (filterId.startsWith("arn:")) {return inRegion(clusters, region, DocDbCluster::getDbClusterIdentifier).stream()+ .map(c -> filled(c, region)).filter(c -> filterId.equalsIgnoreCase(c.getDbClusterArn())).toList();}- return findCluster(region, filterId).map(List::of).orElseGet(List::of);+ return findCluster(region, filterId).map(c -> List.of(filled(c, region))).orElseGet(List::of);}- return inRegion(clusters, region, DocDbCluster::getDbClusterIdentifier);+ return inRegion(clusters, region, DocDbCluster::getDbClusterIdentifier).stream()+ .map(c -> filled(c, region))+ .toList();}public DocDbCluster modifyDbCluster(String id, String engineVersion, Boolean iamEnabled) {+ return modifyDbCluster(id, engineVersion, iamEnabled, DocDbClusterSettings.unchanged());+ }++ public DocDbCluster modifyDbCluster(String id, String engineVersion, Boolean iamEnabled,+ DocDbClusterSettings settings) {String region = regionResolver.getRegion();+ settings.validate();synchronized (lockFor("cluster:" + key(region, id))) {DocDbCluster cluster = getDbCluster(id);- if (engineVersion != null && !engineVersion.isBlank()) {- cluster.setEngineVersion(engineVersion);- }+ // every check before any change: the store hands out its own object+ String effectiveEngineVersion = engineVersion != null && !engineVersion.isBlank()+ ? engineVersion : cluster.getEngineVersion();+ DocDbClusterSettings resolved = resolveClusterSettings(settings, cluster, effectiveEngineVersion, region);+ cluster.setEngineVersion(effectiveEngineVersion);if (iamEnabled != null) {cluster.setIamDatabaseAuthenticationEnabled(iamEnabled);}+ resolved.applyTo(cluster);clusters.put(key(region, id), cluster);LOG.infov("DocDB cluster {0} modified", id);return cluster;@@ -345,6 +593,10 @@ public class DocDbService {new AwsException("DBClusterNotFoundFault","DocDB cluster " + id + " not found.", 404));+ if (cluster.isDeletionProtection()) {+ throw new AwsException("InvalidParameterCombination",+ "Cannot delete protected Cluster, please disable deletion protection and try again.", 400);+ }if (cluster.getDbClusterMembers() != null && !cluster.getDbClusterMembers().isEmpty()) {throw new AwsException("InvalidDBClusterStateFault","Cannot delete DocDB cluster " + id + " — it still has DB instances.", 400);@@ -370,7 +622,16 @@ public class DocDbService {public DocDbInstance createDbInstance(String id, String dbClusterIdentifier,String dbInstanceClass, String engineVersion,boolean iamEnabled) {+ return createDbInstance(id, dbClusterIdentifier, dbInstanceClass, engineVersion, iamEnabled,+ DocDbInstanceSettings.defaults(), Map.of());+ }++ public DocDbInstance createDbInstance(String id, String dbClusterIdentifier,+ String dbInstanceClass, String engineVersion,+ boolean iamEnabled, DocDbInstanceSettings settings,+ Map<String, String> tags) {String region = regionResolver.getRegion();+ settings.validate();// Instance monitor before cluster monitor, the one order every path that holds both uses.synchronized (lockFor("instance:" + key(region, id))) {synchronized (lockFor("cluster:" + key(region, dbClusterIdentifier))) {@@ -394,6 +655,12 @@ public class DocDbService {instance.setDbiResourceId("db-" + UUID.randomUUID().toString().replace("-", "").substring(0, 24).toUpperCase());instance.setCreatedAt(Instant.now());+ instance.setPreferredMaintenanceWindow(cluster.getPreferredMaintenanceWindow() != null+ ? cluster.getPreferredMaintenanceWindow() : BackupWindows.DEFAULT_MAINTENANCE_WINDOW);+ settings.applyTo(instance);+ if (tags != null && !tags.isEmpty()) {+ instance.setTags(new LinkedHashMap<>(tags));+ }cluster.getDbClusterMembers().add(id);clusters.put(key(region, dbClusterIdentifier), cluster);@@ -410,8 +677,24 @@ public class DocDbService {DocDbInstance instance = findInstance(region, id).orElseThrow(() ->new AwsException("DBInstanceNotFound","DocDB instance " + id + " not found.", 404));+ return filled(instance, region);+ }++ /** An instance with its ARN and, as on a live account, its cluster's maintenance window. */+ private DocDbInstance filled(DocDbInstance instance, String region) {+ String id = instance.getDbInstanceIdentifier();+ boolean changed = false;if (instance.getDbInstanceArn() == null || instance.getDbInstanceArn().isBlank()) {instance.setDbInstanceArn(legacyArn(region, "db:" + id));+ changed = true;+ }+ if (instance.getPreferredMaintenanceWindow() == null) {+ instance.setPreferredMaintenanceWindow(findCluster(region, instance.getDbClusterIdentifier())+ .map(c -> filled(c, region).getPreferredMaintenanceWindow())+ .orElse(BackupWindows.DEFAULT_MAINTENANCE_WINDOW));+ changed = true;+ }+ if (changed) {persistIfStillPresent(instances, "instance:" + key(region, id), key(region, id), instance);}return instance;@@ -424,16 +707,25 @@ public class DocDbService {// listDbClusters for why the match is against the stored ARN.if (filterId.startsWith("arn:")) {return inRegion(instances, region, DocDbInstance::getDbInstanceIdentifier).stream()+ .map(i -> filled(i, region)).filter(i -> filterId.equalsIgnoreCase(i.getDbInstanceArn())).toList();}- return findInstance(region, filterId).map(List::of).orElseGet(List::of);+ return findInstance(region, filterId).map(i -> List.of(filled(i, region))).orElseGet(List::of);}- return inRegion(instances, region, DocDbInstance::getDbInstanceIdentifier);+ return inRegion(instances, region, DocDbInstance::getDbInstanceIdentifier).stream()+ .map(i -> filled(i, region))+ .toList();}public DocDbInstance modifyDbInstance(String id, String dbInstanceClass, Boolean iamEnabled) {+ return modifyDbInstance(id, dbInstanceClass, iamEnabled, DocDbInstanceSettings.defaults());+ }++ public DocDbInstance modifyDbInstance(String id, String dbInstanceClass, Boolean iamEnabled,+ DocDbInstanceSettings settings) {String region = regionResolver.getRegion();+ settings.validate();synchronized (lockFor("instance:" + key(region, id))) {DocDbInstance instance = getDbInstance(id);if (dbInstanceClass != null && !dbInstanceClass.isBlank()) {@@ -442,6 +734,7 @@ public class DocDbService {if (iamEnabled != null) {instance.setIamDatabaseAuthenticationEnabled(iamEnabled);}+ settings.applyTo(instance);instances.put(key(region, id), instance);LOG.infov("DocDB instance {0} modified", id);return instance;diff --git a/src/main/java/io/github/hectorvent/floci/services/docdb/model/DocDbCluster.java b/src/main/java/io/github/hectorvent/floci/services/docdb/model/DocDbCluster.javaindex 9dc54f4393..72f6f2aace 100644--- a/src/main/java/io/github/hectorvent/floci/services/docdb/model/DocDbCluster.java+++ b/src/main/java/io/github/hectorvent/floci/services/docdb/model/DocDbCluster.java@@ -23,6 +23,16 @@ public class DocDbCluster {private String dbClusterArn;private String dbClusterResourceId;private List<String> dbClusterMembers = new ArrayList<>();+ private String dbSubnetGroupName;+ private String dbClusterParameterGroupName;+ private List<String> vpcSecurityGroupIds = new ArrayList<>();+ private boolean storageEncrypted;+ private String kmsKeyId;+ // AWS defaults to one day of automated backups; a record persisted before the field reads the same+ private int backupRetentionPeriod = 1;+ private String preferredBackupWindow;+ private String preferredMaintenanceWindow;+ private boolean deletionProtection;private Instant createdAt;// Docker / proxy runtime fields — persisted so cleanup works across restarts@@ -64,6 +74,25 @@ public class DocDbCluster {public String getDbClusterResourceId() { return dbClusterResourceId; }public void setDbClusterResourceId(String dbClusterResourceId) { this.dbClusterResourceId = dbClusterResourceId; }+ public String getDbSubnetGroupName() { return dbSubnetGroupName; }+ public void setDbSubnetGroupName(String dbSubnetGroupName) { this.dbSubnetGroupName = dbSubnetGroupName; }+ public String getDbClusterParameterGroupName() { return dbClusterParameterGroupName; }+ public void setDbClusterParameterGroupName(String dbClusterParameterGroupName) { this.dbClusterParameterGroupName = dbClusterParameterGroupName; }+ public List<String> getVpcSecurityGroupIds() { return vpcSecurityGroupIds; }+ public void setVpcSecurityGroupIds(List<String> vpcSecurityGroupIds) { this.vpcSecurityGroupIds = vpcSecurityGroupIds == null ? new ArrayList<>() : vpcSecurityGroupIds; }+ 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 isDeletionProtection() { return deletionProtection; }+ public void setDeletionProtection(boolean deletionProtection) { this.deletionProtection = deletionProtection; }+public List<String> getDbClusterMembers() { return dbClusterMembers; }public void setDbClusterMembers(List<String> dbClusterMembers) {this.dbClusterMembers = dbClusterMembers != null ? new ArrayList<>(dbClusterMembers) : new ArrayList<>();diff --git a/src/main/java/io/github/hectorvent/floci/services/docdb/model/DocDbClusterSettings.java b/src/main/java/io/github/hectorvent/floci/services/docdb/model/DocDbClusterSettings.javanew file mode 100644index 0000000000..1bf67bef06--- /dev/null+++ b/src/main/java/io/github/hectorvent/floci/services/docdb/model/DocDbClusterSettings.java@@ -0,0 +1,85 @@+package io.github.hectorvent.floci.services.docdb.model;++import io.github.hectorvent.floci.core.common.AwsException;+import io.github.hectorvent.floci.core.common.BackupWindows;+import io.quarkus.runtime.annotations.RegisterForReflection;++import java.util.List;++/**+ * The placement, encryption and backup settings of a DocumentDB cluster as a request carries+ * them: a null member is one the request left out — the AWS default on create, unchanged on modify.+ */+@RegisterForReflection+public record DocDbClusterSettings(String dbSubnetGroupName,+ String dbClusterParameterGroupName,+ List<String> vpcSecurityGroupIds,+ Boolean storageEncrypted,+ String kmsKeyId,+ Integer backupRetentionPeriod,+ String preferredBackupWindow,+ String preferredMaintenanceWindow,+ Boolean deletionProtection) {++ public static DocDbClusterSettings defaults() {+ return new DocDbClusterSettings(null, null, null, null, null, null, null, null, null);+ }++ public static DocDbClusterSettings unchanged() {+ return defaults();+ }++ /** The per-parameter checks a live account applies, with its wording. */+ public void validate() {+ if (kmsKeyId != null && !kmsKeyId.isBlank() && !Boolean.TRUE.equals(storageEncrypted)) {+ throw new AwsException("InvalidParameterCombination",+ "You cannot specify KMS key for unencrypted clusters.", 400);+ }+ if (backupRetentionPeriod != null && (backupRetentionPeriod < 1 || backupRetentionPeriod > 354)) {+ throw new AwsException("InvalidParameterValue", "Invalid backup retention period: "+ + backupRetentionPeriod + ". Retention period must be between 1 and 354.", 400);+ }+ if (preferredBackupWindow != null) {+ BackupWindows.parseBackupWindow(preferredBackupWindow);+ }+ if (preferredMaintenanceWindow != null) {+ BackupWindows.parseMaintenanceWindow(preferredMaintenanceWindow);+ }+ }++ public DocDbClusterSettings with(String resolvedKmsKeyId, String backupWindow, String maintenanceWindow) {+ return new DocDbClusterSettings(dbSubnetGroupName, dbClusterParameterGroupName, vpcSecurityGroupIds,+ storageEncrypted, resolvedKmsKeyId, backupRetentionPeriod, backupWindow, maintenanceWindow,+ deletionProtection);+ }++ public void applyTo(DocDbCluster cluster) {+ if (dbSubnetGroupName != null && !dbSubnetGroupName.isBlank()) {+ cluster.setDbSubnetGroupName(dbSubnetGroupName);+ }+ if (dbClusterParameterGroupName != null && !dbClusterParameterGroupName.isBlank()) {+ cluster.setDbClusterParameterGroupName(dbClusterParameterGroupName);+ }+ if (vpcSecurityGroupIds != null && !vpcSecurityGroupIds.isEmpty()) {+ cluster.setVpcSecurityGroupIds(List.copyOf(vpcSecurityGroupIds));+ }+ if (storageEncrypted != null) {+ cluster.setStorageEncrypted(storageEncrypted);+ }+ if (kmsKeyId != null && !kmsKeyId.isBlank()) {+ cluster.setKmsKeyId(kmsKeyId);+ }+ if (backupRetentionPeriod != null) {+ cluster.setBackupRetentionPeriod(backupRetentionPeriod);+ }+ if (preferredBackupWindow != null && !preferredBackupWindow.isBlank()) {+ cluster.setPreferredBackupWindow(preferredBackupWindow);+ }+ if (preferredMaintenanceWindow != null && !preferredMaintenanceWindow.isBlank()) {+ cluster.setPreferredMaintenanceWindow(BackupWindows.lowerCase(preferredMaintenanceWindow));+ }+ if (deletionProtection != null) {+ cluster.setDeletionProtection(deletionProtection);+ }+ }+}diff --git a/src/main/java/io/github/hectorvent/floci/services/docdb/model/DocDbInstance.java b/src/main/java/io/github/hectorvent/floci/services/docdb/model/DocDbInstance.javaindex 57eaa9b34f..7aa25dce11 100644--- a/src/main/java/io/github/hectorvent/floci/services/docdb/model/DocDbInstance.java+++ b/src/main/java/io/github/hectorvent/floci/services/docdb/model/DocDbInstance.java@@ -17,6 +17,11 @@ public class DocDbInstance {private int port;private boolean iamDatabaseAuthenticationEnabled;private String dbInstanceArn;+ // AWS defaults minor-version upgrades to on when CreateDBInstance omits the flag+ private boolean autoMinorVersionUpgrade = true;+ private String preferredMaintenanceWindow;+ private boolean copyTagsToSnapshot;+ private int promotionTier = 1;private String dbiResourceId;private Instant createdAt;@@ -48,6 +53,15 @@ public class DocDbInstance {this.iamDatabaseAuthenticationEnabled = iamDatabaseAuthenticationEnabled;}+ public boolean isAutoMinorVersionUpgrade() { return autoMinorVersionUpgrade; }+ public void setAutoMinorVersionUpgrade(boolean autoMinorVersionUpgrade) { this.autoMinorVersionUpgrade = autoMinorVersionUpgrade; }+ 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 int getPromotionTier() { return promotionTier; }+ public void setPromotionTier(int promotionTier) { this.promotionTier = promotionTier; }+public String getDbInstanceArn() { return dbInstanceArn; }public void setDbInstanceArn(String dbInstanceArn) { this.dbInstanceArn = dbInstanceArn; }diff --git a/src/main/java/io/github/hectorvent/floci/services/docdb/model/DocDbInstanceSettings.java b/src/main/java/io/github/hectorvent/floci/services/docdb/model/DocDbInstanceSettings.javanew file mode 100644index 0000000000..48434cbd22--- /dev/null+++ b/src/main/java/io/github/hectorvent/floci/services/docdb/model/DocDbInstanceSettings.java@@ -0,0 +1,37 @@+package io.github.hectorvent.floci.services.docdb.model;++import io.github.hectorvent.floci.core.common.BackupWindows;+import io.quarkus.runtime.annotations.RegisterForReflection;++/** The settings of a DocumentDB instance as a request carries them; null means left out. */+@RegisterForReflection+public record DocDbInstanceSettings(Boolean autoMinorVersionUpgrade,+ String preferredMaintenanceWindow,+ Boolean copyTagsToSnapshot,+ Integer promotionTier) {++ public static DocDbInstanceSettings defaults() {+ return new DocDbInstanceSettings(null, null, null, null);+ }++ public void validate() {+ if (preferredMaintenanceWindow != null) {+ BackupWindows.parseMaintenanceWindow(preferredMaintenanceWindow);+ }+ }++ public void applyTo(DocDbInstance instance) {+ if (autoMinorVersionUpgrade != null) {+ instance.setAutoMinorVersionUpgrade(autoMinorVersionUpgrade);+ }+ if (preferredMaintenanceWindow != null && !preferredMaintenanceWindow.isBlank()) {+ instance.setPreferredMaintenanceWindow(BackupWindows.lowerCase(preferredMaintenanceWindow));+ }+ if (copyTagsToSnapshot != null) {+ instance.setCopyTagsToSnapshot(copyTagsToSnapshot);+ }+ if (promotionTier != null) {+ instance.setPromotionTier(promotionTier);+ }+ }+}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.javaindex d9d01a01aa..4302f94683 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@@ -90,7 +90,12 @@ public class RdsService implements Resettable, ResourceProvider {managedDefault("postgres15"),managedDefault("postgres16"),managedDefault("postgres17"),- managedDefault("postgres18"));+ managedDefault("postgres18"),+ // DocumentDB clusters name these as their default (the families the DocDB engine+ // versions map to); DocDbService validates a cluster's parameter group here+ managedDefault("docdb3.6"),+ managedDefault("docdb4.0"),+ managedDefault("docdb5.0"));/*** Engines AWS accepts for {@code EngineName} on an option group. Floci can only run@@ -2642,6 +2647,11 @@ public class RdsService implements Resettable, ResourceProvider {return unique.values();}+ /** Whether a name is one of the default cluster parameter groups the service itself provides. */+ public boolean isManagedClusterParameterGroup(String name) {+ return managedClusterParameterGroup(name) != null;+ }+private static ManagedClusterParameterGroup managedClusterParameterGroup(String name) {for (ManagedClusterParameterGroup group : MANAGED_CLUSTER_PARAMETER_GROUPS) {if (group.name().equals(name)) {diff --git a/src/test/java/io/github/hectorvent/floci/services/docdb/DocDbClusterSettingsIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/docdb/DocDbClusterSettingsIntegrationTest.javanew file mode 100644index 0000000000..9af118446f--- /dev/null+++ b/src/test/java/io/github/hectorvent/floci/services/docdb/DocDbClusterSettingsIntegrationTest.java@@ -0,0 +1,116 @@+package io.github.hectorvent.floci.services.docdb;++import io.github.hectorvent.floci.services.kms.KmsService;+import io.quarkus.test.junit.QuarkusTest;+import jakarta.inject.Inject;+import io.quarkus.test.junit.QuarkusTestProfile;+import io.quarkus.test.junit.TestProfile;+import org.junit.jupiter.api.AfterEach;+import org.junit.jupiter.api.Test;++import java.util.Map;++import static io.restassured.RestAssured.given;+import static io.restassured.http.ContentType.URLENC;+import static org.hamcrest.Matchers.containsString;++/**+ * The settings a DocumentDB cluster is created with come back from DescribeDBClusters, as on a+ * live account — through the Query protocol, with a real KMS key from the KMS store and the+ * default subnet group, parameter group and security group.+ */+@QuarkusTest+@TestProfile(DocDbClusterSettingsIntegrationTest.NoContainersProfile.class)+class DocDbClusterSettingsIntegrationTest {++ public static class NoContainersProfile implements QuarkusTestProfile {+ @Override+ public Map<String, String> getConfigOverrides() {+ return Map.of("floci.services.rds.mock", "true", "floci.services.docdb.mock", "true");+ }+ }++ private static final String ID = "settings-cluster";++ @Inject+ KmsService kmsService;++ private static io.restassured.specification.RequestSpecification rds(String action) {+ return given().header("Authorization",+ "AWS4-HMAC-SHA256 Credential=test/20260615/us-east-1/rds/aws4_request, "+ + "SignedHeaders=content-type;host, Signature=test")+ .contentType(URLENC)+ .formParam("Action", action)+ .formParam("Version", "2014-10-31");+ }++ @AfterEach+ void cleanUp() {+ rds("DeleteDBInstance").formParam("DBInstanceIdentifier", ID + "-1").when().post("/");+ rds("DeleteDBCluster").formParam("DBClusterIdentifier", ID).formParam("SkipFinalSnapshot", "true").when().post("/");+ }++ @Test+ void settingsGivenOnCreateComeBackFromDescribeAndModifyChangesThem() {+ String keyArn = kmsService.createKey("docdb settings", "us-east-1").getArn();++ rds("CreateDBCluster")+ .formParam("DBClusterIdentifier", ID)+ .formParam("Engine", "docdb")+ .formParam("MasterUsername", "u")+ .formParam("MasterUserPassword", "secret99password")+ .formParam("StorageEncrypted", "true")+ .formParam("KmsKeyId", keyArn)+ .formParam("BackupRetentionPeriod", "5")+ .formParam("PreferredBackupWindow", "23:30-00:00")+ .formParam("PreferredMaintenanceWindow", "Sun:03:00-Sun:04:00")+ .formParam("Tags.Tag.1.Key", "Name")+ .formParam("Tags.Tag.1.Value", ID)+ .when().post("/")+ .then().statusCode(200)+ .body(containsString("<KmsKeyId>" + keyArn + "</KmsKeyId>"));++ rds("DescribeDBClusters").formParam("DBClusterIdentifier", ID)+ .when().post("/")+ .then().statusCode(200)+ .body(containsString("<DBSubnetGroup>default</DBSubnetGroup>"))+ .body(containsString("<DBClusterParameterGroup>default.docdb5.0</DBClusterParameterGroup>"))+ .body(containsString("<StorageEncrypted>true</StorageEncrypted>"))+ .body(containsString("<KmsKeyId>" + keyArn + "</KmsKeyId>"))+ .body(containsString("<BackupRetentionPeriod>5</BackupRetentionPeriod>"))+ .body(containsString("<PreferredBackupWindow>23:30-00:00</PreferredBackupWindow>"))+ .body(containsString("<PreferredMaintenanceWindow>sun:03:00-sun:04:00</PreferredMaintenanceWindow>"))+ .body(containsString("<VpcSecurityGroupId>sg-"));++ rds("ListTagsForResource").formParam("ResourceName", "arn:aws:rds:us-east-1:000000000000:cluster:" + ID)+ .when().post("/").then().statusCode(200).body(containsString("<Value>" + ID + "</Value>"));++ rds("CreateDBInstance")+ .formParam("DBInstanceIdentifier", ID + "-1")+ .formParam("DBInstanceClass", "db.t3.medium")+ .formParam("Engine", "docdb")+ .formParam("DBClusterIdentifier", ID)+ .formParam("AutoMinorVersionUpgrade", "false")+ .formParam("Tags.Tag.1.Key", "Name")+ .formParam("Tags.Tag.1.Value", ID + "-1")+ .when().post("/")+ .then().statusCode(200)+ .body(containsString("<AutoMinorVersionUpgrade>false</AutoMinorVersionUpgrade>"))+ .body(containsString("<PreferredMaintenanceWindow>sun:03:00-sun:04:00</PreferredMaintenanceWindow>"))+ .body(containsString("<StorageEncrypted>true</StorageEncrypted>"));++ rds("ModifyDBCluster").formParam("DBClusterIdentifier", ID)+ .formParam("BackupRetentionPeriod", "7")+ .formParam("PreferredBackupWindow", "02:00-02:30")+ .when().post("/")+ .then().statusCode(200)+ .body(containsString("<BackupRetentionPeriod>7</BackupRetentionPeriod>"))+ .body(containsString("<PreferredBackupWindow>02:00-02:30</PreferredBackupWindow>"));++ rds("ModifyDBCluster").formParam("DBClusterIdentifier", ID)+ .formParam("PreferredMaintenanceWindow", "mon:02:15-mon:02:45")+ .when().post("/")+ .then().statusCode(400)+ .body(containsString("The backup window and maintenance window must not overlap."));+ }+}diff --git a/src/test/java/io/github/hectorvent/floci/services/docdb/DocDbLegacyMigrationRaceTest.java b/src/test/java/io/github/hectorvent/floci/services/docdb/DocDbLegacyMigrationRaceTest.javaindex 2720f224e5..e7b8ab65fe 100644--- a/src/test/java/io/github/hectorvent/floci/services/docdb/DocDbLegacyMigrationRaceTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/docdb/DocDbLegacyMigrationRaceTest.java@@ -10,6 +10,9 @@ import io.github.hectorvent.floci.services.docdb.container.DocDbContainerManagerimport io.github.hectorvent.floci.services.docdb.model.DocDbCluster;import io.github.hectorvent.floci.services.docdb.model.DocDbInstance;import org.junit.jupiter.api.Test;+import io.github.hectorvent.floci.services.ec2.Ec2Service;+import io.github.hectorvent.floci.services.kms.KmsService;+import io.github.hectorvent.floci.services.rds.RdsService;import org.mockito.Mockito;import java.util.concurrent.CountDownLatch;@@ -51,7 +54,8 @@ class DocDbLegacyMigrationRaceTest {when(servicesConfig.docdb()).thenReturn(docdbConfig);when(docdbConfig.mock()).thenReturn(true);service = new DocDbService(config, new RegionResolver("us-east-1", "000000000000"),- Mockito.mock(DocDbContainerManager.class), storageFactory);+ Mockito.mock(DocDbContainerManager.class), storageFactory,+ Mockito.mock(RdsService.class), Mockito.mock(Ec2Service.class), Mockito.mock(KmsService.class));}private void freshService() {@@ -70,7 +74,8 @@ class DocDbLegacyMigrationRaceTest {when(docdbConfig.mock()).thenReturn(true);service = new DocDbService(config, new RegionResolver("us-east-1", "000000000000"),- Mockito.mock(DocDbContainerManager.class), storageFactory);+ Mockito.mock(DocDbContainerManager.class), storageFactory,+ Mockito.mock(RdsService.class), Mockito.mock(Ec2Service.class), Mockito.mock(KmsService.class));}@Testdiff --git a/src/test/java/io/github/hectorvent/floci/services/docdb/DocDbServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/docdb/DocDbServiceTest.javaindex 96ec80201f..039152a8b5 100644--- a/src/test/java/io/github/hectorvent/floci/services/docdb/DocDbServiceTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/docdb/DocDbServiceTest.java@@ -3,6 +3,7 @@ package io.github.hectorvent.floci.services.docdb;import io.github.hectorvent.floci.config.EmulatorConfig;import io.github.hectorvent.floci.core.common.AwsException;import io.github.hectorvent.floci.core.common.RegionResolver;+import io.github.hectorvent.floci.core.common.BackupWindows;import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend;import io.github.hectorvent.floci.core.storage.StorageBackend;import io.github.hectorvent.floci.core.storage.StorageFactory;@@ -11,8 +12,21 @@ import io.github.hectorvent.floci.services.docdb.model.DocDbCluster;import io.github.hectorvent.floci.services.docdb.model.DocDbInstance;import org.junit.jupiter.api.BeforeEach;import org.junit.jupiter.api.Test;+import io.github.hectorvent.floci.services.docdb.model.DocDbClusterSettings;+import io.github.hectorvent.floci.services.docdb.model.DocDbInstanceSettings;+import io.github.hectorvent.floci.services.ec2.Ec2Service;+import io.github.hectorvent.floci.services.ec2.model.SecurityGroup;+import io.github.hectorvent.floci.services.kms.KmsService;+import io.github.hectorvent.floci.services.kms.model.KmsKey;+import io.github.hectorvent.floci.services.rds.RdsService;+import io.github.hectorvent.floci.services.rds.model.DbSubnetGroup;+import io.github.hectorvent.floci.services.rds.model.DbClusterParameterGroup;import org.mockito.Mockito;+import static org.junit.jupiter.api.Assertions.assertNull;+import static org.mockito.ArgumentMatchers.anyList;+import java.util.List;+import java.util.Map;import static org.junit.jupiter.api.Assertions.assertEquals;import static org.junit.jupiter.api.Assertions.assertFalse;import static org.junit.jupiter.api.Assertions.assertNotNull;@@ -29,6 +43,9 @@ class DocDbServiceTest {private DocDbService docDbService;private DocDbContainerManager containerManager;+ private RdsService rdsService;+ private Ec2Service ec2Service;+ private KmsService kmsService;@BeforeEachvoid setUp() {@@ -45,7 +62,14 @@ class DocDbServiceTest {containerManager = Mockito.mock(DocDbContainerManager.class);RegionResolver regionResolver = new RegionResolver("us-east-1", "000000000000");- docDbService = new DocDbService(config, regionResolver, containerManager, storageFactory);+ rdsService = Mockito.mock(RdsService.class);+ ec2Service = Mockito.mock(Ec2Service.class);+ kmsService = Mockito.mock(KmsService.class);+ when(kmsService.describeKey(any(), any())).thenThrow(new AwsException("NotFoundException", "Key not found", 404));+ when(rdsService.isManagedClusterParameterGroup(any())).thenAnswer(inv ->+ List.of("default.docdb3.6", "default.docdb4.0", "default.docdb5.0").contains(inv.<String>getArgument(0)));+ docDbService = new DocDbService(config, regionResolver, containerManager, storageFactory,+ rdsService, ec2Service, kmsService);}@Test@@ -145,7 +169,8 @@ class DocDbServiceTest {when(noDaemonContainerManager.tryStart(anyString(), anyString(), anyString(), anyString())).thenReturn(null);RegionResolver regionResolver = new RegionResolver("us-east-1", "000000000000");- DocDbService noDaemonService = new DocDbService(config, regionResolver, noDaemonContainerManager, storageFactory);+ DocDbService noDaemonService = new DocDbService(config, regionResolver, noDaemonContainerManager, storageFactory,+ rdsService, ec2Service, kmsService);DocDbCluster created = noDaemonService.createDbCluster("no-docker-cluster", null, "admin", "secret", false);@@ -209,7 +234,8 @@ class DocDbServiceTest {when(docdbConfig.mock()).thenReturn(true);DocDbService service = new DocDbService(config,new RegionResolver("us-east-1", "000000000000"),- Mockito.mock(DocDbContainerManager.class), storageFactory);+ Mockito.mock(DocDbContainerManager.class), storageFactory,+ Mockito.mock(RdsService.class), Mockito.mock(Ec2Service.class), Mockito.mock(KmsService.class));DocDbCluster legacy = new DocDbCluster();legacy.setDbClusterIdentifier("bare-key-cluster");@@ -229,4 +255,308 @@ class DocDbServiceTest {// And it lists there, once rather than twice.assertEquals(1, service.listDbClusters(null).size());}++ private static final String KEY_ARN = "arn:aws:kms:us-east-1:000000000000:key/k1";++ private void knownReferences() {+ when(rdsService.getDbSubnetGroup(eq("sng"), any())).thenReturn(new DbSubnetGroup());+ when(rdsService.getDbSubnetGroup(eq("nope"), any())).thenThrow(new AwsException("DBSubnetGroupNotFoundFault", "x", 404));+ when(rdsService.getDbClusterParameterGroup(eq("pg"), any())).thenReturn(new DbClusterParameterGroup("pg", "docdb5.0", "d"));+ when(rdsService.getDbClusterParameterGroup(eq("nope"), any())).thenThrow(new AwsException("DBClusterParameterGroupNotFound", "x", 404));+ SecurityGroup sg = new SecurityGroup();+ sg.setGroupId("sg-1");+ when(ec2Service.describeSecurityGroups(any(), eq(List.of("sg-1")), any(), any())).thenReturn(List.of(sg));+ when(ec2Service.describeSecurityGroups(any(), eq(List.of("sg-nope")), any(), any())).thenReturn(List.of());+ KmsKey key = new KmsKey();+ key.setKeyId("k1");+ key.setArn(KEY_ARN);+ key.setEnabled(true);+ key.setKeyState("Enabled");+ Mockito.doReturn(key).when(kmsService).describeKey("alias/docs", "us-east-1");+ Mockito.doReturn(key).when(kmsService).describeKey(KEY_ARN, "us-east-1");+ }++ @Test+ void createDbClusterStoresPlacementEncryptionBackupSettingsAndTags() {+ knownReferences();+ DocDbCluster created = docDbService.createDbCluster("c1", "5.0.0", "u", "pw", false,+ new DocDbClusterSettings("sng", "pg", List.of("sg-1"), true, "alias/docs", 5,+ "23:30-00:00", "Sun:03:00-Sun:04:00", true),+ Map.of("Name", "c1"));++ DocDbCluster stored = docDbService.getDbCluster("c1");+ assertEquals("sng", stored.getDbSubnetGroupName());+ assertEquals("pg", stored.getDbClusterParameterGroupName());+ assertEquals(List.of("sg-1"), stored.getVpcSecurityGroupIds());+ assertTrue(stored.isStorageEncrypted());+ assertEquals(KEY_ARN, stored.getKmsKeyId());+ assertEquals(5, stored.getBackupRetentionPeriod());+ assertEquals("23:30-00:00", stored.getPreferredBackupWindow());+ assertEquals("sun:03:00-sun:04:00", stored.getPreferredMaintenanceWindow());+ assertTrue(stored.isDeletionProtection());+ assertEquals(Map.of("Name", "c1"), stored.getTags());+ assertEquals(created.getDbClusterArn(), stored.getDbClusterArn());+ }++ @Test+ void createDbClusterWithoutSettingsTakesAwsDefaults() {+ docDbService.createDbCluster("c1", "5.0.0", "u", "pw", false);+ DocDbCluster stored = docDbService.getDbCluster("c1");+ assertEquals("default", stored.getDbSubnetGroupName());+ assertEquals("default.docdb5.0", stored.getDbClusterParameterGroupName());+ assertEquals(List.of(Ec2Service.defaultSecurityGroupId("us-east-1")), stored.getVpcSecurityGroupIds());+ assertFalse(stored.isStorageEncrypted());+ assertNull(stored.getKmsKeyId());+ assertEquals(1, stored.getBackupRetentionPeriod());+ assertEquals("04:00-06:00", stored.getPreferredBackupWindow());+ assertEquals("mon:00:00-mon:03:00", stored.getPreferredMaintenanceWindow());+ assertFalse(stored.isDeletionProtection());+ }++ @Test+ void recordsPersistedBeforeSettingsWereStoredReadBackWithTheAwsDefaults() {+ StorageBackend<String, DocDbCluster> clusterStore = AccountAwareStorageBackend.inMemory("000000000000");+ StorageBackend<String, DocDbInstance> instanceStore = AccountAwareStorageBackend.inMemory("000000000000");+ StorageFactory storageFactory = Mockito.mock(StorageFactory.class);+ when(storageFactory.create(anyString(), anyString(), any())).thenAnswer(inv ->+ "docdb-clusters.json".equals(inv.getArgument(1)) ? clusterStore : instanceStore);+ EmulatorConfig config = Mockito.mock(EmulatorConfig.class);+ var servicesConfig = Mockito.mock(EmulatorConfig.ServicesConfig.class);+ var docdbConfig = Mockito.mock(EmulatorConfig.DocDbServiceConfig.class);+ when(config.services()).thenReturn(servicesConfig);+ when(servicesConfig.docdb()).thenReturn(docdbConfig);+ when(docdbConfig.mock()).thenReturn(true);+ DocDbService service = new DocDbService(config, new RegionResolver("us-east-1", "000000000000"),+ containerManager, storageFactory, rdsService, ec2Service, kmsService);++ DocDbCluster legacy = new DocDbCluster();+ legacy.setDbClusterIdentifier("old");+ legacy.setEngineVersion("4.0.0");+ legacy.setDbClusterArn("arn:aws:rds:us-east-1:000000000000:cluster:old");+ clusterStore.put("us-east-1::old", legacy);+ DocDbInstance legacyInstance = new DocDbInstance();+ legacyInstance.setDbInstanceIdentifier("old-1");+ legacyInstance.setDbClusterIdentifier("old");+ legacyInstance.setDbInstanceArn("arn:aws:rds:us-east-1:000000000000:db:old-1");+ instanceStore.put("us-east-1::old-1", legacyInstance);++ for (DocDbCluster read : List.of(service.getDbCluster("old"),+ service.listDbClusters(null).iterator().next(),+ service.listDbClusters("arn:aws:rds:us-east-1:000000000000:cluster:old").iterator().next())) {+ assertEquals("default", read.getDbSubnetGroupName());+ assertEquals("default.docdb4.0", read.getDbClusterParameterGroupName());+ assertEquals(List.of(Ec2Service.defaultSecurityGroupId("us-east-1")), read.getVpcSecurityGroupIds());+ assertEquals(1, read.getBackupRetentionPeriod());+ assertEquals("04:00-06:00", read.getPreferredBackupWindow());+ assertEquals("mon:00:00-mon:03:00", read.getPreferredMaintenanceWindow());+ assertFalse(read.isStorageEncrypted());+ assertFalse(read.isDeletionProtection());+ }+ assertEquals("default", clusterStore.get("us-east-1::old").orElseThrow().getDbSubnetGroupName());++ DocDbInstance readInstance = service.getDbInstance("old-1");+ assertEquals("mon:00:00-mon:03:00", readInstance.getPreferredMaintenanceWindow());+ assertTrue(readInstance.isAutoMinorVersionUpgrade());+ assertEquals(1, readInstance.getPromotionTier());+ assertEquals("mon:00:00-mon:03:00",+ instanceStore.get("us-east-1::old-1").orElseThrow().getPreferredMaintenanceWindow());+ }++ @Test+ void deleteDbClusterRefusesAProtectedClusterUntilProtectionIsTurnedOff() {+ docDbService.createDbCluster("c1", "5.0.0", "u", "pw", false,+ new DocDbClusterSettings(null, null, null, null, null, null, null, null, true), Map.of());+ AwsException e = assertThrows(AwsException.class, () -> docDbService.deleteDbCluster("c1"));+ assertEquals("InvalidParameterCombination", e.getErrorCode());+ assertEquals("Cannot delete protected Cluster, please disable deletion protection and try again.", e.getMessage());+ assertEquals("available", docDbService.getDbCluster("c1").getStatus());++ docDbService.modifyDbCluster("c1", null, null,+ new DocDbClusterSettings(null, null, null, null, null, null, null, null, false));+ docDbService.deleteDbCluster("c1");+ assertThrows(AwsException.class, () -> docDbService.getDbCluster("c1"));+ }++ @Test+ void parameterGroupMustBelongToTheEngineVersionTheClusterWillRun() {+ knownReferences();+ when(rdsService.getDbClusterParameterGroup(eq("pg4"), any()))+ .thenReturn(new DbClusterParameterGroup("pg4", "docdb4.0", "d"));+ String expected = "The Parameter Group pg4 with DBParameterGroupFamily docdb4.0 cannot be used for this "+ + "instance. Please use a Parameter Group with DBParameterGroupFamily docdb5.0";++ AwsException e = refused(new DocDbClusterSettings(null, "pg4", null, null, null, null, null, null, null));+ assertEquals("InvalidParameterCombination", e.getErrorCode());+ assertEquals(expected, e.getMessage());++ docDbService.createDbCluster("c1", "5.0.0", "u", "pw", false);+ e = assertThrows(AwsException.class, () -> docDbService.modifyDbCluster("c1", null, null,+ new DocDbClusterSettings(null, "pg4", null, null, null, null, null, null, null)));+ assertEquals(expected, e.getMessage());+ assertEquals("default.docdb5.0", docDbService.getDbCluster("c1").getDbClusterParameterGroupName());++ // judged against the version the cluster will have, not the one it has+ e = assertThrows(AwsException.class, () -> docDbService.modifyDbCluster("c1", "4.0.0", null,+ new DocDbClusterSettings(null, "pg", null, null, null, null, null, null, null)));+ assertEquals("The Parameter Group pg with DBParameterGroupFamily docdb5.0 cannot be used for this "+ + "instance. Please use a Parameter Group with DBParameterGroupFamily docdb4.0", e.getMessage());+ assertEquals("5.0.0", docDbService.getDbCluster("c1").getEngineVersion());++ docDbService.modifyDbCluster("c1", "4.0.0", null, new DocDbClusterSettings(null, "pg4", null, null, null, null, null, null, null));+ assertEquals("pg4", docDbService.getDbCluster("c1").getDbClusterParameterGroupName());++ // a custom group of the old family is not carried across an engine version change+ e = assertThrows(AwsException.class, () -> docDbService.modifyDbCluster("c1", "5.0.0", null));+ assertEquals("InvalidParameterCombination", e.getErrorCode());+ assertEquals("The current DB cluster parameter group pg4 is custom. You must explicitly specify a new "+ + "DB cluster parameter group, either default or custom, for the engine version upgrade.", e.getMessage());+ assertEquals("4.0.0", docDbService.getDbCluster("c1").getEngineVersion());+ docDbService.modifyDbCluster("c1", "5.0.0", null, new DocDbClusterSettings(null, "pg", null, null, null, null, null, null, null));+ assertEquals("pg", docDbService.getDbCluster("c1").getDbClusterParameterGroupName());+ assertEquals("5.0.0", docDbService.getDbCluster("c1").getEngineVersion());++ // the group's stored family decides, whatever its name says+ when(rdsService.getDbClusterParameterGroup(eq("mine-docdb5.0"), any()))+ .thenReturn(new DbClusterParameterGroup("mine-docdb5.0", "docdb4.0", "d"));+ docDbService.createDbCluster("c3", "4.0.0", "u", "pw", false,+ new DocDbClusterSettings(null, "mine-docdb5.0", null, null, null, null, null, null, null), Map.of());+ e = assertThrows(AwsException.class, () -> docDbService.modifyDbCluster("c3", "5.0.0", null));+ assertEquals("InvalidParameterCombination", e.getErrorCode());+ assertEquals("4.0.0", docDbService.getDbCluster("c3").getEngineVersion());++ // a custom group is not a default one because of its name+ when(rdsService.getDbClusterParameterGroup(eq("default.mine"), any()))+ .thenReturn(new DbClusterParameterGroup("default.mine", "docdb4.0", "d"));+ docDbService.createDbCluster("c4", "4.0.0", "u", "pw", false,+ new DocDbClusterSettings(null, "default.mine", null, null, null, null, null, null, null), Map.of());+ e = assertThrows(AwsException.class, () -> docDbService.modifyDbCluster("c4", "5.0.0", null));+ assertEquals("InvalidParameterCombination", e.getErrorCode());+ assertEquals("default.mine", docDbService.getDbCluster("c4").getDbClusterParameterGroupName());++ // a cluster on a default group follows the engine version to the new family's default+ docDbService.createDbCluster("c2", "4.0.0", "u", "pw", false);+ docDbService.modifyDbCluster("c2", "5.0.0", null);+ assertEquals("default.docdb5.0", docDbService.getDbCluster("c2").getDbClusterParameterGroupName());+ }++ @Test+ void aWindowThatLeavesNoRoomForTheDerivedOneIsRefused() {+ AwsException e = refused(new DocDbClusterSettings(null, null, null, null, null, null, "00:00-23:45", null, null));+ assertEquals("InvalidParameterValue", e.getErrorCode());+ assertEquals("The specified backup window overlaps all available default maintenance windows. "+ + "Shrink the backup window or specify a non-overlapping maintenance window.", e.getMessage());+ e = refused(new DocDbClusterSettings(null, null, null, null, null, null, "23:00-22:59", null, null));+ assertEquals("The specified backup window overlaps all available default maintenance windows. "+ + "Shrink the backup window or specify a non-overlapping maintenance window.", e.getMessage());+ e = refused(new DocDbClusterSettings(null, null, null, null, null, null, null, "mon:00:00-mon:23:45", null));+ assertEquals("The specified maintenance window overlaps all available default backup windows. "+ + "Shrink the maintenance window or specify a non-overlapping backup window.", e.getMessage());++ docDbService.createDbCluster("c1", "5.0.0", "u", "pw", false,+ new DocDbClusterSettings(null, null, null, null, null, null, "22:00-23:50", null, null), Map.of());+ DocDbCluster stored = docDbService.getDbCluster("c1");+ assertEquals("22:00-23:50", stored.getPreferredBackupWindow());+ assertFalse(BackupWindows.overlap(stored.getPreferredBackupWindow(), stored.getPreferredMaintenanceWindow()));+ }++ private AwsException refused(DocDbClusterSettings settings) {+ return assertThrows(AwsException.class, () -> docDbService.createDbCluster(+ "c1", "5.0.0", "u", "pw", false, settings, Map.of()));+ }++ @Test+ void createDbClusterRefusesWhatALiveAccountRefusesBeforeStartingAContainer() {+ knownReferences();+ AwsException e;+ e = refused(new DocDbClusterSettings(null, null, null, false, KEY_ARN, null, null, null, null));+ assertEquals("InvalidParameterCombination", e.getErrorCode());+ assertEquals("You cannot specify KMS key for unencrypted clusters.", e.getMessage());+ e = refused(new DocDbClusterSettings(null, null, null, null, KEY_ARN, null, null, null, null));+ assertEquals("InvalidParameterCombination", e.getErrorCode());+ e = refused(new DocDbClusterSettings(null, null, null, true, "alias/nope", null, null, null, null));+ assertEquals("KMSKeyNotAccessibleFault", e.getErrorCode());+ assertEquals("The specified KMS key [alias/nope] does not exist, is not enabled or you do not have permissions to access it.", e.getMessage());+ e = refused(new DocDbClusterSettings("nope", null, null, null, null, null, null, null, null));+ assertEquals("DBSubnetGroupNotFoundFault", e.getErrorCode());+ assertEquals("DB subnet group 'nope' does not exist.", e.getMessage());+ e = refused(new DocDbClusterSettings(null, "nope", null, null, null, null, null, null, null));+ assertEquals("DBClusterParameterGroupNotFound", e.getErrorCode());+ assertEquals("DBClusterParameterGroup not found: nope", e.getMessage());+ e = refused(new DocDbClusterSettings(null, null, List.of("sg-nope"), null, null, null, null, null, null));+ assertEquals("InvalidParameterValue", e.getErrorCode());+ assertEquals("Invalid security group , groupId= sg-nope, groupName=.", e.getMessage());+ e = refused(new DocDbClusterSettings(null, null, null, null, null, 0, null, null, null));+ assertEquals("Invalid backup retention period: 0. Retention period must be between 1 and 354.", e.getMessage());+ e = refused(new DocDbClusterSettings(null, null, null, null, null, null, "25:00-26:00", null, null));+ assertEquals("Invalid backup window time '25:00' specified. Should be specified as a time hh24:mi (24H Clock UTC). Example: 03:15", e.getMessage());+ e = refused(new DocDbClusterSettings(null, null, null, null, null, null, "02:00-02:10", null, null));+ assertEquals("Backup window must be at least 30 minutes.", e.getMessage());+ e = refused(new DocDbClusterSettings(null, null, null, null, null, null, null, "xxx:00:00-xxx:01:00", null));+ assertEquals("Invalid maintenance window time 'xxx:00:00' specified. Should be specified as a time ddd:hh24:mi (24H Clock UTC). Example: Mon:00:15", e.getMessage());+ e = refused(new DocDbClusterSettings(null, null, null, null, null, null, "02:00-02:30", "tue:02:15-tue:02:45", null));+ assertEquals("The backup window and maintenance window must not overlap.", e.getMessage());+ assertThrows(AwsException.class, () -> docDbService.getDbCluster("c1"));+ verify(containerManager, never()).tryStart(any(), any(), any(), any());++ // a window given alone is paired with a default clear of it+ docDbService.createDbCluster("alone", "5.0.0", "u", "pw", false,+ new DocDbClusterSettings(null, null, null, null, null, null, "00:30-01:00", null, null), Map.of());+ assertEquals("mon:01:00-mon:01:30", docDbService.getDbCluster("alone").getPreferredMaintenanceWindow());+ }++ @Test+ void modifyDbClusterAppliesGivenSettingsChecksTheStoredWindowAndChangesNothingWhenRefused() {+ knownReferences();+ docDbService.createDbCluster("c1", "5.0.0", "u", "pw", false,+ new DocDbClusterSettings(null, null, null, true, KEY_ARN, 5, "01:00-01:30", "thu:10:00-thu:10:30", null),+ Map.of());++ docDbService.modifyDbCluster("c1", null, null,+ new DocDbClusterSettings(null, "pg", List.of("sg-1"), null, null, 7, "02:00-02:30", "wed:05:00-wed:06:00", true));+ DocDbCluster stored = docDbService.getDbCluster("c1");+ assertEquals(7, stored.getBackupRetentionPeriod());+ assertEquals("02:00-02:30", stored.getPreferredBackupWindow());+ assertEquals("wed:05:00-wed:06:00", stored.getPreferredMaintenanceWindow());+ assertEquals("pg", stored.getDbClusterParameterGroupName());+ assertEquals(List.of("sg-1"), stored.getVpcSecurityGroupIds());+ assertTrue(stored.isDeletionProtection());+ assertTrue(stored.isStorageEncrypted());+ assertEquals(KEY_ARN, stored.getKmsKeyId());++ AwsException overlap = assertThrows(AwsException.class, () -> docDbService.modifyDbCluster("c1", null, null,+ new DocDbClusterSettings(null, null, null, null, null, null, null, "mon:02:15-mon:02:45", null)));+ assertEquals("The backup window and maintenance window must not overlap.", overlap.getMessage());+ AwsException badPg = assertThrows(AwsException.class, () -> docDbService.modifyDbCluster("c1", null, null,+ new DocDbClusterSettings(null, "nope", null, null, null, 9, null, null, null)));+ assertEquals("DBClusterParameterGroupNotFound", badPg.getErrorCode());+ assertEquals(7, docDbService.getDbCluster("c1").getBackupRetentionPeriod());+ assertEquals("wed:05:00-wed:06:00", docDbService.getDbCluster("c1").getPreferredMaintenanceWindow());+ }++ @Test+ void createDbInstanceStoresSettingsAndTagsAndTakesTheClustersMaintenanceWindow() {+ knownReferences();+ docDbService.createDbCluster("c1", "5.0.0", "u", "pw", false,+ new DocDbClusterSettings(null, null, null, null, null, null, null, "thu:10:00-thu:10:30", null), Map.of());+ docDbService.createDbInstance("i1", "c1", "db.t3.medium", null, false,+ new DocDbInstanceSettings(false, "mon:05:00-mon:06:00", true, 2), Map.of("Name", "i1"));+ docDbService.createDbInstance("i2", "c1", "db.t3.medium", null, false);++ DocDbInstance i1 = docDbService.getDbInstance("i1");+ assertFalse(i1.isAutoMinorVersionUpgrade());+ assertEquals("mon:05:00-mon:06:00", i1.getPreferredMaintenanceWindow());+ assertTrue(i1.isCopyTagsToSnapshot());+ assertEquals(2, i1.getPromotionTier());+ assertEquals(Map.of("Name", "i1"), i1.getTags());+ DocDbInstance i2 = docDbService.getDbInstance("i2");+ assertTrue(i2.isAutoMinorVersionUpgrade());+ assertEquals("thu:10:00-thu:10:30", i2.getPreferredMaintenanceWindow());+ assertFalse(i2.isCopyTagsToSnapshot());+ assertEquals(1, i2.getPromotionTier());++ docDbService.modifyDbInstance("i2", null, null, new DocDbInstanceSettings(false, null, true, null));+ assertFalse(docDbService.getDbInstance("i2").isAutoMinorVersionUpgrade());+ assertTrue(docDbService.getDbInstance("i2").isCopyTagsToSnapshot());+ assertEquals("thu:10:00-thu:10:30", docDbService.getDbInstance("i2").getPreferredMaintenanceWindow());+ }}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.javaindex 8e8641ff9c..57da7632ce 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@@ -93,7 +93,10 @@ class RdsServiceTest {"postgres15","postgres16","postgres17",- "postgres18");+ "postgres18",+ "docdb3.6",+ "docdb4.0",+ "docdb5.0");private RdsService rdsService;private RdsContainerManager containerManager;