diff --git a/docs/services/redshift.md b/docs/services/redshift.md index 8996fabcbf..a9f8837cb9 100644 --- a/docs/services/redshift.md +++ b/docs/services/redshift.md @@ -47,7 +47,7 @@ For running SQL without a PostgreSQL wire connection (the way Lambda and Step Fu | `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 | +| `EnableLogging` | Store audit-logging configuration (S3 bucket, CloudWatch, or S3 table 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 | 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 bddc944830..3c9969186e 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 @@ -667,7 +667,9 @@ public class RedshiftQueryHandler { String clusterIdentifier = requireParam(params, "ClusterIdentifier"); Cluster cluster = service.enableLogging(clusterIdentifier, params.getFirst("BucketName"), params.getFirst("S3KeyPrefix"), params.getFirst("LogDestinationType"), - memberList(params, "LogExports")); + memberList(params, "LogExports"), + params.getFirst("S3TableKmsKeyId"), + params.getFirst("S3TableGranularity")); return loggingStatusResponse(action, cluster); } case "DisableLogging" -> { @@ -871,10 +873,16 @@ public class RedshiftQueryHandler { // RestoreFromClusterSnapshot accepts SnapshotIdentifier or SnapshotArn. private String resolveSnapshotIdentifier(String snapshotIdentifier, String snapshotArn, String authorizationHeader) { - if (snapshotIdentifier != null && !snapshotIdentifier.isBlank()) { + boolean hasIdentifier = snapshotIdentifier != null && !snapshotIdentifier.isBlank(); + boolean hasArn = snapshotArn != null && !snapshotArn.isBlank(); + if (hasIdentifier && hasArn) { + throw new AwsException("InvalidParameterCombination", + "You must specify either SnapshotIdentifier or SnapshotArn, but not both.", 400); + } + if (hasIdentifier) { return snapshotIdentifier; } - if (snapshotArn == null || snapshotArn.isBlank()) { + if (!hasArn) { throw new AwsException("InvalidParameterValue", "SnapshotIdentifier or SnapshotArn is required", 400); } AwsArnUtils.Arn arn; @@ -944,6 +952,22 @@ public class RedshiftQueryHandler { } builder.end("LogExports"); } + if ("s3table".equalsIgnoreCase(cluster.getLoggingDestinationType())) { + builder.start("S3Tables"); + List exports = cluster.getLoggingExports(); + if (exports != null && !exports.isEmpty()) { + builder.start("S3Tables"); + for (String export : exports) { + builder.elem("member", export); + } + builder.end("S3Tables"); + } + if (cluster.getLoggingS3TableGranularity() != null) { + builder.elem("S3TableGranularity", cluster.getLoggingS3TableGranularity()); + } + builder.elem("EnabledAll", exports == null || exports.isEmpty() || exports.contains("all")); + builder.end("S3Tables"); + } return builder.build(); } 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 5bfb744af4..800669a66d 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 @@ -732,10 +732,26 @@ public class RedshiftService { public synchronized Cluster enableLogging(String clusterIdentifier, String bucketName, String s3KeyPrefix, String logDestinationType, List logExports) { + return enableLogging(clusterIdentifier, bucketName, s3KeyPrefix, logDestinationType, logExports, null, null); + } + + public synchronized Cluster enableLogging(String clusterIdentifier, String bucketName, String s3KeyPrefix, + String logDestinationType, List logExports, + String s3TableKmsKeyId, String s3TableGranularity) { 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())) { + boolean s3Table = "s3table".equalsIgnoreCase(logDestinationType); + if (s3Table && s3TableGranularity != null + && !List.of("cluster", "account").contains(s3TableGranularity)) { + throw new AwsException("InvalidParameterValue", + "S3TableGranularity must be cluster or account", 400); + } + if (!s3Table && (s3TableKmsKeyId != null || s3TableGranularity != null)) { + throw new AwsException("InvalidParameterCombination", + "S3-table logging settings are valid only when LogDestinationType is s3table", 400); + } + if (!cloudWatch && !s3Table && (bucketName == null || bucketName.isBlank())) { throw new AwsException("InvalidParameterValue", "BucketName is required for an S3 log destination", 400); } cluster.setLoggingEnabled(true); @@ -743,6 +759,8 @@ public class RedshiftService { cluster.setLoggingDestinationType(logDestinationType); cluster.setLoggingExports(logExports == null || logExports.isEmpty() ? null : List.copyOf(logExports)); cluster.setLoggingS3KeyPrefix(s3KeyPrefix); + cluster.setLoggingS3TableKmsKeyId(s3TableKmsKeyId); + cluster.setLoggingS3TableGranularity(s3TableGranularity); clusters.put(clusterIdentifier, cluster); clusters.flush(); return cluster; @@ -756,6 +774,8 @@ public class RedshiftService { cluster.setLoggingS3KeyPrefix(null); cluster.setLoggingDestinationType(null); cluster.setLoggingExports(null); + cluster.setLoggingS3TableKmsKeyId(null); + cluster.setLoggingS3TableGranularity(null); clusters.put(clusterIdentifier, cluster); clusters.flush(); return cluster; 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 59d1a8875b..7182ee64c8 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 @@ -60,11 +60,17 @@ public class Cluster { private String loggingDestinationType; private List loggingExports; + private String loggingS3TableKmsKeyId; + private String loggingS3TableGranularity; 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; } + public String getLoggingS3TableKmsKeyId() { return loggingS3TableKmsKeyId; } + public void setLoggingS3TableKmsKeyId(String loggingS3TableKmsKeyId) { this.loggingS3TableKmsKeyId = loggingS3TableKmsKeyId; } + public String getLoggingS3TableGranularity() { return loggingS3TableGranularity; } + public void setLoggingS3TableGranularity(String loggingS3TableGranularity) { this.loggingS3TableGranularity = loggingS3TableGranularity; } private boolean multiAZ = false; diff --git a/src/test/java/io/github/hectorvent/floci/services/redshift/RedshiftOperationsTest.java b/src/test/java/io/github/hectorvent/floci/services/redshift/RedshiftOperationsTest.java index 8ee9c49c4f..59b201d253 100644 --- a/src/test/java/io/github/hectorvent/floci/services/redshift/RedshiftOperationsTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/redshift/RedshiftOperationsTest.java @@ -5,6 +5,7 @@ import io.github.hectorvent.floci.services.redshift.container.RedshiftContainerM import io.quarkus.test.InjectMock; import io.quarkus.test.junit.QuarkusTest; import io.restassured.path.xml.XmlPath; +import jakarta.inject.Inject; import org.junit.jupiter.api.MethodOrderer; import org.junit.jupiter.api.Order; import org.junit.jupiter.api.Test; @@ -47,6 +48,9 @@ public class RedshiftOperationsTest { @InjectMock RedshiftContainerManager containerManager; + @Inject + RedshiftService service; + @Test @Order(1) void parameterGroupLifecycle() { @@ -210,7 +214,154 @@ public class RedshiftOperationsTest { .statusCode(200) .body(not(containsString("redshift-copy"))); - // 1e. Static discovery APIs + // 1e. Logging: EnableLogging with s3table destination preserves its publishing status + given() + .contentType("application/x-www-form-urlencoded") + .header("Authorization", AUTH_HEADER) + .formParam("Action", "EnableLogging") + .formParam("ClusterIdentifier", "cluster-src") + .formParam("LogDestinationType", "s3table") + .formParam("S3TableGranularity", "cluster") + .formParam("S3TableKmsKeyId", "test-kms-key") + .formParam("LogExports.member.1", "sys_query_history") + .when() + .post("/") + .then() + .statusCode(200) + .contentType("application/xml") + .body(containsString("true")) + .body(containsString("s3table")) + .body(containsString("sys_query_history")) + .body(containsString("cluster")) + .body(containsString("false")); + + given() + .contentType("application/x-www-form-urlencoded") + .header("Authorization", AUTH_HEADER) + .formParam("Action", "DescribeLoggingStatus") + .formParam("ClusterIdentifier", "cluster-src") + .when() + .post("/") + .then() + .statusCode(200) + .contentType("application/xml") + .body(containsString("true")) + .body(containsString("s3table")) + .body(containsString("sys_query_history")) + .body(containsString("cluster")) + .body(containsString("false")) + .body(not(containsString(""))); + + assertEquals("test-kms-key", service.describeLoggingStatus("cluster-src").getLoggingS3TableKmsKeyId()); + + given() + .contentType("application/x-www-form-urlencoded") + .header("Authorization", AUTH_HEADER) + .formParam("Action", "EnableLogging") + .formParam("ClusterIdentifier", "cluster-src") + .formParam("LogDestinationType", "s3table") + .when() + .post("/") + .then() + .statusCode(200) + .body(containsString("true")) + .body(not(containsString(""))) + .body(not(containsString(""))); + + given() + .contentType("application/x-www-form-urlencoded") + .header("Authorization", AUTH_HEADER) + .formParam("Action", "DisableLogging") + .formParam("ClusterIdentifier", "cluster-src") + .when() + .post("/") + .then() + .statusCode(200) + .contentType("application/xml") + .body(containsString("false")) + .body(not(containsString(""))); + + // 1e.1 S3-table settings are invalid for a non-table destination + given() + .contentType("application/x-www-form-urlencoded") + .header("Authorization", AUTH_HEADER) + .formParam("Action", "EnableLogging") + .formParam("ClusterIdentifier", "cluster-src") + .formParam("LogDestinationType", "cloudwatch") + .formParam("S3TableGranularity", "cluster") + .when() + .post("/") + .then() + .statusCode(400) + .contentType("application/xml") + .body(containsString("InvalidParameterCombination")); + + // 1e.1.1 Only AWS-supported S3-table granularity values are accepted + given() + .contentType("application/x-www-form-urlencoded") + .header("Authorization", AUTH_HEADER) + .formParam("Action", "EnableLogging") + .formParam("ClusterIdentifier", "cluster-src") + .formParam("LogDestinationType", "s3table") + .formParam("S3TableGranularity", "daily") + .when() + .post("/") + .then() + .statusCode(400) + .contentType("application/xml") + .body(containsString("InvalidParameterValue")); + + // 1e.2 Non-table logging status must omit the S3-table status block + given() + .contentType("application/x-www-form-urlencoded") + .header("Authorization", AUTH_HEADER) + .formParam("Action", "EnableLogging") + .formParam("ClusterIdentifier", "cluster-src") + .formParam("LogDestinationType", "cloudwatch") + .when() + .post("/") + .then() + .statusCode(200) + .body(containsString("cloudwatch")) + .body(not(containsString(""))); + + given() + .contentType("application/x-www-form-urlencoded") + .header("Authorization", AUTH_HEADER) + .formParam("Action", "DisableLogging") + .formParam("ClusterIdentifier", "cluster-src") + .when() + .post("/") + .then() + .statusCode(200) + .body(containsString("false")); + + given() + .contentType("application/x-www-form-urlencoded") + .header("Authorization", AUTH_HEADER) + .formParam("Action", "DescribeLoggingStatus") + .formParam("ClusterIdentifier", "cluster-src") + .when() + .post("/") + .then() + .statusCode(200) + .body(containsString("false")) + .body(not(containsString(""))); + + given() + .contentType("application/x-www-form-urlencoded") + .header("Authorization", AUTH_HEADER) + .formParam("Action", "DescribeLoggingStatus") + .formParam("ClusterIdentifier", "cluster-src") + .when() + .post("/") + .then() + .statusCode(200) + .contentType("application/xml") + .body(containsString("false")) + .body(not(containsString(""))); + + // 1f. Static discovery APIs given() .contentType("application/x-www-form-urlencoded") .header("Authorization", AUTH_HEADER) @@ -274,6 +425,21 @@ public class RedshiftOperationsTest { .body(containsString("cluster-src")) .body(containsString("deleting")); + // 4b. RestoreFromClusterSnapshot rejects carrying both SnapshotIdentifier and SnapshotArn + given() + .contentType("application/x-www-form-urlencoded") + .header("Authorization", AUTH_HEADER) + .formParam("Action", "RestoreFromClusterSnapshot") + .formParam("ClusterIdentifier", "cluster-restored-invalid") + .formParam("SnapshotIdentifier", "snap-test-1") + .formParam("SnapshotArn", "arn:aws:redshift:us-east-1:000000000000:snapshot:cluster-src/snap-test-1") + .when() + .post("/") + .then() + .statusCode(400) + .contentType("application/xml") + .body(containsString("InvalidParameterCombination")); + // 5. RestoreFromClusterSnapshot given() .contentType("application/x-www-form-urlencoded") 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 377247c6f9..73365065f9 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 @@ -345,6 +345,20 @@ class RedshiftQueryHandlerTest { verify(service, never()).restoreFromClusterSnapshot(any(), any(), any()); } + @Test + void restoreFromClusterSnapshotWithBothIdentifierAndArnIs400() { + MultivaluedMap params = new MultivaluedHashMap<>(); + params.putSingle("ClusterIdentifier", "restored-cluster"); + params.putSingle("SnapshotIdentifier", "snap-a"); + params.putSingle("SnapshotArn", "arn:aws:redshift:us-east-1:acc:snapshot:src/snap-b"); + + AwsException ex = assertThrows(AwsException.class, + () -> handler.handle("RestoreFromClusterSnapshot", params, "auth")); + assertEquals("InvalidParameterCombination", ex.getErrorCode()); + assertEquals(400, ex.getHttpStatus()); + verify(service, never()).restoreFromClusterSnapshot(any(), any(), any()); + } + @Test void clusterXmlCarriesDefaultParameterGroupAndMultiAZ() { MultivaluedMap params = new MultivaluedHashMap<>(); @@ -644,6 +658,24 @@ class RedshiftQueryHandlerTest { assertTrue(xml.contains("logs/")); } + @Test + void describeS3TableLoggingStatusIncludesEnabledAllWithoutGranularity() { + MultivaluedMap params = new MultivaluedHashMap<>(); + params.putSingle("ClusterIdentifier", "test-cluster"); + + Cluster cluster = new Cluster(); + cluster.setClusterIdentifier("test-cluster"); + cluster.setLoggingEnabled(true); + cluster.setLoggingDestinationType("s3table"); + when(service.describeLoggingStatus("test-cluster")).thenReturn(cluster); + + Response response = handler.handle("DescribeLoggingStatus", params); + + String xml = (String) response.getEntity(); + assertTrue(xml.contains("true")); + assertFalse(xml.contains("")); + } + @Test void describeLoggingStatusRequiresClusterIdentifier() { MultivaluedMap params = new MultivaluedHashMap<>(); @@ -666,7 +698,7 @@ class RedshiftQueryHandlerTest { cluster.setLoggingEnabled(true); cluster.setLoggingBucketName("my-bucket"); cluster.setLoggingS3KeyPrefix("logs/"); - when(service.enableLogging("test-cluster", "my-bucket", "logs/", null, List.of())).thenReturn(cluster); + when(service.enableLogging("test-cluster", "my-bucket", "logs/", null, List.of(), null, null)).thenReturn(cluster); Response response = handler.handle("EnableLogging", params); assertEquals(200, response.getStatus()); @@ -675,6 +707,37 @@ class RedshiftQueryHandlerTest { assertTrue(xml.contains("true")); } + @Test + void enableLoggingS3TableWithGranularityAndKmsKey() { + MultivaluedMap params = new MultivaluedHashMap<>(); + params.putSingle("ClusterIdentifier", "test-cluster"); + params.putSingle("LogDestinationType", "s3table"); + params.putSingle("S3TableKmsKeyId", "my-kms-key"); + params.putSingle("S3TableGranularity", "cluster"); + params.putSingle("LogExports.member.1", "sys_query_history"); + + Cluster cluster = new Cluster(); + cluster.setClusterIdentifier("test-cluster"); + cluster.setLoggingEnabled(true); + cluster.setLoggingDestinationType("s3table"); + cluster.setLoggingS3TableKmsKeyId("my-kms-key"); + cluster.setLoggingS3TableGranularity("cluster"); + cluster.setLoggingExports(List.of("sys_query_history")); + when(service.enableLogging("test-cluster", null, null, "s3table", List.of("sys_query_history"), + "my-kms-key", "cluster")) + .thenReturn(cluster); + + Response response = handler.handle("EnableLogging", params); + assertEquals(200, response.getStatus()); + String xml = (String) response.getEntity(); + assertTrue(xml.contains("")); + assertTrue(xml.contains("true")); + assertTrue(xml.contains("s3table")); + assertTrue(xml.contains("sys_query_history")); + assertTrue(xml.contains("false")); + assertTrue(xml.contains("cluster")); + } + @Test void disableLogging() { 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 d1c5e27eee..a863204a90 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 @@ -929,6 +929,63 @@ class RedshiftServiceTest { assertEquals(List.of("connectionlog"), result.getLoggingExports()); } + @Test + void enableLoggingS3TableDoesNotRequireBucketName() { + 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, "s3table", null, "my-kms-key", "account"); + assertTrue(result.isLoggingEnabled()); + assertEquals("s3table", result.getLoggingDestinationType()); + assertNull(result.getLoggingBucketName()); + assertEquals("my-kms-key", result.getLoggingS3TableKmsKeyId()); + assertEquals("account", result.getLoggingS3TableGranularity()); + } + + @Test + void enableLoggingRejectsInvalidS3TableGranularity() { + Cluster cluster = new Cluster(); + cluster.setClusterIdentifier("my-cluster"); + when(clusterBackend.get("my-cluster")).thenReturn(Optional.of(cluster)); + + AwsException ex = assertThrows(AwsException.class, + () -> service.enableLogging("my-cluster", null, null, "s3table", null, null, "daily")); + + assertEquals("InvalidParameterValue", ex.getErrorCode()); + assertFalse(cluster.isLoggingEnabled()); + } + + @Test + void enableLoggingRejectsS3TableSettingsForOtherDestinations() { + Cluster cluster = new Cluster(); + cluster.setClusterIdentifier("my-cluster"); + when(clusterBackend.get("my-cluster")).thenReturn(Optional.of(cluster)); + + AwsException ex = assertThrows(AwsException.class, + () -> service.enableLogging("my-cluster", null, null, "s3", null, null, "cluster")); + + assertEquals("InvalidParameterCombination", ex.getErrorCode()); + assertFalse(cluster.isLoggingEnabled()); + } + + @Test + void disableLoggingClearsS3TableFields() { + Cluster cluster = new Cluster(); + cluster.setClusterIdentifier("my-cluster"); + cluster.setLoggingEnabled(true); + cluster.setLoggingDestinationType("s3table"); + cluster.setLoggingS3TableKmsKeyId("my-kms-key"); + cluster.setLoggingS3TableGranularity("account"); + when(clusterBackend.get("my-cluster")).thenReturn(Optional.of(cluster)); + + Cluster result = service.disableLogging("my-cluster"); + assertFalse(result.isLoggingEnabled()); + assertNull(result.getLoggingDestinationType()); + assertNull(result.getLoggingS3TableKmsKeyId()); + assertNull(result.getLoggingS3TableGranularity()); + } + @Test void createClusterAssignsDefaultParameterGroup() { when(clusterBackend.get(anyString())).thenReturn(Optional.empty());