mirror of
https://github.com/floci-io/floci.git
synced 2026-11-23 06:00:46 -05:00
fix(glue): paginate security configurations (#6496)
* fix(glue): paginate security configurations Honor GetSecurityConfigurations page size and continuation tokens using the existing Glue paging helper. Enforce the documented 1-200 range and preserve account and region isolation. * refactor(glue): share action-specific pagination limits Centralize range validation for security configurations, workflows and triggers. Defer list loading until after validation to preserve dependent-job error ordering and keep the existing default and general helper limit.
This commit is contained in:
1 parent
0a13e4259e
commit
9c8d2ba58d
5 files changed
+165
-20
No files matched your search
@@ -134,7 +134,7 @@ retroactively.
|
||||
|--------|-------------|
|
||||
| CreateSecurityConfiguration | Creates a named security configuration. |
|
||||
| GetSecurityConfiguration | Returns a named security configuration. |
|
||||
| GetSecurityConfigurations | Lists stored security configurations. `MaxResults` and `NextToken` are currently ignored, so the complete list is returned. |
|
||||
| GetSecurityConfigurations | Lists stored security configurations with `MaxResults` (1 to 200) and `NextToken` pagination, scoped to the account and region. |
|
||||
| DeleteSecurityConfiguration | Deletes a named security configuration. |
|
||||
|
||||
#### Jobs
|
||||
|
||||
@@ -455,12 +455,15 @@ public class GlueJsonHandler {
|
||||
glueService.deleteSecurityConfiguration(request.path("Name").asText(null), region);
|
||||
yield Response.ok(Map.of()).build();
|
||||
}
|
||||
case "GetSecurityConfigurations" -> {
|
||||
GlueService.Page<SecurityConfiguration> page = glueService.getSecurityConfigurations(
|
||||
region, readMaxResults(request), readNextToken(request));
|
||||
yield Response.ok(pageResponse("SecurityConfigurations", page.items(), page.nextToken())).build();
|
||||
}
|
||||
// Read-only Glue actions for resources the emulator does not model. The AWS SDK
|
||||
// expects each to return a 200 with its result key present (empty), so we emit the
|
||||
// documented empty shape rather than an InvalidAction 400 that callers can't read.
|
||||
case "ListDataQualityRulesets" -> Response.ok(Map.of("Rulesets", List.of())).build();
|
||||
case "GetSecurityConfigurations" -> Response.ok(Map.of(
|
||||
"SecurityConfigurations", glueService.getSecurityConfigurations(region))).build();
|
||||
case "CreateTrigger" -> {
|
||||
Trigger trigger = toTrigger(request);
|
||||
trigger.setName(request.path("Name").asText(null));
|
||||
|
||||
@@ -64,6 +64,7 @@ import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.function.Supplier;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.regex.PatternSyntaxException;
|
||||
@@ -320,6 +321,14 @@ public class GlueService {
|
||||
return securityConfigurationStore.scan(key -> key.startsWith(prefix));
|
||||
}
|
||||
|
||||
public Page<SecurityConfiguration> getSecurityConfigurations(String region, Integer maxResults, String nextToken) {
|
||||
return paginate(() -> {
|
||||
List<SecurityConfiguration> configurations = new ArrayList<>(getSecurityConfigurations(region));
|
||||
configurations.sort(Comparator.comparing(SecurityConfiguration::getName));
|
||||
return configurations;
|
||||
}, maxResults, nextToken, 200);
|
||||
}
|
||||
|
||||
private static String securityConfigurationKey(String region, String name) {
|
||||
return region + ":" + name;
|
||||
}
|
||||
@@ -2243,16 +2252,14 @@ public class GlueService {
|
||||
}
|
||||
|
||||
public Page<String> listWorkflows(Integer maxResults, String nextToken) {
|
||||
if (maxResults != null && (maxResults < 1 || maxResults > MAX_WORKFLOWS_PAGE_SIZE)) {
|
||||
throw new AwsException("InvalidInputException",
|
||||
"MaxResults must be between 1 and " + MAX_WORKFLOWS_PAGE_SIZE, 400);
|
||||
}
|
||||
List<String> names = new ArrayList<>();
|
||||
for (Workflow workflow : workflowStore.scan(k -> true)) {
|
||||
names.add(workflow.getName());
|
||||
}
|
||||
names.sort(Comparator.naturalOrder());
|
||||
return paginate(names, maxResults, nextToken);
|
||||
return paginate(() -> {
|
||||
List<String> names = new ArrayList<>();
|
||||
for (Workflow workflow : workflowStore.scan(k -> true)) {
|
||||
names.add(workflow.getName());
|
||||
}
|
||||
names.sort(Comparator.naturalOrder());
|
||||
return names;
|
||||
}, maxResults, nextToken, MAX_WORKFLOWS_PAGE_SIZE);
|
||||
}
|
||||
|
||||
public BatchGetWorkflowsResult batchGetWorkflows(List<String> names) {
|
||||
@@ -2365,11 +2372,7 @@ public class GlueService {
|
||||
* all triggers are returned when none can.
|
||||
*/
|
||||
public Page<Trigger> getTriggers(String dependentJobName, Integer maxResults, String nextToken) {
|
||||
if (maxResults != null && (maxResults < 1 || maxResults > MAX_TRIGGERS_PAGE_SIZE)) {
|
||||
throw new AwsException("InvalidInputException",
|
||||
"MaxResults must be between 1 and " + MAX_TRIGGERS_PAGE_SIZE, 400);
|
||||
}
|
||||
return paginate(triggersStarting(dependentJobName), maxResults, nextToken);
|
||||
return paginate(() -> triggersStarting(dependentJobName), maxResults, nextToken, MAX_TRIGGERS_PAGE_SIZE);
|
||||
}
|
||||
|
||||
public Page<String> listTriggers(String dependentJobName, Integer maxResults, String nextToken,
|
||||
@@ -3011,9 +3014,14 @@ public class GlueService {
|
||||
public record Page<T>(List<T> items, String nextToken) {}
|
||||
|
||||
<T> Page<T> paginate(List<T> all, Integer maxResults, String nextToken) {
|
||||
if (maxResults != null && (maxResults < 1 || maxResults > 1000)) {
|
||||
throw new AwsException("InvalidInputException", "MaxResults must be between 1 and 1000", 400);
|
||||
return paginate(() -> all, maxResults, nextToken, 1000);
|
||||
}
|
||||
|
||||
private <T> Page<T> paginate(Supplier<List<T>> items, Integer maxResults, String nextToken, int maximum) {
|
||||
if (maxResults != null && (maxResults < 1 || maxResults > maximum)) {
|
||||
throw new AwsException("InvalidInputException", "MaxResults must be between 1 and " + maximum, 400);
|
||||
}
|
||||
List<T> all = items.get();
|
||||
int limit = maxResults == null ? 100 : maxResults;
|
||||
int start = 0;
|
||||
if (nextToken != null && !nextToken.isBlank()) {
|
||||
|
||||
+106
@@ -0,0 +1,106 @@
|
||||
package io.github.hectorvent.floci.services.glue;
|
||||
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.restassured.RestAssured;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
|
||||
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
|
||||
import software.amazon.awssdk.http.urlconnection.UrlConnectionHttpClient;
|
||||
import software.amazon.awssdk.regions.Region;
|
||||
import software.amazon.awssdk.services.glue.GlueClient;
|
||||
import software.amazon.awssdk.services.glue.model.GetSecurityConfigurationsResponse;
|
||||
import software.amazon.awssdk.services.glue.model.InvalidInputException;
|
||||
import software.amazon.awssdk.services.glue.model.SecurityConfiguration;
|
||||
|
||||
import java.net.URI;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
@QuarkusTest
|
||||
class GlueSecurityConfigurationPaginationIntegrationTest {
|
||||
|
||||
@Test
|
||||
void sdkPagesConfigurationsInNameOrderWithinTheAccountAndRegion() {
|
||||
String prefix = "page-" + UUID.randomUUID();
|
||||
List<String> names = List.of(prefix + "-a", prefix + "-b", prefix + "-c");
|
||||
try (GlueClient client = client("741648700001", Region.US_EAST_1);
|
||||
GlueClient foreignAccount = client("741648700002", Region.US_EAST_1);
|
||||
GlueClient foreignRegion = client("741648700001", Region.US_WEST_2)) {
|
||||
for (String name : names.reversed()) {
|
||||
client.createSecurityConfiguration(request -> request.name(name).encryptionConfiguration(builder -> {}));
|
||||
}
|
||||
foreignAccount.createSecurityConfiguration(request -> request.name(prefix + "-0")
|
||||
.encryptionConfiguration(builder -> {}));
|
||||
foreignRegion.createSecurityConfiguration(request -> request.name(prefix + "-0")
|
||||
.encryptionConfiguration(builder -> {}));
|
||||
try {
|
||||
assertEquals(names, client.getSecurityConfigurations(request -> {}).securityConfigurations().stream()
|
||||
.map(SecurityConfiguration::name).sorted().toList());
|
||||
GetSecurityConfigurationsResponse first = client.getSecurityConfigurations(request -> request.maxResults(1));
|
||||
assertEquals(List.of(names.get(0)), first.securityConfigurations().stream()
|
||||
.map(SecurityConfiguration::name).toList());
|
||||
assertNotNull(first.nextToken());
|
||||
GetSecurityConfigurationsResponse second = client.getSecurityConfigurations(request -> request
|
||||
.maxResults(1).nextToken(first.nextToken()));
|
||||
assertEquals(List.of(names.get(1)), second.securityConfigurations().stream()
|
||||
.map(SecurityConfiguration::name).toList());
|
||||
assertNotNull(second.nextToken());
|
||||
GetSecurityConfigurationsResponse last = client.getSecurityConfigurations(request -> request
|
||||
.maxResults(1).nextToken(second.nextToken()));
|
||||
assertEquals(List.of(names.get(2)), last.securityConfigurations().stream()
|
||||
.map(SecurityConfiguration::name).toList());
|
||||
assertNull(last.nextToken());
|
||||
assertEquals(names, client.getSecurityConfigurationsPaginator(request -> request.maxResults(2))
|
||||
.securityConfigurations().stream().map(SecurityConfiguration::name).toList());
|
||||
} finally {
|
||||
names.forEach(name -> client.deleteSecurityConfiguration(request -> request.name(name)));
|
||||
foreignAccount.deleteSecurityConfiguration(request -> request.name(prefix + "-0"));
|
||||
foreignRegion.deleteSecurityConfiguration(request -> request.name(prefix + "-0"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(ints = {-1, 0, 201})
|
||||
void sdkRejectsPageSizesOutsideTheDocumentedRange(int maxResults) {
|
||||
try (GlueClient client = client("741648700003", Region.US_EAST_1)) {
|
||||
InvalidInputException failure = assertThrows(InvalidInputException.class,
|
||||
() -> client.getSecurityConfigurations(request -> request.maxResults(maxResults)));
|
||||
assertEquals(400, failure.statusCode());
|
||||
}
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"not-a-token", "-1", "2147483647"})
|
||||
void sdkRejectsMalformedOrOutOfRangeTokens(String token) {
|
||||
try (GlueClient client = client("741648700003", Region.US_EAST_1)) {
|
||||
InvalidInputException failure = assertThrows(InvalidInputException.class,
|
||||
() -> client.getSecurityConfigurations(request -> request.nextToken(token)));
|
||||
assertEquals(400, failure.statusCode());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void sdkReadsAnEmptyPageAtTheMaximumPageSizeWithoutAToken() {
|
||||
try (GlueClient client = client("741648700003", Region.US_EAST_1)) {
|
||||
GetSecurityConfigurationsResponse response = client.getSecurityConfigurations(request -> request.maxResults(200));
|
||||
assertTrue(response.securityConfigurations().isEmpty());
|
||||
assertNull(response.nextToken());
|
||||
}
|
||||
}
|
||||
|
||||
private static GlueClient client(String account, Region region) {
|
||||
return GlueClient.builder().endpointOverride(URI.create("http://localhost:" + RestAssured.port))
|
||||
.httpClientBuilder(UrlConnectionHttpClient.builder()).region(region)
|
||||
.credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create(account, "test")))
|
||||
.build();
|
||||
}
|
||||
}
|
||||
@@ -1365,6 +1365,34 @@ class GlueServiceTest {
|
||||
assertEquals("InvalidInputException", hugeMaxResultsJobs.getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void workflowAndTriggerListsKeepTheirOwnPageSizeLimits() {
|
||||
assertTrue(glueService.listWorkflows(25, null).items().isEmpty());
|
||||
assertTrue(glueService.getTriggers(null, 200, null).items().isEmpty());
|
||||
for (int maxResults : List.of(-1, 0, 26)) {
|
||||
AwsException failure = assertThrows(AwsException.class,
|
||||
() -> glueService.listWorkflows(maxResults, null));
|
||||
assertEquals("InvalidInputException", failure.getErrorCode());
|
||||
assertEquals("MaxResults must be between 1 and 25", failure.getMessage());
|
||||
}
|
||||
for (int maxResults : List.of(-1, 0, 201)) {
|
||||
AwsException failure = assertThrows(AwsException.class,
|
||||
() -> glueService.getTriggers(null, maxResults, null));
|
||||
assertEquals("InvalidInputException", failure.getErrorCode());
|
||||
assertEquals("MaxResults must be between 1 and 200", failure.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void triggerPageSizeIsValidatedBeforeTheDependentJobLookup() {
|
||||
AwsException failure = assertThrows(AwsException.class,
|
||||
() -> glueService.getTriggers("missing", 201, null));
|
||||
assertEquals("InvalidInputException", failure.getErrorCode());
|
||||
assertEquals("MaxResults must be between 1 and 200", failure.getMessage());
|
||||
assertEquals("EntityNotFoundException", assertThrows(AwsException.class,
|
||||
() -> glueService.getTriggers("missing", 200, null)).getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void taggingNonExistentResourceThrows() {
|
||||
String fakeJobArn = "arn:aws:glue:" + REGION + ":" + ACCOUNT_ID + ":job/fake-job";
|
||||
|
||||
Reference in new issue
Block a user