mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(securityhub): page DescribeStandards with MaxResults and NextToken (#9635)
DescribeStandards ignored MaxResults and NextToken and always returned the whole catalogue. It now pages through the shared Pagination helper in catalogue order, returns a NextToken while standards remain, and refuses a MaxResults outside 1 to 100 with InvalidInputException. Closes #7607
This commit is contained in:
1 parent
171f42ce6e
commit
fd2eef7f74
4 files changed
+54
-2
No files matched your search
@@ -198,11 +198,16 @@ public class SecurityHubController {
|
||||
|
||||
@GET
|
||||
@Path("/standards")
|
||||
public Response describeStandards(@Context HttpHeaders headers) {
|
||||
public Response describeStandards(@Context HttpHeaders headers,
|
||||
@QueryParam("MaxResults") String maxResultsValue,
|
||||
@QueryParam("NextToken") String nextToken) {
|
||||
String region = region(headers);
|
||||
Integer maxResults = Pagination.parseMaxResults(maxResultsValue, "InvalidInputException");
|
||||
PaginatedResult<SecurityHubService.Standard> page =
|
||||
securityHubService.standardsPage(maxResults, nextToken);
|
||||
ObjectNode response = objectMapper.createObjectNode();
|
||||
ArrayNode standards = response.putArray("Standards");
|
||||
for (SecurityHubService.Standard standard : SecurityHubService.STANDARDS) {
|
||||
for (SecurityHubService.Standard standard : page.items()) {
|
||||
ObjectNode item = standards.addObject()
|
||||
.put("StandardsArn", securityHubService.standardsArn(region, standard))
|
||||
.put("Name", standard.name())
|
||||
@@ -210,6 +215,9 @@ public class SecurityHubController {
|
||||
.put("EnabledByDefault", standard.enabledByDefault());
|
||||
item.putObject("StandardsManagedBy").put("Company", "AWS").put("Product", "Security Hub");
|
||||
}
|
||||
if (page.nextToken() != null) {
|
||||
response.put("NextToken", page.nextToken());
|
||||
}
|
||||
return Response.ok(response).build();
|
||||
}
|
||||
|
||||
|
||||
@@ -387,6 +387,11 @@ public class SecurityHubService implements Resettable, StoredResourceArns {
|
||||
new Standard("standards/aws-resource-tagging-standard/v/1.0.0",
|
||||
"AWS Resource Tagging Standard v1.0.0", false, false));
|
||||
|
||||
public PaginatedResult<Standard> standardsPage(Integer maxResults, String nextToken) {
|
||||
return Pagination.paginate(STANDARDS, standard -> String.format("%03d", STANDARDS.indexOf(standard)),
|
||||
maxResults, nextToken, 100, 100, "InvalidInputException");
|
||||
}
|
||||
|
||||
public String standardsArn(String region, Standard standard) {
|
||||
return standard.global()
|
||||
? AwsArnUtils.Arn.global(regionResolver.partitionForRegion(region), "securityhub", "", standard.path()).toString()
|
||||
|
||||
+17
@@ -10,6 +10,7 @@ import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.hasSize;
|
||||
import static org.hamcrest.Matchers.notNullValue;
|
||||
import static org.hamcrest.Matchers.nullValue;
|
||||
|
||||
@QuarkusTest
|
||||
class SecurityHubOrganizationIntegrationTest {
|
||||
@@ -17,6 +18,22 @@ class SecurityHubOrganizationIntegrationTest {
|
||||
@Inject
|
||||
OrganizationsService organizationsService;
|
||||
|
||||
@Test
|
||||
void describeStandardsHonoursMaxResultsAndNextToken() {
|
||||
String token = given().header("Authorization", auth("920000000010"))
|
||||
.queryParam("MaxResults", 1).get("/standards").then().statusCode(200)
|
||||
.body("Standards", hasSize(1))
|
||||
.body("NextToken", notNullValue())
|
||||
.extract().path("NextToken");
|
||||
given().header("Authorization", auth("920000000010"))
|
||||
.queryParam("MaxResults", 100).queryParam("NextToken", token).get("/standards").then()
|
||||
.statusCode(200)
|
||||
.body("Standards", hasSize(SecurityHubService.STANDARDS.size() - 1))
|
||||
.body("NextToken", nullValue());
|
||||
given().header("Authorization", auth("920000000010"))
|
||||
.queryParam("MaxResults", 101).get("/standards").then().statusCode(400);
|
||||
}
|
||||
|
||||
@Test
|
||||
void organizationAndConfigurationPolicyLifecycle() {
|
||||
String management = "920000000001";
|
||||
|
||||
@@ -2,6 +2,7 @@ package io.floci.aws.services.securityhub;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.floci.aws.core.common.AwsException;
|
||||
import io.floci.aws.core.common.PaginatedResult;
|
||||
import io.floci.aws.core.common.RegionResolver;
|
||||
import io.floci.aws.core.storage.AccountAwareStorageBackend;
|
||||
import io.floci.aws.core.storage.InMemoryStorage;
|
||||
@@ -10,6 +11,7 @@ import io.floci.aws.services.securityhub.model.SecurityHubState;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
@@ -34,6 +36,26 @@ class SecurityHubServiceTest {
|
||||
mock(OrganizationsService.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void standardsPagesWalkEveryStandardOnce() {
|
||||
List<String> seen = new ArrayList<>();
|
||||
String token = null;
|
||||
do {
|
||||
PaginatedResult<SecurityHubService.Standard> page = service.standardsPage(4, token);
|
||||
assertTrue(page.items().size() <= 4);
|
||||
page.items().forEach(standard -> seen.add(standard.name()));
|
||||
token = page.nextToken();
|
||||
} while (token != null);
|
||||
|
||||
assertEquals(SecurityHubService.STANDARDS.stream().map(SecurityHubService.Standard::name).toList(), seen);
|
||||
}
|
||||
|
||||
@Test
|
||||
void standardsPageRefusesMaxResultsOutsideOneToOneHundred() {
|
||||
assertThrows(AwsException.class, () -> service.standardsPage(0, null));
|
||||
assertThrows(AwsException.class, () -> service.standardsPage(101, null));
|
||||
}
|
||||
|
||||
@Test
|
||||
void enableSecurityHubPersistsConfigurationAndHubTags() throws Exception {
|
||||
service.enableSecurityHub(REGION, objectMapper.readTree("""
|
||||
|
||||
Reference in new issue
Block a user