mirror of
https://github.com/floci-io/floci.git
synced 2026-11-27 07:39:24 -05:00
fix(ram): paginate GetResourceShares responses (#7025)
* fix(ram): paginate GetResourceShares responses * test(ram): exercise GetResourceShares SDK paginator * refactor(ram): share resource pagination validation
This commit is contained in:
1 parent
2463708199
commit
fa243bf78e
6 files changed
+235
-34
No files matched your search
@@ -8,6 +8,7 @@ import com.fasterxml.jackson.databind.node.ArrayNode;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.common.PaginatedResult;
|
||||
import io.github.hectorvent.floci.core.common.Pagination;
|
||||
import io.github.hectorvent.floci.core.common.RegionResolver;
|
||||
import io.github.hectorvent.floci.core.common.ServicePrincipals;
|
||||
import io.github.hectorvent.floci.services.iam.IamService;
|
||||
@@ -113,19 +114,24 @@ public class RamController {
|
||||
JsonNode request = readTree(body);
|
||||
String resourceOwner = request.hasNonNull("resourceOwner")
|
||||
? request.path("resourceOwner").asText() : null;
|
||||
List<ResourceShare> shares = service.getResourceShares(
|
||||
PaginatedResult<ResourceShare> page = service.getResourceSharesPage(
|
||||
regionResolver.getAccountId(),
|
||||
resourceOwner,
|
||||
request.hasNonNull("name") ? request.path("name").asText() : null,
|
||||
stringList(request.path("resourceShareArns")),
|
||||
request.hasNonNull("resourceShareStatus")
|
||||
? request.path("resourceShareStatus").asText() : null,
|
||||
regionResolver.getRegion());
|
||||
regionResolver.getRegion(),
|
||||
Pagination.parseMaxResults(request, "maxResults", "InvalidParameterException"),
|
||||
Pagination.parseNextToken(request, "nextToken", "InvalidNextTokenException"));
|
||||
|
||||
ObjectNode response = objectMapper.createObjectNode();
|
||||
ArrayNode array = objectMapper.createArrayNode();
|
||||
shares.forEach(share -> array.add(shareNode(share)));
|
||||
page.items().forEach(share -> array.add(shareNode(share)));
|
||||
response.set("resourceShares", array);
|
||||
if (page.nextToken() != null) {
|
||||
response.put("nextToken", page.nextToken());
|
||||
}
|
||||
return Response.ok(response).build();
|
||||
}
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@ import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import io.github.hectorvent.floci.core.common.AwsArnUtils;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.common.AwsPartitions;
|
||||
import io.github.hectorvent.floci.core.common.Base64Url;
|
||||
import io.github.hectorvent.floci.core.common.PaginatedResult;
|
||||
import io.github.hectorvent.floci.core.common.Pagination;
|
||||
import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend;
|
||||
@@ -31,6 +32,8 @@ import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Supplier;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
@@ -178,6 +181,19 @@ public class RamService {
|
||||
return result;
|
||||
}
|
||||
|
||||
public PaginatedResult<ResourceShare> getResourceSharesPage(String callerAccountId, String resourceOwner,
|
||||
String name, List<String> resourceShareArns,
|
||||
String resourceShareStatus, String region,
|
||||
Integer maxResults, String nextToken) {
|
||||
return paginateRamResults(
|
||||
() -> getResourceShares(callerAccountId, resourceOwner, name,
|
||||
resourceShareArns, resourceShareStatus, region),
|
||||
ResourceShare::getResourceShareArn, maxResults, nextToken,
|
||||
() -> "ram-resource-shares|" + callerAccountId + "|" + region + "|" + resourceOwner
|
||||
+ "|" + (name == null ? "~" : Base64Url.encode(name)) + "|" + resourceShareStatus + "|"
|
||||
+ resourceShareArns.stream().distinct().sorted().map(Base64Url::encode).toList());
|
||||
}
|
||||
|
||||
/**
|
||||
* @param resourceShareArns restrict to invitations for these shares, or empty for any
|
||||
* @param resourceShareInvitationArns restrict to these invitation ARNs, or empty for any
|
||||
@@ -437,41 +453,49 @@ public class RamService {
|
||||
if (resourceArn != null && !isValidArn(resourceArn)) {
|
||||
throw new AwsException("MalformedArnException", "Invalid resourceArn: " + resourceArn, 400);
|
||||
}
|
||||
return paginateRamResults(() -> {
|
||||
List<ResourceShareAssociation> associations = new ArrayList<>();
|
||||
if (associationStatus == null || "ASSOCIATED".equals(associationStatus)) {
|
||||
for (ResourceShare share : sharesIn(region)) {
|
||||
if (!isConsumableShare(share, callerAccountId, "SELF", resourceShareArns)) {
|
||||
continue;
|
||||
}
|
||||
if ("RESOURCE".equals(associationType)) {
|
||||
for (String entity : new LinkedHashSet<>(share.getResourceArns())) {
|
||||
if (resourceArn == null || resourceArn.equals(entity)) {
|
||||
associations.add(association(share, entity, "RESOURCE"));
|
||||
}
|
||||
}
|
||||
} else if ("PRINCIPAL".equals(associationType)) {
|
||||
for (String entity : new LinkedHashSet<>(share.getPrincipals())) {
|
||||
if (principal == null || principal.equals(entity)) {
|
||||
associations.add(association(share, entity, "PRINCIPAL"));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return associations;
|
||||
},
|
||||
association -> association.resourceShareArn() + "\u0000" + association.associatedEntity(),
|
||||
maxResults, nextToken,
|
||||
() -> "ram-associations|" + callerAccountId + "|" + region + "|" + associationType
|
||||
+ "|" + associationStatus + "|" + resourceShareArns.stream().sorted().toList()
|
||||
+ "|" + resourceArn + "|" + principal);
|
||||
}
|
||||
|
||||
private static <T> PaginatedResult<T> paginateRamResults(Supplier<List<T>> items,
|
||||
Function<T, String> cursorOf,
|
||||
Integer maxResults, String nextToken,
|
||||
Supplier<String> namespace) {
|
||||
if (maxResults != null && (maxResults < 1 || maxResults > 500)) {
|
||||
throw new AwsException("InvalidParameterException", "maxResults must be between 1 and 500.", 400);
|
||||
}
|
||||
if (nextToken != null && nextToken.isEmpty()) {
|
||||
throw new AwsException("InvalidNextTokenException", "Invalid nextToken.", 400);
|
||||
}
|
||||
|
||||
List<ResourceShareAssociation> associations = new ArrayList<>();
|
||||
if (associationStatus == null || "ASSOCIATED".equals(associationStatus)) {
|
||||
for (ResourceShare share : sharesIn(region)) {
|
||||
if (!isConsumableShare(share, callerAccountId, "SELF", resourceShareArns)) {
|
||||
continue;
|
||||
}
|
||||
if ("RESOURCE".equals(associationType)) {
|
||||
for (String entity : new LinkedHashSet<>(share.getResourceArns())) {
|
||||
if (resourceArn == null || resourceArn.equals(entity)) {
|
||||
associations.add(association(share, entity, "RESOURCE"));
|
||||
}
|
||||
}
|
||||
} else if ("PRINCIPAL".equals(associationType)) {
|
||||
for (String entity : new LinkedHashSet<>(share.getPrincipals())) {
|
||||
if (principal == null || principal.equals(entity)) {
|
||||
associations.add(association(share, entity, "PRINCIPAL"));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
String namespace = "ram-associations|" + callerAccountId + "|" + region + "|" + associationType
|
||||
+ "|" + associationStatus + "|" + resourceShareArns.stream().sorted().toList()
|
||||
+ "|" + resourceArn + "|" + principal;
|
||||
return Pagination.paginate(associations,
|
||||
association -> association.resourceShareArn() + "\u0000" + association.associatedEntity(),
|
||||
maxResults != null ? maxResults : 100, nextToken, namespace,
|
||||
return Pagination.paginate(items.get(), cursorOf,
|
||||
maxResults != null ? maxResults : 100, nextToken, namespace.get(),
|
||||
ignored -> new AwsException("InvalidNextTokenException", "Invalid nextToken.", 400));
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user