diff --git a/docs/services/redshift.md b/docs/services/redshift.md index af0dd94123..caa8ceaee3 100644 --- a/docs/services/redshift.md +++ b/docs/services/redshift.md @@ -23,7 +23,7 @@ For running SQL without a PostgreSQL wire connection (the way Lambda and Step Fu | `CreateClusterSnapshot` | Back up a cluster to a SQL dump via `pg_dump` | | `DescribeClusterSnapshots` | List snapshots, optionally filtered by snapshot or cluster identifier | | `DeleteClusterSnapshot` | Remove a snapshot and its stored dump | -| `RestoreFromClusterSnapshot` | Create a new cluster and load a snapshot's dump into it via `psql` | +| `RestoreFromClusterSnapshot` | Create a new cluster and load a snapshot's dump into it via `psql`; accepts `SnapshotIdentifier` or a same-account, same-region `SnapshotArn` | | `CreateClusterParameterGroup` | Register a parameter group (metadata only) | | `DescribeClusterParameterGroups` | List parameter groups, optionally filtered by name | | `DescribeClusterParameters` | Return the parameters of a group, with any values set by `ModifyClusterParameterGroup` | @@ -42,10 +42,13 @@ For running SQL without a PostgreSQL wire connection (the way Lambda and Step Fu | `CreateSnapshotCopyGrant` | Register a snapshot copy grant, defaulting `KmsKeyId` to the AWS-managed Redshift key. `SnapshotCopyGrantName` must be 1-63 characters, start with a lowercase letter, and contain only lowercase letters, digits and non-consecutive hyphens | | `DescribeSnapshotCopyGrants` | List snapshot copy grants, optionally filtered by name, paged with `MaxRecords` and `Marker` | | `DeleteSnapshotCopyGrant` | Remove a snapshot copy grant | -| `ModifyCluster` | Update node type, parameter group, security groups, or the master password | +| `ModifyCluster` | Update node type, parameter group, security groups, Multi-AZ flag, or the master password | | `DescribeClusterVersions` | Return the single emulated engine version and its parameter group family | | `DescribeOrderableClusterOptions` | Return the static node types and cluster types, optionally filtered by `NodeType` or `ClusterVersion` | | `ModifyClusterIamRoles` | Add or remove the IAM roles associated with a cluster; COPY and UNLOAD see the change on new connections. `DefaultIamRoleArn` is ignored | +| `DescribeLoggingStatus` | Return a cluster's stored audit-logging configuration | +| `EnableLogging` | Store audit-logging configuration (S3 bucket, or CloudWatch via `LogDestinationType`); no logs are delivered | +| `DisableLogging` | Clear a cluster's audit-logging configuration | | `RebootCluster` | Restart a cluster's container | | `GetClusterCredentials` | Issue a short-lived DbUser / DbPassword pair the auth proxy and Data API accept for a non-master user | | `GetClusterCredentialsWithIAM` | Issue short-lived credentials with the DbUser derived from the caller's IAM identity | diff --git a/src/main/java/io/github/hectorvent/floci/services/redshift/RedshiftQueryHandler.java b/src/main/java/io/github/hectorvent/floci/services/redshift/RedshiftQueryHandler.java index 97ab0adcc7..3194f2e80e 100644 --- a/src/main/java/io/github/hectorvent/floci/services/redshift/RedshiftQueryHandler.java +++ b/src/main/java/io/github/hectorvent/floci/services/redshift/RedshiftQueryHandler.java @@ -1,6 +1,7 @@ package io.github.hectorvent.floci.services.redshift; import io.github.hectorvent.floci.config.EmulatorConfig; +import io.github.hectorvent.floci.core.common.AwsArnUtils; import io.github.hectorvent.floci.core.common.AwsException; import io.github.hectorvent.floci.core.common.PaginatedResult; import io.github.hectorvent.floci.core.common.RegionResolver; @@ -91,6 +92,13 @@ public class RedshiftQueryHandler { clusterSubnetGroupName, vpcSecurityGroupIds) : service.createCluster(identifier, nodeType, masterUsername, masterUserPassword, clusterSubnetGroupName, vpcSecurityGroupIds, iamRoleArns); + String parameterGroupName = params.getFirst("ClusterParameterGroupName"); + String multiAz = params.getFirst("MultiAZ"); + boolean hasParameterGroup = parameterGroupName != null && !parameterGroupName.isBlank(); + if (hasParameterGroup || multiAz != null) { + cluster = service.modifyCluster(identifier, null, null, null, parameterGroupName, null, + multiAz == null ? null : Boolean.parseBoolean(multiAz)); + } String xml = new XmlBuilder() .start("CreateClusterResponse") .start("CreateClusterResult") @@ -193,8 +201,10 @@ public class RedshiftQueryHandler { case "RestoreFromClusterSnapshot" -> { String clusterIdentifier = params.getFirst("ClusterIdentifier"); String snapshotIdentifier = params.getFirst("SnapshotIdentifier"); + String snapshotArn = params.getFirst("SnapshotArn"); String nodeType = params.getFirst("NodeType"); - Cluster cluster = service.restoreFromClusterSnapshot(clusterIdentifier, snapshotIdentifier, nodeType); + Cluster cluster = service.restoreFromClusterSnapshot( + clusterIdentifier, resolveSnapshotIdentifier(snapshotIdentifier, snapshotArn, authorizationHeader), nodeType); String xml = new XmlBuilder() .start("RestoreFromClusterSnapshotResponse") .start("RestoreFromClusterSnapshotResult") @@ -563,8 +573,10 @@ public class RedshiftQueryHandler { String masterUserPassword = params.getFirst("MasterUserPassword"); String clusterParameterGroupName = params.getFirst("ClusterParameterGroupName"); List vpcSecurityGroupIds = memberList(params, "VpcSecurityGroupIds"); + String multiAz = params.getFirst("MultiAZ"); Cluster cluster = service.modifyCluster(clusterIdentifier, nodeType, numberOfNodes, - masterUserPassword, clusterParameterGroupName, vpcSecurityGroupIds); + masterUserPassword, clusterParameterGroupName, vpcSecurityGroupIds, + multiAz == null ? null : Boolean.parseBoolean(multiAz)); String xml = new XmlBuilder() .start("ModifyClusterResponse") .start("ModifyClusterResult") @@ -646,6 +658,21 @@ public class RedshiftQueryHandler { .build(); return Response.ok(xml).type(MediaType.APPLICATION_XML).build(); } + case "DescribeLoggingStatus" -> { + String clusterIdentifier = requireParam(params, "ClusterIdentifier"); + return loggingStatusResponse(action, service.describeLoggingStatus(clusterIdentifier)); + } + case "EnableLogging" -> { + String clusterIdentifier = requireParam(params, "ClusterIdentifier"); + Cluster cluster = service.enableLogging(clusterIdentifier, params.getFirst("BucketName"), + params.getFirst("S3KeyPrefix"), params.getFirst("LogDestinationType"), + memberList(params, "LogExports")); + return loggingStatusResponse(action, cluster); + } + case "DisableLogging" -> { + String clusterIdentifier = requireParam(params, "ClusterIdentifier"); + return loggingStatusResponse(action, service.disableLogging(clusterIdentifier)); + } case "RebootCluster" -> { String clusterIdentifier = params.getFirst("ClusterIdentifier"); if (clusterIdentifier == null || clusterIdentifier.isBlank()) { @@ -769,6 +796,8 @@ public class RedshiftQueryHandler { .elem("ClusterStatus", cluster.getClusterStatus()) .elem("ClusterAvailabilityStatus", availabilityStatus(cluster.getClusterStatus())) .elem("AvailabilityZoneRelocationStatus", "disabled") + // AWS always returns "enabled" or "disabled" here, never blank. + .elem("MultiAZ", cluster.isMultiAZ() ? "enabled" : "disabled") .elem("ClusterSubnetGroupName", cluster.getClusterSubnetGroupName()); if (cluster.getMasterPasswordSecretArn() != null) { @@ -792,14 +821,16 @@ public class RedshiftQueryHandler { builder.end("IamRoles"); } - if (cluster.getClusterParameterGroupName() != null) { - builder.start("ClusterParameterGroups") - .start("ClusterParameterGroup") - .elem("ParameterGroupName", cluster.getClusterParameterGroupName()) - .elem("ParameterApplyStatus", "in-sync") - .end("ClusterParameterGroup") - .end("ClusterParameterGroups"); - } + // Clusters persisted before the default was assigned on create have no name stored. + String parameterGroupName = cluster.getClusterParameterGroupName() != null + ? cluster.getClusterParameterGroupName() + : RedshiftService.DEFAULT_PARAMETER_GROUP_NAME; + builder.start("ClusterParameterGroups") + .start("ClusterParameterGroup") + .elem("ParameterGroupName", parameterGroupName) + .elem("ParameterApplyStatus", "in-sync") + .end("ClusterParameterGroup") + .end("ClusterParameterGroups"); if (cluster.getTags() != null && !cluster.getTags().isEmpty()) { builder.start("Tags"); @@ -837,18 +868,84 @@ public class RedshiftQueryHandler { }; } + // RestoreFromClusterSnapshot accepts SnapshotIdentifier or SnapshotArn. + private String resolveSnapshotIdentifier(String snapshotIdentifier, String snapshotArn, String authorizationHeader) { + if (snapshotIdentifier != null && !snapshotIdentifier.isBlank()) { + return snapshotIdentifier; + } + if (snapshotArn == null || snapshotArn.isBlank()) { + throw new AwsException("InvalidParameterValue", "SnapshotIdentifier or SnapshotArn is required", 400); + } + AwsArnUtils.Arn arn; + try { + arn = AwsArnUtils.parse(snapshotArn); + } catch (IllegalArgumentException e) { + throw new AwsException("InvalidParameterValue", "Invalid SnapshotArn: " + snapshotArn, 400); + } + // arn:aws:redshift:::snapshot:/ + String resource = arn.resource(); + int slash = resource.lastIndexOf('/'); + if (!"redshift".equals(arn.service()) || !resource.startsWith("snapshot:") + || slash < 0 || slash == resource.length() - 1) { + throw new AwsException("InvalidParameterValue", "Invalid SnapshotArn: " + snapshotArn, 400); + } + // An ARN from another account or region must not match a local snapshot by name. + if (!arn.accountId().equals(regionResolver.getAccountId()) + || !arn.region().equals(regionResolver.resolveRegionFromAuth(authorizationHeader))) { + throw new AwsException("ClusterSnapshotNotFound", "Snapshot " + snapshotArn + " not found", 404); + } + return resource.substring(slash + 1); + } + private String buildSnapshotXml(Snapshot snapshot) { + // Both are absent on snapshots persisted before these fields existed; omit them. + String createTime = snapshot.getSnapshotCreateTime() != null + ? DateTimeFormatter.ISO_INSTANT.format(snapshot.getSnapshotCreateTime()) + : null; XmlBuilder builder = new XmlBuilder() .start("Snapshot") .elem("SnapshotIdentifier", snapshot.getSnapshotIdentifier()) .elem("ClusterIdentifier", snapshot.getClusterIdentifier()) + .elem("SnapshotArn", snapshot.getSnapshotArn()) + .elem("SnapshotCreateTime", createTime) .elem("Status", snapshot.getStatus()) .elem("Port", String.valueOf(snapshot.getPort())) .elem("MasterUsername", snapshot.getMasterUsername()); - + return builder.end("Snapshot").build(); } + private Response loggingStatusResponse(String operation, Cluster cluster) { + String xml = new XmlBuilder() + .start(operation + "Response") + .start(operation + "Result") + .raw(buildLoggingStatusXml(cluster)) + .end(operation + "Result") + .start("ResponseMetadata") + .elem("RequestId", "test-req-id") + .end("ResponseMetadata") + .end(operation + "Response") + .build(); + return Response.ok(xml).type(MediaType.APPLICATION_XML).build(); + } + + // No log delivery is emulated, so delivery timestamps and failure fields are omitted. + private String buildLoggingStatusXml(Cluster cluster) { + XmlBuilder builder = new XmlBuilder() + .elem("LoggingEnabled", cluster.isLoggingEnabled()) + .elem("BucketName", cluster.getLoggingBucketName()) + .elem("S3KeyPrefix", cluster.getLoggingS3KeyPrefix()) + .elem("LogDestinationType", cluster.getLoggingDestinationType()); + if (cluster.getLoggingExports() != null) { + builder.start("LogExports"); + for (String export : cluster.getLoggingExports()) { + builder.elem("member", export); + } + builder.end("LogExports"); + } + return builder.build(); + } + private String buildClusterParameterGroupXml(ClusterParameterGroup group) { XmlBuilder builder = new XmlBuilder() .start("ClusterParameterGroup") diff --git a/src/main/java/io/github/hectorvent/floci/services/redshift/RedshiftService.java b/src/main/java/io/github/hectorvent/floci/services/redshift/RedshiftService.java index 6ee8a0c0e2..6457ee1a9b 100644 --- a/src/main/java/io/github/hectorvent/floci/services/redshift/RedshiftService.java +++ b/src/main/java/io/github/hectorvent/floci/services/redshift/RedshiftService.java @@ -57,6 +57,9 @@ import java.util.function.Consumer; public class RedshiftService { private static final Logger LOG = Logger.getLogger(RedshiftService.class); + // The group every cluster starts on until one is named. + static final String DEFAULT_PARAMETER_GROUP_NAME = "default.redshift-1.0"; + private final AccountAwareStorageBackend clusters; private final AccountAwareStorageBackend snapshots; private final AccountAwareStorageBackend parameterGroups; @@ -200,6 +203,8 @@ public class RedshiftService { cluster.setClusterSubnetGroupName(clusterSubnetGroupName); cluster.setVpcSecurityGroupIds(vpcSecurityGroupIds != null ? vpcSecurityGroupIds : List.of()); cluster.setIamRoleArns(iamRoleArns != null ? List.copyOf(iamRoleArns) : List.of()); + // A group named at create time is applied afterwards via modifyCluster. + cluster.setClusterParameterGroupName(DEFAULT_PARAMETER_GROUP_NAME); cluster.setClusterStatus("creating"); clusters.put(identifier, cluster); clusters.flush(); @@ -618,9 +623,16 @@ public class RedshiftService { return cluster; } + public Cluster modifyCluster(String clusterIdentifier, String nodeType, Integer numberOfNodes, + String masterUserPassword, String clusterParameterGroupName, + List vpcSecurityGroupIds) { + return modifyCluster(clusterIdentifier, nodeType, numberOfNodes, masterUserPassword, + clusterParameterGroupName, vpcSecurityGroupIds, null); + } + public synchronized Cluster modifyCluster(String clusterIdentifier, String nodeType, Integer numberOfNodes, String masterUserPassword, String clusterParameterGroupName, - List vpcSecurityGroupIds) { + List vpcSecurityGroupIds, Boolean multiAZ) { Cluster cluster = clusters.get(clusterIdentifier) .orElseThrow(() -> new AwsException("ClusterNotFound", "Cluster " + clusterIdentifier + " not found", 404)); @@ -648,6 +660,9 @@ public class RedshiftService { if (vpcSecurityGroupIds != null && !vpcSecurityGroupIds.isEmpty()) { cluster.setVpcSecurityGroupIds(vpcSecurityGroupIds); } + if (multiAZ != null) { + cluster.setMultiAZ(multiAZ); + } clusters.put(clusterIdentifier, cluster); clusters.flush(); @@ -710,6 +725,42 @@ public class RedshiftService { } } + public Cluster describeLoggingStatus(String clusterIdentifier) { + return clusters.get(clusterIdentifier) + .orElseThrow(() -> new AwsException("ClusterNotFound", "Cluster " + clusterIdentifier + " not found", 404)); + } + + public synchronized Cluster enableLogging(String clusterIdentifier, String bucketName, String s3KeyPrefix, + String logDestinationType, List logExports) { + Cluster cluster = clusters.get(clusterIdentifier) + .orElseThrow(() -> new AwsException("ClusterNotFound", "Cluster " + clusterIdentifier + " not found", 404)); + boolean cloudWatch = "cloudwatch".equalsIgnoreCase(logDestinationType); + if (!cloudWatch && (bucketName == null || bucketName.isBlank())) { + throw new AwsException("InvalidParameterValue", "BucketName is required for an S3 log destination", 400); + } + cluster.setLoggingEnabled(true); + cluster.setLoggingBucketName(bucketName); + cluster.setLoggingDestinationType(logDestinationType); + cluster.setLoggingExports(logExports == null || logExports.isEmpty() ? null : List.copyOf(logExports)); + cluster.setLoggingS3KeyPrefix(s3KeyPrefix); + clusters.put(clusterIdentifier, cluster); + clusters.flush(); + return cluster; + } + + public synchronized Cluster disableLogging(String clusterIdentifier) { + Cluster cluster = clusters.get(clusterIdentifier) + .orElseThrow(() -> new AwsException("ClusterNotFound", "Cluster " + clusterIdentifier + " not found", 404)); + cluster.setLoggingEnabled(false); + cluster.setLoggingBucketName(null); + cluster.setLoggingS3KeyPrefix(null); + cluster.setLoggingDestinationType(null); + cluster.setLoggingExports(null); + clusters.put(clusterIdentifier, cluster); + clusters.flush(); + return cluster; + } + public synchronized Cluster rebootCluster(String clusterIdentifier) { Cluster cluster = clusters.get(clusterIdentifier) .orElseThrow(() -> new AwsException("ClusterNotFound", "Cluster " + clusterIdentifier + " not found", 404)); @@ -855,6 +906,8 @@ public class RedshiftService { snapshot.setStatus("available"); snapshot.setMasterUsername(cluster.getMasterUsername()); snapshot.setMasterPassword(cluster.getMasterPassword()); + snapshot.setSnapshotArn(snapshotArn(clusterIdentifier, snapshotIdentifier)); + snapshot.setSnapshotCreateTime(Instant.now()); if (cluster.getEndpoint() != null) { snapshot.setPort(cluster.getEndpoint().getPort()); } else { @@ -967,6 +1020,7 @@ public class RedshiftService { cluster.setNodeType(effectiveNodeType); cluster.setMasterUsername(username); cluster.setMasterPassword(password); + cluster.setClusterParameterGroupName(DEFAULT_PARAMETER_GROUP_NAME); cluster.setClusterStatus("creating"); clusters.put(clusterIdentifier, cluster); clusters.flush(); @@ -1037,12 +1091,25 @@ public class RedshiftService { public List describeClusterParameterGroups(String parameterGroupName) { if (parameterGroupName != null && !parameterGroupName.isBlank()) { Optional group = parameterGroups.get(parameterGroupName); + if (group.isEmpty() && DEFAULT_PARAMETER_GROUP_NAME.equals(parameterGroupName)) { + return List.of(defaultParameterGroup()); + } if (group.isEmpty()) { throw new AwsException("ClusterParameterGroupNotFound", "Cluster parameter group " + parameterGroupName + " not found", 404); } return List.of(group.get()); } - return parameterGroups.scan(k -> true); + List all = new ArrayList<>(parameterGroups.scan(k -> true)); + if (all.stream().noneMatch(g -> DEFAULT_PARAMETER_GROUP_NAME.equals(g.getParameterGroupName()))) { + all.add(0, defaultParameterGroup()); + } + return all; + } + + // The implicit group every cluster references; not persisted, so it can't be deleted or drift. + private static ClusterParameterGroup defaultParameterGroup() { + return new ClusterParameterGroup(DEFAULT_PARAMETER_GROUP_NAME, "redshift-1.0", + "Default parameter group for redshift-1.0"); } public Optional getClusterParameterGroup(String parameterGroupName) { @@ -1115,8 +1182,9 @@ public class RedshiftService { public List describeClusterSubnetGroups(String name) { if (name != null && !name.isBlank()) { + // The Terraform AWS provider matches this exact "...Fault" code to detect a missing group. ClusterSubnetGroup group = subnetGroups.get(name) - .orElseThrow(() -> new AwsException("ClusterSubnetGroupNotFound", "Cluster subnet group " + name + " not found", 404)); + .orElseThrow(() -> new AwsException("ClusterSubnetGroupNotFoundFault", "Cluster subnet group " + name + " not found", 400)); return List.of(group); } return subnetGroups.scan(k -> true); @@ -1124,7 +1192,7 @@ public class RedshiftService { public synchronized ClusterSubnetGroup modifyClusterSubnetGroup(String name, String description, List subnetIds) { ClusterSubnetGroup group = subnetGroups.get(name) - .orElseThrow(() -> new AwsException("ClusterSubnetGroupNotFound", "Cluster subnet group " + name + " not found", 404)); + .orElseThrow(() -> new AwsException("ClusterSubnetGroupNotFoundFault", "Cluster subnet group " + name + " not found", 400)); if (description != null) { group.setDescription(description); } @@ -1138,7 +1206,7 @@ public class RedshiftService { public ClusterSubnetGroup deleteClusterSubnetGroup(String name) { ClusterSubnetGroup group = subnetGroups.get(name) - .orElseThrow(() -> new AwsException("ClusterSubnetGroupNotFound", "Cluster subnet group " + name + " not found", 404)); + .orElseThrow(() -> new AwsException("ClusterSubnetGroupNotFoundFault", "Cluster subnet group " + name + " not found", 400)); subnetGroups.delete(name); subnetGroups.flush(); return group; @@ -1401,7 +1469,7 @@ public class RedshiftService { } case "subnetgroup" -> { ClusterSubnetGroup group = subnetGroups.get(id) - .orElseThrow(() -> new AwsException("ClusterSubnetGroupNotFound", "Cluster subnet group " + id + " not found", 404)); + .orElseThrow(() -> new AwsException("ClusterSubnetGroupNotFoundFault", "Cluster subnet group " + id + " not found", 400)); yield new TagHandle(group.getTags(), updated -> { group.setTags(updated); subnetGroups.put(id, group); diff --git a/src/main/java/io/github/hectorvent/floci/services/redshift/model/Cluster.java b/src/main/java/io/github/hectorvent/floci/services/redshift/model/Cluster.java index 5331b018d2..59d1a8875b 100644 --- a/src/main/java/io/github/hectorvent/floci/services/redshift/model/Cluster.java +++ b/src/main/java/io/github/hectorvent/floci/services/redshift/model/Cluster.java @@ -46,6 +46,31 @@ public class Cluster { public List getIamRoleArns() { return iamRoleArns; } public void setIamRoleArns(List iamRoleArns) { this.iamRoleArns = iamRoleArns; } + // Audit logging config as set by EnableLogging; no log delivery is emulated. + private boolean loggingEnabled = false; + private String loggingBucketName; + private String loggingS3KeyPrefix; + + public boolean isLoggingEnabled() { return loggingEnabled; } + public void setLoggingEnabled(boolean loggingEnabled) { this.loggingEnabled = loggingEnabled; } + public String getLoggingBucketName() { return loggingBucketName; } + public void setLoggingBucketName(String loggingBucketName) { this.loggingBucketName = loggingBucketName; } + public String getLoggingS3KeyPrefix() { return loggingS3KeyPrefix; } + public void setLoggingS3KeyPrefix(String loggingS3KeyPrefix) { this.loggingS3KeyPrefix = loggingS3KeyPrefix; } + + private String loggingDestinationType; + private List loggingExports; + + public String getLoggingDestinationType() { return loggingDestinationType; } + public void setLoggingDestinationType(String loggingDestinationType) { this.loggingDestinationType = loggingDestinationType; } + public List getLoggingExports() { return loggingExports; } + public void setLoggingExports(List loggingExports) { this.loggingExports = loggingExports; } + + private boolean multiAZ = false; + + public boolean isMultiAZ() { return multiAZ; } + public void setMultiAZ(boolean multiAZ) { this.multiAZ = multiAZ; } + // Real backend address of this cluster's PostgreSQL container. `endpoint` now points at the // auth proxy, not the container, so the container address is kept here for proxy wiring and // for restarting the proxy after a reboot or an adopt-on-startup. diff --git a/src/main/java/io/github/hectorvent/floci/services/redshift/model/Snapshot.java b/src/main/java/io/github/hectorvent/floci/services/redshift/model/Snapshot.java index 9f6a7a825c..1e8a4bb1e4 100644 --- a/src/main/java/io/github/hectorvent/floci/services/redshift/model/Snapshot.java +++ b/src/main/java/io/github/hectorvent/floci/services/redshift/model/Snapshot.java @@ -1,6 +1,7 @@ package io.github.hectorvent.floci.services.redshift.model; import io.quarkus.runtime.annotations.RegisterForReflection; +import java.time.Instant; import java.util.LinkedHashMap; import java.util.Map; @@ -13,6 +14,8 @@ public class Snapshot { private String masterUsername; private String masterPassword; private String sqlDump; + private String snapshotArn; + private Instant snapshotCreateTime; public Snapshot() {} @@ -89,6 +92,22 @@ public class Snapshot { this.sqlDump = sqlDump; } + public String getSnapshotArn() { + return snapshotArn; + } + + public void setSnapshotArn(String snapshotArn) { + this.snapshotArn = snapshotArn; + } + + public Instant getSnapshotCreateTime() { + return snapshotCreateTime; + } + + public void setSnapshotCreateTime(Instant snapshotCreateTime) { + this.snapshotCreateTime = snapshotCreateTime; + } + private Map tags = new LinkedHashMap<>(); public Map getTags() { return tags; } diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/RedshiftClusterCfnIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/RedshiftClusterCfnIntegrationTest.java index 6fabacf64c..71682fe480 100644 --- a/src/test/java/io/github/hectorvent/floci/services/cloudformation/RedshiftClusterCfnIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/RedshiftClusterCfnIntegrationTest.java @@ -283,8 +283,8 @@ class RedshiftClusterCfnIntegrationTest { .formParam("Action", "DescribeClusterSubnetGroups") .formParam("ClusterSubnetGroupName", sgName) .when().post("/").then() - .statusCode(404) - .body(containsString("ClusterSubnetGroupNotFound")); + .statusCode(400) + .body(containsString("ClusterSubnetGroupNotFoundFault")); } private static String outputValue(String xml, String key) { diff --git a/src/test/java/io/github/hectorvent/floci/services/redshift/RedshiftQueryHandlerTest.java b/src/test/java/io/github/hectorvent/floci/services/redshift/RedshiftQueryHandlerTest.java index 6071d5c8c4..6183c2ea8f 100644 --- a/src/test/java/io/github/hectorvent/floci/services/redshift/RedshiftQueryHandlerTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/redshift/RedshiftQueryHandlerTest.java @@ -17,6 +17,7 @@ import jakarta.ws.rs.core.Response; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.mockito.ArgumentCaptor; +import java.time.Instant; import java.util.List; import java.util.Map; @@ -230,6 +231,10 @@ class RedshiftQueryHandlerTest { assertTrue(xml.contains("5439")); assertTrue(xml.contains("admin")); assertTrue(xml.contains("test-req-id")); + // A Snapshot built via this constructor carries no ARN/CreateTime; the elements + // must be omitted rather than serialized as empty or "null". + assertFalse(xml.contains("")); + assertFalse(xml.contains("")); } @Test @@ -239,6 +244,8 @@ class RedshiftQueryHandlerTest { params.putSingle("ClusterIdentifier", "test-cluster"); Snapshot snapshot = new Snapshot("test-snapshot", "test-cluster", "available", 5439, "admin"); + snapshot.setSnapshotArn("arn:aws:redshift:us-east-1:111111111111:snapshot:test-cluster/test-snapshot"); + snapshot.setSnapshotCreateTime(Instant.parse("2026-09-16T04:00:00Z")); when(service.describeSnapshots("test-snapshot", "test-cluster")).thenReturn(List.of(snapshot)); Response response = handler.handle("DescribeClusterSnapshots", params); @@ -246,6 +253,9 @@ class RedshiftQueryHandlerTest { String xml = (String) response.getEntity(); assertTrue(xml.contains("")); assertTrue(xml.contains("test-snapshot")); + assertTrue(xml.contains( + "arn:aws:redshift:us-east-1:111111111111:snapshot:test-cluster/test-snapshot")); + assertTrue(xml.contains("2026-09-16T04:00:00Z")); assertTrue(xml.contains("")); } @@ -288,6 +298,64 @@ class RedshiftQueryHandlerTest { assertTrue(xml.contains("5439")); } + @Test + void testRestoreFromClusterSnapshotBySnapshotArn() { + MultivaluedMap params = new MultivaluedHashMap<>(); + params.putSingle("ClusterIdentifier", "restored-cluster"); + params.putSingle("SnapshotArn", "arn:aws:redshift:us-east-1:acc:snapshot:src/my-snap"); + Cluster cluster = availableCluster("restored-cluster"); + when(service.restoreFromClusterSnapshot("restored-cluster", "my-snap", null)).thenReturn(cluster); + + Response response = handler.handle("RestoreFromClusterSnapshot", params, "auth"); + assertEquals(200, response.getStatus()); + verify(service).restoreFromClusterSnapshot("restored-cluster", "my-snap", null); + } + + @Test + void testRestoreFromClusterSnapshotMalformedArnIs400() { + MultivaluedMap params = new MultivaluedHashMap<>(); + params.putSingle("ClusterIdentifier", "restored-cluster"); + params.putSingle("SnapshotArn", "not-an-arn"); + + AwsException ex = assertThrows(AwsException.class, + () -> handler.handle("RestoreFromClusterSnapshot", params, "auth")); + assertEquals("InvalidParameterValue", ex.getErrorCode()); + assertEquals(400, ex.getHttpStatus()); + } + + @Test + void testRestoreFromClusterSnapshotWithoutSnapshotIs400() { + MultivaluedMap params = new MultivaluedHashMap<>(); + params.putSingle("ClusterIdentifier", "restored-cluster"); + + AwsException ex = assertThrows(AwsException.class, + () -> handler.handle("RestoreFromClusterSnapshot", params, "auth")); + assertEquals(400, ex.getHttpStatus()); + } + + @Test + void testRestoreFromClusterSnapshotForeignAccountArnNotFound() { + MultivaluedMap params = new MultivaluedHashMap<>(); + params.putSingle("ClusterIdentifier", "restored-cluster"); + params.putSingle("SnapshotArn", "arn:aws:redshift:us-east-1:999999999999:snapshot:src/my-snap"); + + AwsException ex = assertThrows(AwsException.class, + () -> handler.handle("RestoreFromClusterSnapshot", params, "auth")); + assertEquals("ClusterSnapshotNotFound", ex.getErrorCode()); + verify(service, never()).restoreFromClusterSnapshot(any(), any(), any()); + } + + @Test + void testClusterXmlCarriesDefaultParameterGroupAndMultiAZ() { + MultivaluedMap params = new MultivaluedHashMap<>(); + params.putSingle("ClusterIdentifier", "c1"); + when(service.describeClusters("c1")).thenReturn(List.of(availableCluster("c1"))); + + String xml = (String) handler.handle("DescribeClusters", params).getEntity(); + assertTrue(xml.contains("default.redshift-1.0")); + assertTrue(xml.contains("disabled")); + } + @Test void testCreateClusterParameterGroup() { MultivaluedMap params = new MultivaluedHashMap<>(); @@ -553,6 +621,79 @@ class RedshiftQueryHandlerTest { assertEquals("InvalidParameterValue", ex.getErrorCode()); } + @Test + void testDescribeLoggingStatus() { + MultivaluedMap params = new MultivaluedHashMap<>(); + params.putSingle("ClusterIdentifier", "test-cluster"); + + Cluster cluster = new Cluster(); + cluster.setClusterIdentifier("test-cluster"); + cluster.setLoggingEnabled(true); + cluster.setLoggingBucketName("my-bucket"); + cluster.setLoggingS3KeyPrefix("logs/"); + when(service.describeLoggingStatus("test-cluster")).thenReturn(cluster); + + Response response = handler.handle("DescribeLoggingStatus", params); + assertEquals(200, response.getStatus()); + String xml = (String) response.getEntity(); + // The real AWS wire format wraps the fields in a *Result element matching the + // operation name — verified against the SDK's own deserializer, not guessed. + assertTrue(xml.contains("")); + assertTrue(xml.contains("true")); + assertTrue(xml.contains("my-bucket")); + assertTrue(xml.contains("logs/")); + } + + @Test + void testDescribeLoggingStatusRequiresClusterIdentifier() { + MultivaluedMap params = new MultivaluedHashMap<>(); + + AwsException ex = assertThrows( + AwsException.class, + () -> handler.handle("DescribeLoggingStatus", params)); + assertEquals("InvalidParameterValue", ex.getErrorCode()); + } + + @Test + void testEnableLogging() { + MultivaluedMap params = new MultivaluedHashMap<>(); + params.putSingle("ClusterIdentifier", "test-cluster"); + params.putSingle("BucketName", "my-bucket"); + params.putSingle("S3KeyPrefix", "logs/"); + + Cluster cluster = new Cluster(); + cluster.setClusterIdentifier("test-cluster"); + cluster.setLoggingEnabled(true); + cluster.setLoggingBucketName("my-bucket"); + cluster.setLoggingS3KeyPrefix("logs/"); + when(service.enableLogging("test-cluster", "my-bucket", "logs/", null, List.of())).thenReturn(cluster); + + Response response = handler.handle("EnableLogging", params); + assertEquals(200, response.getStatus()); + String xml = (String) response.getEntity(); + assertTrue(xml.contains("")); + assertTrue(xml.contains("true")); + } + + @Test + void testDisableLogging() { + MultivaluedMap params = new MultivaluedHashMap<>(); + params.putSingle("ClusterIdentifier", "test-cluster"); + + Cluster cluster = new Cluster(); + cluster.setClusterIdentifier("test-cluster"); + cluster.setLoggingEnabled(false); + when(service.disableLogging("test-cluster")).thenReturn(cluster); + + Response response = handler.handle("DisableLogging", params); + assertEquals(200, response.getStatus()); + String xml = (String) response.getEntity(); + assertTrue(xml.contains("")); + assertTrue(xml.contains("false")); + // No bucket was ever configured; the element must be omitted, not emitted empty/"null". + assertFalse(xml.contains("")); + } + @Test void describesSnapshotCopyGrants() { MultivaluedMap params = new MultivaluedHashMap<>(); diff --git a/src/test/java/io/github/hectorvent/floci/services/redshift/RedshiftServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/redshift/RedshiftServiceTest.java index 0da8c123a0..4709993e8e 100644 --- a/src/test/java/io/github/hectorvent/floci/services/redshift/RedshiftServiceTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/redshift/RedshiftServiceTest.java @@ -725,6 +725,9 @@ class RedshiftServiceTest { // Password captured at snapshot time so restore can recover it after the source cluster is gone assertEquals("secret-pw", snapshot.getMasterPassword()); assertEquals(5439, snapshot.getPort()); + assertEquals("arn:aws:redshift:us-east-1:111111111111:snapshot:my-cluster/my-snapshot", + snapshot.getSnapshotArn()); + assertNotNull(snapshot.getSnapshotCreateTime()); // sqlDump is now an absolute path scoped by account to avoid collisions across accounts assertTrue(snapshot.getSqlDump().contains("111111111111")); assertTrue(snapshot.getSqlDump().endsWith("my-snapshot.sql")); @@ -833,6 +836,40 @@ class RedshiftServiceTest { verify(clusterBackend).flush(); } + @Test + void testDescribeLoggingStatusDefaultsToDisabled() { + Cluster cluster = new Cluster(); + cluster.setClusterIdentifier("my-cluster"); + when(clusterBackend.get("my-cluster")).thenReturn(Optional.of(cluster)); + + Cluster result = service.describeLoggingStatus("my-cluster"); + + assertFalse(result.isLoggingEnabled()); + assertNull(result.getLoggingBucketName()); + } + + @Test + void testDescribeLoggingStatusNotFound() { + when(clusterBackend.get("missing")).thenReturn(Optional.empty()); + + assertThrows(AwsException.class, () -> service.describeLoggingStatus("missing")); + } + + @Test + void testEnableLogging() { + Cluster cluster = new Cluster(); + cluster.setClusterIdentifier("my-cluster"); + when(clusterBackend.get("my-cluster")).thenReturn(Optional.of(cluster)); + + Cluster result = service.enableLogging("my-cluster", "my-bucket", "logs/", null, null); + + assertTrue(result.isLoggingEnabled()); + assertEquals("my-bucket", result.getLoggingBucketName()); + assertEquals("logs/", result.getLoggingS3KeyPrefix()); + verify(clusterBackend).put(eq("my-cluster"), any(Cluster.class)); + verify(clusterBackend).flush(); + } + @Test void modifyClusterIamRolesRejectsMalformedRoleArnWithoutChangingTheCluster() { Cluster cluster = new Cluster(); @@ -870,6 +907,86 @@ class RedshiftServiceTest { assertEquals("ClusterNotFound", ex.getErrorCode()); } + @Test + void testEnableLoggingRequiresBucketName() { + Cluster cluster = new Cluster(); + cluster.setClusterIdentifier("my-cluster"); + when(clusterBackend.get("my-cluster")).thenReturn(Optional.of(cluster)); + + assertThrows(AwsException.class, () -> service.enableLogging("my-cluster", null, null, null, null)); + } + + @Test + void testEnableLoggingCloudWatchDoesNotRequireBucketName() { + Cluster cluster = new Cluster(); + cluster.setClusterIdentifier("my-cluster"); + when(clusterBackend.get("my-cluster")).thenReturn(Optional.of(cluster)); + + Cluster result = service.enableLogging("my-cluster", null, null, "cloudwatch", List.of("connectionlog")); + assertTrue(result.isLoggingEnabled()); + assertEquals("cloudwatch", result.getLoggingDestinationType()); + assertEquals(List.of("connectionlog"), result.getLoggingExports()); + } + + @Test + void testCreateClusterAssignsDefaultParameterGroup() { + when(clusterBackend.get(anyString())).thenReturn(Optional.empty()); + when(cm.start(any(), any(), any(), any())).thenReturn(new RedshiftContainerHandle("c1", "my-cluster", "localhost", 5432)); + + Cluster cluster = service.createCluster("my-cluster", "dc2.large", "admin", "password123"); + assertEquals("default.redshift-1.0", cluster.getClusterParameterGroupName()); + } + + @Test + void testModifyClusterStoresMultiAZ() { + Cluster cluster = new Cluster(); + cluster.setClusterIdentifier("my-cluster"); + when(clusterBackend.get("my-cluster")).thenReturn(Optional.of(cluster)); + + assertTrue(service.modifyCluster("my-cluster", null, null, null, null, null, true).isMultiAZ()); + } + + @Test + void testDescribeDefaultParameterGroupIsSynthesized() { + when(parameterGroupBackend.get("default.redshift-1.0")).thenReturn(Optional.empty()); + + List groups = service.describeClusterParameterGroups("default.redshift-1.0"); + assertEquals(1, groups.size()); + assertEquals("default.redshift-1.0", groups.get(0).getParameterGroupName()); + } + + @Test + void testEnableLoggingNotFound() { + when(clusterBackend.get("missing")).thenReturn(Optional.empty()); + + assertThrows(AwsException.class, () -> service.enableLogging("missing", "my-bucket", null, null, null)); + } + + @Test + void testDisableLoggingClearsConfig() { + Cluster cluster = new Cluster(); + cluster.setClusterIdentifier("my-cluster"); + cluster.setLoggingEnabled(true); + cluster.setLoggingBucketName("my-bucket"); + cluster.setLoggingS3KeyPrefix("logs/"); + when(clusterBackend.get("my-cluster")).thenReturn(Optional.of(cluster)); + + Cluster result = service.disableLogging("my-cluster"); + + assertFalse(result.isLoggingEnabled()); + assertNull(result.getLoggingBucketName()); + assertNull(result.getLoggingS3KeyPrefix()); + verify(clusterBackend).put(eq("my-cluster"), any(Cluster.class)); + verify(clusterBackend).flush(); + } + + @Test + void testDisableLoggingNotFound() { + when(clusterBackend.get("missing")).thenReturn(Optional.empty()); + + assertThrows(AwsException.class, () -> service.disableLogging("missing")); + } + @Test void testDescribeSnapshots() { Snapshot s = new Snapshot("snap-1", "my-cluster", "available", 5439, "admin");