Ministack: Free, open-source local AWS emulator - 60+ services, Terraform compatible, real databases. Free forever. MIT licensed.
Python awsaws-emulatoraws-localaws-sdkdevtoolsdockerdynamodbec2emulatorlambdalocalstacklocalstack-alternativeministackmock-awsopen-sourcepythons3sqsterraform
Fix/arn account (#1619)
Nahuel Nucera3beb4ea0470f
diff --git a/README.md b/README.mdindex 04aee0ec..f03629d2 100644--- a/README.md+++ b/README.md@@ -833,7 +833,8 @@ end-to-end without any client config.| `LAMBDA_EXECUTOR` | `local` | Lambda execution mode: `local` (subprocess) or `docker` (container). `provided` runtimes and `PackageType: Image` always use Docker || `LAMBDA_STRICT` | `0` | Set `1` for AWS-fidelity mode: every Lambda invocation runs in a Docker container via the AWS RIE image; in-process fallbacks are disabled. Missing Docker surfaces as `Runtime.DockerUnavailable` instead of degrading to a subprocess. Opt-in because the default install doesn't require Docker || `LAMBDA_DOCKER_NETWORK` | _(unset)_ | Legacy alias for `DOCKER_NETWORK` (Lambda only). Prefer `DOCKER_NETWORK` which covers all services |-| `LAMBDA_DOCKER_FLAGS` | _(unset)_ | Extra `docker run` flags injected into Lambda containers (matches LocalStack). Supports `-e` / `-v` / `--dns` / `--network` / `--cap-add` / `-m` / `--shm-size` / `--tmpfs` / `--add-host` / `--privileged` / `--read-only`. Useful for TLS proxies, custom CAs, and routed dev networks |+| `LAMBDA_DOCKER_FLAGS` | _(unset)_ | Extra `docker run` flags injected into Lambda containers (matches LocalStack). Supports `-e` / `-v` / `--dns` / `--network` / `--cap-add` / `-m` / `--shm-size` / `--tmpfs` / `--add-host` / `--security-opt` / `--privileged` / `--read-only`. Useful for TLS proxies, custom CAs, and routed dev networks |+| `CODEBUILD_DOCKER_FLAGS` | _(unset)_ | Extra `docker run` flags for the CodeBuild local-agent container, same syntax and parser as `LAMBDA_DOCKER_FLAGS`. On an SELinux-enforcing host the agent needs `--security-opt label=disable` to reach the Docker socket it is handed || `MINISTACK_IMAGE_PREFIX` | _(unset)_ | Private-registry prefix prepended to every nested container image (RDS postgres/mysql/mariadb, ElastiCache redis/memcached, EKS k3s, Lambda runtimes). Testcontainers' `hub.image.name.prefix` is auto-forwarded into this var by the Java/Python modules || `LAMBDA_WARM_TTL_SECONDS` | `300` | How long an idle warm Lambda container stays in the pool before the reaper evicts it || `LAMBDA_ACCOUNT_CONCURRENCY` | `0` | Account-level concurrent-invocation cap (0 = unbounded). Match real AWS by setting to `1000`. Used to simulate `ConcurrentInvocationLimitExceeded` throttles |diff --git a/ministack/core/iam_evaluator.py b/ministack/core/iam_evaluator.pyindex 0745fe13..01bbb03f 100644--- a/ministack/core/iam_evaluator.py+++ b/ministack/core/iam_evaluator.py@@ -718,6 +718,70 @@ _ALWAYS_ALLOWED_ACTIONS = frozenset({})+def resolve_caller_identity(access_key_id: str) -> dict | None:+ """Display identity for a caller's access key — no signature verification+ (never modeled) and no policy evaluation, just the same key resolution the+ evaluator uses, shaped for API Gateway's IAM-authorized proxy events.++ Returns None for an unknown key. The ``session`` value is the raw+ ``sts._sessions`` record when the key is a temporary session (AssumeRole or+ Cognito identity-pool credentials), letting callers surface the cognito*+ identity fields it carries.+ """+ from ministack.core.responses import get_account_id+ from ministack.services import iam as iam_svc+ from ministack.services import sts as sts_svc++ if not access_key_id:+ return None+ account_id = get_account_id()++ def _principal_org_id():+ # aws:PrincipalOrgID — the caller account's Organization, when it has+ # one. In this emulator's model every account is the master of its own+ # org the moment it calls Organizations, so the id resolves from the+ # caller's own scope.+ try:+ from ministack.services import organizations as org_svc+ org = org_svc._orgs.get("self")+ return org.get("Id") if org else None+ except Exception:+ return None++ if _is_root_key(access_key_id):+ return {+ "accessKey": access_key_id,+ "accountId": account_id,+ "userArn": f"arn:aws:iam::{account_id}:root",+ "userId": account_id,+ "principalOrgId": _principal_org_id(),+ "session": None,+ }+ session = sts_svc._sessions.get(access_key_id)+ if session is not None:+ return {+ "accessKey": access_key_id,+ "accountId": account_id,+ "userArn": session.get("Arn", ""),+ "userId": session.get("UserId", ""),+ "principalOrgId": _principal_org_id(),+ "session": session,+ }+ key_record = iam_svc._access_keys.get_scoped(account_id, None, access_key_id)+ if key_record is not None:+ user_name = key_record.get("UserName", "")+ user = iam_svc._users.get_scoped(account_id, None, user_name) or {}+ return {+ "accessKey": access_key_id,+ "accountId": account_id,+ "userArn": f"arn:aws:iam::{account_id}:user/{user_name}",+ "userId": user.get("UserId", ""),+ "principalOrgId": _principal_org_id(),+ "session": None,+ }+ return None++def enforce(access_key_id: str, iam_action: str, service: str,region: str, resource_arn: str = "*") -> EvalResult | AuthError | None:"""Check whether the request should be allowed.diff --git a/ministack/services/apigateway.py b/ministack/services/apigateway.pyindex 948efe7f..0aa4838c 100644--- a/ministack/services/apigateway.py+++ b/ministack/services/apigateway.py@@ -558,6 +558,36 @@ def _b64url_decode(segment: str) -> bytes:return base64.urlsafe_b64decode(padded.encode("utf-8"))+def _iam_caller_identity_v2(headers, query_params):+ """The payload-2.0 ``requestContext.authorizer.iam`` block for an AWS_IAM+ route — field names per the AWS-maintained typed event model+ (aws-lambda-go ``APIGatewayV2HTTPRequestContextAuthorizerIAMDescription``).+ Key resolution only; unknown keys return None and the event carries no+ authorizer block."""+ from ministack.core.iam_evaluator import resolve_caller_identity+ from ministack.core.router import extract_access_key_id++ info = resolve_caller_identity(extract_access_key_id(headers, query_params))+ if not info:+ return None+ iam = {+ "accessKey": info["accessKey"],+ "accountId": info["accountId"],+ "callerId": info["userId"] or None,+ "principalOrgId": info.get("principalOrgId"),+ "userArn": info["userArn"] or None,+ "userId": info["userId"] or None,+ }+ session = info.get("session") or {}+ if session.get("_identity_id"):+ iam["cognitoIdentity"] = {+ "amr": session.get("_cognito_amr") or [],+ "identityId": session.get("_identity_id"),+ "identityPoolId": session.get("_identity_pool_id"),+ }+ return iam++def _jwt_unauthorized(message: str = "Unauthorized") -> tuple:return 401, {"Content-Type": "application/json"}, json.dumps({"message": message}).encode("utf-8")@@ -1282,6 +1312,7 @@ async def _handle_execute_in_scope(authorizer_claims = Noneauthorizer_scopes = []authorizer_lambda_ctx = None+ authorizer_iam = Noneif auth_type == "JWT":authorizer_id = route.get("authorizerId")if not authorizer_id:@@ -1294,6 +1325,13 @@ async def _handle_execute_in_scope(return auth_errorauthorizer_claims = claims or {}authorizer_scopes = scopes or []+ elif auth_type == "AWS_IAM":+ # Same stance as REST (v1): signatures are never verified; a request+ # with no Authorization header is rejected, a resolvable access key+ # fills requestContext.authorizer.iam (aws-lambda-go event shape).+ if not any(k.lower() == "authorization" for k in request_headers):+ return _jwt_forbidden()+ authorizer_iam = _iam_caller_identity_v2(request_headers, query_params or {})elif auth_type == "CUSTOM":# A route's AuthorizationType is CUSTOM when it references a Lambda# (REQUEST-type) authorizer — same convention as REST (v1): the@@ -1344,6 +1382,7 @@ async def _handle_execute_in_scope(authorizer_claims=authorizer_claims,authorizer_scopes=authorizer_scopes,authorizer_lambda_context=authorizer_lambda_ctx,+ authorizer_iam=authorizer_iam,owner_account_id=owner_account_id,owner_region=owner_region,)@@ -1498,6 +1537,7 @@ async def _invoke_lambda_proxy(authorizer_claims=None,authorizer_scopes=None,authorizer_lambda_context=None,+ authorizer_iam=None,owner_account_id=None,owner_region=None,):@@ -1587,6 +1627,8 @@ async def _invoke_lambda_proxy(}elif authorizer_lambda_context is not None:event["requestContext"]["authorizer"] = {"lambda": authorizer_lambda_context}+ elif authorizer_iam is not None:+ event["requestContext"]["authorizer"] = {"iam": authorizer_iam}# Route through the central _execute_function dispatcher so CloudWatch# Logs emission and Docker log output work for API Gateway invocations.diff --git a/ministack/services/apigateway_v1.py b/ministack/services/apigateway_v1.pyindex 3dfe1a78..620ef6a4 100644--- a/ministack/services/apigateway_v1.py+++ b/ministack/services/apigateway_v1.py@@ -1522,6 +1522,44 @@ def _cache_authorizer_result(key, expires_at, policy_doc, context):_authorizer_cache[key] = (expires_at, policy_doc, context)+def _iam_caller_identity(headers, query_params):+ """The payload-1.0 ``requestContext.identity`` fields for an AWS_IAM method.++ Key resolution only — signatures are never verified. Unknown/absent keys+ return None and the event keeps its two-field identity (the documented+ no-IAM behaviour). For a Cognito identity-pool session the cognito* fields+ ride along, ``cognitoAuthenticationProvider`` in the documented+ ``<provider>,<provider>:CognitoSignIn:<sub>`` format.+ """+ from ministack.core.iam_evaluator import resolve_caller_identity+ from ministack.core.router import extract_access_key_id++ info = resolve_caller_identity(extract_access_key_id(headers, query_params))+ if not info:+ return None+ identity = {+ "accessKey": info["accessKey"],+ "accountId": info["accountId"],+ "caller": info["userId"] or None,+ "user": info["userId"] or None,+ "userArn": info["userArn"] or None,+ "principalOrgId": info.get("principalOrgId"),+ "cognitoAuthenticationProvider": None,+ "cognitoAuthenticationType": None,+ "cognitoIdentityId": None,+ "cognitoIdentityPoolId": None,+ }+ session = info.get("session") or {}+ if session.get("_identity_id"):+ identity.update({+ "cognitoAuthenticationProvider": session.get("_cognito_auth_provider"),+ "cognitoAuthenticationType": session.get("_cognito_auth_type"),+ "cognitoIdentityId": session.get("_identity_id"),+ "cognitoIdentityPoolId": session.get("_identity_pool_id"),+ })+ return identity++async def _authorize_request_v1(api_id, stage_name, method_obj, method, request_path, resource,headers, body, query_params, path_params, stage,@@ -1746,6 +1784,9 @@ async def _handle_execute_in_scope(# non-proxy `AWS` (custom / "lambda") integration returns the handler's# output as the body verbatim. Same event in, different response# contract out.+ caller_identity = None+ if (method_obj.get("authorizationType") or "").upper() == "AWS_IAM":+ caller_identity = _iam_caller_identity(headers, query_params)invoke = _invoke_lambda_proxy_v1 if int_type == "AWS_PROXY" else _invoke_lambda_custom_v1return await invoke(integration, api_id, stage_name, stage, resource, path, method,@@ -1754,6 +1795,7 @@ async def _handle_execute_in_scope(owner_region=owner_region,binary_media_types=api.get("binaryMediaTypes") or [],authorizer_context=authorizer_context,+ caller_identity=caller_identity,)elif int_type in ("HTTP_PROXY", "HTTP"):return await _invoke_http_proxy_v1(@@ -1798,6 +1840,7 @@ def _build_lambda_event_v1(*,binary_media_types=None,authorizer_context=None,+ caller_identity=None,):"""Build the API Gateway v1 payload format 1.0 event handed to Lambda.@@ -1866,6 +1909,11 @@ def _build_lambda_event_v1(# A custom authorizer's returned context (values stringified) plus its# principalId reach the integration under requestContext.authorizer.+ if caller_identity:+ # AWS_IAM methods report the resolved caller: accessKey/accountId/+ # caller/user/userArn, plus the cognito* fields for identity-pool+ # sessions — per the payload 1.0 identity shape.+ event["requestContext"]["identity"].update(caller_identity)if authorizer_context is not None:event["requestContext"]["authorizer"] = authorizer_context@@ -1889,6 +1937,7 @@ async def _invoke_lambda_proxy_v1(owner_region=None,binary_media_types=None,authorizer_context=None,+ caller_identity=None,):"""Invoke Lambda through an AWS_PROXY integration and interpret its`{statusCode, headers, body}` response envelope."""@@ -1899,6 +1948,7 @@ async def _invoke_lambda_proxy_v1(headers, body, query_params, path_params,binary_media_types=binary_media_types,authorizer_context=authorizer_context,+ caller_identity=caller_identity,)lambda_response, err = await _call_lambda(@@ -1989,6 +2039,7 @@ async def _invoke_lambda_custom_v1(owner_region=None,binary_media_types=None,authorizer_context=None,+ caller_identity=None,):"""Invoke Lambda through a non-proxy (custom) ``AWS`` integration.@@ -2011,6 +2062,7 @@ async def _invoke_lambda_custom_v1(headers, body, query_params, path_params,binary_media_types=binary_media_types,authorizer_context=authorizer_context,+ caller_identity=caller_identity,)result, err = await _call_lambda_raw(diff --git a/ministack/services/cloudformation/handlers.py b/ministack/services/cloudformation/handlers.pyindex a85033b9..74edba3b 100644--- a/ministack/services/cloudformation/handlers.py+++ b/ministack/services/cloudformation/handlers.py@@ -264,18 +264,12 @@ def _list_stacks(params):# --- DescribeStackEvents ---def _describe_stack_events(params):- from ministack.services.cloudformation import _stack_events, _stacks+ from ministack.services.cloudformation import _stack_eventsstack_name = _p(params, "StackName")if not stack_name:return _error("ValidationError", "StackName is required")- stack = _stacks.get(stack_name)- if not stack:- # Try by stack ID- for s in _stacks.values():- if s.get("StackId") == stack_name:- stack = s- break+ stack = _resolve_stack(stack_name)if not stack:return _error("ValidationError",f"Stack [{stack_name}] does not exist")@@ -385,17 +379,11 @@ def _describe_stack_resources(params):# --- ListStackResources ---def _list_stack_resources(params):- from ministack.services.cloudformation import _stacksstack_name = _p(params, "StackName")if not stack_name:return _error("ValidationError", "StackName is required")- stack = _stacks.get(stack_name)- if not stack:- for s in _stacks.values():- if s.get("StackId") == stack_name:- stack = s- break+ stack = _resolve_stack(stack_name)if not stack:return _error("ValidationError",f"Stack [{stack_name}] does not exist")@@ -422,15 +410,9 @@ def _list_stack_resources(params):# --- GetTemplate ---def _get_template(params):- from ministack.services.cloudformation import _stacksstack_name = _p(params, "StackName")- stack = _stacks.get(stack_name)- if not stack:- for s in _stacks.values():- if s.get("StackId") == stack_name:- stack = s- break+ stack = _resolve_stack(stack_name)if not stack:return _error("ValidationError",f"Stack [{stack_name}] does not exist")@@ -514,6 +496,12 @@ def _update_stack(params):return _error("ValidationError",f"Stack [{stack_name}] does not exist")+ # Address the stack by NAME from here on, as _delete_stack does: `_stacks`+ # is keyed by name, and `_deploy_stack_async` re-looks the stack up by this+ # value — handed the caller's stack ID it misses, returns silently, and the+ # stack is left UPDATE_IN_PROGRESS forever.+ stack_name = stack.get("StackName", stack_name)+current_status = stack.get("StackStatus", "")if current_status not in ("CREATE_COMPLETE", "UPDATE_COMPLETE","UPDATE_ROLLBACK_COMPLETE"):diff --git a/ministack/services/cloudformation/provisioners.py b/ministack/services/cloudformation/provisioners.pyindex da3520a5..f4c7bade 100644--- a/ministack/services/cloudformation/provisioners.py+++ b/ministack/services/cloudformation/provisioners.py@@ -3939,6 +3939,13 @@ def _cognito_user_pool_create(logical_id, props, stack_name):"AliasAttributes": props.get("AliasAttributes", []),"UsernameAttributes": props.get("UsernameAttributes", []),"MfaConfiguration": props.get("MfaConfiguration", "OFF"),+ # EnabledMfas (what CDK emits for mfaSecondFactor) maps onto the per+ # method config blocks GetUserPoolMfaConfig reports. Only the software+ # token block carries an Enabled flag in the API model; SMS and email+ # configs are message/role shapes that come from their own properties,+ # so a bare SMS_MFA/EMAIL_OTP entry has nothing faithful to invent.+ **({"SoftwareTokenMfaConfiguration": {"Enabled": True}}+ if "SOFTWARE_TOKEN_MFA" in (props.get("EnabledMfas") or []) else {}),"EstimatedNumberOfUsers": 0,"UserPoolTags": props.get("UserPoolTags", {}),"AdminCreateUserConfig": props.get("AdminCreateUserConfig", {diff --git a/ministack/services/cloudwatch.py b/ministack/services/cloudwatch.pyindex 76a5a5fc..8455d63c 100644--- a/ministack/services/cloudwatch.py+++ b/ministack/services/cloudwatch.py@@ -1710,7 +1710,9 @@ def _json_timestamp(value):if isinstance(value, str):epoch = _parse_ts(value)if epoch is not None:- return float(epoch)+ # Int epoch, not float: the project convention for JSON bodies+ # (Java SDK v2 rejects high-precision float timestamps).+ return int(epoch)return valuediff --git a/ministack/services/codebuild.py b/ministack/services/codebuild.pyindex 2d158f95..ff622f21 100644--- a/ministack/services/codebuild.py+++ b/ministack/services/codebuild.py@@ -206,7 +206,7 @@ WORKSPACE = "/tmp/ministack-codebuild"# Without this hook there is no configuration that makes CodeBuild work under# SELinux, and the failure is silent — the agent exits 0 after starting nothing,# and _execute_build reads that exit code as SUCCEEDED.-CODEBUILD_DOCKER_FLAGS = os.environ.get("MINISTACK_CODEBUILD_DOCKER_FLAGS", "")+CODEBUILD_DOCKER_FLAGS = os.environ.get("CODEBUILD_DOCKER_FLAGS", "")_PHASE_COMPLETE_RE = re.compile(r"Phase complete: ([A-Z_]+) State: ([A-Z_]+)")@@ -490,7 +490,19 @@ def _execute_build(build_id, project):_finish_build(build, "TIMED_OUT")else:exit_code = container.wait().get("StatusCode", 1)- _finish_build(build, "SUCCEEDED" if exit_code == 0 else "FAILED")+ if exit_code == 0 and not saw_phase:+ # The agent exits 0 even when it started nothing (e.g. it was+ # denied the Docker socket under SELinux), so a bare exit code+ # would report a build that never ran as SUCCEEDED. No "Phase+ # complete" line means no build phase executed: that is an+ # infrastructure failure, not a build result.+ logger.error(+ "Build %s: agent exited 0 without completing any phase; "+ "reporting FAULT", build_id,+ )+ _finish_build(build, "FAULT")+ else:+ _finish_build(build, "SUCCEEDED" if exit_code == 0 else "FAILED")logger.info("Build %s finished: %s", build_id, build["buildStatus"])except Exception:if _stop_requested(build_id):diff --git a/ministack/services/cognito.py b/ministack/services/cognito.pyindex 7e1a98eb..15115e7f 100644--- a/ministack/services/cognito.py+++ b/ministack/services/cognito.py@@ -60,6 +60,7 @@ Wire protocol:import base64import copyimport hashlib+import hmacimport html as html_modimport jsonimport logging@@ -67,6 +68,7 @@ import osimport reimport secretsimport string+import structimport timeimport zlibfrom datetime import datetime, timezone@@ -3565,7 +3567,15 @@ def _admin_respond_to_auth_challenge(data):user, _err = _resolve_user(pool, username)if _err:return _hidden_user_error(pool, cid, _err)- # Accept any TOTP code in emulator — no real TOTP validation+ # The code is checked against the secret VerifySoftwareToken enrolled.+ # A user enrolled before secrets were stored has nothing to check+ # against; that legacy state keeps the old accept-any behaviour until+ # re-enrolment.+ totp_secret = user.get("_totp_secret")+ if totp_secret and not _totp_matches(+ totp_secret, responses.get("SOFTWARE_TOKEN_MFA_CODE", "")):+ return error_response_json(+ "CodeMismatchException", "Invalid code received for user", 400)return json_response({"AuthenticationResult": _build_auth_result(pid, cid, user, client_metadata=client_metadata)})@@ -3904,7 +3914,15 @@ def _respond_to_auth_challenge(data):user, _err = _resolve_user(pool, username)if _err:return _hidden_user_error(pool, cid, _err)- # Accept any TOTP code in emulator+ # SOFTWARE_TOKEN_MFA checks the code against the enrolled secret+ # (legacy pre-secret enrolments keep accept-any until re-enrolment).+ # MFA_SETUP's verification already happened in VerifySoftwareToken.+ if challenge_name == "SOFTWARE_TOKEN_MFA":+ totp_secret = user.get("_totp_secret")+ if totp_secret and not _totp_matches(+ totp_secret, responses.get("SOFTWARE_TOKEN_MFA_CODE", "")):+ return error_response_json(+ "CodeMismatchException", "Invalid code received for user", 400)return json_response({"AuthenticationResult": _build_auth_result(pid, cid, user, client_metadata=client_metadata)})@@ -4114,68 +4132,74 @@ def _confirm_forgot_password(data):return json_response({})+def _user_pool_for_access_token(access_token):+ """Resolve (user, pool) from an access token across the pools in scope."""+ if not access_token:+ return None, None+ for pool in _user_pools.values():+ user = _user_from_token(access_token, pool)+ if user:+ return user, pool+ return None, None++def _change_password(data):access_token = data.get("AccessToken", "")if not access_token:return error_response_json("NotAuthorizedException", "Access token is missing.", 400)proposed = data.get("ProposedPassword", "")- # Decode token to find user and update password- for pool in _user_pools.values():- user = _user_from_token(access_token, pool)- if user:- pw_err = _validate_password(pool, proposed)- if pw_err:- return pw_err- user["_password"] = proposed- user["UserLastModifiedDate"] = _now_epoch()- return json_response({})- return error_response_json("NotAuthorizedException", "Invalid access token.", 400)+ user, pool = _user_pool_for_access_token(access_token)+ if not user:+ return error_response_json("NotAuthorizedException", "Invalid access token.", 400)+ pw_err = _validate_password(pool, proposed)+ if pw_err:+ return pw_err+ user["_password"] = proposed+ user["UserLastModifiedDate"] = _now_epoch()+ return json_response({})def _get_user(data):access_token = data.get("AccessToken", "")if not access_token:return error_response_json("NotAuthorizedException", "Access token is missing.", 400)- for pool in _user_pools.values():- user = _user_from_token(access_token, pool)- if user:- out = _user_out(user)- # GetUser uses UserAttributes, not Attributes (per AWS API shape)- out["UserAttributes"] = out.pop("Attributes", [])- out["UserMFASettingList"] = user.get("_mfa_enabled", [])- out["PreferredMfaSetting"] = user.get("_preferred_mfa", "")- return json_response(out)- return error_response_json("NotAuthorizedException", "Invalid access token.", 400)+ user, _pool = _user_pool_for_access_token(access_token)+ if not user:+ return error_response_json("NotAuthorizedException", "Invalid access token.", 400)+ out = _user_out(user)+ # GetUser uses UserAttributes, not Attributes (per AWS API shape)+ out["UserAttributes"] = out.pop("Attributes", [])+ out["UserMFASettingList"] = user.get("_mfa_enabled", [])+ out["PreferredMfaSetting"] = user.get("_preferred_mfa", "")+ return json_response(out)def _update_user_attributes(data):access_token = data.get("AccessToken", "")if not access_token:return error_response_json("NotAuthorizedException", "Access token is missing.", 400)- for pool in _user_pools.values():- user = _user_from_token(access_token, pool)- if user:- user["Attributes"] = _merge_attributes(- user.get("Attributes", []),- data.get("UserAttributes", []),- )- user["UserLastModifiedDate"] = _now_epoch()- return json_response({"CodeDeliveryDetailsList": []})- return error_response_json("NotAuthorizedException", "Invalid access token.", 400)+ user, _pool = _user_pool_for_access_token(access_token)+ if not user:+ return error_response_json("NotAuthorizedException", "Invalid access token.", 400)+ user["Attributes"] = _merge_attributes(+ user.get("Attributes", []),+ data.get("UserAttributes", []),+ )+ user["UserLastModifiedDate"] = _now_epoch()+ return json_response({"CodeDeliveryDetailsList": []})def _delete_user(data):access_token = data.get("AccessToken", "")if not access_token:return error_response_json("NotAuthorizedException", "Access token is missing.", 400)- for pool in _user_pools.values():- user = _user_from_token(access_token, pool)- if user:- username = user["Username"]- del pool["_users"][username]- pool["EstimatedNumberOfUsers"] = len(pool["_users"])- return json_response({})- return error_response_json("NotAuthorizedException", "Invalid access token.", 400)+ user, pool = _user_pool_for_access_token(access_token)+ if not user:+ return error_response_json("NotAuthorizedException", "Invalid access token.", 400)+ username = user["Username"]+ del pool["_users"][username]+ pool["EstimatedNumberOfUsers"] = len(pool["_users"])+ return json_response({})# ===========================================================================@@ -4840,12 +4864,11 @@ def _set_user_mfa_preference(data):access_token = data.get("AccessToken")if not access_token:return error_response_json("NotAuthorizedException", "Missing access token.", 400)- for pool in _user_pools.values():- user = _user_from_token(access_token, pool)- if user:- _apply_mfa_preference(user, data)- return json_response({})- return error_response_json("NotAuthorizedException", "Invalid access token.", 400)+ user, _pool = _user_pool_for_access_token(access_token)+ if not user:+ return error_response_json("NotAuthorizedException", "Invalid access token.", 400)+ _apply_mfa_preference(user, data)+ return json_response({})def _apply_mfa_preference(user: dict, data: dict):@@ -4909,31 +4932,84 @@ def _set_user_pool_mfa_config(data):return json_response(resp)+# Session string (from AssociateSoftwareToken) -> pending TOTP secret, for the+# MFA_SETUP flow where no access token exists yet. Ephemeral, keyed by an+# unguessable random session — the same isolation argument as the OAuth code+# stores above.+_totp_session_secrets: dict[str, str] = {}+++def _totp_code(secret_b32: str, step_offset: int = 0) -> str:+ """RFC 6238: HMAC-SHA1, 30-second step, 6 digits."""+ key = base64.b32decode(secret_b32 + "=" * (-len(secret_b32) % 8), casefold=True)+ counter = int(time.time() // 30) + step_offset+ mac = hmac.new(key, struct.pack(">Q", counter), hashlib.sha1).digest()+ offset = mac[-1] & 0x0F+ return f"{(struct.unpack('>I', mac[offset:offset + 4])[0] & 0x7FFFFFFF) % 10**6:06d}"+++def _totp_matches(secret_b32: str, user_code) -> bool:+ """Accept the current step plus one on either side, as authenticators drift."""+ if not secret_b32 or not isinstance(user_code, str):+ return False+ try:+ return any(hmac.compare_digest(_totp_code(secret_b32, off), user_code)+ for off in (-1, 0, 1))+ except Exception:+ return False+++def _user_for_access_token(access_token):+ return _user_pool_for_access_token(access_token)[0]++def _associate_software_token(data):- """Issue a stub TOTP secret. Works with both AccessToken and Session."""+ """Issue a TOTP secret and remember it as pending for verification."""secret = base64.b32encode(secrets.token_bytes(20)).decode()session = base64.b64encode(secrets.token_bytes(32)).decode()+ user = _user_for_access_token(data.get("AccessToken"))+ if user is not None:+ user["_totp_pending_secret"] = secret+ _totp_session_secrets[session] = secretreturn json_response({"SecretCode": secret, "Session": session})def _verify_software_token(data):- """Accept any TOTP code. Mark the user as TOTP-enrolled so auth flow issues the challenge."""+ """Verify the user's code against the pending secret (RFC 6238) and, on+ success, promote it to the active TOTP secret and mark the user enrolled."""access_token = data.get("AccessToken")- user_code = data.get("UserCode", "") # accepted regardless of value in emulator- friendly_name = data.get("FriendlyDeviceName", "TOTP device")-- if access_token:- # Find the user by token across all pools- for pool in _user_pools.values():- user = _user_from_token(access_token, pool)- if user:- user.setdefault("_mfa_enabled", [])- if "SOFTWARE_TOKEN_MFA" not in user["_mfa_enabled"]:- user["_mfa_enabled"].append("SOFTWARE_TOKEN_MFA")- user["_preferred_mfa"] = "SOFTWARE_TOKEN_MFA"- break-- return json_response({"Status": "SUCCESS"})+ session = data.get("Session")+ user_code = data.get("UserCode", "")++ user = _user_for_access_token(access_token)+ secret = None+ if user is not None:+ secret = user.get("_totp_pending_secret") or user.get("_totp_secret")+ if not secret and session:+ secret = _totp_session_secrets.get(session)+ if not secret:+ # Nothing was associated: there is no secret to verify a code against.+ return error_response_json(+ "EnableSoftwareTokenMFAException",+ "No software token MFA has been associated for verification.", 400)+ if not _totp_matches(secret, user_code):+ return error_response_json(+ "EnableSoftwareTokenMFAException", "Code mismatch", 400)++ if user is not None:+ user["_totp_secret"] = secret+ user.pop("_totp_pending_secret", None)+ user.setdefault("_mfa_enabled", [])+ if "SOFTWARE_TOKEN_MFA" not in user["_mfa_enabled"]:+ user["_mfa_enabled"].append("SOFTWARE_TOKEN_MFA")+ user["_preferred_mfa"] = "SOFTWARE_TOKEN_MFA"+ if session:+ _totp_session_secrets.pop(session, None)++ resp = {"Status": "SUCCESS"}+ if session:+ resp["Session"] = base64.b64encode(secrets.token_bytes(32)).decode()+ return json_response(resp)# ===========================================================================@@ -6409,13 +6485,80 @@ def _get_id(data):def _get_credentials_for_identity(data):+ """Vend identity-pool credentials AND register them as an STS session.++ The AUTH=true evaluator resolves temporary keys through ``sts._sessions``;+ unregistered vended keys were rejected as an invalid security token, so+ our own GetCredentialsForIdentity output could never call an enforced API.+ On AWS these credentials are the pool role assumed with the session name+ ``CognitoIdentityCredentials`` — GetCallerIdentity reports+ ``arn:aws:sts::<account>:assumed-role/<RoleName>/CognitoIdentityCredentials``.+ """identity_id = data.get("IdentityId", new_uuid())now = int(time.time())+ logins = data.get("Logins") or {}+ pool = None+ for _iid, candidate in _identity_pools.items():+ if identity_id in candidate.get("_identities", {}):+ pool = candidate+ if not logins:+ logins = candidate["_identities"][identity_id].get("Logins") or {}+ break++ flavor = "authenticated" if logins else "unauthenticated"+ role_arn = (pool or {}).get("_roles", {}).get(flavor, "")+ # A pool with no SetIdentityPoolRoles keeps vending (lenient, matching the+ # emulator's accept-all model) under a synthesized role name.+ role_name = role_arn.rpartition("/")[2] if role_arn else "CognitoIdentityPoolRole"++ access_key = f"ASIA{''.join(secrets.choice(string.ascii_uppercase + string.digits) for _ in range(16))}"+ secret_key = base64.b64encode(secrets.token_bytes(30)).decode()+ role_id = "AROA" + new_uuid().replace("-", "")[:17].upper()++ # The cognito* identity fields API Gateway reports for IAM-authorized+ # calls, precomputed here where the login tokens are at hand: the provider+ # string is "<provider>,<provider>:CognitoSignIn:<sub>" per the mapping+ # template reference, sub from the login's id token.+ provider_parts = []+ amr = ["authenticated"] if logins else ["unauthenticated"]+ for provider_name, login_token in logins.items():+ sub = ""+ try:+ payload = json.loads(base64.urlsafe_b64decode(+ login_token.split(".")[1] + "=="))+ sub = payload.get("sub", "")+ except Exception:+ pass+ # amr carries the bare provider name (what trust policies match with+ # ForAnyValue:StringLike per the Cognito RBAC doc) and, for a+ # user-pool sign-in, the CognitoSignIn entry alongside it.+ amr.append(provider_name)+ if sub:+ provider_parts.append(f"{provider_name},{provider_name}:CognitoSignIn:{sub}")+ amr.append(f"{provider_name}:CognitoSignIn:{sub}")+ else:+ provider_parts.append(provider_name)++ from ministack.services import sts as sts_svc+ sts_svc._sessions[access_key] = {+ "Arn": (f"arn:aws:sts::{get_account_id()}:assumed-role/"+ f"{role_name}/CognitoIdentityCredentials"),+ "UserId": f"{role_id}:CognitoIdentityCredentials",+ "SecretAccessKey": secret_key,+ "Expiration": now + 3600,+ "_identity_id": identity_id,+ "_identity_pool_id": (pool or {}).get("IdentityPoolId", ""),+ "_logins": logins,+ "_cognito_auth_type": "authenticated" if logins else "unauthenticated",+ "_cognito_auth_provider": ",".join(provider_parts) or None,+ "_cognito_amr": amr,+ }+return json_response({"IdentityId": identity_id,"Credentials": {- "AccessKeyId": f"ASIA{''.join(secrets.choice(string.ascii_uppercase + string.digits) for _ in range(16))}",- "SecretKey": base64.b64encode(secrets.token_bytes(30)).decode(),+ "AccessKeyId": access_key,+ "SecretKey": secret_key,"SessionToken": base64.b64encode(secrets.token_bytes(64)).decode(),"Expiration": now + 3600,},diff --git a/ministack/services/ec2.py b/ministack/services/ec2.pyindex 56429260..1116be0b 100644--- a/ministack/services/ec2.py+++ b/ministack/services/ec2.py@@ -1224,7 +1224,12 @@ def _image_view(image):"Architecture": image["Architecture"],"VirtualizationType": image["VirtualizationType"],"OwnerId": image.get("OwnerId") or get_account_id(),- "IsPublic": "false",+ # An AMI is public exactly when its launch permissions carry the+ # ``all`` group, as on AWS.+ "IsPublic": ("true" if any(perm.get("Group") == "all"+ for perm in image.get("LaunchPermissions") or [])+ else "false"),+ "LaunchPermissions": image.get("LaunchPermissions") or [],"Backed": True,}@@ -1344,9 +1349,19 @@ def _image_owner_matches(view, owners):def _image_executable_by_matches(view, users):"""ExecutableBy.N: "Specify an AWS account ID, self (the sender of the request), or all- (public AMIs)." Nothing here is shared by explicit launch permission, so only `all`- selects anything and it selects the public images."""- return "all" in users and view["IsPublic"] == "true"+ (public AMIs)." ``all`` selects public images; ``self`` or an account ID selects images+ whose launch permissions name that account explicitly."""+ perms = view.get("LaunchPermissions") or []+ perm_users = {perm.get("UserId") for perm in perms if perm.get("UserId")}+ account = get_account_id()+ for u in users:+ if u == "all" and view["IsPublic"] == "true":+ return True+ if u == "self" and account in perm_users:+ return True+ if u in perm_users:+ return True+ return Falsedef _describe_images(p):@@ -1356,7 +1371,23 @@ def _describe_images(p):filters = _parse_filters(p)# Registered images first: those are the ones that actually boot. The stubs# stay so a workload naming one keeps launching a metadata-only instance.- views = [_image_view(img) for img in _images.values()]+ # Visibility matches AWS: the caller's own images, plus other accounts'+ # images in this region whose launch permissions name the caller (or the+ # ``all`` group). A shared image keeps its owner's OwnerId.+ account = get_account_id()+ region = get_region()+ visible = []+ for (img_account, img_region, _key), img in _images.all_items():+ if img_region != region:+ continue+ if img_account == account:+ visible.append(img)+ continue+ perms = img.get("LaunchPermissions") or []+ if any(perm.get("Group") == "all" or perm.get("UserId") == account+ for perm in perms):+ visible.append(img)+ views = [_image_view(img) for img in visible]seen = {v["ImageId"] for v in views}views += [_stub_image_view(*stub) for stub in _STUB_AMIS if stub[0] not in seen]@@ -1955,8 +1986,27 @@ def _get_ministack_network(client):def _registered_image(image_id):- """The registered image record for an AMI id, or None."""- return _images.get(image_id) if image_id else None+ """The registered image record for an AMI id, or None.++ Resolves in the caller's scope first; a miss falls through to another+ account's image in this region that the caller holds a launch permission+ for (or a public one) — launch permission is exactly the right to run it.+ """+ if not image_id:+ return None+ image = _images.get(image_id)+ if image is not None:+ return image+ account = get_account_id()+ region = get_region()+ for (img_account, img_region, key), img in _images.all_items():+ if key != image_id or img_region != region or img_account == account:+ continue+ perms = img.get("LaunchPermissions") or []+ if any(perm.get("Group") == "all" or perm.get("UserId") == account+ for perm in perms):+ return img+ return None# Set the first time a container is launched. A registration can be withdrawn while its@@ -2073,6 +2123,105 @@ def _deregister_image(p):return _xml(200, "DeregisterImageResponse", "<return>true</return>")+def _parse_launch_permission_list(p, prefix):+ """LaunchPermission.{Add|Remove}.N.{UserId|Group} off the Query wire."""+ perms = []+ i = 1+ while True:+ user = _p(p, f"{prefix}.{i}.UserId")+ group = _p(p, f"{prefix}.{i}.Group")+ if not user and not group:+ break+ perms.append({"UserId": user} if user else {"Group": group})+ i += 1+ return perms+++def _modify_image_attribute(p):+ """launchPermission add/remove — the AMI sharing flow. Owner-only: another+ account's image answers ``AuthFailure`` "Not authorized for image:{id}"+ (the error real EC2 returns to a non-owner), an unknown id NotFound."""+ image_id = _p(p, "ImageId")+ if not image_id:+ return _error("MissingParameter", "The request must contain the parameter ImageId", 400)+ image = _images.get(image_id)+ if not image:+ region = get_region()+ if any(key == image_id and img_region == region+ for (_acct, img_region, key), _img in _images.all_items()):+ return _error("AuthFailure",+ f"Not authorized for image:{image_id}", 400)+ return _error("InvalidAMIID.NotFound",+ f"The image id '[{image_id}]' does not exist", 400)++ add = _parse_launch_permission_list(p, "LaunchPermission.Add")+ remove = _parse_launch_permission_list(p, "LaunchPermission.Remove")+ # Legacy flat form: Attribute=launchPermission + OperationType=add|remove+ # + UserId.N / UserGroup.N (what older SDKs and the CLI shorthand send).+ if not add and not remove and _p(p, "Attribute") == "launchPermission":+ flat = ([{"UserId": u} for u in _parse_member_list(p, "UserId")]+ + [{"Group": g} for g in _parse_member_list(p, "UserGroup")])+ if _p(p, "OperationType") == "remove":+ remove = flat+ else:+ add = flat+ if not add and not remove:+ return _error("InvalidParameterCombination",+ "The request must contain launch permissions to add or remove", 400)+ for perm in add + remove:+ if perm.get("Group") and perm["Group"] != "all":+ return _error("InvalidParameterValue",+ f"Value ({perm['Group']}) for parameter Group is invalid. Valid value: all", 400)++ perms = image.setdefault("LaunchPermissions", [])+ for perm in add:+ if perm not in perms:+ perms.append(perm)+ for perm in remove:+ if perm in perms:+ perms.remove(perm)+ return _xml(200, "ModifyImageAttributeResponse", "<return>true</return>")+++def _describe_image_attribute(p):+ image_id = _p(p, "ImageId")+ attribute = _p(p, "Attribute")+ image = _images.get(image_id) if image_id else None+ if not image:+ return _error("InvalidAMIID.NotFound",+ f"The image id '[{image_id}]' does not exist", 400)+ if attribute == "launchPermission":+ items = "".join(+ (f"<item><userId>{perm['UserId']}</userId></item>" if perm.get("UserId")+ else f"<item><group>{perm['Group']}</group></item>")+ for perm in image.get("LaunchPermissions") or [])+ return _xml(200, "DescribeImageAttributeResponse",+ f"<imageId>{image_id}</imageId>"+ f"<launchPermission>{items}</launchPermission>")+ if attribute == "description":+ return _xml(200, "DescribeImageAttributeResponse",+ f"<imageId>{image_id}</imageId>"+ f"<description><value>{_esc(image.get('Description') or '')}</value></description>")+ return _error("InvalidParameterValue",+ f"Value ({attribute}) for parameter attribute is invalid.", 400)+++def _reset_image_attribute(p):+ image_id = _p(p, "ImageId")+ attribute = _p(p, "Attribute")+ if attribute != "launchPermission":+ # launchPermission is the only resettable image attribute in the model.+ return _error("InvalidParameterValue",+ f"Value ({attribute}) for parameter attribute is invalid. "+ "Valid value: launchPermission", 400)+ image = _images.get(image_id) if image_id else None+ if not image:+ return _error("InvalidAMIID.NotFound",+ f"The image id '[{image_id}]' does not exist", 400)+ image["LaunchPermissions"] = []+ return _xml(200, "ResetImageAttributeResponse", "<return>true</return>")++# ── Container lifecycle ────────────────────────────────────def _vm_apply_container_ip(inst, container, network):@@ -6894,6 +7043,9 @@ _ACTION_MAP = {"ReplaceIamInstanceProfileAssociation": _replace_iam_instance_profile_association,"DescribeImages": _describe_images,"RegisterImage": _register_image,+ "ModifyImageAttribute": _modify_image_attribute,+ "DescribeImageAttribute": _describe_image_attribute,+ "ResetImageAttribute": _reset_image_attribute,"DeregisterImage": _deregister_image,"CreateSecurityGroup": _create_security_group,"DeleteSecurityGroup": _delete_security_group,diff --git a/ministack/services/lambda_svc.py b/ministack/services/lambda_svc.pyindex 191535ee..f6d07054 100644--- a/ministack/services/lambda_svc.py+++ b/ministack/services/lambda_svc.py@@ -1712,6 +1712,25 @@ def _validate_latest_mutation_qualifier(func_name: str, qualifier: str | None):)+def _is_rie_sentinel_scope(spec) -> bool:+ """True for the ARN scope the AWS Lambda RIE manufactures.++ Under ``LAMBDA_EXECUTOR=docker`` the handler's+ ``context.invoked_function_arn`` comes from the RIE inside the official+ image, which hardcodes ``arn:aws:lambda:us-east-1:012345678912:function:…``+ — account AND region, with no override (verified in+ aws-lambda-runtime-interface-emulator ``internal/lambda/rie/handlers.go``).+ A function that self-registers that ARN (the textbook+ ``context.invoked_function_arn`` pattern, e.g. into a Cognito+ ``LambdaConfig`` trigger) then points at a scope where nothing exists. An+ ARN carrying exactly that scope is treated as "self": it re-resolves in+ the caller's own account and region. A tenant genuinely running as+ 012345678912 in us-east-1 is unaffected — its account matches the caller+ and resolution never reaches this fallback.+ """+ return spec.account_id == "012345678912" and spec.region == "us-east-1"++def _get_func_record_for_ref(function_ref: str) -> tuple[dict | None, dict | None, str]:if isinstance(function_ref, str) and function_ref.startswith("arn:"):try:@@ -1722,8 +1741,11 @@ def _get_func_record_for_ref(function_ref: str) -> tuple[dict | None, dict | Nonname, qualifier = _lambda_function_name_and_qualifier_from_arn(function_ref)if name:if spec.account_id != get_account_id():- return None, None, name- func = _functions.get_scoped(get_account_id(), spec.region, name)+ if not _is_rie_sentinel_scope(spec):+ return None, None, name+ spec = None # RIE self-ARN: resolve in the caller's scope+ region = spec.region if spec else get_region()+ func = _functions.get_scoped(get_account_id(), region, name)if not _function_qualifier_exists(func, qualifier):return None, None, namerecord, config = _effective_func_record_for_qualifier(func, qualifier)@@ -1747,8 +1769,11 @@ def _get_base_func_record_for_ref(function_ref: str) -> tuple[dict | None, dictname, _qualifier = _lambda_function_name_and_qualifier_from_arn(function_ref)if name:if spec.account_id != get_account_id():- return None, None, name- func = _functions.get_scoped(get_account_id(), spec.region, name)+ if not _is_rie_sentinel_scope(spec):+ return None, None, name+ spec = None # RIE self-ARN: resolve in the caller's scope+ region = spec.region if spec else get_region()+ func = _functions.get_scoped(get_account_id(), region, name)return func, (func or {}).get("config"), namename, _qualifier = _resolve_name_and_qualifier(function_ref)@@ -3929,6 +3954,89 @@ def _spawn_lambda_container(config: dict, code_zip: bytes | None,raise+_PY_CTX_ARN_SHIM = '''\+"""MiniStack shim: hand user code the control-plane ARN in its context."""+import importlib+import os++_real = os.environ.get("_MS_REAL_HANDLER", "index.handler")+_mod_name, _, _fn_name = _real.rpartition(".")+_target = getattr(importlib.import_module(_mod_name.replace("/", ".")), _fn_name)+_ARN = os.environ.get("_LAMBDA_FUNCTION_ARN", "")+++def handler(event, context):+ if _ARN:+ try:+ context.invoked_function_arn = _ARN+ except Exception:+ pass+ return _target(event, context)+'''++_JS_CTX_ARN_SHIM = '''\+// MiniStack shim: hand user code the control-plane ARN in its context.+const path = require("path");+const fs = require("fs");+const REAL = process.env._MS_REAL_HANDLER || "index.handler";+const ARN = process.env._LAMBDA_FUNCTION_ARN || "";+const dot = REAL.lastIndexOf(".");+const modPart = REAL.slice(0, dot);+const fnName = REAL.slice(dot + 1);+let cached = null;+async function load() {+ if (cached) return cached;+ const base = path.join("/var/task", modPart);+ for (const ext of [".mjs", ".js", ".cjs"]) {+ const p = base + ext;+ if (fs.existsSync(p)) {+ const m = await import("file://" + p);+ cached = m[fnName] || (m.default && m.default[fnName]);+ if (cached) return cached;+ }+ }+ throw new Error("ministack context shim: handler not found: " + REAL);+}+exports.handler = async (event, context) => {+ if (ARN) { try { context.invokedFunctionArn = ARN; } catch (e) {} }+ const fn = await load();+ if (fn.length >= 3) {+ return await new Promise((resolve, reject) =>+ fn(event, context, (err, res) => (err ? reject(err) : resolve(res))));+ }+ return fn(event, context);+};+'''+++def _write_context_arn_shim(code_dir: str, runtime: str, handler: str) -> str | None:+ """Drop a context-ARN shim into the code dir; return its handler string.++ Returns None (no shim, original handler runs directly) for runtimes we+ can't wrap, a handler already pointing at the shim's name, or a code dir+ that already contains a same-named file.+ """+ if handler.rpartition(".")[0].replace("/", ".") == "_msctx_shim":+ return None+ if runtime.startswith("python"):+ name, shim_handler, source = "_msctx_shim.py", "_msctx_shim.handler", _PY_CTX_ARN_SHIM+ elif runtime.startswith("nodejs"):+ name, shim_handler, source = "_msctx_shim.js", "_msctx_shim.handler", _JS_CTX_ARN_SHIM+ else:+ return None+ shim_path = os.path.join(code_dir, name)+ if os.path.exists(shim_path):+ return None+ try:+ with open(shim_path, "w") as f:+ f.write(source)+ except OSError as exc:+ logger.warning("Lambda context-ARN shim not written (%s); "+ "container will report the RIE default ARN", exc)+ return None+ return shim_handler++def _spawn_lambda_container_impl(config: dict, code_zip: bytes | None,_pin_platform: bool, _made_tmpdirs: list):"""Create and start a Lambda container for the given config.@@ -4115,6 +4223,20 @@ def _spawn_lambda_container_impl(config: dict, code_zip: bytes | None,else:# Zip: RIE expects handler as CMD (or "bootstrap" for provided)run_kwargs["command"] = ["bootstrap"] if is_provided else [handler]+ # The RIE inside the official images hardcodes+ # arn:aws:lambda:us-east-1:012345678912:function:{name} as the+ # context's invoked_function_arn — account AND region, no override —+ # so a handler that self-registers its own ARN (the textbook+ # ``context.invoked_function_arn`` pattern) stores a scope where+ # nothing exists. For the runtimes whose code dir we own, run the+ # handler through a shim that overwrites the context ARN with the+ # control-plane FunctionArn (already in ``_LAMBDA_FUNCTION_ARN``)+ # before user code sees it. ``provided`` bootstraps and Image-type+ # functions own their code path end-to-end and cannot be shimmed.+ shim_cmd = _write_context_arn_shim(code_dir, runtime, handler)+ if shim_cmd:+ container_env["_MS_REAL_HANDLER"] = handler+ run_kwargs["command"] = [shim_cmd]if mounts:run_kwargs["mounts"] = mountsdiff --git a/ministack/services/rds.py b/ministack/services/rds.pyindex c10a339d..0acb5325 100644--- a/ministack/services/rds.py+++ b/ministack/services/rds.py@@ -3521,9 +3521,14 @@ def _cluster_endpoint_aliases(cluster):address. Docker's embedded DNS accepts dotted names, so the alias can be thereal endpoint rather than something invented alongside it.- The writer endpoint only. ReaderEndpoint has to follow whichever member is- currently a reader, and a cluster can promote one on failover, so pinning it- to the writer's container would answer with the wrong database.+ The reader endpoint rides along while reads cannot move off this+ container: ``CreateDBCluster`` hands out the ``cluster-ro-`` name too, and+ a consumer that stored it (Terraform's ``reader_endpoint`` during the+ creating apply) otherwise gets a name nothing ever registered — the only+ unresolvable endpoint we emit. In shared-container mode reads and writes+ are the same database, so both names belong on the one container. When+ PG streaming replication is on, a standby serves reads and the name is+ left off the writer's container."""value = cluster.get("Endpoint")# Two shapes in practice: a bare string when the cluster is created, and the@@ -3539,7 +3544,12 @@ def _cluster_endpoint_aliases(cluster):# host-run -> containerized restart, so it never qualifies.if (isinstance(value, str) and value and not value[0].isdigit()and value not in ("localhost", _MINISTACK_HOST)):- return [value]+ aliases = [value]+ if not _pg_cluster_replication_enabled(cluster) and ".cluster-" in value:+ # Same unique suffix as the writer name, so the reader alias is+ # derivable across relaunches without storing another field.+ aliases.append(value.replace(".cluster-", ".cluster-ro-", 1))+ return aliasesreturn []@@ -3987,16 +3997,24 @@ def _sync_cluster_endpoints(cluster):if not endpoint:returnreader_endpoint = _cluster_reader_endpoint(cluster) or endpoint- cluster["Endpoint"] = endpoint.get("Address", cluster.get("Endpoint", ""))+ writer_address = endpoint.get("Address", cluster.get("Endpoint", ""))+ cluster["Endpoint"] = writer_addressif reader_endpoint is endpoint:- # Falling back to the writer's container. The writer's Address may be a- # network alias, which is stable precisely because it is pinned to one- # container — and the reader endpoint must be free to move to a standby- # when one appears. Publish the address here, as before.- reader_address = (- cluster.get("_shared_internal_address")- or endpoint.get("Address", cluster.get("ReaderEndpoint", ""))- )+ # Falling back to the writer's container. When the writer publishes+ # its stable cluster- name (alias mode), the reader publishes the+ # matching cluster-ro- name — the same one registered as an alias on+ # the shared container — so DescribeDBClusters reports the AWS shape:+ # a stable reader name that, with no replica, connects to the primary.+ # Outside alias mode (host-run, default bridge) names cannot resolve,+ # so the address is published as before.+ if (isinstance(writer_address, str) and ".cluster-" in writer_address+ and not _pg_cluster_replication_enabled(cluster)):+ reader_address = writer_address.replace(".cluster-", ".cluster-ro-", 1)+ else:+ reader_address = (+ cluster.get("_shared_internal_address")+ or endpoint.get("Address", cluster.get("ReaderEndpoint", ""))+ )else:reader_address = reader_endpoint.get("Address", cluster.get("ReaderEndpoint", ""),diff --git a/ministack/services/sts.py b/ministack/services/sts.pyindex 020395d2..72fa4183 100644--- a/ministack/services/sts.py+++ b/ministack/services/sts.py@@ -122,11 +122,16 @@ async def handle_request(method, path, headers, body, query_params):role_account = role_arn.split(":")[4] if role_arn.count(":") > 4 else ""role = Noneif role_account and role_account != get_account_id():+ # The ARN's account is authoritative: a role that isn't found+ # there does NOT fall back to a same-named role in the caller's+ # account — that resolution exists nowhere in AWS, and the miss+ # must surface as the same AccessDenied a denial produces (AWS+ # deliberately doesn't disclose role existence).try:role = iam_svc._roles.get_scoped(role_account, None, role_name)except Exception:role = None- if role is None:+ else:role = iam_svc._roles.get(role_name)if role is None:return _error(403, "AccessDenied",diff --git a/tests/test_apigatewayv1.py b/tests/test_apigatewayv1.pyindex 6b34a624..9a1bebaf 100644--- a/tests/test_apigatewayv1.py+++ b/tests/test_apigatewayv1.py@@ -4178,3 +4178,106 @@ def test_apigwv1_cors_preflight_only_resource_falls_through(apigw_v1, lam):_proxy_fb_drop_api(apigw_v1, api_id)_proxy_fb_drop_lambda(lam, proxy_backend)_proxy_fb_drop_lambda(lam, preflight_backend)+++def test_apigwv1_aws_iam_method_fills_caller_identity(apigw_v1, lam, cognito_idp, cognito_identity):+ """An AWS_IAM method reports the resolved caller in requestContext.identity:+ accessKey/accountId/caller/user/userArn for any resolvable key, plus the+ four cognito* fields (documented CognitoSignIn format) when the request is+ signed with identity-pool credentials."""+ import http.client as _http+ import uuid as _uuid++ from botocore.auth import SigV4Auth+ from botocore.awsrequest import AWSRequest+ from botocore.credentials import Credentials++ fname = f"intg-v1-iam-ident-{_uuid.uuid4().hex[:8]}"+ code = (b"import json\n"+ b"def handler(event, context):\n"+ b" return {'statusCode': 200, 'body': json.dumps(event['requestContext']['identity'])}\n")+ buf = io.BytesIO()+ with zipfile.ZipFile(buf, "w") as zf:+ zf.writestr("index.py", code)+ lam.create_function(+ FunctionName=fname, Runtime="python3.12",+ Role="arn:aws:iam::000000000000:role/test-role",+ Handler="index.handler", Code={"ZipFile": buf.getvalue()},+ )++ api_id = apigw_v1.create_rest_api(name=f"v1-iam-ident-{fname}")["id"]+ pool_id = client_id = None+ try:+ root = next(r for r in apigw_v1.get_resources(restApiId=api_id)["items"] if r["path"] == "/")+ resource_id = apigw_v1.create_resource(+ restApiId=api_id, parentId=root["id"], pathPart="whoami",+ )["id"]+ apigw_v1.put_method(+ restApiId=api_id, resourceId=resource_id, httpMethod="GET",+ authorizationType="AWS_IAM",+ )+ apigw_v1.put_integration(+ restApiId=api_id, resourceId=resource_id, httpMethod="GET",+ type="AWS_PROXY", integrationHttpMethod="POST",+ uri=(+ f"arn:aws:apigateway:us-east-1:lambda:path/2015-03-31/functions/"+ f"arn:aws:lambda:us-east-1:000000000000:function:{fname}/invocations"+ ),+ )+ dep_id = apigw_v1.create_deployment(restApiId=api_id)["id"]+ apigw_v1.create_stage(restApiId=api_id, stageName="dev", deploymentId=dep_id)++ host = f"{api_id}.execute-api.localhost:{_EXECUTE_PORT}"++ def signed_call(creds):+ req = AWSRequest(method="GET", url=f"http://{host}/dev/whoami", headers={"Host": host})+ SigV4Auth(creds, "execute-api", "us-east-1").add_auth(req)+ conn = _http.HTTPConnection("127.0.0.1", _EXECUTE_PORT)+ conn.request("GET", "/dev/whoami", headers=dict(req.headers))+ resp = conn.getresponse()+ return resp.status, json.loads(resp.read().decode())++ status, ident = signed_call(Credentials("test", "test"))+ assert status == 200+ assert ident["accessKey"] == "test"+ assert ident["accountId"] == "000000000000"+ assert ident["userArn"] == "arn:aws:iam::000000000000:root"+ assert ident["cognitoIdentityId"] is None++ # Identity-pool credentials carry the cognito* fields.+ pool_id = cognito_idp.create_user_pool(PoolName=f"iam-ident-{fname}")["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="u", MessageAction="SUPPRESS")+ cognito_idp.admin_set_user_password(+ UserPoolId=pool_id, Username="u", Password="Passw0rd!x", Permanent=True)+ id_token = cognito_idp.initiate_auth(+ ClientId=client_id, AuthFlow="USER_PASSWORD_AUTH",+ AuthParameters={"USERNAME": "u", "PASSWORD": "Passw0rd!x"},+ )["AuthenticationResult"]["IdToken"]+ provider = f"cognito-idp.us-east-1.amazonaws.com/{pool_id}"+ idpool = cognito_identity.create_identity_pool(+ IdentityPoolName=f"ip-{fname}", AllowUnauthenticatedIdentities=False,+ CognitoIdentityProviders=[{"ProviderName": provider, "ClientId": client_id}],+ )["IdentityPoolId"]+ cognito_identity.set_identity_pool_roles(+ IdentityPoolId=idpool,+ Roles={"authenticated": "arn:aws:iam::000000000000:role/AuthPoolRole"})+ logins = {provider: id_token}+ identity_id = cognito_identity.get_id(IdentityPoolId=idpool, Logins=logins)["IdentityId"]+ c = cognito_identity.get_credentials_for_identity(+ IdentityId=identity_id, Logins=logins)["Credentials"]++ status, ident = signed_call(+ Credentials(c["AccessKeyId"], c["SecretKey"], c["SessionToken"]))+ assert status == 200+ assert ident["userArn"] == (+ "arn:aws:sts::000000000000:assumed-role/AuthPoolRole/CognitoIdentityCredentials")+ assert ident["cognitoIdentityId"] == identity_id+ assert ident["cognitoIdentityPoolId"] == idpool+ assert ident["cognitoAuthenticationType"] == "authenticated"+ assert ident["cognitoAuthenticationProvider"].startswith(f"{provider},{provider}:CognitoSignIn:")+ finally:+ apigw_v1.delete_rest_api(restApiId=api_id)+ lam.delete_function(FunctionName=fname)diff --git a/tests/test_apigatewayv2.py b/tests/test_apigatewayv2.pyindex 0ca9721c..16c945d3 100644--- a/tests/test_apigatewayv2.py+++ b/tests/test_apigatewayv2.py@@ -4197,3 +4197,95 @@ def test_concurrent_apigw_lambda_proxy_requests(apigw, lam):f"{codes.count(None)}/{CONCURRENCY_N} API Gateway requests got no response at all — "f"the Lambda dispatch wedged")+++def test_apigwv2_aws_iam_route_fills_authorizer_iam(apigw, lam, cognito_identity):+ """An AWS_IAM route reports the resolved caller under+ requestContext.authorizer.iam (payload 2.0 shape: accessKey/accountId/+ callerId/userArn/userId, plus cognitoIdentity for identity-pool sessions),+ and a request with no Authorization header is 403."""+ import http.client as _http++ from botocore.auth import SigV4Auth+ from botocore.awsrequest import AWSRequest+ from botocore.credentials import Credentials++ fname = f"v2-iam-ident-{_uuid_mod.uuid4().hex[:8]}"+ code = (b"import json\n"+ b"def handler(event, context):\n"+ b" return {'statusCode': 200,\n"+ b" 'body': json.dumps(event['requestContext'].get('authorizer'))}\n")+ buf = io.BytesIO()+ with zipfile.ZipFile(buf, "w") as zf:+ zf.writestr("index.py", code)+ lam.create_function(+ FunctionName=fname, Runtime="python3.12",+ Role="arn:aws:iam::000000000000:role/test-role",+ Handler="index.handler", Code={"ZipFile": buf.getvalue()},+ )+ fn_arn = f"arn:aws:lambda:us-east-1:000000000000:function:{fname}"++ api_id = apigw.create_api(Name=f"iam-ident-{fname}", ProtocolType="HTTP")["ApiId"]+ try:+ integ_id = apigw.create_integration(+ ApiId=api_id, IntegrationType="AWS_PROXY",+ IntegrationUri=fn_arn, PayloadFormatVersion="2.0",+ )["IntegrationId"]+ apigw.create_route(+ ApiId=api_id, RouteKey="GET /whoami",+ AuthorizationType="AWS_IAM", Target=f"integrations/{integ_id}",+ )+ apigw.create_stage(ApiId=api_id, StageName="$default", AutoDeploy=True)++ host = f"{api_id}.execute-api.localhost:{_EXECUTE_PORT}"++ # No Authorization header: 403 Forbidden.+ conn = _http.HTTPConnection("127.0.0.1", _EXECUTE_PORT)+ conn.request("GET", "/whoami", headers={"Host": host})+ assert conn.getresponse().status == 403++ def signed_call(creds):+ req = AWSRequest(method="GET", url=f"http://{host}/whoami", headers={"Host": host})+ SigV4Auth(creds, "execute-api", "us-east-1").add_auth(req)+ c2 = _http.HTTPConnection("127.0.0.1", _EXECUTE_PORT)+ c2.request("GET", "/whoami", headers=dict(req.headers))+ resp = c2.getresponse()+ return resp.status, json.loads(resp.read().decode())++ import boto3 as _boto3+ orgs = _boto3.client(+ "organizations", endpoint_url=_endpoint, region_name="us-east-1",+ aws_access_key_id="test", aws_secret_access_key="test")+ org_id = orgs.describe_organization()["Organization"]["Id"]++ status, authorizer = signed_call(Credentials("test", "test"))+ assert status == 200+ iam = authorizer["iam"]+ assert iam["accessKey"] == "test"+ assert iam["accountId"] == "000000000000"+ assert iam["userArn"] == "arn:aws:iam::000000000000:root"+ assert iam["principalOrgId"] == org_id+ assert "cognitoIdentity" not in iam++ idpool = cognito_identity.create_identity_pool(+ IdentityPoolName=f"ip-{fname}", AllowUnauthenticatedIdentities=True,+ )["IdentityPoolId"]+ cognito_identity.set_identity_pool_roles(+ IdentityPoolId=idpool,+ Roles={"unauthenticated": "arn:aws:iam::000000000000:role/UnauthPoolRole"})+ identity_id = cognito_identity.get_id(IdentityPoolId=idpool)["IdentityId"]+ c = cognito_identity.get_credentials_for_identity(+ IdentityId=identity_id)["Credentials"]++ status, authorizer = signed_call(+ Credentials(c["AccessKeyId"], c["SecretKey"], c["SessionToken"]))+ assert status == 200+ iam = authorizer["iam"]+ assert iam["userArn"] == (+ "arn:aws:sts::000000000000:assumed-role/UnauthPoolRole/CognitoIdentityCredentials")+ assert iam["cognitoIdentity"]["identityId"] == identity_id+ assert iam["cognitoIdentity"]["identityPoolId"] == idpool+ assert iam["cognitoIdentity"]["amr"] == ["unauthenticated"]+ finally:+ apigw.delete_api(ApiId=api_id)+ lam.delete_function(FunctionName=fname)diff --git a/tests/test_cfn.py b/tests/test_cfn.pyindex 8815ad4e..9cb84fba 100644--- a/tests/test_cfn.py+++ b/tests/test_cfn.py@@ -10848,3 +10848,51 @@ def test_cfn_update_iam_managed_policy_in_place(cfn, iam):assert document["Statement"][0]["Action"] == "s3:GetObject"finally:_delete_cfn_test_stack(cfn, stack_name)+++def test_cfn_stack_addressable_by_id_for_delete_and_update(cfn):+ """Every StackName parameter accepts the unique stack ID; the CDK CLI+ addresses stacks by ARN, so DeleteStack/UpdateStack must resolve it —+ a missed DeleteStack returned 200 and deleted nothing."""+ template = {+ "AWSTemplateFormatVersion": "2010-09-09",+ "Resources": {"Topic": {"Type": "AWS::SNS::Topic",+ "Properties": {"TopicName": "cfn-by-id-a"}}},+ }+ cfn.create_stack(StackName="cfn-by-id", TemplateBody=json.dumps(template))+ stack = _wait_stack(cfn, "cfn-by-id")+ assert stack["StackStatus"] == "CREATE_COMPLETE"+ stack_id = stack["StackId"]++ template["Resources"]["Topic"]["Properties"]["TopicName"] = "cfn-by-id-b"+ cfn.update_stack(StackName=stack_id, TemplateBody=json.dumps(template))+ assert _wait_stack(cfn, "cfn-by-id")["StackStatus"] == "UPDATE_COMPLETE"++ cfn.delete_stack(StackName=stack_id)+ _wait_stack(cfn, "cfn-by-id")+ described = cfn.describe_stacks(StackName=stack_id)["Stacks"]+ assert described[0]["StackStatus"] == "DELETE_COMPLETE"+++def test_cfn_cognito_user_pool_enabled_mfas(cfn, cognito_idp):+ """EnabledMfas (what CDK emits for mfaSecondFactor otp) maps onto the+ software-token config block GetUserPoolMfaConfig reports."""+ template = {+ "AWSTemplateFormatVersion": "2010-09-09",+ "Resources": {"Pool": {"Type": "AWS::Cognito::UserPool", "Properties": {+ "UserPoolName": "cfn-enabled-mfas",+ "MfaConfiguration": "OPTIONAL",+ "EnabledMfas": ["SOFTWARE_TOKEN_MFA"]}}},+ "Outputs": {"PoolId": {"Value": {"Ref": "Pool"}}},+ }+ cfn.create_stack(StackName="cfn-enabled-mfas", TemplateBody=json.dumps(template))+ stack = _wait_stack(cfn, "cfn-enabled-mfas")+ assert stack["StackStatus"] == "CREATE_COMPLETE"+ pool_id = {o["OutputKey"]: o["OutputValue"] for o in stack["Outputs"]}["PoolId"]++ cfg = cognito_idp.get_user_pool_mfa_config(UserPoolId=pool_id)+ assert cfg["MfaConfiguration"] == "OPTIONAL"+ assert cfg["SoftwareTokenMfaConfiguration"]["Enabled"] is True++ cfn.delete_stack(StackName="cfn-enabled-mfas")+ _wait_stack(cfn, "cfn-enabled-mfas")diff --git a/tests/test_cloudfront.py b/tests/test_cloudfront.pyindex e6766fcf..659385a4 100644--- a/tests/test_cloudfront.py+++ b/tests/test_cloudfront.py@@ -2546,3 +2546,19 @@ def test_cf_saas_connection_group_tagging(cloudfront):)tags = cloudfront.list_tags_for_resource(Resource=cg["Arn"])["Tags"]["Items"]assert {"Key": "env", "Value": "test"} in tags+++def test_cloudfront_get_distribution_xml_has_no_namespace_prefixes(cloudfront):+ """Re-serialising the client's own namespaced config used to invent ns0:+ prefixes on every child, which SDK REST-XML parsers read as absent members.+ Raw HTTP because boto3's parser is namespace-tolerant and masked the bug."""+ dist_id = cloudfront.create_distribution(+ DistributionConfig=_CF_DIST_CONFIG)["Distribution"]["Id"]+ req = urllib.request.Request(+ f"{ENDPOINT}/2020-05-31/distribution/{dist_id}",+ headers={"Authorization": "AWS4-HMAC-SHA256 Credential=test/20200101/us-east-1/cloudfront/aws4_request, SignedHeaders=host, Signature=00"},+ )+ with urllib.request.urlopen(req) as resp:+ raw = resp.read()+ assert b"ns0:" not in raw, "namespace prefixes leaked into the response XML"+ assert b"<Origins>" in raw and b"<DefaultCacheBehavior>" in rawdiff --git a/tests/test_cloudwatch.py b/tests/test_cloudwatch.pyindex c650b9a9..f5d8bc1e 100644--- a/tests/test_cloudwatch.py+++ b/tests/test_cloudwatch.py@@ -863,3 +863,45 @@ def test_cloudwatch_percentile_alarm_reason_reports_statistic(cw):assert alarm["StateValue"] == "ALARM"assert "p90" in alarm["StateReason"]assert "Average" not in alarm["StateReason"]+++def test_cloudwatch_json_timestamps_are_epoch_numbers(cw):+ """Over awsJson1_0 a smithy timestamp is epoch seconds; the JSON branches+ used to leak the XML path's ISO strings, which every SDK's number parser+ rejects. Raw HTTP because boto3 speaks Query here and would mask the shape.+ Int (not float) epoch is the project convention for JSON bodies."""+ import os+ import urllib.request++ ns = f"JsonTs{_uuid_mod.uuid4().hex[:8]}"+ cw.put_metric_data(+ Namespace=ns,+ MetricData=[{"MetricName": "Latency", "Value": 42.0}],+ )+ now = int(time.time())+ body = json.dumps({+ "Namespace": ns,+ "MetricName": "Latency",+ "StartTime": now - 600,+ "EndTime": now + 60,+ "Period": 60,+ "Statistics": ["Average"],+ }).encode()+ endpoint = os.environ.get("MINISTACK_ENDPOINT", "http://localhost:4566")+ req = urllib.request.Request(+ f"{endpoint}/",+ data=body,+ headers={+ "Content-Type": "application/x-amz-json-1.0",+ "X-Amz-Target": "GraniteServiceVersion20100801.GetMetricStatistics",+ "x-amzn-query-mode": "true",+ "Authorization": "AWS4-HMAC-SHA256 Credential=test/20200101/us-east-1/monitoring/aws4_request, SignedHeaders=host, Signature=00",+ },+ )+ with urllib.request.urlopen(req) as resp:+ payload = json.loads(resp.read())+ datapoints = payload.get("Datapoints", [])+ assert datapoints, "expected at least one datapoint"+ for dp in datapoints:+ assert isinstance(dp["Timestamp"], int), (+ f"Timestamp must be int epoch over JSON, got {type(dp['Timestamp']).__name__}: {dp['Timestamp']}")diff --git a/tests/test_codebuild.py b/tests/test_codebuild.pyindex e4a2d3e5..5fb089eb 100644--- a/tests/test_codebuild.py+++ b/tests/test_codebuild.py@@ -584,3 +584,23 @@ def test_execute_build_without_docker_reports_fault(monkeypatch, tmp_path):codebuild._execute_build("demo:0004", project)assert build["buildStatus"] == "FAULT"+++def test_execute_build_agent_exit_zero_without_phases_is_fault(monkeypatch, tmp_path):+ """The agent exits 0 even when it started nothing (e.g. denied the Docker+ socket under SELinux). A zero exit with no 'Phase complete' line means no+ build phase ran, so the outcome is FAULT — not a silent SUCCEEDED."""+ container = _FakeContainer([+ "permission denied while trying to connect to the Docker daemon socket",+ ])+ docker = _FakeDocker(container)+ monkeypatch.setattr(codebuild, "_get_docker", lambda: docker)+ monkeypatch.setattr(codebuild, "WORKSPACE", str(tmp_path))++ project = _execution_project()+ build = _seed_execution_build(project)++ codebuild._execute_build("demo:0001", project)++ assert build["buildStatus"] == "FAULT"+ assert container.removed is Truediff --git a/tests/test_cognito.py b/tests/test_cognito.pyindex 75ab060d..e8bc0583 100644--- a/tests/test_cognito.py+++ b/tests/test_cognito.py@@ -1504,6 +1504,19 @@ def test_cognito_force_change_password_challenge(cognito_idp):assert auth.get("ChallengeName") == "NEW_PASSWORD_REQUIRED"assert "Session" in auth+def _totp_code(secret_b32: str) -> str:+ """RFC 6238 code for the secret AssociateSoftwareToken returned —+ HMAC-SHA1, 30-second step, 6 digits."""+ import hashlib+ import hmac+ import struct++ key = base64.b32decode(secret_b32 + "=" * (-len(secret_b32) % 8), casefold=True)+ mac = hmac.new(key, struct.pack(">Q", int(time.time() // 30)), hashlib.sha1).digest()+ offset = mac[-1] & 0x0F+ return f"{(struct.unpack('>I', mac[offset:offset + 4])[0] & 0x7FFFFFFF) % 10**6:06d}"++def test_cognito_totp_full_flow(cognito_idp):"""Full TOTP MFA flow: SetUserPoolMfaConfig ON → AssociateSoftwareToken →VerifySoftwareToken → InitiateAuth returns SOFTWARE_TOKEN_MFA challenge →@@ -1544,8 +1557,13 @@ def test_cognito_totp_full_flow(cognito_idp):assert "SecretCode" in assocassert len(assoc["SecretCode"]) > 0- # Verify (accept any code)- verify = cognito_idp.verify_software_token(AccessToken=access_token, UserCode="123456")+ # A wrong code is refused — that refusal is what an MFA test exists for.+ with pytest.raises(ClientError) as exc:+ cognito_idp.verify_software_token(AccessToken=access_token, UserCode="000000")+ assert exc.value.response["Error"]["Code"] == "EnableSoftwareTokenMFAException"++ verify = cognito_idp.verify_software_token(+ AccessToken=access_token, UserCode=_totp_code(assoc["SecretCode"]))assert verify["Status"] == "SUCCESS"# Now auth should return SOFTWARE_TOKEN_MFA challenge@@ -1558,12 +1576,23 @@ def test_cognito_totp_full_flow(cognito_idp):assert auth2.get("ChallengeName") == "SOFTWARE_TOKEN_MFA"assert "Session" in auth2- # Respond with any TOTP code → get tokens+ # A wrong challenge code is refused with CodeMismatchException.+ with pytest.raises(ClientError) as exc:+ cognito_idp.admin_respond_to_auth_challenge(+ UserPoolId=pid,+ ClientId=cid,+ ChallengeName="SOFTWARE_TOKEN_MFA",+ ChallengeResponses={"USERNAME": "totp-user", "SOFTWARE_TOKEN_MFA_CODE": "000000"},+ )+ assert exc.value.response["Error"]["Code"] == "CodeMismatchException"++ # The correct code, derived from the enrolled secret, gets tokens.result = cognito_idp.admin_respond_to_auth_challenge(UserPoolId=pid,ClientId=cid,ChallengeName="SOFTWARE_TOKEN_MFA",- ChallengeResponses={"USERNAME": "totp-user", "SOFTWARE_TOKEN_MFA_CODE": "123456"},+ ChallengeResponses={"USERNAME": "totp-user",+ "SOFTWARE_TOKEN_MFA_CODE": _totp_code(assoc["SecretCode"])},)assert "AuthenticationResult" in resultassert "AccessToken" in result["AuthenticationResult"]@@ -5403,7 +5432,7 @@ def test_cognito_unsigned_user_pool_operations_pin_the_owning_region():assert software_token["SecretCode"]assert west.verify_software_token(AccessToken=access_token,- UserCode="123456",+ UserCode=_totp_code(software_token["SecretCode"]),)["Status"] == "SUCCESS"west.set_user_mfa_preference(AccessToken=access_token,@@ -8142,3 +8171,34 @@ def test_cognito_prevent_user_existence_errors_masks_srp_challenge(cognito_idp):assert srp_failure("[email protected]") == ("NotAuthorizedException", "Incorrect username or password.")+++def test_cognito_identity_pool_credentials_are_sts_sessions(cognito_identity):+ """Vended identity-pool credentials compose with the rest of the emulator:+ GetCallerIdentity resolves them as the pool role assumed with the AWS+ session name CognitoIdentityCredentials (previously they were unregistered,+ so AUTH=true rejected them as an invalid security token)."""+ import boto3++ pool_id = cognito_identity.create_identity_pool(+ IdentityPoolName="SessionPool", AllowUnauthenticatedIdentities=True,+ )["IdentityPoolId"]+ cognito_identity.set_identity_pool_roles(+ IdentityPoolId=pool_id,+ Roles={"unauthenticated": "arn:aws:iam::000000000000:role/UnauthPoolRole"},+ )+ identity_id = cognito_identity.get_id(IdentityPoolId=pool_id)["IdentityId"]+ creds = cognito_identity.get_credentials_for_identity(+ IdentityId=identity_id)["Credentials"]++ endpoint = os.environ.get("MINISTACK_ENDPOINT", "http://localhost:4566")+ session_sts = boto3.client(+ "sts", endpoint_url=endpoint, region_name="us-east-1",+ aws_access_key_id=creds["AccessKeyId"],+ aws_secret_access_key=creds["SecretKey"],+ aws_session_token=creds["SessionToken"],+ )+ ident = session_sts.get_caller_identity()+ assert ident["Arn"] == (+ "arn:aws:sts::000000000000:assumed-role/UnauthPoolRole/CognitoIdentityCredentials")+ assert ident["UserId"].endswith(":CognitoIdentityCredentials")diff --git a/tests/test_ec2.py b/tests/test_ec2.pyindex d7130c69..1dbb46a5 100644--- a/tests/test_ec2.py+++ b/tests/test_ec2.py@@ -4314,3 +4314,62 @@ def test_ec2_describe_images_scopes_by_executable_by(ec2):assert ec2.describe_images(ExecutableUsers=["self"])["Images"] == []finally:ec2.deregister_image(ImageId=ami)+++def test_ec2_cross_account_ami_sharing():+ """The normal AMI sharing flow: launch permissions make another account's+ image visible (owner preserved), Group=all makes it public, reset revokes."""+ import uuid as _uuid++ def _acct(account):+ return boto3.client(+ "ec2", endpoint_url=ENDPOINT, region_name="us-east-1",+ aws_access_key_id=account, aws_secret_access_key="test",+ )++ owner = _acct("111100002222")+ consumer = _acct("333300004444")+ name = f"xacct-share-{_uuid.uuid4().hex[:8]}"+ ami = owner.register_image(Name=name, ImageLocation="alpine:3.20")["ImageId"]++ def _consumer_sees():+ images = consumer.describe_images(+ Owners=["111100002222"],+ Filters=[{"Name": "name", "Values": [name]}])["Images"]+ return images++ assert _consumer_sees() == []++ owner.modify_image_attribute(+ ImageId=ami,+ LaunchPermission={"Add": [{"UserId": "333300004444"}]})+ shared = _consumer_sees()+ assert [i["ImageId"] for i in shared] == [ami]+ assert shared[0]["OwnerId"] == "111100002222"+ assert shared[0]["Public"] is False++ by_self = consumer.describe_images(ExecutableUsers=["self"])["Images"]+ assert ami in [i["ImageId"] for i in by_self]++ perms = owner.describe_image_attribute(+ ImageId=ami, Attribute="launchPermission")["LaunchPermissions"]+ assert perms == [{"UserId": "333300004444"}]++ owner.modify_image_attribute(+ ImageId=ami, LaunchPermission={"Add": [{"Group": "all"}]})+ assert consumer.describe_images(ImageIds=[ami])["Images"][0]["Public"] is True++ owner.reset_image_attribute(ImageId=ami, Attribute="launchPermission")+ assert _consumer_sees() == []++ # The consumer never gains modify rights: the image is not in their scope.+ from botocore.exceptions import ClientError+ import pytest as _pytest+ with _pytest.raises(ClientError) as exc:+ consumer.modify_image_attribute(+ ImageId=ami, LaunchPermission={"Add": [{"Group": "all"}]})+ # Real EC2 answers a non-owner with AuthFailure "Not authorized for image:...".+ assert exc.value.response["Error"]["Code"] == "AuthFailure"+ assert f"Not authorized for image:{ami}" in exc.value.response["Error"]["Message"]++ owner.deregister_image(ImageId=ami)diff --git a/tests/test_lambda.py b/tests/test_lambda.pyindex 30b338b9..ce9bdb58 100644--- a/tests/test_lambda.py+++ b/tests/test_lambda.py@@ -11292,3 +11292,46 @@ def test_snapstart_pending_version_reprovisions_on_restore():from ministack.core import lambda_runtime as _lr_lr.invalidate_worker("snap-restore-fn", account="000000000000",region="us-east-1")+++def test_lambda_rie_sentinel_arn_resolves_in_caller_scope():+ """An ARN carrying the RIE's hardcoded scope (us-east-1 / 012345678912 —+ what context.invoked_function_arn reports inside an unshimmed docker+ container) resolves as "self": in the caller's own account and region."""+ account_id = "555566667777"+ function_name = f"rie-sentinel-{_uuid_mod.uuid4().hex}"+ real_arn = f"arn:aws:lambda:eu-central-1:{account_id}:function:{function_name}"+ sentinel_arn = f"arn:aws:lambda:us-east-1:012345678912:function:{function_name}"+ original_account = get_account_id()+ original_region = get_region()++ lsvc._functions.set_scoped(+ account_id, "eu-central-1", function_name,+ {+ "config": {"FunctionName": function_name, "FunctionArn": real_arn},+ "versions": {},+ "aliases": {},+ },+ )+ try:+ set_request_account_id(account_id)+ set_request_region("eu-central-1")++ record, config, resolved_name = lsvc._get_func_record_for_ref(sentinel_arn)+ assert record is not None+ assert resolved_name == function_name+ assert config["FunctionArn"] == real_arn++ base_record, base_config, _ = lsvc._get_base_func_record_for_ref(sentinel_arn)+ assert base_record is not None+ assert base_config["FunctionArn"] == real_arn++ # Any OTHER foreign account is still rejected — the fallback is only+ # for the exact RIE-manufactured scope.+ other = f"arn:aws:lambda:eu-central-1:999999999999:function:{function_name}"+ rejected, _, _ = lsvc._get_func_record_for_ref(other)+ assert rejected is None+ finally:+ lsvc._functions.pop_scoped(account_id, "eu-central-1", function_name, None)+ set_request_account_id(original_account)+ set_request_region(original_region)diff --git a/tests/test_rds.py b/tests/test_rds.pyindex 88ac9917..f9ebb065 100644--- a/tests/test_rds.py+++ b/tests/test_rds.py@@ -14107,9 +14107,13 @@ def test_rds_cluster_endpoint_alias_survives_the_record_changing_shape():from ministack.services import rds as rds_servicename = "mydb.cluster-abc123.us-east-2.rds.amazonaws.com"- assert rds_service._cluster_endpoint_aliases({"Endpoint": name}) == [name]+ reader = "mydb.cluster-ro-abc123.us-east-2.rds.amazonaws.com"+ # The reader name rides along in shared-container mode: CreateDBCluster+ # hands it out, so it has to resolve to the container that serves reads —+ # which, while reads cannot move, is the writer's own container.+ assert rds_service._cluster_endpoint_aliases({"Endpoint": name}) == [name, reader]assert rds_service._cluster_endpoint_aliases(- {"Endpoint": {"Address": name, "Port": 5432}}) == [name]+ {"Endpoint": {"Address": name, "Port": 5432}}) == [name, reader]# An address is not a name. An earlier container may have left one behind,# and aliasing it would pin the endpoint to an address that has already moved.@@ -14123,11 +14127,12 @@ def test_rds_cluster_endpoint_alias_survives_the_record_changing_shape():{"Endpoint": {"Address": "172.20.0.4"}}) == []assert rds_service._cluster_endpoint_aliases({}) == []- # ReaderEndpoint is deliberately not aliased: it has to follow whichever- # member is currently a reader, and failover moves that.+ # The reader alias derives from the writer name (same unique suffix), not+ # from whatever ReaderEndpoint currently holds — after the first sync that+ # field carries an address, and the created name must survive relaunches.assert rds_service._cluster_endpoint_aliases(- {"Endpoint": name, "ReaderEndpoint": "ro.cluster-abc.rds.amazonaws.com"}- ) == [name]+ {"Endpoint": name, "ReaderEndpoint": "172.20.0.9"}+ ) == [name, reader]def test_rds_network_aliases_only_on_user_defined_networks():@@ -14162,3 +14167,18 @@ def test_rds_container_worker_does_not_clobber_an_intervened_stop(monkeypatch):instance["DBInstanceStatus"] = "creating"m._start_rds_container_for_instance("stopped-mid-create", instance)assert instance["DBInstanceStatus"] == "available"+++def test_rds_cluster_reader_alias_skipped_under_pg_replication(monkeypatch):+ """With PG streaming replication on, a standby serves reads — the+ cluster-ro- name must not pin to the writer's container."""+ from ministack.services import rds as rds_service++ name = "mydb.cluster-abc123.us-east-2.rds.amazonaws.com"+ monkeypatch.setattr(rds_service, "RDS_PG_CLUSTER_REPLICATION", True)+ assert rds_service._cluster_endpoint_aliases(+ {"Endpoint": name, "Engine": "aurora-postgresql"}) == [name]+ # Aurora MySQL keeps aliasing the shared container even with the flag on.+ assert rds_service._cluster_endpoint_aliases(+ {"Endpoint": name, "Engine": "aurora-mysql"}) == [+ name, "mydb.cluster-ro-abc123.us-east-2.rds.amazonaws.com"]diff --git a/tests/test_sts.py b/tests/test_sts.pyindex a8876828..0aac308a 100644--- a/tests/test_sts.py+++ b/tests/test_sts.py@@ -306,3 +306,63 @@ def test_get_caller_identity_different_roles_return_different_arns(sts_as_role):assert "RoleA" in arn_aassert "RoleB" in arn_bassert arn_a != arn_b+++def test_sts_cross_account_assumed_session_runs_in_role_account():+ """An assumed session's ASIA key resolves to the account of the role it+ assumed, so the session operates in that tenant — not the default one."""+ import boto3++ endpoint = os.environ.get("MINISTACK_ENDPOINT", "http://localhost:4566")+ caller = boto3.client(+ "sts", endpoint_url=endpoint, region_name="us-east-1",+ aws_access_key_id="111111111111", aws_secret_access_key="test",+ )+ resp = caller.assume_role(+ RoleArn="arn:aws:iam::999888777666:role/CrossReader",+ RoleSessionName="xacct",+ )+ assert resp["AssumedRoleUser"]["Arn"].startswith(+ "arn:aws:sts::999888777666:assumed-role/CrossReader/")+ creds = resp["Credentials"]+ session_sts = boto3.client(+ "sts", endpoint_url=endpoint, region_name="us-east-1",+ aws_access_key_id=creds["AccessKeyId"],+ aws_secret_access_key=creds["SecretAccessKey"],+ aws_session_token=creds["SessionToken"],+ )+ assert session_sts.get_caller_identity()["Account"] == "999888777666"+++def test_sts_assume_role_foreign_arn_never_falls_back_to_caller(monkeypatch):+ """Under AUTH, a role ARN naming another account resolves ONLY there: a+ miss is the same AccessDenied a denial produces (AWS never resolves the+ caller's same-named role, and never discloses role existence)."""+ import asyncio++ import ministack.app as app_mod+ from ministack.core.responses import set_request_account_id+ from ministack.services import iam as iam_svc+ from ministack.services import sts as sts_mod++ monkeypatch.setattr(app_mod, "AUTH", True)+ set_request_account_id("111111111111")+ # The caller's own account HAS a role with the same name — the trap the+ # fallback used to fall into.+ iam_svc._roles.set_scoped("111111111111", None, "Trap", {+ "RoleName": "Trap",+ "Arn": "arn:aws:iam::111111111111:role/Trap",+ "AssumeRolePolicyDocument": "{}",+ })+ body = (+ "Action=AssumeRole&Version=2011-06-15"+ "&RoleArn=arn%3Aaws%3Aiam%3A%3A999999999999%3Arole%2FTrap"+ "&RoleSessionName=probe"+ ).encode()+ status, _headers, payload = asyncio.run(sts_mod.handle_request(+ "POST", "/", {"content-type": "application/x-www-form-urlencoded"},+ body, {},+ ))+ assert status == 403+ assert b"AccessDenied" in payload+ iam_svc._roles.pop_scoped("111111111111", None, "Trap", None)