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

add validation for environment prop #2290

Merged
merged 3 commits into from
Dec 3, 2024
Merged

add validation for environment prop #2290

merged 3 commits into from
Dec 3, 2024

Conversation

rtpascual
Copy link
Contributor

Problem

Deployment can fail with the following error message when function environment keys are invalid:

1 validation error detected: Value at <environment-key> failed to satisfy constraint: Map keys must satisfy constraint: [Member must satisfy regular expression pattern: [a-zA-Z]([a-zA-Z0-9_])+]

Issue number, if available:

Changes

Add validation for defineFunction environment prop.

Corresponding docs PR, if applicable:

Validation

Unit tests.

Checklist

  • If this PR includes a functional change to the runtime behavior of the code, I have added or updated automated test coverage for this change.
  • If this PR requires a change to the Project Architecture README, I have included that update in this PR.
  • If this PR requires a docs update, I have linked to that docs PR above.
  • If this PR modifies E2E tests, makes changes to resource provisioning, or makes SDK calls, I have run the PR checks with the run-e2e label set.

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

Copy link

changeset-bot bot commented Dec 3, 2024

🦋 Changeset detected

Latest commit: f1242d5

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@aws-amplify/backend-function Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@rtpascual rtpascual marked this pull request as ready for review December 3, 2024 00:52
@rtpascual rtpascual requested a review from a team as a code owner December 3, 2024 00:52
sobolk
sobolk previously approved these changes Dec 3, 2024

Object.keys(this.props.environment).forEach((key) => {
// validate using key pattern from https://docs.aws.amazon.com/lambda/latest/api/API_Environment.html
if (!key.match(/^[a-zA-Z]([a-zA-Z0-9_])+$/) || key.length < 2) {
Copy link
Member

Choose a reason for hiding this comment

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

the length validation seems redundant with regex.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good catch, removed the length validation.

sobolk
sobolk previously approved these changes Dec 3, 2024
entry: './test-assets/default-lambda/handler.ts',
name: 'myCoolLambda',
environment: {
['this.is.wrong']: 'testValue',
Copy link
Member

Choose a reason for hiding this comment

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

a suggestion:
it might be worth running this in the loop with couple of wrong values.

for example ['a', 'this.is.wrong', ...]

reason is that. - we removed that length check, but we should still validate that regex catches 1 character violation.

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 have that captured in another unit test. But this got me thinking if we should validate all keys and list them in the error?

Copy link
Member

Choose a reason for hiding this comment

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

But this got me thinking if we should validate all keys and list them in the error?

that's better DX.

vigy02
vigy02 previously approved these changes Dec 3, 2024
Object.keys(this.props.environment).forEach((key) => {
// validate using key pattern from https://docs.aws.amazon.com/lambda/latest/api/API_Environment.html
if (!key.match(/^[a-zA-Z]([a-zA-Z0-9_])+$/)) {
throw new Error(
Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldn't this be AmplifyUserError?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

After comparing with other packages I believe you're right, though this error and others in FunctionFactory are caught by https://github.com/aws-amplify/amplify-backend/blob/main/packages/backend-deployer/src/cdk_error_mapper.ts#L352. I'll update all of them to AmplifyUserError.

@rtpascual rtpascual dismissed stale reviews from vigy02 and sobolk via f1242d5 December 3, 2024 19:32
@rtpascual rtpascual merged commit d227f96 into main Dec 3, 2024
40 checks passed
@rtpascual rtpascual deleted the validate-env-prop branch December 3, 2024 22:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants