Skip to content

feat: Scan classes that do not implement an interface#25

Merged
mbfreder merged 2 commits intoaws:mainfrom
mbfreder:enhancement
Feb 28, 2023
Merged

feat: Scan classes that do not implement an interface#25
mbfreder merged 2 commits intoaws:mainfrom
mbfreder:enhancement

Conversation

@mbfreder
Copy link
Copy Markdown
Contributor

@mbfreder mbfreder commented Feb 9, 2023

Issue #, if available: Issue #24

Description of changes:

As explained here, these changes allow our rules to visit classes that do not implement an interface or do not have a method that receives either (Map, com.amazonaws.services.lambda.runtime.Context) or (InputStream, OutputStream, com.amazonaws.services.lambda.runtime.Context) as parameters, but are still valid lambda handler classes.

  • Updated the isLambdahandler() method to handle the above-mentioned case.
  • Added test class to reproduce the issue
  • Added unit tests

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

@mbfreder mbfreder requested a review from a team as a code owner February 9, 2023 19:19
* Returns true if the class has the word "handler" in the name.
*/
private boolean hasHandlerInClassName(XClass xClass) {
return xClass.toString().contains("Handler") || xClass.toString().contains("handler");
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I'm worried that this might increase the noise significantly. Isn't there any other way to detect whether a class is auto-wired to a Lambda function?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

It's not really possible to do that. Basically customers can't use almost any class and method as a handler for a Lambda function (A very minimal example and a broader description in the issue related to this PR: #24). So we're following the most typical naming convention to catch more cases that we're not checking yet. And the condition here is a class name AND a specific method name, so hopefully that's enough of a restriction.

The main problem here is that customers might not be using any of these as handlers, but they could, and there's no way of knowing that. (It's just a different handler in their function configuration and they start using it without changing anything in their code base)

Copy link
Copy Markdown
Contributor

@valerena valerena Feb 13, 2023

Choose a reason for hiding this comment

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

I would just keep Handler in upper case only though, considering that it's a class name, you would expect CamelCasing, so "Handler" should start with a capital H.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Got it, thanks. Updated.

Copy link
Copy Markdown
Contributor

@valerena valerena left a comment

Choose a reason for hiding this comment

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

Let's use a somewhat standard format for commit messages, using conventional commits, starting with a prefix feat: for new features, and trying to not make it too long (there are many places with guidance around this, but for example here).

In this case, it should be something more like
feat: Scan classes that do not implement an interface

/**
* Returns true if the class has a method called "handleRequest"
*/
private boolean hasMethodCalledHandleRequest(XClass xClass) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Very minor, but I would prefer this to be called hasHandleRequestMethod

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Got it. Thanks

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

You didn't update this naming yet. We can discuss if you prefer to not change the name.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

You're right. I first thought it might be confusing since we already have a hasLambdaHandlerMethod method. That's why I tried to make it more explicit and remove any confusion by naming it the way I did. But I think the method's comment is clear enough in this regard, so hasHandleRequestMethod makes more sense.

@mbfreder mbfreder changed the title Scan lambda handler classes that do not implement an interface feat: Scan classes that do not implement an interface Feb 21, 2023
/**
* Returns true if the class has a method called "handleRequest"
*/
private boolean hasMethodCalledHandleRequest(XClass xClass) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

You didn't update this naming yet. We can discuss if you prefer to not change the name.

}

/**
* Returns true if the class has the word "handler" in the name.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Another minor thing, but in the comment specify "Handler" with capital H

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Updated. Thanks

@mbfreder mbfreder requested a review from valerena February 28, 2023 18:28
@mbfreder mbfreder merged commit 15429f0 into aws:main Feb 28, 2023
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.

3 participants