Skip to content

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

fix(apigw): create api key (#2103)

Nahuel Nuceradebb2ca13441
diff --git a/ministack/services/apigateway_v1.py b/ministack/services/apigateway_v1.py
index 600f7cad..3df0cc68 100644
--- a/ministack/services/apigateway_v1.py
+++ b/ministack/services/apigateway_v1.py
@@ -2920,15 +2920,23 @@ def _parse_service_integration_uri(uri: str):
return region, service, verb, rest
-# ── Minimal request-template (VTL) subset ────────────────────────────────
+# ── Request-template (VTL) subset ────────────────────────────────────────
#
# Non-proxy AWS service integrations shape the backend call through a
-# `requestTemplates` mapping template. Only the subset real SQS templates use
-# is modeled: `$input.body`, `$input.json('<json-path>')` / `$input.path(...)`,
-# `$input.params('name')` (merged header < querystring < path params, the
-# precedence AWS documents), and `$util.urlEncode(...)` — e.g.
-# `Action=SendMessage&MessageBody=$util.urlEncode($input.body)`.
-# Anything the subset does not cover renders as an empty string.
+# `requestTemplates` mapping template. Modeled: `$input.body`,
+# `$input.json('<json-path>')` / `$input.path(...)`, `$input.params('name')`
+# (path > querystring > header, the precedence AWS documents),
+# `$util.urlEncode(...)`, `$context.authorizer.*` (Cognito claims included),
+# `#set` with Velocity 1.x newline gobbling, `##` / `#* *#` comments, quiet
+# `$!` references, and string literals ("..." interpolated with `""` escapes,
+# '...' as-is with `''` escapes). An unmodeled `$input` / `$util` / `$context`
+# reference renders as an empty string; any other `$` text passes through.
+
+_VTL_ROOTS = ("input", "util", "context")
+_VTL_IDENT = re.compile(r"[A-Za-z][A-Za-z0-9_]*")
+_VTL_SET = re.compile(r"#\{?set\}?[ \t]*\(")
+_VTL_SET_LHS = re.compile(r"\s*\$!?\{?([A-Za-z][A-Za-z0-9_]*)\}?\s*=(.*)", re.DOTALL)
+
def _vtl_json_path(body_str: str, path: str):
"""Evaluate the small ``$input.json('<path>')`` subset: ``$``, ``$.a.b``,
@@ -2954,75 +2962,258 @@ def _vtl_json_path(body_str: str, path: str):
return node
-def _vtl_eval(expr: str, body_str: str, params_map: dict):
- """Evaluate one template reference; unrecognised references render ""."""
- expr = expr.strip()
- if expr.startswith("$util.urlEncode(") and expr.endswith(")"):
- inner = _vtl_eval(expr[len("$util.urlEncode("):-1], body_str, params_map)
- return urllib.parse.quote(str(inner), safe="")
- if expr == "$input.body":
- return body_str
- m = re.fullmatch(r"\$input\.(?:json|path)\(\s*(['\"])(.*?)\1\s*\)",
- expr, re.DOTALL)
- if m:
- value = _vtl_json_path(body_str, m.group(2))
- if isinstance(value, (dict, list)):
- return json.dumps(value, separators=(",", ":"), ensure_ascii=False)
- return "" if value is None else str(value)
- m = re.fullmatch(r"\$input\.params\(\s*(['\"])(.*?)\1\s*\)",
- expr, re.DOTALL)
- if m:
- return str(params_map.get(m.group(2), ""))
- if len(expr) >= 2 and expr[0] == expr[-1] and expr[0] in ("'", '"'):
- return expr[1:-1]
- return ""
-
-
-def _vtl_expr_end(template: str, start: int) -> int:
- """Index just past the ``$...`` expression at ``start`` in ``template``.
-
- Bare references end at the first non-identifier/dot character; parenthesised
- calls end at the closing paren, skipping over quoted strings.
- """
- i = start + 1
+def _vtl_close_paren(text: str, start: int):
+ """Index of the ``)`` matching the ``(`` at ``start``, skipping quoted
+ strings (a doubled quote pairs off as two adjacent strings); None if open."""
depth = 0
- while i < len(template):
- ch = template[i]
+ i = start
+ while i < len(text):
+ ch = text[i]
if ch in ("'", '"'):
- quote = ch
- i += 1
- while i < len(template) and template[i] != quote:
- i += 1
- elif ch == "(":
+ close = text.find(ch, i + 1)
+ if close < 0:
+ return None
+ i = close + 1
+ continue
+ if ch == "(":
depth += 1
elif ch == ")":
- if depth == 0:
- return i
depth -= 1
if depth == 0:
- return i + 1
- elif depth == 0 and not (ch.isalnum() or ch in "._"):
- return i
+ return i
i += 1
- return i
+ return None
-def _render_request_template_v1(template: str, body_str: str,
- params_map: dict) -> str:
- """Render the VTL subset; bare text passes through unchanged."""
+def _vtl_split_args(src: str) -> list:
+ args, depth, quote, current = [], 0, None, []
+ for ch in src:
+ if quote:
+ if ch == quote:
+ quote = None
+ elif ch in ("'", '"'):
+ quote = ch
+ elif ch == "(":
+ depth += 1
+ elif ch == ")":
+ depth -= 1
+ elif ch == "," and depth == 0:
+ args.append("".join(current))
+ current = []
+ continue
+ current.append(ch)
+ if "".join(current).strip():
+ args.append("".join(current))
+ return args
+
+
+def _vtl_parse_ref(text: str, i: int):
+ """Parse the reference starting at ``text[i] == "$"``.
+
+ Returns ``(end, quiet, root, steps)`` where each step is ``(name, args)``
+ and ``args`` is the raw argument source of a call or None for a property;
+ None when no identifier follows the ``$``.
+ """
+ j = i + 1
+ quiet = text.startswith("!", j)
+ if quiet:
+ j += 1
+ braced = text.startswith("{", j)
+ if braced:
+ j += 1
+ m = _VTL_IDENT.match(text, j)
+ if not m:
+ return None
+ root, j = m.group(0), m.end()
+ steps = []
+ while text.startswith(".", j):
+ m = _VTL_IDENT.match(text, j + 1)
+ if not m:
+ break
+ name, k = m.group(0), m.end()
+ if text.startswith("(", k):
+ close = _vtl_close_paren(text, k)
+ if close is None:
+ break
+ steps.append((name, text[k + 1:close]))
+ j = close + 1
+ else:
+ steps.append((name, None))
+ j = k
+ if braced:
+ if not text.startswith("}", j):
+ return None
+ j += 1
+ return j, quiet, root, steps
+
+
+def _vtl_str(value) -> str:
+ if value is None:
+ return ""
+ if isinstance(value, bool):
+ return "true" if value else "false"
+ if isinstance(value, (dict, list)):
+ return json.dumps(value, separators=(",", ":"), ensure_ascii=False)
+ return str(value)
+
+
+def _vtl_value(expr: str, st: dict):
+ """Evaluate an argument or ``#set`` right-hand side."""
+ expr = expr.strip()
+ if len(expr) >= 2 and expr[0] == expr[-1] == '"':
+ gobbled = st["gobbled"]
+ value = _vtl_render(expr[1:-1].replace('""', '"'), st)
+ st["gobbled"] = gobbled
+ return value
+ if len(expr) >= 2 and expr[0] == expr[-1] == "'":
+ return expr[1:-1].replace("''", "'")
+ if re.fullmatch(r"-?\d+", expr):
+ return int(expr)
+ if expr in ("true", "false"):
+ return expr == "true"
+ if expr.startswith("$"):
+ ref = _vtl_parse_ref(expr, 0)
+ if ref is not None and ref[0] == len(expr):
+ return _vtl_resolve(ref, st)
+ return None
+
+
+def _vtl_walk(node, steps, st):
+ for name, args in steps:
+ if not isinstance(node, dict):
+ return None
+ if args is None:
+ node = node.get(name)
+ elif name == "get":
+ arg = _vtl_split_args(args)
+ key = _vtl_value(arg[0], st) if arg else None
+ node = node.get(key) if isinstance(key, str) else None
+ else:
+ return None
+ if node is None:
+ return None
+ return node
+
+
+def _vtl_input(steps, st):
+ if not steps:
+ return None
+ (name, args), rest = steps[0], steps[1:]
+ if name == "body" and args is None:
+ value = st["body"]
+ elif name in ("json", "path") and args is not None:
+ arg = _vtl_split_args(args)
+ node = _vtl_json_path(st["body"], str(_vtl_value(arg[0], st)) if arg else "$")
+ if name == "path":
+ value = node
+ elif isinstance(node, (dict, list)):
+ value = json.dumps(node, separators=(",", ":"), ensure_ascii=False)
+ else:
+ value = "" if node is None else str(node)
+ elif name == "params":
+ arg = _vtl_split_args(args) if args is not None else []
+ if not arg:
+ value = dict(st["params"])
+ else:
+ value = str(st["params"].get(_vtl_str(_vtl_value(arg[0], st)), ""))
+ else:
+ return None
+ return _vtl_walk(value, rest, st) if rest else value
+
+
+def _vtl_util(steps, st):
+ if not steps:
+ return None
+ name, args = steps[0]
+ if name == "urlEncode" and args is not None:
+ arg = _vtl_split_args(args)
+ text = _vtl_str(_vtl_value(arg[0], st)) if arg else ""
+ # java.net.URLEncoder: application/x-www-form-urlencoded, as AWS documents.
+ return urllib.parse.quote_plus(text, safe="*").replace("~", "%7E")
+ return None
+
+
+def _vtl_resolve(ref, st):
+ _end, _quiet, root, steps = ref
+ if root == "input":
+ return _vtl_input(steps, st)
+ if root == "util":
+ return _vtl_util(steps, st)
+ if root == "context":
+ value = _vtl_walk(st["context"], steps, st)
+ # AWS: "Calling $context.authorizer.claims returns null."
+ return None if isinstance(value, dict) else value
+ if root in st["vars"]:
+ return _vtl_walk(st["vars"][root], steps, st) if steps else st["vars"][root]
+ return None
+
+
+def _vtl_set(inner: str, st: dict) -> None:
+ m = _VTL_SET_LHS.fullmatch(inner)
+ if not m:
+ return
+ value = _vtl_value(m.group(2), st)
+ if value is not None: # Velocity leaves the variable as it was on null
+ st["vars"][m.group(1)] = value
+
+
+def _vtl_render(text: str, st: dict) -> str:
out = []
i = 0
- while i < len(template):
- if template[i] != "$" or not template[i + 1:].startswith(("input", "util")):
- out.append(template[i])
- i += 1
- continue
- end = _vtl_expr_end(template, i)
- out.append(str(_vtl_eval(template[i:end], body_str, params_map)))
- i = end
+ while i < len(text):
+ ch = text[i]
+ if ch == "#":
+ if text.startswith("##", i):
+ newline = text.find("\n", i)
+ i = len(text) if newline < 0 else newline + 1
+ continue
+ if text.startswith("#*", i):
+ close = text.find("*#", i + 2)
+ i = len(text) if close < 0 else close + 2
+ continue
+ m = _VTL_SET.match(text, i)
+ close = _vtl_close_paren(text, m.end() - 1) if m else None
+ if close is not None:
+ if st["gobbled"]:
+ while out and out[-1] in (" ", "\t"):
+ out.pop()
+ _vtl_set(text[m.end():close], st)
+ i = close + 1
+ k = i
+ while k < len(text) and text[k] in " \t":
+ k += 1
+ if text.startswith("\r\n", k):
+ i, st["gobbled"] = k + 2, True
+ elif text.startswith("\n", k):
+ i, st["gobbled"] = k + 1, True
+ continue
+ elif ch == "$":
+ ref = _vtl_parse_ref(text, i)
+ if ref is not None and (ref[2] in _VTL_ROOTS or ref[2] in st["vars"]):
+ value = _vtl_str(_vtl_resolve(ref, st))
+ out.append(value)
+ if value:
+ st["gobbled"] = False
+ i = ref[0]
+ continue
+ if ref is not None and ref[1]:
+ i = ref[0]
+ continue
+ out.append(ch)
+ if ch not in (" ", "\t"):
+ st["gobbled"] = False
+ i += 1
return "".join(out)
+def _render_request_template_v1(template: str, body_str: str,
+ params_map: dict, context=None) -> str:
+ """Render the VTL subset; bare text passes through unchanged."""
+ st = {"vars": {}, "context": context or {}, "body": body_str,
+ "params": params_map, "gobbled": False}
+ return _vtl_render(template, st)
+
+
def _select_service_integration_response(integration, backend_status: int):
"""Pick the integration response for a non-proxy service call.
@@ -3047,7 +3238,7 @@ def _select_service_integration_response(integration, backend_status: int):
def _service_integration_response(integration, backend_status: int,
backend_text: str, accept: str,
- request_params: dict):
+ request_params: dict, context=None):
"""Map a backend service result through the integration responses."""
# No match and no default answers 500, configured responses or not.
selected = _select_service_integration_response(integration, backend_status)
@@ -3072,7 +3263,8 @@ def _service_integration_response(integration, backend_status: int,
if template is None and templates:
template = next(iter(templates.values()))
if template is not None:
- body_out = _render_request_template_v1(template, backend_text, request_params)
+ body_out = _render_request_template_v1(
+ template, backend_text, request_params, context)
return status, resp_headers, body_out.encode()
return status, resp_headers, backend_text.encode()
@@ -3141,7 +3333,8 @@ async def _invoke_sqs_v1(
if key.split(";", 1)[0].strip().lower() == content_type:
template = value
break
- rendered = (_render_request_template_v1(template, body_str, params_map)
+ vtl_context = {"authorizer": authorizer_context} if authorizer_context else {}
+ rendered = (_render_request_template_v1(template, body_str, params_map, vtl_context)
if template is not None else body_str)
form = dict(urllib.parse.parse_qsl(rendered, keep_blank_values=True))
action = form.get("Action", "")
@@ -3154,7 +3347,8 @@ async def _invoke_sqs_v1(
"MissingAction", "Missing Action parameter", 400)
backend_text = xbody.decode("utf-8", errors="replace")
return _service_integration_response(
- integration, backend_status, backend_text, accept, params_map)
+ integration, backend_status, backend_text, accept, params_map,
+ vtl_context)
qurl = (_sqs_svc._queue_name_to_url.get_scoped(account, region, queue_name)
or _sqs_svc._queue_url_for_account(account, queue_name))
@@ -3187,7 +3381,8 @@ async def _invoke_sqs_v1(
return (504, {"Content-Type": "application/json"},
json.dumps({"message": "Internal server error"}).encode())
return _service_integration_response(
- integration, backend_status, backend_text, accept, params_map)
+ integration, backend_status, backend_text, accept, params_map,
+ vtl_context)
async def _invoke_http_proxy_v1(integration, path, method, headers, body, query_params, path_params=None):
@@ -4666,8 +4861,13 @@ def _delete_gateway_response(api_id, response_type):
# ---- Control plane: API Keys ----
def _create_api_key(data):
+ key_value = data.get("value")
+ if key_value is None:
+ key_value = new_uuid().replace("-", "")
+ elif len(str(key_value)) < 20:
+ return _v1_error("BadRequestException",
+ "API Key value should be at least 20 characters", 400)
key_id = _new_id()[:8]
- key_value = new_uuid().replace("-", "")
api_key = {
"id": key_id,
"name": data.get("name", ""),
@@ -4679,6 +4879,8 @@ def _create_api_key(data):
"stageKeys": data.get("stageKeys", []),
"tags": data.get("tags", {}),
}
+ if data.get("customerId"):
+ api_key["customerId"] = data["customerId"]
_api_keys[key_id] = api_key
return _v1_response(api_key, 201)
diff --git a/ministack/services/cloudformation/provisioners.py b/ministack/services/cloudformation/provisioners.py
index bb84d605..cf416caf 100644
--- a/ministack/services/cloudformation/provisioners.py
+++ b/ministack/services/cloudformation/provisioners.py
@@ -5153,9 +5153,7 @@ def _apigw_api_key_create(logical_id, props, stack_name):
"""Provision an ``AWS::ApiGateway::ApiKey``.
Ref returns the generated key id; ``Fn::GetAtt APIKeyId`` returns the same
- id, matching the AWS CloudFormation resource contract. The runtime create
- always mints a fresh value, so an explicit ``Value`` is applied afterwards
- to honor a caller-pinned key.
+ id, matching the AWS CloudFormation resource contract.
"""
data = {
"name": props.get("Name") or _physical_name(stack_name, logical_id),
@@ -5166,22 +5164,13 @@ def _apigw_api_key_create(logical_id, props, stack_name):
for sk in props.get("StageKeys", [])
],
"tags": _tag_map(props.get("Tags")),
+ "value": props.get("Value"),
+ "customerId": props.get("CustomerId"),
}
status, _headers, body = _apigw_v1._create_api_key(data)
if status >= 400:
raise ValueError(f"AWS::ApiGateway::ApiKey create failed: {body!r}")
- api_key = json.loads(body)
- key_id = api_key.get("id", "")
- value = props.get("Value")
- if value:
- stored = _apigw_v1._api_keys.get(key_id)
- if stored is not None:
- stored["value"] = value
- customer_id = props.get("CustomerId")
- if customer_id:
- stored = _apigw_v1._api_keys.get(key_id)
- if stored is not None:
- stored["customerId"] = customer_id
+ key_id = json.loads(body).get("id", "")
return key_id, {"APIKeyId": key_id}
diff --git a/tests/test_apigatewayv1.py b/tests/test_apigatewayv1.py
index 7eedb7df..a9a3a349 100644
--- a/tests/test_apigatewayv1.py
+++ b/tests/test_apigatewayv1.py
@@ -4937,6 +4937,47 @@ def test_apigwv1_api_key_required_refuses_a_missing_or_unknown_key(apigw_v1):
apigw_v1.delete_rest_api(restApiId=api_id)
+def test_apigwv1_create_api_key_keeps_the_given_value(apigw_v1):
+ wanted = f"own-key-value-{_uuid_mod.uuid4().hex[:12]}"
+ api_id, root = _gw_api(apigw_v1, f"gwkey-{_uuid_mod.uuid4().hex[:8]}")
+ key = apigw_v1.create_api_key(name=f"k-{_uuid_mod.uuid4().hex[:6]}", enabled=True,
+ value=wanted, customerId="customer-1")
+ try:
+ assert key["value"] == wanted
+ assert key["customerId"] == "customer-1"
+ stored = apigw_v1.get_api_key(apiKey=key["id"], includeValue=True)
+ assert stored["value"] == wanted
+ assert stored["customerId"] == "customer-1"
+
+ resource = apigw_v1.create_resource(restApiId=api_id, parentId=root,
+ pathPart="key")["id"]
+ _mock_method(apigw_v1, api_id, resource, "GET", apiKeyRequired=True)
+ apigw_v1.create_deployment(restApiId=api_id, stageName="p")
+ plan = apigw_v1.create_usage_plan(
+ name=f"p-{_uuid_mod.uuid4().hex[:6]}",
+ apiStages=[{"apiId": api_id, "stage": "p"}])["id"]
+ apigw_v1.create_usage_plan_key(usagePlanId=plan, keyId=key["id"],
+ keyType="API_KEY")
+ assert _stage_call(api_id, "/key",
+ headers=[("x-api-key", wanted)]) == (200, '{"ok":true}')
+ finally:
+ apigw_v1.delete_rest_api(restApiId=api_id)
+ apigw_v1.delete_api_key(apiKey=key["id"])
+
+
+def test_apigwv1_create_api_key_refuses_a_value_under_20_characters(apigw_v1):
+ with pytest.raises(ClientError) as exc:
+ apigw_v1.create_api_key(name=f"k-{_uuid_mod.uuid4().hex[:6]}",
+ value="x" * 19)
+ err = exc.value.response
+ assert err["Error"]["Code"] == "BadRequestException"
+ assert err["Error"]["Message"] == "API Key value should be at least 20 characters"
+ assert err["ResponseMetadata"]["HTTPStatusCode"] == 400
+ key = apigw_v1.create_api_key(name=f"k-{_uuid_mod.uuid4().hex[:6]}", value="x" * 20)
+ assert key["value"] == "x" * 20
+ apigw_v1.delete_api_key(apiKey=key["id"])
+
+
def test_apigwv1_usage_plan_quota_is_enforced_per_key(apigw_v1):
"""Quota 1 serves one request, then 429 QUOTA_EXCEEDED."""
api_id, root = _gw_api(apigw_v1, f"gwquota-{_uuid_mod.uuid4().hex[:8]}")
@@ -5418,6 +5459,117 @@ def test_apigwv1_execute_sqs_integration_vtl_subset(apigw_v1, sqs):
sqs.delete_queue(QueueUrl=qurl)
+def test_apigwv1_execute_sqs_integration_cognito_claims_template(apigw_v1, sqs, cognito_idp):
+ """#set, interpolated "" strings and $context.authorizer.claims, as in the
+ template CDK users pass through an AwsIntegration to SQS."""
+ suffix = _uuid_mod.uuid4().hex[:8]
+ queue_name = f"apigw-q-{suffix}"
+ qurl = sqs.create_queue(QueueName=queue_name)["QueueUrl"]
+ pool_id = cognito_idp.create_user_pool(PoolName=f"sqs-claims-{suffix}")["UserPool"]["Id"]
+ client_id = cognito_idp.create_user_pool_client(
+ UserPoolId=pool_id, ClientName="c",
+ ExplicitAuthFlows=["ALLOW_USER_PASSWORD_AUTH"])["UserPoolClient"]["ClientId"]
+ cognito_idp.admin_create_user(
+ UserPoolId=pool_id, Username="test-user", MessageAction="SUPPRESS",
+ UserAttributes=[{"Name": "email", "Value": "[email protected]"}])
+ cognito_idp.admin_set_user_password(
+ UserPoolId=pool_id, Username="test-user", Password="Passw0rd!", Permanent=True)
+ token = cognito_idp.initiate_auth(
+ ClientId=client_id, AuthFlow="USER_PASSWORD_AUTH",
+ AuthParameters={"USERNAME": "test-user", "PASSWORD": "Passw0rd!"},
+ )["AuthenticationResult"]["IdToken"]
+
+ template = (
+ "#set($body = $input.json('$'))\n"
+ "#set($id = $input.params('id'))\n"
+ "Action=SendMessage&MessageBody=$util.urlEncode(\"{\"\"body\"\":$body,"
+ "\"\"pathParameters\"\":{\"\"id\"\":\"\"$id\"\"},\"\"requestContext\"\":"
+ "{\"\"authorizer\"\":{\"\"claims\"\":{\"\"cognito:username\"\":"
+ "\"\"$!context.authorizer.claims.get('cognito:username')\"\",\"\"email\"\":"
+ "\"\"$!context.authorizer.claims.get('email')\"\"}}}}\")")
+ api_id, root = _gw_api(apigw_v1, f"gwsqs-{suffix}")
+ items = apigw_v1.create_resource(restApiId=api_id, parentId=root, pathPart="items")["id"]
+ resource = apigw_v1.create_resource(restApiId=api_id, parentId=items, pathPart="{id}")["id"]
+ authorizer = apigw_v1.create_authorizer(
+ restApiId=api_id, name="cog", type="COGNITO_USER_POOLS",
+ providerARNs=[f"arn:aws:cognito-idp:us-east-1:000000000000:userpool/{pool_id}"],
+ identitySource="method.request.header.Authorization")["id"]
+ apigw_v1.put_method(restApiId=api_id, resourceId=resource, httpMethod="PUT",
+ authorizationType="COGNITO_USER_POOLS", authorizerId=authorizer)
+ apigw_v1.put_integration(
+ restApiId=api_id, resourceId=resource, httpMethod="PUT", type="AWS",
+ integrationHttpMethod="POST",
+ uri=f"arn:aws:apigateway:us-east-1:sqs:path/000000000000/{queue_name}",
+ credentials="arn:aws:iam::000000000000:role/apigw-sqs",
+ requestParameters={"integration.request.header.Content-Type":
+ "'application/x-www-form-urlencoded'"},
+ requestTemplates={"application/json": template})
+ for code in ("200", "400"):
+ apigw_v1.put_method_response(restApiId=api_id, resourceId=resource,
+ httpMethod="PUT", statusCode=code)
+ apigw_v1.put_integration_response(
+ restApiId=api_id, resourceId=resource, httpMethod="PUT", statusCode="200",
+ responseTemplates={"application/json": '{"message": "Request accepted"}'})
+ apigw_v1.put_integration_response(
+ restApiId=api_id, resourceId=resource, httpMethod="PUT", statusCode="400",
+ selectionPattern=r"4\d{2}",
+ responseTemplates={"application/json": '{"message": "Bad Request"}'})
+ apigw_v1.create_deployment(restApiId=api_id, stageName="p")
+ try:
+ status, text = _stage_call(
+ api_id, "/items/item-1", method="PUT", body='{"name":"example"}',
+ headers=[("Content-Type", "application/json"), ("Authorization", token)])
+ assert (status, json.loads(text)) == (200, {"message": "Request accepted"})
+ msgs = sqs.receive_message(QueueUrl=qurl, WaitTimeSeconds=1).get("Messages", [])
+ assert [m["Body"] for m in msgs] == [
+ '{"body":{"name":"example"},"pathParameters":{"id":"item-1"},'
+ '"requestContext":{"authorizer":{"claims":{"cognito:username":"test-user",'
+ '"email":"[email protected]"}}}}']
+ finally:
+ apigw_v1.delete_rest_api(restApiId=api_id)
+ sqs.delete_queue(QueueUrl=qurl)
+ cognito_idp.delete_user_pool(UserPoolId=pool_id)
+
+
+def _render_v1(template, body="", params=None, context=None):
+ return apigateway_v1._render_request_template_v1(template, body, params or {}, context)
+
+
+def test_apigwv1_template_set_lines_render_nothing():
+ assert _render_v1("#set($a = 'x')\n #set ($b = \"$a/y\")\r\nA=$a&B=$b") == "A=x&B=x/y"
+
+
+def test_apigwv1_template_comments_strip_through_end_of_line():
+ assert _render_v1("Action=SendMessage##\n&MessageBody=x## tail\n#* block\n*#&k=v") == (
+ "Action=SendMessage&MessageBody=x&k=v")
+
+
+def test_apigwv1_template_string_literals():
+ template = "#set($v = \"in\")$util.urlEncode(\"a \"\"$v\"\" b\")|$util.urlEncode('$v ''q''')"
+ assert _render_v1(template) == "a+%22in%22+b|%24v+%27q%27"
+
+
+def test_apigwv1_template_set_null_keeps_the_previous_value():
+ ctx = {"authorizer": {"claims": {}}}
+ assert _render_v1("#set($a = 'kept')#set($a = $context.authorizer.claims.get('nope'))$a",
+ context=ctx) == "kept"
+
+
+def test_apigwv1_template_undefined_references():
+ assert _render_v1("$undefined|$!undefined|$!{undefined}|$10") == "$undefined|||$10"
+
+
+def test_apigwv1_template_context_authorizer_values():
+ claims_ctx = {"authorizer": {"claims": {"email": "[email protected]", "cognito:username": "u"}}}
+ assert _render_v1(
+ "$context.authorizer.claims.email|$context.authorizer.claims.get('cognito:username')"
+ "|$!context.authorizer.claims.missing|$!context.authorizer.claims",
+ context=claims_ctx) == "[email protected]|u||"
+ lambda_ctx = {"authorizer": {"principalId": "p1", "numKey": "1"}}
+ assert _render_v1("$context.authorizer.principalId-$context.authorizer.numKey",
+ context=lambda_ctx) == "p1-1"
+
+
def test_apigwv1_execute_sqs_integration_passthrough(apigw_v1, sqs):
"""No matching request template: the body passes through as the Query
call itself (WHEN_NO_MATCH default)."""
diff --git a/tests/test_cfn.py b/tests/test_cfn.py
index 9047e08a..6569743c 100644
--- a/tests/test_cfn.py
+++ b/tests/test_cfn.py
@@ -15835,6 +15835,21 @@ def test_cfn_apigateway_api_key_lifecycle(cfn, apigw_v1):
pass
+def test_cfn_apigateway_api_key_short_value_fails_the_stack(cfn):
+ stack = f"cfn-api-key-short-{_uuid_mod.uuid4().hex[:8]}"
+ try:
+ cfn.create_stack(StackName=stack, TemplateBody=json.dumps({"Resources": {"Key": {
+ "Type": "AWS::ApiGateway::ApiKey", "Properties": {"Value": "x" * 19}}}}))
+ assert _wait_stack(cfn, stack)["StackStatus"] == "ROLLBACK_COMPLETE"
+ reasons = [e.get("ResourceStatusReason", "") for e in
+ cfn.describe_stack_events(StackName=stack)["StackEvents"]
+ if e["LogicalResourceId"] == "Key" and e["ResourceStatus"] == "CREATE_FAILED"]
+ assert len(reasons) == 1
+ assert "API Key value should be at least 20 characters" in reasons[0]
+ finally:
+ _delete_cfn_test_stack(cfn, stack)
+
+
def test_cfn_apigateway_usage_plan_and_key_lifecycle(cfn, apigw_v1):
"""UsagePlan and UsagePlanKey provision, expose ids, associate a key, and delete.