mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
feat(elbv2): support path-pattern regex conditions (#9387)
Listener rules now accept PathPatternConfig.RegexValues. CreateRule, ModifyRule, DescribeRules and CloudFormation keep the expressions, and both admission paths validate them before a mutation: glob and regex values can't be mixed, an expression must compile under RE2/J and avoid the syntax the ALB guide leaves out, and the condition and per-rule evaluation limits apply. The three refusals for mixed values, invalid syntax and too many values use AWS's recorded ValidationError messages. The data plane compiles a rule chain's expressions once and matches the request path without its query string, leaving glob matching as it was. Co-authored-by: Tyler Parmelee <7409652+tparmelee@users.noreply.github.com> Co-authored-by: Andre Kurait <AndreKurait@gmail.com>
This commit is contained in:
11 files changed
+849
-14
No files matched your search
+5
@@ -11,6 +11,7 @@ import io.floci.aws.services.cloudformation.model.StackResource;
|
||||
import io.floci.aws.services.elbv2.ElbV2HealthChecker;
|
||||
import io.floci.aws.services.elbv2.ElbV2Service;
|
||||
import io.floci.aws.services.elbv2.ElbV2Service.ListenerCertificate;
|
||||
import io.floci.aws.services.elbv2.PathPatternRegexValues;
|
||||
import io.floci.aws.services.elbv2.model.Action;
|
||||
import io.floci.aws.services.elbv2.model.AvailabilityZone;
|
||||
import io.floci.aws.services.elbv2.model.Listener;
|
||||
@@ -1014,6 +1015,9 @@ public class ElbV2CfnProvisioner implements CfnResourceProvisioner {
|
||||
if (pathCfg.path("Values").isArray()) {
|
||||
condition.setPathPatternValues(toStringList(pathCfg.path("Values")));
|
||||
}
|
||||
if (pathCfg.path("RegexValues").isArray()) {
|
||||
condition.setPathPatternRegexValues(toStringList(pathCfg.path("RegexValues")));
|
||||
}
|
||||
JsonNode hostCfg = item.path("HostHeaderConfig");
|
||||
if (hostCfg.path("Values").isArray()) {
|
||||
condition.setHostHeaderValues(toStringList(hostCfg.path("Values")));
|
||||
@@ -1046,6 +1050,7 @@ public class ElbV2CfnProvisioner implements CfnResourceProvisioner {
|
||||
}
|
||||
result.add(condition);
|
||||
}
|
||||
PathPatternRegexValues.validateConditions(result);
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
@@ -5,6 +5,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.floci.aws.config.EmulatorConfig;
|
||||
import io.floci.aws.config.TlsProxyServer;
|
||||
import io.floci.aws.core.common.AwsArnUtils;
|
||||
import io.floci.aws.core.common.AwsException;
|
||||
import io.floci.aws.core.common.CertificateReferenceResolver;
|
||||
import io.floci.aws.core.common.CertificateReferenceResolver.CertificateMaterial;
|
||||
import io.floci.aws.core.common.Pem;
|
||||
@@ -14,6 +15,7 @@ import io.floci.aws.core.common.WebSocketUpgrades;
|
||||
import io.floci.aws.core.common.tls.ReloadingKeyManager;
|
||||
import io.floci.aws.services.ec2.Ec2InstancePorts;
|
||||
import io.floci.aws.services.ec2.Ec2Service;
|
||||
import io.floci.aws.services.elbv2.PathPatternRegexValues.Compiled;
|
||||
import io.floci.aws.services.elbv2.model.Action;
|
||||
import io.floci.aws.services.elbv2.model.Listener;
|
||||
import io.floci.aws.services.elbv2.model.LoadBalancer;
|
||||
@@ -66,7 +68,9 @@ import java.security.cert.X509Certificate;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Base64;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.IdentityHashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
@@ -1779,6 +1783,22 @@ public class ElbV2DataPlane {
|
||||
return last;
|
||||
}
|
||||
|
||||
static List<Compiled> compilePathRegexes(List<String> regexValues) {
|
||||
List<Compiled> compiled = new ArrayList<>();
|
||||
for (String regex : regexValues) {
|
||||
try {
|
||||
compiled.add(PathPatternRegexValues.compile(regex));
|
||||
} catch (AwsException e) {
|
||||
LOG.warnv("Ignoring path-pattern regex value that does not compile: {0}", regex);
|
||||
}
|
||||
}
|
||||
return List.copyOf(compiled);
|
||||
}
|
||||
|
||||
static boolean pathRegexMatches(List<Compiled> patterns, String path) {
|
||||
return path != null && patterns.stream().anyMatch(p -> p.matches(path));
|
||||
}
|
||||
|
||||
private static boolean globMatches(String pattern, String text) {
|
||||
if (text == null) {
|
||||
return false;
|
||||
@@ -1800,6 +1820,8 @@ public class ElbV2DataPlane {
|
||||
final Rule rule;
|
||||
final Action action;
|
||||
final List<String> hostHeaderPatterns;
|
||||
private final Map<RuleCondition, List<Compiled>> pathRegexes =
|
||||
Collections.synchronizedMap(new IdentityHashMap<>());
|
||||
|
||||
CompiledRule(Rule rule) {
|
||||
this.rule = rule;
|
||||
@@ -1833,6 +1855,17 @@ public class ElbV2DataPlane {
|
||||
return hostHeaderPatterns.stream().anyMatch(p -> globMatches(p, host));
|
||||
}
|
||||
|
||||
/**
|
||||
* ALB regex path conditions: the condition holds when any expression matches the whole
|
||||
* request path (not its query string). Expressions are compiled once per rule chain; one
|
||||
* that does not compile never matches.
|
||||
*/
|
||||
private boolean matchesPathRegex(RuleCondition condition, String path) {
|
||||
List<Compiled> patterns = pathRegexes.computeIfAbsent(condition,
|
||||
c -> compilePathRegexes(c.getPathPatternRegexValues()));
|
||||
return pathRegexMatches(patterns, path);
|
||||
}
|
||||
|
||||
boolean matches(HttpServerRequest req) {
|
||||
if (rule.isDefault()) {
|
||||
return true;
|
||||
@@ -1863,10 +1896,13 @@ public class ElbV2DataPlane {
|
||||
yield patterns.stream().anyMatch(p -> globMatches(p, effectiveHost));
|
||||
}
|
||||
case "path-pattern" -> {
|
||||
String path = req.path();
|
||||
if (!condition.getPathPatternRegexValues().isEmpty()) {
|
||||
yield matchesPathRegex(condition, path);
|
||||
}
|
||||
List<String> patterns = condition.getPathPatternValues().isEmpty()
|
||||
? condition.getValues()
|
||||
: condition.getPathPatternValues();
|
||||
String path = req.path();
|
||||
yield patterns.stream().anyMatch(p -> globMatches(p, path));
|
||||
}
|
||||
case "http-header" -> {
|
||||
|
||||
@@ -1051,10 +1051,23 @@ public class ElbV2QueryHandler {
|
||||
for (String v : c.getHostHeaderValues()) xml.start("member").raw(v).end("member");
|
||||
xml.end("Values").end("HostHeaderConfig");
|
||||
}
|
||||
if (!c.getPathPatternValues().isEmpty()) {
|
||||
xml.start("PathPatternConfig").start("Values");
|
||||
for (String v : c.getPathPatternValues()) xml.start("member").raw(v).end("member");
|
||||
xml.end("Values").end("PathPatternConfig");
|
||||
if (!c.getPathPatternValues().isEmpty() || !c.getPathPatternRegexValues().isEmpty()) {
|
||||
xml.start("PathPatternConfig");
|
||||
if (!c.getPathPatternValues().isEmpty()) {
|
||||
xml.start("Values");
|
||||
for (String value : c.getPathPatternValues()) {
|
||||
xml.start("member").raw(value).end("member");
|
||||
}
|
||||
xml.end("Values");
|
||||
}
|
||||
if (!c.getPathPatternRegexValues().isEmpty()) {
|
||||
xml.start("RegexValues");
|
||||
for (String value : c.getPathPatternRegexValues()) {
|
||||
xml.elem("member", value);
|
||||
}
|
||||
xml.end("RegexValues");
|
||||
}
|
||||
xml.end("PathPatternConfig");
|
||||
}
|
||||
if (c.getHttpHeaderName() != null) {
|
||||
xml.start("HttpHeaderConfig")
|
||||
@@ -1361,6 +1374,8 @@ public class ElbV2QueryHandler {
|
||||
v++;
|
||||
}
|
||||
c.setValues(legacyValues);
|
||||
String pathConfig = "Conditions.member." + i + ".PathPatternConfig.";
|
||||
c.setPathPatternRegexValues(readMembers(p, pathConfig + "RegexValues.member."));
|
||||
|
||||
switch (field) {
|
||||
case "host-header" -> {
|
||||
@@ -1376,16 +1391,12 @@ public class ElbV2QueryHandler {
|
||||
else c.setHostHeaderValues(new ArrayList<>(legacyValues));
|
||||
}
|
||||
case "path-pattern" -> {
|
||||
List<String> vals = new ArrayList<>();
|
||||
int j = 1;
|
||||
while (true) {
|
||||
String val = p.getFirst("Conditions.member." + i + ".PathPatternConfig.Values.member." + j);
|
||||
if (val == null) break;
|
||||
vals.add(val);
|
||||
j++;
|
||||
List<String> vals = readMembers(p, pathConfig + "Values.member.");
|
||||
if (!vals.isEmpty()) {
|
||||
c.setPathPatternValues(vals);
|
||||
} else {
|
||||
c.setPathPatternValues(new ArrayList<>(legacyValues));
|
||||
}
|
||||
if (!vals.isEmpty()) c.setPathPatternValues(vals);
|
||||
else c.setPathPatternValues(new ArrayList<>(legacyValues));
|
||||
}
|
||||
case "http-header" -> {
|
||||
c.setHttpHeaderName(p.getFirst("Conditions.member." + i + ".HttpHeaderConfig.HttpHeaderName"));
|
||||
@@ -1440,9 +1451,21 @@ public class ElbV2QueryHandler {
|
||||
result.add(c);
|
||||
i++;
|
||||
}
|
||||
PathPatternRegexValues.validateConditions(result);
|
||||
return result;
|
||||
}
|
||||
|
||||
private static List<String> readMembers(MultivaluedMap<String, String> parameters, String prefix) {
|
||||
List<String> values = new ArrayList<>();
|
||||
for (int i = 1; ; i++) {
|
||||
String value = parameters.getFirst(prefix + i);
|
||||
if (value == null) {
|
||||
return values;
|
||||
}
|
||||
values.add(value);
|
||||
}
|
||||
}
|
||||
|
||||
private List<TargetDescription> parseTargets(MultivaluedMap<String, String> p) {
|
||||
List<TargetDescription> result = new ArrayList<>();
|
||||
int i = 1;
|
||||
|
||||
@@ -0,0 +1,129 @@
|
||||
package io.floci.aws.services.elbv2;
|
||||
|
||||
import com.google.re2j.Pattern;
|
||||
import com.google.re2j.PatternSyntaxException;
|
||||
import io.floci.aws.core.common.AwsException;
|
||||
import io.floci.aws.services.elbv2.model.RuleCondition;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/** Shared admission and linear-time matching for path-pattern regex conditions. */
|
||||
public final class PathPatternRegexValues {
|
||||
|
||||
private static final int MAX_CONDITION_EVALUATIONS = 3;
|
||||
private static final int MAX_RULE_EVALUATIONS = 5;
|
||||
|
||||
private PathPatternRegexValues() {
|
||||
}
|
||||
|
||||
/** Validates regex-bearing rules at both public admission paths before mutation. */
|
||||
public static void validateConditions(List<RuleCondition> conditions) {
|
||||
boolean hasRegex = conditions.stream().anyMatch(c -> !c.getPathPatternRegexValues().isEmpty());
|
||||
if (!hasRegex) {
|
||||
return;
|
||||
}
|
||||
int evaluations = 0;
|
||||
for (RuleCondition condition : conditions) {
|
||||
List<String> regexValues = condition.getPathPatternRegexValues();
|
||||
if (!regexValues.isEmpty() && !"path-pattern".equals(condition.getField())) {
|
||||
throw new AwsException("ValidationError",
|
||||
"PathPatternConfig.RegexValues requires the path-pattern field.", 400);
|
||||
}
|
||||
int conditionEvaluations = matchEvaluations(condition);
|
||||
if (conditionEvaluations > MAX_CONDITION_EVALUATIONS) {
|
||||
throw new AwsException("ValidationError",
|
||||
"A condition can have at most three match evaluations.", 400);
|
||||
}
|
||||
evaluations += conditionEvaluations;
|
||||
if (evaluations > MAX_RULE_EVALUATIONS) {
|
||||
throw new AwsException("ValidationError",
|
||||
"A rule can only have '5' condition values and regex values", 400);
|
||||
}
|
||||
}
|
||||
for (RuleCondition condition : conditions) {
|
||||
if (!condition.getPathPatternRegexValues().isEmpty()) {
|
||||
validate(condition.getPathPatternRegexValues(),
|
||||
!condition.getValues().isEmpty() || !condition.getPathPatternValues().isEmpty());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static int matchEvaluations(RuleCondition condition) {
|
||||
return switch (condition.getField()) {
|
||||
case "path-pattern" -> condition.getPathPatternRegexValues().isEmpty()
|
||||
? valueCount(condition.getPathPatternValues(), condition.getValues())
|
||||
: condition.getPathPatternRegexValues().size();
|
||||
case "host-header" -> valueCount(condition.getHostHeaderValues(), condition.getValues());
|
||||
case "http-header" -> condition.getHttpHeaderValues().size();
|
||||
case "http-request-method" -> condition.getHttpMethodValues().size();
|
||||
case "source-ip" -> condition.getSourceIpValues().size();
|
||||
case "query-string" -> condition.getQueryStringValues().size();
|
||||
case null, default -> condition.getValues().size();
|
||||
};
|
||||
}
|
||||
|
||||
private static int valueCount(List<String> configured, List<String> legacy) {
|
||||
return configured.isEmpty() ? legacy.size() : configured.size();
|
||||
}
|
||||
|
||||
public static void validate(List<String> regexValues, boolean hasGlobValues) {
|
||||
if (hasGlobValues) {
|
||||
throw new AwsException("ValidationError",
|
||||
"You must provide exactly one of the following: ['Values' 'RegexValues']"
|
||||
+ " for config of type 'PathPatternConfig'", 400);
|
||||
}
|
||||
if (regexValues.size() > MAX_CONDITION_EVALUATIONS) {
|
||||
throw new AwsException("ValidationError",
|
||||
"A condition can have at most three match evaluations.", 400);
|
||||
}
|
||||
for (String regex : regexValues) {
|
||||
compile(regex);
|
||||
}
|
||||
}
|
||||
|
||||
static Compiled compile(String regex) {
|
||||
if (regex.isEmpty() || regex.length() > 128) {
|
||||
throw new AwsException("ValidationError",
|
||||
"Each path-pattern regex value must be between 1 and 128 characters.", 400);
|
||||
}
|
||||
try {
|
||||
if (hasUnicodeClass(regex)) {
|
||||
throw new PatternSyntaxException("Unicode character classes are unsupported", regex);
|
||||
}
|
||||
return new Compiled(Pattern.compile(regex));
|
||||
} catch (PatternSyntaxException e) {
|
||||
throw new AwsException("ValidationError",
|
||||
"One or more provided condition regex values are not valid", 400);
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean hasUnicodeClass(String regex) {
|
||||
boolean quoted = false;
|
||||
for (int i = 0; i + 1 < regex.length(); i++) {
|
||||
if (regex.charAt(i) != '\\') {
|
||||
continue;
|
||||
}
|
||||
char escaped = regex.charAt(i + 1);
|
||||
if (quoted) {
|
||||
if (escaped == 'E') {
|
||||
quoted = false;
|
||||
i++;
|
||||
}
|
||||
} else {
|
||||
i++;
|
||||
if (escaped == 'Q') {
|
||||
quoted = true;
|
||||
} else if (escaped == 'p' || escaped == 'P') {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
record Compiled(Pattern pattern) {
|
||||
boolean matches(String path) {
|
||||
return pattern.matcher(path).matches();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -16,6 +16,7 @@ public class RuleCondition {
|
||||
// typed configs
|
||||
private List<String> hostHeaderValues = new ArrayList<>();
|
||||
private List<String> pathPatternValues = new ArrayList<>();
|
||||
private List<String> pathPatternRegexValues = new ArrayList<>();
|
||||
private String httpHeaderName;
|
||||
private List<String> httpHeaderValues = new ArrayList<>();
|
||||
private List<String> httpMethodValues = new ArrayList<>();
|
||||
@@ -36,6 +37,12 @@ public class RuleCondition {
|
||||
public List<String> getPathPatternValues() { return pathPatternValues; }
|
||||
public void setPathPatternValues(List<String> pathPatternValues) { this.pathPatternValues = pathPatternValues; }
|
||||
|
||||
/** {@code PathPatternConfig.RegexValues}: regular expressions matched against the whole request path. */
|
||||
public List<String> getPathPatternRegexValues() { return pathPatternRegexValues; }
|
||||
public void setPathPatternRegexValues(List<String> pathPatternRegexValues) {
|
||||
this.pathPatternRegexValues = pathPatternRegexValues != null ? pathPatternRegexValues : new ArrayList<>();
|
||||
}
|
||||
|
||||
public String getHttpHeaderName() { return httpHeaderName; }
|
||||
public void setHttpHeaderName(String httpHeaderName) { this.httpHeaderName = httpHeaderName; }
|
||||
|
||||
|
||||
Reference in new issue
Block a user