fix(redshift): accept s3table log destination and reject mutual snapshot restore params (#4340)

* fix(redshift): accept s3table log destination and reject mutual snapshot restore params

Closes #4242

* fix(redshift): preserve S3-table logging configuration and report status

Pass S3TableKmsKeyId and S3TableGranularity through EnableLogging, store them on Cluster, and serialize S3Tables in LoggingStatus responses.

* fix(redshift): validate S3-table logging settings

* fix(redshift): complete S3 table logging status
This commit is contained in:
Vũ Thế Huy authored and GitHub committed 2026-09-25 15:20:20 -04:00
1 parent 41d07f6e3b
commit 6bdf14210e
7 files changed
+343 -7

No files matched your search

+1 -1
View File
@@ -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 |
@@ -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<String> 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();
}
@@ -732,10 +732,26 @@ public class RedshiftService {
public synchronized Cluster enableLogging(String clusterIdentifier, String bucketName, String s3KeyPrefix,
String logDestinationType, List<String> logExports) {
return enableLogging(clusterIdentifier, bucketName, s3KeyPrefix, logDestinationType, logExports, null, null);
}
public synchronized Cluster enableLogging(String clusterIdentifier, String bucketName, String s3KeyPrefix,
String logDestinationType, List<String> 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;
@@ -60,11 +60,17 @@ public class Cluster {
private String loggingDestinationType;
private List<String> loggingExports;
private String loggingS3TableKmsKeyId;
private String loggingS3TableGranularity;
public String getLoggingDestinationType() { return loggingDestinationType; }
public void setLoggingDestinationType(String loggingDestinationType) { this.loggingDestinationType = loggingDestinationType; }
public List<String> getLoggingExports() { return loggingExports; }
public void setLoggingExports(List<String> 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;
@@ -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("<LoggingEnabled>true</LoggingEnabled>"))
.body(containsString("<LogDestinationType>s3table</LogDestinationType>"))
.body(containsString("<S3Tables><S3Tables><member>sys_query_history</member></S3Tables>"))
.body(containsString("<S3TableGranularity>cluster</S3TableGranularity>"))
.body(containsString("<EnabledAll>false</EnabledAll>"));
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("<LoggingEnabled>true</LoggingEnabled>"))
.body(containsString("<LogDestinationType>s3table</LogDestinationType>"))
.body(containsString("<S3Tables><S3Tables><member>sys_query_history</member></S3Tables>"))
.body(containsString("<S3TableGranularity>cluster</S3TableGranularity>"))
.body(containsString("<EnabledAll>false</EnabledAll>"))
.body(not(containsString("<S3TableKmsKeyId>")));
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("<S3Tables><EnabledAll>true</EnabledAll></S3Tables>"))
.body(not(containsString("<S3Tables></S3Tables>")))
.body(not(containsString("<S3TableGranularity>")));
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("<LoggingEnabled>false</LoggingEnabled>"))
.body(not(containsString("<S3Tables>")));
// 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("<Code>InvalidParameterCombination</Code>"));
// 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("<Code>InvalidParameterValue</Code>"));
// 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("<LogDestinationType>cloudwatch</LogDestinationType>"))
.body(not(containsString("<S3Tables>")));
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("<LoggingEnabled>false</LoggingEnabled>"));
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("<LoggingEnabled>false</LoggingEnabled>"))
.body(not(containsString("<S3Tables>")));
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("<LoggingEnabled>false</LoggingEnabled>"))
.body(not(containsString("<S3Tables>")));
// 1f. Static discovery APIs
given()
.contentType("application/x-www-form-urlencoded")
.header("Authorization", AUTH_HEADER)
@@ -274,6 +425,21 @@ public class RedshiftOperationsTest {
.body(containsString("<ClusterIdentifier>cluster-src</ClusterIdentifier>"))
.body(containsString("<ClusterStatus>deleting</ClusterStatus>"));
// 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("<Code>InvalidParameterCombination</Code>"));
// 5. RestoreFromClusterSnapshot
given()
.contentType("application/x-www-form-urlencoded")
@@ -345,6 +345,20 @@ class RedshiftQueryHandlerTest {
verify(service, never()).restoreFromClusterSnapshot(any(), any(), any());
}
@Test
void restoreFromClusterSnapshotWithBothIdentifierAndArnIs400() {
MultivaluedMap<String, String> 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<String, String> params = new MultivaluedHashMap<>();
@@ -644,6 +658,24 @@ class RedshiftQueryHandlerTest {
assertTrue(xml.contains("<S3KeyPrefix>logs/</S3KeyPrefix>"));
}
@Test
void describeS3TableLoggingStatusIncludesEnabledAllWithoutGranularity() {
MultivaluedMap<String, String> 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("<S3Tables><EnabledAll>true</EnabledAll></S3Tables>"));
assertFalse(xml.contains("<S3Tables></S3Tables>"));
}
@Test
void describeLoggingStatusRequiresClusterIdentifier() {
MultivaluedMap<String, String> 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("<LoggingEnabled>true</LoggingEnabled>"));
}
@Test
void enableLoggingS3TableWithGranularityAndKmsKey() {
MultivaluedMap<String, String> 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("<EnableLoggingResult>"));
assertTrue(xml.contains("<LoggingEnabled>true</LoggingEnabled>"));
assertTrue(xml.contains("<LogDestinationType>s3table</LogDestinationType>"));
assertTrue(xml.contains("<S3Tables><S3Tables><member>sys_query_history</member></S3Tables>"));
assertTrue(xml.contains("<EnabledAll>false</EnabledAll>"));
assertTrue(xml.contains("<S3TableGranularity>cluster</S3TableGranularity>"));
}
@Test
void disableLogging() {
MultivaluedMap<String, String> params = new MultivaluedHashMap<>();
@@ -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());