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

fix(custom-resources): empty Lambda response payload causes deployment failure #27000

Merged
merged 8 commits into from
Sep 6, 2023

Conversation

rix0rrr
Copy link
Contributor

@rix0rrr rix0rrr commented Sep 4, 2023

The payload response of a Lambda used to be a string, and could occasionally be "", which we detected and special-case parsed to an empty object. The Payload should never be empty, and will only be that under exceptional circumstances which we haven't been able to pin down yet, but we shouldn't fail in any case.

In SDKv3, the payload response of a Lambda changed to type Uint8Array, but a Uint8Array(0) doesn't check as falsey, so we'd decode it to "" and then the JSON.parse() of that would fail.

First decode, then check the string for emptyness.

Fixes #26429.


By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license

The payload response of a Lambda used to be a `string`, and could
occasionally be `""`, which we detected and special-case parsed to
an empty object.

In SDKv3, the payload response of a Lambda changed to type `Uint8Array`,
but a `Uint8Array(0)` doesn't check as *falsey*, so we'd decode it to
`""` and then the `JSON.parse()` of that would fail.

First decode, then check the string for emptyness.

Fixes #26429.
@rix0rrr rix0rrr requested a review from a team September 4, 2023 13:49
@aws-cdk-automation aws-cdk-automation requested a review from a team September 4, 2023 13:49
@github-actions github-actions bot added bug This issue is a bug. effort/small Small work item – less than a day of effort p1 labels Sep 4, 2023
@mergify mergify bot added the contribution/core This is a PR that came from AWS. label Sep 4, 2023
Copy link
Collaborator

@aws-cdk-automation aws-cdk-automation left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The pull request linter has failed. See the aws-cdk-automation comment below for failure reasons. If you believe this pull request should receive an exemption, please comment and provide a justification.

A comment requesting an exemption should contain the text Exemption Request. Additionally, if clarification is needed add Clarification Request to a comment.

Copy link
Contributor

@iliapolo iliapolo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think a test is warranted...if not an integ that at least a unit test. though i suspect integ would actually be easier to write :)

@mrgrain mrgrain added the pr/do-not-merge This PR should not be merged at this time. label Sep 4, 2023
Copy link
Contributor

@mrgrain mrgrain left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The same issue will be present here, and we probably should align on a single implementation for this.

export function parseJsonPayload(payload: any): any {

@rix0rrr rix0rrr changed the title fix(custom-resources): empty response causes deployment failure fix(custom-resources): empty Lambda response payload causes deployment failure Sep 5, 2023
@rix0rrr
Copy link
Contributor Author

rix0rrr commented Sep 5, 2023

though i suspect integ would actually be easier to write :)

In fact I don't know how to trigger the empty Payload response condition. It seems to be an unusual case ☹️

} catch {
return payload;
throw new Error(`return values from user-handlers must be JSON objects. got: "${text}"`);
Copy link
Contributor

@mrgrain mrgrain Sep 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can a Lambda Handler not return plain text? Or is it always converted to a JSON parsable string, i.e. "foobar"?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks to be always a JSON representation of a JavaScript object.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I.e. a plain text is returned as "Hi I'm a plain string".

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm still concerned we are going to fail on valid values or return unexpected values.
In the assertion handler parseJsonPayload is naively called on a potentially existing payload property of the response object. That's different than the provider framework where we know we are dealing with a lambda response.

In hindsight, maybe we should do the assertions handler on a different PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that the code is making too many assumptions, and might be subtly broken.

I would posit that it is subtly broken already, and that nothing we're doing is making it worse than what it currently is.

But sure, I'll revert!

@rix0rrr rix0rrr added the pr-linter/exempt-integ-test The PR linter will not require integ test changes label Sep 5, 2023
@aws-cdk-automation aws-cdk-automation dismissed their stale review September 5, 2023 09:08

✅ Updated pull request passes all PRLinter validations. Dismissing previous PRLinter review.

@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • CodeBuild project: AutoBuildv2Project1C6BFA3F-wQm2hXv2jqQv
  • Commit ID: cf7524e
  • Result: SUCCEEDED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

@rix0rrr rix0rrr merged commit e260d30 into main Sep 6, 2023
7 checks passed
@rix0rrr rix0rrr deleted the huijbers/payload-parse branch September 6, 2023 11:09
mikewrighton pushed a commit that referenced this pull request Sep 14, 2023
…t failure (#27000)

The payload response of a Lambda used to be a `string`, and could occasionally be `""`, which we detected and special-case parsed to an empty object. The Payload should never be empty, and will only be that under exceptional circumstances which we haven't been able to pin down yet, but we shouldn't fail in any case.

In SDKv3, the payload response of a Lambda changed to type `Uint8Array`, but a `Uint8Array(0)` doesn't check as *falsey*, so we'd decode it to `""` and then the `JSON.parse()` of that would fail.

First decode, then check the string for emptyness.

Fixes #26429.
@analytically
Copy link

This seems to break the release.
screentshot-2023-10-04 at 16 26 58@2x

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug This issue is a bug. contribution/core This is a PR that came from AWS. effort/small Small work item – less than a day of effort p1 pr/do-not-merge This PR should not be merged at this time. pr-linter/exempt-integ-test The PR linter will not require integ test changes
Projects
None yet
5 participants