diff --git a/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2QueryHandler.java b/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2QueryHandler.java index 48d547e8f8..907eb7f957 100644 --- a/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2QueryHandler.java +++ b/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2QueryHandler.java @@ -1953,25 +1953,77 @@ public class Ec2QueryHandler { private Response handleDescribeVpcEndpointServices(MultivaluedMap p, String region) { List requested = getList(p, "ServiceName"); + List>> filters = getFiltersList(p); List 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 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 serviceTypes, + List availabilityZones, + String owner, + String acceptanceRequired, + String managesVpcEndpoints, + String serviceRegion, + List supportedIpAddressTypes) { + } + + private List buildServiceDetails(String region, List requested, List azNames) { + List 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 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 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 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 applyVpcEndpointServiceFilters(List services, + List>> 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> 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> filter : filters) { + String name = filter.getKey(); + List 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 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 ──────────────────────────────────────────────────────────── diff --git a/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2Service.java b/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2Service.java index 3a8b93d23f..584caac9a7 100644 --- a/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2Service.java +++ b/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2Service.java @@ -6426,12 +6426,16 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab } static void requireSupportedFilters(Collection filterNames, Set supportedFilters) { + requireSupportedFilters(filterNames, supportedFilters, "InvalidParameterValue"); + } + + static void requireSupportedFilters(Collection filterNames, Set 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); } diff --git a/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2IntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2IntegrationTest.java index 2d51460fc8..b33851e9d9 100644 --- a/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2IntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2IntegrationTest.java @@ -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() {