mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
fix(ec2): implement filters for DescribeVpcEndpointServices (#7261)
- Add support for owner, service-name, service-region, service-type, supported-ip-address-types, tag:key, and tag-key filters - Build service details with all properties for filtering - Validate filter names before iterating services - Return InvalidFilter (HTTP 400) for unknown filter names - Keep requested service names unchanged - Preserve S3 gateway service in China partition - Validate service-name filter values, reject regex metacharacters - Use AWS error message wording for unsupported filter values - Use Ec2Service.requireSupportedFilters for filter name validation - Reuse Ec2Service.wildcardMatches for wildcard matching - Integration tests for all filter types - Cover unknown filter name, '[' and 's3+' values, and refusal after an earlier filter empties the candidate set Closes #6958 Signed-off-by: Matheus Oliveira <matheus.6148@gmail.com>
This commit is contained in:
1 parent
f719d6af56
commit
e42f97df08
3 files changed
+364
-30
No files matched your search
@@ -1953,25 +1953,77 @@ public class Ec2QueryHandler {
|
||||
|
||||
private Response handleDescribeVpcEndpointServices(MultivaluedMap<String, String> p, String region) {
|
||||
List<String> requested = getList(p, "ServiceName");
|
||||
List<Map.Entry<String, List<String>>> filters = getFiltersList(p);
|
||||
List<String> azNames = service.describeAvailabilityZones(region, List.of(), List.of(), null).stream()
|
||||
.map(z -> z.get("zoneName")).toList();
|
||||
|
||||
// Build the full list of service details with all properties for filtering
|
||||
List<ServiceDetail> services = buildServiceDetails(region, requested, azNames);
|
||||
try {
|
||||
// Apply filters
|
||||
services = applyVpcEndpointServiceFilters(services, filters);
|
||||
} catch (AwsException e) {
|
||||
return ec2Error(e.getErrorCode(), e.getMessage(), e.getHttpStatus());
|
||||
}
|
||||
|
||||
XmlBuilder xml = new XmlBuilder()
|
||||
.start("DescribeVpcEndpointServicesResponse", AwsNamespaces.EC2)
|
||||
.elem("requestId", UUID.randomUUID().toString())
|
||||
.start("serviceNameSet");
|
||||
for (ServiceDetail svc : services) {
|
||||
xml.elem("item", svc.serviceName);
|
||||
}
|
||||
xml.end("serviceNameSet").start("serviceDetailSet");
|
||||
for (ServiceDetail svc : services) {
|
||||
xml.start("item")
|
||||
.elem("serviceName", svc.serviceName)
|
||||
.start("serviceType");
|
||||
for (String serviceType : svc.serviceTypes) {
|
||||
xml.start("item").elem("serviceType", serviceType).end("item");
|
||||
}
|
||||
xml.end("serviceType")
|
||||
.start("availabilityZoneSet");
|
||||
for (String az : svc.availabilityZones) xml.elem("item", az);
|
||||
xml.end("availabilityZoneSet")
|
||||
.elem("owner", svc.owner)
|
||||
.elem("acceptanceRequired", svc.acceptanceRequired)
|
||||
.elem("managesVpcEndpoints", svc.managesVpcEndpoints)
|
||||
.end("item");
|
||||
}
|
||||
xml.end("serviceDetailSet").end("DescribeVpcEndpointServicesResponse");
|
||||
return xmlResponse(xml.build());
|
||||
}
|
||||
|
||||
/**
|
||||
* Service detail for VPC endpoint service, used for filtering.
|
||||
*/
|
||||
private record ServiceDetail(
|
||||
String serviceName,
|
||||
List<String> serviceTypes,
|
||||
List<String> availabilityZones,
|
||||
String owner,
|
||||
String acceptanceRequired,
|
||||
String managesVpcEndpoints,
|
||||
String serviceRegion,
|
||||
List<String> supportedIpAddressTypes) {
|
||||
}
|
||||
|
||||
private List<ServiceDetail> buildServiceDetails(String region, List<String> requested, List<String> azNames) {
|
||||
List<ServiceDetail> services = new ArrayList<>();
|
||||
|
||||
// S3 is the one service with both offerings. The gateway keeps com.amazonaws in every
|
||||
// partition; the interface takes the region's prefix, so in China the two are named apart.
|
||||
String s3Gateway = "com.amazonaws." + region + ".s3";
|
||||
String s3Interface = AwsRegionFacts.vpcEndpointServiceName(region, "s3");
|
||||
List<String> fullNames = new ArrayList<>();
|
||||
// An explicit ServiceName filter wins: the emulator supports any
|
||||
// interface service in every AZ, so echo exactly what was asked. CDK's
|
||||
// InterfaceVpcEndpoint queries a specific (often custom) service name
|
||||
// and fails if the response omits it or lists no AZs.
|
||||
|
||||
// Requested service names are echoed verbatim, exactly as AWS does. The emulator
|
||||
// supports any interface service in every AZ, so a requested (often custom) name
|
||||
// must come back unchanged with AZs, otherwise CDK's InterfaceVpcEndpoint fails.
|
||||
List<String> fullNames;
|
||||
if (!requested.isEmpty()) {
|
||||
fullNames.addAll(requested);
|
||||
fullNames = new ArrayList<>(requested);
|
||||
} else {
|
||||
fullNames = new ArrayList<>();
|
||||
for (String name : INTERFACE_ENDPOINT_SERVICES) {
|
||||
fullNames.add(AwsRegionFacts.vpcEndpointServiceName(region, name));
|
||||
}
|
||||
@@ -1980,13 +2032,8 @@ public class Ec2QueryHandler {
|
||||
fullNames.add(s3Interface);
|
||||
}
|
||||
}
|
||||
|
||||
for (String full : fullNames) {
|
||||
xml.elem("item", full);
|
||||
}
|
||||
xml.end("serviceNameSet").start("serviceDetailSet");
|
||||
for (String full : fullNames) {
|
||||
// Where both S3 offerings share a name, AWS reports both types on that one
|
||||
// service detail. Everything else is Interface.
|
||||
List<String> serviceTypes;
|
||||
if (full.equals(s3Interface) && !s3Interface.equals(s3Gateway)) {
|
||||
serviceTypes = List.of("Interface");
|
||||
@@ -1997,23 +2044,97 @@ public class Ec2QueryHandler {
|
||||
} else {
|
||||
serviceTypes = List.of("Interface");
|
||||
}
|
||||
xml.start("item")
|
||||
.elem("serviceName", full)
|
||||
.start("serviceType");
|
||||
for (String serviceType : serviceTypes) {
|
||||
xml.start("item").elem("serviceType", serviceType).end("item");
|
||||
}
|
||||
xml.end("serviceType")
|
||||
.start("availabilityZoneSet");
|
||||
for (String az : azNames) xml.elem("item", az);
|
||||
xml.end("availabilityZoneSet")
|
||||
.elem("owner", "amazon")
|
||||
.elem("acceptanceRequired", "false")
|
||||
.elem("managesVpcEndpoints", "false")
|
||||
.end("item");
|
||||
services.add(new ServiceDetail(
|
||||
full,
|
||||
serviceTypes,
|
||||
azNames,
|
||||
"amazon",
|
||||
"false",
|
||||
"false",
|
||||
region,
|
||||
List.of("ipv4", "ipv6")));
|
||||
}
|
||||
xml.end("serviceDetailSet").end("DescribeVpcEndpointServicesResponse");
|
||||
return xmlResponse(xml.build());
|
||||
|
||||
return services;
|
||||
}
|
||||
|
||||
private List<ServiceDetail> applyVpcEndpointServiceFilters(List<ServiceDetail> services,
|
||||
List<Map.Entry<String, List<String>>> filters) {
|
||||
if (filters == null || filters.isEmpty()) {
|
||||
return services;
|
||||
}
|
||||
|
||||
// Validate filter names before iterating services (AWS returns InvalidFilter for unknown names)
|
||||
Ec2Service.requireSupportedFilters(
|
||||
filters.stream().map(Map.Entry::getKey).toList(),
|
||||
Set.of("owner", "service-name", "service-region", "service-type",
|
||||
"supported-ip-address-types", "tag-key"),
|
||||
"InvalidFilter");
|
||||
|
||||
// Validate service-name filter values: AWS rejects values containing regex
|
||||
// metacharacters other than the supported * and ? wildcards.
|
||||
for (Map.Entry<String, List<String>> filter : filters) {
|
||||
if ("service-name".equals(filter.getKey())) {
|
||||
for (String value : filter.getValue()) {
|
||||
if (!isValidServiceNameFilterValue(value)) {
|
||||
throw new AwsException("InvalidFilter",
|
||||
"The filter value " + value + " contains unsupported characters", 400);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return services.stream()
|
||||
.filter(svc -> {
|
||||
for (Map.Entry<String, List<String>> filter : filters) {
|
||||
String name = filter.getKey();
|
||||
List<String> values = filter.getValue();
|
||||
if (values.isEmpty()) {
|
||||
continue; // Empty filter matches everything
|
||||
}
|
||||
if (!matchesFilter(svc, name, values)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
})
|
||||
.toList();
|
||||
}
|
||||
|
||||
private boolean matchesFilter(ServiceDetail svc, String filterName, List<String> values) {
|
||||
return switch (filterName) {
|
||||
case "owner" -> values.contains(svc.owner);
|
||||
case "service-name" -> values.stream().anyMatch(v -> Ec2Service.wildcardMatches(v, svc.serviceName));
|
||||
case "service-region" -> values.contains(svc.serviceRegion);
|
||||
case "service-type" -> svc.serviceTypes.stream().anyMatch(t -> values.contains(t));
|
||||
case "supported-ip-address-types" -> svc.supportedIpAddressTypes.stream()
|
||||
.anyMatch(t -> values.contains(t));
|
||||
case String n when n.startsWith("tag:") -> {
|
||||
// VPC endpoint services don't have user tags, so no tag filters match
|
||||
yield false;
|
||||
}
|
||||
case "tag-key" -> false; // No tags on these services
|
||||
default -> throw new AwsException("InvalidFilter",
|
||||
"The filter '" + filterName + "' is invalid.", 400);
|
||||
};
|
||||
}
|
||||
|
||||
private boolean isValidServiceNameFilterValue(String value) {
|
||||
if (value == null || value.isEmpty()) {
|
||||
return true;
|
||||
}
|
||||
// AWS supports only * and ? as wildcards in service-name filters. Values
|
||||
// containing other regex metacharacters are rejected with InvalidFilter.
|
||||
for (int i = 0; i < value.length(); i++) {
|
||||
char c = value.charAt(i);
|
||||
if (c == '*' || c == '?') {
|
||||
continue;
|
||||
}
|
||||
if (!Character.isLetterOrDigit(c) && c != '.' && c != '-') {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// ─── Flow Logs ────────────────────────────────────────────────────────────
|
||||
|
||||
@@ -6426,12 +6426,16 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
|
||||
}
|
||||
|
||||
static void requireSupportedFilters(Collection<String> filterNames, Set<String> supportedFilters) {
|
||||
requireSupportedFilters(filterNames, supportedFilters, "InvalidParameterValue");
|
||||
}
|
||||
|
||||
static void requireSupportedFilters(Collection<String> filterNames, Set<String> supportedFilters, String errorCode) {
|
||||
filterNames.stream()
|
||||
.filter(name -> !supportedFilters.contains(name)
|
||||
&& !(supportedFilters.contains("tag-key") && name.startsWith("tag:")))
|
||||
.findFirst()
|
||||
.ifPresent(name -> {
|
||||
throw new AwsException("InvalidParameterValue", "The filter '" + name + "' is invalid", 400);
|
||||
throw new AwsException(errorCode, "The filter '" + name + "' is invalid", 400);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -10167,7 +10171,7 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
|
||||
* caller-supplied pattern of many stars made the regex backtrack for seconds, and its dot
|
||||
* skipped line terminators that a tag value may hold.
|
||||
*/
|
||||
private static boolean wildcardMatches(String pattern, String value) {
|
||||
public static boolean wildcardMatches(String pattern, String value) {
|
||||
return IamPolicyEvaluator.caseSensitiveGlobMatches(pattern, value);
|
||||
}
|
||||
|
||||
|
||||
@@ -3089,6 +3089,215 @@ class Ec2IntegrationTest {
|
||||
.body(containsString("us-east-1a"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(52)
|
||||
void describeVpcEndpointServicesFilterByServiceTypeGateway() {
|
||||
given()
|
||||
.formParam("Action", "DescribeVpcEndpointServices")
|
||||
.formParam("Filter.1.Name", "service-type")
|
||||
.formParam("Filter.1.Value.1", "Gateway")
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("DescribeVpcEndpointServicesResponse.serviceDetailSet.item.size()", greaterThanOrEqualTo(1))
|
||||
// S3 has both Gateway and Interface, so it's included but serviceType has both
|
||||
.body("DescribeVpcEndpointServicesResponse.serviceDetailSet.item.serviceType.item.serviceType",
|
||||
hasItem(equalTo("Gateway")));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(52)
|
||||
void describeVpcEndpointServicesFilterByServiceTypeInterface() {
|
||||
given()
|
||||
.formParam("Action", "DescribeVpcEndpointServices")
|
||||
.formParam("Filter.1.Name", "service-type")
|
||||
.formParam("Filter.1.Value.1", "Interface")
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("DescribeVpcEndpointServicesResponse.serviceDetailSet.item.size()", greaterThanOrEqualTo(1))
|
||||
.body("DescribeVpcEndpointServicesResponse.serviceDetailSet.item.serviceType.item.serviceType",
|
||||
hasItem(equalTo("Interface")));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(52)
|
||||
void describeVpcEndpointServicesFilterByServiceName() {
|
||||
given()
|
||||
.formParam("Action", "DescribeVpcEndpointServices")
|
||||
.formParam("Filter.1.Name", "service-name")
|
||||
.formParam("Filter.1.Value.1", "com.amazonaws.us-east-1.s3")
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("DescribeVpcEndpointServicesResponse.serviceDetailSet.item.size()", equalTo(1))
|
||||
.body("DescribeVpcEndpointServicesResponse.serviceDetailSet.item.serviceName", equalTo("com.amazonaws.us-east-1.s3"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(52)
|
||||
void describeVpcEndpointServicesFilterByServiceNameWildcard() {
|
||||
given()
|
||||
.formParam("Action", "DescribeVpcEndpointServices")
|
||||
.formParam("Filter.1.Name", "service-name")
|
||||
.formParam("Filter.1.Value.1", "com.amazonaws.us-east-1.*")
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("DescribeVpcEndpointServicesResponse.serviceDetailSet.item.size()", greaterThanOrEqualTo(1))
|
||||
.body("DescribeVpcEndpointServicesResponse.serviceDetailSet.item.serviceName", hasItem("com.amazonaws.us-east-1.s3"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(52)
|
||||
void describeVpcEndpointServicesFilterByOwner() {
|
||||
given()
|
||||
.formParam("Action", "DescribeVpcEndpointServices")
|
||||
.formParam("Filter.1.Name", "owner")
|
||||
.formParam("Filter.1.Value.1", "amazon")
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("DescribeVpcEndpointServicesResponse.serviceDetailSet.item.size()", greaterThanOrEqualTo(1))
|
||||
.body("DescribeVpcEndpointServicesResponse.serviceDetailSet.item.owner", everyItem(equalTo("amazon")));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(52)
|
||||
void describeVpcEndpointServicesFilterByServiceRegion() {
|
||||
given()
|
||||
.formParam("Action", "DescribeVpcEndpointServices")
|
||||
.formParam("Filter.1.Name", "service-region")
|
||||
.formParam("Filter.1.Value.1", "us-east-1")
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("DescribeVpcEndpointServicesResponse.serviceDetailSet.item.size()", greaterThanOrEqualTo(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(52)
|
||||
void describeVpcEndpointServicesFilterBySupportedIpAddressTypes() {
|
||||
given()
|
||||
.formParam("Action", "DescribeVpcEndpointServices")
|
||||
.formParam("Filter.1.Name", "supported-ip-address-types")
|
||||
.formParam("Filter.1.Value.1", "ipv4")
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("DescribeVpcEndpointServicesResponse.serviceDetailSet.item.size()", greaterThanOrEqualTo(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(52)
|
||||
void describeVpcEndpointServicesFilterByTagKeyReturnsEmpty() {
|
||||
given()
|
||||
.formParam("Action", "DescribeVpcEndpointServices")
|
||||
.formParam("Filter.1.Name", "tag-key")
|
||||
.formParam("Filter.1.Value.1", "any")
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("DescribeVpcEndpointServicesResponse.serviceDetailSet.item.size()", equalTo(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(51)
|
||||
void describeVpcEndpointServicesRejectsUnknownFilterName() {
|
||||
given()
|
||||
.formParam("Action", "DescribeVpcEndpointServices")
|
||||
.formParam("Filter.1.Name", "invalid-filter")
|
||||
.formParam("Filter.1.Value.1", "any")
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body("Response.Errors.Error.Code", equalTo("InvalidFilter"))
|
||||
.body("Response.Errors.Error.Message", containsString("invalid-filter"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(52)
|
||||
void describeVpcEndpointServicesRejectsUnsupportedBracket() {
|
||||
given()
|
||||
.formParam("Action", "DescribeVpcEndpointServices")
|
||||
.formParam("Filter.1.Name", "service-name")
|
||||
.formParam("Filter.1.Value.1", "[")
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body("Response.Errors.Error.Code", equalTo("InvalidFilter"))
|
||||
.body("Response.Errors.Error.Message", containsString("contains unsupported characters"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(53)
|
||||
void describeVpcEndpointServicesRejectsUnsupportedPlus() {
|
||||
given()
|
||||
.formParam("Action", "DescribeVpcEndpointServices")
|
||||
.formParam("Filter.1.Name", "service-name")
|
||||
.formParam("Filter.1.Value.1", "com.amazonaws.us-east-1.s3+")
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body("Response.Errors.Error.Code", equalTo("InvalidFilter"))
|
||||
.body("Response.Errors.Error.Message", containsString("contains unsupported characters"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(54)
|
||||
void describeVpcEndpointServicesRejectsUnsupportedAfterEmptyFilter() {
|
||||
given()
|
||||
.formParam("Action", "DescribeVpcEndpointServices")
|
||||
.formParam("Filter.1.Name", "service-name")
|
||||
.formParam("Filter.1.Value.1", "nonexistent-service-xyz")
|
||||
.formParam("Filter.2.Name", "service-name")
|
||||
.formParam("Filter.2.Value.1", "[")
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body("Response.Errors.Error.Code", equalTo("InvalidFilter"))
|
||||
.body("Response.Errors.Error.Message", containsString("contains unsupported characters"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(55)
|
||||
void describeVpcEndpointServicesFilterByTagValueReturnsEmpty() {
|
||||
given()
|
||||
.formParam("Action", "DescribeVpcEndpointServices")
|
||||
.formParam("Filter.1.Name", "tag:Environment")
|
||||
.formParam("Filter.1.Value.1", "prod")
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("DescribeVpcEndpointServicesResponse.serviceDetailSet.item.size()", equalTo(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(52)
|
||||
void describeInternetGateways() {
|
||||
|
||||
Reference in new issue
Block a user