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
release/v1.3.22 (#536)
Nahuel Nucerae8e73dd0fd29
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 0fb45cec..34356d99 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -7,6 +7,20 @@ Versioning follows [Semantic Versioning](https://semver.org/).---+## [1.3.22] — 2026-04-30++### Added+- **Cognito PreTokenGeneration Lambda trigger** — `LambdaConfig.PreTokenGenerationConfig` (V2_0) and the legacy `LambdaConfig.PreTokenGeneration` (V1_0) are now round-tripped through `CreateUserPool` / `UpdateUserPool` / `DescribeUserPool` and **invoked** at token-mint time. Before signing an access or id token, ministack synchronously invokes the configured Lambda with the AWS-shaped event (`triggerSource`, `userPoolId`, `request.userAttributes`, `request.groupConfiguration`, `request.scopes` for V2+, `callerContext.clientId`, etc.) and applies the Lambda's `response.claimsAndScopeOverrideDetails.{accessTokenGeneration,idTokenGeneration}` (V2_0: `claimsToAddOrOverride`, `claimsToSuppress`, `scopesToAdd`, `scopesToSuppress`, `groupOverrideDetails`) — or the legacy `response.claimsOverrideDetails` (V1_0, id token only). Refresh tokens are opaque in AWS and skip the trigger. Lambda errors fail open (token issued without overrides + warning logged); set `MINISTACK_COGNITO_PRETOKEN_STRICT=1` to fail closed the way real AWS does. Invocation reuses the existing `_resolve_name_and_qualifier` → `_get_func_record_for_qualifier` → `_execute_function` chain in `lambda_svc.py` — no new handlers added. Reported by @aahoughton (#533).+- **S3 PostObject (browser-based form upload)** — `POST /<bucket>/` with `multipart/form-data` is now handled. Honours `key` (with `${filename}` substitution from the file part), `Content-Type`, `x-amz-meta-*`, `x-amz-storage-class`, `x-amz-tagging`, the object-lock headers, `success_action_status` (200/201/204; default 204), and `success_action_redirect` (303 with `bucket=&key=&etag=` appended). On 201 returns the `<PostResponse>` XML with `Location`/`Bucket`/`Key`/`ETag`. Versioning, persistence, multi-tenancy, S3 event notifications all flow through the same path as `PutObject`. The `content-length-range` policy condition **is** enforced — uploads under the minimum return `EntityTooSmall` 400 and uploads over the maximum return `EntityTooLarge` 400 (matches AWS error codes). Other policy conditions and the signature field are accepted but not validated — same lenient stance as ministack's presigned-URL handling. boto3's `generate_presigned_post` works end-to-end. Requested by @mattburton (#535).+- **Init / ready scripts: expose `MINISTACK_INIT_SCRIPT_DIR` and `MINISTACK_INIT_SCRIPT_PATH` to each script** — every `.sh` / `.py` run from `/docker-entrypoint-initaws.d[/ready.d]` (or `/etc/localstack/init/{boot,ready}.d`) now sees its own directory and absolute path in the environment, so scripts can reference sibling files (`aws s3 cp "${MINISTACK_INIT_SCRIPT_DIR}/data.json" s3://bucket/`) without hardcoding the mount path or computing `dirname "${BASH_SOURCE[0]}"`. Phase-level `MINISTACK_INIT_BOOT_DIR` / `MINISTACK_INIT_READY_DIR` are also set when those directories exist. Requested by @andreluiznsilva (#520).+- **EC2 Instance Metadata Service (IMDS) emulator** — new `imds` service responds on the gateway port at `/latest/api/token` (IMDSv2) and `/latest/meta-data/...` / `/latest/dynamic/instance-identity/document`. Returns a credentials document under role `ministack-instance-role` so SDKs that fall through to the IMDS step of the default credential chain (boto3, aws-sdk-go-v2, AWS SDK Java v2) get a valid `ASIA*` session key + token. Both IMDSv1 (token-less) and IMDSv2 (PUT /token → GET with `X-aws-ec2-metadata-token`) supported; set `MINISTACK_IMDS_V2_REQUIRED=1` to reject token-less requests, matching AWS hop-limit-1 IMDSv2-only instances. Point SDKs at ministack via `AWS_EC2_METADATA_SERVICE_ENDPOINT=http://localhost:4566` (or `ec2_metadata_service_endpoint` in `~/.aws/config`); we don't bind the link-local 169.254.169.254 IP — that's a per-container network-alias concern, not portable from inside ministack. Reported by @bimargulies++### Fixed+- **S3 PutObject `StorageClass` was dropped on the floor** — objects written with `StorageClass=GLACIER` / `INTELLIGENT_TIERING` / etc. came back from `GetObject`, `HeadObject`, `ListObjects(V2)`, and `ListObjectVersions` as `STANDARD`. The header is now stored on the object record and emitted on the wire (header omitted for the default `STANDARD`, matching AWS). Same propagation through `CopyObject` (with optional override via `x-amz-storage-class`) and `CreateMultipartUpload` → `CompleteMultipartUpload`. Unknown storage class values now return `InvalidStorageClass` (400). Verified against `botocore/data/s3/2006-03-01/service-2.json`. Reported by @JoeHale (#534).++---++## [1.3.21] — 2026-04-29### Addeddiff --git a/README.md b/README.mdindex d0d847ea..c9ce97fe 100644--- a/README.md+++ b/README.md@@ -352,6 +352,7 @@ subnet = ec2.create_subnet(| **Lambda** | CreateFunction, DeleteFunction, GetFunction, GetFunctionConfiguration, ListFunctions, Invoke, UpdateFunctionCode, UpdateFunctionConfiguration, AddPermission, RemovePermission, GetPolicy, ListVersionsByFunction, PublishVersion, CreateAlias, GetAlias, UpdateAlias, DeleteAlias, ListAliases, TagResource, UntagResource, ListTags, CreateEventSourceMapping, DeleteEventSourceMapping, GetEventSourceMapping, ListEventSourceMappings, UpdateEventSourceMapping, CreateFunctionUrlConfig, GetFunctionUrlConfig, UpdateFunctionUrlConfig, DeleteFunctionUrlConfig, ListFunctionUrlConfigs, PutFunctionConcurrency, GetFunctionConcurrency, DeleteFunctionConcurrency, PutFunctionEventInvokeConfig, GetFunctionEventInvokeConfig, DeleteFunctionEventInvokeConfig, PublishLayerVersion, GetLayerVersion, GetLayerVersionByArn, ListLayerVersions, DeleteLayerVersion, ListLayers, AddLayerVersionPermission, RemoveLayerVersionPermission, GetLayerVersionPolicy | Python and Node.js runtimes execute with warm worker pool; `provided.al2023`/`provided.al2` runtimes execute via Docker RIE (Go, Rust, C++ support); `Publish=True` creates immutable numbered versions; Code via `ZipFile`, `S3Bucket`/`S3Key` (with optional `S3ObjectVersion`), or `ImageUri` (Docker image); `PackageType: Image` pulls and invokes user-provided Docker images via Lambda RIE; SQS, Kinesis, and DynamoDB Streams event source mappings; Function URL CRUD; Lambda Layers CRUD; Aliases; Concurrency; EventInvokeConfig || **IAM** | CreateUser, GetUser, ListUsers, DeleteUser, CreateRole, GetRole, ListRoles, DeleteRole, CreatePolicy, GetPolicy, DeletePolicy, AttachRolePolicy, DetachRolePolicy, PutRolePolicy, GetRolePolicy, DeleteRolePolicy, ListRolePolicies, ListAttachedRolePolicies, CreateAccessKey, ListAccessKeys, DeleteAccessKey, CreateInstanceProfile, GetInstanceProfile, DeleteInstanceProfile, AddRoleToInstanceProfile, RemoveRoleFromInstanceProfile, ListInstanceProfiles, CreateGroup, GetGroup, AddUserToGroup, RemoveUserFromGroup, CreateServiceLinkedRole, DeleteServiceLinkedRole, GetServiceLinkedRoleDeletionStatus, CreateOpenIDConnectProvider, TagRole, UntagRole, TagUser, UntagUser, TagPolicy, UntagPolicy | || **STS** | GetCallerIdentity, AssumeRole, GetSessionToken, AssumeRoleWithWebIdentity | |+| **IMDS** (EC2 Instance Metadata) | `PUT /latest/api/token`, `GET /latest/meta-data/instance-id`, `GET /latest/meta-data/iam/security-credentials/`, `GET /latest/meta-data/iam/security-credentials/<role>`, `GET /latest/meta-data/iam/info`, `GET /latest/meta-data/placement/{region,availability-zone,...}`, `GET /latest/dynamic/instance-identity/document` | IMDSv1 + IMDSv2; default credential chain falls through to a `ministack-instance-role` document with `ASIA*` session creds. Point SDKs at ministack via `AWS_EC2_METADATA_SERVICE_ENDPOINT=http://localhost:4566` (or `ec2_metadata_service_endpoint` in `~/.aws/config`); set `MINISTACK_IMDS_V2_REQUIRED=1` to require the token PUT || **SecretsManager** | CreateSecret, GetSecretValue, ListSecrets, DeleteSecret, UpdateSecret, DescribeSecret, PutSecretValue, UpdateSecretVersionStage, RestoreSecret, RotateSecret, GetRandomPassword, ListSecretVersionIds, ReplicateSecretToRegions, TagResource, UntagResource, PutResourcePolicy, GetResourcePolicy, DeleteResourcePolicy, ValidateResourcePolicy | || **CloudWatch Logs** | CreateLogGroup, DeleteLogGroup, DescribeLogGroups, CreateLogStream, DeleteLogStream, DescribeLogStreams, PutLogEvents, GetLogEvents, FilterLogEvents, PutRetentionPolicy, DeleteRetentionPolicy, PutSubscriptionFilter, DeleteSubscriptionFilter, DescribeSubscriptionFilters, PutMetricFilter, DeleteMetricFilter, DescribeMetricFilters, TagLogGroup, UntagLogGroup, ListTagsLogGroup, TagResource, UntagResource, ListTagsForResource, StartQuery, GetQueryResults, StopQuery, PutDestination, DeleteDestination, DescribeDestinations, PutDestinationPolicy | `FilterLogEvents` supports `*`/`?` globs, multi-term AND, `-term` exclusion |@@ -664,6 +665,7 @@ ecs.stop_task(cluster="dev", task=task_arn)| `USE_SSL` | `0` | Enable HTTPS on the gateway listener. Accepts `1`, `true`, `yes`. LocalStack-compatible flag name || `MINISTACK_SSL_CERT` | _(unset)_ | Optional PEM-encoded server certificate path; required together with `MINISTACK_SSL_KEY`. When unset, MiniStack auto-generates a self-signed cert under `${TMPDIR}/ministack-tls/` (cached across restarts) || `MINISTACK_SSL_KEY` | _(unset)_ | Optional PEM-encoded private key path; required together with `MINISTACK_SSL_CERT` |+| `MINISTACK_IMDS_V2_REQUIRED` | `0` | Reject token-less GETs on `/latest/meta-data/...`. When set, callers must first `PUT /latest/api/token` and pass the token as `X-aws-ec2-metadata-token`, matching real-AWS hop-limit-1 IMDSv2-only instances |### API Gateway HTTP proxy execution model@@ -683,9 +685,21 @@ If the same filename exists in both paths, the MiniStack-native path takes priorInit scripts automatically receive `AWS_ACCESS_KEY_ID`, `AWS_SECRET_ACCESS_KEY`, `AWS_DEFAULT_REGION`, and `AWS_ENDPOINT_URL` — no manual configuration needed. The `aws` CLI is bundled in the image.+Each script also gets:++| Variable | Set per | Value |+|----------|---------|-------|+| `MINISTACK_INIT_SCRIPT_DIR` | script | Absolute path of the directory the running script lives in |+| `MINISTACK_INIT_SCRIPT_PATH` | script | Absolute path of the running script |+| `MINISTACK_INIT_BOOT_DIR` | phase | Boot-phase directory (`/docker-entrypoint-initaws.d` or `/etc/localstack/init/boot.d`) when present |+| `MINISTACK_INIT_READY_DIR` | phase | Ready-phase directory (`/docker-entrypoint-initaws.d/ready.d` or `/etc/localstack/init/ready.d`) when present |++This lets a script reference sibling files without hardcoding the mount path:+```bash# ready.d/01-create-resources.shaws s3 mb s3://my-bucket+aws s3 cp "${MINISTACK_INIT_SCRIPT_DIR}/seed-data.json" s3://my-bucket/aws sqs create-queue --queue-name my-queue```diff --git a/ministack/app.py b/ministack/app.pyindex 6bc78237..2746c445 100644--- a/ministack/app.py+++ b/ministack/app.py@@ -202,6 +202,7 @@ SERVICE_REGISTRY = {"firehose": {"module": "firehose", "aliases": ("kinesis-firehose",)},"glue": {"module": "glue"},"iam": {"module": "iam"},+ "imds": {"module": "imds"},"kinesis": {"module": "kinesis"},"kms": {"module": "kms"},"lambda": {"module": "lambda_svc"},@@ -1300,16 +1301,23 @@ async def _run_ready_scripts():script_env.setdefault("AWS_SECRET_ACCESS_KEY", "test")script_env.setdefault("AWS_DEFAULT_REGION", os.environ.get("MINISTACK_REGION", "us-east-1"))script_env.setdefault("AWS_ENDPOINT_URL", f"http://{_MINISTACK_HOST}:{port}")+ for ready_dir in ('/docker-entrypoint-initaws.d/ready.d', '/etc/localstack/init/ready.d'):+ if os.path.isdir(ready_dir):+ script_env.setdefault("MINISTACK_INIT_READY_DIR", ready_dir)+ breakfor script_path in scripts:logger.info('Running ready script: %s', script_path)script_failed = Falsetry:cmd = [sys.executable, script_path] if script_path.endswith('.py') else ['sh', script_path]+ per_script_env = {**script_env,+ "MINISTACK_INIT_SCRIPT_DIR": os.path.dirname(script_path),+ "MINISTACK_INIT_SCRIPT_PATH": script_path}proc = await asyncio.create_subprocess_exec(*cmd,stdout=asyncio.subprocess.PIPE,stderr=asyncio.subprocess.PIPE,- env=script_env,+ env=per_script_env,)stdout, stderr = await asyncio.wait_for(proc.communicate(), timeout=300)if stdout:@@ -1351,12 +1359,20 @@ def _run_init_scripts():if not scripts:returnlogger.info("Found %d init script(s)", len(scripts))+ base_env = {**os.environ}+ for boot_dir in ('/docker-entrypoint-initaws.d', '/etc/localstack/init/boot.d'):+ if os.path.isdir(boot_dir):+ base_env.setdefault("MINISTACK_INIT_BOOT_DIR", boot_dir)+ breakfor script_path in scripts:logger.info("Running init script: %s", script_path)try:cmd = [sys.executable, script_path] if script_path.endswith('.py') else ["sh", script_path]+ per_script_env = {**base_env,+ "MINISTACK_INIT_SCRIPT_DIR": os.path.dirname(script_path),+ "MINISTACK_INIT_SCRIPT_PATH": script_path}result = subprocess.run(- cmd, env=os.environ,+ cmd, env=per_script_env,capture_output=True, text=True, timeout=300,)if result.stdout:diff --git a/ministack/core/router.py b/ministack/core/router.pyindex cf56e14b..24aed528 100644--- a/ministack/core/router.py+++ b/ministack/core/router.py@@ -525,6 +525,8 @@ def detect_service(method: str, path: str, headers: dict, query_params: dict) -># 4. Check URL path patternspath_lower = path.lower()+ if path_lower.startswith("/latest/"):+ return "imds"if path_lower.startswith("/v1/apis") or path_lower.startswith("/v1/tags/arn:aws:appsync"):return "appsync"if path_lower.startswith("/2020-05-31/"):diff --git a/ministack/services/cognito.py b/ministack/services/cognito.pyindex 49c42b6c..518a7593 100644--- a/ministack/services/cognito.py+++ b/ministack/services/cognito.py@@ -327,8 +327,14 @@ def _identity_id(pool_id: str) -> str:def _fake_token(sub: str, pool_id: str, client_id: str, token_type: str = "access",username: str = "", user_attrs: dict | None = None,- groups: list[str] | None = None) -> str:- """Return a JWT signed with the RSA key when cryptography is available."""+ groups: list[str] | None = None,+ trigger_source: str = "TokenGeneration_Authentication") -> str:+ """Return a JWT signed with the RSA key when cryptography is available.++ For ``access`` and ``id`` tokens, runs the user pool's PreTokenGeneration+ Lambda trigger (V2_0 — both tokens; V1_0 — id token only) before signing.+ Refresh tokens are opaque in AWS and skip the trigger.+ """header = base64.urlsafe_b64encode(json.dumps({"alg": "RS256", "kid": "ministack-key-1"}).encode()).rstrip(b"=").decode()@@ -371,6 +377,19 @@ def _fake_token(sub: str, pool_id: str, client_id: str, token_type: str = "acceselse:# RefreshToken — opaque in real AWS, but we use a JWT stub for simplicityclaims["client_id"] = client_id++ if token_type in ("access", "id"):+ claims = _apply_pretoken_trigger(+ pool_id=pool_id,+ claims=claims,+ token_type=token_type,+ trigger_source=trigger_source,+ client_id=client_id,+ username=username,+ user_attrs=user_attrs or {},+ groups=groups or [],+ )+payload = base64.urlsafe_b64encode(json.dumps(claims).encode()).rstrip(b"=").decode()@@ -385,6 +404,177 @@ def _fake_token(sub: str, pool_id: str, client_id: str, token_type: str = "accesreturn f"{header}.{payload}.{sig}"+def _apply_pretoken_trigger(pool_id: str, claims: dict, token_type: str,+ trigger_source: str, client_id: str,+ username: str, user_attrs: dict,+ groups: list[str]) -> dict:+ """Invoke the user pool's PreTokenGeneration Lambda and apply its overrides.++ Honours both V1_0 (``LambdaConfig.PreTokenGeneration`` — id token only,+ legacy ``claimsOverrideDetails`` shape) and V2_0+ (``LambdaConfig.PreTokenGenerationConfig`` — id + access tokens,+ ``claimsAndScopeOverrideDetails`` shape).++ By default a Lambda failure is logged and ignored — the unmodified token+ is still issued. Set ``MINISTACK_COGNITO_PRETOKEN_STRICT=1`` to fail-closed+ (raise to the caller) the way real AWS does.+ """+ pool = _user_pools.get(pool_id)+ if not pool:+ return claims+ cfg = pool.get("LambdaConfig") or {}++ v2_cfg = cfg.get("PreTokenGenerationConfig") or {}+ v2_arn = v2_cfg.get("LambdaArn") or v2_cfg.get("LambdaArn".lower())+ v2_version = (v2_cfg.get("LambdaVersion") or "V2_0").upper()+ v1_arn = cfg.get("PreTokenGeneration")++ use_v2 = bool(v2_arn)+ arn = v2_arn or v1_arn+ if not arn:+ return claims+ # V1_0 only fires for id tokens.+ if not use_v2 and token_type != "id":+ return claims++ event = _build_pretoken_event(+ pool_id=pool_id,+ client_id=client_id,+ username=username,+ user_attrs=user_attrs,+ groups=groups,+ trigger_source=trigger_source,+ version=v2_version if use_v2 else "V1_0",+ )++ strict = os.environ.get("MINISTACK_COGNITO_PRETOKEN_STRICT", "").lower() in ("1", "true", "yes")+ try:+ # Lazy import to avoid a circular dependency between cognito and lambda_svc.+ from ministack.services import lambda_svc+ name, qualifier = lambda_svc._resolve_name_and_qualifier(arn)+ record, alias = lambda_svc._get_func_record_for_qualifier(name, qualifier)+ if record is None:+ raise RuntimeError(f"PreTokenGeneration Lambda not found: {arn}")+ result = lambda_svc._execute_function(record, event)+ except Exception as e:+ logger.warning("PreTokenGeneration Lambda invocation failed for pool %s: %s",+ pool_id, e)+ if strict:+ raise+ return claims++ if isinstance(result, dict) and result.get("error"):+ logger.warning("PreTokenGeneration Lambda returned an error for pool %s: %s",+ pool_id, result.get("body"))+ if strict:+ raise RuntimeError(f"PreTokenGeneration error: {result.get('body')}")+ return claims+ payload = result.get("body") if isinstance(result, dict) else result+ response = _extract_pretoken_response(payload)+ if not response:+ return claims++ if use_v2:+ section_name = "accessTokenGeneration" if token_type == "access" else "idTokenGeneration"+ scope_overrides = (response.get("claimsAndScopeOverrideDetails") or {}).get(section_name) or {}+ else:+ scope_overrides = response.get("claimsOverrideDetails") or {}++ add = scope_overrides.get("claimsToAddOrOverride") or {}+ if isinstance(add, dict):+ for k, v in add.items():+ claims[k] = v++ suppress = scope_overrides.get("claimsToSuppress") or []+ if isinstance(suppress, list):+ for k in suppress:+ claims.pop(k, None)++ if use_v2 and token_type == "access":+ scopes_add = scope_overrides.get("scopesToAdd") or []+ scopes_suppress = scope_overrides.get("scopesToSuppress") or []+ if scopes_add or scopes_suppress:+ current = (claims.get("scope") or "").split()+ current = [s for s in current if s not in scopes_suppress]+ for s in scopes_add:+ if s not in current:+ current.append(s)+ claims["scope"] = " ".join(current)++ group_override = scope_overrides.get("groupOverrideDetails") or {}+ groups_to_override = group_override.get("groupsToOverride")+ if isinstance(groups_to_override, list):+ claims["cognito:groups"] = groups_to_override++ return claims+++def _build_pretoken_event(pool_id: str, client_id: str, username: str,+ user_attrs: dict, groups: list[str],+ trigger_source: str, version: str) -> dict:+ """Construct the event payload AWS sends to a PreTokenGeneration Lambda.++ Shape from the Cognito Developer Guide+ (https://docs.aws.amazon.com/cognito/latest/developerguide/user-pool-lambda-pre-token-generation.html).+ """+ request_block: dict = {+ "userAttributes": {k: v for k, v in (user_attrs or {}).items()+ if isinstance(v, (str, int, float, bool))},+ "groupConfiguration": {+ "groupsToOverride": list(groups or []),+ "iamRolesToOverride": [],+ "preferredRole": None,+ },+ }+ if version.startswith("V2") or version.startswith("V3"):+ request_block["scopes"] = ["aws.cognito.signin.user.admin"]++ response_block = (+ {"claimsAndScopeOverrideDetails": None}+ if version.startswith("V2") or version.startswith("V3")+ else {"claimsOverrideDetails": None}+ )++ return {+ "version": "1",+ "triggerSource": trigger_source,+ "region": get_region(),+ "userPoolId": pool_id,+ "userName": username or "",+ "callerContext": {+ "awsSdkVersion": "ministack",+ "clientId": client_id or "",+ },+ "request": request_block,+ "response": response_block,+ }+++def _extract_pretoken_response(payload) -> dict | None:+ """Pull the ``response`` block from a Lambda invocation result.++ Lambdas return the full event echoed back with their overrides written+ into ``response.claimsAndScopeOverrideDetails`` (V2/V3) or+ ``response.claimsOverrideDetails`` (V1). Accept already-parsed dicts,+ JSON strings, and bytes.+ """+ if payload is None:+ return None+ if isinstance(payload, (bytes, bytearray)):+ try:+ payload = payload.decode("utf-8")+ except UnicodeDecodeError:+ return None+ if isinstance(payload, str):+ try:+ payload = json.loads(payload)+ except (TypeError, ValueError):+ return None+ if not isinstance(payload, dict):+ return None+ return payload.get("response") if isinstance(payload.get("response"), dict) else payload++def _user_from_token(token: str, pool: dict):"""Decode a stub JWT and return the matching user from pool, or None."""try:@@ -771,6 +961,7 @@ def _create_user_pool(data):}),"AccountRecoverySetting": data.get("AccountRecoverySetting", {}),"DeletionProtection": data.get("DeletionProtection", "INACTIVE"),+ "LambdaConfig": data.get("LambdaConfig", {}),"Domain": None,"_clients": {},"_users": {},@@ -838,7 +1029,7 @@ def _update_user_pool(data):"SmsAuthenticationMessage", "MfaConfiguration", "DeviceConfiguration","EmailConfiguration", "SmsConfiguration", "UserPoolTags","AdminCreateUserConfig", "UserPoolAddOns", "VerificationMessageTemplate",- "AccountRecoverySetting",+ "AccountRecoverySetting", "LambdaConfig",}for k in updatable:if k in data:@@ -1305,7 +1496,8 @@ def _build_auth_result(pool_id: str, client_id: str, user: dict) -> dict:username = user.get("Username", "")groups = user.get("_groups", [])return {- "AccessToken": _fake_token(sub, pool_id, client_id, "access", username=username, groups=groups),+ "AccessToken": _fake_token(sub, pool_id, client_id, "access", username=username,+ user_attrs=attrs, groups=groups),"IdToken": _fake_token(sub, pool_id, client_id, "id", username=username,user_attrs=attrs, groups=groups),"RefreshToken": _fake_token(sub, pool_id, client_id, "refresh"),diff --git a/ministack/services/imds.py b/ministack/services/imds.pynew file mode 100644index 00000000..acec1859--- /dev/null+++ b/ministack/services/imds.py@@ -0,0 +1,204 @@+"""+EC2 Instance Metadata Service (IMDS) emulator.++Implements the link-local 169.254.169.254 HTTP API that EC2 instances+(and ECS/EKS workloads) use to retrieve instance identity and temporary+security credentials. Routed by URL prefix (/latest/...).++Real EC2 binds 169.254.169.254 at the hypervisor; in ministack we expose+the same endpoints on the gateway port and let users redirect their SDK+via AWS_EC2_METADATA_SERVICE_ENDPOINT=http://localhost:4566.++Both IMDSv1 (token-less GET) and IMDSv2 (PUT /latest/api/token then GET+with X-aws-ec2-metadata-token) are supported. Set+MINISTACK_IMDS_V2_REQUIRED=1 to reject token-less requests, matching+real AWS hop-limit-1, IMDSv2-only instances.+"""++import json+import os+import secrets+import time++from ministack.core.responses import get_account_id, get_region, new_uuid+from ministack.services.iam import (+ _future,+ _gen_secret,+ _gen_session_access_key,+ _gen_session_token,+)++_DEFAULT_ROLE = "ministack-instance-role"+_INSTANCE_ID = "i-" + new_uuid().replace("-", "")[:17]++_tokens: dict[str, float] = {}+++def reset():+ _tokens.clear()+++def _text(body: str, status: int = 200):+ return status, {"Content-Type": "text/plain"}, body.encode()+++def _json_resp(payload, status: int = 200):+ return status, {"Content-Type": "application/json"}, json.dumps(payload).encode()+++def _unauthorized():+ return _text("Unauthorized", 401)+++def _not_found():+ return _text("Not Found", 404)+++def _v2_required() -> bool:+ return os.environ.get("MINISTACK_IMDS_V2_REQUIRED", "").lower() in ("1", "true", "yes")+++def _check_token(headers: dict) -> bool:+ token = headers.get("x-aws-ec2-metadata-token", "")+ if not token:+ return not _v2_required()+ expiry = _tokens.get(token)+ if expiry is None:+ return False+ if expiry < time.time():+ _tokens.pop(token, None)+ return False+ return True+++def _issue_token(headers: dict):+ ttl_raw = headers.get("x-aws-ec2-metadata-token-ttl-seconds", "21600")+ try:+ ttl = max(1, min(21600, int(ttl_raw)))+ except (TypeError, ValueError):+ return _text("Invalid TTL", 400)+ token = secrets.token_urlsafe(43)+ _tokens[token] = time.time() + ttl+ headers_out = {+ "Content-Type": "text/plain",+ "X-Aws-Ec2-Metadata-Token-Ttl-Seconds": str(ttl),+ }+ return 200, headers_out, token.encode()+++def _credentials_doc() -> dict:+ return {+ "Code": "Success",+ "LastUpdated": time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime()),+ "Type": "AWS-HMAC",+ "AccessKeyId": _gen_session_access_key(),+ "SecretAccessKey": _gen_secret(),+ "Token": _gen_session_token(),+ "Expiration": _future(3600),+ }+++def _identity_document() -> dict:+ return {+ "accountId": get_account_id(),+ "architecture": "x86_64",+ "availabilityZone": f"{get_region()}a",+ "imageId": "ami-ministack",+ "instanceId": _INSTANCE_ID,+ "instanceType": "t3.micro",+ "pendingTime": time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime()),+ "privateIp": "127.0.0.1",+ "region": get_region(),+ "version": "2017-09-30",+ }+++_META_FLAT: dict[str, str] = {+ "ami-id": "ami-ministack",+ "ami-launch-index": "0",+ "ami-manifest-path": "(unknown)",+ "hostname": "ministack.local",+ "instance-action": "none",+ "instance-life-cycle": "on-demand",+ "instance-type": "t3.micro",+ "local-hostname": "ministack.local",+ "local-ipv4": "127.0.0.1",+ "mac": "02:00:00:00:00:00",+ "profile": "default-hvm",+ "public-hostname": "ministack.local",+ "public-ipv4": "127.0.0.1",+ "reservation-id": "r-" + new_uuid().replace("-", "")[:17],+ "security-groups": "default",+}+++async def handle_request(method, path, headers, body, query_params):+ method = method.upper()+ path = path.rstrip("/") or path++ if path.startswith("/latest/api/token"):+ if method != "PUT":+ return _text("Method Not Allowed", 405)+ return _issue_token(headers)++ if not _check_token(headers):+ return _unauthorized()++ if method != "GET":+ return _text("Method Not Allowed", 405)++ if path == "/latest/meta-data" or path == "/latest/meta-data/":+ keys = sorted(set(list(_META_FLAT.keys()) + [+ "instance-id", "placement/", "iam/", "services/",+ ]))+ return _text("\n".join(keys))++ if path == "/latest/meta-data/instance-id":+ return _text(_INSTANCE_ID)++ if path.startswith("/latest/meta-data/placement/"):+ leaf = path[len("/latest/meta-data/placement/"):]+ region = get_region()+ placement = {+ "availability-zone": f"{region}a",+ "availability-zone-id": f"{region[:3]}1-az1",+ "region": region,+ }+ if leaf == "":+ return _text("\n".join(sorted(placement.keys())))+ if leaf in placement:+ return _text(placement[leaf])+ return _not_found()++ if path == "/latest/meta-data/iam/security-credentials" or \+ path == "/latest/meta-data/iam/security-credentials/":+ return _text(_DEFAULT_ROLE)++ if path.startswith("/latest/meta-data/iam/security-credentials/"):+ role = path[len("/latest/meta-data/iam/security-credentials/"):]+ if role != _DEFAULT_ROLE:+ return _not_found()+ return _json_resp(_credentials_doc())++ if path == "/latest/meta-data/iam/info":+ return _json_resp({+ "Code": "Success",+ "LastUpdated": time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime()),+ "InstanceProfileArn":+ f"arn:aws:iam::{get_account_id()}:instance-profile/{_DEFAULT_ROLE}",+ "InstanceProfileId": "AIPAMINISTACK0000000",+ })++ if path == "/latest/dynamic/instance-identity/document":+ return _json_resp(_identity_document())++ if path == "/latest/dynamic/instance-identity/pkcs7" or \+ path == "/latest/dynamic/instance-identity/signature":+ return _text("ministack-unsigned")++ if path.startswith("/latest/meta-data/"):+ leaf = path[len("/latest/meta-data/"):]+ if leaf in _META_FLAT:+ return _text(_META_FLAT[leaf])++ return _not_found()diff --git a/ministack/services/s3.py b/ministack/services/s3.pyindex 2cae13b2..f5f96241 100644--- a/ministack/services/s3.py+++ b/ministack/services/s3.py@@ -164,6 +164,27 @@ _PRESERVED_HEADERS = ("expires",)+# Per botocore/data/s3/2006-03-01/service-2.json (StorageClass enum).+_VALID_STORAGE_CLASSES = frozenset({+ "STANDARD", "REDUCED_REDUNDANCY", "STANDARD_IA", "ONEZONE_IA",+ "INTELLIGENT_TIERING", "GLACIER", "DEEP_ARCHIVE", "OUTPOSTS",+ "GLACIER_IR", "SNOW", "EXPRESS_ONEZONE", "FSX_OPENZFS",+})+++def _resolve_storage_class(headers: dict, default: str = "STANDARD"):+ """Read x-amz-storage-class. Returns (value, error_response_or_None)."""+ sc = headers.get("x-amz-storage-class", "")+ if not sc:+ return default, None+ if sc not in _VALID_STORAGE_CLASSES:+ return None, _error(+ "InvalidStorageClass",+ "The storage class you specified is not valid",+ 400,+ )+ return sc, None+# ---------------------------------------------------------------------------# Helpers# ---------------------------------------------------------------------------@@ -343,6 +364,7 @@ def _build_object_record(body: bytes, headers: dict, etag: str = None) -> dict:"size": len(body),"metadata": _extract_user_metadata(headers),"preserved_headers": preserved,+ "storage_class": headers.get("x-amz-storage-class") or "STANDARD",}@@ -361,6 +383,10 @@ def _object_response_headers(obj: dict, bucket_name: str = "", key: str = "") ->h.update(obj.get("metadata", {}))if obj.get("version_id"):h["x-amz-version-id"] = obj["version_id"]+ sc = obj.get("storage_class") or "STANDARD"+ if sc != "STANDARD":+ # AWS omits the header for STANDARD; SDKs default to STANDARD when absent.+ h["x-amz-storage-class"] = scif bucket_name and key:retention = _object_retention.get((bucket_name, key))if retention:@@ -569,6 +595,8 @@ def _dispatch(if method == "POST":if "delete" in query_params:return _delete_objects(bucket, body, headers)+ if headers.get("content-type", "").startswith("multipart/form-data"):+ return _post_object(bucket, body, headers)return _error("MethodNotAllowed","The specified method is not allowed against this resource.",@@ -1265,7 +1293,11 @@ def _list_object_versions(bucket_name: str, query_params: dict):SubElement(ver, "LastModified").text = v["last_modified"]SubElement(ver, "ETag").text = v["etag"]SubElement(ver, "Size").text = str(v["size"])- SubElement(ver, "StorageClass").text = "STANDARD"+ SubElement(ver, "StorageClass").text = (+ v.get("storage_class")+ or bucket["objects"].get(k, {}).get("storage_class")+ or "STANDARD"+ )owner = SubElement(ver, "Owner")SubElement(owner, "ID").text = "owner-id"SubElement(owner, "DisplayName").text = "ministack"@@ -1282,7 +1314,7 @@ def _list_object_versions(bucket_name: str, query_params: dict):SubElement(ver, "LastModified").text = obj["last_modified"]SubElement(ver, "ETag").text = obj["etag"]SubElement(ver, "Size").text = str(obj["size"])- SubElement(ver, "StorageClass").text = "STANDARD"+ SubElement(ver, "StorageClass").text = obj.get("storage_class") or "STANDARD"owner = SubElement(ver, "Owner")SubElement(owner, "ID").text = "owner-id"SubElement(owner, "DisplayName").text = "ministack"@@ -1629,6 +1661,10 @@ def _put_object(bucket_name: str, key: str, body: bytes, headers: dict):if md5_err:return md5_err+ _, sc_err = _resolve_storage_class(headers)+ if sc_err:+ return sc_err+etag = f'"{md5_hash(body)}"'obj = _build_object_record(body, headers, etag=etag)bucket["objects"][key] = obj@@ -1666,6 +1702,7 @@ def _put_object(bucket_name: str, key: str, body: bytes, headers: dict):"size": obj["size"],"is_latest": True,"data": body,+ "storage_class": obj.get("storage_class") or "STANDARD",})# Mark all previous versions as not latestfor v in _object_versions[vkey][:-1]:@@ -1673,6 +1710,221 @@ def _put_object(bucket_name: str, key: str, body: bytes, headers: dict):return 200, resp_headers, b""+def _parse_multipart_form(content_type: str, body: bytes) -> list[tuple]:+ """Return [(name, filename, headers, value_bytes), ...] in form order.++ Returns [] if the body is malformed or the boundary cannot be found.+ """+ import re as _re+ m = _re.search(r'boundary=("?)([^";\s]+)\1', content_type, _re.IGNORECASE)+ if not m:+ return []+ boundary = b"--" + m.group(2).encode()+ chunks = body.split(boundary)+ out = []+ # First chunk is preamble (often empty); last is "--\r\n" terminator.+ for chunk in chunks[1:-1]:+ if chunk.startswith(b"\r\n"):+ chunk = chunk[2:]+ if chunk.endswith(b"\r\n"):+ chunk = chunk[:-2]+ head_blob, sep, value = chunk.partition(b"\r\n\r\n")+ if not sep:+ continue+ part_headers: dict = {}+ for line in head_blob.split(b"\r\n"):+ if b":" in line:+ k, _, v = line.partition(b":")+ part_headers[k.strip().decode("latin-1").lower()] = v.strip().decode("latin-1")+ cd = part_headers.get("content-disposition", "")+ name_m = _re.search(r'name="([^"]*)"', cd)+ fn_m = _re.search(r'filename="([^"]*)"', cd)+ out.append((+ name_m.group(1) if name_m else "",+ fn_m.group(1) if fn_m else None,+ part_headers,+ value,+ ))+ return out+++def _enforce_post_policy_size(policy_b64: str, size: int):+ """Apply the `content-length-range` condition from a POST policy.++ Returns an error response when size is outside [min, max], else None.+ Other policy conditions (key, starts-with, signature) are not enforced —+ same lenient stance as ministack's presigned-URL handling.+ """+ if not policy_b64:+ return None+ try:+ decoded = base64.b64decode(policy_b64 + "==").decode("utf-8")+ policy = json.loads(decoded)+ except (ValueError, json.JSONDecodeError):+ return None+ for cond in policy.get("conditions", []):+ if (isinstance(cond, list) and len(cond) == 3+ and isinstance(cond[0], str)+ and cond[0].lower() == "content-length-range"):+ try:+ lo = int(cond[1])+ hi = int(cond[2])+ except (TypeError, ValueError):+ continue+ if size < lo:+ return _error(+ "EntityTooSmall",+ "Your proposed upload is smaller than the minimum allowed object size.",+ 400,+ )+ if size > hi:+ return _error(+ "EntityTooLarge",+ "Your proposed upload exceeds the maximum allowed size.",+ 400,+ )+ return None+++def _post_object(bucket_name: str, body: bytes, headers: dict):+ """Browser-based form upload (RFC 1867 / S3 PostObject).++ Per https://docs.aws.amazon.com/AmazonS3/latest/API/RTPM-mpuoverview.html,+ callers POST a multipart/form-data body to the bucket root. We honour+ `key` (with `${filename}` substitution), `Content-Type`, `x-amz-meta-*`,+ `x-amz-storage-class`, `x-amz-tagging`, `success_action_status`, and+ `success_action_redirect`. Policy and signature fields are accepted and+ ignored — same lenient stance as ministack's presigned-URL handling.+ """+ bucket = _ensure_bucket(bucket_name)+ if bucket is None:+ return _no_such_bucket(bucket_name)++ parts = _parse_multipart_form(headers.get("content-type", ""), body)+ if not parts:+ return _error("MalformedPOSTRequest",+ "The body of your POST request is not well-formed multipart/form-data.",+ 400)++ fields: dict[str, str] = {}+ file_value: bytes | None = None+ file_filename: str | None = None+ file_headers: dict = {}+ for name, filename, ph, value in parts:+ if name == "file" or filename is not None:+ file_value = value+ file_filename = filename or ""+ file_headers = ph+ else:+ try:+ fields[name.lower()] = value.decode("utf-8")+ except UnicodeDecodeError:+ fields[name.lower()] = value.decode("latin-1")++ if file_value is None:+ return _error("InvalidArgument",+ "POST requires a file field.", 400)++ err = _enforce_post_policy_size(fields.get("policy", ""), len(file_value))+ if err:+ return err++ key_template = fields.get("key", "")+ if not key_template:+ return _error("InvalidArgument",+ "Bucket POST must contain a field named 'key'.", 400)+ key = key_template.replace("${filename}", file_filename or "")++ # Build a synthetic header dict so _build_object_record reuses the PUT path.+ synth = {}+ if "content-type" in fields:+ synth["content-type"] = fields["content-type"]+ elif file_headers.get("content-type"):+ synth["content-type"] = file_headers["content-type"]+ for fname, fval in fields.items():+ if fname.startswith("x-amz-meta-"):+ synth[fname] = fval+ for h in ("cache-control", "content-disposition", "content-encoding",+ "content-language", "expires", "x-amz-storage-class",+ "x-amz-tagging", "x-amz-acl",+ "x-amz-object-lock-mode", "x-amz-object-lock-retain-until-date",+ "x-amz-object-lock-legal-hold"):+ if h in fields:+ synth[h] = fields[h]++ _, sc_err = _resolve_storage_class(synth)+ if sc_err:+ return sc_err++ etag = f'"{md5_hash(file_value)}"'+ obj = _build_object_record(file_value, synth, etag=etag)+ bucket["objects"][key] = obj+ _apply_object_lock_from_headers(bucket_name, key, synth)++ tagging_header = synth.get("x-amz-tagging", "")+ if tagging_header:+ tags = {k: v[0] for k, v in _parse_qs(tagging_header).items()}+ if len(tags) <= 10:+ _object_tags[(bucket_name, key)] = tags++ if S3_PERSIST:+ _persist_object(bucket_name, key, obj)++ _fire_s3_event_async(+ bucket_name, key, "s3:ObjectCreated:Post", size=obj["size"], etag=etag+ )++ version_id = None+ if _bucket_versioning.get(bucket_name) in ("Enabled", "Suspended"):+ version_id = new_uuid()+ obj["version_id"] = version_id+ vkey = (bucket_name, key)+ if vkey not in _object_versions:+ _object_versions[vkey] = []+ _object_versions[vkey].append({+ "version_id": version_id,+ "last_modified": obj["last_modified"],+ "etag": etag,+ "size": obj["size"],+ "is_latest": True,+ "data": file_value,+ "storage_class": obj.get("storage_class") or "STANDARD",+ })+ for v in _object_versions[vkey][:-1]:+ v["is_latest"] = False++ location = f"http://{bucket_name}.s3.amazonaws.com/{url_quote(key, safe='/')}"+ base_resp = {"ETag": etag, "Location": location}+ if version_id:+ base_resp["x-amz-version-id"] = version_id++ redirect = fields.get("success_action_redirect") or fields.get("redirect")+ if redirect:+ sep = "&" if "?" in redirect else "?"+ target = (f"{redirect}{sep}bucket={bucket_name}"+ f"&key={url_quote(key, safe='/')}"+ f"&etag={url_quote(etag, safe='')}")+ return 303, {**base_resp, "Location": target}, b""++ status_str = fields.get("success_action_status", "204")+ try:+ status = int(status_str)+ except (TypeError, ValueError):+ status = 204+ if status not in (200, 201, 204):+ status = 204++ if status == 201:+ root = Element("PostResponse")+ SubElement(root, "Location").text = location+ SubElement(root, "Bucket").text = bucket_name+ SubElement(root, "Key").text = key+ SubElement(root, "ETag").text = etag+ return 201, {**base_resp, "Content-Type": "application/xml"}, _xml_body(root)++ return status, base_resp, b""++def _apply_object_lock_from_headers(bucket_name: str, key: str, headers: dict):lock_mode = headers.get("x-amz-object-lock-mode", "")lock_until = headers.get("x-amz-object-lock-retain-until-date", "")@@ -1928,6 +2180,12 @@ def _copy_object(bucket_name: str, dest_key: str, headers: dict):content_encoding = src_obj.get("content_encoding")preserved = dict(src_obj.get("preserved_headers", {}))+ dest_sc, sc_err = _resolve_storage_class(+ headers, default=src_obj.get("storage_class") or "STANDARD"+ )+ if sc_err:+ return sc_err+new_etag = src_obj["etag"]last_modified = now_iso()src_body = _read_body(src_bucket_name, src_key, src_obj)@@ -1940,6 +2198,7 @@ def _copy_object(bucket_name: str, dest_key: str, headers: dict):"size": src_obj["size"],"metadata": metadata,"preserved_headers": preserved,+ "storage_class": dest_sc,}dest_bucket["objects"][dest_key] = dest_obj@@ -1985,6 +2244,8 @@ def _copy_object(bucket_name: str, dest_key: str, headers: dict):)resp_headers = {"Content-Type": "application/xml"}+ if dest_sc != "STANDARD":+ resp_headers["x-amz-storage-class"] = dest_scif _bucket_versioning.get(bucket_name) in ("Enabled", "Suspended"):version_id = new_uuid()dest_obj["version_id"] = version_id@@ -1999,6 +2260,7 @@ def _copy_object(bucket_name: str, dest_key: str, headers: dict):"size": dest_obj["size"],"is_latest": True,"data": src_body,+ "storage_class": dest_obj.get("storage_class") or "STANDARD",})for v in _object_versions[vkey][:-1]:v["is_latest"] = False@@ -2549,7 +2811,7 @@ def _list_objects_v1(bucket_name: str, query_params: dict):SubElement(c, "LastModified").text = obj["last_modified"]SubElement(c, "ETag").text = obj["etag"]SubElement(c, "Size").text = str(obj["size"])- SubElement(c, "StorageClass").text = "STANDARD"+ SubElement(c, "StorageClass").text = obj.get("storage_class") or "STANDARD"owner = SubElement(c, "Owner")SubElement(owner, "ID").text = "owner-id"SubElement(owner, "DisplayName").text = "ministack"@@ -2624,7 +2886,7 @@ def _list_objects_v2(bucket_name: str, query_params: dict):SubElement(c, "LastModified").text = obj["last_modified"]SubElement(c, "ETag").text = obj["etag"]SubElement(c, "Size").text = str(obj["size"])- SubElement(c, "StorageClass").text = "STANDARD"+ SubElement(c, "StorageClass").text = obj.get("storage_class") or "STANDARD"if fetch_owner:owner = SubElement(c, "Owner")SubElement(owner, "ID").text = "owner-id"@@ -2707,6 +2969,10 @@ def _create_multipart_upload(bucket_name: str, key: str, headers: dict):if bucket is None:return _no_such_bucket(bucket_name)+ storage_class, sc_err = _resolve_storage_class(headers)+ if sc_err:+ return sc_err+upload_id = new_uuid()content_type = headers.get("content-type", "application/octet-stream")content_encoding = headers.get("content-encoding")@@ -2725,6 +2991,7 @@ def _create_multipart_upload(bucket_name: str, key: str, headers: dict):"content_type": content_type,"content_encoding": content_encoding,"preserved_headers": preserved,+ "storage_class": storage_class,"created": now_iso(),}@@ -2911,6 +3178,7 @@ def _complete_multipart_upload("size": len(combined),"metadata": upload["metadata"],"preserved_headers": upload.get("preserved_headers", {}),+ "storage_class": upload.get("storage_class") or "STANDARD",}bucket["objects"][key] = obj@@ -3035,7 +3303,7 @@ def _list_multipart_uploads(bucket_name: str, query_params: dict):owner = SubElement(u, "Owner")SubElement(owner, "ID").text = "owner-id"SubElement(owner, "DisplayName").text = "ministack"- SubElement(u, "StorageClass").text = "STANDARD"+ SubElement(u, "StorageClass").text = upload.get("storage_class") or "STANDARD"SubElement(u, "Initiated").text = upload["created"]if is_truncated and uploads:@@ -3083,7 +3351,7 @@ def _list_parts(bucket_name: str, key: str, query_params: dict):owner = SubElement(root, "Owner")SubElement(owner, "ID").text = "owner-id"SubElement(owner, "DisplayName").text = "ministack"- SubElement(root, "StorageClass").text = "STANDARD"+ SubElement(root, "StorageClass").text = upload.get("storage_class") or "STANDARD"SubElement(root, "PartNumberMarker").text = str(part_marker)SubElement(root, "MaxParts").text = str(max_parts)@@ -3165,6 +3433,7 @@ def _persist_object(bucket: str, key: str, obj):"size": obj.get("size", 0),"metadata": obj.get("metadata", {}),"preserved_headers": obj.get("preserved_headers", {}),+ "storage_class": obj.get("storage_class", "STANDARD"),}_atomic_write(fpath + ".meta.json", json.dumps(meta), text=True)# Drop body from in-memory record to save RAM@@ -3289,6 +3558,7 @@ def _load_persisted_bucket(account_id, bucket_name, bucket_path):"size": size,"metadata": meta.get("metadata", {}),"preserved_headers": meta.get("preserved_headers", {}),+ "storage_class": meta.get("storage_class", "STANDARD"),}diff --git a/pyproject.toml b/pyproject.tomlindex 34028ea3..3a9403dc 100644--- a/pyproject.toml+++ b/pyproject.toml@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"[project]name = "ministack"-version = "1.3.21"+version = "1.3.22"description = "Free, open-source local AWS emulator — drop-in LocalStack replacement"readme = "README.md"license = { file = "LICENSE" }diff --git a/tests/test_cognito.py b/tests/test_cognito.pyindex 5fa7021e..037e019e 100644--- a/tests/test_cognito.py+++ b/tests/test_cognito.py@@ -2211,6 +2211,185 @@ def _cognito_module():return importlib.import_module("ministack.services.cognito")+def _decode_jwt_claims(jwt: str) -> dict:+ p = jwt.split(".")[1]+ p += "=" * (-len(p) % 4)+ return json.loads(base64.urlsafe_b64decode(p))+++def _make_pretoken_lambda_zip(handler_body: str) -> bytes:+ buf = io.BytesIO()+ with zipfile.ZipFile(buf, "w") as zf:+ zf.writestr("index.py", handler_body)+ return buf.getvalue()+++def test_cognito_user_pool_lambda_config_round_trip(cognito_idp):+ """LambdaConfig.PreTokenGenerationConfig persists on Create/Update (#533)."""+ pid = cognito_idp.create_user_pool(+ PoolName="lc-rt",+ LambdaConfig={+ "PreTokenGenerationConfig": {+ "LambdaArn": "arn:aws:lambda:us-east-1:000000000000:function:none",+ "LambdaVersion": "V2_0",+ }+ },+ )["UserPool"]["Id"]+ desc = cognito_idp.describe_user_pool(UserPoolId=pid)["UserPool"]+ cfg = desc["LambdaConfig"]["PreTokenGenerationConfig"]+ assert cfg["LambdaArn"].endswith(":function:none")+ assert cfg["LambdaVersion"] == "V2_0"++ cognito_idp.update_user_pool(+ UserPoolId=pid,+ LambdaConfig={"PreTokenGenerationConfig": {+ "LambdaArn": "arn:aws:lambda:us-east-1:000000000000:function:other",+ "LambdaVersion": "V2_0",+ }},+ )+ desc2 = cognito_idp.describe_user_pool(UserPoolId=pid)["UserPool"]+ assert desc2["LambdaConfig"]["PreTokenGenerationConfig"]["LambdaArn"].endswith(":function:other")+++def test_cognito_pretoken_v2_adds_custom_claim_to_access_token(cognito_idp, lam):+ """V2_0 PreTokenGeneration trigger injects custom claims into the access token (#533)."""+ handler = (+ "import json\n"+ "def handler(event, ctx):\n"+ " attrs = event['request']['userAttributes']\n"+ " event['response']['claimsAndScopeOverrideDetails'] = {\n"+ " 'accessTokenGeneration': {\n"+ " 'claimsToAddOrOverride': {\n"+ " 'userIDs': attrs.get('custom:userIDs', ''),\n"+ " 'tier': 'platinum',\n"+ " },\n"+ " 'claimsToSuppress': ['origin_jti'],\n"+ " }\n"+ " }\n"+ " return event\n"+ )+ fn_name = "ministack-pretoken-v2"+ lam.create_function(+ FunctionName=fn_name, Runtime="python3.12",+ Role="arn:aws:iam::000000000000:role/test-role",+ Handler="index.handler",+ Code={"ZipFile": _make_pretoken_lambda_zip(handler)},+ )+ fn_arn = lam.get_function(FunctionName=fn_name)["Configuration"]["FunctionArn"]++ pid = cognito_idp.create_user_pool(+ PoolName="pretoken-v2",+ LambdaConfig={"PreTokenGenerationConfig": {+ "LambdaArn": fn_arn, "LambdaVersion": "V2_0",+ }},+ )["UserPool"]["Id"]+ cid = cognito_idp.create_user_pool_client(+ UserPoolId=pid, ClientName="app",+ ExplicitAuthFlows=["ALLOW_USER_PASSWORD_AUTH"],+ )["UserPoolClient"]["ClientId"]+ cognito_idp.admin_create_user(+ UserPoolId=pid, Username="u",+ UserAttributes=[{"Name": "custom:userIDs", "Value": "abc,def"}],+ TemporaryPassword="Temp1234!", MessageAction="SUPPRESS",+ )+ cognito_idp.admin_set_user_password(+ UserPoolId=pid, Username="u", Password="Pwd1234!", Permanent=True,+ )+ tok = cognito_idp.initiate_auth(+ ClientId=cid, AuthFlow="USER_PASSWORD_AUTH",+ AuthParameters={"USERNAME": "u", "PASSWORD": "Pwd1234!"},+ )["AuthenticationResult"]+ access = _decode_jwt_claims(tok["AccessToken"])+ assert access.get("userIDs") == "abc,def"+ assert access.get("tier") == "platinum"+ assert "origin_jti" not in access+++def test_cognito_pretoken_v2_id_token_section(cognito_idp, lam):+ """V2_0 idTokenGeneration / accessTokenGeneration sections target their own token (#533)."""+ handler = (+ "def handler(event, ctx):\n"+ " event['response']['claimsAndScopeOverrideDetails'] = {\n"+ " 'idTokenGeneration': {'claimsToAddOrOverride': {'id_only_marker': 'yes'}},\n"+ " 'accessTokenGeneration': {'claimsToAddOrOverride': {'access_only_marker': 'yes'}},\n"+ " }\n"+ " return event\n"+ )+ fn_name = "ministack-pretoken-v2-split"+ lam.create_function(+ FunctionName=fn_name, Runtime="python3.12",+ Role="arn:aws:iam::000000000000:role/test-role",+ Handler="index.handler",+ Code={"ZipFile": _make_pretoken_lambda_zip(handler)},+ )+ fn_arn = lam.get_function(FunctionName=fn_name)["Configuration"]["FunctionArn"]++ pid = cognito_idp.create_user_pool(+ PoolName="pretoken-split",+ LambdaConfig={"PreTokenGenerationConfig": {+ "LambdaArn": fn_arn, "LambdaVersion": "V2_0",+ }},+ )["UserPool"]["Id"]+ cid = cognito_idp.create_user_pool_client(+ UserPoolId=pid, ClientName="app",+ ExplicitAuthFlows=["ALLOW_USER_PASSWORD_AUTH"],+ )["UserPoolClient"]["ClientId"]+ cognito_idp.admin_create_user(+ UserPoolId=pid, Username="u",+ TemporaryPassword="Temp1234!", MessageAction="SUPPRESS",+ )+ cognito_idp.admin_set_user_password(+ UserPoolId=pid, Username="u", Password="Pwd1234!", Permanent=True,+ )+ tok = cognito_idp.initiate_auth(+ ClientId=cid, AuthFlow="USER_PASSWORD_AUTH",+ AuthParameters={"USERNAME": "u", "PASSWORD": "Pwd1234!"},+ )["AuthenticationResult"]+ access = _decode_jwt_claims(tok["AccessToken"])+ id_tok = _decode_jwt_claims(tok["IdToken"])+ assert access.get("access_only_marker") == "yes"+ assert "id_only_marker" not in access+ assert id_tok.get("id_only_marker") == "yes"+ assert "access_only_marker" not in id_tok+++def test_cognito_pretoken_lambda_failure_fail_open(cognito_idp, lam):+ """A broken PreTokenGeneration Lambda fails open: token issued without overrides (#533)."""+ handler = "def handler(event, ctx):\n raise RuntimeError('boom')\n"+ fn_name = "ministack-pretoken-broken"+ lam.create_function(+ FunctionName=fn_name, Runtime="python3.12",+ Role="arn:aws:iam::000000000000:role/test-role",+ Handler="index.handler",+ Code={"ZipFile": _make_pretoken_lambda_zip(handler)},+ )+ fn_arn = lam.get_function(FunctionName=fn_name)["Configuration"]["FunctionArn"]++ pid = cognito_idp.create_user_pool(+ PoolName="pretoken-broken",+ LambdaConfig={"PreTokenGenerationConfig": {+ "LambdaArn": fn_arn, "LambdaVersion": "V2_0",+ }},+ )["UserPool"]["Id"]+ cid = cognito_idp.create_user_pool_client(+ UserPoolId=pid, ClientName="app",+ ExplicitAuthFlows=["ALLOW_USER_PASSWORD_AUTH"],+ )["UserPoolClient"]["ClientId"]+ cognito_idp.admin_create_user(+ UserPoolId=pid, Username="u",+ TemporaryPassword="Temp1234!", MessageAction="SUPPRESS",+ )+ cognito_idp.admin_set_user_password(+ UserPoolId=pid, Username="u", Password="Pwd1234!", Permanent=True,+ )+ tok = cognito_idp.initiate_auth(+ ClientId=cid, AuthFlow="USER_PASSWORD_AUTH",+ AuthParameters={"USERNAME": "u", "PASSWORD": "Pwd1234!"},+ )["AuthenticationResult"]+ access = _decode_jwt_claims(tok["AccessToken"])+ assert access["client_id"] == cid # token still issued++@pytest.fixturedef _enable_persistence(monkeypatch, tmp_path):"""Force PERSIST_STATE on and point STATE_DIR at a tmp dir sodiff --git a/tests/test_imds.py b/tests/test_imds.pynew file mode 100644index 00000000..6b1de5c5--- /dev/null+++ b/tests/test_imds.py@@ -0,0 +1,70 @@+import json+import os++import pytest+import requests++ENDPOINT = os.environ.get("MINISTACK_ENDPOINT", "http://localhost:4566")+++def test_imds_v1_role_listing():+ r = requests.get(f"{ENDPOINT}/latest/meta-data/iam/security-credentials/")+ assert r.status_code == 200+ assert r.text.strip() == "ministack-instance-role"+++def test_imds_v1_credentials_doc_shape():+ r = requests.get(f"{ENDPOINT}/latest/meta-data/iam/security-credentials/ministack-instance-role")+ assert r.status_code == 200+ doc = r.json()+ assert doc["Code"] == "Success"+ assert doc["Type"] == "AWS-HMAC"+ for k in ("AccessKeyId", "SecretAccessKey", "Token", "Expiration", "LastUpdated"):+ assert k in doc and doc[k]+++def test_imds_v2_token_then_get():+ t = requests.put(+ f"{ENDPOINT}/latest/api/token",+ headers={"X-aws-ec2-metadata-token-ttl-seconds": "60"},+ )+ assert t.status_code == 200+ token = t.text.strip()+ assert token++ r = requests.get(+ f"{ENDPOINT}/latest/meta-data/instance-id",+ headers={"X-aws-ec2-metadata-token": token},+ )+ assert r.status_code == 200+ assert r.text.startswith("i-")+++def test_imds_identity_document():+ r = requests.get(f"{ENDPOINT}/latest/dynamic/instance-identity/document")+ assert r.status_code == 200+ doc = r.json()+ assert doc["accountId"] == "000000000000"+ assert doc["region"]+ assert doc["instanceId"].startswith("i-")+++def test_imds_unknown_leaf_404():+ r = requests.get(f"{ENDPOINT}/latest/meta-data/does-not-exist")+ assert r.status_code == 404+++def test_imds_token_endpoint_rejects_get():+ r = requests.get(f"{ENDPOINT}/latest/api/token")+ assert r.status_code == 405+++def test_imds_v2_required_blocks_tokenless():+ """When MINISTACK_IMDS_V2_REQUIRED=1 the server rejects token-less GETs."""+ pytest.skip("requires server restart with MINISTACK_IMDS_V2_REQUIRED=1; covered manually")+++def test_imds_placement_region():+ r = requests.get(f"{ENDPOINT}/latest/meta-data/placement/region")+ assert r.status_code == 200+ assert r.text.strip()diff --git a/tests/test_init_scripts.py b/tests/test_init_scripts.pyindex 96b6e908..5d21d4a2 100644--- a/tests/test_init_scripts.py+++ b/tests/test_init_scripts.py@@ -141,3 +141,27 @@ def test_init_scripts_uses_correct_interpreter(tmp_path, monkeypatch):assert calls[0][1] == str(sh_script)assert calls[1][0] == sys.executableassert calls[1][1] == str(py_script)+++def test_init_scripts_expose_script_dir_env(tmp_path, monkeypatch):+ """Each init script gets MINISTACK_INIT_SCRIPT_DIR/PATH pointing at its own directory."""+ script = tmp_path / "01-seed.sh"+ script.write_text("#!/bin/sh\necho seed")++ monkeypatch.setattr('ministack.app._collect_scripts', lambda *a: [str(script)])++ captured = {}++ def fake_run(cmd, **kwargs):+ captured.update(kwargs.get("env", {}))+ class Result:+ returncode = 0+ stdout = ""+ stderr = ""+ return Result()++ monkeypatch.setattr('subprocess.run', fake_run)+ _run_init_scripts()++ assert captured["MINISTACK_INIT_SCRIPT_DIR"] == str(tmp_path)+ assert captured["MINISTACK_INIT_SCRIPT_PATH"] == str(script)diff --git a/tests/test_s3.py b/tests/test_s3.pyindex feccd182..4de70706 100644--- a/tests/test_s3.py+++ b/tests/test_s3.py@@ -1,4 +1,5 @@import json+import osimport timefrom urllib.parse import urlparse@@ -1322,6 +1323,204 @@ def test_s3_put_object_content_type_preserved(s3):resp = s3.get_object(Bucket="qa-s3-ct", Key="page.html")assert "text/html" in resp["ContentType"]+def test_s3_put_object_storage_class_roundtrip(s3):+ """PutObject with StorageClass is returned by GetObject and HeadObject (#534)."""+ s3.create_bucket(Bucket="qa-s3-sc")+ s3.put_object(+ Bucket="qa-s3-sc",+ Key="cold.bin",+ Body=b"x",+ StorageClass="INTELLIGENT_TIERING",+ )+ g = s3.get_object(Bucket="qa-s3-sc", Key="cold.bin")+ assert g["StorageClass"] == "INTELLIGENT_TIERING"+ h = s3.head_object(Bucket="qa-s3-sc", Key="cold.bin")+ assert h["StorageClass"] == "INTELLIGENT_TIERING"+++def test_s3_put_object_default_storage_class_is_standard(s3):+ """When PutObject does not set StorageClass, GetObject omits the field+ (botocore reports STANDARD via absence — no header on the wire)."""+ s3.create_bucket(Bucket="qa-s3-sc-default")+ s3.put_object(Bucket="qa-s3-sc-default", Key="f", Body=b"x")+ g = s3.get_object(Bucket="qa-s3-sc-default", Key="f")+ # AWS does not send the header for STANDARD; boto3 surfaces it as missing.+ assert g.get("StorageClass") in (None, "STANDARD")+++def test_s3_invalid_storage_class_rejected(s3):+ """Unknown StorageClass values return InvalidStorageClass (#534)."""+ from botocore.exceptions import ClientError+ s3.create_bucket(Bucket="qa-s3-sc-bad")+ with pytest.raises(ClientError) as ei:+ s3.put_object(+ Bucket="qa-s3-sc-bad",+ Key="f",+ Body=b"x",+ StorageClass="NOT_A_CLASS",+ )+ assert ei.value.response["Error"]["Code"] == "InvalidStorageClass"+++def test_s3_list_objects_reports_storage_class(s3):+ """ListObjectsV2 returns the per-object storage class, not a hardcoded STANDARD (#534)."""+ s3.create_bucket(Bucket="qa-s3-sc-list")+ s3.put_object(Bucket="qa-s3-sc-list", Key="hot", Body=b"x")+ s3.put_object(+ Bucket="qa-s3-sc-list", Key="cold", Body=b"x",+ StorageClass="GLACIER",+ )+ listing = {o["Key"]: o["StorageClass"]+ for o in s3.list_objects_v2(Bucket="qa-s3-sc-list")["Contents"]}+ assert listing["hot"] == "STANDARD"+ assert listing["cold"] == "GLACIER"+++def test_s3_post_object_presigned(s3):+ """Browser POST upload via generate_presigned_post round-trips (#535)."""+ import requests+ s3.create_bucket(Bucket="qa-s3-post")+ post = s3.generate_presigned_post(Bucket="qa-s3-post", Key="hello.txt")+ r = requests.post(+ post["url"], data=post["fields"],+ files={"file": ("hello.txt", b"hello world")},+ )+ assert r.status_code == 204+ assert r.headers["ETag"]+ assert "qa-s3-post" in r.headers["Location"] and "hello.txt" in r.headers["Location"]+ assert s3.get_object(Bucket="qa-s3-post", Key="hello.txt")["Body"].read() == b"hello world"+++def test_s3_post_object_filename_substitution(s3):+ """`${filename}` in the key is replaced with the uploaded file's filename (#535)."""+ import requests+ s3.create_bucket(Bucket="qa-s3-post-fn")+ post = s3.generate_presigned_post(+ Bucket="qa-s3-post-fn", Key="uploads/${filename}",+ )+ r = requests.post(+ post["url"], data=post["fields"],+ files={"file": ("photo.png", b"PNG-bytes")},+ )+ assert r.status_code == 204+ assert s3.get_object(Bucket="qa-s3-post-fn", Key="uploads/photo.png")["Body"].read() == b"PNG-bytes"+++def test_s3_post_object_success_action_status_201(s3):+ """success_action_status=201 returns XML PostResponse (#535)."""+ import requests+ s3.create_bucket(Bucket="qa-s3-post-201")+ post = s3.generate_presigned_post(+ Bucket="qa-s3-post-201", Key="k",+ Fields={"success_action_status": "201"},+ Conditions=[{"success_action_status": "201"}],+ )+ r = requests.post(post["url"], data=post["fields"], files={"file": ("x", b"x")})+ assert r.status_code == 201+ assert "<PostResponse>" in r.text and "<Bucket>qa-s3-post-201</Bucket>" in r.text+ assert "<Key>k</Key>" in r.text+++def test_s3_post_object_content_type_passthrough(s3):+ """A `Content-Type` form field is stored on the object (#535)."""+ import requests+ s3.create_bucket(Bucket="qa-s3-post-ct")+ post = s3.generate_presigned_post(+ Bucket="qa-s3-post-ct", Key="page.html",+ Fields={"Content-Type": "text/html; charset=utf-8"},+ Conditions=[["starts-with", "$Content-Type", "text/"]],+ )+ r = requests.post(post["url"], data=post["fields"], files={"file": ("p", b"<html/>")})+ assert r.status_code == 204+ assert "text/html" in s3.get_object(Bucket="qa-s3-post-ct", Key="page.html")["ContentType"]+++def test_s3_post_object_storage_class(s3):+ """`x-amz-storage-class` form field is honored (#534 + #535)."""+ import requests+ s3.create_bucket(Bucket="qa-s3-post-sc")+ post = s3.generate_presigned_post(+ Bucket="qa-s3-post-sc", Key="cold",+ Fields={"x-amz-storage-class": "GLACIER"},+ Conditions=[{"x-amz-storage-class": "GLACIER"}],+ )+ r = requests.post(post["url"], data=post["fields"], files={"file": ("x", b"x")})+ assert r.status_code == 204+ assert s3.get_object(Bucket="qa-s3-post-sc", Key="cold")["StorageClass"] == "GLACIER"+++def test_s3_post_object_content_length_range_enforced(s3):+ """`content-length-range` condition rejects oversize uploads with EntityTooLarge."""+ import requests+ s3.create_bucket(Bucket="qa-s3-post-clr")+ post = s3.generate_presigned_post(+ Bucket="qa-s3-post-clr", Key="k",+ Conditions=[["content-length-range", 0, 5]],+ )+ # Within the limit -> 204+ ok = requests.post(post["url"], data=post["fields"], files={"file": ("f", b"abcde")})+ assert ok.status_code == 204++ # Over the limit -> 400 EntityTooLarge+ too_big = requests.post(post["url"], data=post["fields"], files={"file": ("f", b"abcdef")})+ assert too_big.status_code == 400+ assert "EntityTooLarge" in too_big.text+++def test_s3_post_object_content_length_range_minimum(s3):+ """A minimum bound on `content-length-range` rejects undersize uploads."""+ import requests+ s3.create_bucket(Bucket="qa-s3-post-clr-min")+ post = s3.generate_presigned_post(+ Bucket="qa-s3-post-clr-min", Key="k",+ Conditions=[["content-length-range", 5, 1024]],+ )+ too_small = requests.post(post["url"], data=post["fields"], files={"file": ("f", b"abc")})+ assert too_small.status_code == 400+ assert "EntityTooSmall" in too_small.text+++def test_s3_storage_class_persisted_to_disk(tmp_path, monkeypatch):+ """storage_class survives _persist_object → _load_persisted_bucket round-trip (#534)."""+ from ministack.services import s3 as s3mod+ monkeypatch.setattr(s3mod, "DATA_DIR", str(tmp_path))+ monkeypatch.setattr(s3mod, "S3_PERSIST", True)++ obj = {+ "body": b"hello",+ "content_type": "application/octet-stream",+ "content_encoding": None,+ "etag": '"abc"',+ "last_modified": s3mod.now_iso(),+ "size": 5,+ "metadata": {},+ "preserved_headers": {},+ "storage_class": "GLACIER",+ }++ monkeypatch.setattr(s3mod, "get_account_id", lambda: "000000000000")+ s3mod._persist_object("qa-bucket", "k", obj)++ s3mod._buckets._data.pop(("000000000000", "qa-bucket"), None)+ s3mod._load_persisted_bucket("000000000000", "qa-bucket",+ os.path.join(str(tmp_path), "000000000000", "qa-bucket"))+ restored = s3mod._buckets._data[("000000000000", "qa-bucket")]["objects"]["k"]+ assert restored["storage_class"] == "GLACIER"+++def test_s3_copy_object_propagates_storage_class(s3):+ """CopyObject with explicit StorageClass overrides the source's class (#534)."""+ s3.create_bucket(Bucket="qa-s3-sc-copy")+ s3.put_object(Bucket="qa-s3-sc-copy", Key="src", Body=b"x")+ s3.copy_object(+ Bucket="qa-s3-sc-copy",+ Key="dst",+ CopySource={"Bucket": "qa-s3-sc-copy", "Key": "src"},+ StorageClass="STANDARD_IA",+ )+ assert s3.get_object(Bucket="qa-s3-sc-copy", Key="dst")["StorageClass"] == "STANDARD_IA"++def test_s3_head_object_returns_content_length(s3):"""HeadObject must return correct ContentLength."""s3.create_bucket(Bucket="qa-s3-head-len")