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): not work fromSdkCalls using sdk v3 format #27268

Conversation

WinterYukky
Copy link
Contributor

@WinterYukky WinterYukky commented Sep 25, 2023

Summary

Add support AWS SDK v3 format to AwsCustomResourcePolicy.fromSdkCalls.

How to fix this?

There are services that cannot be addressed simply by excluding @aws-sdk/client- from the service name like a @aws-sdk/client-emr (in iam service is elasticmapreduce). There is a similar issue with AWS SDK v2, and in AWS SDK v2, IAM service names are resolved from API metadata. However, API Metadata is only compatible with AWS SDK v2, and since it cannot be used with v3, I created a script to register the v3 package name in API Metadata and made it compatible.

Other

There are integration tests for AWS SDK v2 and v3 in packages/@aws-cdk-testing/framework-integ/test/test/aws-custom-resource/integ.aws-customs-custom-resource.ts, but the AWS SDK has already been changed to V3 and both were an AWS SDK v3 test using the v2 format. So I updated my tests for v3 and reused them for testing the v3 format.

Closes #27255.


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

@github-actions github-actions bot added admired-contributor [Pilot] contributed between 13-24 PRs to the CDK bug This issue is a bug. labels Sep 25, 2023
@aws-cdk-automation aws-cdk-automation requested a review from a team September 25, 2023 03:45
@github-actions github-actions bot added the p2 label Sep 25, 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.

@aws-cdk-automation aws-cdk-automation dismissed their stale review September 25, 2023 04:58

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

@aws-cdk-automation aws-cdk-automation added the pr/needs-community-review This PR needs a review from a Trusted Community Member or Core Team Member. label Sep 25, 2023
@WinterYukky WinterYukky changed the title fix(custom-resources): not work fromSdkCalls using sdk v3 fix(custom-resources): not work fromSdkCalls using sdk v3 format Sep 25, 2023
Copy link
Contributor

@rix0rrr rix0rrr left a comment

Choose a reason for hiding this comment

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

Thanks for helping out, but wait a minute.

It seems these changes are predicated on the fact that the user would be specifying the SDKv3 service name. For backwards compatibility, we also need to let people specify the SDKv2 service name (even though the call will be serviced using SDKv3).

So I'd rather see the following transform:

SDKv3 package name -> SDKv2 service name -> IAM permissions name

Or the alternative:

SDKv2 service name -> SDKv3 package name -> IAM permissions name

In any case: a deterministic order of mapping.

As opposed to what it feels to me like we have now:

SDKv2 service name (if using) -> IAM permissions name
SDKv3 package name (if using) -> IAM permissions name

Because the current is harder to test.

@aws-cdk-automation aws-cdk-automation removed the pr/needs-community-review This PR needs a review from a Trusted Community Member or Core Team Member. label Sep 25, 2023
@rix0rrr
Copy link
Contributor

rix0rrr commented Sep 25, 2023

Thanks for the pointers! I will take it from here myself I think. I'm bit bothered by the current state of the code.

@WinterYukky
Copy link
Contributor Author

Thanks your reviewing @rix0rrr !
I think SDKv2 service name -> SDKv3 package name -> IAM permissions name is better than SDKv3 package name -> SDKv2 service name -> IAM permissions name solution. The reason is AWS SDK v2 will be maintenance mode since next year. After maintenance mode maybe AWS SDK v2 doesn't add new service, so it is able to convert v2 to v3, but v3 to v2 will have unconvertable resource.

@WinterYukky
Copy link
Contributor Author

