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(config): Creating multiple rules from the same lambda #21594

Merged
merged 9 commits into from
Oct 19, 2022

Conversation

watany-dev
Copy link
Contributor

@watany-dev watany-dev commented Aug 14, 2022

fixes #17582

because the id of ".addPermission" is set to a fixed value of ″permission″, which means that only one can be set in the stack.

  1. and add a unique suffix to the id. This will allow multiple custom rules to be handled in one stack.
  2. Do the id check before addPermission. This will allow only one permission to be granted to a custom rule from the config service.

Addendum:.
I have created a hash from FunctionName, AccountID, and Region to make the suffix unique.
Therefore, the omitted parts in the test code have been modified to fix the result.


All Submissions:

Adding new Unconventional Dependencies:

  • This PR adds new unconventional dependencies following the process described here

New Features

  • Have you added the new feature to an integration test?
    • Did you use yarn integ to deploy the infrastructure and generate the snapshot (i.e. yarn integ without --dry-run)?

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

@gitpod-io
Copy link

gitpod-io bot commented Aug 14, 2022

@github-actions github-actions bot added the p2 label Aug 14, 2022
@aws-cdk-automation aws-cdk-automation requested a review from a team August 14, 2022 15:11
@github-actions github-actions bot added bug This issue is a bug. effort/medium Medium work item – several days of effort p1 and removed p2 labels Aug 14, 2022
@watany-dev watany-dev changed the title fix:(config) Creating multiple rules from the same lambda fix(config): Creating multiple rules from the same lambda Aug 14, 2022
@watany-dev watany-dev force-pushed the fix-multiple-rules-from-same-lambda branch from b4280f0 to b7fe9f8 Compare August 15, 2022 00:32
@corymhall corymhall self-assigned this Aug 15, 2022
@watany-dev
Copy link
Contributor Author

@corymhall

Hi.
I understand from the PR that the CDK team is busy and it pains me to say this, but may I ask when you expect to be able to review this?
*If you look at it a little and see something obviously missing, a rough comment would be fine!

