Skip to content

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

feat(cloudfront): add KeyValueStore management API (#542)

Davi Reis Vieirac4f6b1c7e442
diff --git a/ministack/services/cloudfront.py b/ministack/services/cloudfront.py
index 73afb45e..dffb4336 100644
--- a/ministack/services/cloudfront.py
+++ b/ministack/services/cloudfront.py
@@ -13,6 +13,8 @@ Supports:
UpdateOriginAccessControl, DeleteOriginAccessControl
Functions (stub): CreateFunction, DeleteFunction, DescribeFunction, GetFunction,
ListFunctions, PublishFunction, UpdateFunction
+ KeyValueStore: CreateKeyValueStore, DescribeKeyValueStore, ListKeyValueStores,
+ UpdateKeyValueStore, DeleteKeyValueStore
Tags: TagResource, UntagResource, ListTagsForResource
"""
@@ -38,31 +40,35 @@ NS = "http://cloudfront.amazonaws.com/doc/2020-05-31/"
# ---------------------------------------------------------------------------
# Path regexes — note: _DIST_CFG_RE must be matched before _DIST_ID_RE
# ---------------------------------------------------------------------------
-_DIST_RE = re.compile(r"^/2020-05-31/distribution/?$")
+_DIST_RE = re.compile(r"^/2020-05-31/distribution/?$")
_DIST_CFG_RE = re.compile(r"^/2020-05-31/distribution/([^/]+)/config$")
-_DIST_ID_RE = re.compile(r"^/2020-05-31/distribution/([^/]+)/?$")
-_INV_RE = re.compile(r"^/2020-05-31/distribution/([^/]+)/invalidation/?$")
-_INV_ID_RE = re.compile(r"^/2020-05-31/distribution/([^/]+)/invalidation/([^/]+)$")
-_TAG_RE = re.compile(r"^/2020-05-31/tagging/?$")
+_DIST_ID_RE = re.compile(r"^/2020-05-31/distribution/([^/]+)/?$")
+_INV_RE = re.compile(r"^/2020-05-31/distribution/([^/]+)/invalidation/?$")
+_INV_ID_RE = re.compile(r"^/2020-05-31/distribution/([^/]+)/invalidation/([^/]+)$")
+_TAG_RE = re.compile(r"^/2020-05-31/tagging/?$")
# OAC path regexes — note: _OAC_CFG_RE must be matched before _OAC_ID_RE
-_OAC_RE = re.compile(r"^/2020-05-31/origin-access-control/?$")
-_OAC_CFG_RE = re.compile(r"^/2020-05-31/origin-access-control/([^/]+)/config$")
-_OAC_ID_RE = re.compile(r"^/2020-05-31/origin-access-control/([^/]+)/?$")
+_OAC_RE = re.compile(r"^/2020-05-31/origin-access-control/?$")
+_OAC_CFG_RE = re.compile(r"^/2020-05-31/origin-access-control/([^/]+)/config$")
+_OAC_ID_RE = re.compile(r"^/2020-05-31/origin-access-control/([^/]+)/?$")
-_FUN_LIST_RE = re.compile(r"^/2020-05-31/function/?$")
+_FUN_LIST_RE = re.compile(r"^/2020-05-31/function/?$")
_FUN_DESCRIBE_RE = re.compile(r"^/2020-05-31/function/([^/]+)/describe/?$")
-_FUN_PUBLISH_RE = re.compile(r"^/2020-05-31/function/([^/]+)/publish/?$")
-_FUN_NAME_RE = re.compile(r"^/2020-05-31/function/([^/]+)/?$")
+_FUN_PUBLISH_RE = re.compile(r"^/2020-05-31/function/([^/]+)/publish/?$")
+_FUN_NAME_RE = re.compile(r"^/2020-05-31/function/([^/]+)/?$")
+
+_KVS_LIST_RE = re.compile(r"^/2020-05-31/key-value-store/?$")
+_KVS_NAME_RE = re.compile(r"^/2020-05-31/key-value-store/([^/]+)/?$")
# ---------------------------------------------------------------------------
# In-memory state
# ---------------------------------------------------------------------------
-_distributions = AccountScopedDict() # Id -> distribution record
-_invalidations = AccountScopedDict() # distribution_id -> [invalidation record, ...]
-_tags = AccountScopedDict() # arn -> [{"Key": ..., "Value": ...}]
-_oacs = AccountScopedDict() # Id -> OAC record
-_functions = AccountScopedDict() # Name -> function record (CloudFront Functions API)
+_distributions = AccountScopedDict() # Id -> distribution record
+_invalidations = AccountScopedDict() # distribution_id -> [invalidation record, ...]
+_tags = AccountScopedDict() # arn -> [{"Key": ..., "Value": ...}]
+_oacs = AccountScopedDict() # Id -> OAC record
+_functions = AccountScopedDict() # Name -> function record (CloudFront Functions API)
+_kvstores = AccountScopedDict() # Name -> KVS record
def reset():
@@ -71,16 +77,20 @@ def reset():
_tags.clear()
_oacs.clear()
_functions.clear()
+ _kvstores.clear()
def get_state():
- return copy.deepcopy({
- "distributions": _distributions,
- "invalidations": _invalidations,
- "tags": _tags,
- "oacs": _oacs,
- "functions": _functions,
- })
+ return copy.deepcopy(
+ {
+ "distributions": _distributions,
+ "invalidations": _invalidations,
+ "tags": _tags,
+ "oacs": _oacs,
+ "functions": _functions,
+ "kvstores": _kvstores,
+ }
+ )
def restore_state(data):
@@ -89,6 +99,7 @@ def restore_state(data):
_tags.update(data.get("tags", {}))
_oacs.update(data.get("oacs", {}))
_functions.update(data.get("functions", {}))
+ _kvstores.update(data.get("kvstores", {}))
try:
@@ -97,9 +108,8 @@ try:
restore_state(_restored)
except Exception:
import logging
- logging.getLogger(__name__).exception(
- "Failed to restore persisted state; continuing with fresh store"
- )
+
+ logging.getLogger(__name__).exception("Failed to restore persisted state; continuing with fresh store")
# ---------------------------------------------------------------------------
@@ -124,6 +134,7 @@ def _now_iso() -> str:
# XML helpers
# ---------------------------------------------------------------------------
+
def _xml_response(root_tag: str, builder_fn, status: int = 200, extra_headers: dict = None) -> tuple:
root = Element(root_tag, xmlns=NS)
builder_fn(root)
@@ -319,13 +330,21 @@ def _func_arn(name: str) -> str:
return f"arn:aws:cloudfront::{get_account_id()}:function/{name}"
+def _kvs_arn(name: str) -> str:
+ return f"arn:aws:cloudfront::{get_account_id()}:key-value-store/{name}"
+
+
def _function_summary_builder(fn: dict, stage: str, status: str, last_modified: str):
def build(root):
fc = SubElement(root, "FunctionConfig")
SubElement(fc, "Comment").text = fn.get("comment", "")
+ kvs_arns = fn.get("kvs_arns", [])
kvs = SubElement(fc, "KeyValueStoreAssociations")
- SubElement(kvs, "Quantity").text = "0"
- SubElement(kvs, "Items")
+ SubElement(kvs, "Quantity").text = str(len(kvs_arns))
+ items_el = SubElement(kvs, "Items")
+ for arn in kvs_arns:
+ assoc = SubElement(items_el, "KeyValueStoreAssociation")
+ SubElement(assoc, "KeyValueStoreARN").text = arn
SubElement(fc, "Runtime").text = fn["runtime"]
md = SubElement(root, "FunctionMetadata")
SubElement(md, "CreatedTime").text = fn["created"]
@@ -345,7 +364,17 @@ def _cf_parse_function_config(cfg_el):
runtime = _text(cfg_el, "Runtime")
if not runtime:
return None, _error("InvalidArgument", "Runtime is required.", 400)
- return {"comment": comment, "runtime": runtime}, None
+ kvs_arns = []
+ kvs_el = _find(cfg_el, "KeyValueStoreAssociations")
+ if kvs_el is not None:
+ items_el = _find(kvs_el, "Items")
+ if items_el is not None:
+ for child in items_el:
+ if _local_tag_name(child) == "KeyValueStoreAssociation":
+ arn = _text(child, "KeyValueStoreARN")
+ if arn:
+ kvs_arns.append(arn)
+ return {"comment": comment, "runtime": runtime, "kvs_arns": kvs_arns}, None
def _cf_decode_function_code(code_b64: str):
@@ -382,6 +411,7 @@ def _cf_create_function(headers, body):
"arn": _func_arn(name),
"comment": cfg["comment"],
"runtime": cfg["runtime"],
+ "kvs_arns": cfg["kvs_arns"],
"code": code,
"created": now,
"last_modified_dev": now,
@@ -410,9 +440,7 @@ def _cf_list_functions(query_params):
if stage_filter in ("", "DEVELOPMENT"):
summaries.append((fn, "DEVELOPMENT", "UNPUBLISHED", fn["last_modified_dev"]))
if stage_filter in ("", "LIVE") and fn["live_etag"]:
- summaries.append(
- (fn, "LIVE", "DEPLOYED", fn["last_modified_live"] or fn["last_modified_dev"])
- )
+ summaries.append((fn, "LIVE", "DEPLOYED", fn["last_modified_live"] or fn["last_modified_dev"]))
def build(root):
SubElement(root, "MaxItems").text = "100"
@@ -478,7 +506,11 @@ def _cf_publish_function(name: str, headers):
if not if_match:
return _error("InvalidIfMatchVersion", "The If-Match version is missing or not valid for the resource.", 400)
if if_match != fn["dev_etag"]:
- return _error("PreconditionFailed", "The precondition given in one or more of the request-header fields evaluated to false.", 412)
+ return _error(
+ "PreconditionFailed",
+ "The precondition given in one or more of the request-header fields evaluated to false.",
+ 412,
+ )
now = _now_iso()
fn["live_etag"] = new_uuid()
@@ -501,7 +533,11 @@ def _cf_update_function(name: str, headers, body):
if not if_match:
return _error("InvalidIfMatchVersion", "The If-Match version is missing or not valid for the resource.", 400)
if if_match != fn["dev_etag"]:
- return _error("PreconditionFailed", "The precondition given in one or more of the request-header fields evaluated to false.", 412)
+ return _error(
+ "PreconditionFailed",
+ "The precondition given in one or more of the request-header fields evaluated to false.",
+ 412,
+ )
el = _parse_body(body)
if el is None:
@@ -517,6 +553,7 @@ def _cf_update_function(name: str, headers, body):
now = _now_iso()
fn["comment"] = cfg["comment"]
fn["runtime"] = cfg["runtime"]
+ fn["kvs_arns"] = cfg["kvs_arns"]
fn["code"] = code
fn["last_modified_dev"] = now
fn["dev_etag"] = new_uuid()
@@ -539,7 +576,11 @@ def _cf_delete_function(name: str, headers):
if not if_match:
return _error("InvalidIfMatchVersion", "The If-Match version is missing or not valid for the resource.", 400)
if if_match != fn["dev_etag"]:
- return _error("PreconditionFailed", "The precondition given in one or more of the request-header fields evaluated to false.", 412)
+ return _error(
+ "PreconditionFailed",
+ "The precondition given in one or more of the request-header fields evaluated to false.",
+ 412,
+ )
del _functions[name]
logger.info("DeleteFunction name=%s", name)
@@ -550,6 +591,7 @@ def _cf_delete_function(name: str, headers):
# Request dispatcher
# ---------------------------------------------------------------------------
+
async def handle_request(method, path, headers, body, query_params):
logger.debug("%s %s", method, path)
@@ -593,8 +635,16 @@ async def handle_request(method, path, headers, body, query_params):
m = _TAG_RE.match(path)
if m:
- resource = query_params.get("Resource", [""])[0] if isinstance(query_params.get("Resource"), list) else query_params.get("Resource", "")
- operation = query_params.get("Operation", [""])[0] if isinstance(query_params.get("Operation"), list) else query_params.get("Operation", "")
+ resource = (
+ query_params.get("Resource", [""])[0]
+ if isinstance(query_params.get("Resource"), list)
+ else query_params.get("Resource", "")
+ )
+ operation = (
+ query_params.get("Operation", [""])[0]
+ if isinstance(query_params.get("Operation"), list)
+ else query_params.get("Operation", "")
+ )
if method == "GET":
return _list_tags(resource)
if method == "POST" and operation == "Tag":
@@ -662,6 +712,24 @@ async def handle_request(method, path, headers, body, query_params):
if method == "GET":
return _cf_list_functions(query_params)
+ # KeyValueStore routes
+ m = _KVS_NAME_RE.match(path)
+ if m:
+ kvs_name = m.group(1)
+ if method == "GET":
+ return _describe_kvs(kvs_name)
+ if method == "PUT":
+ return _update_kvs(kvs_name, headers, body)
+ if method == "DELETE":
+ return _delete_kvs(kvs_name, headers)
+
+ m = _KVS_LIST_RE.match(path)
+ if m:
+ if method == "POST":
+ return _create_kvs(headers, body)
+ if method == "GET":
+ return _list_kvstores(query_params)
+
return _error("NoSuchResource", f"No route for {method} {path}", 404)
@@ -669,6 +737,7 @@ async def handle_request(method, path, headers, body, query_params):
# Distribution handlers
# ---------------------------------------------------------------------------
+
def _create_distribution(headers, body):
root_el = _parse_body(body)
config_el, tags_el = _unwrap_distribution_create_xml(root_el)
@@ -681,8 +750,10 @@ def _create_distribution(headers, body):
# CallerReference idempotency — return existing distribution if CallerReference matches
for existing in _distributions.values():
if existing.get("CallerReference") == caller_ref:
+
def build(root, _dist=existing):
_build_distribution_xml(root, _dist)
+
return _xml_response("Distribution", build, status=200, extra_headers={"ETag": existing["ETag"]})
if _find(config_el, "Origins") is None:
return _error("InvalidArgument", "Origins is required.", 400)
@@ -714,10 +785,15 @@ def _create_distribution(headers, body):
def build(root):
_build_distribution_xml(root, dist)
- return _xml_response("Distribution", build, status=201, extra_headers={
- "ETag": etag,
- "Location": f"/2020-05-31/distribution/{dist_id}",
- })
+ return _xml_response(
+ "Distribution",
+ build,
+ status=201,
+ extra_headers={
+ "ETag": etag,
+ "Location": f"/2020-05-31/distribution/{dist_id}",
+ },
+ )
def _get_distribution(dist_id):
@@ -776,7 +852,11 @@ def _update_distribution(dist_id, headers, body):
if not if_match:
return _error("InvalidIfMatchVersion", "The If-Match version is missing or not valid for the resource.", 400)
if if_match != dist["ETag"]:
- return _error("PreconditionFailed", "The precondition given in one or more of the request-header fields evaluated to false.", 412)
+ return _error(
+ "PreconditionFailed",
+ "The precondition given in one or more of the request-header fields evaluated to false.",
+ 412,
+ )
config_el = _parse_body(body)
if config_el is None:
@@ -805,10 +885,16 @@ def _delete_distribution(dist_id, headers):
if not if_match:
return _error("InvalidIfMatchVersion", "The If-Match version is missing or not valid for the resource.", 400)
if if_match != dist["ETag"]:
- return _error("PreconditionFailed", "The precondition given in one or more of the request-header fields evaluated to false.", 412)
+ return _error(
+ "PreconditionFailed",
+ "The precondition given in one or more of the request-header fields evaluated to false.",
+ 412,
+ )
if dist["enabled"]:
- return _error("DistributionNotDisabled", "The distribution you are trying to delete has not been disabled.", 409)
+ return _error(
+ "DistributionNotDisabled", "The distribution you are trying to delete has not been disabled.", 409
+ )
del _distributions[dist_id]
_invalidations.pop(dist_id, None)
@@ -821,6 +907,7 @@ def _delete_distribution(dist_id, headers):
# Invalidation handlers
# ---------------------------------------------------------------------------
+
def _create_invalidation(dist_id, body):
if dist_id not in _distributions:
return _error("NoSuchDistribution", "The specified distribution does not exist.", 404)
@@ -858,9 +945,14 @@ def _create_invalidation(dist_id, body):
def build(root):
_build_invalidation_xml(root, inv)
- return _xml_response("Invalidation", build, status=201, extra_headers={
- "Location": f"/2020-05-31/distribution/{dist_id}/invalidation/{inv_id}",
- })
+ return _xml_response(
+ "Invalidation",
+ build,
+ status=201,
+ extra_headers={
+ "Location": f"/2020-05-31/distribution/{dist_id}/invalidation/{inv_id}",
+ },
+ )
def _list_invalidations(dist_id):
@@ -904,6 +996,7 @@ def _get_invalidation(dist_id, inv_id):
# Tagging
# ---------------------------------------------------------------------------
+
def _list_tags(resource_arn):
tags = _tags.get(resource_arn, [])
root = Element("Tags", xmlns=NS)
@@ -951,6 +1044,7 @@ def _untag_resource(resource_arn, body):
# OAC handlers
# ---------------------------------------------------------------------------
+
def _create_oac(headers, body):
el = _parse_body(body)
if el is None:
@@ -990,10 +1084,15 @@ def _create_oac(headers, body):
def build(root):
_build_oac_xml(root, oac)
- return _xml_response("OriginAccessControl", build, status=201, extra_headers={
- "ETag": etag,
- "Location": f"/2020-05-31/origin-access-control/{oac_id}",
- })
+ return _xml_response(
+ "OriginAccessControl",
+ build,
+ status=201,
+ extra_headers={
+ "ETag": etag,
+ "Location": f"/2020-05-31/origin-access-control/{oac_id}",
+ },
+ )
def _get_oac(oac_id):
@@ -1049,7 +1148,11 @@ def _update_oac(oac_id, headers, body):
if not if_match:
return _error("InvalidIfMatchVersion", "The If-Match version is missing or not valid for the resource.", 400)
if if_match != oac["ETag"]:
- return _error("PreconditionFailed", "The precondition given in one or more of the request-header fields evaluated to false.", 412)
+ return _error(
+ "PreconditionFailed",
+ "The precondition given in one or more of the request-header fields evaluated to false.",
+ 412,
+ )
el = _parse_body(body)
if el is None:
@@ -1095,9 +1198,168 @@ def _delete_oac(oac_id, headers):
if not if_match:
return _error("InvalidIfMatchVersion", "The If-Match version is missing or not valid for the resource.", 400)
if if_match != oac["ETag"]:
- return _error("PreconditionFailed", "The precondition given in one or more of the request-header fields evaluated to false.", 412)
+ return _error(
+ "PreconditionFailed",
+ "The precondition given in one or more of the request-header fields evaluated to false.",
+ 412,
+ )
del _oacs[oac_id]
logger.info("DeleteOriginAccessControl id=%s", oac_id)
return 204, {}, b""
+
+
+# ---------------------------------------------------------------------------
+# KeyValueStore handlers
+# ---------------------------------------------------------------------------
+
+_KVS_NAME_RE_VALIDATE = re.compile(r"^[a-zA-Z0-9\-_]{1,64}$")
+
+
+def _build_kvs_xml(parent, kvs):
+ SubElement(parent, "ARN").text = kvs["ARN"]
+ SubElement(parent, "Comment").text = kvs.get("Comment", "")
+ SubElement(parent, "Id").text = kvs["Id"]
+ SubElement(parent, "LastModifiedTime").text = kvs["LastModifiedTime"]
+ SubElement(parent, "Name").text = kvs["Name"]
+ SubElement(parent, "Status").text = kvs.get("Status", "READY")
+
+
+def _create_kvs(headers, body):
+ el = _parse_body(body)
+ if el is None:
+ return _error("MalformedXML", "The XML document is malformed.", 400)
+
+ name = _text(el, "Name")
+ if not name:
+ return _error("InvalidArgument", "Name is required.", 400)
+ if not _KVS_NAME_RE_VALIDATE.match(name):
+ return _error("InvalidArgument", "Name must match pattern [a-zA-Z0-9-_]{1,64}.", 400)
+ if name in _kvstores:
+ return _error("EntityAlreadyExists", f"A key value store with name {name} already exists.", 409)
+
+ comment = _text(el, "Comment")
+ kvs_id = new_uuid()
+ etag = new_uuid()
+ now = _now_iso()
+ arn = _kvs_arn(name)
+
+ kvs = {
+ "Id": kvs_id,
+ "Name": name,
+ "Comment": comment,
+ "ARN": arn,
+ "Status": "READY",
+ "LastModifiedTime": now,
+ "ETag": etag,
+ }
+ _kvstores[name] = kvs
+
+ tags_el = _find(el, "Tags")
+ if tags_el is not None:
+ _ingest_distribution_tags_from_xml(arn, tags_el)
+
+ logger.info("CreateKeyValueStore name=%s id=%s", name, kvs_id)
+
+ def build(root):
+ _build_kvs_xml(root, kvs)
+
+ return _xml_response(
+ "KeyValueStore",
+ build,
+ status=201,
+ extra_headers={
+ "ETag": etag,
+ "Location": f"/2020-05-31/key-value-store/{name}",
+ },
+ )
+
+
+def _describe_kvs(name):
+ kvs = _kvstores.get(name)
+ if not kvs:
+ return _error("EntityNotFound", f"The key value store {name} was not found.", 404)
+
+ def build(root):
+ _build_kvs_xml(root, kvs)
+
+ return _xml_response("KeyValueStore", build, extra_headers={"ETag": kvs["ETag"]})
+
+
+def _list_kvstores(query_params):
+ max_items = int(_qval(query_params, "MaxItems", "100") or "100")
+ items = list(_kvstores.values())[:max_items]
+
+ def build(root):
+ items_el = SubElement(root, "Items")
+ for kvs in items:
+ kvs_el = SubElement(items_el, "KeyValueStore")
+ _build_kvs_xml(kvs_el, kvs)
+ SubElement(root, "MaxItems").text = str(max_items)
+ SubElement(root, "Quantity").text = str(len(items))
+
+ return _xml_response("KeyValueStoreList", build)
+
+
+def _update_kvs(name, headers, body):
+ kvs = _kvstores.get(name)
+ if not kvs:
+ return _error("EntityNotFound", f"The key value store {name} was not found.", 404)
+
+ 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 != kvs["ETag"]:
+ return _error(
+ "PreconditionFailed",
+ "The precondition given in one or more of the request-header fields evaluated to false.",
+ 412,
+ )
+
+ el = _parse_body(body)
+ if el is None:
+ return _error("MalformedXML", "The XML document is malformed.", 400)
+
+ comment = _text(el, "Comment")
+ new_etag = new_uuid()
+ kvs["Comment"] = comment
+ kvs["ETag"] = new_etag
+ kvs["LastModifiedTime"] = _now_iso()
+
+ logger.info("UpdateKeyValueStore name=%s", name)
+
+ def build(root):
+ _build_kvs_xml(root, kvs)
+
+ return _xml_response("KeyValueStore", build, extra_headers={"ETag": new_etag})
+
+
+def _delete_kvs(name, headers):
+ kvs = _kvstores.get(name)
+ if not kvs:
+ return _error("EntityNotFound", f"The key value store {name} was not found.", 404)
+
+ 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 != kvs["ETag"]:
+ return _error(
+ "PreconditionFailed",
+ "The precondition given in one or more of the request-header fields evaluated to false.",
+ 412,
+ )
+
+ arn = kvs["ARN"]
+ for fn in _functions.values():
+ if arn in fn.get("kvs_arns", []):
+ return _error(
+ "CannotDeleteEntityWhileInUse",
+ "The key value store is associated with a function and cannot be deleted.",
+ 409,
+ )
+
+ del _kvstores[name]
+
+ logger.info("DeleteKeyValueStore name=%s", name)
+ return 204, {}, b""
diff --git a/tests/test_cloudfront.py b/tests/test_cloudfront.py
index 3e5cc25c..c4775cab 100644
--- a/tests/test_cloudfront.py
+++ b/tests/test_cloudfront.py
@@ -36,6 +36,7 @@ _CF_DIST_CONFIG = {
"Enabled": True,
}
+
def test_cloudfront_create_distribution(cloudfront):
resp = cloudfront.create_distribution(DistributionConfig=_CF_DIST_CONFIG)
dist = resp["Distribution"]
@@ -68,6 +69,7 @@ def test_cloudfront_create_distribution_with_tags(cloudfront):
upd = cloudfront.update_distribution(DistributionConfig=disabled_cfg, Id=dist_id, IfMatch=etag)
cloudfront.delete_distribution(Id=dist_id, IfMatch=upd["ETag"])
+
def test_cloudfront_list_distributions(cloudfront):
cfg_a = {**_CF_DIST_CONFIG, "CallerReference": "cf-list-a", "Comment": "list-a"}
cfg_b = {**_CF_DIST_CONFIG, "CallerReference": "cf-list-b", "Comment": "list-b"}
@@ -78,6 +80,7 @@ def test_cloudfront_list_distributions(cloudfront):
ids = [d["Id"] for d in dist_list.get("Items", [])]
assert len(ids) >= 2
+
def test_cloudfront_get_distribution(cloudfront):
cfg = {**_CF_DIST_CONFIG, "CallerReference": "cf-get-1", "Comment": "get-test"}
create_resp = cloudfront.create_distribution(DistributionConfig=cfg)
@@ -91,6 +94,7 @@ def test_cloudfront_get_distribution(cloudfront):
# terraform-provider-aws v6+ dereferences OriginGroups without a nil check
assert dist["DistributionConfig"]["OriginGroups"]["Quantity"] == 0
+
def test_cloudfront_get_distribution_config(cloudfront):
cfg = {**_CF_DIST_CONFIG, "CallerReference": "cf-getcfg-1", "Comment": "getcfg-test"}
create_resp = cloudfront.create_distribution(DistributionConfig=cfg)
@@ -102,6 +106,7 @@ def test_cloudfront_get_distribution_config(cloudfront):
assert resp["DistributionConfig"]["Comment"] == "getcfg-test"
assert resp["DistributionConfig"]["OriginGroups"]["Quantity"] == 0
+
def test_cloudfront_update_distribution(cloudfront):
cfg = {**_CF_DIST_CONFIG, "CallerReference": "cf-upd-1", "Comment": "before-update"}
create_resp = cloudfront.create_distribution(DistributionConfig=cfg)
@@ -116,17 +121,17 @@ def test_cloudfront_update_distribution(cloudfront):
get_resp = cloudfront.get_distribution_config(Id=dist_id)
assert get_resp["DistributionConfig"]["Comment"] == "after-update"
+
def test_cloudfront_update_distribution_etag_mismatch(cloudfront):
cfg = {**_CF_DIST_CONFIG, "CallerReference": "cf-etag-mismatch", "Comment": "mismatch-test"}
create_resp = cloudfront.create_distribution(DistributionConfig=cfg)
dist_id = create_resp["Distribution"]["Id"]
with pytest.raises(ClientError) as exc:
- cloudfront.update_distribution(
- DistributionConfig=cfg, Id=dist_id, IfMatch="wrong-etag-value"
- )
+ cloudfront.update_distribution(DistributionConfig=cfg, Id=dist_id, IfMatch="wrong-etag-value")
assert exc.value.response["Error"]["Code"] == "PreconditionFailed"
+
def test_cloudfront_delete_distribution(cloudfront):
cfg = {**_CF_DIST_CONFIG, "CallerReference": "cf-del-1", "Comment": "delete-test", "Enabled": True}
create_resp = cloudfront.create_distribution(DistributionConfig=cfg)
@@ -144,6 +149,7 @@ def test_cloudfront_delete_distribution(cloudfront):
cloudfront.get_distribution(Id=dist_id)
assert exc.value.response["Error"]["Code"] == "NoSuchDistribution"
+
def test_cloudfront_delete_enabled_distribution(cloudfront):
cfg = {**_CF_DIST_CONFIG, "CallerReference": "cf-del-enabled", "Comment": "del-enabled-test", "Enabled": True}
create_resp = cloudfront.create_distribution(DistributionConfig=cfg)
@@ -154,11 +160,13 @@ def test_cloudfront_delete_enabled_distribution(cloudfront):
cloudfront.delete_distribution(Id=dist_id, IfMatch=etag)
assert exc.value.response["Error"]["Code"] == "DistributionNotDisabled"
+
def test_cloudfront_get_nonexistent(cloudfront):
with pytest.raises(ClientError) as exc:
cloudfront.get_distribution(Id="ENONEXISTENT1234")
assert exc.value.response["Error"]["Code"] == "NoSuchDistribution"
+
def test_cloudfront_create_invalidation(cloudfront):
cfg = {**_CF_DIST_CONFIG, "CallerReference": "cf-inv-1", "Comment": "inv-test"}
create_resp = cloudfront.create_distribution(DistributionConfig=cfg)
@@ -176,6 +184,7 @@ def test_cloudfront_create_invalidation(cloudfront):
assert inv["Status"] == "Completed"
assert inv_resp["ResponseMetadata"]["HTTPStatusCode"] == 201
+
def test_cloudfront_list_invalidations(cloudfront):
cfg = {**_CF_DIST_CONFIG, "CallerReference": "cf-listinv-1", "Comment": "listinv-test"}
create_resp = cloudfront.create_distribution(DistributionConfig=cfg)
@@ -195,6 +204,7 @@ def test_cloudfront_list_invalidations(cloudfront):
assert inv_list["Quantity"] == 2
assert len(inv_list["Items"]) == 2
+
def test_cloudfront_get_invalidation(cloudfront):
cfg = {**_CF_DIST_CONFIG, "CallerReference": "cf-getinv-1", "Comment": "getinv-test"}
create_resp = cloudfront.create_distribution(DistributionConfig=cfg)
@@ -215,29 +225,36 @@ def test_cloudfront_get_invalidation(cloudfront):
assert inv["Status"] == "Completed"
assert "/getinv-path" in inv["InvalidationBatch"]["Paths"]["Items"]
+
def test_cloudfront_tags(cloudfront):
"""TagResource / ListTagsForResource / UntagResource for CloudFront distributions."""
resp = cloudfront.create_distribution(
DistributionConfig={
"CallerReference": "tag-test-v42",
- "Origins": {"Items": [{"Id": "o1", "DomainName": "example.com",
- "S3OriginConfig": {"OriginAccessIdentity": ""}}], "Quantity": 1},
+ "Origins": {
+ "Items": [{"Id": "o1", "DomainName": "example.com", "S3OriginConfig": {"OriginAccessIdentity": ""}}],
+ "Quantity": 1,
+ },
"DefaultCacheBehavior": {
- "TargetOriginId": "o1", "ViewerProtocolPolicy": "allow-all",
+ "TargetOriginId": "o1",
+ "ViewerProtocolPolicy": "allow-all",
"ForwardedValues": {"QueryString": False, "Cookies": {"Forward": "none"}},
"MinTTL": 0,
},
- "Comment": "tag test", "Enabled": True,
+ "Comment": "tag test",
+ "Enabled": True,
}
)
dist_arn = resp["Distribution"]["ARN"]
cloudfront.tag_resource(
Resource=dist_arn,
- Tags={"Items": [
- {"Key": "env", "Value": "test"},
- {"Key": "team", "Value": "platform"},
- ]},
+ Tags={
+ "Items": [
+ {"Key": "env", "Value": "test"},
+ {"Key": "team", "Value": "platform"},
+ ]
+ },
)
tags = cloudfront.list_tags_for_resource(Resource=dist_arn)
@@ -260,6 +277,7 @@ def test_cloudfront_tags(cloudfront):
# OAC happy-path integration tests
# ---------------------------------------------------------------------------
+
def _oac_config(name, description="", origin_type="s3", signing_behavior="always", signing_protocol="sigv4"):
"""Helper to build an OAC config dict for boto3."""
return {
@@ -660,3 +678,154 @@ def test_cloudfront_sdk_compat_injects_origin_groups():
og = cf._find(el, "OriginGroups")
assert og is not None
assert cf._text(og, "Quantity") == "0"
+
+
+# ---------------------------------------------------------------------------
+# KeyValueStore tests
+# ---------------------------------------------------------------------------
+
+
+def test_kvs_create_and_describe(cloudfront):
+ resp = cloudfront.create_key_value_store(Name="test-kvs-1", Comment="test comment")
+ kvs = resp["KeyValueStore"]
+ assert kvs["Name"] == "test-kvs-1"
+ assert kvs["Comment"] == "test comment"
+ assert kvs["Status"] == "READY"
+ assert "Id" in kvs
+ assert kvs["ARN"].endswith(":key-value-store/test-kvs-1")
+ assert "LastModifiedTime" in kvs
+ etag = resp["ETag"]
+ assert etag
+
+ desc = cloudfront.describe_key_value_store(Name="test-kvs-1")
+ assert desc["KeyValueStore"]["Name"] == "test-kvs-1"
+ assert desc["KeyValueStore"]["Id"] == kvs["Id"]
+ assert desc["ETag"] == etag
+
+
+def test_kvs_list(cloudfront):
+ name_a = f"kvs-list-a-{_uuid_mod.uuid4().hex[:8]}"
+ name_b = f"kvs-list-b-{_uuid_mod.uuid4().hex[:8]}"
+ cloudfront.create_key_value_store(Name=name_a, Comment="a")
+ cloudfront.create_key_value_store(Name=name_b, Comment="b")
+
+ resp = cloudfront.list_key_value_stores()
+ names = [item["Name"] for item in resp["KeyValueStoreList"]["Items"]]
+ assert name_a in names
+ assert name_b in names
+ assert resp["KeyValueStoreList"]["Quantity"] >= 2
+
+
+def test_kvs_update_comment(cloudfront):
+ name = f"kvs-update-{_uuid_mod.uuid4().hex[:8]}"
+ create_resp = cloudfront.create_key_value_store(Name=name, Comment="old")
+ etag = create_resp["ETag"]
+
+ update_resp = cloudfront.update_key_value_store(Name=name, Comment="new comment", IfMatch=etag)
+ assert update_resp["KeyValueStore"]["Comment"] == "new comment"
+ new_etag = update_resp["ETag"]
+ assert new_etag != etag
+
+ desc = cloudfront.describe_key_value_store(Name=name)
+ assert desc["KeyValueStore"]["Comment"] == "new comment"
+ assert desc["ETag"] == new_etag
+
+
+def test_kvs_delete(cloudfront):
+ name = f"kvs-delete-{_uuid_mod.uuid4().hex[:8]}"
+ create_resp = cloudfront.create_key_value_store(Name=name, Comment="to delete")
+ etag = create_resp["ETag"]
+
+ cloudfront.delete_key_value_store(Name=name, IfMatch=etag)
+
+ with pytest.raises(ClientError) as exc:
+ cloudfront.describe_key_value_store(Name=name)
+ assert exc.value.response["Error"]["Code"] == "EntityNotFound"
+
+
+def test_kvs_duplicate_name(cloudfront):
+ name = f"kvs-dup-{_uuid_mod.uuid4().hex[:8]}"
+ cloudfront.create_key_value_store(Name=name, Comment="first")
+
+ with pytest.raises(ClientError) as exc:
+ cloudfront.create_key_value_store(Name=name, Comment="second")
+ assert exc.value.response["Error"]["Code"] == "EntityAlreadyExists"
+ assert exc.value.response["ResponseMetadata"]["HTTPStatusCode"] == 409
+
+
+def test_kvs_describe_nonexistent(cloudfront):
+ with pytest.raises(ClientError) as exc:
+ cloudfront.describe_key_value_store(Name="nonexistent-kvs")
+ assert exc.value.response["Error"]["Code"] == "EntityNotFound"
+ assert exc.value.response["ResponseMetadata"]["HTTPStatusCode"] == 404
+
+
+def test_kvs_delete_etag_mismatch(cloudfront):
+ name = f"kvs-del-etag-{_uuid_mod.uuid4().hex[:8]}"
+ cloudfront.create_key_value_store(Name=name, Comment="test")
+
+ with pytest.raises(ClientError) as exc:
+ cloudfront.delete_key_value_store(Name=name, IfMatch="wrong-etag")
+ assert exc.value.response["Error"]["Code"] == "PreconditionFailed"
+ assert exc.value.response["ResponseMetadata"]["HTTPStatusCode"] == 412
+
+
+def test_kvs_update_etag_mismatch(cloudfront):
+ name = f"kvs-upd-etag-{_uuid_mod.uuid4().hex[:8]}"
+ cloudfront.create_key_value_store(Name=name, Comment="test")
+
+ with pytest.raises(ClientError) as exc:
+ cloudfront.update_key_value_store(Name=name, Comment="new", IfMatch="wrong-etag")
+ assert exc.value.response["Error"]["Code"] == "PreconditionFailed"
+ assert exc.value.response["ResponseMetadata"]["HTTPStatusCode"] == 412
+
+
+def test_kvs_function_association(cloudfront):
+ kvs_name = f"kvs-assoc-{_uuid_mod.uuid4().hex[:8]}"
+ kvs_resp = cloudfront.create_key_value_store(Name=kvs_name, Comment="for function")
+ kvs_arn = kvs_resp["KeyValueStore"]["ARN"]
+
+ func_name = f"fn-kvs-{_uuid_mod.uuid4().hex[:8]}"
+ cloudfront.create_function(
+ Name=func_name,
+ FunctionConfig={
+ "Comment": "with kvs",
+ "Runtime": "cloudfront-js-2.0",
+ "KeyValueStoreAssociations": {
+ "Quantity": 1,
+ "Items": [{"KeyValueStoreARN": kvs_arn}],
+ },
+ },
+ FunctionCode=b"function handler(event) { return event.response; }",
+ )
+
+ desc = cloudfront.describe_function(Name=func_name, Stage="DEVELOPMENT")
+ kvs_assocs = desc["FunctionSummary"]["FunctionConfig"]["KeyValueStoreAssociations"]
+ assert kvs_assocs["Quantity"] == 1
+ assert kvs_assocs["Items"][0]["KeyValueStoreARN"] == kvs_arn
+
+
+def test_kvs_delete_in_use(cloudfront):
+ kvs_name = f"kvs-inuse-{_uuid_mod.uuid4().hex[:8]}"
+ kvs_resp = cloudfront.create_key_value_store(Name=kvs_name, Comment="in use")
+ kvs_arn = kvs_resp["KeyValueStore"]["ARN"]
+ kvs_etag = kvs_resp["ETag"]
+
+ func_name = f"fn-inuse-{_uuid_mod.uuid4().hex[:8]}"
+ cloudfront.create_function(
+ Name=func_name,
+ FunctionConfig={
+ "Comment": "uses kvs",
+ "Runtime": "cloudfront-js-2.0",
+ "KeyValueStoreAssociations": {
+ "Quantity": 1,
+ "Items": [{"KeyValueStoreARN": kvs_arn}],
+ },
+ },
+ FunctionCode=b"function handler(event) { return event.response; }",
+ )
+
+ with pytest.raises(ClientError) as exc:
+ cloudfront.delete_key_value_store(Name=kvs_name, IfMatch=kvs_etag)
+ assert exc.value.response["Error"]["Code"] == "CannotDeleteEntityWhileInUse"
+ assert exc.value.response["ResponseMetadata"]["HTTPStatusCode"] == 409