Skip to content

Ministack: Free, open-source local AWS emulator - 60+ services, Terraform compatible, real databases. Free forever. MIT licensed.

feat(cfn): adding resources (#1259)

Nahuel Nuceraa7c8c1cf2916
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 7bcd83b4..0a5404a8 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -8,7 +8,7 @@ Versioning follows [Semantic Versioning](https://semver.org/).
## [Unreleased]
### Added
-- **CloudFront — cache policies** — `CreateCachePolicy`, `GetCachePolicy`, `GetCachePolicyConfig`, `UpdateCachePolicy`, `DeleteCachePolicy`, and `ListDistributionsByCachePolicyId` under `/2020-05-31/cache-policy`, so Terraform's `aws_cloudfront_cache_policy` creates, reads, updates, and deletes end-to-end. The full `CachePolicyConfig` round-trips — `MinTTL`/`DefaultTTL`/`MaxTTL` and `ParametersInCacheKeyAndForwardedToOrigin` with the gzip/brotli flags and the header/cookie/query-string behaviors and name lists — with `ETag` on every read, `If-Match` enforced on update/delete (`InvalidIfMatchVersion` / `PreconditionFailed`), and `CachePolicyAlreadyExists` / `NoSuchCachePolicy` / `CachePolicyInUse` errors matching AWS. Reported by @wparad.
+- **CloudFront — cache, origin request, and response headers policies** — full CRUD plus `GetConfig` and `ListDistributionsBy...Id` for `CachePolicy` (`/2020-05-31/cache-policy`), `OriginRequestPolicy` (`/origin-request-policy`), and `ResponseHeadersPolicy` (`/response-headers-policy`), so Terraform's `aws_cloudfront_cache_policy`, `aws_cloudfront_origin_request_policy`, and `aws_cloudfront_response_headers_policy` create, read, update, and delete end-to-end. Each config round-trips in full: `CachePolicyConfig` (`MinTTL`/`DefaultTTL`/`MaxTTL` and `ParametersInCacheKeyAndForwardedToOrigin`), `OriginRequestPolicyConfig` (header/cookie/query-string behaviors and name lists), and `ResponseHeadersPolicyConfig` (CORS, security headers, server-timing, custom and remove headers). `ETag` is returned on every read, `If-Match` is enforced on update/delete (`InvalidIfMatchVersion` / `PreconditionFailed`), and the `...AlreadyExists` / `NoSuch...` / `...InUse` error codes match AWS. Reported by @wparad.
### Fixed
- **IoT — binary topic-rule payloads are no longer corrupted** — the rules engine built every rule event by decoding the payload as UTF-8 with `errors="replace"`, so a payload that is not valid UTF-8 reached the action with each non-ASCII byte replaced by `U+FFFD`. `encode(<expr>, 'base64')` is now supported and encodes the payload as published, so `SELECT encode(*, 'base64') AS data FROM 'telemetry'` delivers `{"data": "<base64>"}` that decodes back to the published bytes — the documented way to reach a Lambda action, which does not accept binary input. No lossy decode remains on the publish → rule → invoke path; a payload that is not valid UTF-8 and whose SELECT projects no attributes dispatches no action rather than dispatching corrupted text.
diff --git a/ministack/services/cloudfront.py b/ministack/services/cloudfront.py
index deb18ddf..0281bb03 100644
--- a/ministack/services/cloudfront.py
+++ b/ministack/services/cloudfront.py
@@ -66,6 +66,16 @@ _CACHE_POLICY_CFG_RE = re.compile(r"^/2020-05-31/cache-policy/([^/]+)/config$")
_CACHE_POLICY_ID_RE = re.compile(r"^/2020-05-31/cache-policy/([^/]+)/?$")
_DIST_BY_CACHE_POLICY_RE = re.compile(r"^/2020-05-31/distributionsByCachePolicyId/([^/]+)/?$")
+_ORP_RE = re.compile(r"^/2020-05-31/origin-request-policy/?$")
+_ORP_CFG_RE = re.compile(r"^/2020-05-31/origin-request-policy/([^/]+)/config$")
+_ORP_ID_RE = re.compile(r"^/2020-05-31/origin-request-policy/([^/]+)/?$")
+_DIST_BY_ORP_RE = re.compile(r"^/2020-05-31/distributionsByOriginRequestPolicyId/([^/]+)/?$")
+
+_RHP_RE = re.compile(r"^/2020-05-31/response-headers-policy/?$")
+_RHP_CFG_RE = re.compile(r"^/2020-05-31/response-headers-policy/([^/]+)/config$")
+_RHP_ID_RE = re.compile(r"^/2020-05-31/response-headers-policy/([^/]+)/?$")
+_DIST_BY_RHP_RE = re.compile(r"^/2020-05-31/distributionsByResponseHeadersPolicyId/([^/]+)/?$")
+
# ---------------------------------------------------------------------------
# In-memory state
# ---------------------------------------------------------------------------
@@ -76,6 +86,8 @@ _oacs = AccountScopedDict() # Id -> OAC record
_functions = AccountScopedDict() # Name -> function record (CloudFront Functions API)
_kvstores = AccountScopedDict() # Name -> KVS record
_cache_policies = AccountScopedDict() # Id -> cache policy record
+_origin_request_policies = AccountScopedDict() # Id -> origin request policy record
+_response_headers_policies = AccountScopedDict() # Id -> response headers policy record
def reset():
@@ -86,6 +98,8 @@ def reset():
_functions.clear()
_kvstores.clear()
_cache_policies.clear()
+ _origin_request_policies.clear()
+ _response_headers_policies.clear()
def get_state():
@@ -98,6 +112,8 @@ def get_state():
"functions": _functions,
"kvstores": _kvstores,
"cache_policies": _cache_policies,
+ "origin_request_policies": _origin_request_policies,
+ "response_headers_policies": _response_headers_policies,
}
)
@@ -110,6 +126,8 @@ def restore_state(data):
_functions.update(data.get("functions", {}))
_kvstores.update(data.get("kvstores", {}))
_cache_policies.update(data.get("cache_policies", {}))
+ _origin_request_policies.update(data.get("origin_request_policies", {}))
+ _response_headers_policies.update(data.get("response_headers_policies", {}))
try:
@@ -932,6 +950,436 @@ def _list_distributions_by_cache_policy(policy_id):
return _xml_response("DistributionIdList", build)
+# ---------------------------------------------------------------------------
+# Origin request policies (aws_cloudfront_origin_request_policy) and response
+# headers policies (aws_cloudfront_response_headers_policy) — #1249.
+# Shapes verified against botocore cloudfront service-2.json (2020-05-31).
+# ---------------------------------------------------------------------------
+
+
+def _xbool(el, tag, default=None):
+ """Parse a boolean child element; return ``default`` when it is absent."""
+ child = _find(el, tag)
+ if child is None:
+ return default
+ return (child.text or "").strip().lower() == "true"
+
+
+def _opt_text(el, tag):
+ """Return a child element's text, or None when the element is absent."""
+ child = _find(el, tag)
+ return (child.text or "") if child is not None else None
+
+
+def _bstr(value):
+ return "true" if value else "false"
+
+
+def _fmt_rate(x):
+ return "%g" % x
+
+
+def _parse_str_list_block(cfg_el, block_tag, item_tag):
+ """Parse ``<block_tag><Items><item_tag>..</item_tag></Items></block_tag>`` to a list."""
+ block = _find(cfg_el, block_tag)
+ items = []
+ if block is not None:
+ items_el = _find(block, "Items")
+ if items_el is not None:
+ for child in items_el:
+ local = child.tag.split("}")[-1] if "}" in child.tag else child.tag
+ if local == item_tag:
+ items.append(child.text or "")
+ return items
+
+
+def _build_str_list_block(parent, block_tag, item_tag, items):
+ block = SubElement(parent, block_tag)
+ SubElement(block, "Quantity").text = str(len(items))
+ if items:
+ items_el = SubElement(block, "Items")
+ for it in items:
+ SubElement(items_el, item_tag).text = it
+
+
+def _distributions_using_policy(policy_id):
+ return [d.get("Id", "") for d in _distributions.values() if _value_contains(d, policy_id)]
+
+
+# ---- generic policy CRUD, shared by ORP and RHP ----
+
+
+def _policy_precheck_if_match(headers, policy):
+ if_match = headers.get("if-match")
+ if not if_match:
+ return _error("InvalidIfMatchVersion", "The If-Match version is missing or not valid for the resource.", 400)
+ if if_match != policy["ETag"]:
+ return _error(
+ "PreconditionFailed",
+ "The precondition given in one or more of the request-header fields evaluated to false.",
+ 412,
+ )
+ return None
+
+
+def _policy_create(store, spec, body):
+ el = _parse_body(body)
+ if el is None:
+ return _error("MalformedXML", "The XML document is malformed.", 400)
+ cfg, err = spec["parse"](el)
+ if err is not None:
+ return err
+ for existing in store.values():
+ if existing["Config"]["Name"] == cfg["Name"]:
+ return _error(spec["dup"], f"A {spec['label']} with the same name already exists.", 409)
+ pid = new_uuid()
+ etag = new_uuid()
+ policy = {"Id": pid, "ETag": etag, "LastModifiedTime": _now_iso(), "Config": cfg}
+ store[pid] = policy
+ logger.info("Create %s id=%s name=%s", spec["label"], pid, cfg["Name"])
+ return _xml_response(
+ spec["resource_tag"], lambda r: spec["build_resource"](r, policy),
+ status=201, extra_headers={"ETag": etag, "Location": f"{spec['path']}/{pid}"},
+ )
+
+
+def _policy_get(store, spec, pid):
+ policy = store.get(pid)
+ if not policy:
+ return _error(spec["missing"], f"The {spec['label']} does not exist.", 404)
+ return _xml_response(spec["resource_tag"], lambda r: spec["build_resource"](r, policy),
+ extra_headers={"ETag": policy["ETag"]})
+
+
+def _policy_get_config(store, spec, pid):
+ policy = store.get(pid)
+ if not policy:
+ return _error(spec["missing"], f"The {spec['label']} does not exist.", 404)
+ return _xml_response(spec["config_tag"], lambda r: spec["build_config"](r, policy["Config"]),
+ extra_headers={"ETag": policy["ETag"]})
+
+
+def _policy_update(store, spec, pid, headers, body):
+ policy = store.get(pid)
+ if not policy:
+ return _error(spec["missing"], f"The {spec['label']} does not exist.", 404)
+ pc = _policy_precheck_if_match(headers, policy)
+ if pc is not None:
+ return pc
+ el = _parse_body(body)
+ if el is None:
+ return _error("MalformedXML", "The XML document is malformed.", 400)
+ cfg, err = spec["parse"](el)
+ if err is not None:
+ return err
+ for existing in store.values():
+ if existing["Id"] != pid and existing["Config"]["Name"] == cfg["Name"]:
+ return _error(spec["dup"], f"A {spec['label']} with the same name already exists.", 409)
+ new_etag = new_uuid()
+ policy["Config"] = cfg
+ policy["ETag"] = new_etag
+ policy["LastModifiedTime"] = _now_iso()
+ return _xml_response(spec["resource_tag"], lambda r: spec["build_resource"](r, policy),
+ extra_headers={"ETag": new_etag})
+
+
+def _policy_delete(store, spec, pid, headers):
+ policy = store.get(pid)
+ if not policy:
+ return _error(spec["missing"], f"The {spec['label']} does not exist.", 404)
+ pc = _policy_precheck_if_match(headers, policy)
+ if pc is not None:
+ return pc
+ if _distributions_using_policy(pid):
+ return _error(spec["in_use"],
+ f"The {spec['label']} cannot be deleted because it is attached to one or more cache behaviors.",
+ 409)
+ del store[pid]
+ logger.info("Delete %s id=%s", spec["label"], pid)
+ return 204, {}, b""
+
+
+def _policy_list_distributions(store, spec, pid):
+ if not store.get(pid):
+ return _error(spec["missing"], f"The {spec['label']} does not exist.", 404)
+ dist_ids = _distributions_using_policy(pid)
+
+ def build(root):
+ SubElement(root, "Marker").text = ""
+ SubElement(root, "MaxItems").text = "100"
+ SubElement(root, "IsTruncated").text = "false"
+ SubElement(root, "Quantity").text = str(len(dist_ids))
+ if dist_ids:
+ items_el = SubElement(root, "Items")
+ for did in dist_ids:
+ SubElement(items_el, "DistributionId").text = did
+
+ return _xml_response("DistributionIdList", build)
+
+
+# ---- OriginRequestPolicy ----
+
+_ORP_HEADER_BEHAVIORS = {"none", "whitelist", "allViewer", "allViewerAndWhitelistCloudFront", "allExcept"}
+_ORP_COOKIE_BEHAVIORS = {"none", "whitelist", "all", "allExcept"}
+_ORP_QUERYSTRING_BEHAVIORS = {"none", "whitelist", "all", "allExcept"}
+
+
+def _parse_orp_config(el):
+ name = _text(el, "Name")
+ if not name:
+ return None, _error("InvalidArgument", "The origin request policy name is required.", 400)
+ headers_cfg = _find(el, "HeadersConfig")
+ cookies_cfg = _find(el, "CookiesConfig")
+ qs_cfg = _find(el, "QueryStringsConfig")
+ if headers_cfg is None or cookies_cfg is None or qs_cfg is None:
+ return None, _error("InvalidArgument",
+ "HeadersConfig, CookiesConfig, and QueryStringsConfig are required.", 400)
+ hb = _text(headers_cfg, "HeaderBehavior")
+ cb = _text(cookies_cfg, "CookieBehavior")
+ qb = _text(qs_cfg, "QueryStringBehavior")
+ if hb not in _ORP_HEADER_BEHAVIORS:
+ return None, _error("InvalidArgument", "Invalid HeaderBehavior value.", 400)
+ if cb not in _ORP_COOKIE_BEHAVIORS:
+ return None, _error("InvalidArgument", "Invalid CookieBehavior value.", 400)
+ if qb not in _ORP_QUERYSTRING_BEHAVIORS:
+ return None, _error("InvalidArgument", "Invalid QueryStringBehavior value.", 400)
+ return {
+ "Name": name, "Comment": _text(el, "Comment"),
+ "HeaderBehavior": hb, "Headers": _parse_name_items(headers_cfg, "Headers"),
+ "CookieBehavior": cb, "Cookies": _parse_name_items(cookies_cfg, "Cookies"),
+ "QueryStringBehavior": qb, "QueryStrings": _parse_name_items(qs_cfg, "QueryStrings"),
+ }, None
+
+
+def _build_orp_config_xml(parent, cfg):
+ SubElement(parent, "Comment").text = cfg.get("Comment", "")
+ SubElement(parent, "Name").text = cfg["Name"]
+ hc = SubElement(parent, "HeadersConfig")
+ SubElement(hc, "HeaderBehavior").text = cfg["HeaderBehavior"]
+ _build_names_block(hc, "Headers", cfg["Headers"])
+ cc = SubElement(parent, "CookiesConfig")
+ SubElement(cc, "CookieBehavior").text = cfg["CookieBehavior"]
+ _build_names_block(cc, "Cookies", cfg["Cookies"])
+ qc = SubElement(parent, "QueryStringsConfig")
+ SubElement(qc, "QueryStringBehavior").text = cfg["QueryStringBehavior"]
+ _build_names_block(qc, "QueryStrings", cfg["QueryStrings"])
+
+
+def _build_orp_xml(parent, policy):
+ SubElement(parent, "Id").text = policy["Id"]
+ SubElement(parent, "LastModifiedTime").text = policy["LastModifiedTime"]
+ cfg_el = SubElement(parent, "OriginRequestPolicyConfig")
+ _build_orp_config_xml(cfg_el, policy["Config"])
+
+
+_ORP_SPEC = {
+ "label": "origin request policy", "resource_tag": "OriginRequestPolicy",
+ "config_tag": "OriginRequestPolicyConfig", "path": "/2020-05-31/origin-request-policy",
+ "missing": "NoSuchOriginRequestPolicy", "dup": "OriginRequestPolicyAlreadyExists",
+ "in_use": "OriginRequestPolicyInUse", "parse": _parse_orp_config,
+ "build_resource": _build_orp_xml, "build_config": _build_orp_config_xml,
+}
+
+
+# ---- ResponseHeadersPolicy ----
+
+_RHP_FRAME_OPTIONS = {"DENY", "SAMEORIGIN"}
+_RHP_REFERRER = {
+ "no-referrer", "no-referrer-when-downgrade", "origin", "origin-when-cross-origin",
+ "same-origin", "strict-origin", "strict-origin-when-cross-origin", "unsafe-url",
+}
+
+
+def _parse_rhp_config(el):
+ name = _text(el, "Name")
+ if not name:
+ return None, _error("InvalidArgument", "The response headers policy name is required.", 400)
+ cfg = {"Name": name, "Comment": _text(el, "Comment"), "Cors": None, "Security": None,
+ "ServerTiming": None, "CustomHeaders": [], "RemoveHeaders": []}
+
+ cors_el = _find(el, "CorsConfig")
+ if cors_el is not None:
+ cors = {
+ "AllowOrigins": _parse_str_list_block(cors_el, "AccessControlAllowOrigins", "Origin"),
+ "AllowHeaders": _parse_str_list_block(cors_el, "AccessControlAllowHeaders", "Header"),
+ "AllowMethods": _parse_str_list_block(cors_el, "AccessControlAllowMethods", "Method"),
+ "AllowCredentials": _xbool(cors_el, "AccessControlAllowCredentials", False),
+ "OriginOverride": _xbool(cors_el, "OriginOverride", False),
+ "ExposeHeaders": None, "MaxAgeSec": None,
+ }
+ if _find(cors_el, "AccessControlExposeHeaders") is not None:
+ cors["ExposeHeaders"] = _parse_str_list_block(cors_el, "AccessControlExposeHeaders", "Header")
+ maxage = _find(cors_el, "AccessControlMaxAgeSec")
+ if maxage is not None and (maxage.text or "").strip():
+ cors["MaxAgeSec"] = int(maxage.text)
+ cfg["Cors"] = cors
+
+ sec_el = _find(el, "SecurityHeadersConfig")
+ if sec_el is not None:
+ sec = {}
+ xss = _find(sec_el, "XSSProtection")
+ if xss is not None:
+ sec["XSSProtection"] = {
+ "Override": _xbool(xss, "Override", False),
+ "Protection": _xbool(xss, "Protection", False),
+ "ModeBlock": _xbool(xss, "ModeBlock"),
+ "ReportUri": _opt_text(xss, "ReportUri"),
+ }
+ fo = _find(sec_el, "FrameOptions")
+ if fo is not None:
+ fov = _text(fo, "FrameOption")
+ if fov not in _RHP_FRAME_OPTIONS:
+ return None, _error("InvalidArgument", "Invalid FrameOption value.", 400)
+ sec["FrameOptions"] = {"Override": _xbool(fo, "Override", False), "FrameOption": fov}
+ rp = _find(sec_el, "ReferrerPolicy")
+ if rp is not None:
+ rpv = _text(rp, "ReferrerPolicy")
+ if rpv not in _RHP_REFERRER:
+ return None, _error("InvalidArgument", "Invalid ReferrerPolicy value.", 400)
+ sec["ReferrerPolicy"] = {"Override": _xbool(rp, "Override", False), "ReferrerPolicy": rpv}
+ csp = _find(sec_el, "ContentSecurityPolicy")
+ if csp is not None:
+ sec["ContentSecurityPolicy"] = {"Override": _xbool(csp, "Override", False),
+ "ContentSecurityPolicy": _text(csp, "ContentSecurityPolicy")}
+ cto = _find(sec_el, "ContentTypeOptions")
+ if cto is not None:
+ sec["ContentTypeOptions"] = {"Override": _xbool(cto, "Override", False)}
+ hsts = _find(sec_el, "StrictTransportSecurity")
+ if hsts is not None:
+ sec["StrictTransportSecurity"] = {
+ "Override": _xbool(hsts, "Override", False),
+ "IncludeSubdomains": _xbool(hsts, "IncludeSubdomains"),
+ "Preload": _xbool(hsts, "Preload"),
+ "AccessControlMaxAgeSec": int(_text(hsts, "AccessControlMaxAgeSec") or "0"),
+ }
+ cfg["Security"] = sec
+
+ st_el = _find(el, "ServerTimingHeadersConfig")
+ if st_el is not None:
+ st = {"Enabled": _xbool(st_el, "Enabled", False), "SamplingRate": None}
+ sr = _find(st_el, "SamplingRate")
+ if sr is not None and (sr.text or "").strip():
+ st["SamplingRate"] = float(sr.text)
+ cfg["ServerTiming"] = st
+
+ ch_el = _find(el, "CustomHeadersConfig")
+ if ch_el is not None:
+ items_el = _find(ch_el, "Items")
+ if items_el is not None:
+ for it in items_el:
+ local = it.tag.split("}")[-1] if "}" in it.tag else it.tag
+ if local == "ResponseHeadersPolicyCustomHeader":
+ cfg["CustomHeaders"].append({
+ "Header": _text(it, "Header"), "Value": _text(it, "Value"),
+ "Override": _xbool(it, "Override", False),
+ })
+
+ rh_el = _find(el, "RemoveHeadersConfig")
+ if rh_el is not None:
+ items_el = _find(rh_el, "Items")
+ if items_el is not None:
+ for it in items_el:
+ local = it.tag.split("}")[-1] if "}" in it.tag else it.tag
+ if local == "ResponseHeadersPolicyRemoveHeader":
+ cfg["RemoveHeaders"].append({"Header": _text(it, "Header")})
+
+ return cfg, None
+
+
+def _build_rhp_config_xml(parent, cfg):
+ SubElement(parent, "Comment").text = cfg.get("Comment", "")
+ SubElement(parent, "Name").text = cfg["Name"]
+
+ cors = cfg.get("Cors")
+ if cors is not None:
+ c = SubElement(parent, "CorsConfig")
+ _build_str_list_block(c, "AccessControlAllowOrigins", "Origin", cors["AllowOrigins"])
+ _build_str_list_block(c, "AccessControlAllowHeaders", "Header", cors["AllowHeaders"])
+ _build_str_list_block(c, "AccessControlAllowMethods", "Method", cors["AllowMethods"])
+ SubElement(c, "AccessControlAllowCredentials").text = _bstr(cors["AllowCredentials"])
+ if cors.get("ExposeHeaders") is not None:
+ _build_str_list_block(c, "AccessControlExposeHeaders", "Header", cors["ExposeHeaders"])
+ if cors.get("MaxAgeSec") is not None:
+ SubElement(c, "AccessControlMaxAgeSec").text = str(cors["MaxAgeSec"])
+ SubElement(c, "OriginOverride").text = _bstr(cors["OriginOverride"])
+
+ sec = cfg.get("Security")
+ if sec is not None:
+ s = SubElement(parent, "SecurityHeadersConfig")
+ if "XSSProtection" in sec:
+ x = SubElement(s, "XSSProtection")
+ SubElement(x, "Override").text = _bstr(sec["XSSProtection"]["Override"])
+ SubElement(x, "Protection").text = _bstr(sec["XSSProtection"]["Protection"])
+ if sec["XSSProtection"].get("ModeBlock") is not None:
+ SubElement(x, "ModeBlock").text = _bstr(sec["XSSProtection"]["ModeBlock"])
+ if sec["XSSProtection"].get("ReportUri") is not None:
+ SubElement(x, "ReportUri").text = sec["XSSProtection"]["ReportUri"]
+ if "FrameOptions" in sec:
+ f = SubElement(s, "FrameOptions")
+ SubElement(f, "Override").text = _bstr(sec["FrameOptions"]["Override"])
+ SubElement(f, "FrameOption").text = sec["FrameOptions"]["FrameOption"]
+ if "ReferrerPolicy" in sec:
+ r = SubElement(s, "ReferrerPolicy")
+ SubElement(r, "Override").text = _bstr(sec["ReferrerPolicy"]["Override"])
+ SubElement(r, "ReferrerPolicy").text = sec["ReferrerPolicy"]["ReferrerPolicy"]
+ if "ContentSecurityPolicy" in sec:
+ cs = SubElement(s, "ContentSecurityPolicy")
+ SubElement(cs, "Override").text = _bstr(sec["ContentSecurityPolicy"]["Override"])
+ SubElement(cs, "ContentSecurityPolicy").text = sec["ContentSecurityPolicy"]["ContentSecurityPolicy"]
+ if "ContentTypeOptions" in sec:
+ ct = SubElement(s, "ContentTypeOptions")
+ SubElement(ct, "Override").text = _bstr(sec["ContentTypeOptions"]["Override"])
+ if "StrictTransportSecurity" in sec:
+ h = SubElement(s, "StrictTransportSecurity")
+ SubElement(h, "Override").text = _bstr(sec["StrictTransportSecurity"]["Override"])
+ if sec["StrictTransportSecurity"].get("IncludeSubdomains") is not None:
+ SubElement(h, "IncludeSubdomains").text = _bstr(sec["StrictTransportSecurity"]["IncludeSubdomains"])
+ if sec["StrictTransportSecurity"].get("Preload") is not None:
+ SubElement(h, "Preload").text = _bstr(sec["StrictTransportSecurity"]["Preload"])
+ SubElement(h, "AccessControlMaxAgeSec").text = str(sec["StrictTransportSecurity"]["AccessControlMaxAgeSec"])
+
+ st = cfg.get("ServerTiming")
+ if st is not None:
+ stel = SubElement(parent, "ServerTimingHeadersConfig")
+ SubElement(stel, "Enabled").text = _bstr(st["Enabled"])
+ if st.get("SamplingRate") is not None:
+ SubElement(stel, "SamplingRate").text = _fmt_rate(st["SamplingRate"])
+
+ ch = SubElement(parent, "CustomHeadersConfig")
+ SubElement(ch, "Quantity").text = str(len(cfg["CustomHeaders"]))
+ if cfg["CustomHeaders"]:
+ items = SubElement(ch, "Items")
+ for hdr in cfg["CustomHeaders"]:
+ it = SubElement(items, "ResponseHeadersPolicyCustomHeader")
+ SubElement(it, "Header").text = hdr["Header"]
+ SubElement(it, "Value").text = hdr["Value"]
+ SubElement(it, "Override").text = _bstr(hdr["Override"])
+
+ rh = SubElement(parent, "RemoveHeadersConfig")
+ SubElement(rh, "Quantity").text = str(len(cfg["RemoveHeaders"]))
+ if cfg["RemoveHeaders"]:
+ items = SubElement(rh, "Items")
+ for hdr in cfg["RemoveHeaders"]:
+ it = SubElement(items, "ResponseHeadersPolicyRemoveHeader")
+ SubElement(it, "Header").text = hdr["Header"]
+
+
+def _build_rhp_xml(parent, policy):
+ SubElement(parent, "Id").text = policy["Id"]
+ SubElement(parent, "LastModifiedTime").text = policy["LastModifiedTime"]
+ cfg_el = SubElement(parent, "ResponseHeadersPolicyConfig")
+ _build_rhp_config_xml(cfg_el, policy["Config"])
+
+
+_RHP_SPEC = {
+ "label": "response headers policy", "resource_tag": "ResponseHeadersPolicy",
+ "config_tag": "ResponseHeadersPolicyConfig", "path": "/2020-05-31/response-headers-policy",
+ "missing": "NoSuchResponseHeadersPolicy", "dup": "ResponseHeadersPolicyAlreadyExists",
+ "in_use": "ResponseHeadersPolicyInUse", "parse": _parse_rhp_config,
+ "build_resource": _build_rhp_xml, "build_config": _build_rhp_config_xml,
+}
+
+
# ---------------------------------------------------------------------------
# Request dispatcher
# ---------------------------------------------------------------------------
@@ -1047,6 +1495,58 @@ async def handle_request(method, path, headers, body, query_params):
if method == "GET":
return _list_distributions_by_cache_policy(m.group(1))
+ # Origin request policy routes
+ m = _ORP_CFG_RE.match(path)
+ if m:
+ if method == "GET":
+ return _policy_get_config(_origin_request_policies, _ORP_SPEC, m.group(1))
+
+ m = _ORP_RE.match(path)
+ if m:
+ if method == "POST":
+ return _policy_create(_origin_request_policies, _ORP_SPEC, body)
+
+ m = _ORP_ID_RE.match(path)
+ if m:
+ pid = m.group(1)
+ if method == "GET":
+ return _policy_get(_origin_request_policies, _ORP_SPEC, pid)
+ if method == "PUT":
+ return _policy_update(_origin_request_policies, _ORP_SPEC, pid, headers, body)
+ if method == "DELETE":
+ return _policy_delete(_origin_request_policies, _ORP_SPEC, pid, headers)
+
+ m = _DIST_BY_ORP_RE.match(path)
+ if m:
+ if method == "GET":
+ return _policy_list_distributions(_origin_request_policies, _ORP_SPEC, m.group(1))
+
+ # Response headers policy routes
+ m = _RHP_CFG_RE.match(path)
+ if m:
+ if method == "GET":
+ return _policy_get_config(_response_headers_policies, _RHP_SPEC, m.group(1))
+
+ m = _RHP_RE.match(path)
+ if m:
+ if method == "POST":
+ return _policy_create(_response_headers_policies, _RHP_SPEC, body)
+
+ m = _RHP_ID_RE.match(path)
+ if m:
+ pid = m.group(1)
+ if method == "GET":
+ return _policy_get(_response_headers_policies, _RHP_SPEC, pid)
+ if method == "PUT":
+ return _policy_update(_response_headers_policies, _RHP_SPEC, pid, headers, body)
+ if method == "DELETE":
+ return _policy_delete(_response_headers_policies, _RHP_SPEC, pid, headers)
+
+ m = _DIST_BY_RHP_RE.match(path)
+ if m:
+ if method == "GET":
+ return _policy_list_distributions(_response_headers_policies, _RHP_SPEC, m.group(1))
+
# CloudFront Functions API (used by Terraform aws_cloudfront_function)
m = _FUN_DESCRIBE_RE.match(path)
if m:
diff --git a/tests/test_cloudfront.py b/tests/test_cloudfront.py
index f851867b..1f818479 100644
--- a/tests/test_cloudfront.py
+++ b/tests/test_cloudfront.py
@@ -1390,3 +1390,190 @@ def test_cloudfront_list_distributions_by_cache_policy(cloudfront):
assert dil["IsTruncated"] is False
assert dil.get("Items", []) == []
cloudfront.delete_cache_policy(Id=pid, IfMatch=create["ETag"])
+
+
+# ---------------------------------------------------------------------------
+# Origin request policies (aws_cloudfront_origin_request_policy) — #1249
+# ---------------------------------------------------------------------------
+
+
+def _orp_config(name):
+ return {
+ "Name": name,
+ "Comment": "test orp",
+ "HeadersConfig": {"HeaderBehavior": "whitelist", "Headers": {"Quantity": 1, "Items": ["X-Custom"]}},
+ "CookiesConfig": {"CookieBehavior": "all"},
+ "QueryStringsConfig": {"QueryStringBehavior": "whitelist", "QueryStrings": {"Quantity": 2, "Items": ["a", "b"]}},
+ }
+
+
+def test_cloudfront_create_and_get_origin_request_policy(cloudfront):
+ name = f"orp-{_uuid_mod.uuid4().hex[:8]}"
+ create = cloudfront.create_origin_request_policy(OriginRequestPolicyConfig=_orp_config(name))
+ assert create["ETag"]
+ pid = create["OriginRequestPolicy"]["Id"]
+ assert "LastModifiedTime" in create["OriginRequestPolicy"]
+
+ got = cloudfront.get_origin_request_policy(Id=pid)
+ cfg = got["OriginRequestPolicy"]["OriginRequestPolicyConfig"]
+ assert got["ETag"] == create["ETag"]
+ assert cfg["Name"] == name
+ assert cfg["HeadersConfig"]["HeaderBehavior"] == "whitelist"
+ assert cfg["HeadersConfig"]["Headers"]["Items"] == ["X-Custom"]
+ assert cfg["CookiesConfig"]["CookieBehavior"] == "all"
+ assert cfg["QueryStringsConfig"]["QueryStringBehavior"] == "whitelist"
+ assert cfg["QueryStringsConfig"]["QueryStrings"]["Items"] == ["a", "b"]
+
+ cloudfront.delete_origin_request_policy(Id=pid, IfMatch=got["ETag"])
+
+
+def test_cloudfront_origin_request_policy_config_and_update(cloudfront):
+ name = f"orp-{_uuid_mod.uuid4().hex[:8]}"
+ create = cloudfront.create_origin_request_policy(OriginRequestPolicyConfig=_orp_config(name))
+ pid = create["OriginRequestPolicy"]["Id"]
+
+ resp = cloudfront.get_origin_request_policy_config(Id=pid)
+ assert resp["ETag"] == create["ETag"]
+ assert resp["OriginRequestPolicyConfig"]["Name"] == name
+
+ updated = _orp_config(name)
+ updated["CookiesConfig"] = {"CookieBehavior": "none"}
+ with pytest.raises(ClientError) as exc:
+ cloudfront.update_origin_request_policy(OriginRequestPolicyConfig=updated, Id=pid)
+ assert exc.value.response["Error"]["Code"] == "InvalidIfMatchVersion"
+
+ upd = cloudfront.update_origin_request_policy(OriginRequestPolicyConfig=updated, Id=pid, IfMatch=create["ETag"])
+ assert upd["ETag"] != create["ETag"]
+ assert upd["OriginRequestPolicy"]["OriginRequestPolicyConfig"]["CookiesConfig"]["CookieBehavior"] == "none"
+ cloudfront.delete_origin_request_policy(Id=pid, IfMatch=upd["ETag"])
+
+
+def test_cloudfront_origin_request_policy_delete_and_duplicate(cloudfront):
+ name = f"orp-{_uuid_mod.uuid4().hex[:8]}"
+ create = cloudfront.create_origin_request_policy(OriginRequestPolicyConfig=_orp_config(name))
+ pid = create["OriginRequestPolicy"]["Id"]
+
+ with pytest.raises(ClientError) as exc:
+ cloudfront.create_origin_request_policy(OriginRequestPolicyConfig=_orp_config(name))
+ assert exc.value.response["Error"]["Code"] == "OriginRequestPolicyAlreadyExists"
+
+ cloudfront.delete_origin_request_policy(Id=pid, IfMatch=create["ETag"])
+ with pytest.raises(ClientError) as exc:
+ cloudfront.get_origin_request_policy(Id=pid)
+ assert exc.value.response["Error"]["Code"] == "NoSuchOriginRequestPolicy"
+
+
+def test_cloudfront_list_distributions_by_origin_request_policy(cloudfront):
+ name = f"orp-{_uuid_mod.uuid4().hex[:8]}"
+ create = cloudfront.create_origin_request_policy(OriginRequestPolicyConfig=_orp_config(name))
+ pid = create["OriginRequestPolicy"]["Id"]
+ resp = cloudfront.list_distributions_by_origin_request_policy_id(OriginRequestPolicyId=pid)
+ assert resp["DistributionIdList"]["Quantity"] == 0
+ cloudfront.delete_origin_request_policy(Id=pid, IfMatch=create["ETag"])
+
+
+# ---------------------------------------------------------------------------
+# Response headers policies (aws_cloudfront_response_headers_policy) — #1249
+# ---------------------------------------------------------------------------
+
+
+def _rhp_config(name):
+ return {
+ "Name": name,
+ "Comment": "test rhp",
+ "CorsConfig": {
+ "AccessControlAllowOrigins": {"Quantity": 1, "Items": ["https://example.com"]},
+ "AccessControlAllowHeaders": {"Quantity": 1, "Items": ["X-Custom"]},
+ "AccessControlAllowMethods": {"Quantity": 2, "Items": ["GET", "POST"]},
+ "AccessControlAllowCredentials": False,
+ "AccessControlExposeHeaders": {"Quantity": 1, "Items": ["X-Expose"]},
+ "AccessControlMaxAgeSec": 600,
+ "OriginOverride": True,
+ },
+ "SecurityHeadersConfig": {
+ "FrameOptions": {"Override": True, "FrameOption": "DENY"},
+ "ContentTypeOptions": {"Override": True},
+ "ReferrerPolicy": {"Override": True, "ReferrerPolicy": "same-origin"},
+ "StrictTransportSecurity": {
+ "Override": True, "AccessControlMaxAgeSec": 31536000,
+ "IncludeSubdomains": True, "Preload": False,
+ },
+ },
+ "CustomHeadersConfig": {
+ "Quantity": 1,
+ "Items": [{"Header": "X-Extra", "Value": "yes", "Override": True}],
+ },
+ "RemoveHeadersConfig": {"Quantity": 1, "Items": [{"Header": "Server"}]},
+ }
+
+
+def test_cloudfront_create_and_get_response_headers_policy(cloudfront):
+ name = f"rhp-{_uuid_mod.uuid4().hex[:8]}"
+ create = cloudfront.create_response_headers_policy(ResponseHeadersPolicyConfig=_rhp_config(name))
+ assert create["ETag"]
+ pid = create["ResponseHeadersPolicy"]["Id"]
+
+ got = cloudfront.get_response_headers_policy(Id=pid)
+ cfg = got["ResponseHeadersPolicy"]["ResponseHeadersPolicyConfig"]
+ assert got["ETag"] == create["ETag"]
+ assert cfg["Name"] == name
+
+ cors = cfg["CorsConfig"]
+ assert cors["AccessControlAllowOrigins"]["Items"] == ["https://example.com"]
+ assert cors["AccessControlAllowMethods"]["Items"] == ["GET", "POST"]
+ assert cors["AccessControlAllowCredentials"] is False
+ assert cors["AccessControlExposeHeaders"]["Items"] == ["X-Expose"]
+ assert cors["AccessControlMaxAgeSec"] == 600
+ assert cors["OriginOverride"] is True
+
+ sec = cfg["SecurityHeadersConfig"]
+ assert sec["FrameOptions"]["FrameOption"] == "DENY"
+ assert sec["ContentTypeOptions"]["Override"] is True
+ assert sec["ReferrerPolicy"]["ReferrerPolicy"] == "same-origin"
+ assert sec["StrictTransportSecurity"]["AccessControlMaxAgeSec"] == 31536000
+ assert sec["StrictTransportSecurity"]["IncludeSubdomains"] is True
+ assert sec["StrictTransportSecurity"]["Preload"] is False
+
+ assert cfg["CustomHeadersConfig"]["Items"] == [{"Header": "X-Extra", "Value": "yes", "Override": True}]
+ assert cfg["RemoveHeadersConfig"]["Items"] == [{"Header": "Server"}]
+
+ cloudfront.delete_response_headers_policy(Id=pid, IfMatch=got["ETag"])
+
+
+def test_cloudfront_response_headers_policy_config_update_delete(cloudfront):
+ name = f"rhp-{_uuid_mod.uuid4().hex[:8]}"
+ create = cloudfront.create_response_headers_policy(ResponseHeadersPolicyConfig=_rhp_config(name))
+ pid = create["ResponseHeadersPolicy"]["Id"]
+
+ resp = cloudfront.get_response_headers_policy_config(Id=pid)
+ assert resp["ETag"] == create["ETag"]
+ assert resp["ResponseHeadersPolicyConfig"]["Name"] == name
+
+ updated = _rhp_config(name)
+ updated["CorsConfig"]["AccessControlMaxAgeSec"] = 1200
+ with pytest.raises(ClientError) as exc:
+ cloudfront.update_response_headers_policy(ResponseHeadersPolicyConfig=updated, Id=pid, IfMatch="stale")
+ assert exc.value.response["Error"]["Code"] == "PreconditionFailed"
+
+ upd = cloudfront.update_response_headers_policy(
+ ResponseHeadersPolicyConfig=updated, Id=pid, IfMatch=create["ETag"]
+ )
+ assert upd["ResponseHeadersPolicy"]["ResponseHeadersPolicyConfig"]["CorsConfig"]["AccessControlMaxAgeSec"] == 1200
+
+ cloudfront.delete_response_headers_policy(Id=pid, IfMatch=upd["ETag"])
+ with pytest.raises(ClientError) as exc:
+ cloudfront.get_response_headers_policy(Id=pid)
+ assert exc.value.response["Error"]["Code"] == "NoSuchResponseHeadersPolicy"
+
+
+def test_cloudfront_response_headers_policy_duplicate_and_list(cloudfront):
+ name = f"rhp-{_uuid_mod.uuid4().hex[:8]}"
+ create = cloudfront.create_response_headers_policy(ResponseHeadersPolicyConfig=_rhp_config(name))
+ pid = create["ResponseHeadersPolicy"]["Id"]
+ with pytest.raises(ClientError) as exc:
+ cloudfront.create_response_headers_policy(ResponseHeadersPolicyConfig=_rhp_config(name))
+ assert exc.value.response["Error"]["Code"] == "ResponseHeadersPolicyAlreadyExists"
+
+ resp = cloudfront.list_distributions_by_response_headers_policy_id(ResponseHeadersPolicyId=pid)
+ assert resp["DistributionIdList"]["Quantity"] == 0
+ cloudfront.delete_response_headers_policy(Id=pid, IfMatch=create["ETag"])
diff --git a/tests/test_dynamodb.py b/tests/test_dynamodb.py
index 3275e775..3c7886c0 100644
--- a/tests/test_dynamodb.py
+++ b/tests/test_dynamodb.py
@@ -4504,7 +4504,8 @@ def test_dynamodb_projection_list_index_with_sibling_attribute(ddb):
})
r = ddb.get_item(
TableName=name, Key={"pk": {"S": "a"}},
- ProjectionExpression="pk, items[2]",
+ ProjectionExpression="pk, #i[2]",
+ ExpressionAttributeNames={"#i": "items"},
)
assert r["Item"] == {
"pk": {"S": "a"},