if (!props.lambdaFunction.permissionsNode.tryFindChild(customRulePermissionId)) {
props.lambdaFunction.addPermission(customRulePermissionId, {
principal: new iam.ServicePrincipal('config.amazonaws.com'),
sourceAccount: this.env.account,
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this wouldn't work if we had multiple config rules in different accounts. This would only add the permission for the first account. I think we somehow need to add the account into the id.

Copy link
Contributor

Choose a reason for hiding this comment

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

And actually I wonder if it would be better to handle this in the addPermission method since this problem is probably not unique to config. We could probably do something similar to what we do for grantInvoke

https://github.com/aws/aws-cdk/blob/8a733bad022fb2ac0551e32fd26a4847c0b36ff2/packages/%40aws-cdk/aws-lambda/lib/function-base.ts#L433-L414

Copy link
Contributor Author

@watany-dev watany-dev Aug 30, 2022

Choose a reason for hiding this comment

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

Add.
I have created a hash from FunctionName, AccountID, and Region to make the suffix unique.
Therefore, the omitted parts in the test code have been modified to fix the result.

Copy link
Contributor

Choose a reason for hiding this comment

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

@watany-dev what do you think about my suggestion for doing this in the addPermission method instead? That would be a more global fix for this issue.

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 guess I misunderstood your comment. I should have fixed the Lambda addPermission.I will implement it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@corymhall
I have a concern during the design process and am seeking advice
...Example grantInvoke is

  1. generate a unique ID from grantee information
  2. If the ID has not been used before, grant it.
    Also, addpermissions will set the id as it comes in the argument.

If addPermission is modified with this requirement, it would look like the following.
"Generate a unique hash from the argument permission of addPermisson and set the incoming id and permission if it was not used before."

This appears to be a vexing problem specific to addPermission taking the id as an argument rather than generating it dynamically; how should this work if the permission is different and the id is duplicated? I fear that some destructive change would be necessary.

Copy link
Contributor

Choose a reason for hiding this comment

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

yeah I see your point. Ok i'm good with your solution, just a couple of comments on the integ tests.

@watany-dev watany-dev force-pushed the fix-multiple-rules-from-same-lambda branch from b7fe9f8 to 41b31dc Compare August 26, 2022 00:37
@mergify mergify bot dismissed corymhall’s stale review August 26, 2022 00:37

Pull request has been modified.

@watany-dev
Copy link
Contributor Author

Addendum:.
I have created a hash from FunctionName, AccountID, and Region to make the suffix unique.
Therefore, the omitted parts in the test code have been modified to fix the result.

@watany-dev watany-dev requested a review from corymhall August 27, 2022 02:39
@watany-dev
Copy link
Contributor Author

@corymhall

Excuse me for being busy, I tried to fix it with reference to the lambda implementation, but what do you think?

Copy link
Contributor

@corymhall corymhall left a comment

Choose a reason for hiding this comment

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

Putting back in request changes.

@mergify mergify bot dismissed corymhall’s stale review September 13, 2022 13:44

Pull request has been modified.

Copy link
Contributor

@corymhall corymhall left a comment

Choose a reason for hiding this comment

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

back in request changes.

if (!props.lambdaFunction.permissionsNode.tryFindChild(customRulePermissionId)) {
props.lambdaFunction.addPermission(customRulePermissionId, {
principal: new iam.ServicePrincipal('config.amazonaws.com'),
sourceAccount: this.env.account,
Copy link
Contributor

Choose a reason for hiding this comment

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

yeah I see your point. Ok i'm good with your solution, just a couple of comments on the integ tests.

packages/@aws-cdk/aws-config/test/integ.rule.lit.ts Outdated Show resolved Hide resolved
@@ -38,5 +39,7 @@ class ConfigStack extends cdk.Stack {
}
}

new ConfigStack(app, 'aws-cdk-config-rule-integ');
new ConfigStack(app, 'aws-cdk-config-rule-integ', {
env: { account: '123456789012', region: 'us-east-1' },
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
env: { account: '123456789012', region: 'us-east-1' },

The integ tests stacks should be environment agnostic.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Let me leave this comment as it is. It is certainly not a process that asks for region and account ID. However, if it is randomized, the hash seed of the snapshot test will change every time, and the test will not converge.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think it's probably because when the env is not defined then the account and region are tokens.

I left a review comment on what you need to updated.

@@ -4,12 +4,15 @@ import * as config from '../lib';

const app = new cdk.App();

const stack = new cdk.Stack(app, 'aws-cdk-config-rule-scoped-integ');
const stack = new cdk.Stack(app, 'aws-cdk-config-rule-scoped-integ', {
env: { account: '123456789012', region: 'us-east-1' },
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
env: { account: '123456789012', region: 'us-east-1' },

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Let me leave this comment as it is. It is certainly not a process that asks for region and account ID. However, if it is randomized, the hash seed of the snapshot test will change every time, and the test will not converge.

packages/@aws-cdk/aws-config/test/integ.scoped-rule.ts Outdated Show resolved Hide resolved
packages/@aws-cdk/aws-config/lib/rule.ts Outdated Show resolved Hide resolved
Co-authored-by: Cory Hall <43035978+corymhall@users.noreply.github.com>
@mergify mergify bot dismissed corymhall’s stale review September 22, 2022 00:46

Pull request has been modified.

Co-authored-by: Cory Hall <43035978+corymhall@users.noreply.github.com>
@watany-dev
Copy link
Contributor Author

Thanks for confirming. I was satisfied with your comments and have incorporated all of them.
I have imported them with the suggestion feature of Github and will add the test now.

@watany-dev watany-dev force-pushed the fix-multiple-rules-from-same-lambda branch from 62918dd to 368ecd2 Compare September 22, 2022 08:29
const hash = createHash('sha256')
.update(JSON.stringify({
fnName: props.lambdaFunction.functionName.toString,
accountId: this.env.account,
Copy link
Contributor

Choose a reason for hiding this comment

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

Both account and region need to be resolved. This should fix the issue you are having with the environment agnostic integ tests.

accountId: Stack.of(this).resolve(this.env.account),

@watany-dev watany-dev requested a review from Naumel September 24, 2022 00:16
@watany-dev watany-dev force-pushed the fix-multiple-rules-from-same-lambda branch from 9d269e0 to d50e428 Compare September 24, 2022 00:27
@watany-dev watany-dev requested review from corymhall and Naumel and removed request for Naumel and corymhall September 28, 2022 14:14
@watany-dev
Copy link
Contributor Author

@Naumel
How did you fix the comment (typo) you received? I am sending you a reviewer once, but if you need to change it, I would appreciate it if you could reply.

@corymhall
Copy link
Contributor

@watany-dev sorry for the delay on this. We have been discussing this change internally and our hesitation is whether or not changing the logicalId of the permission resource is a breaking change or not. When the logicalId changes a new resource will be created and the existing resource will be deleted. I think that should be fine because the new permission will be created first, establishing the permission and then the old one will be deleted. What we don't know is whether there is any delay in this taking place. For this to work, the change needs to be instant and not cause any permission errors.

We need to test this scenario to be sure, but i'm not sure how.

@watany-dev
Copy link
Contributor Author

watany-dev commented Oct 10, 2022

@corymhall
Thank you for your answer.
To be honest, it's hard for me to come up with test ideas right away.
If you hesitate to implement a variable fixed ID for the purpose of avoiding duplication like this time, it is a concern that the CDK may become a complicated implementation that creates a stack for each resource.

Here's a suggested implementation

  1. The resource for the first time sets the id to a fixed value. Specifically, it searches for id="permission" and sets "permission" to id if it does not exist. If it exists, use this implementation to make the id variable. The latter pattern is a bug in this issue and should not exist now, so it seems that there is no existing impact.
  2. Add an option manualPermissionId to set the id dynamically. This breaking change will be done as planned, and if you really want to avoid replacing existing resources, specify "permission" manually. If this conflicts, it is the user's responsibility to change.
  3. Or if the solution in this PR is correct, it may be helpful
    fix(cognito): cannot use same lambda function as trigger in multiple user pools #22444

I will follow and respond to the above implementation suggestions, or better ideas.

corymhall
corymhall previously approved these changes Oct 18, 2022
@mergify
Copy link
Contributor

mergify bot commented Oct 18, 2022

Thank you for contributing! Your pull request will be updated from main and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork).

Copy link
Contributor

@TheRealAmazonKendra TheRealAmazonKendra left a comment

Choose a reason for hiding this comment

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

We now need the conflicts resolved.

@mergify mergify bot dismissed stale reviews from TheRealAmazonKendra and corymhall October 19, 2022 03:12

Pull request has been modified.

@watany-dev watany-dev force-pushed the fix-multiple-rules-from-same-lambda branch from 387470a to 4bfa8b9 Compare October 19, 2022 05:39
@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

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

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

@watany-dev
Copy link
Contributor Author

Removed the conflict. please confirm.

@watany-dev watany-dev requested review from TheRealAmazonKendra and removed request for Naumel October 19, 2022 07:00
@Naumel
Copy link
Contributor

Naumel commented Oct 19, 2022

Is this conversation

I will follow and respond to the above implementation suggestions, or better ideas.

concluded?

@watany-dev
Copy link
Contributor Author

I haven't changed the implementation since corymhall tried to merge. This time I only fixed the conflicting test code

Copy link
Contributor

@Naumel Naumel left a comment

Choose a reason for hiding this comment

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

♻️ conflicts were addressed and the PR has been previously approved.

@mergify
Copy link
Contributor

mergify bot commented Oct 19, 2022

Thank you for contributing! Your pull request will be updated from main and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork).

@mergify mergify bot merged commit 0d2b529 into aws:main Oct 19, 2022
@watany-dev watany-dev deleted the fix-multiple-rules-from-same-lambda branch October 19, 2022 12:04
mrgrain pushed a commit to mrgrain/aws-cdk that referenced this pull request Oct 24, 2022
fixes aws#17582 

because the id of ".addPermission" is set to a fixed value of ″permission″, which means that only one can be set in the stack.

1. and add a unique suffix to the id. This will allow multiple custom rules to be handled in one stack.
2. Do the id check before addPermission. This will allow only one permission to be granted to a custom rule from the config service.

Addendum:.
I have created a hash from FunctionName, AccountID, and Region to make the suffix unique.
Therefore, the omitted parts in the test code have been modified to fix the result.

----

### All Submissions:

* [x] Have you followed the guidelines in our [Contributing guide?](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md)

### Adding new Unconventional Dependencies:

* [ ] This PR adds new unconventional dependencies following the process described [here](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md/#adding-new-unconventional-dependencies)

### New Features

* [ ] Have you added the new feature to an [integration test](https://github.com/aws/aws-cdk/blob/main/INTEGRATION_TESTS.md)?
	* [ ] Did you use `yarn integ` to deploy the infrastructure and generate the snapshot (i.e. `yarn integ` without `--dry-run`)?

*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
@aws-cdk/aws-config Related to AWS Config bug This issue is a bug. effort/medium Medium work item – several days of effort p1
Projects
None yet
Development

Successfully merging this pull request may close these issues.

(aws-config): Creating multiple rules from the same lambda fails
5 participants