mirror of
https://github.com/floci-io/floci.git
synced 2026-11-23 06:00:46 -05:00
fix(core): name the real pagination parameter in invalid-token errors (#5776)
* fix(core): name the real pagination parameter in invalid-token errors The maxResults/nextToken overload of Pagination.paginate hardcoded "Invalid nextToken." for a token that does not decode. Redshift DescribeSnapshotCopyGrants and the DMS Describe* operations take that token as Marker, so a malformed marker produced an error naming a parameter those operations do not have. Add an overload taking the wire name of the token parameter and use it in the message; the existing overloads delegate with "nextToken", so every NextToken caller is unchanged. The Redshift and both DMS call sites (DescribeReplicationSubnetGroups and the shared describe used by DescribeEndpoints, DescribeReplicationInstances and DescribeReplicationTasks) now pass "Marker". * fix(redshift,ce): name Marker and NextPageToken in invalid-token errors DescribeSnapshotSchedules (added in #5574) takes its page token as Marker but went through the paginate overload that reports "Invalid nextToken.". Cost Explorer GetAnomalyMonitors and GetAnomalySubscriptions take NextPageToken and had the same message. Both now pass the wire name to the tokenName overload. These were the remaining paginate callers whose token parameter is not nextToken; callers that render their own token error (Lambda durable executions, Auto Scaling, SES, Cognito) are unaffected.
This commit is contained in:
1 parent
832f1008bb
commit
0264c1b960
9 files changed
+63
-6
No files matched your search
@@ -34,13 +34,26 @@ public final class Pagination {
|
||||
Integer maxResults, String nextToken,
|
||||
int defaultPageSize, int maxResultsLimit,
|
||||
String errorCode) {
|
||||
return paginate(all, cursorOf, maxResults, nextToken, defaultPageSize, maxResultsLimit, errorCode,
|
||||
"nextToken");
|
||||
}
|
||||
|
||||
/**
|
||||
* As above, naming the token parameter in the invalid-token message: query-protocol and DMS
|
||||
* operations call it {@code Marker}, and an error naming {@code nextToken} points the caller
|
||||
* at a parameter the operation does not have.
|
||||
*/
|
||||
public static <T> PaginatedResult<T> paginate(List<T> all, Function<T, String> cursorOf,
|
||||
Integer maxResults, String nextToken,
|
||||
int defaultPageSize, int maxResultsLimit,
|
||||
String errorCode, String tokenName) {
|
||||
if (maxResults != null && (maxResults < 1 || maxResults > maxResultsLimit)) {
|
||||
throw new AwsException(errorCode,
|
||||
"maxResults must be between 1 and " + maxResultsLimit, 400);
|
||||
}
|
||||
int limit = maxResults != null ? maxResults : defaultPageSize;
|
||||
return slice(all, cursorOf, limit,
|
||||
decodeCursor(nextToken, "", token -> new AwsException(errorCode, "Invalid nextToken.", 400)), "");
|
||||
decodeCursor(nextToken, "", token -> new AwsException(errorCode, "Invalid " + tokenName + ".", 400)), "");
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -241,7 +241,8 @@ public class CostAnomalyService implements Resettable {
|
||||
String token = present(request, "NextPageToken") ? text(request, "NextPageToken", 8192) : null;
|
||||
int pageSize = Math.min(maxResults, Math.max(definitions.size(), 1));
|
||||
PaginatedResult<ObjectNode> page = Pagination.paginate(definitions,
|
||||
value -> value.path(arnKey).asText(), pageSize, token, Integer.MAX_VALUE, "InvalidNextTokenException");
|
||||
value -> value.path(arnKey).asText(), pageSize, token, Integer.MAX_VALUE, Integer.MAX_VALUE,
|
||||
"InvalidNextTokenException", "NextPageToken");
|
||||
ObjectNode response = mapper.createObjectNode();
|
||||
response.putArray(responseKey).addAll(page.items());
|
||||
if (page.nextToken() != null) {
|
||||
|
||||
@@ -380,7 +380,7 @@ public class DmsService implements Resettable {
|
||||
}
|
||||
return Pagination.paginate(matching, ReplicationSubnetGroup::getReplicationSubnetGroupIdentifier,
|
||||
maxRecords, marker, DEFAULT_MAX_RECORDS, MAXIMUM_MAX_RECORDS,
|
||||
"InvalidParameterValueException");
|
||||
"InvalidParameterValueException", "Marker");
|
||||
}
|
||||
|
||||
public synchronized void deleteReplicationSubnetGroup(JsonNode request, String region) {
|
||||
@@ -495,7 +495,8 @@ public class DmsService implements Resettable {
|
||||
"No " + kind.label() + " found matching provided filters.", 400);
|
||||
}
|
||||
return Pagination.paginate(matching, resource -> member(resource, kind.idMember()),
|
||||
maxRecords, marker, DEFAULT_MAX_RECORDS, MAXIMUM_MAX_RECORDS, "InvalidParameterValueException");
|
||||
maxRecords, marker, DEFAULT_MAX_RECORDS, MAXIMUM_MAX_RECORDS, "InvalidParameterValueException",
|
||||
"Marker");
|
||||
}
|
||||
|
||||
private void rename(Kind kind, String region, Found found, ObjectNode attributes, JsonNode request) {
|
||||
|
||||
@@ -1315,7 +1315,7 @@ public class RedshiftService {
|
||||
|
||||
return Pagination.paginate(matching, SnapshotCopyGrant::getSnapshotCopyGrantName,
|
||||
maxRecords, marker, SNAPSHOT_COPY_GRANT_PAGE_DEFAULT, SNAPSHOT_COPY_GRANT_PAGE_MAX,
|
||||
"InvalidParameterValue");
|
||||
"InvalidParameterValue", "Marker");
|
||||
}
|
||||
|
||||
public synchronized SnapshotCopyGrant deleteSnapshotCopyGrant(String name) {
|
||||
@@ -1520,7 +1520,7 @@ public class RedshiftService {
|
||||
|| tagValues.stream().anyMatch(s.getTags()::containsValue))
|
||||
.toList();
|
||||
return Pagination.paginate(matching, SnapshotSchedule::getScheduleIdentifier, maxRecords, marker,
|
||||
SNAPSHOT_SCHEDULE_PAGE_MAX, "InvalidParameterValue");
|
||||
SNAPSHOT_SCHEDULE_PAGE_MAX, SNAPSHOT_SCHEDULE_PAGE_MAX, "InvalidParameterValue", "Marker");
|
||||
}
|
||||
|
||||
/** Identifiers of the clusters currently using the schedule, sorted for a stable response. */
|
||||
|
||||
@@ -85,6 +85,14 @@ class PaginationTest {
|
||||
assertEquals("Invalid nextToken.", e.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void legacyPaging_namesTheTokenParameterTheCallerGives() {
|
||||
AwsException e = assertThrows(AwsException.class, () -> Pagination.paginate(ITEMS,
|
||||
Function.identity(), 1, "!!!", 3, 3, "InvalidParameterValue", "Marker"));
|
||||
assertEquals("InvalidParameterValue", e.getErrorCode());
|
||||
assertEquals("Invalid Marker.", e.getMessage());
|
||||
}
|
||||
|
||||
private static PaginatedResult<String> page(int limit, String token, String namespace) {
|
||||
return Pagination.paginate(ITEMS, Function.identity(), limit, token, namespace, INVALID_TOKEN);
|
||||
}
|
||||
|
||||
@@ -190,6 +190,9 @@ class CostAnomalyServiceTest {
|
||||
() -> service.getMonitors(mapper.createObjectNode().put("MaxResults", 0)));
|
||||
assertError("InvalidNextTokenException",
|
||||
() -> service.getMonitors(mapper.createObjectNode().put("NextPageToken", "!not-base64")));
|
||||
AwsException badToken = assertThrows(AwsException.class,
|
||||
() -> service.getSubscriptions(mapper.createObjectNode().put("NextPageToken", "!not-base64")));
|
||||
assertEquals("Invalid NextPageToken.", badToken.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -193,6 +193,19 @@ class DmsServiceTest {
|
||||
second.items().stream().map(ReplicationSubnetGroup::getReplicationSubnetGroupIdentifier).toList());
|
||||
}
|
||||
|
||||
@Test
|
||||
void describeRejectsMalformedMarkerNamingMarker() {
|
||||
AwsException groups = assertThrows(AwsException.class,
|
||||
() -> service.describeReplicationSubnetGroups(pageRequest(20, "!!!"), REGION));
|
||||
assertEquals("InvalidParameterValueException", groups.getErrorCode());
|
||||
assertEquals("Invalid Marker.", groups.getMessage());
|
||||
|
||||
AwsException endpoints = assertThrows(AwsException.class,
|
||||
() -> service.describeEndpoints(pageRequest(20, "!!!"), REGION));
|
||||
assertEquals("InvalidParameterValueException", endpoints.getErrorCode());
|
||||
assertEquals("Invalid Marker.", endpoints.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void describeDefaultsToOneHundredRecordsPerPage() {
|
||||
createGroups(25);
|
||||
|
||||
@@ -1153,4 +1153,12 @@ public class RedshiftOperationsTest {
|
||||
|
||||
assertEquals(200, redshift("DeleteCluster", "ClusterIdentifier", "cluster-sched").statusCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(11)
|
||||
void describeSnapshotSchedulesRejectsMalformedMarkerNamingMarker() {
|
||||
Response response = redshift("DescribeSnapshotSchedules", "Marker", "!!!");
|
||||
assertFault(response, 400, "InvalidParameterValue");
|
||||
assertEquals("Invalid Marker.", XmlPath.from(response.asString()).getString("ErrorResponse.Error.Message"));
|
||||
}
|
||||
}
|
||||
@@ -1678,6 +1678,16 @@ class RedshiftServiceTest {
|
||||
assertNull(second.nextToken(), "last page must not carry a marker");
|
||||
}
|
||||
|
||||
@Test
|
||||
void describeSnapshotCopyGrantsRejectsMalformedMarkerNamingMarker() {
|
||||
when(snapshotCopyGrantBackend.scan(any())).thenReturn(List.of(new SnapshotCopyGrant("grant-a", "key-a")));
|
||||
|
||||
AwsException ex = assertThrows(AwsException.class,
|
||||
() -> service.describeSnapshotCopyGrants(null, null, "!!!"));
|
||||
assertEquals("InvalidParameterValue", ex.getErrorCode());
|
||||
assertEquals("Invalid Marker.", ex.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void describeSnapshotCopyGrantsOmitsMarkerWhenPageExactlyFits() {
|
||||
List<SnapshotCopyGrant> stored = new ArrayList<>();
|
||||
|
||||
Reference in new issue
Block a user