Ministack: Free, open-source local AWS emulator - 60+ services, Terraform compatible, real databases. Free forever. MIT licensed.
Python awsaws-emulatoraws-localaws-sdkdevtoolsdockerdynamodbec2emulatorlambdalocalstacklocalstack-alternativeministackmock-awsopen-sourcepythons3sqsterraform
fix(cfn) clientSecret (#1708)
Nahuel Nuceraf0fa8406dfdf
diff --git a/ministack/services/cloudformation/provisioners.py b/ministack/services/cloudformation/provisioners.pyindex 97db7ecd..3343ddf1 100644--- a/ministack/services/cloudformation/provisioners.py+++ b/ministack/services/cloudformation/provisioners.py@@ -5714,8 +5714,23 @@ def _cognito_user_pool_client_create(logical_id, props, stack_name):status, _, body = _cognito._create_user_pool_client(payload)if status >= 400:raise ValueError(f"AWS::Cognito::UserPoolClient create failed: {body!r}")- cid = json.loads(body)["UserPoolClient"]["ClientId"]- return cid, {"ClientId": cid}+ client = json.loads(body)["UserPoolClient"]+ return client["ClientId"], _cognito_user_pool_client_attributes(client)+++def _cognito_user_pool_client_attributes(client):+ """The attributes the type reports: ClientId, ClientSecret and Name.+ ClientSecret is a documented Fn::GetAtt attribute of the type and a+ template that reads it (Serverless Framework emits one) failed the stack+ with "does not exist in schema" while the value was right there on the+ record. A client created without GenerateSecret has none, and the+ attribute reads empty rather than failing the template.+ """+ return {+ "ClientId": client.get("ClientId", ""),+ "ClientSecret": client.get("ClientSecret") or "",+ "Name": client.get("ClientName", ""),+ }def _cognito_user_pool_client_update(physical_id, old_props, new_props, stack_name,@@ -5747,7 +5762,9 @@ def _cognito_user_pool_client_update(physical_id, old_props, new_props, stack_nastatus, _, body = _cognito._update_user_pool_client(payload)if status >= 400:raise ValueError(f"AWS::Cognito::UserPoolClient update failed: {body!r}")- return physical_id, {"ClientId": physical_id}+ pool = _cognito._user_pools.get(new_props.get("UserPoolId", "")) or {}+ client = (pool.get("_clients") or {}).get(physical_id) or {"ClientId": physical_id}+ return physical_id, _cognito_user_pool_client_attributes(client)def _cognito_user_pool_client_delete(physical_id, props):diff --git a/tests/test_cfn.py b/tests/test_cfn.pyindex fee10ab5..0d095b73 100644--- a/tests/test_cfn.py+++ b/tests/test_cfn.py@@ -8417,6 +8417,70 @@ Outputs:assert "rgn-test-us-east-2" in buckets+def test_cfn_cognito_user_pool_client_getatt_client_secret(cfn, cognito_idp):+ """The type's Fn::GetAtt attributes are ClientId, ClientSecret and Name.+ Only ClientId was returned, so a template reading the secret (Serverless+ Framework emits one) failed with "Requested attribute ClientSecret does+ not exist in schema" and rolled the whole stack back, on create and on+ the update that follows it. Reported by @JoshuaSmeda."""+ template = """+AWSTemplateFormatVersion: '2010-09-09'+Resources:+ Pool:+ Type: AWS::Cognito::UserPool+ Properties:+ UserPoolName: cfn-upc-getatt-pool+ Client:+ Type: AWS::Cognito::UserPoolClient+ Properties:+ UserPoolId: !Ref Pool+ ClientName: %s+ GenerateSecret: true+ Plain:+ Type: AWS::Cognito::UserPoolClient+ Properties:+ UserPoolId: !Ref Pool+ ClientName: plain+Outputs:+ PoolId:+ Value: !Ref Pool+ ClientId:+ Value: !GetAtt Client.ClientId+ ClientSecret:+ Value: !GetAtt Client.ClientSecret+ ClientName:+ Value: !GetAtt Client.Name+ PlainSecret:+ Value: !GetAtt Plain.ClientSecret+"""+ stack_name = f"cfn-upc-getatt-{_uuid_mod.uuid4().hex[:8]}"+ try:+ cfn.create_stack(StackName=stack_name, TemplateBody=template % "getatt-demo")+ stack = _wait_stack(cfn, stack_name)+ assert stack["StackStatus"] == "CREATE_COMPLETE", stack.get("StackStatusReason")+ outputs = {o["OutputKey"]: o["OutputValue"] for o in stack.get("Outputs", [])}++ described = cognito_idp.describe_user_pool_client(+ UserPoolId=outputs["PoolId"], ClientId=outputs["ClientId"],+ )["UserPoolClient"]+ assert outputs["ClientSecret"] == described["ClientSecret"]+ assert outputs["ClientName"] == "getatt-demo"+ # A client created without GenerateSecret has no secret; the+ # attribute reads empty rather than failing the template.+ assert outputs["PlainSecret"] == ""++ # Serverless Framework always follows the create with an update, and+ # that path resolved the attributes too.+ cfn.update_stack(StackName=stack_name, TemplateBody=template % "getatt-demo-2")+ stack = _wait_stack(cfn, stack_name)+ assert stack["StackStatus"] == "UPDATE_COMPLETE", stack.get("StackStatusReason")+ outputs = {o["OutputKey"]: o["OutputValue"] for o in stack.get("Outputs", [])}+ assert outputs["ClientSecret"] == described["ClientSecret"]+ assert outputs["ClientName"] == "getatt-demo-2"+ finally:+ _delete_cfn_test_stack(cfn, stack_name)++def test_cfn_cognito_user_pool_client_generate_secret(cfn, cognito_idp):"""CFN AWS::Cognito::UserPoolClient with GenerateSecret=true creates aClientSecret; GenerateSecret=false/absent leaves it None (#403)."""