Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature Request: Update some outdated Cognito user pool event data classes #3987

Open
knightmre opened this issue Mar 20, 2024 · 2 comments
Open
Labels
event_sources Event Source Data Class utility feature-request feature request

Comments

@knightmre
Copy link

knightmre commented Mar 20, 2024

Expected Behavior

Accessing code_parameter property of cognito_user_pool_event.CustomMessageTriggerEvent returns the proper encrypted code.

Current Behaviour

Mapping the CustomMessageTriggerEvent data class of cognito_user_pool_event leads to a KeyError because the received event property names have been changed by AWS.
The new request and documentation can be found at : https://docs.aws.amazon.com/cognito/latest/developerguide/user-pool-lambda-custom-sms-sender.html#custom-sms-sender-parameters

Code snippet

from aws_lambda_powertools.utilities import data_classes
from aws_lambda_powertools.utilities.data_classes import cognito_user_pool_event

@data_classes.event_source(data_class=cognito_user_pool_event.CustomMessageTriggerEvent)
def handler(event, context):
    code = event.request.code_parameter

Possible Solution

Update data class source for cognito_user_pool_event to reflect the updated event. Currently it fetches event['request']['code_parameter'] but it should fetch event['request']['code']

If that's OK I could contribute, I'd be glad to go through all the source events for Cognito as I see it's been quite a while since they have been added to the project. I could go through the current AWS docs and test it with existing Cognito Pool event behavior for all the events as we rely heavily on those in our current project.

For now I've recreated the event class in our local code, deriving BaseTriggerEvent.

Steps to Reproduce

This is the event trigger that is sent by Cognito that can be used as an input to the lambda handler.

{
	"version": "1",
	"triggerSource": "CustomSMSSender_ResendCode",
	"region": "<region>",
	"userPoolId": "<pool_id>",
	"userName": "<user_name>",
	"callerContext": {
		"awsSdkVersion": "aws-sdk-unknown-unknown",
		"clientId": "<client_id>"
	},
	"request": {
		"type": "customEmailSenderRequestV1",
		"code": "<encrypted_code>",
		"clientMetadata": {},
		"userAttributes": {
			<usual_attributes>
		}
	}
}

Powertools for AWS Lambda (Python) version

latest

AWS Lambda function runtime

3.11

Packaging format used

PyPi

Debugging logs

[ERROR] KeyError: 'codeParameter'
Traceback (most recent call last):
  File "/opt/python/aws_lambda_powertools/logging/logger.py", line 447, in decorate
    return lambda_handler(event, context, *args, **kwargs)
  File "/opt/python/aws_lambda_powertools/middleware_factory/factory.py", line 135, in wrapper
    response = middleware()
  File "/opt/python/aws_lambda_powertools/utilities/data_classes/event_source.py", line 39, in event_source
    return handler(data_class(event), context)
  File "/var/task/custom_sms_trigger/index.py", line 39, in handler
    return handle_sms_event(event=event)
  File "/var/task/custom_sms_trigger/index.py", line 25, in handle_sms_event
    if not event.request.code_parameter:
  File "/opt/python/aws_lambda_powertools/utilities/data_classes/cognito_user_pool_event.py", line 271, in code_parameter
    return self["request"]["codeParameter"]
@knightmre knightmre added bug Something isn't working triage Pending triage from maintainers labels Mar 20, 2024
Copy link

boring-cyborg bot commented Mar 20, 2024

Thanks for opening your first issue here! We'll come back to you as soon as we can.
In the meantime, check out the #python channel on our Powertools for AWS Lambda Discord: Invite link

@leandrodamascena
Copy link
Contributor

Hey @knightmre! Thanks for raising this issue. You're correct that the field code should be accessed through event.request. However, I believe we should leave code_parameter unchanged as it's still relevant when using the Custom Message Lambda trigger, and we need to create a new one to access code.

If that's OK I could contribute, I'd be glad to go through all the source events for Cognito as I see it's been quite a while since they have been added to the project. I could go through the current AWS docs and test it with existing Cognito Pool event behavior for all the events as we rely heavily on those in our current project.

This is truly a significant improvement! Your contribution in identifying any discrepancies between our current implementation and the actual schema used by Cognito during Lambda invocation would be immensely helpful. Please do that!
I kindly request that no fields be removed, as doing so could result in breaking changes. If you notice any drift in a field, please create a new one with the updated name, and we can review it during the PR process.

I've reclassified this issue as a Feature Request. Please feel free to reach out if you have any obstacles during the contribution process.

@leandrodamascena leandrodamascena added feature-request feature request event_sources Event Source Data Class utility and removed bug Something isn't working triage Pending triage from maintainers labels Mar 20, 2024
@leandrodamascena leandrodamascena changed the title Bug: Some Cognito User Pool Event Data Classes are out of date Feature Request: Update some outdated Cognito user pool event data classes Mar 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
event_sources Event Source Data Class utility feature-request feature request
Projects
Status: Pending customer
Development

No branches or pull requests

2 participants