mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
fix(cloudfront): read policy Name and Comment from the config root only (#7252)
* fix(cloudfront): read policy Name and Comment from the config root only Cache and origin request policy configs nest <Name> elements (cookie, header and query-string items). A flat first-match lookup stored the first item name as the policy name when the SDK serialised those blocks before the top-level Name. * fix(cloudfront): keep policy Name and Comment text as sent The direct-child lookup for cache, origin-request and response-headers policy Name and Comment read XmlElement.text(), which the element tree trims. A Comment of " note " came back as "note". XmlElement now also carries the untrimmed text as rawText(), which directChildText uses; text() stays trimmed for the S3 and CloudFront callers that match it against enum values. * test(cloudfront): cover padded policy Name and newline-padded Comment
This commit is contained in:
1 parent
601b4d132d
commit
5680ec4b8f
3 files changed
+120
-9
No files matched your search
@@ -497,14 +497,19 @@ public final class XmlParser {
|
||||
|| event == XMLStreamConstants.CDATA) {
|
||||
text.append(r.getText());
|
||||
} else if (event == XMLStreamConstants.END_ELEMENT) {
|
||||
return new XmlElement(name, text.toString().trim(), List.copyOf(children));
|
||||
String raw = text.toString();
|
||||
return new XmlElement(name, raw.trim(), List.copyOf(children), raw);
|
||||
}
|
||||
}
|
||||
throw new XMLStreamException("Unexpected end of XML while reading " + name);
|
||||
}
|
||||
|
||||
/** A namespace-agnostic XML element used by {@link #extractElementTree(String, String)}. */
|
||||
public record XmlElement(String name, String text, List<XmlElement> children) {
|
||||
/**
|
||||
* A namespace-agnostic XML element used by {@link #extractElementTree(String, String)}.
|
||||
* {@code text} is trimmed for enum and number fields; {@code rawText} keeps a free-form
|
||||
* value such as a name or comment exactly as the client sent it.
|
||||
*/
|
||||
public record XmlElement(String name, String text, List<XmlElement> children, String rawText) {
|
||||
public XmlElement child(String childName) {
|
||||
return children.stream()
|
||||
.filter(child -> childName.equals(child.name()))
|
||||
|
||||
+18
-6
@@ -3529,26 +3529,38 @@ public class CloudFrontController {
|
||||
return inv;
|
||||
}
|
||||
|
||||
/**
|
||||
* Text of a direct child of the config root. Policy configs nest other {@code Name} elements
|
||||
* (cookie, header and query-string items) that a flat first-match lookup would pick up.
|
||||
*/
|
||||
private static String directChildText(XmlParser.XmlElement root, String name) {
|
||||
XmlParser.XmlElement child = root == null ? null : root.child(name);
|
||||
return child == null ? null : child.rawText();
|
||||
}
|
||||
|
||||
private CachePolicy parseCachePolicy(String body) {
|
||||
CachePolicy policy = new CachePolicy();
|
||||
policy.setName(XmlParser.extractFirst(body, "Name", null));
|
||||
policy.setComment(XmlParser.extractFirst(body, "Comment", null));
|
||||
XmlParser.XmlElement root = XmlParser.parseDocumentElementTree(body);
|
||||
policy.setName(directChildText(root, "Name"));
|
||||
policy.setComment(directChildText(root, "Comment"));
|
||||
policy.setConfig(CloudFrontPolicyConfigCodec.parseCachePolicy(body));
|
||||
return policy;
|
||||
}
|
||||
|
||||
private OriginRequestPolicy parseOriginRequestPolicy(String body) {
|
||||
OriginRequestPolicy policy = new OriginRequestPolicy();
|
||||
policy.setName(XmlParser.extractFirst(body, "Name", null));
|
||||
policy.setComment(XmlParser.extractFirst(body, "Comment", null));
|
||||
XmlParser.XmlElement root = XmlParser.parseDocumentElementTree(body);
|
||||
policy.setName(directChildText(root, "Name"));
|
||||
policy.setComment(directChildText(root, "Comment"));
|
||||
policy.setConfig(CloudFrontPolicyConfigCodec.parseOriginRequestPolicy(body));
|
||||
return policy;
|
||||
}
|
||||
|
||||
private ResponseHeadersPolicy parseResponseHeadersPolicy(String body) {
|
||||
ResponseHeadersPolicy policy = new ResponseHeadersPolicy();
|
||||
policy.setName(XmlParser.extractFirst(body, "Name", null));
|
||||
policy.setComment(XmlParser.extractFirst(body, "Comment", null));
|
||||
XmlParser.XmlElement root = XmlParser.parseDocumentElementTree(body);
|
||||
policy.setName(directChildText(root, "Name"));
|
||||
policy.setComment(directChildText(root, "Comment"));
|
||||
policy.setConfig(ResponseHeadersPolicyConfigCodec.parse(body));
|
||||
return policy;
|
||||
}
|
||||
|
||||
+94
@@ -439,6 +439,100 @@ class CloudFrontManagementProtocolTest {
|
||||
.body(hasXPath("//*[local-name()='Code']/text()", equalTo("InvalidArgument")));
|
||||
}
|
||||
|
||||
@ParameterizedTest(name = "{0} name is the root Name, not an item Name")
|
||||
@CsvSource({"cache-policy, CachePolicy", "origin-request-policy, OriginRequestPolicy"})
|
||||
void policyNameIgnoresItemNamesThatPrecedeIt(String endpoint, String type) {
|
||||
String name = "name-order-" + UUID.randomUUID();
|
||||
String items = """
|
||||
<Quantity>1</Quantity><Items><Name>Session-ID</Name></Items>""";
|
||||
String params = """
|
||||
<HeadersConfig><HeaderBehavior>whitelist</HeaderBehavior>
|
||||
<Headers><Quantity>1</Quantity><Items><Name>X-Hdr</Name></Items></Headers></HeadersConfig>
|
||||
<CookiesConfig><CookieBehavior>whitelist</CookieBehavior>
|
||||
<Cookies>%s</Cookies></CookiesConfig>
|
||||
<QueryStringsConfig><QueryStringBehavior>whitelist</QueryStringBehavior>
|
||||
<QueryStrings>%s</QueryStrings></QueryStringsConfig>
|
||||
""".formatted(items, items);
|
||||
String config = endpoint.equals("cache-policy")
|
||||
? "<ParametersInCacheKeyAndForwardedToOrigin>"
|
||||
+ "<EnableAcceptEncodingGzip>false</EnableAcceptEncodingGzip>" + params
|
||||
+ "</ParametersInCacheKeyAndForwardedToOrigin><MinTTL>0</MinTTL>"
|
||||
: params;
|
||||
String body = "<" + type + "Config xmlns=\"http://cloudfront.amazonaws.com/doc/2020-05-31/\">"
|
||||
+ config + "<Comment>real-comment</Comment><Name>" + name + "</Name></" + type + "Config>";
|
||||
Response created = given().contentType("application/xml").body(body)
|
||||
.when().post(API + endpoint);
|
||||
created.then().statusCode(201);
|
||||
String id = XmlParser.extractFirst(created.asString(), "Id", null);
|
||||
String etag = created.header("ETag");
|
||||
try {
|
||||
given().when().get(API + endpoint + "/" + id + "/config").then().statusCode(200)
|
||||
.body(hasXPath("/*/*[local-name()='Name']/text()", equalTo(name)))
|
||||
.body(hasXPath("/*/*[local-name()='Comment']/text()", equalTo("real-comment")));
|
||||
given().when().get(API + endpoint).then().statusCode(200)
|
||||
.body(hasXPath("//*[local-name()='" + type + "Summary']/*[local-name()='" + type
|
||||
+ "']/*[local-name()='" + type + "Config']/*[local-name()='Name' and text()='"
|
||||
+ name + "']"));
|
||||
} finally {
|
||||
given().header("If-Match", etag).when().delete(API + endpoint + "/" + id)
|
||||
.then().statusCode(204);
|
||||
}
|
||||
}
|
||||
|
||||
@ParameterizedTest(name = "{0} keeps the Comment's surrounding whitespace")
|
||||
@ValueSource(strings = {"cache-policy", "origin-request-policy", "response-headers-policy"})
|
||||
void policyCommentRoundTripsWithSurroundingWhitespace(String endpoint) {
|
||||
String name = "padded-" + UUID.randomUUID();
|
||||
String comment = "\n padded comment \n";
|
||||
String body = switch (endpoint) {
|
||||
case "cache-policy" -> cachePolicyBody(name, comment, "3600", "session");
|
||||
case "origin-request-policy" -> originRequestPolicyBody(name, comment, "X-Hdr");
|
||||
default -> "<ResponseHeadersPolicyConfig xmlns=\"http://cloudfront.amazonaws.com/doc/2020-05-31/\">"
|
||||
+ "<Name>" + name + "</Name><Comment>" + comment + "</Comment></ResponseHeadersPolicyConfig>";
|
||||
};
|
||||
Response created = given().contentType("application/xml").body(body)
|
||||
.when().post(API + endpoint);
|
||||
created.then().statusCode(201);
|
||||
String id = XmlParser.extractFirst(created.asString(), "Id", null);
|
||||
try {
|
||||
given().when().get(API + endpoint + "/" + id + "/config").then().statusCode(200)
|
||||
.body(hasXPath("/*/*[local-name()='Comment']/text()", equalTo(comment)));
|
||||
} finally {
|
||||
given().header("If-Match", created.header("ETag")).when().delete(API + endpoint + "/" + id)
|
||||
.then().statusCode(204);
|
||||
}
|
||||
}
|
||||
|
||||
@ParameterizedTest(name = "{0} keeps the Name's surrounding whitespace")
|
||||
@ValueSource(strings = {"cache-policy", "origin-request-policy"})
|
||||
void policyNameRoundTripsWithSurroundingWhitespace(String endpoint) {
|
||||
String name = " padded-" + UUID.randomUUID() + " ";
|
||||
String body = endpoint.equals("cache-policy")
|
||||
? cachePolicyBody(name, "c", "3600", "session")
|
||||
: originRequestPolicyBody(name, "c", "X-Hdr");
|
||||
Response created = given().contentType("application/xml").body(body)
|
||||
.when().post(API + endpoint);
|
||||
created.then().statusCode(201);
|
||||
String id = XmlParser.extractFirst(created.asString(), "Id", null);
|
||||
try {
|
||||
given().when().get(API + endpoint + "/" + id + "/config").then().statusCode(200)
|
||||
.body(hasXPath("/*/*[local-name()='Name']/text()", equalTo(name)));
|
||||
} finally {
|
||||
given().header("If-Match", created.header("ETag")).when().delete(API + endpoint + "/" + id)
|
||||
.then().statusCode(204);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void responseHeadersPolicyRejectsNameWithSurroundingWhitespace() {
|
||||
// Name pattern [A-Za-z0-9_-]{1,128} has no spaces; trimming would wrongly accept it.
|
||||
given().contentType("application/xml")
|
||||
.body("<ResponseHeadersPolicyConfig xmlns=\"http://cloudfront.amazonaws.com/doc/2020-05-31/\">"
|
||||
+ "<Name> padded-" + UUID.randomUUID() + " </Name></ResponseHeadersPolicyConfig>")
|
||||
.when().post(API + "response-headers-policy")
|
||||
.then().statusCode(400);
|
||||
}
|
||||
|
||||
@Test
|
||||
void cachePolicyReadFillsAwsTtlDefaults() {
|
||||
String name = "default-ttl-cache-policy-" + UUID.randomUUID();
|
||||
|
||||
Reference in new issue
Block a user