feat(redshift-serverless): read and list snapshots another account shares (#7899)

GetSnapshot and ListSnapshots with an ownerAccount other than the caller's now read the
owner's snapshots and return only the ones the snapshot resource policy shares with the
caller. A snapshot is never shared across partitions.
This commit is contained in:
Vũ Thế Huy authored and GitHub committed 2026-11-12 09:03:53 -04:00
1 parent 77d73ee4b7
commit 8a817ec5e9
11 files changed
+408 -36

No files matched your search

@@ -100,7 +100,7 @@ class RedshiftServerlessAdministrationHandlerTest {
new RedshiftServerlessSnapshotCopyService(storageFactory, regionResolver, serverlessService),
new RedshiftServerlessScheduledActionService(storageFactory, serverlessService,
mock(RedshiftServerlessScheduledActionAuthorizer.class), regionResolver)),
customDomains);
customDomains, mock(RedshiftServerlessSharedSnapshots.class));
}
@Test
@@ -658,6 +658,44 @@ class RedshiftServerlessIntegrationTest {
.body("__type", equalTo("ValidationException"));
}
@Test
void aSnapshotOfAnotherAccountIsReadAndOnlyTheSharedOnesAreListed() {
String owner = "111122223333";
String caller = "444455556666";
String allowCaller = "{\\\"Version\\\":\\\"2012-10-17\\\",\\\"Statement\\\":[{\\\"Effect\\\":\\\"Allow\\\","
+ "\\\"Principal\\\":{\\\"AWS\\\":\\\"arn:aws:iam::" + caller + ":root\\\"},"
+ "\\\"Action\\\":\\\"redshift-serverless:GetSnapshot\\\",\\\"Resource\\\":\\\"*\\\"}]}";
callAs(owner, "CreateNamespace", "{\"namespaceName\":\"read-src\",\"adminUsername\":\"admin\"}").statusCode(200);
String sharedArn = callAs(owner, "CreateSnapshot",
"{\"snapshotName\":\"read-shared\",\"namespaceName\":\"read-src\"}")
.statusCode(200).extract().path("snapshot.snapshotArn");
callAs(owner, "CreateSnapshot", "{\"snapshotName\":\"read-private\",\"namespaceName\":\"read-src\"}")
.statusCode(200);
String get = "{\"snapshotName\":\"read-shared\",\"ownerAccount\":\"" + owner + "\"}";
String list = "{\"ownerAccount\":\"" + owner + "\"}";
// GetSnapshot reads the owner's snapshot, and IAM enforcement checks its policy. The list leaves out what
// is not shared rather than refusing.
callAs(caller, "ListSnapshots", list).statusCode(200).body("snapshots", hasSize(0));
callAs(owner, "PutResourcePolicy", "{\"resourceArn\":\"" + sharedArn + "\",\"policy\":\"" + allowCaller + "\"}")
.statusCode(200);
callAs(caller, "GetSnapshot", get).statusCode(200)
.body("snapshot.snapshotName", equalTo("read-shared"))
.body("snapshot.ownerAccount", equalTo(owner));
callAs(caller, "GetSnapshot", "{\"snapshotArn\":\"" + sharedArn + "\",\"ownerAccount\":\"" + owner + "\"}")
.statusCode(200).body("snapshot.snapshotName", equalTo("read-shared"));
callAs(caller, "ListSnapshots", list).statusCode(200)
.body("snapshots", hasSize(1))
.body("snapshots[0].snapshotName", equalTo("read-shared"));
callAs(caller, "GetSnapshot", get.replace("read-shared", "no-such-snap")).statusCode(400)
.body("__type", equalTo("ResourceNotFoundException"));
callAs(owner, "DeleteResourcePolicy", "{\"resourceArn\":\"" + sharedArn + "\"}").statusCode(200);
callAs(caller, "ListSnapshots", list).statusCode(200).body("snapshots", hasSize(0));
}
@Test
void resourcePolicyIsIsolatedPerAccount() {
String accountA = "111122223333";
@@ -134,7 +134,7 @@ class RedshiftServerlessJsonHandlerTest {
new RedshiftServerlessSnapshotCopyService(storageFactory, regionResolver, service),
new RedshiftServerlessScheduledActionService(storageFactory, service,
mock(RedshiftServerlessScheduledActionAuthorizer.class), regionResolver)),
customDomains);
customDomains, mock(RedshiftServerlessSharedSnapshots.class));
}
@SuppressWarnings({"unchecked", "rawtypes"})
@@ -0,0 +1,114 @@
package io.floci.aws.services.redshiftserverless;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.floci.aws.core.common.RegionResolver;
import io.floci.aws.core.common.RequestContext;
import io.floci.aws.services.iam.IamPolicyEvaluator;
import io.floci.aws.services.iam.IamService;
import io.floci.aws.services.iam.RequestCallerResolver;
import io.floci.aws.services.redshiftserverless.model.RedshiftServerlessSnapshot;
import jakarta.enterprise.inject.Instance;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.time.Instant;
import java.util.List;
import java.util.Optional;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
class RedshiftServerlessSharedSnapshotsTest {
private static final String OWNER = "111111111111";
private static final String CALLER = "222222222222";
private static final String REGION = "us-east-1";
private static final String SNAPSHOT_ARN = "arn:aws:redshift-serverless:us-east-1:" + OWNER + ":snapshot/shared";
private RedshiftServerlessResourcePolicyService policies;
private RedshiftServerlessSharedSnapshots shared;
private RedshiftServerlessSnapshot snapshot;
@BeforeEach
@SuppressWarnings("unchecked")
void setUp() {
policies = mock(RedshiftServerlessResourcePolicyService.class);
IamService iamService = mock(IamService.class);
when(iamService.resolveCallerArns(any())).thenReturn(Optional.empty());
Instance<RequestContext> requestContext = mock(Instance.class);
ObjectMapper objectMapper = new ObjectMapper();
RegionResolver regionResolver = new RegionResolver(REGION, CALLER);
RedshiftServerlessResourcePolicyProvider resourcePolicyProvider = new RedshiftServerlessResourcePolicyProvider(
mock(RedshiftServerlessService.class), policies, objectMapper);
shared = new RedshiftServerlessSharedSnapshots(policies,
new IamPolicyEvaluator(objectMapper), regionResolver,
new RequestCallerResolver(iamService, regionResolver, requestContext), resourcePolicyProvider);
snapshot = new RedshiftServerlessSnapshot();
snapshot.setSnapshotName("shared");
snapshot.setSnapshotArn(SNAPSHOT_ARN);
snapshot.setSnapshotCreateTime(Instant.parse("2030-01-01T00:00:00Z"));
}
private void policy(String document) {
when(policies.policyOfSnapshot(eq(OWNER), eq(snapshot), eq(REGION))).thenReturn(Optional.of(document));
}
private static String statement(String effect, String principal) {
return "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"" + effect + "\",\"Principal\":{\"AWS\":\""
+ principal + "\"},\"Action\":\"redshift-serverless:GetSnapshot\",\"Resource\":\"*\"}]}";
}
@Test
void aResourceLessAllowSharesTheSnapshot() {
policy(statement("Allow", CALLER).replace(",\"Resource\":\"*\"", ""));
assertEquals(List.of(snapshot), shared.readable(OWNER, List.of(snapshot), REGION));
}
@Test
void aResourceLessDenyOverridesAnExplicitAllow() {
policy("""
{"Statement":[
{"Effect":"Allow","Principal":{"AWS":"222222222222"},
"Action":"redshift-serverless:GetSnapshot","Resource":"*"},
{"Effect":"Deny","Principal":{"AWS":"222222222222"},
"Action":"redshift-serverless:GetSnapshot"}
]}
""");
assertEquals(List.of(), shared.readable(OWNER, List.of(snapshot), REGION));
}
@Test
void aSingleResourceLessStatementSharesTheSnapshot() {
policy("""
{"Statement":{"Effect":"Allow","Principal":{"AWS":"222222222222"},
"Action":"redshift-serverless:GetSnapshot"}}
""");
assertEquals(List.of(snapshot), shared.readable(OWNER, List.of(snapshot), REGION));
}
@Test
void readableKeepsOnlyTheSnapshotsSharedWithTheCaller() {
RedshiftServerlessSnapshot hidden = new RedshiftServerlessSnapshot(snapshot);
hidden.setSnapshotName("hidden");
hidden.setSnapshotArn("arn:aws:redshift-serverless:us-east-1:" + OWNER + ":snapshot/hidden");
policy(statement("Allow", "arn:aws:iam::" + CALLER + ":root"));
when(policies.policyOfSnapshot(eq(OWNER), eq(hidden), eq(REGION))).thenReturn(Optional.empty());
assertEquals(List.of(snapshot), shared.readable(OWNER, List.of(snapshot, hidden), REGION));
}
@Test
void aSnapshotOfAnotherPartitionIsNotSharedWhateverThePolicyNames() {
snapshot.setSnapshotArn("arn:aws-cn:redshift-serverless:cn-north-1:" + OWNER + ":snapshot/shared");
policy(statement("Allow", CALLER));
assertEquals(List.of(), shared.readable(OWNER, List.of(snapshot), REGION));
}
}
@@ -26,6 +26,7 @@ import static io.floci.aws.testing.EnforcementFixtures.auth;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasSize;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.ArgumentMatchers.anyInt;
@@ -73,6 +74,68 @@ class RedshiftServerlessSnapshotSharingIamEnforcementIntegrationTest {
Instant.now().plusSeconds(900), List.of()));
}
@Test
void sharedSnapshotReadsAndListsRespectDenialsAndSnapshotRecreation() {
String suffix = UUID.randomUUID().toString().substring(0, 8);
String namespace = "read-src-" + suffix;
String snapshot = "read-shared-" + suffix;
String privateSnapshot = "read-private-" + suffix;
String callerKey = user(CALLER, """
{"Statement":[{"Effect":"Allow","Action":["redshift-serverless:GetSnapshot",
"redshift-serverless:ListSnapshots"],"Resource":"*"}]}
""");
String allow = """
{"Statement":{"Effect":"Allow","Principal":{"AWS":"%s"},
"Action":"redshift-serverless:GetSnapshot"}}
""".formatted(CALLER);
String deny = """
{"Statement":[
{"Effect":"Allow","Principal":{"AWS":"%s"},
"Action":"redshift-serverless:GetSnapshot","Resource":"*"},
{"Effect":"Deny","Principal":{"AWS":"%s"},
"Action":"redshift-serverless:GetSnapshot"}
]}
""".formatted(CALLER, CALLER);
call(OWNER, "CreateNamespace", Map.of("namespaceName", namespace, "adminUsername", "admin")).statusCode(200);
String arn = call(OWNER, "CreateSnapshot", Map.of("snapshotName", snapshot, "namespaceName", namespace))
.statusCode(200).extract().path("snapshot.snapshotArn");
call(OWNER, "CreateSnapshot", Map.of("snapshotName", privateSnapshot, "namespaceName", namespace)).statusCode(200);
Map<String, String> byName = Map.of("snapshotName", snapshot, "ownerAccount", OWNER);
Map<String, String> byArn = Map.of("snapshotArn", arn, "ownerAccount", OWNER);
Map<String, String> list = Map.of("ownerAccount", OWNER);
readDenied(call(callerKey, "GetSnapshot", Map.of("snapshotName", privateSnapshot, "ownerAccount", OWNER)));
readDenied(call(callerKey, "GetSnapshot", byName));
call(callerKey, "ListSnapshots", list).statusCode(200).body("snapshots", hasSize(0));
call(OWNER, "PutResourcePolicy", Map.of("resourceArn", arn, "policy", allow)).statusCode(200);
call(callerKey, "GetSnapshot", byName).statusCode(200).body("snapshot.snapshotName", equalTo(snapshot));
call(callerKey, "GetSnapshot", byArn).statusCode(200).body("snapshot.snapshotName", equalTo(snapshot));
call(callerKey, "ListSnapshots", list).statusCode(200)
.body("snapshots", hasSize(1)).body("snapshots[0].snapshotName", equalTo(snapshot));
call(OWNER, "PutResourcePolicy", Map.of("resourceArn", arn, "policy", deny)).statusCode(200);
readDenied(call(callerKey, "GetSnapshot", byName));
call(callerKey, "ListSnapshots", list).statusCode(200).body("snapshots", hasSize(0));
call(OWNER, "DeleteResourcePolicy", Map.of("resourceArn", arn)).statusCode(200);
readDenied(call(callerKey, "GetSnapshot", byName));
call(callerKey, "ListSnapshots", list).statusCode(200).body("snapshots", hasSize(0));
call(OWNER, "PutResourcePolicy", Map.of("resourceArn", arn, "policy", allow)).statusCode(200);
call(callerKey, "GetSnapshot", byName).statusCode(200);
call(OWNER, "DeleteSnapshot", Map.of("snapshotName", snapshot)).statusCode(200);
call(OWNER, "CreateSnapshot", Map.of("snapshotName", snapshot, "namespaceName", namespace)).statusCode(200);
readDenied(call(callerKey, "GetSnapshot", byName));
readDenied(call(callerKey, "GetSnapshot", byArn));
call(callerKey, "ListSnapshots", list).statusCode(200).body("snapshots", hasSize(0));
}
private static void readDenied(ValidatableResponse response) {
response.statusCode(400).body("__type", equalTo("AccessDeniedException"))
.body("message", containsString("redshift-serverless:GetSnapshot"));
}
@Test
void aSnapshotIsRestorableByAnotherAccountOnlyWhenItsPolicyNamesTheCaller() {
String suffix = UUID.randomUUID().toString().substring(0, 8);