Skip to content

armgit / floci

Public Imported from GitHub
0

Light, fluffy, and always free - The AWS Local Emulator alternative

fix(core): name the real pagination parameter in invalid-token errors (#5776)

Allen Sanborn0264c1b9602f
diff --git a/src/main/java/io/github/hectorvent/floci/core/common/Pagination.java b/src/main/java/io/github/hectorvent/floci/core/common/Pagination.java
index e80955f02d..acc091b7fd 100644
--- a/src/main/java/io/github/hectorvent/floci/core/common/Pagination.java
+++ b/src/main/java/io/github/hectorvent/floci/core/common/Pagination.java
@@ -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)), "");
}
/**
diff --git a/src/main/java/io/github/hectorvent/floci/services/ce/CostAnomalyService.java b/src/main/java/io/github/hectorvent/floci/services/ce/CostAnomalyService.java
index 77d46fb22e..eccc303fa0 100644
--- a/src/main/java/io/github/hectorvent/floci/services/ce/CostAnomalyService.java
+++ b/src/main/java/io/github/hectorvent/floci/services/ce/CostAnomalyService.java
@@ -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) {
diff --git a/src/main/java/io/github/hectorvent/floci/services/dms/DmsService.java b/src/main/java/io/github/hectorvent/floci/services/dms/DmsService.java
index 2c61cde28c..b2f0b9b5eb 100644
--- a/src/main/java/io/github/hectorvent/floci/services/dms/DmsService.java
+++ b/src/main/java/io/github/hectorvent/floci/services/dms/DmsService.java
@@ -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) {
diff --git a/src/main/java/io/github/hectorvent/floci/services/redshift/RedshiftService.java b/src/main/java/io/github/hectorvent/floci/services/redshift/RedshiftService.java
index aa74953c25..cee5db4894 100644
--- a/src/main/java/io/github/hectorvent/floci/services/redshift/RedshiftService.java
+++ b/src/main/java/io/github/hectorvent/floci/services/redshift/RedshiftService.java
@@ -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. */
diff --git a/src/test/java/io/github/hectorvent/floci/core/common/PaginationTest.java b/src/test/java/io/github/hectorvent/floci/core/common/PaginationTest.java
index 2de6025e74..724d7d0e49 100644
--- a/src/test/java/io/github/hectorvent/floci/core/common/PaginationTest.java
+++ b/src/test/java/io/github/hectorvent/floci/core/common/PaginationTest.java
@@ -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);
}
diff --git a/src/test/java/io/github/hectorvent/floci/services/ce/CostAnomalyServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/ce/CostAnomalyServiceTest.java
index ad59ec9b8d..2409c73a4d 100644
--- a/src/test/java/io/github/hectorvent/floci/services/ce/CostAnomalyServiceTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/ce/CostAnomalyServiceTest.java
@@ -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
diff --git a/src/test/java/io/github/hectorvent/floci/services/dms/DmsServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/dms/DmsServiceTest.java
index a8dd1ccd96..8151bc8057 100644
--- a/src/test/java/io/github/hectorvent/floci/services/dms/DmsServiceTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/dms/DmsServiceTest.java
@@ -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);
diff --git a/src/test/java/io/github/hectorvent/floci/services/redshift/RedshiftOperationsTest.java b/src/test/java/io/github/hectorvent/floci/services/redshift/RedshiftOperationsTest.java
index eff00a4e92..546d443f5d 100644
--- a/src/test/java/io/github/hectorvent/floci/services/redshift/RedshiftOperationsTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/redshift/RedshiftOperationsTest.java
@@ -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"));
+ }
}
diff --git a/src/test/java/io/github/hectorvent/floci/services/redshift/RedshiftServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/redshift/RedshiftServiceTest.java
index e8e770d840..90cc10f449 100644
--- a/src/test/java/io/github/hectorvent/floci/services/redshift/RedshiftServiceTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/redshift/RedshiftServiceTest.java
@@ -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<>();