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
feat(cognito): add SetUICustomization and GetUICustomization (#2001)
Antonie Popovic30205e274db5
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex da49bc0b..299ba215 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -29,6 +29,7 @@ Versioning follows [Semantic Versioning](https://semver.org/).- **RDS — a MySQL master user started in the background keeps its admin grant** — an instance whose engine image was not cached yet, or one respawned on restore, started on a background path that skipped the master-user grant the inline path gives, so the master user held only its own database and `SET PERSIST` failed for lack of `SYSTEM_VARIABLES_ADMIN`. Both paths now grant it. Contributed by @skialpine.### Added+- **Cognito — `SetUICustomization` and `GetUICustomization`** — store and return the classic hosted UI branding (CSS) for a user pool (`ClientId` `ALL`) or one app client, with the client falling back to the pool default, so SDK calls and Terraform's `aws_cognito_user_pool_ui_customization` work. As on AWS the pool needs a domain first. A logo `ImageFile` is accepted but not stored, as ministack does not serve hosted UI assets.- **OpenSearch Serverless — collections, security and access policies, and a proxied data plane** — `CreateCollection`, `BatchGetCollection`, `ListCollections`, `UpdateCollection`, `DeleteCollection`, `Create`/`Get`/`Update`/`Delete`/`ListSecurityPolicy` (`encryption`, `network`), `Create`/`Get`/`Update`/`Delete`/`ListAccessPolicy` (`data`), `GetPoliciesStats` and `TagResource` / `UntagResource` / `ListTagsForResource`, enough for Terraform's `aws_opensearchserverless_collection`, `_security_policy` and `_access_policy` to apply, plan clean and destroy. A collection needs a matching encryption policy (exact name or `prefix*`, most specific wins) unless `encryptionConfig` is given, and answers `kmsKeyArn: auto` for an AWS-owned key; policy documents are validated, a resource pattern may sit in only one encryption policy, a data policy refuses empty or wildcard principals, and an update must name the current `policyVersion` (`base64("<lastModifiedDate>_<n>")`). Its `collectionEndpoint`, `http://<id>.<region>.aoss.<host>:<port>`, is served through the gateway and requests must be SigV4-signed for `aoss` and carry `x-amz-content-sha256` (403 otherwise); with `OPENSEARCH_DATAPLANE=1` it is backed by its own `opensearchproject/opensearch` container, persisted on a named Docker volume across a restart under `PERSIST_STATE` and removed on `DeleteCollection`, otherwise it answers 503. APIs AOSS does not support answer 404, as `_refresh`, `_cluster/*` and scroll do on AWS, and `TIMESERIES` collections refuse custom document ids and updates. Data access and network policies are stored, not enforced. Checked against the botocore model and the OpenSearch Serverless developer guide and API reference.### Fixeddiff --git a/ministack/services/cognito.py b/ministack/services/cognito.pyindex 9ee51280..4dcd7c1a 100644--- a/ministack/services/cognito.py+++ b/ministack/services/cognito.py@@ -11,7 +11,7 @@ User Pools operations:CreateUserPool, DeleteUserPool, DescribeUserPool, ListUserPools, UpdateUserPool,AddCustomAttributes,CreateUserPoolClient, DeleteUserPoolClient, DescribeUserPoolClient,- ListUserPoolClients, UpdateUserPoolClient,+ ListUserPoolClients, UpdateUserPoolClient, GetUICustomization, SetUICustomization,AdminCreateUser, AdminDeleteUser, AdminGetUser, ListUsers,AdminSetUserPassword, AdminUpdateUserAttributes,AdminInitiateAuth, AdminRespondToAuthChallenge,@@ -455,6 +455,7 @@ _user_pools = AccountRegionScopedDict()# _groups: {group_name -> group_dict},# _identity_providers: {provider_name -> provider_dict},# _resource_servers: {identifier -> resource_server_dict},+# _ui_customizations: {client_id or ALL -> UICustomizationType},# }_pool_domain_map = AccountRegionScopedDict() # domain -> pool_id@@ -2280,6 +2281,8 @@ async def _dispatch_idp(action: str, data: dict):"DescribeUserPoolClient": _describe_user_pool_client,"ListUserPoolClients": _list_user_pool_clients,"UpdateUserPoolClient": _update_user_pool_client,+ "GetUICustomization": _get_ui_customization,+ "SetUICustomization": _set_ui_customization,# Resource Servers"CreateResourceServer": _create_resource_server,"UpdateResourceServer": _update_resource_server,@@ -2736,6 +2739,42 @@ def _update_user_pool_client(data):return json_response({"UserPoolClient": {k: v for k, v in client.items() if v is not None}})+def _ui_customization_target(data):+ pid = data.get("UserPoolId")+ pool, err = _resolve_pool(pid)+ if err:+ return None, None, err+ cid = data.get("ClientId") or "ALL"+ if cid != "ALL" and cid not in pool["_clients"]:+ return None, None, error_response_json("ResourceNotFoundException", f"Client {cid} not found.", 400)+ return pool, cid, None+++def _set_ui_customization(data):+ pool, cid, err = _ui_customization_target(data)+ if err:+ return err+ if not (pool.get("Domain") or pool.get("CustomDomain")):+ return error_response_json(+ "InvalidParameterException", "There has to be an existing domain associated with this user pool", 400)+ # ImageFile is accepted but not stored: ministack does not serve the classic hosted UI assets.+ now = _now_epoch()+ ui = pool.setdefault("_ui_customizations", {})+ ui[cid] = {+ "UserPoolId": pool["Id"], "ClientId": cid, "CSS": data.get("CSS", ""), "CSSVersion": str(int(now * 1000)),+ "CreationDate": ui.get(cid, {}).get("CreationDate", now), "LastModifiedDate": now,+ }+ return json_response({"UICustomization": ui[cid]})+++def _get_ui_customization(data):+ pool, cid, err = _ui_customization_target(data)+ if err:+ return err+ ui = pool.get("_ui_customizations", {})+ return json_response({"UICustomization": ui.get(cid) or ui.get("ALL") or {"UserPoolId": pool["Id"], "ClientId": cid}})++# ---------------------------------------------------------------------------# Resource Servers# ---------------------------------------------------------------------------diff --git a/tests/test_cognito.py b/tests/test_cognito.pyindex 943a0ef5..fbda1837 100644--- a/tests/test_cognito.py+++ b/tests/test_cognito.py@@ -105,6 +105,28 @@ def test_cognito_list_user_pool_clients(cognito_idp):assert "App1" in namesassert "App2" in names+def test_cognito_ui_customization(cognito_idp):+ pid = cognito_idp.create_user_pool(PoolName="UiPool")["UserPool"]["Id"]+ cid = cognito_idp.create_user_pool_client(UserPoolId=pid, ClientName="App")["UserPoolClient"]["ClientId"]+ with pytest.raises(ClientError) as exc:+ cognito_idp.set_ui_customization(UserPoolId=pid, CSS=".banner-customizable {}")+ assert exc.value.response["Error"]["Code"] == "InvalidParameterException"++ cognito_idp.create_user_pool_domain(UserPoolId=pid, Domain=f"ui-{pid.split('_')[1].lower()}")+ pool_ui = cognito_idp.set_ui_customization(UserPoolId=pid, CSS=".banner-customizable {}")["UICustomization"]+ assert pool_ui["ClientId"] == "ALL" and pool_ui["CSS"] == ".banner-customizable {}" and pool_ui["CSSVersion"]+ assert cognito_idp.get_ui_customization(UserPoolId=pid, ClientId=cid)["UICustomization"]["CSS"] == pool_ui["CSS"]++ ui = cognito_idp.set_ui_customization(UserPoolId=pid, ClientId=cid, CSS=".label-customizable {}", ImageFile=b"\x89PNG")+ assert ui["UICustomization"]["ClientId"] == cid+ got = cognito_idp.get_ui_customization(UserPoolId=pid, ClientId=cid)["UICustomization"]+ assert got["CSS"] == ".label-customizable {}"+ assert cognito_idp.get_ui_customization(UserPoolId=pid)["UICustomization"]["CSS"] == pool_ui["CSS"]++ with pytest.raises(ClientError) as exc:+ cognito_idp.set_ui_customization(UserPoolId=pid, ClientId="missing", CSS="")+ assert exc.value.response["Error"]["Code"] == "ResourceNotFoundException"+def test_cognito_create_and_describe_resource_server(cognito_idp):pid = cognito_idp.create_user_pool(PoolName="ResourceServerPool")["UserPool"]["Id"]resp = cognito_idp.create_resource_server(