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

@@ -2,12 +2,10 @@ package io.floci.aws.services.dsql;
import io.floci.aws.core.common.IamConditionContextResolver;
import io.floci.aws.core.common.RegionResolver;
import io.floci.aws.core.common.RequestContext;
import io.floci.aws.services.iam.IamService;
import io.floci.aws.services.iam.IamService.CallerArns;
import io.floci.aws.services.iam.RequestCallerResolver;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.context.ContextNotActiveException;
import jakarta.enterprise.inject.Instance;
import jakarta.inject.Inject;
import java.util.HashMap;
@@ -23,26 +21,22 @@ public class DsqlPrincipals {
private final IamService iamService;
private final RegionResolver regionResolver;
private final Instance<RequestContext> requestContext;
private final RequestCallerResolver callers;
@Inject
public DsqlPrincipals(IamService iamService, RegionResolver regionResolver,
Instance<RequestContext> requestContext) {
public DsqlPrincipals(IamService iamService, RegionResolver regionResolver, RequestCallerResolver callers) {
this.iamService = iamService;
this.regionResolver = regionResolver;
this.requestContext = requestContext;
this.callers = callers;
}
public CallerArns caller() {
return callerOf(accessKeyId());
return callers.caller();
}
/** Who signs with {@code accessKeyId}, as {@link #caller()} names the caller of a request. */
public CallerArns callerOf(String accessKeyId) {
String accountId = regionResolver.getAccountId();
return iamService.resolveCallerArns(accessKeyId)
.orElseGet(() -> CallerArns.accountRoot(regionResolver.buildGlobalArn("iam", accountId, "root"),
accountId));
return callers.callerOf(accessKeyId);
}
/**
@@ -65,13 +59,4 @@ public class DsqlPrincipals {
public boolean exists(String arn) {
return iamService.heldPrincipalArn(arn).isPresent();
}
private String accessKeyId() {
try {
return requestContext.isResolvable() ? requestContext.get().getAccessKeyId() : null;
} catch (ContextNotActiveException e) {
// A call outside a request, such as CloudFormation's, is the account's own.
return null;
}
}
}
@@ -0,0 +1,51 @@
package io.floci.aws.services.iam;
import io.floci.aws.core.common.RegionResolver;
import io.floci.aws.core.common.RequestContext;
import io.floci.aws.services.iam.IamService.CallerArns;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.context.ContextNotActiveException;
import jakarta.enterprise.inject.Instance;
import jakarta.inject.Inject;
/**
* Who is calling, for a service that checks a resource policy against the caller itself rather than leaving it to
* IAM enforcement. A credential that names no IAM identity is its account's root, as for STS.
*/
@ApplicationScoped
public class RequestCallerResolver {
private final IamService iamService;
private final RegionResolver regionResolver;
private final Instance<RequestContext> requestContext;
@Inject
public RequestCallerResolver(IamService iamService, RegionResolver regionResolver,
Instance<RequestContext> requestContext) {
this.iamService = iamService;
this.regionResolver = regionResolver;
this.requestContext = requestContext;
}
/** The caller of the current request. */
public CallerArns caller() {
return callerOf(accessKeyId());
}
/** Who signs with {@code accessKeyId}, as {@link #caller()} names the caller of a request. */
public CallerArns callerOf(String accessKeyId) {
String accountId = regionResolver.getAccountId();
return iamService.resolveCallerArns(accessKeyId)
.orElseGet(() -> CallerArns.accountRoot(regionResolver.buildGlobalArn("iam", accountId, "root"),
accountId));
}
private String accessKeyId() {
try {
return requestContext.isResolvable() ? requestContext.get().getAccessKeyId() : null;
} catch (ContextNotActiveException e) {
// A call outside a request, such as CloudFormation's, is the account's own.
return null;
}
}
}
@@ -47,19 +47,22 @@ public class RedshiftServerlessJsonHandler {
private final RedshiftServerlessUsageLimitService usageLimits;
private final RedshiftServerlessAdminJsonHandler admin;
private final RedshiftServerlessCustomDomainService customDomains;
private final RedshiftServerlessSharedSnapshots sharedSnapshots;
@Inject
public RedshiftServerlessJsonHandler(RedshiftServerlessService service, ObjectMapper objectMapper,
RedshiftIamDbUserResolver iamDbUserResolver,
RedshiftServerlessUsageLimitService usageLimits,
RedshiftServerlessAdminJsonHandler admin,
RedshiftServerlessCustomDomainService customDomains) {
RedshiftServerlessCustomDomainService customDomains,
RedshiftServerlessSharedSnapshots sharedSnapshots) {
this.service = service;
this.objectMapper = objectMapper;
this.iamDbUserResolver = iamDbUserResolver;
this.usageLimits = usageLimits;
this.admin = admin;
this.customDomains = customDomains;
this.sharedSnapshots = sharedSnapshots;
}
public Response handle(String action, JsonNode request, String region) {
@@ -249,19 +252,34 @@ public class RedshiftServerlessJsonHandler {
}
/**
* GetSnapshot also accepts {@code snapshotArn}, which must name a snapshot of this account
* and Region.
* GetSnapshot also accepts {@code snapshotArn}, which must name a snapshot of {@code ownerAccount}, or of
* this account without one, in this Region. A snapshot of another account is read from that account; whether
* its policy shares it with the caller is for IAM enforcement to decide.
*/
private Response handleGetSnapshot(JsonNode request, String region) {
return snapshotResponse(service.getSnapshot(snapshotNameOf(request, region, null), region));
String ownerAccount = ServerlessSupport.text(request, "ownerAccount");
if (!service.isOtherAccount(ownerAccount)) {
return snapshotResponse(service.getSnapshot(snapshotNameOf(request, region, null), region));
}
String snapshotName = snapshotNameOf(request, region, ownerAccount);
return snapshotResponse(service.findSnapshotOfAccount(ownerAccount, snapshotName, region)
.orElseThrow(() -> ServerlessSupport.notFound("The snapshot " + snapshotName + " was not found.")));
}
private Response handleListSnapshots(JsonNode request, String region) {
PaginatedResult<RedshiftServerlessSnapshot> page = service.listSnapshots(
ServerlessSupport.text(request, "namespaceName"), ServerlessSupport.text(request, "namespaceArn"), ServerlessSupport.text(request, "ownerAccount"),
AwsJsonTimestamp.epochSeconds(request, "startTime", "ValidationException"),
AwsJsonTimestamp.epochSeconds(request, "endTime", "ValidationException"), region,
ServerlessSupport.maxResults(request), ServerlessSupport.text(request, "nextToken"));
String ownerAccount = ServerlessSupport.text(request, "ownerAccount");
Instant startTime = AwsJsonTimestamp.epochSeconds(request, "startTime", "ValidationException");
Instant endTime = AwsJsonTimestamp.epochSeconds(request, "endTime", "ValidationException");
PaginatedResult<RedshiftServerlessSnapshot> page = service.isOtherAccount(ownerAccount)
? service.listSnapshotsOfAccount(ownerAccount,
owned -> sharedSnapshots.readable(ownerAccount, owned, region),
ServerlessSupport.text(request, "namespaceName"),
ServerlessSupport.text(request, "namespaceArn"), startTime, endTime, region,
ServerlessSupport.maxResults(request), ServerlessSupport.text(request, "nextToken"))
: service.listSnapshots(
ServerlessSupport.text(request, "namespaceName"),
ServerlessSupport.text(request, "namespaceArn"), ownerAccount, startTime, endTime, region,
ServerlessSupport.maxResults(request), ServerlessSupport.text(request, "nextToken"));
ObjectNode response = objectMapper.createObjectNode();
ArrayNode items = response.putArray("snapshots");
page.items().forEach(snapshot -> items.add(snapshotNode(snapshot)));
@@ -48,6 +48,7 @@ import java.util.Set;
import java.util.UUID;
import java.util.function.Consumer;
import java.util.function.Supplier;
import java.util.function.UnaryOperator;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@@ -886,7 +887,29 @@ public class RedshiftServerlessService implements Resettable, StoredResourceArns
String ownerAccount, Instant startTime,
Instant endTime, String region,
Integer maxResults, String nextToken) {
List<RedshiftServerlessSnapshot> all = snapshots.scan(key -> key.startsWith(region + "::")).stream()
return pageOfSnapshots(snapshots.scan(key -> key.startsWith(region + "::")), namespaceName, namespaceArn,
ownerAccount, startTime, endTime, maxResults, nextToken);
}
/**
* Lists the snapshots {@code accountId} owns in {@code region}, narrowed as {@link #listSnapshots} does, to the
* ones {@code visible} keeps. Whether the caller may see a snapshot of another account is for the caller of
* this method to decide.
*/
public PaginatedResult<RedshiftServerlessSnapshot> listSnapshotsOfAccount(
String accountId, UnaryOperator<List<RedshiftServerlessSnapshot>> visible, String namespaceName,
String namespaceArn, Instant startTime, Instant endTime, String region, Integer maxResults,
String nextToken) {
List<RedshiftServerlessSnapshot> owned = snapshots.scanForAccount(accountId,
key -> key.startsWith(region + "::"));
return pageOfSnapshots(visible.apply(owned), namespaceName, namespaceArn, accountId, startTime, endTime,
maxResults, nextToken);
}
private PaginatedResult<RedshiftServerlessSnapshot> pageOfSnapshots(
List<RedshiftServerlessSnapshot> candidates, String namespaceName, String namespaceArn,
String ownerAccount, Instant startTime, Instant endTime, Integer maxResults, String nextToken) {
List<RedshiftServerlessSnapshot> all = candidates.stream()
.filter(snapshot -> namespaceName == null || namespaceName.isBlank()
|| namespaceName.equals(snapshot.getNamespaceName()))
.filter(snapshot -> namespaceArn == null || namespaceArn.isBlank()
@@ -0,0 +1,80 @@
package io.floci.aws.services.redshiftserverless;
import io.floci.aws.core.common.AwsArnUtils;
import io.floci.aws.core.common.IamConditionContextResolver;
import io.floci.aws.core.common.RegionResolver;
import io.floci.aws.services.iam.IamPolicyEvaluator;
import io.floci.aws.services.iam.IamPolicyEvaluator.ResourcePolicyDecision;
import io.floci.aws.services.iam.IamService.CallerArns;
import io.floci.aws.services.iam.RequestCallerResolver;
import io.floci.aws.services.iam.RequestPrincipal;
import io.floci.aws.services.redshiftserverless.model.RedshiftServerlessSnapshot;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Narrows the snapshots another account owns to the ones its resource policy shares with the caller, for
* ListSnapshots, which IAM cannot filter item by item. A snapshot with no policy, or a policy that does not
* allow the caller {@code redshift-serverless:GetSnapshot}, is not shared. GetSnapshot and RestoreFromSnapshot
* name one snapshot, so IAM enforcement checks them through {@link RedshiftServerlessResourcePolicyProvider}.
*/
@ApplicationScoped
public class RedshiftServerlessSharedSnapshots {
static final String GET_ACTION = "redshift-serverless:GetSnapshot";
private final RedshiftServerlessResourcePolicyService policies;
private final IamPolicyEvaluator evaluator;
private final RegionResolver regionResolver;
private final RequestCallerResolver callers;
private final RedshiftServerlessResourcePolicyProvider resourcePolicyProvider;
@Inject
public RedshiftServerlessSharedSnapshots(RedshiftServerlessResourcePolicyService policies,
IamPolicyEvaluator evaluator, RegionResolver regionResolver,
RequestCallerResolver callers,
RedshiftServerlessResourcePolicyProvider resourcePolicyProvider) {
this.policies = policies;
this.evaluator = evaluator;
this.regionResolver = regionResolver;
this.callers = callers;
this.resourcePolicyProvider = resourcePolicyProvider;
}
/**
* The snapshots of {@code ownerAccount} that are shared with the caller. One that is not shared is left out,
* as it is for any caller that cannot see it, rather than refused.
*/
public List<RedshiftServerlessSnapshot> readable(String ownerAccount, List<RedshiftServerlessSnapshot> snapshots,
String region) {
return snapshots.stream().filter(snapshot -> isShared(snapshot, ownerAccount, region)).toList();
}
private boolean isShared(RedshiftServerlessSnapshot snapshot, String ownerAccount, String region) {
String policy = policies.policyOfSnapshot(ownerAccount, snapshot, region).orElse(null);
return policy != null && allows(policy, snapshot, ownerAccount, region);
}
private boolean allows(String policy, RedshiftServerlessSnapshot snapshot, String ownerAccount, String region) {
CallerArns caller = callers.caller();
// an account in one partition cannot be granted a snapshot of another, whatever the policy names
if (!AwsArnUtils.parse(snapshot.getSnapshotArn()).partition()
.equals(AwsArnUtils.parse(caller.callerArn()).partition())) {
return false;
}
Map<String, List<String>> context = new HashMap<>();
caller.putPrincipalKeys(context);
Map<String, List<String>> effective = IamConditionContextResolver.withGlobalContext(context,
snapshot.getSnapshotArn(), region, regionResolver.getAccountId(), ownerAccount, GET_ACTION);
String normalizedPolicy = resourcePolicyProvider.withResource(policy, snapshot.getSnapshotArn());
ResourcePolicyDecision decision = evaluator.evaluateResourcePolicyFor(List.of(normalizedPolicy),
RequestPrincipal.caller(caller), GET_ACTION, snapshot.getSnapshotArn(), effective);
// ALLOW_ACCOUNT is a grant to the caller's whole account, which a cross-account policy may give.
return decision == ResourcePolicyDecision.ALLOW || decision == ResourcePolicyDecision.ALLOW_ACCOUNT
|| decision == ResourcePolicyDecision.ALLOW_DIRECT_IAM_USER;
}
}