By the way, If we can add @aws-cdk/sdk-v2-to-v3 package to dependencies we will realization it... I could not it :(

@mergify mergify bot dismissed rix0rrr’s stale review September 26, 2023 02:27

Pull request has been modified.

@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

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

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

@aws-cdk-automation aws-cdk-automation added the pr/needs-community-review This PR needs a review from a Trusted Community Member or Core Team Member. label Sep 26, 2023
@rix0rrr
Copy link
Contributor

rix0rrr commented Sep 26, 2023

By the way, If we can add @aws-cdk/sdk-v2-to-v3 package to dependencies we will realization it... I could not it :(

I know. sdk-v2-to-v3 is intended to be bundled into custom resource handlers. It cannot be used as a dependency of CDK construct libraries. It's all good, I'll take care of it.

rix0rrr added a commit that referenced this pull request Sep 27, 2023
In the `AwsCustomResource` and the `Assertions` libraries, we advertise
accepting all of the following service name formats:

* The SDKv3 service name: `api-gateway`
* The full SDKv3 package name: `@aws-sdk/client-api-gateway`
* The SDKv2 constructor name: `APIGateway`
* The SDKv2 constructor name in all lower case: `apigateway`

And the following action name formats:

* The API call name: `GetRestApi`
* The API call name with a lowercase starting letter method name: `getRestApi`
* The SDKv3 command class name: `GetRestApiCommand`

However, the code that was taking care of mapping service names into an
IAM name was not handling all cases correctly. There was also an issue
with some commands that end in the word `"Command"`, like ECS's
`ExecuteCommand`, which according to the rules above should work both
written as `ExecuteCommand` as well as `ExecuteCommandCommand`: we did
not have enough information to know if we saw the string
`ExecuteCommand`, whether we should interpret it as `Execute` or
`ExecuteCommand`.

Also, we were recommending to use the full SDKv3 package name and class
name formats:

```
{
  service: '@aws-sdk/client-api-gateway',
  action: 'GetRestApiCommand',
}
```

Which looks ugly (imo) and leaks too many of the underlying
implementation details.

This PR changes the following:

- Deprecate the `sdk-api-metadata.json` we extracted from SDKv2.
- From SDKv3 models, extract a new `sdk-v3-metadata.json` which contains
  the following information:
  - IAM prefix for every service
  - A list of APIs that end in the word `Command`, so we can
    disambiguate around these.
- From `aws-sdk-codemod`, extract a mapping from SDKv2 service names to
  SDKv3 service names (replacing the copy/pasted code we used to have
  with a build-time extraction).
- Unfortunately, both of these mappings are duplicated: once for the
  construct library, and once for the handlers. I did not want to go
  into deduplicating between these for now.
- At runtime, we now map a potential V2 service name to a V3 service
  name, then look up the V3 metadata to determine the IAM prefix and
  the normalized action name.
- There was a lot of duplication between the `assertions` handler and
  the `AwsCustomResource` handler. Introduce a new `ApiCall` class that
  unifies the behavior between these two call sites.
- Change the recommendation in the README from using SDKv3 names to
  using shorter form names (`api-gateway` and `GetRestApi`).

Fixes #27255, closes #27268, closes #27270.
@mergify mergify bot closed this in #27313 Oct 5, 2023
mergify bot pushed a commit that referenced this pull request Oct 5, 2023
In the `AwsCustomResource` and the `Assertions` libraries, we advertise accepting all of the following service name formats:

* The SDKv3 service name: `api-gateway`
* The full SDKv3 package name: `@aws-sdk/client-api-gateway`
* The SDKv2 constructor name: `APIGateway`
* The SDKv2 constructor name in all lower case: `apigateway`

And the following action name formats:

* The API call name: `GetRestApi`
* The API call name with a lowercase starting letter method name: `getRestApi`
* The SDKv3 command class name: `GetRestApiCommand`

However, the code that was taking care of mapping service names into an IAM name was not handling all cases correctly. There was also an issue with some commands that end in the word `"Command"`, like ECS's `ExecuteCommand`, which according to the rules above should work both written as `ExecuteCommand` as well as `ExecuteCommandCommand`: we did not have enough information to know if we saw the string `ExecuteCommand`, whether we should interpret it as `Execute` or `ExecuteCommand`.

Also, we were recommending to use the full SDKv3 package name and class name formats:

```
{
  service: '@aws-sdk/client-api-gateway',
  action: 'GetRestApiCommand',
}
```

Which looks ugly (imo) and leaks too many of the underlying implementation details.

This PR changes the following:

- Deprecate the `sdk-api-metadata.json` we extracted from SDKv2.
- From SDKv3 models, extract a new `sdk-v3-metadata.json` which contains the following information:
  - IAM prefix for every service
  - A list of APIs that end in the word `Command`, so we can disambiguate around these.
- From `aws-sdk-codemod`, extract a mapping from SDKv2 service names to SDKv3 service names (replacing the copy/pasted code we used to have with a build-time extraction).
- Unfortunately, both of these mappings are duplicated: once for the construct library, and once for the handlers. I did not want to go into deduplicating between these for now.
- At runtime, we now map a potential V2 service name to a V3 service name, then look up the V3 metadata to determine the IAM prefix and the normalized action name.
- There was a lot of duplication between the `assertions` handler and the `AwsCustomResource` handler (and to a lesser degree, the `events.ApiCall` handler), around loading SDKs and coercing values. Introduce a new `ApiCall` class that unifies the behavior between these call sites.
- Change the recommendation in the README from using SDKv3 names to using shorter form names (`api-gateway` and `GetRestApi`).
- Add "dynamic reuqire" protection to the `esbuild` commands for custom resources. 

Fixes #27255, closes #27268, closes #27270, closes #27395.

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
admired-contributor [Pilot] contributed between 13-24 PRs to the CDK bug This issue is a bug. p2 pr/needs-community-review This PR needs a review from a Trusted Community Member or Core Team Member.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

(custom-resources): AwsCustomResourcePolicy.fromSdkCalls does not work with v3 command format
3 participants