Skip to content

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

fix/aws parity fixes (#1573)

Nahuel Nucera89ca6e94d384
diff --git a/ministack/core/appsync_js.py b/ministack/core/appsync_js.py
index 662acbe9..ed49ff2d 100644
--- a/ministack/core/appsync_js.py
+++ b/ministack/core/appsync_js.py
@@ -5,10 +5,14 @@ Evaluating them needs a JavaScript engine, and the image already ships Node for
the `nodejs*` Lambda runtimes (`Dockerfile`, `core/lambda_runtime.py`), so this
reuses it rather than adding a dependency.
-One long-lived worker handles every evaluation over a JSON-line protocol, the
-same shape `lambda_runtime` uses: a request per line on stdin, a reply per line
-on stdout. Compiled modules are cached in the worker by the hash of their source,
-so a resolver is only compiled once however many times it runs.
+A pool of workers handles evaluations over a JSON-line protocol, the same
+shape `lambda_runtime` uses: a request per line on stdin, a reply per line on
+stdout, one evaluation in flight per worker. A free worker is leased per
+evaluation and one is spawned when none is free — never queued behind, which
+would recreate the re-entrancy deadlock `lambda_runtime` documents. Each
+worker caches compiled modules by the hash of their source, evaluations are
+bounded by a timeout that kills and respawns a stuck process, and resolver
+stderr is surfaced through the service logger.
Fidelity note: Node is more permissive than the real APPSYNC_JS sandbox, which
forbids `async`/`await`, `try`/`catch` and most globals. A resolver that runs
@@ -26,6 +30,17 @@ import threading
logger = logging.getLogger("appsync")
_NODE_BINARY = "node"
+# AppSync ends a request after 30 seconds, so no single evaluation may outlive
+# that. An infinite loop cannot be interrupted from inside Node (vm timeouts do
+# not cover async continuations); on expiry the process is killed and respawned.
+_EVAL_TIMEOUT = 30.0
+# Heap ceiling per worker (MB), so a runaway resolver is contained rather than
+# taking the host's memory with it.
+_MAX_OLD_SPACE_MB = 256
+# Evaluations a worker serves before its process is recycled.
+_RECYCLE_AFTER = 1000
+# Idle workers kept warm beyond the first; a burst's surplus is folded on release.
+_MAX_IDLE = 2
# stdout carries the protocol, so anything the resolver logs goes to stderr.
_WORKER_SCRIPT = r"""
@@ -503,46 +518,91 @@ class AppSyncJsError(Exception):
self.error_info = error_info
+class AppSyncJsTimeout(RuntimeError):
+ """An evaluation outlived its deadline; the worker process was killed."""
+
+
class _Worker:
- """One Node process, shared by every evaluation."""
+ """One Node process, running one evaluation at a time.
+
+ A worker holds no evaluation-independent state other than its compiled-
+ module cache, so any free worker can serve any resolver. Single-flight per
+ worker matters beyond throughput: the compiled module's mutable holder
+ (``state`` in the worker script) is per-process, so two evaluations of the
+ same resolver in one process would corrupt each other's appended errors.
+ """
def __init__(self):
self._proc = None
self._lock = threading.Lock()
+ self.in_use = False
+ self.evals = 0
def _ensure(self):
if self._proc is not None and self._proc.poll() is None:
return self._proc
self._proc = subprocess.Popen(
- [_NODE_BINARY, "-e", _WORKER_SCRIPT],
- stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL,
+ [_NODE_BINARY, f"--max-old-space-size={_MAX_OLD_SPACE_MB}",
+ "-e", _WORKER_SCRIPT],
+ stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
text=True, bufsize=1,
)
+ # Resolver console.log lands on stderr (stdout carries the protocol);
+ # surface it through the service logger rather than dropping it.
+ threading.Thread(target=_pump_stderr, args=(self._proc,), daemon=True).start()
return self._proc
- def evaluate(self, code, fn, ctx):
+ def _take_proc(self):
+ proc, self._proc = self._proc, None
+ return proc
+
+ def evaluate(self, code, fn, ctx, timeout=None):
"""Run `fn` (request/response) from `code` against `ctx`.
Returns (status, value, appended_errors, stash, skip_to) where status is
"ok", "earlyReturn" or "missing". Raises AppSyncJsError on a resolver
- error, and RuntimeError when the worker cannot be used at all.
+ error, AppSyncJsTimeout when the evaluation outlives `timeout` (the
+ worker is killed — an infinite loop cannot be interrupted from inside
+ Node — and a fresh process spawns on the next call), and RuntimeError
+ when the worker cannot be used at all.
"""
+ timeout = _EVAL_TIMEOUT if timeout is None else timeout
payload = json.dumps({"code": code, "fn": fn, "ctx": ctx}) + "\n"
with self._lock:
+ self.evals += 1
try:
proc = self._ensure()
proc.stdin.write(payload)
proc.stdin.flush()
- line = proc.stdout.readline()
- except (BrokenPipeError, OSError) as exc:
- self._proc = None
- raise RuntimeError(f"APPSYNC_JS worker failed: {exc}") from exc
except FileNotFoundError as exc: # no node on PATH
raise RuntimeError(
"APPSYNC_JS resolvers need Node, which was not found") from exc
- if not line:
- self._proc = None
- raise RuntimeError("APPSYNC_JS worker closed unexpectedly")
+ except (BrokenPipeError, OSError) as exc:
+ self._proc = None
+ raise RuntimeError(f"APPSYNC_JS worker failed: {exc}") from exc
+
+ # readline on a thread so a stuck resolver is bounded by `timeout`
+ # rather than holding this worker forever — the same shape
+ # core/lambda_runtime.py uses for a handler that never returns.
+ box = []
+
+ def _read():
+ try:
+ box.append(proc.stdout.readline())
+ except Exception:
+ box.append("")
+
+ reader = threading.Thread(target=_read, daemon=True)
+ reader.start()
+ reader.join(timeout)
+ if reader.is_alive():
+ _terminate(self._take_proc())
+ raise AppSyncJsTimeout(
+ f"APPSYNC_JS evaluation exceeded {int(timeout)} seconds and was cancelled")
+ line = box[0] if box else ""
+ if not line:
+ self._proc = None
+ raise RuntimeError("APPSYNC_JS worker closed unexpectedly")
out = json.loads(line)
if out["status"] == "error":
@@ -554,24 +614,94 @@ class _Worker:
def shutdown(self):
with self._lock:
- if self._proc is not None:
- try:
- self._proc.terminate()
- except Exception:
- pass
- self._proc = None
+ _terminate(self._take_proc())
-_worker = _Worker()
+def _pump_stderr(proc):
+ try:
+ for line in proc.stderr:
+ line = line.rstrip("\n")
+ if line:
+ logger.info("[appsync-js] %s", line)
+ except Exception:
+ pass
-def evaluate(code, fn, ctx):
- return _worker.evaluate(code, fn, ctx)
+def _terminate(proc):
+ if proc is None:
+ return
+ try:
+ proc.terminate()
+ try:
+ proc.wait(timeout=2)
+ except subprocess.TimeoutExpired:
+ proc.kill()
+ proc.wait(timeout=2)
+ except Exception:
+ pass
+
+
+# ---------------------------------------------------------------------------
+# Worker pool
+#
+# Lease a free worker or spawn one; never queue behind a busy worker. Waiting
+# would recreate the re-entrancy deadlock core/lambda_runtime.py documents: a
+# resolver whose data source calls back into ministack needs a second worker
+# while its own request still holds the first. Concurrency is already bounded
+# upstream by the asyncio.to_thread pool the data plane runs on, so unbounded
+# spawn here cannot run away.
+# ---------------------------------------------------------------------------
+
+_pool_lock = threading.Lock()
+_pool: list = []
+
+
+def _acquire():
+ with _pool_lock:
+ for worker in _pool:
+ if not worker.in_use:
+ worker.in_use = True
+ return worker
+ worker = _Worker()
+ worker.in_use = True
+ _pool.append(worker)
+ return worker
+
+
+def _release(worker):
+ to_kill = None
+ with _pool_lock:
+ worker.in_use = False
+ if worker.evals >= _RECYCLE_AFTER:
+ # Recycling bounds whatever a long-lived Node process accumulates
+ # (compiled modules, heap fragmentation). The worker stays pooled
+ # and respawns lazily on its next lease.
+ worker.evals = 0
+ to_kill = worker._take_proc()
+ else:
+ idle = [w for w in _pool if not w.in_use]
+ if len(idle) > _MAX_IDLE and worker is not _pool[0]:
+ # A burst leaves surplus processes behind; keep a couple warm
+ # beyond the first and fold the rest.
+ _pool.remove(worker)
+ to_kill = worker._take_proc()
+ _terminate(to_kill)
+
+
+def evaluate(code, fn, ctx, timeout=None):
+ worker = _acquire()
+ try:
+ return worker.evaluate(code, fn, ctx, timeout=timeout)
+ finally:
+ _release(worker)
def reset():
- """Drop the worker, so a reset leaves no compiled-module cache behind."""
- _worker.shutdown()
+ """Drop every worker, so a reset leaves no compiled-module cache behind."""
+ with _pool_lock:
+ workers, _pool[:] = _pool[:], []
+ for worker in workers:
+ worker.shutdown()
def available():
diff --git a/ministack/services/appsync.py b/ministack/services/appsync.py
index 36dc3737..c08fedcf 100644
--- a/ministack/services/appsync.py
+++ b/ministack/services/appsync.py
@@ -24,6 +24,7 @@ import json
import logging
import os
import re
+import threading
import time
from ministack.core.arn import ArnParseError, parse_arn
@@ -58,7 +59,10 @@ _caches = AccountRegionScopedDict() # apiId -> ApiCache record
# apiId -> {cache key -> (expires_at, value)}. Separate from _caches, which holds
# the ApiCache configuration; this is the cached data itself. Not persisted: a
# restart is a cold cache, as replacing the cache instance would be on AWS.
+# Read and written from the worker threads resolver execution runs on, so every
+# access takes the lock — the expiry check-then-pop is not atomic without it.
_cache_entries: dict = {}
+_cache_entries_lock = threading.Lock()
_tags = AccountScopedDict() # resource_arn -> {key: value}
# ---------------------------------------------------------------------------
@@ -269,7 +273,8 @@ def _delete_graphql_api(api_id):
_caches.pop(api_id, None)
from ministack.core import appsync_graphql
appsync_graphql.forget_schema(api_id)
- _cache_entries.pop(api_id, None)
+ with _cache_entries_lock:
+ _cache_entries.pop(api_id, None)
_tags.pop(arn, None)
return _json(200, {})
@@ -583,23 +588,36 @@ def _get_schema_creation_status(api_id):
def _get_introspection_schema(api_id, query_params):
- """GetIntrospectionSchema — the response body is the schema blob itself."""
+ """GetIntrospectionSchema — the response body is the schema blob itself.
+
+ `format` is a required parameter with two values: SDL serves the stored
+ definition verbatim, JSON serves the introspection query result built from
+ it — the document `aws appsync get-introspection-schema --format JSON`
+ hands to codegen tooling.
+ """
if api_id not in _apis:
return error_response_json("NotFoundException", f"GraphQL API {api_id} not found", 404)
schema = _schemas.get(api_id)
if not schema:
return error_response_json("GraphQLSchemaException", "No schema found for this API", 404)
- fmt = (query_params.get("format") or ["SDL"])
- fmt = (fmt[0] if isinstance(fmt, list) else fmt or "SDL").upper()
+ fmt = query_params.get("format")
+ fmt = (fmt[0] if isinstance(fmt, list) else fmt or "").upper()
+ if not fmt:
+ return error_response_json("BadRequestException", "format is required", 400)
if fmt not in ("SDL", "JSON"):
return error_response_json("BadRequestException", f"Unsupported format: {fmt}", 400)
+
if fmt == "JSON":
- # A JSON introspection result requires a parsed type graph, which is
- # deliberately not built here; SDL is what tooling against MiniStack uses.
- return error_response_json(
- "BadRequestException",
- "JSON introspection is not supported by MiniStack; request format=SDL.", 400)
+ from graphql.utilities import introspection_from_schema
+
+ from ministack.core import appsync_graphql
+ try:
+ built = appsync_graphql.build_api_schema(api_id, schema["definition"])
+ except appsync_graphql.SchemaUnavailable as exc:
+ return error_response_json("GraphQLSchemaException", str(exc), 400)
+ document = introspection_from_schema(built)
+ return 200, {"Content-Type": "application/json"}, json.dumps(document).encode("utf-8")
return 200, {"Content-Type": "application/octet-stream"}, schema["definition"].encode("utf-8")
@@ -651,6 +669,8 @@ def _update_type(api_id, type_name, body):
return error_response_json("NotFoundException", f"GraphQL API {api_id} not found", 404)
if type_name not in _types.get(api_id, {}):
return error_response_json("NotFoundException", f"Type {type_name} not found", 404)
+ if not body.get("format"):
+ return error_response_json("BadRequestException", "format is required", 400)
body = dict(body)
body.setdefault("name", type_name)
return _create_type(api_id, body)
@@ -717,6 +737,8 @@ def _create_function(api_id, body):
name = body.get("name")
if not name:
return error_response_json("BadRequestException", "name is required", 400)
+ if not body.get("dataSourceName"):
+ return error_response_json("BadRequestException", "dataSourceName is required", 400)
function_id = new_uuid().replace("-", "")[:26]
record = _function_record(api_id, function_id, body)
@@ -744,6 +766,10 @@ def _update_function(api_id, function_id, body):
return error_response_json("NotFoundException", f"GraphQL API {api_id} not found", 404)
if function_id not in _functions.get(api_id, {}):
return error_response_json("NotFoundException", f"Function {function_id} not found", 404)
+ if not body.get("name"):
+ return error_response_json("BadRequestException", "name is required", 400)
+ if not body.get("dataSourceName"):
+ return error_response_json("BadRequestException", "dataSourceName is required", 400)
record = _function_record(api_id, function_id, body)
_functions[api_id][function_id] = record
return _json(200, {"functionConfiguration": record})
@@ -788,8 +814,46 @@ def _list_tags_for_resource(arn):
return _json(200, {"tags": tags})
+# Enum values from the AppSync API reference (CreateApiCache).
+_API_CACHING_BEHAVIORS = ("FULL_REQUEST_CACHING", "PER_RESOLVER_CACHING",
+ "OPERATION_LEVEL_CACHING")
+_API_CACHE_TYPES = ("T2_SMALL", "T2_MEDIUM", "R4_LARGE", "R4_XLARGE", "R4_2XLARGE",
+ "R4_4XLARGE", "R4_8XLARGE", "SMALL", "MEDIUM", "LARGE", "XLARGE",
+ "LARGE_2X", "LARGE_4X", "LARGE_8X", "LARGE_12X")
+_CACHE_HEALTH_METRICS = ("ENABLED", "DISABLED")
+
+
+def _validate_cache_request(data):
+ """ttl, apiCachingBehavior and type are required on create AND update, ttl
+ within 1-3600, enums closed — AWS refuses rather than defaulting. Returns an
+ error response, or None when the request is valid."""
+ for member in ("ttl", "apiCachingBehavior", "type"):
+ if data.get(member) is None:
+ return error_response_json("BadRequestException", f"{member} is required", 400)
+ try:
+ ttl = int(data["ttl"])
+ except (TypeError, ValueError):
+ return error_response_json("BadRequestException", "ttl must be a number", 400)
+ if not 1 <= ttl <= 3600:
+ return error_response_json(
+ "BadRequestException", "ttl must be between 1 and 3600 seconds", 400)
+ if data["apiCachingBehavior"] not in _API_CACHING_BEHAVIORS:
+ return error_response_json(
+ "BadRequestException",
+ f"Unknown apiCachingBehavior: {data['apiCachingBehavior']}", 400)
+ if data["type"] not in _API_CACHE_TYPES:
+ return error_response_json(
+ "BadRequestException", f"Unknown cache type: {data['type']}", 400)
+ if data.get("healthMetricsConfig") is not None \
+ and data["healthMetricsConfig"] not in _CACHE_HEALTH_METRICS:
+ return error_response_json(
+ "BadRequestException",
+ f"Unknown healthMetricsConfig: {data['healthMetricsConfig']}", 400)
+ return None
+
+
def _cache_record(data, existing=None):
- """Build an ApiCache from a create or update request.
+ """Build an ApiCache from a validated create or update request.
atRestEncryptionEnabled and transitEncryptionEnabled are set at create time
and cannot be changed afterwards, so an update carries them forward from the
@@ -797,11 +861,9 @@ def _cache_record(data, existing=None):
"""
base = existing or {}
return {
- "ttl": int(data.get("ttl", base.get("ttl", 0))),
- "apiCachingBehavior": data.get(
- "apiCachingBehavior", base.get("apiCachingBehavior", "FULL_REQUEST_CACHING")
- ),
- "type": data.get("type", base.get("type", "SMALL")),
+ "ttl": int(data["ttl"]),
+ "apiCachingBehavior": data["apiCachingBehavior"],
+ "type": data["type"],
"transitEncryptionEnabled": bool(
base.get("transitEncryptionEnabled",
data.get("transitEncryptionEnabled", False))
@@ -810,10 +872,8 @@ def _cache_record(data, existing=None):
base.get("atRestEncryptionEnabled",
data.get("atRestEncryptionEnabled", False))
),
- # No cache is actually kept — AppSync's cache is not observable through
- # the control plane, and emulating eviction would invent behaviour a
- # caller cannot verify. The record exists so the resource can be
- # created, read back and destroyed.
+ "healthMetricsConfig": data.get(
+ "healthMetricsConfig", base.get("healthMetricsConfig", "DISABLED")),
"status": "AVAILABLE",
}
@@ -824,6 +884,9 @@ def _create_api_cache(api_id, data):
if _caches.get(api_id) is not None:
return error_response_json(
"BadRequestException", f"Cache already exists for API {api_id}", 400)
+ invalid = _validate_cache_request(data)
+ if invalid:
+ return invalid
record = _cache_record(data)
_caches[api_id] = record
return _json(200, {"apiCache": record})
@@ -842,6 +905,9 @@ def _update_api_cache(api_id, data):
if existing is None:
return error_response_json(
"NotFoundException", f"Cache not found for API {api_id}", 404)
+ invalid = _validate_cache_request(data)
+ if invalid:
+ return invalid
record = _cache_record(data, existing)
_caches[api_id] = record
return _json(200, {"apiCache": record})
@@ -852,7 +918,8 @@ def _delete_api_cache(api_id):
return error_response_json(
"NotFoundException", f"Cache not found for API {api_id}", 404)
_caches.pop(api_id, None)
- _cache_entries.pop(api_id, None)
+ with _cache_entries_lock:
+ _cache_entries.pop(api_id, None)
return _json(200, {})
@@ -860,7 +927,8 @@ def _flush_api_cache(api_id):
if _caches.get(api_id) is None:
return error_response_json(
"NotFoundException", f"Cache not found for API {api_id}", 404)
- _cache_entries.pop(api_id, None)
+ with _cache_entries_lock:
+ _cache_entries.pop(api_id, None)
return _json(200, {})
@@ -902,6 +970,11 @@ def _evaluate_code(body):
return _json(200, {"error": {"message": str(exc),
"codeErrors": []},
"logs": []})
+ except appsync_js.AppSyncJsTimeout as exc:
+ # A stuck evaluation is a result of the code under test, not a fault of
+ # the service — report it in the response's error detail.
+ return _json(200, {"error": {"message": str(exc), "codeErrors": []},
+ "logs": []})
except RuntimeError as exc:
return error_response_json("InternalFailureException", str(exc), 500)
@@ -955,7 +1028,9 @@ async def handle_request(method, path, headers, body, query_params):
return _list_tags_for_resource(arn)
if path == "/v1/dataplane-evaluatecode" and method == "POST":
- return _evaluate_code(json.loads(body) if body else {})
+ # Evaluation blocks on the Node worker; keep it off the event loop for
+ # the same reason resolver execution runs on a thread.
+ return await asyncio.to_thread(_evaluate_code, json.loads(body) if body else {})
# GraphQL data plane: POST /graphql or POST /v1/apis/{apiId}/graphql
if path == "/graphql" and method == "POST":
@@ -1155,13 +1230,13 @@ def reset():
_functions.clear()
_schemas.clear()
_caches.clear()
- _cache_entries.clear()
- # Drop the JS worker so its compiled-module cache does not outlive a reset.
+ with _cache_entries_lock:
+ _cache_entries.clear()
+ # Drop the JS workers so their compiled-module cache does not outlive a
+ # reset, and the built-schema cache with them.
from ministack.core import appsync_graphql, appsync_js
appsync_js.reset()
appsync_graphql.forget_schema()
- from ministack.core import appsync_js
- appsync_js.reset()
_tags.clear()
@@ -1375,14 +1450,6 @@ def _unauthorized_response():
})
-# Off by default: ministack has no authentication of its own by design, and a
-# suite written against the permissive behaviour would start failing. Set
-# APPSYNC_ENFORCE_AUTH=1 to have an API refuse a caller that satisfies none of
-# its configured providers, the way AWS does — which is what makes an
-# authorization test meaningful.
-_ENFORCE_AUTH = os.environ.get("APPSYNC_ENFORCE_AUTH", "0") not in ("0", "", "false", "False")
-
-
def _auth_modes(api):
"""Every authentication type the API accepts."""
modes = {api.get("authenticationType") or "API_KEY"}
@@ -1491,9 +1558,11 @@ def _execute_graphql(api_id, data, request_headers=None):
if api_id not in _apis:
return _json(404, {"errors": [{"message": f"API {api_id} not found"}]})
- # Applied before either execution path, so it does not depend on whether the
- # API has a schema.
- if _ENFORCE_AUTH and not _request_is_authenticated(
+ # Applied before either execution path, so it does not depend on whether
+ # the API has a schema. AWS refuses a request that satisfies none of the
+ # API's configured auth providers; matching that is what makes an
+ # authorization test against ministack meaningful.
+ if not _request_is_authenticated(
api_id, _apis.get(api_id, {}), request_headers or {}):
return _unauthorized_response()
@@ -1688,18 +1757,20 @@ def _cache_key_for(api_id, resolver, field_name, args, identity, source):
def _cache_get(api_id, key):
- entry = _cache_entries.get(api_id, {}).get(key)
- if not entry:
- return None
- expires_at, value = entry
- if expires_at <= time.time():
- _cache_entries.get(api_id, {}).pop(key, None)
- return None
- return copy.deepcopy(value)
+ with _cache_entries_lock:
+ entry = _cache_entries.get(api_id, {}).get(key)
+ if not entry:
+ return None
+ expires_at, value = entry
+ if expires_at <= time.time():
+ _cache_entries.get(api_id, {}).pop(key, None)
+ return None
+ return copy.deepcopy(value)
def _cache_put(api_id, key, ttl, value):
- _cache_entries.setdefault(api_id, {})[key] = (time.time() + ttl, copy.deepcopy(value))
+ with _cache_entries_lock:
+ _cache_entries.setdefault(api_id, {})[key] = (time.time() + ttl, copy.deepcopy(value))
def _find_resolver(api_id, type_name, field_name):
@@ -1772,7 +1843,21 @@ def _ds_dynamodb(api_id, data_source, request_obj, ctx):
The non-JS path infers an operation from the field name because it has no
request object to read; here the resolver said what it wanted, so honour it.
+ Runs in the data source's declared region, as the non-JS path does — the
+ table lives where dynamodbConfig.awsRegion says, not where the request came
+ in.
"""
+ cfg = data_source.get("dynamodbConfig", {})
+ request_region = get_region()
+ data_source_region = cfg.get("awsRegion") or request_region
+ set_request_region(data_source_region)
+ try:
+ return _ds_dynamodb_in_region(api_id, data_source, request_obj, ctx)
+ finally:
+ set_request_region(request_region)
+
+
+def _ds_dynamodb_in_region(api_id, data_source, request_obj, ctx):
import ministack.services.dynamodb as _ddb
cfg = data_source.get("dynamodbConfig", {})
@@ -1809,18 +1894,36 @@ def _ddb_plain(value):
if isinstance(value, dict):
if len(value) == 1:
(tag, inner), = value.items()
- if tag in ("S", "N", "BOOL", "B"):
- return int(inner) if tag == "N" and str(inner).isdigit() else inner
+ if tag in ("S", "BOOL", "B"):
+ return inner
+ if tag == "N":
+ return _ddb_number(inner)
if tag == "NULL":
return None
if tag == "M":
return {k: _ddb_plain(v) for k, v in inner.items()}
if tag == "L":
return [_ddb_plain(v) for v in inner]
+ if tag in ("SS", "BS"):
+ return list(inner or [])
+ if tag == "NS":
+ return [_ddb_number(v) for v in (inner or [])]
return {k: _ddb_plain(v) for k, v in value.items()}
return value
+def _ddb_number(inner):
+ """DynamoDB's N is a string; floats and negatives are numbers too."""
+ text = str(inner)
+ try:
+ return int(text)
+ except (TypeError, ValueError):
+ try:
+ return float(text)
+ except (TypeError, ValueError):
+ return inner
+
+
def _ds_lambda(api_id, data_source, request_obj, ctx):
"""Invoke the Lambda with what the resolver's request() returned.
diff --git a/ministack/services/cloudformation/provisioners.py b/ministack/services/cloudformation/provisioners.py
index 57921ff5..7724fe84 100644
--- a/ministack/services/cloudformation/provisioners.py
+++ b/ministack/services/cloudformation/provisioners.py
@@ -3722,11 +3722,11 @@ def _appsync_api_create(logical_id, props, stack_name):
def _appsync_api_delete(physical_id, props):
- _appsync._apis.pop(physical_id, None)
- _appsync._api_keys.pop(physical_id, None)
- _appsync._data_sources.pop(physical_id, None)
- _appsync._resolvers.pop(physical_id, None)
- _appsync._types.pop(physical_id, None)
+ # Route through the service's own delete so every child store the API owns
+ # — functions, schema, cache, cache entries, tags, the built-schema cache —
+ # is released with it; popping a hand-kept list here is how the newer
+ # stores got leaked. A missing API is already the deleted outcome.
+ _appsync._delete_graphql_api(physical_id)
def _appsync_ds_create(logical_id, props, stack_name):
@@ -3817,6 +3817,16 @@ def _appsync_schema_create(logical_id, props, stack_name):
_appsync._types.setdefault(api_id, {})["__schema__"] = {
"typeName": "__schema__", "definition": definition, "format": "SDL",
}
+ # The data plane and GetIntrospectionSchema read the schema from _schemas,
+ # not from the "__schema__" type entry — a CFN-provisioned schema has to
+ # land in both or the API behaves as if it had none.
+ from ministack.core import appsync_graphql
+ appsync_graphql.forget_schema(api_id)
+ _appsync._schemas[api_id] = {
+ "definition": definition,
+ "status": "SUCCESS",
+ "details": "Schema creation successful.",
+ }
return f"{api_id}/schema", {}
@@ -3825,6 +3835,9 @@ def _appsync_schema_delete(physical_id, props):
# stored properties are missing the ApiId.
api_id = props.get("ApiId") or physical_id.rsplit("/", 1)[0]
_appsync._types.get(api_id, {}).pop("__schema__", None)
+ _appsync._schemas.pop(api_id, None)
+ from ministack.core import appsync_graphql
+ appsync_graphql.forget_schema(api_id)
def _appsync_apikey_create(logical_id, props, stack_name):
diff --git a/ministack/services/ecs.py b/ministack/services/ecs.py
index 4e68a25d..ca7e8879 100644
--- a/ministack/services/ecs.py
+++ b/ministack/services/ecs.py
@@ -44,6 +44,7 @@ from ministack.core.responses import (
json_response,
new_uuid,
now_iso,
+ request_scope,
)
from ministack.services import ecs_metadata, secretsmanager
@@ -254,12 +255,17 @@ def _reconcile_restored_services():
Failures are logged and skipped rather than raised, so one unreachable image
cannot stop the remaining services from coming back.
"""
- for svc_key, svc in list(_services.items()):
+ # This runs on a daemon thread with no request scope, so plain .items()
+ # would only see the default account and region — a service persisted under
+ # any other tenant would never come back. Walk every stored service and pin
+ # its own account and region around the relaunch.
+ for (account_id, region, svc_key), svc in _services.all_items():
if not isinstance(svc, dict) or svc.get("status") != "ACTIVE":
continue
cluster_name = svc_key.split("/", 1)[0]
try:
- _reconcile_service_tasks(cluster_name, svc_key)
+ with request_scope(account_id, region):
+ _reconcile_service_tasks(cluster_name, svc_key)
except Exception as exc:
logger.warning(
"ECS: could not relaunch service %s after restore: %s",
@@ -1502,7 +1508,56 @@ def _run_task(data):
)
try:
- container = docker_client.containers.run(cdef["image"], **run_kwargs)
+ try:
+ container = docker_client.containers.run(cdef["image"], **run_kwargs)
+ except Exception as exc:
+ # Best-effort platform pin: a host that cannot run the
+ # declared runtimePlatform (no emulation handler, or a
+ # cached image of the other architecture) logs the
+ # mismatch and runs on its own architecture, as every
+ # setup did before the pin existed.
+ pinned = run_kwargs.pop("platform", None)
+ if not pinned:
+ raise
+ logger.warning(
+ "ECS: task definition declares %s but this host cannot "
+ "run it (%s); running %s on the host architecture "
+ "instead. Install a binfmt/qemu handler for real "
+ "cross-architecture execution.",
+ pinned, exc, cdef.get("image"))
+ container = docker_client.containers.run(cdef["image"], **run_kwargs)
+ pinned = run_kwargs.get("platform")
+ if pinned:
+ # No emulation handler makes the container start cleanly
+ # and die on its first instruction — docker raises
+ # nothing. Only an exec-format death triggers the
+ # fallback: an instant exit for any other reason is the
+ # task's own business.
+ time.sleep(0.25)
+ exec_dead = False
+ try:
+ container.reload()
+ exec_dead = (container.status == "exited"
+ and b"exec format error"
+ in (container.logs(tail=5) or b""))
+ except Exception:
+ # The death-check is best-effort; if it cannot even
+ # be performed, keep the container that started.
+ exec_dead = False
+ if exec_dead:
+ logger.warning(
+ "ECS: task definition declares %s but this host "
+ "cannot execute it; running %s on the host "
+ "architecture instead. Install a binfmt/qemu "
+ "handler for real cross-architecture execution.",
+ pinned, cdef.get("image"))
+ try:
+ container.remove(force=True)
+ except Exception:
+ pass
+ run_kwargs.pop("platform", None)
+ container = docker_client.containers.run(
+ cdef["image"], **run_kwargs)
task["_docker_ids"].append(container.id)
ecs_metadata.set_docker_id(metadata_token, container.id)
task.setdefault("_metadata_tokens", []).append(metadata_token)
diff --git a/ministack/services/kms.py b/ministack/services/kms.py
index 2ff94d3d..6c5d9756 100644
--- a/ministack/services/kms.py
+++ b/ministack/services/kms.py
@@ -1511,8 +1511,11 @@ def _generate_random(data):
absence with "NumberOfBytes is required.", so an omitted count is a
``ValidationException`` rather than a silent default. ``CustomKeyStoreId``
answers ``CustomKeyStoreNotFoundException`` — MiniStack has no custom key
- stores. The Nitro-enclave ``Recipient`` parameter is not supported and is
- ignored.
+ stores. The Nitro-enclave ``Recipient`` parameter answers
+ ``UnsupportedOperationException``: a caller sending it expects
+ ``CiphertextForRecipient`` and a null ``Plaintext``, and MiniStack cannot
+ encrypt to an enclave's attestation key — answering ``Plaintext`` anyway
+ would be a shape the real service never produces for that request.
"""
number_of_bytes = data.get("NumberOfBytes")
if number_of_bytes is None:
@@ -1539,6 +1542,13 @@ def _generate_random(data):
f"Custom key store {data['CustomKeyStoreId']} does not exist",
400,
)
+ if data.get("Recipient"):
+ return error_response_json(
+ "UnsupportedOperationException",
+ "The Recipient parameter is not supported by MiniStack: plaintext "
+ "cannot be encrypted to a Nitro enclave attestation key.",
+ 400,
+ )
return json_response({"Plaintext": base64.b64encode(os.urandom(number_of_bytes)).decode()})
diff --git a/ministack/services/lambda_svc.py b/ministack/services/lambda_svc.py
index ac5311d8..9229d110 100644
--- a/ministack/services/lambda_svc.py
+++ b/ministack/services/lambda_svc.py
@@ -1999,6 +1999,11 @@ def _create_function(data: dict):
"s3_object_storage_mode": code_data.get("S3ObjectStorageMode"),
"source_kms_key_arn": code_data.get("SourceKMSKeyArn"),
"s3_object_ref": _s3_object_ref,
+ # Whether the caller explicitly chose an architecture, as distinct from
+ # the stored x86_64 default. The wire shape is identical either way;
+ # only the Docker executor reads this, to pin the container platform
+ # for functions that actually declared one.
+ "architectures_declared": "Architectures" in data,
"versions": {},
"next_version": 1,
"tags": data.get("Tags", {}),
@@ -2359,6 +2364,11 @@ def _update_code(name: str, data: dict):
)
func = _functions[name]
code_zip = None
+ # UpdateFunctionCode is where AWS lets the architecture change (the
+ # UpdateFunctionConfiguration model has no Architectures member).
+ if "Architectures" in data:
+ func["config"]["Architectures"] = data["Architectures"]
+ func["architectures_declared"] = True
if "ImageUri" in data:
func["config"]["ImageUri"] = data["ImageUri"]
func["config"]["PackageType"] = "Image"
@@ -2481,6 +2491,8 @@ def _update_config(name: str, data: dict):
config["Layers"] = layers_cfg
else:
config[key] = data[key]
+ if "Architectures" in data:
+ _functions[name]["architectures_declared"] = True
if "ImageConfig" in data:
config["ImageConfigResponse"] = {"ImageConfig": data["ImageConfig"]}
config["LastModified"] = _now_iso()
@@ -3644,9 +3656,33 @@ def _parse_docker_flags(flags: str) -> dict:
return kwargs
-def _spawn_lambda_container(config: dict, code_zip: bytes | None):
+def _declared_docker_platform(config: dict):
+ """The linux/* platform to pin, or None when the function never declared one.
+
+ Every stored config carries ``Architectures`` because AWS's x86_64 default
+ is echoed on the wire — so the config alone cannot say whether the caller
+ chose an architecture. The function record tracks that at create/update
+ time, and only an explicit choice pins the container platform: pinning the
+ stored default onto undeclared functions would break arm64 hosts with no
+ amd64 binfmt handler, whose functions ran natively before.
+ """
+ func = _functions.get(config.get("FunctionName") or "")
+ if not func or not func.get("architectures_declared"):
+ return None
+ arch = (config.get("Architectures") or ["x86_64"])[0]
+ return "linux/arm64" if arch == "arm64" else "linux/amd64"
+
+
+def _spawn_lambda_container(config: dict, code_zip: bytes | None,
+ _pin_platform: bool = True):
"""Create and start a Lambda container for the given config.
+ A function that explicitly declared an architecture is pinned to it — but
+ only best-effort: a host that cannot run the declared platform (no qemu or
+ Rosetta for the emulation) logs the mismatch and retries on the host's own
+ architecture instead of failing an invoke that worked before the pin
+ existed. `_pin_platform=False` is that retry.
+
Returns (container, tmpdir). The caller is responsible for pool registration
and for `_kill_pool_entry` on cleanup (tmpdir is None for Image-type).
@@ -3834,10 +3870,26 @@ def _spawn_lambda_container(config: dict, code_zip: bytes | None):
# handler dies at import naming the library rather than the mismatch
# ("no pq wrapper available" from psycopg, for instance). Set it before the
# flags merge so an explicit --platform in LAMBDA_DOCKER_FLAGS still wins.
- declared_arch = (config.get("Architectures") or ["x86_64"])[0]
- docker_arch = "arm64" if declared_arch == "arm64" else "amd64"
- docker_platform = f"linux/{docker_arch}"
- run_kwargs["platform"] = docker_platform
+ # Only a function whose creator explicitly chose an architecture is pinned:
+ # every stored record carries the x86_64 default, and pinning that onto
+ # functions that never declared one would break arm64 hosts without a
+ # binfmt handler that ran those functions natively before.
+ docker_platform = _declared_docker_platform(config) if _pin_platform else None
+ docker_arch = docker_platform.rsplit("/", 1)[1] if docker_platform else None
+ if docker_platform:
+ run_kwargs["platform"] = docker_platform
+
+ def _platform_fallback(reason):
+ """The declared platform cannot run here: log it, run on the host's."""
+ logger.warning(
+ "Lambda %s declares %s but this host cannot run it (%s); "
+ "running on the host architecture instead. Install a binfmt/qemu "
+ "handler (or Rosetta) for real cross-architecture execution.",
+ config.get("FunctionName"), docker_platform, reason)
+ if tmpdir and os.path.exists(tmpdir):
+ import shutil
+ shutil.rmtree(tmpdir, ignore_errors=True)
+ return _spawn_lambda_container(config, code_zip, _pin_platform=False)
# Apply LAMBDA_DOCKER_FLAGS — merge parsed kwargs into run_kwargs
if LAMBDA_DOCKER_FLAGS:
@@ -3868,14 +3920,18 @@ def _spawn_lambda_container(config: dict, code_zip: bytes | None):
local_image = client.images.get(image)
# A cached image of the wrong architecture is as unusable as no image:
# docker refuses to start it, and without this check the refusal arrives
- # as an opaque run error rather than a pull.
- if local_image.attrs.get("Architecture") not in (None, docker_arch):
+ # as an opaque run error rather than a pull. Only checked when the
+ # function pinned a platform — with no declaration any cached image is
+ # the host's, which is what will run.
+ if docker_arch and local_image.attrs.get("Architecture") not in (None, docker_arch):
raise docker_lib.errors.ImageNotFound("cached image is the wrong architecture")
except docker_lib.errors.ImageNotFound:
- logger.info("Pulling Lambda image: %s (%s)", image, docker_platform)
+ logger.info("Pulling Lambda image: %s (%s)", image, docker_platform or "host platform")
try:
client.images.pull(image, platform=docker_platform)
except Exception as exc:
+ if docker_platform:
+ return _platform_fallback(f"pull failed: {exc}")
if tmpdir and os.path.exists(tmpdir):
import shutil
shutil.rmtree(tmpdir, ignore_errors=True)
@@ -3902,12 +3958,38 @@ def _spawn_lambda_container(config: dict, code_zip: bytes | None):
container.start()
else:
container = client.containers.run(**run_kwargs)
- except Exception:
+ except Exception as exc:
+ if docker_platform:
+ return _platform_fallback(f"container create/start failed: {exc}")
if tmpdir and os.path.exists(tmpdir):
import shutil
shutil.rmtree(tmpdir, ignore_errors=True)
raise
+ if docker_platform:
+ # A host with no emulation handler starts the container fine and its
+ # entrypoint dies instantly with an exec-format error — docker raises
+ # nothing, and without this check the failure surfaces later as an
+ # opaque invoke timeout. An immediate exit while pinned means the host
+ # cannot run the declared platform: fall back to the host's.
+ time.sleep(0.25)
+ try:
+ container.reload()
+ died = container.status == "exited"
+ except Exception:
+ died = False
+ if died:
+ try:
+ tail = container.logs(tail=5).decode(errors="replace").strip()
+ except Exception:
+ tail = ""
+ try:
+ container.remove(force=True)
+ except Exception:
+ pass
+ return _platform_fallback(
+ f"container exited immediately: {tail or 'no output'}")
+
return container, tmpdir
diff --git a/tests/test_appsync.py b/tests/test_appsync.py
index dd26d9ac..ec845cad 100644
--- a/tests/test_appsync.py
+++ b/tests/test_appsync.py
@@ -60,9 +60,11 @@ def test_appsync_graphql_apis_are_region_scoped():
# Local data-plane URLs do not encode or sign a region. The API ID
# must select its stored region before resolver execution.
+ west_key = west.create_api_key(apiId=west_api["apiId"])["apiKey"]["id"]
west_graphql = _appsync_graphql_post(
f"{_ENDPOINT}/v1/apis/{west_api['apiId']}/graphql",
"{ __typename }",
+ headers={"x-api-key": west_key},
)
assert "errors" not in west_graphql
finally:
@@ -184,10 +186,13 @@ def test_appsync_graphql_honors_dynamodb_data_source_region():
dataSourceName="east-table",
)
+ api_key = west_appsync.create_api_key(apiId=api["apiId"])["apiKey"]["id"]
+
try:
response = _appsync_graphql_post(
f"{_ENDPOINT}/v1/apis/{api['apiId']}/graphql",
'query { getItem(id: "item-1") { id name } }',
+ headers={"x-api-key": api_key},
)
assert response["data"]["getItem"] == {
"id": "item-1",
@@ -443,7 +448,8 @@ def test_appsync_graphql_update_mutation(ddb):
def gql(query):
req = urllib.request.Request(f"{_ENDPOINT}/v1/apis/{api['apiId']}/graphql",
- data=_json.dumps({"query": query}).encode(), headers={"Content-Type": "application/json"})
+ data=_json.dumps({"query": query}).encode(),
+ headers={"Content-Type": "application/json", "x-api-key": key["id"]})
with urllib.request.urlopen(req) as r:
return _json.loads(r.read())
@@ -471,6 +477,7 @@ def test_appsync_graphql_delete_mutation(ddb):
pass
api = appsync.create_graphql_api(name="gql-del", authenticationType="API_KEY")["graphqlApi"]
+ key = appsync.create_api_key(apiId=api["apiId"])["apiKey"]
appsync.create_data_source(apiId=api["apiId"], name="ds", type="AMAZON_DYNAMODB",
dynamodbConfig={"tableName": "gql-del", "awsRegion": "us-east-1"})
appsync.create_resolver(apiId=api["apiId"], typeName="Mutation", fieldName="createItem", dataSourceName="ds")
@@ -479,7 +486,8 @@ def test_appsync_graphql_delete_mutation(ddb):
def gql(query):
req = urllib.request.Request(f"{_ENDPOINT}/v1/apis/{api['apiId']}/graphql",
- data=_json.dumps({"query": query}).encode(), headers={"Content-Type": "application/json"})
+ data=_json.dumps({"query": query}).encode(),
+ headers={"Content-Type": "application/json", "x-api-key": key["id"]})
with urllib.request.urlopen(req) as r:
return _json.loads(r.read())
@@ -506,6 +514,7 @@ def test_appsync_graphql_with_variables():
pass
api = appsync.create_graphql_api(name="gql-vars", authenticationType="API_KEY")["graphqlApi"]
+ key = appsync.create_api_key(apiId=api["apiId"])["apiKey"]
appsync.create_data_source(apiId=api["apiId"], name="ds", type="AMAZON_DYNAMODB",
dynamodbConfig={"tableName": "gql-vars", "awsRegion": "us-east-1"})
appsync.create_resolver(apiId=api["apiId"], typeName="Mutation", fieldName="createItem", dataSourceName="ds")
@@ -516,7 +525,8 @@ def test_appsync_graphql_with_variables():
if variables:
body["variables"] = variables
req = urllib.request.Request(f"{_ENDPOINT}/v1/apis/{api['apiId']}/graphql",
- data=_json.dumps(body).encode(), headers={"Content-Type": "application/json"})
+ data=_json.dumps(body).encode(),
+ headers={"Content-Type": "application/json", "x-api-key": key["id"]})
with urllib.request.urlopen(req) as r:
return _json.loads(r.read())
@@ -540,13 +550,14 @@ def test_appsync_graphql_nonexistent_item():
pass
api = appsync.create_graphql_api(name="gql-404", authenticationType="API_KEY")["graphqlApi"]
+ key = appsync.create_api_key(apiId=api["apiId"])["apiKey"]
appsync.create_data_source(apiId=api["apiId"], name="ds", type="AMAZON_DYNAMODB",
dynamodbConfig={"tableName": "gql-404", "awsRegion": "us-east-1"})
appsync.create_resolver(apiId=api["apiId"], typeName="Query", fieldName="getItem", dataSourceName="ds")
req = urllib.request.Request(f"{_ENDPOINT}/v1/apis/{api['apiId']}/graphql",
data=_json.dumps({"query": 'query { getItem(id: "ghost") { id } }'}).encode(),
- headers={"Content-Type": "application/json"})
+ headers={"Content-Type": "application/json", "x-api-key": key["id"]})
with urllib.request.urlopen(req) as r:
resp = _json.loads(r.read())
assert resp["data"]["getItem"] is None
@@ -1326,12 +1337,6 @@ def _graphql(api_id, api_key, query, variables=None):
r = _rq.post(f"{_ENDPOINT}/v1/apis/{api_id}/graphql",
headers={"x-api-key": api_key, "content-type": "application/json"},
json=payload, timeout=15)
-def _graphql(api_id, api_key, query):
- """POST a query to the API's data plane the way an SDK would."""
- import requests as _rq
- r = _rq.post(f"{_ENDPOINT}/v1/apis/{api_id}/graphql",
- headers={"x-api-key": api_key, "content-type": "application/json"},
- json={"query": query}, timeout=15)
return r.json()
def _cache_ds_api(appsync, ddb, name):
@@ -1987,12 +1992,6 @@ def test_appsync_js_function_early_return_continues_the_pipeline(appsync):
function request handler (or the pipeline resolver response handler if this
was the last AWS AppSync function) is called."
-def test_appsync_js_function_early_return_continues_the_pipeline(appsync):
- """earlyReturn in a FUNCTION skips its data source and response — and the
- pipeline continues to the next function.
- AWS: "the data source and response handler are skipped, and the next
- function request handler (or the pipeline resolver response handler if this
- was the last AWS AppSync function) is called."
Ending the whole pipeline instead means every later function is silently
skipped. A real resolver that guards its first function this way then
returns null with no error, because the function that would have set the
@@ -2032,9 +2031,6 @@ def test_appsync_js_resolver_early_return_still_runs_the_response(appsync):
"""earlyReturn in the pipeline RESOLVER's request skips the functions, and
the resolver's own response handler still runs.
-def test_appsync_js_resolver_early_return_still_runs_the_response(appsync):
- """earlyReturn in the pipeline RESOLVER's request skips the functions, and
- the resolver's own response handler still runs.
AWS: "the pipeline execution is skipped, and the pipeline resolver response
handler is called immediately."
"""
@@ -2122,9 +2118,6 @@ def test_appsync_js_lambda_datasource_receives_the_payload_verbatim(appsync, lam
"""A JS resolver returning {operation: 'Invoke', payload} sends exactly that
payload as the Lambda event.
-def test_appsync_js_lambda_datasource_receives_the_payload_verbatim(appsync, lam):
- """A JS resolver returning {operation: 'Invoke', payload} sends exactly that
- payload as the Lambda event.
It was being wrapped in the standard resolver event instead, so a function
expecting its own shape — say {text: [...]} — received
{arguments: {text: [...]}, info: {...}} and returned something the resolver
@@ -2174,17 +2167,14 @@ def test_appsync_js_lambda_datasource_receives_the_payload_verbatim(appsync, lam
f"the payload must arrive verbatim, not wrapped in a resolver event: {got}"
- os.environ.get("APPSYNC_ENFORCE_AUTH", "0") in ("0", "", "false", "False"),
- reason="enforcement is opt-in; run the server with APPSYNC_ENFORCE_AUTH=1")
def test_appsync_cognito_api_refuses_an_unauthenticated_request(appsync, cognito_idp):
"""An API whose auth mode is AMAZON_COGNITO_USER_POOLS refuses a caller with
- no credentials.
+ no credentials, as AWS does — unconditionally, not behind a switch.
- ministack already refuses an unknown API key on this path, so an API that
- declares Cognito should likewise not serve an anonymous caller — otherwise
- every authorization test passes locally regardless of what the resolvers do,
- which is the opposite of useful.
+ Credentials are still not verified (ministack issued them itself); what is
+ refused is a request satisfying none of the API's configured providers,
+ which is the case that made every authorization test pass regardless of
+ what the resolvers did.
"""
pool = cognito_idp.create_user_pool(PoolName="qa-authmode-pool")["UserPool"]
api = appsync.create_graphql_api(
@@ -2221,6 +2211,7 @@ def test_appsync_schemaless_api_keeps_lenient_execution(ddb):
"""
import json
import urllib.request
+
from conftest import make_client
appsync = make_client("appsync")
@@ -2266,3 +2257,238 @@ def test_appsync_schemaless_api_keeps_lenient_execution(ddb):
# before any resolver ran. Schemaless, it must still reach the resolver.
body = _post('mutation { createUser(input: {id: "u2", nope: 1}) { id } }')
assert body.get("data", {}).get("createUser", {}).get("id") == "u2", body
+
+
+# ---------------------------------------------------------------------------
+# Fixes on top of the AppSync execution merge: required members validated,
+# ApiCache carries healthMetricsConfig, introspection serves JSON, the JS
+# worker is bounded, DynamoDB requests decode and scope correctly, and CFN
+# releases every child store.
+# ---------------------------------------------------------------------------
+
+
+def test_appsync_ddb_plain_decodes_numbers_and_sets():
+ """AttributeValue N is a string that may be a float or negative, and the
+ set types are sets — none of them survive a digits-only int() decode."""
+ from ministack.services.appsync import _ddb_plain
+
+ assert _ddb_plain({"N": "42"}) == 42
+ assert _ddb_plain({"N": "-2"}) == -2
+ assert _ddb_plain({"N": "3.5"}) == 3.5
+ assert _ddb_plain({"N": "-0.25"}) == -0.25
+ assert _ddb_plain({"SS": ["a", "b"]}) == ["a", "b"]
+ assert _ddb_plain({"NS": ["1", "-2", "3.5"]}) == [1, -2, 3.5]
+ assert _ddb_plain({"BS": ["AQ==", "Ag=="]}) == ["AQ==", "Ag=="]
+ assert _ddb_plain({"M": {"n": {"N": "-7"}, "s": {"SS": ["x"]}}}) == {"n": -7, "s": ["x"]}
+ assert _ddb_plain({"L": [{"N": "1.5"}, {"NULL": True}]}) == [1.5, None]
+
+
+def test_appsync_api_cache_validates_required_members():
+ """CreateApiCache requires ttl (1-3600), apiCachingBehavior and type; AWS
+ refuses a missing or out-of-range member rather than defaulting it.
+
+ Driven through the handlers directly: boto3 enforces required members
+ client-side, so the wire-level refusal is unreachable through it.
+ """
+ import json as _json_mod
+
+ from ministack.services import appsync as _appsync
+
+ resp = _appsync._create_graphql_api({"name": "cache-validate", "authenticationType": "API_KEY"})
+ api_id = _json_mod.loads(resp[2])["graphqlApi"]["apiId"]
+
+ valid = {"ttl": 60, "apiCachingBehavior": "FULL_REQUEST_CACHING", "type": "SMALL"}
+ for member in ("ttl", "apiCachingBehavior", "type"):
+ body = {k: v for k, v in valid.items() if k != member}
+ status, _hdrs, payload = _appsync._create_api_cache(api_id, body)
+ assert status == 400, f"missing {member} must be refused, got {status}"
+ assert _json_mod.loads(payload)["__type"].endswith("BadRequestException")
+
+ for bad_ttl in (0, 3601):
+ status, _hdrs, payload = _appsync._create_api_cache(api_id, {**valid, "ttl": bad_ttl})
+ assert status == 400, f"ttl={bad_ttl} must be refused"
+
+ status, _hdrs, payload = _appsync._create_api_cache(api_id, {**valid, "apiCachingBehavior": "SOMETIMES"})
+ assert status == 400
+
+ # A valid create reports healthMetricsConfig, defaulted DISABLED when unset.
+ status, _hdrs, payload = _appsync._create_api_cache(api_id, valid)
+ assert status == 200
+ record = _json_mod.loads(payload)["apiCache"]
+ assert record["healthMetricsConfig"] == "DISABLED"
+
+ # An update must re-supply the required members too, and carries
+ # healthMetricsConfig forward when not restated.
+ status, _hdrs, payload = _appsync._update_api_cache(api_id, {"ttl": 120})
+ assert status == 400
+ status, _hdrs, payload = _appsync._update_api_cache(
+ api_id, {**valid, "ttl": 120, "healthMetricsConfig": "ENABLED"})
+ assert _json_mod.loads(payload)["apiCache"]["healthMetricsConfig"] == "ENABLED"
+ status, _hdrs, payload = _appsync._update_api_cache(api_id, {**valid, "ttl": 180})
+ assert _json_mod.loads(payload)["apiCache"]["healthMetricsConfig"] == "ENABLED"
+
+ _appsync._delete_graphql_api(api_id)
+
+
+def test_appsync_function_requires_data_source_name():
+ """CreateFunction and UpdateFunction refuse a missing dataSourceName, which
+ the API declares required, instead of storing an empty string."""
+ import json as _json_mod
+
+ from ministack.services import appsync as _appsync
+
+ resp = _appsync._create_graphql_api({"name": "fn-validate", "authenticationType": "API_KEY"})
+ api_id = _json_mod.loads(resp[2])["graphqlApi"]["apiId"]
+
+ status, _hdrs, payload = _appsync._create_function(api_id, {"name": "orphan"})
+ assert status == 400
+ assert _json_mod.loads(payload)["__type"].endswith("BadRequestException")
+
+ _appsync._create_data_source(api_id, {"name": "DS", "type": "NONE"})
+ resp = _appsync._create_function(api_id, {"name": "fn", "dataSourceName": "DS"})
+ fn_id = _json_mod.loads(resp[2])["functionConfiguration"]["functionId"]
+
+ status, _hdrs, _payload = _appsync._update_function(api_id, fn_id, {"name": "fn"})
+ assert status == 400, "UpdateFunction without dataSourceName must be refused"
+
+ _appsync._delete_graphql_api(api_id)
+
+
+def test_appsync_introspection_schema_json_is_a_real_document(appsync):
+ """format=JSON answers the introspection query result, not SDL and not a
+ 400 — it is what `aws appsync get-introspection-schema --format JSON`
+ writes for codegen tooling."""
+ api = appsync.create_graphql_api(name="introspect-json", authenticationType="API_KEY")["graphqlApi"]
+ try:
+ appsync.start_schema_creation(
+ apiId=api["apiId"],
+ definition=b"type Query { hello: String }\nschema { query: Query }\n")
+
+ blob = appsync.get_introspection_schema(apiId=api["apiId"], format="JSON")["schema"].read()
+ document = json.loads(blob)
+ assert "__schema" in document
+ type_names = {t["name"] for t in document["__schema"]["types"]}
+ assert "Query" in type_names
+
+ sdl = appsync.get_introspection_schema(apiId=api["apiId"], format="SDL")["schema"].read()
+ assert b"type Query" in sdl
+ finally:
+ appsync.delete_graphql_api(apiId=api["apiId"])
+
+
+def test_appsync_js_worker_timeout_kills_and_recovers():
+ """An infinite loop in a resolver must not wedge evaluation for the whole
+ service: the evaluation is bounded, the stuck process is killed, and the
+ next evaluation runs on a fresh worker."""
+ from ministack.core import appsync_js
+
+ if not appsync_js.available():
+ pytest.skip("node is not available")
+
+ code = "export function request(ctx) { return { payload: ctx.args.x } }"
+ try:
+ with pytest.raises(appsync_js.AppSyncJsTimeout):
+ appsync_js.evaluate(
+ "export function request(ctx) { while (true) {} }",
+ "request", {"args": {}, "stash": {}}, timeout=2)
+
+ status, value, _appended, _stash, _skip = appsync_js.evaluate(
+ code, "request", {"args": {"x": 7}, "stash": {}})
+ assert status == "ok" and value == {"payload": 7}
+ finally:
+ appsync_js.reset()
+
+
+def test_appsync_js_resolver_honors_dynamodb_data_source_region(appsync, ddb):
+ """A JS resolver's DynamoDB data source reads the table in the region the
+ data source declares, not the region the request came in on."""
+ import uuid as _uuid
+
+ east_ddb = ddb # conftest ddb client is us-east-1
+ west = boto3.client("appsync", endpoint_url=_ENDPOINT, region_name="us-west-2",
+ aws_access_key_id="test", aws_secret_access_key="test")
+ table = f"js-cross-region-{_uuid.uuid4().hex[:12]}"
+ east_ddb.create_table(
+ TableName=table,
+ KeySchema=[{"AttributeName": "id", "KeyType": "HASH"}],
+ AttributeDefinitions=[{"AttributeName": "id", "AttributeType": "S"}],
+ BillingMode="PAY_PER_REQUEST",
+ )
+ east_ddb.put_item(TableName=table, Item={"id": {"S": "k1"}, "v": {"S": "east"}})
+
+ api = west.create_graphql_api(name="js-cross-region", authenticationType="API_KEY")["graphqlApi"]
+ api_id = api["apiId"]
+ key = west.create_api_key(apiId=api_id)["apiKey"]["id"]
+ west.create_data_source(
+ apiId=api_id, name="EastTable", type="AMAZON_DYNAMODB",
+ dynamodbConfig={"tableName": table, "awsRegion": "us-east-1"},
+ )
+ west.create_resolver(
+ apiId=api_id, typeName="Query", fieldName="getThing", dataSourceName="EastTable",
+ runtime={"name": "APPSYNC_JS", "runtimeVersion": "1.0.0"},
+ code="""
+ export function request(ctx) {
+ return { operation: 'GetItem', key: { id: { S: ctx.args.id } } }
+ }
+ export function response(ctx) { return ctx.result }
+ """,
+ )
+ try:
+ out = _appsync_graphql_post(
+ f"{_ENDPOINT}/v1/apis/{api_id}/graphql",
+ 'query { getThing(id: "k1") { id v } }',
+ headers={"x-api-key": key},
+ )
+ assert out["data"]["getThing"] == {"id": "k1", "v": "east"}, out
+ finally:
+ west.delete_graphql_api(apiId=api_id)
+ east_ddb.delete_table(TableName=table)
+
+
+def test_appsync_cfn_api_delete_releases_every_child_store():
+ """The CloudFormation provisioner's delete must release the same stores the
+ service's own DeleteGraphqlApi releases — functions, schema, cache and
+ cache entries included, which a hand-kept pop list silently missed."""
+ import json as _json_mod
+
+ from ministack.services import appsync as _appsync
+ from ministack.services.cloudformation import provisioners as _prov
+
+ resp = _appsync._create_graphql_api({"name": "cfn-cascade", "authenticationType": "API_KEY"})
+ api_id = _json_mod.loads(resp[2])["graphqlApi"]["apiId"]
+ _appsync._start_schema_creation(api_id, {"definition": "type Query { hi: String }"})
+ _appsync._create_data_source(api_id, {"name": "DS", "type": "NONE"})
+ _appsync._create_function(api_id, {"name": "fn", "dataSourceName": "DS"})
+ _appsync._create_api_cache(
+ api_id, {"ttl": 60, "apiCachingBehavior": "FULL_REQUEST_CACHING", "type": "SMALL"})
+ _appsync._cache_put(api_id, "k", 60, {"cached": True})
+
+ _prov._appsync_api_delete(api_id, {})
+
+ for store, label in (
+ (_appsync._apis, "apis"), (_appsync._functions, "functions"),
+ (_appsync._schemas, "schemas"), (_appsync._caches, "caches"),
+ (_appsync._cache_entries, "cache entries"), (_appsync._data_sources, "data sources"),
+ ):
+ assert api_id not in store, f"{label} leaked after the CFN delete"
+
+
+def test_appsync_cfn_schema_lands_in_the_schema_store():
+ """A CFN-provisioned AWS::AppSync::GraphQLSchema must be visible where the
+ data plane and GetIntrospectionSchema read schemas from."""
+ import json as _json_mod
+
+ from ministack.services import appsync as _appsync
+ from ministack.services.cloudformation import provisioners as _prov
+
+ resp = _appsync._create_graphql_api({"name": "cfn-schema", "authenticationType": "API_KEY"})
+ api_id = _json_mod.loads(resp[2])["graphqlApi"]["apiId"]
+
+ _prov._appsync_schema_create(
+ "Schema", {"ApiId": api_id, "Definition": "type Query { hi: String }"}, "stack")
+ assert (_appsync._schemas.get(api_id) or {}).get("definition") == "type Query { hi: String }"
+
+ _prov._appsync_schema_delete(f"{api_id}/schema", {"ApiId": api_id})
+ assert _appsync._schemas.get(api_id) is None
+
+ _appsync._delete_graphql_api(api_id)
diff --git a/tests/test_ecs.py b/tests/test_ecs.py
index 13694a87..44a34c2a 100644
--- a/tests/test_ecs.py
+++ b/tests/test_ecs.py
@@ -1693,7 +1693,8 @@ def test_ecs_sync_service_targets_does_not_publish_a_stale_view(monkeypatch):
def test_ecs_restored_services_relaunch_their_tasks(monkeypatch):
- """A service must satisfy desiredCount again after a restore.
+ """A service must satisfy desiredCount again after a restore — in every
+ account and region, not only the default.
restore_state marks every restored task STOPPED, because its container is
gone with the process that ran it. Nothing then reconciles the services, so
@@ -1701,44 +1702,92 @@ def test_ecs_restored_services_relaunch_their_tasks(monkeypatch):
no container exists, and the load balancer keeps forwarding to addresses
nothing is listening on. Real ECS relaunches: the service scheduler exists
to keep desiredCount satisfied.
+
+ The reconciler runs on a daemon thread with no request scope, so it must
+ walk the stores with all_items() and pin each service's own account and
+ region — a plain .items() only ever saw the default tenant, and a service
+ persisted under any other account or region never came back.
"""
+ from ministack.core.responses import (
+ AccountRegionScopedDict,
+ get_account_id,
+ get_region,
+ )
from ministack.services import ecs as _ecs
launched = []
- monkeypatch.setattr(_ecs, "_run_task", lambda data: launched.append(data))
+
+ def _capture_run_task(data):
+ # Record the scope the reconciler pinned: the spawned task must land in
+ # the service's own account and region.
+ launched.append({**data, "_scope": (get_account_id(), get_region())})
+
+ monkeypatch.setattr(_ecs, "_run_task", _capture_run_task)
monkeypatch.setattr(_ecs, "_clusters", {"c1": {"clusterName": "c1"}})
- monkeypatch.setattr(_ecs, "_task_defs", {
- "web:1": {"taskDefinitionArn": "arn:aws:ecs:us-east-1:000000000000:task-definition/web:1",
- "family": "web"},
- })
- monkeypatch.setattr(_ecs, "_services", {
- "c1/web": {
- "serviceName": "web", "status": "ACTIVE", "desiredCount": 2,
- "taskDefinition": "arn:aws:ecs:us-east-1:000000000000:task-definition/web:1", "clusterArn": "arn:cluster/c1",
- "launchType": "FARGATE", "deployments": [{"runningCount": 2}],
- },
- # An inactive service must not be relaunched.
- "c1/old": {
- "serviceName": "old", "status": "INACTIVE", "desiredCount": 3,
- "taskDefinition": "arn:aws:ecs:us-east-1:000000000000:task-definition/web:1", "clusterArn": "arn:cluster/c1",
- "launchType": "FARGATE", "deployments": [],
- },
- })
+
+ task_defs = AccountRegionScopedDict()
+ task_defs.set_scoped(
+ "000000000000", "us-east-1", "web:1",
+ {"taskDefinitionArn": "arn:aws:ecs:us-east-1:000000000000:task-definition/web:1",
+ "family": "web"})
+ task_defs.set_scoped(
+ "222222222222", "eu-west-1", "api:1",
+ {"taskDefinitionArn": "arn:aws:ecs:eu-west-1:222222222222:task-definition/api:1",
+ "family": "api"})
+ monkeypatch.setattr(_ecs, "_task_defs", task_defs)
+
+ services = AccountRegionScopedDict()
+ services.set_scoped(
+ "000000000000", "us-east-1", "c1/web",
+ {"serviceName": "web", "status": "ACTIVE", "desiredCount": 2,
+ "taskDefinition": "arn:aws:ecs:us-east-1:000000000000:task-definition/web:1",
+ "clusterArn": "arn:cluster/c1",
+ "launchType": "FARGATE", "deployments": [{"runningCount": 2}]})
+ # An inactive service must not be relaunched.
+ services.set_scoped(
+ "000000000000", "us-east-1", "c1/old",
+ {"serviceName": "old", "status": "INACTIVE", "desiredCount": 3,
+ "taskDefinition": "arn:aws:ecs:us-east-1:000000000000:task-definition/web:1",
+ "clusterArn": "arn:cluster/c1",
+ "launchType": "FARGATE", "deployments": []})
+ # A service persisted by another tenant, in another region.
+ services.set_scoped(
+ "222222222222", "eu-west-1", "c2/api",
+ {"serviceName": "api", "status": "ACTIVE", "desiredCount": 1,
+ "taskDefinition": "arn:aws:ecs:eu-west-1:222222222222:task-definition/api:1",
+ "clusterArn": "arn:cluster/c2",
+ "launchType": "FARGATE", "deployments": [{"runningCount": 1}]})
+ monkeypatch.setattr(_ecs, "_services", services)
+
# What restore_state leaves behind: the tasks exist but are STOPPED.
- monkeypatch.setattr(_ecs, "_tasks", {
- "arn:task/1": {"group": "service:web", "clusterArn": "arn:cluster/c1",
- "lastStatus": "STOPPED", "taskDefinitionArn": "arn:aws:ecs:us-east-1:000000000000:task-definition/web:1"},
- "arn:task/2": {"group": "service:web", "clusterArn": "arn:cluster/c1",
- "lastStatus": "STOPPED", "taskDefinitionArn": "arn:aws:ecs:us-east-1:000000000000:task-definition/web:1"},
- })
+ tasks = AccountRegionScopedDict()
+ tasks.set_scoped(
+ "000000000000", "us-east-1", "arn:task/1",
+ {"group": "service:web", "clusterArn": "arn:cluster/c1",
+ "lastStatus": "STOPPED",
+ "taskDefinitionArn": "arn:aws:ecs:us-east-1:000000000000:task-definition/web:1"})
+ tasks.set_scoped(
+ "000000000000", "us-east-1", "arn:task/2",
+ {"group": "service:web", "clusterArn": "arn:cluster/c1",
+ "lastStatus": "STOPPED",
+ "taskDefinitionArn": "arn:aws:ecs:us-east-1:000000000000:task-definition/web:1"})
+ monkeypatch.setattr(_ecs, "_tasks", tasks)
_ecs._reconcile_restored_services()
- assert len(launched) == 1, f"expected one RunTask call, got {launched}"
- call = launched[0]
- assert call["group"] == "service:web"
- assert call["count"] == 2, "both stopped tasks must be replaced"
- assert all(c["group"] != "service:old" for c in launched)
+ by_group = {c["group"]: c for c in launched}
+ assert "service:old" not in by_group
+ assert set(by_group) == {"service:web", "service:api"}, \
+ f"expected both ACTIVE services relaunched, got {launched}"
+
+ web = by_group["service:web"]
+ assert web["count"] == 2, "both stopped tasks must be replaced"
+ assert web["_scope"] == ("000000000000", "us-east-1")
+
+ api = by_group["service:api"]
+ assert api["count"] == 1
+ assert api["_scope"] == ("222222222222", "eu-west-1"), \
+ "the relaunch must run pinned to the service's own account and region"
def test_ecs_service_reconcile_spares_foreign_targets(monkeypatch):
"""A service withdraws only its own registrations: targets registered by
hand (or by another service sharing the group) survive its reconcile and
diff --git a/tests/test_kms.py b/tests/test_kms.py
index 67e0ffd5..3c1023e1 100644
--- a/tests/test_kms.py
+++ b/tests/test_kms.py
@@ -2018,3 +2018,22 @@ def test_kms_generate_random_validation(kms_client):
resp = _raw({"NumberOfBytes": 16, "CustomKeyStoreId": "cks-does-not-exist"})
assert resp.status_code == 400
assert "CustomKeyStoreNotFoundException" in resp.json().get("__type", "")
+
+
+def test_kms_generate_random_refuses_a_recipient(kms_client):
+ """The Nitro-enclave Recipient parameter is refused, not silently ignored.
+
+ A caller sending Recipient expects CiphertextForRecipient and a null
+ Plaintext; MiniStack cannot encrypt to an enclave attestation key, and
+ answering Plaintext anyway is a response shape the request never gets from
+ the real service.
+ """
+ with pytest.raises(ClientError) as e:
+ kms_client.generate_random(
+ NumberOfBytes=32,
+ Recipient={
+ "KeyEncryptionAlgorithm": "RSAES_OAEP_SHA_256",
+ "AttestationDocument": b"not-a-real-attestation-document",
+ },
+ )
+ assert e.value.response["Error"]["Code"] == "UnsupportedOperationException"
diff --git a/tests/test_lambda.py b/tests/test_lambda.py
index 2013e50a..4948499d 100644
--- a/tests/test_lambda.py
+++ b/tests/test_lambda.py
@@ -10779,3 +10779,32 @@ def test_lambda_runs_on_the_architecture_it_declares(lam, declared, expected_mac
assert payload.get("machine") == expected_machine, payload
finally:
lam.delete_function(FunctionName=fname)
+
+
+def test_lambda_platform_pinned_only_when_architectures_declared(monkeypatch):
+ """The Docker platform is pinned only for an explicitly chosen architecture.
+
+ Every stored config carries Architectures because the x86_64 default is
+ echoed on the wire, so the executor must not read the config alone: pinning
+ the stored default onto functions that never declared one would break arm64
+ hosts without an amd64 binfmt handler, whose functions ran natively before.
+ A record persisted before the marker existed counts as undeclared.
+ """
+ from ministack.services import lambda_svc as _lam
+
+ monkeypatch.setattr(_lam, "_functions", {
+ "declared-arm": {"architectures_declared": True},
+ "declared-x86": {"architectures_declared": True},
+ "undeclared": {"architectures_declared": False},
+ "pre-marker-record": {},
+ })
+
+ assert _lam._declared_docker_platform(
+ {"FunctionName": "declared-arm", "Architectures": ["arm64"]}) == "linux/arm64"
+ assert _lam._declared_docker_platform(
+ {"FunctionName": "declared-x86", "Architectures": ["x86_64"]}) == "linux/amd64"
+ assert _lam._declared_docker_platform(
+ {"FunctionName": "undeclared", "Architectures": ["x86_64"]}) is None
+ assert _lam._declared_docker_platform(
+ {"FunctionName": "pre-marker-record", "Architectures": ["x86_64"]}) is None
+ assert _lam._declared_docker_platform({"FunctionName": "never-created"}) is None
diff --git a/tests/test_ministack.py b/tests/test_ministack.py
index 3fcbeb61..fb2cf742 100644
--- a/tests/test_ministack.py
+++ b/tests/test_ministack.py
@@ -361,6 +361,7 @@ def test_ministack_persist_s3_logging_accelerate_request_payment_roundtrip():
def test_ministack_persist_lambda_roundtrip():
import hashlib
import os
+
from ministack.services import lambda_svc as _lam
code = b"fake-zip-bytes"
sha = hashlib.sha256(code).hexdigest()