Skip to content

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

fix(alb): fix access token, clean up test files (#1602)

Nahuel Nucerabf05c52a58fe
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 69a61f62..dc57d27d 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -8,6 +8,7 @@ Versioning follows [Semantic Versioning](https://semver.org/).
## [Unreleased]
### Added
+- **ALB — the `authenticate-oidc` listener action authenticates** — a rule accepted the action's `Type` and silently discarded its `AuthenticateOidcConfig`, so an OIDC-protected service tested against MiniStack passed while every request reached the target unauthenticated. The flow now runs end to end as AWS documents it: an unauthenticated request is redirected to the `AuthorizationEndpoint` with `state`, the callback at `/oauth2/idpresponse` exchanges the code at the `TokenEndpoint` and fetches the claims from the `UserInfoEndpoint`, and the session is written to the client as `AWSELBAuthSessionCookie` shards sized so the whole Set-Cookie pair fits the 4096-byte browser ceiling. Later requests reassemble and validate the shards and reach the target with `x-amzn-oidc-identity`, `x-amzn-oidc-accesstoken` and `x-amzn-oidc-data` — the last as the user claims in AWS's signed-JWT shape (`kid`, `signer`, `iss`, `client`, `exp` in the header; the provider's ID token is never passed to the backend), with any client-supplied copies of those headers stripped so an identity cannot be forged even under `OnUnauthenticatedRequest=allow`. `SessionCookieName`, `SessionTimeout`, `Scope` and all three `OnUnauthenticatedRequest` modes are honoured; an unmatched or expired callback answers `401` per the 15-minute login bound; `ClientSecret` is held for the exchange and never echoed from `Describe*`. Two data-plane fixes came with it: a rule's actions now run as a chain in `Order` (dispatch executed only `Actions[0]`, so authenticate-then-forward could not work), and `ModifyListener` now updates the auto-created default rule the data plane actually serves. `authenticate-cognito` answers `501` rather than forwarding a request nobody authenticated. Contributed by @dhanesh.
- **IAM — the AWS-managed policies a CDK, SAM or Serverless deployment attaches resolve by their real ARNs** — the seeded catalogue filed the Lambda execution-role policies under `arn:aws:iam::aws:policy/<Name>` while AWS publishes them under `…:policy/service-role/<Name>`, the only ARN those tools emit, so `GetPolicy` on the real ARN answered `NoSuchEntity` and under `AUTH=true` a role that attached one carried no permissions from it; `AWSCloudFormationReadOnlyAccess` (the CDK deploy role's read grant), `CloudWatchLambdaInsightsExecutionRolePolicy`, `service-role/AmazonAPIGatewayPushToCloudWatchLogs` and `service-role/AWSIoTThingsRegistration` were missing altogether, which denied `cdk deploy` its first `DescribeStacks` under enforcement. The catalogue now carries them under their real path with the documents from the AWS Managed Policy Reference, reports `PolicyName` and `Path` as AWS does (so `ListPolicies` with `PathPrefix=/service-role/` finds them), adds the SQS, Kinesis and DynamoDB Lambda execution-role policies, and keeps the four path-less ARNs earlier versions seeded as aliases of the real record. Contributed by @iot-rocket.
@@ -23,6 +24,9 @@ Versioning follows [Semantic Versioning](https://semver.org/).
- **Bedrock — Knowledge Base ingestion reads the bucket, and `Retrieve` retrieves** — `StartIngestionJob` answered `COMPLETE` with an all-zero statistics block without touching the data source, and `Retrieve` answered `[]` forever, so a green ingestion run was indistinguishable from a working one. The job now reads the S3 data source (honouring `inclusionPrefixes`) and reports real statistics — scanned, new versus modified, and non-UTF-8 documents as failed with a reason each; a source it cannot read at all (missing bucket, non-S3 type) lands `FAILED` with `failureReasons` instead of a fabricated success. `Retrieve` searches the ingested documents lexically and returns content, the `s3Location` of each hit and a score, `ResourceNotFoundException` for an unknown knowledge base. Reported by @bradleyhet.
- **KMS — multi-Region keys and `ReplicateKey`** — `CreateKey` with `MultiRegion` mints an `mrk-` key carrying `MultiRegionConfiguration`, and `ReplicateKey` creates a same-id replica in another region sharing the key material, so a ciphertext from the primary decrypts against the replica. Invalid sources and occupied regions are refused as on AWS, and deleting a primary with live replicas lands `PendingReplicaDeletion`. Reported by @cringdahl.
+### Changed
+- **RDS — the Aurora MySQL replication channel operations are role-independent** — No user-visible behavior changes. Contributed by @Areson.
+
### Fixed
- **RDS — a cluster endpoint is a stable name, not the container's current address** — the endpoint reported for a cluster changed as it came up (an AWS-shaped hostname, then `localhost`, then the container's IP), so anything that stored it — Terraform reads once, at create — held a value that went stale whenever the container was replaced. The AWS-shaped endpoint the cluster already advertises is now registered as a Docker network alias on its container and reported unchanged, so the stored value keeps resolving after a replacement. Skipped where Docker refuses aliases (the default bridge), which behaves exactly as before; `localhost` and `MINISTACK_HOST` never qualify as aliases. Contributed by @jbschooley.
- **Gateway — a gzip-compressed request body is inflated before the service reads it** — smithy's `@requestCompression` trait makes an AWS SDK gzip a request body once it passes `REQUEST_MIN_COMPRESSION_SIZE_BYTES` (default 10240) and send `Content-Encoding: gzip`; CloudWatch `PutMetricData` carries the trait, so boto3 compresses it with no client configuration. The handler parsed the compressed bytes, so a `PutMetricData` call of more than 10 KB answered `200` and stored nothing, losing every datapoint in the batch without an error. The body is now inflated once the target service is known and the gzip token dropped from `Content-Encoding`. S3 is excluded: there `Content-Encoding` is object metadata, so a gzip upload keeps the exact bytes it was sent and returns the header on `GetObject`. Contributed by @Lukasdoe.
diff --git a/README.md b/README.md
index cb252482..d4e7e7c6 100644
--- a/README.md
+++ b/README.md
@@ -433,7 +433,7 @@ subnet = ec2.create_subnet(
| **Step Functions** | CreateStateMachine, DeleteStateMachine, DescribeStateMachine, UpdateStateMachine, ListStateMachines, StartExecution, StartSyncExecution, StopExecution, DescribeExecution, DescribeStateMachineForExecution, ListExecutions, GetExecutionHistory, SendTaskSuccess, SendTaskFailure, SendTaskHeartbeat, CreateActivity, DeleteActivity, DescribeActivity, ListActivities, GetActivityTask, TestState, TagResource, UntagResource, ListTagsForResource | Full ASL interpreter; Retry/Catch; waitForTaskToken; Activities (worker pattern); Pass/Task/Choice/Wait/Succeed/Fail/Map/Parallel; TestState API with mock and inspectionLevel support; SFN_MOCK_CONFIG for AWS SFN Local compatible mock testing; intrinsic functions (States.StringToJson, States.JsonToString, States.JsonMerge, States.Format); nested startExecution.sync |
| **API Gateway v2** | CreateApi, GetApi, GetApis, UpdateApi, DeleteApi, CreateRoute, GetRoute, GetRoutes, UpdateRoute, DeleteRoute, CreateIntegration, GetIntegration, GetIntegrations, UpdateIntegration, DeleteIntegration, CreateRouteResponse, GetRouteResponse, GetRouteResponses, UpdateRouteResponse, DeleteRouteResponse, CreateIntegrationResponse, GetIntegrationResponse, GetIntegrationResponses, UpdateIntegrationResponse, DeleteIntegrationResponse, CreateStage, GetStage, GetStages, UpdateStage, DeleteStage, CreateDeployment, GetDeployment, GetDeployments, DeleteDeployment, CreateAuthorizer, GetAuthorizer, GetAuthorizers, UpdateAuthorizer, DeleteAuthorizer, TagResource, UntagResource, GetTags, PostToConnection, GetConnection, DeleteConnection | **HTTP API** and **WebSocket API** (`protocolType=WEBSOCKET`); Lambda proxy (`AWS_PROXY`), HTTP proxy (`HTTP_PROXY`), and MOCK integrations; HTTP data plane via `{apiId}.execute-api.localhost` Host header or path-based `/_aws/execute-api/{apiId}/{stage}/{path}` (no DNS/Host override needed — works from browsers on macOS and strict clients); `$default` stage served from the URL root (no stage segment in the path); per-API `corsConfiguration` applied to preflights + dispatched responses; request parameter mapping for HTTP_PROXY (`append/overwrite/remove` for headers/querystring plus `overwrite:path`) with context variables including `$context.authorizer.jwt.claims`; JWT data-plane authorization for HTTP routes (issuer/audience/time/scope checks) and claim propagation to integrations; qualified-alias integration URIs (`arn:...:function:<name>:<alias>`) resolve to the alias's target version; WebSocket data plane on the same two URL forms, with `$connect` / `$disconnect` / `$default` / custom-action routing, `$request.body.*` RouteSelectionExpression, `@connections` management API (PostToConnection / GetConnection / DeleteConnection), per-connection outbox for server-side push; `{param}` / `{proxy+}` matching; JWT/Lambda authorizer CRUD; pin `apiId` across runs with the `ms-custom-id` tag |
| **API Gateway v1** | CreateRestApi, GetRestApi, GetRestApis, UpdateRestApi, DeleteRestApi, CreateResource, GetResource, GetResources, UpdateResource, DeleteResource, PutMethod, GetMethod, DeleteMethod, UpdateMethod, PutMethodResponse, GetMethodResponse, DeleteMethodResponse, PutIntegration, GetIntegration, DeleteIntegration, UpdateIntegration, PutIntegrationResponse, GetIntegrationResponse, DeleteIntegrationResponse, CreateDeployment, GetDeployment, GetDeployments, UpdateDeployment, DeleteDeployment, CreateStage, GetStage, GetStages, UpdateStage, DeleteStage, CreateAuthorizer, GetAuthorizer, GetAuthorizers, UpdateAuthorizer, DeleteAuthorizer, CreateModel, GetModel, GetModels, DeleteModel, CreateDocumentationPart, GetDocumentationPart, GetDocumentationParts, UpdateDocumentationPart, DeleteDocumentationPart, CreateApiKey, GetApiKey, GetApiKeys, UpdateApiKey, DeleteApiKey, CreateUsagePlan, GetUsagePlan, GetUsagePlans, UpdateUsagePlan, DeleteUsagePlan, CreateUsagePlanKey, GetUsagePlanKeys, DeleteUsagePlanKey, CreateDomainName, GetDomainName, GetDomainNames, DeleteDomainName, CreateBasePathMapping, GetBasePathMapping, GetBasePathMappings, DeleteBasePathMapping, TagResource, UntagResource, GetTags | REST API (v1) protocol; Lambda proxy format 1.0 (AWS_PROXY), non-proxy Lambda (AWS — the handler's output is the response body, mapping templates not modeled), HTTP proxy (HTTP_PROXY), MOCK integration; data plane via `{apiId}.execute-api.localhost` Host header, path-based `/_aws/execute-api/{apiId}/{stage}/{path}`, or legacy `/restapis/{apiId}/{stage}/_user_request_/{path}`; qualified-alias integration URIs (`arn:...:function:<name>:<alias>`) resolve to the alias's target version; resource tree with `{param}` and `{proxy+}` path matching; routing is on the resource+method pair, so a request the matched resource does not serve falls through to a `{proxy+}` elsewhere in the tree (a methodless intermediate, a CORS-preflight-only node, or any undeclared verb), as it does on AWS; TOKEN and REQUEST Lambda authorizers on the data plane, with the returned IAM policy evaluated against each request's own method ARN and `authorizerResultTtlInSeconds` caching the policy per stage and identity source; JSON Patch for all PATCH operations; state persistence; pin `id` across runs with the `ms-custom-id` tag |
-| **ELBv2 / ALB** | CreateLoadBalancer, DescribeLoadBalancers, DeleteLoadBalancer, DescribeLoadBalancerAttributes, ModifyLoadBalancerAttributes, CreateTargetGroup, DescribeTargetGroups, ModifyTargetGroup, DeleteTargetGroup, DescribeTargetGroupAttributes, ModifyTargetGroupAttributes, CreateListener, DescribeListeners, ModifyListener, DeleteListener, CreateRule, DescribeRules, ModifyRule, DeleteRule, SetRulePriorities, SetSubnets, SetIpAddressType, SetSecurityGroups, RegisterTargets, DeregisterTargets, DescribeTargetHealth, AddTags, RemoveTags, DescribeTags | Control plane + data plane; ALB→Lambda live traffic routing; `path-pattern`, `host-header`, `http-method`, `query-string`, `http-header` rule conditions; `forward`, `redirect`, `fixed-response` actions; data plane via `{lb-name}.alb.localhost` Host header or `/_alb/{lb-name}/` path prefix |
+| **ELBv2 / ALB** | CreateLoadBalancer, DescribeLoadBalancers, DeleteLoadBalancer, DescribeLoadBalancerAttributes, ModifyLoadBalancerAttributes, CreateTargetGroup, DescribeTargetGroups, ModifyTargetGroup, DeleteTargetGroup, DescribeTargetGroupAttributes, ModifyTargetGroupAttributes, CreateListener, DescribeListeners, ModifyListener, DeleteListener, CreateRule, DescribeRules, ModifyRule, DeleteRule, SetRulePriorities, SetSubnets, SetIpAddressType, SetSecurityGroups, RegisterTargets, DeregisterTargets, DescribeTargetHealth, AddTags, RemoveTags, DescribeTags | Control plane + data plane; ALB→Lambda live traffic routing; `path-pattern`, `host-header`, `http-method`, `query-string`, `http-header` rule conditions; `forward`, `redirect`, `fixed-response`, `authenticate-oidc` actions; data plane via `{lb-name}.alb.localhost` Host header or `/_alb/{lb-name}/` path prefix |
| **KMS** | CreateKey, ReplicateKey, ListKeys, DescribeKey, GetPublicKey, Sign, Verify, Encrypt, Decrypt, GenerateMac, VerifyMac, GenerateDataKey, GenerateDataKeyWithoutPlaintext, GenerateDataKeyPair, GenerateDataKeyPairWithoutPlaintext, GenerateRandom, CreateAlias, DeleteAlias, ListAliases, UpdateAlias, UpdateKeyDescription, EnableKeyRotation, DisableKeyRotation, GetKeyRotationStatus, GetKeyPolicy, PutKeyPolicy, ListKeyPolicies, EnableKey, DisableKey, ScheduleKeyDeletion, CancelKeyDeletion, TagResource, UntagResource, ListResourceTags | 34 actions; multi-Region keys: `CreateKey` with `MultiRegion` mints an `mrk-` key and `ReplicateKey` creates a same-id replica in another region sharing the key material (ciphertext from the primary decrypts against the replica; a primary with live replicas waits in `PendingReplicaDeletion`); CMK specs: RSA (2048/3072/4096), ECC (SECG_P256K1, NIST P-256/384/521), Edwards25519 (`ECC_NIST_EDWARDS25519` — pure Ed25519 via `ED25519_SHA_512` with `MessageType=RAW`; `ED25519_PH_SHA_512` advertised in `SigningAlgorithms` but returns `UnsupportedOperationException` until Ed25519ph lands), HMAC (`HMAC_224`/`HMAC_256`/`HMAC_384`/`HMAC_512` with `KeyUsage=GENERATE_VERIFY_MAC`), and symmetric keys; PKCS1v15, PSS, and ECDSA signing; real RFC 2104 HMAC via `GenerateMac`/`VerifyMac` (4096-byte message cap, `KMSInvalidMacException` on mismatch, `DryRun` supported); envelope encryption, incl. asymmetric data key pairs (`KeyPairSpec` adds RSA_3072); alias resolution; key rotation; key policies; tags; enable/disable/schedule deletion; full Terraform `aws_kms_key` compatible; `cryptography` package included in Docker image |
| **CloudFront** | CreateDistribution, GetDistribution, GetDistributionConfig, ListDistributions, UpdateDistribution, DeleteDistribution, CreateInvalidation, ListInvalidations, GetInvalidation, CreateOriginAccessControl, GetOriginAccessControl, GetOriginAccessControlConfig, ListOriginAccessControls, UpdateOriginAccessControl, DeleteOriginAccessControl, CreateFunction, DeleteFunction, DescribeFunction, GetFunction, ListFunctions, PublishFunction, UpdateFunction, **KeyValueStore (mgmt)**: CreateKeyValueStore, DescribeKeyValueStore, ListKeyValueStores, UpdateKeyValueStore, DeleteKeyValueStore, **Policies**: CreateCachePolicy, GetCachePolicy, GetCachePolicyConfig, UpdateCachePolicy, DeleteCachePolicy, ListDistributionsByCachePolicyId, CreateOriginRequestPolicy, GetOriginRequestPolicy, GetOriginRequestPolicyConfig, UpdateOriginRequestPolicy, DeleteOriginRequestPolicy, ListDistributionsByOriginRequestPolicyId, CreateResponseHeadersPolicy, GetResponseHeadersPolicy, GetResponseHeadersPolicyConfig, UpdateResponseHeadersPolicy, DeleteResponseHeadersPolicy, ListDistributionsByResponseHeadersPolicyId, **SaaS Manager**: CreateConnectionGroup, GetConnectionGroup, GetConnectionGroupByRoutingEndpoint, UpdateConnectionGroup, DeleteConnectionGroup, ListConnectionGroups, CreateDistributionTenant, GetDistributionTenant, GetDistributionTenantByDomain, UpdateDistributionTenant, DeleteDistributionTenant, ListDistributionTenants, ListDistributionTenantsByCustomization, AssociateDistributionTenantWebACL, DisassociateDistributionTenantWebACL, CreateInvalidationForDistributionTenant, GetInvalidationForDistributionTenant, ListInvalidationsForDistributionTenant, VerifyDnsConfiguration, GetManagedCertificateDetails, ListDomainConflicts, UpdateDomainAssociation, ListDistributionsByConnectionMode, TagResource, UntagResource, ListTagsForResource | REST/XML API; ETag-based optimistic concurrency; Origin config round-trip; cache / origin request / response headers policies round-trip their full config with `ETag` / `If-Match` optimistic concurrency; Origin Access Control (OAC) with SigV4 signing for S3, MediaStore, Lambda, MediaPackageV2 origins; **CloudFront Functions** are API stubs (in-memory code + DEVELOPMENT/LIVE ETags for Terraform `aws_cloudfront_function`) with `KeyValueStoreAssociations` round-tripped — no `TestFunction`, no viewer-request JS execution at the edge; `UpdateFunction` clears the emulated LIVE stage until `PublishFunction` runs again; **SaaS Manager** multi-tenant distributions: `DistributionConfig` round-trips `ConnectionMode` / `TenantConfig`, tenants require a `tenant-only` distribution, CNAMEs are unique across tenants and distribution aliases, a tenant without an explicit connection group lazily gets the account default group, and MiniStack collapses async workflows (tenants deploy, domains activate, DNS verifies, managed certificates issue immediately) |
| **CloudFront KeyValueStore (data plane)** | DescribeKeyValueStore, ListKeys, GetKey, PutKey, DeleteKey, UpdateKeys | Separate `cloudfront-keyvaluestore` SDK service (REST/JSON, signing name `cloudfront-keyvaluestore`); ETag-based optimistic concurrency on every mutating op; `UpdateKeys` is atomic (validates whole batch, rejects on first invalid entry); `ListKeys` paginates with opaque `NextToken` capped at 50 results per AWS spec; bridges to the management plane via the shared KVS ARN |
diff --git a/ministack/services/alb.py b/ministack/services/alb.py
index 8d58d784..f527799b 100644
--- a/ministack/services/alb.py
+++ b/ministack/services/alb.py
@@ -28,7 +28,7 @@ import random
import socket
import string
import time
-from urllib.parse import parse_qs
+from urllib.parse import parse_qs, urlencode
from ministack.core.arn import ArnParseError, parse_arn
from ministack.core.concurrency import run_reentrant
@@ -44,6 +44,22 @@ TARGET_CONNECT_TIMEOUT = float(os.environ.get("ALB_TARGET_CONNECT_TIMEOUT_SECOND
TARGET_IDLE_TIMEOUT = float(os.environ.get("ALB_TARGET_IDLE_TIMEOUT_SECONDS", "60"))
REGION = os.environ.get("MINISTACK_REGION", "us-east-1")
+
+# authenticate-oidc defaults, matching the AWS API.
+OIDC_DEFAULT_COOKIE_NAME = "AWSELBAuthSessionCookie"
+OIDC_DEFAULT_SESSION_TIMEOUT = 604800 # 7 days, as AWS documents
+OIDC_CALLBACK_PATH = "/oauth2/idpresponse"
+# AWS splits the session cookie into shards named -0, -1, -2, … A client that
+# reads only the first shard silently loses the tail of a large session, so the
+# split is reproduced here rather than smoothed over.
+#
+# 4096 is the per-cookie ceiling browsers enforce (RFC 6265 §6.1 asks for at
+# least 4096 bytes per cookie, and the major engines treat it as the maximum),
+# and it covers the *whole* Set-Cookie pair — name, value and attributes. A
+# shard sized to 4096 bytes of value alone produces a cookie the browser
+# silently discards, which is the same symptom as no session at all, so the
+# name and attributes are subtracted from the budget below.
+OIDC_COOKIE_MAX_BYTES = 4096
NS = "http://elasticloadbalancing.amazonaws.com/doc/2015-12-01/"
# ---------------------------------------------------------------------------
@@ -62,6 +78,13 @@ _lb_attrs = AccountScopedDict() # lb_arn -> [{Key, Value}] (ARN key embeds r
_tg_attrs = AccountScopedDict() # tg_arn -> [{Key, Value}] (ARN key embeds region)
_listener_attrs = AccountScopedDict() # l_arn -> [{Key, Value}] (ARN key embeds region)
+# authenticate-oidc: in-flight authorization requests, keyed by the opaque
+# `state` value handed to the identity provider. Holds only the URL to return
+# the browser to once the callback lands — the session itself lives in the
+# client's cookie, exactly as it does on a real load balancer, so nothing here
+# is needed to validate a request.
+_oidc_pending = AccountScopedDict() # state -> {"url": str, "created": float}
+
def get_state():
return copy.deepcopy({
@@ -74,6 +97,7 @@ def get_state():
"_lb_attrs": _lb_attrs,
"_tg_attrs": _tg_attrs,
"_listener_attrs": _listener_attrs,
+ "_oidc_pending": _oidc_pending,
})
@@ -115,6 +139,7 @@ def restore_state(data):
_lb_attrs.update(data.get("_lb_attrs", {}))
_tg_attrs.update(data.get("_tg_attrs", {}))
_listener_attrs.update(data.get("_listener_attrs", {}))
+ _oidc_pending.update(data.get("_oidc_pending", {}))
try:
@@ -241,6 +266,32 @@ def _parse_actions(params, prefix="DefaultActions"):
"Path": _p(params, f"{prefix}.member.{i}.RedirectConfig.Path", "/#{path}"),
"StatusCode": rc_code,
}
+ oidc_issuer = _p(params, f"{prefix}.member.{i}.AuthenticateOidcConfig.Issuer")
+ if oidc_issuer:
+ oidc = {
+ "Issuer": oidc_issuer,
+ "AuthorizationEndpoint": _p(params, f"{prefix}.member.{i}.AuthenticateOidcConfig.AuthorizationEndpoint"),
+ "TokenEndpoint": _p(params, f"{prefix}.member.{i}.AuthenticateOidcConfig.TokenEndpoint"),
+ "UserInfoEndpoint": _p(params, f"{prefix}.member.{i}.AuthenticateOidcConfig.UserInfoEndpoint"),
+ "ClientId": _p(params, f"{prefix}.member.{i}.AuthenticateOidcConfig.ClientId"),
+ "Scope": _p(params, f"{prefix}.member.{i}.AuthenticateOidcConfig.Scope", "openid"),
+ "SessionCookieName": _p(params, f"{prefix}.member.{i}.AuthenticateOidcConfig.SessionCookieName",
+ OIDC_DEFAULT_COOKIE_NAME),
+ "SessionTimeout": _p(params, f"{prefix}.member.{i}.AuthenticateOidcConfig.SessionTimeout",
+ str(OIDC_DEFAULT_SESSION_TIMEOUT)),
+ "OnUnauthenticatedRequest": _p(params, f"{prefix}.member.{i}.AuthenticateOidcConfig.OnUnauthenticatedRequest",
+ "authenticate"),
+ }
+ secret = _p(params, f"{prefix}.member.{i}.AuthenticateOidcConfig.ClientSecret")
+ if secret:
+ # Held for the back-channel token exchange only. AWS never
+ # returns it from Describe*, and neither does _action_xml.
+ oidc["ClientSecret"] = secret
+ reuse = _p(params, f"{prefix}.member.{i}.AuthenticateOidcConfig.UseExistingClientSecret")
+ if reuse:
+ oidc["UseExistingClientSecret"] = reuse.lower() == "true"
+ action["AuthenticateOidcConfig"] = oidc
+
fr_code = _p(params, f"{prefix}.member.{i}.FixedResponseConfig.StatusCode")
if fr_code:
action["FixedResponseConfig"] = {
@@ -433,6 +484,22 @@ def _action_xml(a):
f"<StatusCode>{rc.get('StatusCode','HTTP_301')}</StatusCode>"
f"</RedirectConfig>"
)
+ if "AuthenticateOidcConfig" in a:
+ oc = a["AuthenticateOidcConfig"]
+ # ClientSecret is deliberately absent: AWS never echoes it back.
+ inner += (
+ f"<AuthenticateOidcConfig>"
+ f"<Issuer>{oc.get('Issuer','')}</Issuer>"
+ f"<AuthorizationEndpoint>{oc.get('AuthorizationEndpoint','')}</AuthorizationEndpoint>"
+ f"<TokenEndpoint>{oc.get('TokenEndpoint','')}</TokenEndpoint>"
+ f"<UserInfoEndpoint>{oc.get('UserInfoEndpoint','')}</UserInfoEndpoint>"
+ f"<ClientId>{oc.get('ClientId','')}</ClientId>"
+ f"<Scope>{oc.get('Scope','openid')}</Scope>"
+ f"<SessionCookieName>{oc.get('SessionCookieName', OIDC_DEFAULT_COOKIE_NAME)}</SessionCookieName>"
+ f"<SessionTimeout>{oc.get('SessionTimeout', OIDC_DEFAULT_SESSION_TIMEOUT)}</SessionTimeout>"
+ f"<OnUnauthenticatedRequest>{oc.get('OnUnauthenticatedRequest','authenticate')}</OnUnauthenticatedRequest>"
+ f"</AuthenticateOidcConfig>"
+ )
if "FixedResponseConfig" in a:
frc = a["FixedResponseConfig"]
inner += (
@@ -777,6 +844,13 @@ def _modify_listener(params):
actions = _parse_actions(params, "DefaultActions")
if actions:
listener["DefaultActions"] = actions
+ # CreateListener snapshots the default actions into an auto-created
+ # default rule, and the data plane reads the rule, not the listener.
+ # Updating only the listener leaves the old actions serving traffic —
+ # ModifyListener would appear to succeed and change nothing.
+ for rule in _rules.values():
+ if rule.get("ListenerArn") == arn and rule.get("IsDefault"):
+ rule["Actions"] = actions
return _xml(200, "ModifyListener", f"<Listeners>{_listener_xml(listener)}</Listeners>")
@@ -1209,10 +1283,400 @@ def _rule_sort_key(rule):
return (0, 9999)
+# ---------------------------------------------------------------------------
+# Data-plane: authenticate-oidc
+# ---------------------------------------------------------------------------
+#
+# The listener rule action that makes a load balancer an OIDC relying party.
+# An unauthenticated request is sent to the identity provider; the provider
+# returns the browser to OIDC_CALLBACK_PATH with a code; the load balancer
+# exchanges that code for tokens on the back channel and writes the session
+# into the client's cookie. Only then does the request reach the target, with
+# the caller's identity attached in X-Amzn-Oidc-* headers.
+#
+# The session lives entirely in the cookie, as it does on AWS, so no
+# server-side lookup is needed to validate a request. AWS encrypts that cookie
+# with a key only the load balancer holds; here it is base64-encoded JSON,
+# because the point is to reproduce the protocol a client observes, not to
+# keep a secret from the developer running the emulator.
+
+
+def _oidc_parse_cookies(headers):
+ """Parse a Cookie header into a plain dict."""
+ jar = {}
+ for part in (headers.get("cookie") or "").split(";"):
+ name, sep, value = part.partition("=")
+ if sep:
+ jar[name.strip()] = value.strip()
+ return jar
+
+
+def _oidc_read_session(jar, cookie_name):
+ """Reassemble a session from its cookie shards, or return None.
+
+ Shards must be contiguous from -0. A gap means the client dropped part of
+ the session, which is indistinguishable from having no session at all.
+ """
+ shards, index = [], 0
+ while f"{cookie_name}-{index}" in jar:
+ shards.append(jar[f"{cookie_name}-{index}"])
+ index += 1
+ if not shards:
+ return None
+ try:
+ blob = "".join(shards)
+ blob += "=" * (-len(blob) % 4) # restore stripped base64 padding
+ return json.loads(base64.urlsafe_b64decode(blob.encode()))
+ except Exception:
+ return None
+
+
+def _oidc_session_cookies(cookie_name, session, max_age, secure):
+ """Render a session as Set-Cookie values, sharded the way AWS shards them.
+
+ Each shard is sized so the complete Set-Cookie pair fits inside the
+ browser's per-cookie ceiling — see OIDC_COOKIE_MAX_BYTES.
+ """
+ blob = base64.urlsafe_b64encode(json.dumps(session).encode()).decode().rstrip("=")
+ attrs = f"; Path=/; HttpOnly; Max-Age={max_age}"
+ if secure:
+ attrs += "; Secure"
+
+ # Budget for the value alone. The index is allowed three digits, which is
+ # far more shards than a session can plausibly need, so the reservation
+ # stays correct as the count grows.
+ overhead = len(f"{cookie_name}-000=") + len(attrs)
+ budget = max(OIDC_COOKIE_MAX_BYTES - overhead, 1)
+
+ shards = [blob[i:i + budget] for i in range(0, len(blob), budget)] or [""]
+ return [f"{cookie_name}-{i}={shard}{attrs}" for i, shard in enumerate(shards)]
+
+
+def _oidc_expiry_cookies(cookie_name, count):
+ """Expire previously-set shards so a stale session cannot linger."""
+ return [f"{cookie_name}-{i}=; Path=/; HttpOnly; Max-Age=0" for i in range(max(count, 1))]
+
+
+def _oidc_request_url(headers, path, query_params, listener_port):
+ """Rebuild the URL the client asked for, to return to after authenticating."""
+ host = headers.get("host") or f"localhost:{listener_port}"
+ proto = headers.get("x-forwarded-proto") or "http"
+ qs = urlencode([(k, v) for k, vs in (query_params or {}).items()
+ for v in (vs if isinstance(vs, list) else [vs])])
+ return f"{proto}://{host}{path}" + (f"?{qs}" if qs else "")
+
+
+def _oidc_post_form(url, form, timeout=10.0):
+ """POST a form to the identity provider's token endpoint and read JSON back.
+
+ Blocking on purpose: run it through run_reentrant, the same way the target
+ proxy does, rather than adding an async HTTP dependency to the emulator.
+ """
+ import http.client
+ from urllib.parse import urlsplit
+
+ parts = urlsplit(url)
+ conn_cls = http.client.HTTPSConnection if parts.scheme == "https" else http.client.HTTPConnection
+ conn = conn_cls(parts.hostname, parts.port or (443 if parts.scheme == "https" else 80),
+ timeout=timeout)
+ try:
+ body = urlencode(form)
+ conn.request("POST", parts.path or "/", body=body, headers={
+ "Content-Type": "application/x-www-form-urlencoded",
+ "Content-Length": str(len(body)),
+ "Accept": "application/json",
+ })
+ resp = conn.getresponse()
+ raw = resp.read()
+ try:
+ return resp.status, json.loads(raw or b"{}")
+ except ValueError:
+ return resp.status, {"raw": raw.decode("utf-8", errors="replace")}
+ finally:
+ with contextlib.suppress(Exception):
+ conn.close()
+
+
+def _oidc_get_userinfo(url, access_token, timeout=10.0):
+ """GET the IdP's user info endpoint with the access token.
+
+ This is where the claims the target sees come from on AWS: the load
+ balancer exchanges the access token at the user info endpoint and passes
+ those claims on — it never passes the ID token to the backend.
+ """
+ import http.client
+ from urllib.parse import urlsplit
+
+ parts = urlsplit(url)
+ conn_cls = http.client.HTTPSConnection if parts.scheme == "https" else http.client.HTTPConnection
+ conn = conn_cls(parts.hostname, parts.port or (443 if parts.scheme == "https" else 80),
+ timeout=timeout)
+ try:
+ conn.request("GET", parts.path or "/", headers={
+ "Authorization": f"Bearer {access_token}",
+ "Accept": "application/json",
+ })
+ resp = conn.getresponse()
+ raw = resp.read()
+ if resp.status >= 400:
+ return None
+ try:
+ claims = json.loads(raw or b"{}")
+ except ValueError:
+ return None
+ return claims if isinstance(claims, dict) else None
+ except Exception:
+ return None
+ finally:
+ with contextlib.suppress(Exception):
+ conn.close()
+
+
+# The key the emulator signs x-amzn-oidc-data with. AWS signs with a regional
+# key served from public-keys.auth.elb.<region>.amazonaws.com/<kid>; there is
+# no such endpoint locally, so the signature is real ES256 but verifiable only
+# in principle. Apps that decode the payload (the common pattern) work; apps
+# that fetch the AWS key endpoint fail loudly, exactly as they would against
+# any non-AWS host.
+_OIDC_SIGNING_KEY = None
+_OIDC_KID = None
+
+
+def _oidc_signing_material():
+ global _OIDC_SIGNING_KEY, _OIDC_KID
+ if _OIDC_KID is None:
+ _OIDC_KID = new_uuid()
+ try:
+ from cryptography.hazmat.primitives.asymmetric import ec
+ _OIDC_SIGNING_KEY = ec.generate_private_key(ec.SECP256R1())
+ except Exception:
+ _OIDC_SIGNING_KEY = None
+ return _OIDC_SIGNING_KEY, _OIDC_KID
+
+
+def _oidc_b64url(raw: bytes) -> str:
+ # ALB's documented quirk: unlike standard JWTs, the x-amzn-oidc-data
+ # segments are base64url WITH the trailing padding characters ("includes
+ # padding characters at the end" per the user-claims-encoding docs) —
+ # strict JWT libraries choke on real ALB tokens for exactly this reason,
+ # so an emulator that strips the padding hides that failure mode.
+ return base64.urlsafe_b64encode(raw).decode()
+
+
+def _oidc_data_jwt(claims, cfg, lb_arn, exp):
+ """Build x-amzn-oidc-data the way AWS documents it: the user claims as an
+ ES256 JWT whose header carries ``kid``, ``signer`` (the load balancer ARN),
+ ``iss``, ``client`` and ``exp``. The payload is the claims from the user
+ info endpoint — never the provider's ID token, which AWS does not pass to
+ the backend."""
+ key, kid = _oidc_signing_material()
+ header = {
+ "typ": "JWT",
+ "alg": "ES256",
+ "kid": kid,
+ "signer": lb_arn,
+ "iss": cfg.get("Issuer", ""),
+ "client": cfg.get("ClientId", ""),
+ "exp": exp,
+ }
+ signing_input = (
+ _oidc_b64url(json.dumps(header, separators=(",", ":")).encode())
+ + "."
+ + _oidc_b64url(json.dumps(claims, separators=(",", ":")).encode())
+ )
+ signature = b""
+ if key is not None:
+ try:
+ from cryptography.hazmat.primitives import hashes
+ from cryptography.hazmat.primitives.asymmetric import ec, utils
+ der = key.sign(signing_input.encode(), ec.ECDSA(hashes.SHA256()))
+ r, s = utils.decode_dss_signature(der)
+ signature = r.to_bytes(32, "big") + s.to_bytes(32, "big")
+ except Exception:
+ signature = b""
+ return f"{signing_input}.{_oidc_b64url(signature)}"
+
+
+def _oidc_claims(id_token):
+ """Read the claims out of an ID token without verifying it.
+
+ The token came straight from the provider over the back channel, so there
+ is no third party to have tampered with it. Verification would mean
+ fetching JWKS, which buys an emulator nothing.
+ """
+ try:
+ payload = id_token.split(".")[1]
+ payload += "=" * (-len(payload) % 4)
+ return json.loads(base64.urlsafe_b64decode(payload.encode()))
+ except Exception:
+ return {}
+
+
+async def _authenticate_oidc(action, method, path, headers, body, query_params, listener_port,
+ lb_arn=""):
+ """Run one authenticate-oidc action.
+
+ Returns None when the caller is authenticated and the request should carry
+ on to the next action in the rule; otherwise returns the (status, headers,
+ body) triple to send back. On success the caller's identity is injected
+ into `headers` for the target to read.
+ """
+ cfg = action.get("AuthenticateOidcConfig") or {}
+ cookie_name = cfg.get("SessionCookieName") or OIDC_DEFAULT_COOKIE_NAME
+ try:
+ session_timeout = int(cfg.get("SessionTimeout") or OIDC_DEFAULT_SESSION_TIMEOUT)
+ except (TypeError, ValueError):
+ session_timeout = OIDC_DEFAULT_SESSION_TIMEOUT
+ secure = (headers.get("x-forwarded-proto") or "").lower() == "https"
+ jar = _oidc_parse_cookies(headers)
+
+ # The X-Amzn-Oidc-* headers are the load balancer's to set. Strip any the
+ # client supplied before anything else runs — otherwise `allow` mode would
+ # forward a forged identity straight to the target.
+ for forged in ("x-amzn-oidc-identity", "x-amzn-oidc-accesstoken", "x-amzn-oidc-data"):
+ headers.pop(forged, None)
+
+ # --- the callback leg -------------------------------------------------
+ if path == OIDC_CALLBACK_PATH:
+ code = _oidc_first(query_params.get("code"))
+ state = _oidc_first(query_params.get("state"))
+ pending = _oidc_pending.pop(state, None) if state else None
+ if not code or pending is None:
+ # A replayed, forged, or timed-out callback. AWS bounds the whole
+ # login at 15 minutes and answers HTTP 401 when it is exceeded.
+ return (401, {"Content-Type": "text/plain"},
+ b"Invalid or expired authentication response")
+
+ status, tokens = await run_reentrant(
+ lambda: _oidc_post_form(cfg.get("TokenEndpoint", ""), {
+ "grant_type": "authorization_code",
+ "code": code,
+ "client_id": cfg.get("ClientId", ""),
+ "client_secret": cfg.get("ClientSecret", ""),
+ "redirect_uri": pending["redirect_uri"],
+ }),
+ thread_name="ministack-alb-oidc-token",
+ )
+ if status >= 400 or "access_token" not in tokens:
+ logger.warning("ALB authenticate-oidc: token exchange failed (%s): %s", status, tokens)
+ return (561, {"Content-Type": "text/plain"},
+ b"Authentication failed: the identity provider rejected the code exchange")
+
+ # The claims the target sees come from the user info endpoint, as on
+ # AWS; the ID token only serves as a fallback when the IdP has no
+ # usable user info response. AWS never passes the ID token itself to
+ # the backend, and neither does this.
+ userinfo = cfg.get("UserInfoEndpoint", "")
+ claims = None
+ if userinfo:
+ claims = await run_reentrant(
+ lambda: _oidc_get_userinfo(userinfo, tokens.get("access_token", "")),
+ thread_name="ministack-alb-oidc-userinfo",
+ )
+ if not claims:
+ claims = _oidc_claims(tokens.get("id_token", ""))
+ session = {
+ "claims": claims,
+ "access_token": tokens.get("access_token", ""),
+ "exp": int(time.time()) + session_timeout,
+ }
+ return (302, {
+ "Location": pending["url"],
+ "Set-Cookie": _oidc_session_cookies(cookie_name, session, session_timeout, secure),
+ "Content-Type": "text/plain",
+ }, b"")
+
+ # --- an established session -------------------------------------------
+ session = _oidc_read_session(jar, cookie_name)
+ if session and int(session.get("exp", 0)) > time.time():
+ claims = session.get("claims") or {}
+ headers["x-amzn-oidc-identity"] = str(claims.get("sub", ""))
+ headers["x-amzn-oidc-accesstoken"] = session.get("access_token", "")
+ headers["x-amzn-oidc-data"] = _oidc_data_jwt(
+ claims, cfg, lb_arn, int(session.get("exp", 0)))
+ return None
+
+ # --- no usable session -------------------------------------------------
+ on_unauth = (cfg.get("OnUnauthenticatedRequest") or "authenticate").lower()
+ if on_unauth == "allow":
+ return None
+ if on_unauth == "deny":
+ return (401, {"Content-Type": "text/plain"}, b"Unauthorized")
+
+ host = headers.get("host") or f"localhost:{listener_port}"
+ proto = headers.get("x-forwarded-proto") or "http"
+ redirect_uri = f"{proto}://{host}{OIDC_CALLBACK_PATH}"
+ state = new_uuid()
+ # AWS bounds a login attempt at 15 minutes; anything older is dead weight
+ # an abandoned redirect left behind.
+ cutoff = time.time() - 900
+ for stale in [k for k, v in _oidc_pending.items() if v.get("created", 0) < cutoff]:
+ _oidc_pending.pop(stale, None)
+ _oidc_pending[state] = {
+ "url": _oidc_request_url(headers, path, query_params, listener_port),
+ "redirect_uri": redirect_uri,
+ "created": time.time(),
+ }
+ location = cfg.get("AuthorizationEndpoint", "") + "?" + urlencode({
+ "client_id": cfg.get("ClientId", ""),
+ "response_type": "code",
+ "scope": cfg.get("Scope") or "openid",
+ "redirect_uri": redirect_uri,
+ "state": state,
+ })
+ expired = _oidc_expiry_cookies(cookie_name, len(jar)) if session else []
+ out_headers = {"Location": location, "Content-Type": "text/plain"}
+ if expired:
+ out_headers["Set-Cookie"] = expired
+ return (302, out_headers, b"")
+
+
+def _oidc_first(value):
+ """Query parameters arrive as either a scalar or a list."""
+ if isinstance(value, (list, tuple)):
+ return value[0] if value else None
+ return value
+
+
# ---------------------------------------------------------------------------
# Data-plane: action execution
# ---------------------------------------------------------------------------
+async def _execute_actions(actions, method, path, headers, body, query_params, listener_port=80,
+ lb_arn=""):
+ """Run a rule's actions in Order until one produces a response.
+
+ Authentication actions are gates: they either short-circuit with a redirect
+ or a refusal, or they pass the request on to the action behind them, having
+ attached the caller's identity. Every other action type is terminal, which
+ is why the loop returns on the first one it meets.
+ """
+ ordered = sorted(actions, key=lambda a: int(a.get("Order", 1) or 1))
+ forwarded = dict(headers)
+
+ for action in ordered:
+ atype = (action.get("Type") or "").lower()
+ if atype == "authenticate-oidc":
+ response = await _authenticate_oidc(action, method, path, forwarded,
+ body, query_params, listener_port,
+ lb_arn=lb_arn)
+ if response is not None:
+ return response
+ continue
+ if atype == "authenticate-cognito":
+ # Cognito authentication is the same protocol with the endpoints
+ # derived from a user pool rather than given explicitly. Not
+ # implemented; failing loudly beats forwarding an unauthenticated
+ # request to a target that assumes the load balancer checked.
+ return (501, {"Content-Type": "application/json"},
+ json.dumps({"message": "authenticate-cognito is not implemented"}).encode())
+ return await _execute_action(action, method, path, forwarded, body, query_params)
+
+ # Every action was a gate that passed and none of them was terminal.
+ return (502, {"Content-Type": "application/json"},
+ json.dumps({"message": "Rule has no terminal action"}).encode())
+
+
async def _execute_action(action, method, path, headers, body, query_params):
atype = action.get("Type", "").lower()
@@ -1510,8 +1974,9 @@ async def dispatch_request(lb, method, path, headers, body, query_params, port=8
if matched:
actions = rule.get("Actions") or listener.get("DefaultActions", [])
if actions:
- return await _execute_action(actions[0], method, path,
- headers, body, query_params)
+ return await _execute_actions(actions, method, path,
+ headers, body, query_params, port,
+ lb_arn=lb.get("LoadBalancerArn", ""))
return (502, {"Content-Type": "application/json"},
json.dumps({"message": "No matching ALB rule found"}).encode())
diff --git a/ministack/services/rds.py b/ministack/services/rds.py
index d49855af..324738e4 100644
--- a/ministack/services/rds.py
+++ b/ministack/services/rds.py
@@ -1994,6 +1994,18 @@ def _image_is_local(docker_client, image: str) -> bool:
return False
+def _instance_available_unless_stopped(instance):
+ """Finish a container worker without clobbering an intervened stop.
+
+ StopDBInstance can land while the worker is still pulling and starting
+ the container; flipping the status back to "available" here would undo
+ the stop the caller was already told succeeded — the same intervened-stop
+ rule the replicating-reader path applies.
+ """
+ if instance.get("DBInstanceStatus") != "stopped":
+ instance["DBInstanceStatus"] = "available"
+
+
def _start_rds_container_for_instance(db_id, instance):
"""Re-spin (or re-attach to) the Docker container for a restored instance.
@@ -2007,7 +2019,7 @@ def _start_rds_container_for_instance(db_id, instance):
"""
docker_client = _get_docker()
if not docker_client:
- instance["DBInstanceStatus"] = "available"
+ _instance_available_unless_stopped(instance)
return
engine = instance.get("Engine", "postgres")
@@ -2038,7 +2050,7 @@ def _start_rds_container_for_instance(db_id, instance):
engine, engine_version, master_user, master_pass, db_name,
)
if not image:
- instance["DBInstanceStatus"] = "available"
+ _instance_available_unless_stopped(instance)
return
container_name = _rds_docker_name(db_id)
@@ -2173,7 +2185,7 @@ def _start_rds_container_for_instance(db_id, instance):
except Exception:
pass
return
- instance["DBInstanceStatus"] = "available"
+ _instance_available_unless_stopped(instance)
logger.info("RDS: respawned container %s for instance %s",
container_name, db_id)
diff --git a/tests/test_alb.py b/tests/test_alb.py
index 67e047b1..63a85882 100644
--- a/tests/test_alb.py
+++ b/tests/test_alb.py
@@ -2,9 +2,11 @@ import io
import json
import os
import time
+import urllib.request as _req
import uuid as _uuid_mod
import zipfile
-from urllib.parse import urlparse
+from urllib.error import HTTPError
+from urllib.parse import parse_qs, urlparse
import pytest
from botocore.exceptions import ClientError
@@ -1277,3 +1279,544 @@ def test_alb_rule_conditions_round_trip_in_both_shapes(elbv2):
cond = rules[str(priority)]["Conditions"][0]
assert cond["Values"] == expected
assert cond[config_key]["Values"] == expected, f"{config_key} not returned: {cond}"
+
+
+# ===========================================================================
+# authenticate-oidc listener action
+# ===========================================================================
+
+
+class _NoRedirect(_req.HTTPRedirectHandler):
+ """Keep redirects visible: the 302 is the assertion, and its Location
+ points at an identity provider that does not exist."""
+
+ def redirect_request(self, req, fp, code, msg, headers, newurl):
+ return None
+
+
+_oidc_opener = _req.build_opener(_NoRedirect)
+
+
+def _oidc_get(path, host, headers=None):
+ req = _req.Request(f"{_endpoint}{path}", method="GET")
+ req.add_header("Host", f"{host}.alb.localhost:{_EXECUTE_PORT}")
+ for k, v in (headers or {}).items():
+ req.add_header(k, v)
+ try:
+ resp = _oidc_opener.open(req)
+ return resp.status, dict(resp.headers), resp.read()
+ except HTTPError as e:
+ return e.code, dict(e.headers), e.read()
+
+
+
+def _oidc_action(order=1, **overrides):
+ cfg = {
+ "Issuer": "https://idp.example.com",
+ "AuthorizationEndpoint": "https://idp.example.com/authorize",
+ "TokenEndpoint": "https://idp.example.com/token",
+ "UserInfoEndpoint": "https://idp.example.com/userinfo",
+ "ClientId": "client-abc",
+ "ClientSecret": "shhh",
+ "Scope": "openid email",
+ "SessionTimeout": 3600,
+ }
+ cfg.update(overrides)
+ return {"Type": "authenticate-oidc", "Order": order, "AuthenticateOidcConfig": cfg}
+
+
+# --------------------------------------------------------------- control plane
+
+def test_authenticate_oidc_config_round_trips(elbv2):
+ """The rule survives Create -> Describe with its configuration intact."""
+ lb = elbv2.create_load_balancer(Name="oidc-rt")["LoadBalancers"][0]
+ tg = elbv2.create_target_group(Name="oidc-rt-tg", TargetType="lambda")["TargetGroups"][0]
+ try:
+ listener = elbv2.create_listener(
+ LoadBalancerArn=lb["LoadBalancerArn"], Protocol="HTTP", Port=80,
+ DefaultActions=[
+ _oidc_action(order=1),
+ {"Type": "forward", "Order": 2, "TargetGroupArn": tg["TargetGroupArn"]},
+ ],
+ )["Listeners"][0]
+
+ described = elbv2.describe_listeners(ListenerArns=[listener["ListenerArn"]])["Listeners"][0]
+ actions = sorted(described["DefaultActions"], key=lambda a: a.get("Order", 1))
+ assert [a["Type"] for a in actions] == ["authenticate-oidc", "forward"]
+
+ cfg = actions[0]["AuthenticateOidcConfig"]
+ assert cfg["Issuer"] == "https://idp.example.com"
+ assert cfg["AuthorizationEndpoint"] == "https://idp.example.com/authorize"
+ assert cfg["TokenEndpoint"] == "https://idp.example.com/token"
+ assert cfg["ClientId"] == "client-abc"
+ assert cfg["Scope"] == "openid email"
+ assert cfg["SessionTimeout"] == 3600
+ # Defaults AWS fills in when the caller omits them.
+ assert cfg["SessionCookieName"] == "AWSELBAuthSessionCookie"
+ assert cfg["OnUnauthenticatedRequest"] == "authenticate"
+ # AWS never echoes the secret back, and neither do we.
+ assert "ClientSecret" not in cfg
+ finally:
+ elbv2.delete_load_balancer(LoadBalancerArn=lb["LoadBalancerArn"])
+ elbv2.delete_target_group(TargetGroupArn=tg["TargetGroupArn"])
+
+
+def test_authenticate_oidc_config_round_trips_on_a_rule(elbv2):
+ """A path-based rule carries the action just as a listener default does."""
+ lb = elbv2.create_load_balancer(Name="oidc-rule")["LoadBalancers"][0]
+ tg = elbv2.create_target_group(Name="oidc-rule-tg", TargetType="lambda")["TargetGroups"][0]
+ try:
+ listener = elbv2.create_listener(
+ LoadBalancerArn=lb["LoadBalancerArn"], Protocol="HTTP", Port=80,
+ DefaultActions=[{"Type": "forward", "TargetGroupArn": tg["TargetGroupArn"]}],
+ )["Listeners"][0]
+ rule = elbv2.create_rule(
+ ListenerArn=listener["ListenerArn"], Priority=10,
+ Conditions=[{"Field": "path-pattern", "Values": ["/private/*"]}],
+ Actions=[
+ _oidc_action(order=1, SessionCookieName="MySession"),
+ {"Type": "forward", "Order": 2, "TargetGroupArn": tg["TargetGroupArn"]},
+ ],
+ )["Rules"][0]
+
+ described = elbv2.describe_rules(RuleArns=[rule["RuleArn"]])["Rules"][0]
+ actions = sorted(described["Actions"], key=lambda a: a.get("Order", 1))
+ assert actions[0]["AuthenticateOidcConfig"]["SessionCookieName"] == "MySession"
+ finally:
+ elbv2.delete_load_balancer(LoadBalancerArn=lb["LoadBalancerArn"])
+ elbv2.delete_target_group(TargetGroupArn=tg["TargetGroupArn"])
+
+
+# ------------------------------------------------------------------- sharding
+
+def test_session_cookie_fits_the_browser_ceiling():
+ """Every shard must fit in 4096 bytes including its name and attributes.
+
+ A shard sized to 4096 bytes of value alone yields a Set-Cookie the browser
+ silently discards, which is indistinguishable from having no session.
+ """
+ from ministack.services import alb
+
+ session = {"id_token": "x" * 12000, "access_token": "y" * 4000, "exp": 99999999999}
+ cookies = alb._oidc_session_cookies("AWSELBAuthSessionCookie", session, 3600, secure=True)
+
+ assert len(cookies) > 1, "a session this size must shard"
+ for cookie in cookies:
+ assert len(cookie) <= alb.OIDC_COOKIE_MAX_BYTES, (
+ f"shard is {len(cookie)} bytes, over the {alb.OIDC_COOKIE_MAX_BYTES} ceiling"
+ )
+ assert "; Path=/" in cookie and "; HttpOnly" in cookie
+ assert "; Max-Age=3600" in cookie
+ assert "; Secure" in cookie
+
+
+def test_session_cookie_shards_are_named_in_sequence():
+ from ministack.services import alb
+
+ cookies = alb._oidc_session_cookies("AWSELBAuthSessionCookie",
+ {"id_token": "x" * 12000}, 60, secure=False)
+ for index, cookie in enumerate(cookies):
+ assert cookie.startswith(f"AWSELBAuthSessionCookie-{index}=")
+ assert "; Secure" not in cookies[0], "plain HTTP must not mark the cookie Secure"
+
+
+def test_session_survives_a_shard_round_trip():
+ from ministack.services import alb
+
+ original = {"sub": "user-1", "email": "[email protected]",
+ "id_token": "j" * 9000, "exp": 123}
+ cookies = alb._oidc_session_cookies("AWSELBAuthSessionCookie", original, 3600, False)
+
+ jar = {}
+ for cookie in cookies:
+ name, _, rest = cookie.partition("=")
+ jar[name] = rest.split(";", 1)[0]
+
+ assert alb._oidc_read_session(jar, "AWSELBAuthSessionCookie") == original
+
+
+def test_a_missing_shard_invalidates_the_session():
+ """A gap in the sequence means the client lost part of the session."""
+ from ministack.services import alb
+
+ cookies = alb._oidc_session_cookies("AWSELBAuthSessionCookie",
+ {"id_token": "j" * 9000}, 3600, False)
+ assert len(cookies) > 2
+ jar = {}
+ for cookie in cookies:
+ name, _, rest = cookie.partition("=")
+ jar[name] = rest.split(";", 1)[0]
+ del jar["AWSELBAuthSessionCookie-1"]
+
+ assert alb._oidc_read_session(jar, "AWSELBAuthSessionCookie") is None
+
+
+def test_no_session_cookie_reads_as_no_session():
+ from ministack.services import alb
+ assert alb._oidc_read_session({}, "AWSELBAuthSessionCookie") is None
+ assert alb._oidc_read_session({"AWSELBAuthSessionCookie-0": "not-base64!!"},
+ "AWSELBAuthSessionCookie") is None
+
+
+def test_cookie_header_parsing():
+ from ministack.services import alb
+ jar = alb._oidc_parse_cookies({"cookie": "a=1; b = 2 ;c=3; malformed"})
+ assert jar == {"a": "1", "b": "2", "c": "3"}
+ assert alb._oidc_parse_cookies({}) == {}
+
+
+def test_claims_are_read_without_verification():
+ """The ID token arrives over the back channel, so there is nothing to verify."""
+ import base64
+
+ from ministack.services import alb
+
+ payload = base64.urlsafe_b64encode(
+ json.dumps({"sub": "abc", "email": "[email protected]"}).encode()
+ ).decode().rstrip("=")
+ assert alb._oidc_claims(f"header.{payload}.signature")["sub"] == "abc"
+ assert alb._oidc_claims("not-a-jwt") == {}
+
+
+# ------------------------------------------------------------------ data plane
+
+def _alb_with_oidc(elbv2, lam, name, **cfg_overrides):
+ """Build an ALB whose only rule authenticates, then forwards to a Lambda."""
+ fn_code = (
+ "import json\n"
+ "def handler(event, context):\n"
+ " h = {k.lower(): v for k, v in (event.get('headers') or {}).items()}\n"
+ " return {'statusCode': 200, 'headers': {'Content-Type': 'application/json'},\n"
+ " 'body': json.dumps({'identity': h.get('x-amzn-oidc-identity'),\n"
+ " 'data': bool(h.get('x-amzn-oidc-data'))})}\n"
+ )
+ lb_arn, tg_arn, l_arn, fn_arn = _alb_setup(elbv2, lam, name, f"{name}-fn", fn_code)
+ elbv2.modify_listener(
+ ListenerArn=l_arn,
+ DefaultActions=[
+ _oidc_action(order=1, **cfg_overrides),
+ {"Type": "forward", "Order": 2, "TargetGroupArn": tg_arn},
+ ],
+ )
+ return lb_arn, tg_arn, l_arn
+
+
+
+
+def test_unauthenticated_request_redirects_to_the_identity_provider(elbv2, lam):
+ name = "oidc-dp-redir"
+ lb_arn, tg_arn, l_arn = _alb_with_oidc(elbv2, lam, name)
+ try:
+ status, headers, _ = _oidc_get("/private/thing", name)
+ assert status == 302
+
+ location = urlparse(headers["Location"])
+ assert location.netloc == "idp.example.com"
+ assert location.path == "/authorize"
+
+ q = parse_qs(location.query)
+ assert q["client_id"] == ["client-abc"]
+ assert q["response_type"] == ["code"]
+ assert q["scope"] == ["openid email"]
+ assert q["redirect_uri"][0].endswith("/oauth2/idpresponse")
+ assert q["state"], "the provider must be given a state to return"
+ finally:
+ _alb_teardown(elbv2, lam, lb_arn, tg_arn, l_arn, f"{name}-fn")
+
+
+def test_on_unauthenticated_request_allow_lets_the_request_through(elbv2, lam):
+ name = "oidc-dp-allow"
+ lb_arn, tg_arn, l_arn = _alb_with_oidc(elbv2, lam, name, OnUnauthenticatedRequest="allow")
+ try:
+ status, _, body = _oidc_get("/public", name)
+ assert status == 200
+ # The target runs, but with no identity attached.
+ assert json.loads(body)["identity"] is None
+ finally:
+ _alb_teardown(elbv2, lam, lb_arn, tg_arn, l_arn, f"{name}-fn")
+
+
+def test_on_unauthenticated_request_deny_refuses(elbv2, lam):
+ name = "oidc-dp-deny"
+ lb_arn, tg_arn, l_arn = _alb_with_oidc(elbv2, lam, name, OnUnauthenticatedRequest="deny")
+ try:
+ status, _, _ = _oidc_get("/private", name)
+ assert status == 401
+ finally:
+ _alb_teardown(elbv2, lam, lb_arn, tg_arn, l_arn, f"{name}-fn")
+
+
+def test_a_valid_session_reaches_the_target_with_identity_attached(elbv2, lam):
+ name = "oidc-dp-session"
+ lb_arn, tg_arn, l_arn = _alb_with_oidc(elbv2, lam, name)
+ try:
+ from ministack.services import alb
+
+ session = {"claims": {"sub": "user-42", "email": "[email protected]"},
+ "access_token": "at",
+ "exp": int(time.time()) + 600}
+ cookies = alb._oidc_session_cookies("AWSELBAuthSessionCookie", session, 600, False)
+ jar = "; ".join(c.split(";", 1)[0] for c in cookies)
+
+ status, _, body = _oidc_get("/private", name, {"Cookie": jar})
+ assert status == 200
+ payload = json.loads(body)
+ assert payload["identity"] == "user-42"
+ assert payload["data"] is True
+ finally:
+ _alb_teardown(elbv2, lam, lb_arn, tg_arn, l_arn, f"{name}-fn")
+
+
+def test_an_expired_session_redirects_again(elbv2, lam):
+ name = "oidc-dp-expired"
+ lb_arn, tg_arn, l_arn = _alb_with_oidc(elbv2, lam, name)
+ try:
+ from ministack.services import alb
+
+ session = {"sub": "user-42", "exp": int(time.time()) - 1}
+ cookies = alb._oidc_session_cookies("AWSELBAuthSessionCookie", session, 600, False)
+ jar = "; ".join(c.split(";", 1)[0] for c in cookies)
+
+ status, headers, _ = _oidc_get("/private", name, {"Cookie": jar})
+ assert status == 302
+ assert "idp.example.com" in headers["Location"]
+ # The dead shards are cleared so they cannot be replayed.
+ assert "Set-Cookie" in headers
+ finally:
+ _alb_teardown(elbv2, lam, lb_arn, tg_arn, l_arn, f"{name}-fn")
+
+
+def test_a_callback_without_a_matching_request_is_refused(elbv2, lam):
+ """A replayed or forged callback has no pending state to match."""
+ name = "oidc-dp-badcb"
+ lb_arn, tg_arn, l_arn = _alb_with_oidc(elbv2, lam, name)
+ try:
+ status, _, _ = _oidc_get("/oauth2/idpresponse?code=abc&state=never-issued", name)
+ assert status == 401
+ status, _, _ = _oidc_get("/oauth2/idpresponse", name)
+ assert status == 401
+ finally:
+ _alb_teardown(elbv2, lam, lb_arn, tg_arn, l_arn, f"{name}-fn")
+
+
+def test_a_custom_session_cookie_name_is_honoured(elbv2, lam):
+ name = "oidc-dp-cookiename"
+ lb_arn, tg_arn, l_arn = _alb_with_oidc(elbv2, lam, name, SessionCookieName="MySession")
+ try:
+ from ministack.services import alb
+
+ session = {"claims": {"sub": "user-9"}, "exp": int(time.time()) + 600}
+ cookies = alb._oidc_session_cookies("MySession", session, 600, False)
+ jar = "; ".join(c.split(";", 1)[0] for c in cookies)
+
+ status, _, body = _oidc_get("/private", name, {"Cookie": jar})
+ assert status == 200
+ assert json.loads(body)["identity"] == "user-9"
+
+ # The default name must not be accepted when another was configured.
+ wrong = alb._oidc_session_cookies("AWSELBAuthSessionCookie", session, 600, False)
+ wrong_jar = "; ".join(c.split(";", 1)[0] for c in wrong)
+ status, _, _ = _oidc_get("/private", name, {"Cookie": wrong_jar})
+ assert status == 302
+ finally:
+ _alb_teardown(elbv2, lam, lb_arn, tg_arn, l_arn, f"{name}-fn")
+
+
+def test_authenticate_cognito_is_reported_as_unimplemented(elbv2, lam):
+ """Better a loud 501 than forwarding a request nobody authenticated."""
+ name = "oidc-dp-cognito"
+ fn_code = ("def handler(event, context):\n"
+ " return {'statusCode': 200, 'body': 'reached'}\n")
+ lb_arn, tg_arn, l_arn, _ = _alb_setup(elbv2, lam, name, f"{name}-fn", fn_code)
+ try:
+ elbv2.modify_listener(
+ ListenerArn=l_arn,
+ DefaultActions=[
+ {"Type": "authenticate-cognito", "Order": 1},
+ {"Type": "forward", "Order": 2, "TargetGroupArn": tg_arn},
+ ],
+ )
+ status, _, body = _oidc_get("/private", name)
+ assert status == 501
+ assert b"not implemented" in body
+ finally:
+ _alb_teardown(elbv2, lam, lb_arn, tg_arn, l_arn, f"{name}-fn")
+
+
+def test_modify_listener_updates_the_serving_default_rule(elbv2, lam):
+ """ModifyListener must change what the data plane actually serves.
+
+ CreateListener copies the default actions into an auto-created default
+ rule, and dispatch reads that rule. A ModifyListener that updates only the
+ listener record reports success while traffic keeps hitting the old
+ actions — silently, which is the worst way for it to be wrong.
+ """
+ name = "oidc-modify"
+ fn_code = ("def handler(event, context):\n"
+ " return {'statusCode': 200, 'body': 'target'}\n")
+ lb_arn, tg_arn, l_arn, _ = _alb_setup(elbv2, lam, name, f"{name}-fn", fn_code)
+ try:
+ status, _, body = _oidc_get("/anything", name)
+ assert status == 200 and body == b"target"
+
+ elbv2.modify_listener(ListenerArn=l_arn, DefaultActions=[{
+ "Type": "fixed-response",
+ "FixedResponseConfig": {"StatusCode": "418", "ContentType": "text/plain",
+ "MessageBody": "changed"},
+ }])
+
+ status, _, body = _oidc_get("/anything", name)
+ assert status == 418, "ModifyListener did not reach the data plane"
+ assert body == b"changed"
+ finally:
+ _alb_teardown(elbv2, lam, lb_arn, tg_arn, l_arn, f"{name}-fn")
+
+
+def test_client_supplied_identity_headers_are_stripped_in_allow_mode(elbv2, lam):
+ """AWS owns the X-Amzn-Oidc-* headers: a client cannot forge an identity by
+ sending them directly, even when OnUnauthenticatedRequest=allow forwards
+ the request without authentication."""
+ name = "oidc-dp-forge"
+ lb_arn, tg_arn, l_arn = _alb_with_oidc(elbv2, lam, name, OnUnauthenticatedRequest="allow")
+ try:
+ status, _, body = _oidc_get("/public", name, {
+ "x-amzn-oidc-identity": "forged-admin",
+ "x-amzn-oidc-data": "forged.jwt.blob",
+ })
+ assert status == 200
+ payload = json.loads(body)
+ assert payload["identity"] is None
+ assert payload["data"] is False
+ finally:
+ _alb_teardown(elbv2, lam, lb_arn, tg_arn, l_arn, f"{name}-fn")
+
+
+def test_oidc_data_is_an_alb_format_claims_jwt(elbv2, lam):
+ """x-amzn-oidc-data carries the user claims in AWS's JWT shape — kid,
+ signer (the load balancer ARN), iss, client and exp in the header — never
+ the provider's ID token, which AWS does not pass to the backend."""
+ import base64
+
+ name = "oidc-dp-jwt"
+ fn_code = (
+ "import json\n"
+ "def handler(event, context):\n"
+ " h = {k.lower(): v for k, v in (event.get('headers') or {}).items()}\n"
+ " return {'statusCode': 200, 'headers': {'Content-Type': 'application/json'},\n"
+ " 'body': json.dumps({'data': h.get('x-amzn-oidc-data')})}\n"
+ )
+ lb_arn, tg_arn, l_arn, _ = _alb_setup(elbv2, lam, name, f"{name}-fn", fn_code)
+ try:
+ elbv2.modify_listener(
+ ListenerArn=l_arn,
+ DefaultActions=[
+ _oidc_action(order=1),
+ {"Type": "forward", "Order": 2, "TargetGroupArn": tg_arn},
+ ],
+ )
+ from ministack.services import alb
+
+ session = {"claims": {"sub": "user-77", "email": "[email protected]"},
+ "access_token": "at", "exp": int(time.time()) + 600}
+ cookies = alb._oidc_session_cookies("AWSELBAuthSessionCookie", session, 600, False)
+ jar = "; ".join(c.split(";", 1)[0] for c in cookies)
+
+ status, _, body = _oidc_get("/private", name, {"Cookie": jar})
+ assert status == 200
+ token = json.loads(body)["data"]
+ head, payload, sig = token.split(".")
+ # ALB's documented deviation from standard JWT: every segment keeps
+ # its base64 padding. Strict JWT parsers fail on real ALB tokens
+ # because of it, so the emulator must reproduce it.
+ for segment in (head, payload, sig):
+ assert len(segment) % 4 == 0
+ header = json.loads(base64.urlsafe_b64decode(head))
+ claims = json.loads(base64.urlsafe_b64decode(payload))
+ assert header["alg"] == "ES256"
+ assert header["signer"] == lb_arn
+ assert header["kid"]
+ assert header["iss"] == "https://idp.example.com"
+ assert header["client"] == "client-abc"
+ assert claims == {"sub": "user-77", "email": "[email protected]"}
+ finally:
+ _alb_teardown(elbv2, lam, lb_arn, tg_arn, l_arn, f"{name}-fn")
+
+
+# ------------------------------------------------------- the fixed behaviours
+
+def _run_auth(action_cfg, path, query=None, cookie=None):
+ """Drive _authenticate_oidc in-process, as dispatch would."""
+ import asyncio
+
+ from ministack.services import alb
+
+ headers = {"host": "lb.alb.localhost:4566"}
+ if cookie:
+ headers["cookie"] = cookie
+ action = {"Type": "authenticate-oidc", "AuthenticateOidcConfig": action_cfg}
+ return asyncio.run(alb._authenticate_oidc(
+ action, "GET", path, headers, b"", query or {}, 80,
+ lb_arn="arn:aws:elasticloadbalancing:us-east-1:000000000000:loadbalancer/app/lb/abc",
+ )), headers
+
+
+def test_callback_claims_come_from_the_userinfo_endpoint(monkeypatch):
+ """The session stores the UserInfo claims, as AWS documents — the ID token
+ (whose own claims differ here on purpose) is never stored or forwarded."""
+ from ministack.services import alb
+
+ id_payload = json.dumps({"sub": "from-id-token"}).encode()
+ import base64 as _b64
+ id_token = "h." + _b64.urlsafe_b64encode(id_payload).decode().rstrip("=") + ".s"
+ monkeypatch.setattr(alb, "_oidc_post_form", lambda url, form, timeout=10.0: (
+ 200, {"access_token": "at-1", "id_token": id_token}))
+ monkeypatch.setattr(alb, "_oidc_get_userinfo", lambda url, token, timeout=10.0: (
+ {"sub": "from-userinfo", "email": "[email protected]"}))
+
+ alb._oidc_pending["st-1"] = {"url": "http://lb/orig", "redirect_uri": "http://lb/oauth2/idpresponse",
+ "created": time.time()}
+ (status, out_headers, _), _ = _run_auth(
+ {"TokenEndpoint": "https://idp/t", "UserInfoEndpoint": "https://idp/u",
+ "ClientId": "c", "ClientSecret": "s"},
+ "/oauth2/idpresponse", {"code": ["abc"], "state": ["st-1"]})
+ assert status == 302
+
+ jar = {}
+ for cookie in out_headers["Set-Cookie"]:
+ name, _, rest = cookie.partition("=")
+ jar[name] = rest.split(";", 1)[0]
+ session = alb._oidc_read_session(jar, "AWSELBAuthSessionCookie")
+ assert session["claims"] == {"sub": "from-userinfo", "email": "[email protected]"}
+ assert "id_token" not in session, "AWS never passes the ID token to the backend"
+
+
+def test_callback_falls_back_to_id_token_claims_when_userinfo_fails(monkeypatch):
+ import base64 as _b64
+
+ from ministack.services import alb
+ payload = _b64.urlsafe_b64encode(json.dumps({"sub": "fallback-sub"}).encode()).decode().rstrip("=")
+ monkeypatch.setattr(alb, "_oidc_post_form", lambda url, form, timeout=10.0: (
+ 200, {"access_token": "at-2", "id_token": f"h.{payload}.s"}))
+ monkeypatch.setattr(alb, "_oidc_get_userinfo", lambda url, token, timeout=10.0: None)
+
+ alb._oidc_pending["st-2"] = {"url": "http://lb/orig", "redirect_uri": "http://lb/oauth2/idpresponse",
+ "created": time.time()}
+ (status, out_headers, _), _ = _run_auth(
+ {"TokenEndpoint": "https://idp/t", "UserInfoEndpoint": "https://idp/u", "ClientId": "c"},
+ "/oauth2/idpresponse", {"code": ["abc"], "state": ["st-2"]})
+ assert status == 302
+ jar = {c.partition("=")[0]: c.partition("=")[2].split(";", 1)[0]
+ for c in out_headers["Set-Cookie"]}
+ assert alb._oidc_read_session(jar, "AWSELBAuthSessionCookie")["claims"]["sub"] == "fallback-sub"
+
+
+def test_abandoned_login_attempts_are_pruned_after_the_15_minute_bound():
+ from ministack.services import alb
+
+ alb._oidc_pending["stale-1"] = {"url": "u", "redirect_uri": "r", "created": time.time() - 901}
+ alb._oidc_pending["fresh-1"] = {"url": "u", "redirect_uri": "r", "created": time.time()}
+ (status, _, _), _ = _run_auth(
+ {"AuthorizationEndpoint": "https://idp/a", "ClientId": "c"}, "/private")
+ assert status == 302
+ assert "stale-1" not in alb._oidc_pending, "abandoned attempt survived the prune"
+ assert "fresh-1" in alb._oidc_pending, "a live attempt must not be pruned"
diff --git a/tests/test_dynamodb.py b/tests/test_dynamodb.py
index bf1e920c..d8a92966 100644
--- a/tests/test_dynamodb.py
+++ b/tests/test_dynamodb.py
@@ -10,7 +10,6 @@ import pytest
from botocore.config import Config
from botocore.exceptions import ClientError
-
_ENDPOINT = os.environ.get("MINISTACK_ENDPOINT", "http://localhost:4566").rstrip("/")
@@ -4036,7 +4035,8 @@ def _raw_ddb(target: str, body: dict):
"""Direct HTTP DDB call — bypasses boto3's client-side validation so we
can verify the server's own length checks (which is what the conformance
suite hits)."""
- import urllib.request, urllib.error
+ import urllib.error
+ import urllib.request
req = urllib.request.Request(
f"{_ENDPOINT}/",
data=json.dumps(body).encode("utf-8"),
@@ -7071,3 +7071,17 @@ def test_update_allows_value_reference_named_after_key(ddb, key_guard_table):
)
assert resp["Attributes"]["label"] == {"S": "v"}
assert resp["Attributes"]["tally"] == {"N": "3"}
+
+
+def test_unicode_dynamodb_item(ddb):
+ table = "unicode-ddb"
+ ddb.create_table(
+ TableName=table,
+ KeySchema=[{"AttributeName": "pk", "KeyType": "HASH"}],
+ AttributeDefinitions=[{"AttributeName": "pk", "AttributeType": "S"}],
+ BillingMode="PAY_PER_REQUEST",
+ )
+ item = {"pk": {"S": "ключ"}, "value": {"S": "значение 日本語 مرحبا"}}
+ ddb.put_item(TableName=table, Item=item)
+ resp = ddb.get_item(TableName=table, Key={"pk": {"S": "ключ"}})
+ assert resp["Item"]["value"]["S"] == "значение 日本語 مرحبا"
diff --git a/tests/test_rds.py b/tests/test_rds.py
index f01ff02e..8cfc7f42 100644
--- a/tests/test_rds.py
+++ b/tests/test_rds.py
@@ -13714,3 +13714,21 @@ def test_rds_network_aliases_only_on_user_defined_networks():
assert not rds_service._network_supports_aliases(network), network
for network in ("ministack_default", "my-compose_default", "anything-else"):
assert rds_service._network_supports_aliases(network), network
+
+
+def test_rds_container_worker_does_not_clobber_an_intervened_stop(monkeypatch):
+ """StopDBInstance while the create worker is still starting the container
+ must stay stopped: the finishing worker used to write "available"
+ unconditionally, so a proxy target went back to AVAILABLE and pollers saw
+ the instance running again."""
+ from ministack.services import rds as m
+
+ monkeypatch.setattr(m, "_get_docker", lambda: None)
+ instance = {"DBInstanceIdentifier": "stopped-mid-create",
+ "DBInstanceStatus": "stopped", "Engine": "postgres"}
+ m._start_rds_container_for_instance("stopped-mid-create", instance)
+ assert instance["DBInstanceStatus"] == "stopped"
+
+ instance["DBInstanceStatus"] = "creating"
+ m._start_rds_container_for_instance("stopped-mid-create", instance)
+ assert instance["DBInstanceStatus"] == "available"
diff --git a/tests/test_route53.py b/tests/test_route53.py
index 52a3ef1c..11057c7b 100644
--- a/tests/test_route53.py
+++ b/tests/test_route53.py
@@ -675,3 +675,14 @@ def test_route53_create_record_duplicate_fails(r53):
)
assert exc.value.response["Error"]["Code"] == "InvalidChangeBatch"
+
+
+def test_unicode_route53_zone_comment(r53):
+ resp = r53.create_hosted_zone(
+ Name="unicode-zone.com",
+ CallerReference="ref-uc-1",
+ HostedZoneConfig={"Comment": "zona en español — Ünïcödé"},
+ )
+ zone_id = resp["HostedZone"]["Id"].split("/")[-1]
+ get = r53.get_hosted_zone(Id=zone_id)
+ assert get["HostedZone"]["Config"]["Comment"] == "zona en español — Ünïcödé"
diff --git a/tests/test_s3.py b/tests/test_s3.py
index 4e769f1f..c3da628f 100644
--- a/tests/test_s3.py
+++ b/tests/test_s3.py
@@ -5067,8 +5067,9 @@ def test_s3_post_object_field_with_filename_is_not_body(s3):
"""A form field carrying a filename (HTTP libraries set one on every field)
must NOT be treated as the object body — only the field named `file` is.
Regression for #1322 defect 1."""
- import requests
from collections import OrderedDict
+
+ import requests
bucket = "intg-s3-post-filename-field"
s3.create_bucket(Bucket=bucket)
# `key` is an ordinary form field that carries a filename; `file` is the body.
@@ -6034,7 +6035,7 @@ def test_s3_replication_copies_object_and_stamps_status(s3):
def test_s3_replication_honors_prefix_and_failure(s3):
"""A key outside the rule's Prefix carries no status; a destination that
stops being usable stamps the source FAILED."""
- src, dst = f"qa-repl-src-pref", f"qa-repl-dst-pref"
+ src, dst = "qa-repl-src-pref", "qa-repl-dst-pref"
for b in (src, dst):
s3.create_bucket(Bucket=b)
s3.put_bucket_versioning(Bucket=b, VersioningConfiguration={"Status": "Enabled"})
@@ -6120,3 +6121,28 @@ def test_s3_control_list_tags_uses_the_tag_element_aws_uses(s3):
assert "<Tag><Key>team</Key><Value>platform</Value></Tag>" in body, body
assert "<member>" not in body, body
+
+
+def test_unicode_s3_object_key(s3):
+ s3.create_bucket(Bucket="unicode-keys")
+ key = "données/résumé/文件.txt"
+ body = "Ünïcödé cöntënt 日本語".encode("utf-8")
+ s3.put_object(Bucket="unicode-keys", Key=key, Body=body)
+ resp = s3.get_object(Bucket="unicode-keys", Key=key)
+ assert resp["Body"].read() == body
+
+
+def test_unicode_s3_metadata(s3):
+ # S3 metadata values must be ASCII per AWS/botocore; encode non-ASCII with percent-encoding
+ from urllib.parse import quote, unquote
+
+ s3.create_bucket(Bucket="unicode-meta")
+ s3.put_object(
+ Bucket="unicode-meta",
+ Key="file.bin",
+ Body=b"data",
+ Metadata={"filename": quote("résumé.pdf"), "author": quote("Ñoño")},
+ )
+ head = s3.head_object(Bucket="unicode-meta", Key="file.bin")
+ assert unquote(head["Metadata"]["filename"]) == "résumé.pdf"
+ assert unquote(head["Metadata"]["author"]) == "Ñoño"
diff --git a/tests/test_secretsmanager.py b/tests/test_secretsmanager.py
index ea60df53..209adb39 100644
--- a/tests/test_secretsmanager.py
+++ b/tests/test_secretsmanager.py
@@ -932,3 +932,9 @@ def test_secret_label_updated_event_to_eventbridge(sm, eb, sqs):
# Metadata-only update must not have emitted an extra event beyond the
# AWSCURRENT moves (create + put); AWSPENDING/AWSPREVIOUS never emit.
assert all(e["detail"]["labelUpdated"] == "AWSCURRENT" for e in label_events)
+
+
+def test_unicode_secretsmanager(sm):
+ sm.create_secret(Name="unicode-secret", SecretString="пароль: 密码")
+ resp = sm.get_secret_value(SecretId="unicode-secret")
+ assert resp["SecretString"] == "пароль: 密码"
diff --git a/tests/test_sqs.py b/tests/test_sqs.py
index bc11de44..a506e704 100644
--- a/tests/test_sqs.py
+++ b/tests/test_sqs.py
@@ -1522,3 +1522,11 @@ def test_sqs_invalid_chars_regex_matches_xml10_complement():
for c in ("\t", "\n", "\r", "퟿", "", "�",
"a", "こ", "\U0001f600"):
assert not rx.search(c), f"must allow {c!r}"
+
+
+def test_unicode_sqs_message(sqs):
+ url = sqs.create_queue(QueueName="unicode-sqs")["QueueUrl"]
+ msg = "こんにちは世界 héllo wörld"
+ sqs.send_message(QueueUrl=url, MessageBody=msg)
+ resp = sqs.receive_message(QueueUrl=url, MaxNumberOfMessages=1)
+ assert resp["Messages"][0]["Body"] == msg
diff --git a/tests/test_ssm.py b/tests/test_ssm.py
index 36d1a6fb..3b7d33e7 100644
--- a/tests/test_ssm.py
+++ b/tests/test_ssm.py
@@ -962,3 +962,9 @@ def test_ssm_command_lookup_filters_and_errors(ssm, ec2):
finally:
ec2.terminate_instances(InstanceIds=[instance_id, other_id])
ec2.deregister_image(ImageId=ami)
+
+
+def test_unicode_ssm_parameter(ssm):
+ ssm.put_parameter(Name="/unicode/param", Value="값: τιμή", Type="String")
+ resp = ssm.get_parameter(Name="/unicode/param")
+ assert resp["Parameter"]["Value"] == "값: τιμή"
diff --git a/tests/test_unicode.py b/tests/test_unicode.py
deleted file mode 100644
index 4319d1e3..00000000
--- a/tests/test_unicode.py
+++ /dev/null
@@ -1,65 +0,0 @@
-
-
-
-def test_unicode_s3_object_key(s3):
- s3.create_bucket(Bucket="unicode-keys")
- key = "données/résumé/文件.txt"
- body = "Ünïcödé cöntënt 日本語".encode("utf-8")
- s3.put_object(Bucket="unicode-keys", Key=key, Body=body)
- resp = s3.get_object(Bucket="unicode-keys", Key=key)
- assert resp["Body"].read() == body
-
-def test_unicode_s3_metadata(s3):
- # S3 metadata values must be ASCII per AWS/botocore; encode non-ASCII with percent-encoding
- from urllib.parse import quote, unquote
-
- s3.create_bucket(Bucket="unicode-meta")
- s3.put_object(
- Bucket="unicode-meta",
- Key="file.bin",
- Body=b"data",
- Metadata={"filename": quote("résumé.pdf"), "author": quote("Ñoño")},
- )
- head = s3.head_object(Bucket="unicode-meta", Key="file.bin")
- assert unquote(head["Metadata"]["filename"]) == "résumé.pdf"
- assert unquote(head["Metadata"]["author"]) == "Ñoño"
-
-def test_unicode_dynamodb_item(ddb):
- table = "unicode-ddb"
- ddb.create_table(
- TableName=table,
- KeySchema=[{"AttributeName": "pk", "KeyType": "HASH"}],
- AttributeDefinitions=[{"AttributeName": "pk", "AttributeType": "S"}],
- BillingMode="PAY_PER_REQUEST",
- )
- item = {"pk": {"S": "ключ"}, "value": {"S": "значение 日本語 مرحبا"}}
- ddb.put_item(TableName=table, Item=item)
- resp = ddb.get_item(TableName=table, Key={"pk": {"S": "ключ"}})
- assert resp["Item"]["value"]["S"] == "значение 日本語 مرحبا"
-
-def test_unicode_sqs_message(sqs):
- url = sqs.create_queue(QueueName="unicode-sqs")["QueueUrl"]
- msg = "こんにちは世界 héllo wörld"
- sqs.send_message(QueueUrl=url, MessageBody=msg)
- resp = sqs.receive_message(QueueUrl=url, MaxNumberOfMessages=1)
- assert resp["Messages"][0]["Body"] == msg
-
-def test_unicode_secretsmanager(sm):
- sm.create_secret(Name="unicode-secret", SecretString="пароль: 密码")
- resp = sm.get_secret_value(SecretId="unicode-secret")
- assert resp["SecretString"] == "пароль: 密码"
-
-def test_unicode_ssm_parameter(ssm):
- ssm.put_parameter(Name="/unicode/param", Value="값: τιμή", Type="String")
- resp = ssm.get_parameter(Name="/unicode/param")
- assert resp["Parameter"]["Value"] == "값: τιμή"
-
-def test_unicode_route53_zone_comment(r53):
- resp = r53.create_hosted_zone(
- Name="unicode-zone.com",
- CallerReference="ref-uc-1",
- HostedZoneConfig={"Comment": "zona en español — Ünïcödé"},
- )
- zone_id = resp["HostedZone"]["Id"].split("/")[-1]
- get = r53.get_hosted_zone(Id=zone_id)
- assert get["HostedZone"]["Config"]["Comment"] == "zona en español — Ünïcödé"