-
Notifications
You must be signed in to change notification settings - Fork 9.2k
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
Ensure that all errors are checked during read, not just tfresource.NotFound
#38292
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e346512
Fix cases where `tfresource.NotFound` was checked, but the general `e…
gdavison a1e85f4
Excludes semgrep check tests from semgrep scans
gdavison 4baaa9a
Prevents `nil` value panic
gdavison a7b25a7
Adds CHANGELOG entry
gdavison a716178
Adds semgrep rule tests to GHA and Make
gdavison File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
```release-note:bug | ||
aws_dx_lag: Checks for errors other than NotFound when reading. | ||
``` | ||
|
||
```release-note:bug | ||
aws_dynamodb_kinesis_streaming_destination: Checks for errors other than NotFound when reading. | ||
``` | ||
|
||
```release-note:bug | ||
aws_ec2_capacity_block_reservation: Checks for errors other than NotFound when reading. | ||
``` | ||
|
||
```release-note:bug | ||
aws_route_table: Checks for errors other than NotFound when reading. | ||
``` | ||
|
||
```release-note:bug | ||
aws_opensearchserverless_access_policy: Checks for errors other than NotFound when reading. | ||
``` | ||
|
||
```release-note:bug | ||
aws_opensearchserverless_collection: Checks for errors other than NotFound when reading. | ||
``` | ||
|
||
```release-note:bug | ||
aws_opensearchserverless_security_config: Checks for errors other than NotFound when reading. | ||
``` | ||
|
||
```release-note:bug | ||
aws_opensearchserverless_security_policy: Checks for errors other than NotFound when reading. | ||
``` | ||
|
||
```release-note:bug | ||
aws_opensearchserverless_vpc_endpoint: Checks for errors other than NotFound when reading. | ||
``` | ||
|
||
```release-note:bug | ||
aws_ram_principal_association: Checks for errors other than NotFound when reading. | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,183 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"time" | ||
|
||
"github.com/hashicorp/aws-sdk-go-base/v2/awsv1shim/v2/tfawserr" | ||
"github.com/hashicorp/terraform-provider-aws/internal/tfresource" | ||
) | ||
|
||
func test1() { | ||
_, err := call() | ||
|
||
// ruleid: notfound-without-err-checks | ||
if tfresource.NotFound(err) { | ||
return | ||
} | ||
|
||
return | ||
} | ||
|
||
func test2() { | ||
_, err := call() | ||
|
||
// ok: notfound-without-err-checks | ||
if tfresource.NotFound(err) { | ||
return | ||
} | ||
|
||
if err != nil { | ||
return | ||
} | ||
|
||
return | ||
} | ||
|
||
func test3() { | ||
_, err := call() | ||
|
||
if err != nil { | ||
// ok: notfound-without-err-checks | ||
if tfresource.NotFound(err) { | ||
return | ||
} | ||
return | ||
} | ||
|
||
return | ||
} | ||
|
||
func test4() { | ||
_, err := call() | ||
|
||
if err == nil { | ||
return | ||
// ok: notfound-without-err-checks | ||
} else if tfresource.NotFound(err) { | ||
return | ||
} else { | ||
return | ||
} | ||
} | ||
|
||
func test5() { | ||
_, err := call() | ||
|
||
// ok: notfound-without-err-checks | ||
if tfresource.NotFound(err) { | ||
return | ||
} else if err != nil { | ||
return | ||
} else { | ||
return | ||
} | ||
} | ||
|
||
func test6() error { | ||
_, err := call() | ||
|
||
// ok: notfound-without-err-checks | ||
if tfresource.NotFound(err) { | ||
return | ||
} | ||
|
||
return err | ||
} | ||
|
||
func test7() { | ||
for { | ||
_, err := call() | ||
|
||
// ok: notfound-without-err-checks | ||
if tfresource.NotFound(err) { | ||
continue | ||
} | ||
} | ||
|
||
return err | ||
} | ||
|
||
func test8() { | ||
_, err := call() | ||
|
||
// ok: notfound-without-err-checks | ||
if tfresource.NotFound(err) { | ||
return | ||
} | ||
|
||
if tfawserr.ErrCodeEquals(err, "SomeError") { | ||
return | ||
} | ||
|
||
if err != nil { | ||
return | ||
} | ||
|
||
return | ||
} | ||
|
||
func test9() { | ||
_, err := call() | ||
|
||
if err != nil { | ||
// ok: notfound-without-err-checks | ||
if tfresource.NotFound(err) { | ||
return | ||
} else { | ||
return | ||
} | ||
} | ||
|
||
return | ||
} | ||
|
||
func test10() { | ||
_, err := call() | ||
|
||
// ok: notfound-without-err-checks | ||
if tfresource.NotFound(err) { | ||
return | ||
} else if err != nil { | ||
return | ||
} | ||
|
||
return | ||
} | ||
|
||
func test11() { | ||
ctx := context.Background() | ||
|
||
tfresource.RetryWhen(ctx, 1*time.Second, nil, func(err error) (bool error) { | ||
// ok: notfound-without-err-checks | ||
if tfresource.NotFound(err) { | ||
return true, err | ||
} | ||
|
||
return false, err | ||
}) | ||
} | ||
|
||
func test12() { | ||
_, err := call() | ||
|
||
// ok: notfound-without-err-checks | ||
if tfresource.NotFound(err) { | ||
return | ||
} | ||
|
||
if PreCheckSkipError(err) { | ||
return | ||
} | ||
|
||
if err != nil { | ||
return | ||
} | ||
|
||
return | ||
} | ||
|
||
func call() (any, error) { | ||
return nil, errors.New("error") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
rules: | ||
- id: notfound-without-err-checks | ||
languages: [go] | ||
message: When checking for tfresource.NotFound() errors, typically other error conditions should be checked. | ||
patterns: | ||
- pattern: | | ||
if tfresource.NotFound($ERR) { ... } | ||
- pattern-not-inside: | | ||
if tfresource.NotFound($ERR) { ... } | ||
if $ERR != nil { ... } | ||
- pattern-not-inside: | | ||
if tfresource.NotFound($ERR) { ... } | ||
if $FUNC($ERR, ...) { ... } | ||
if $ERR != nil { ... } | ||
- pattern-not-inside: | | ||
if err != nil { | ||
if tfresource.NotFound($ERR) { ... } | ||
return ... | ||
} | ||
- pattern-not-inside: | | ||
if err != nil { | ||
if tfresource.NotFound($ERR) { | ||
... | ||
} else { | ||
... | ||
} | ||
} | ||
- pattern-not-inside: | | ||
if err == nil { | ||
... | ||
} else if tfresource.NotFound($ERR) { | ||
... | ||
} else { ... } | ||
- pattern-not-inside: | | ||
if tfresource.NotFound($ERR) { | ||
... | ||
} else if err != nil { | ||
... | ||
} else { | ||
... | ||
} | ||
- pattern-not-inside: | | ||
if tfresource.NotFound($ERR) { | ||
... | ||
} | ||
return $ERR | ||
- pattern-not-inside: | | ||
if tfresource.NotFound($ERR) { | ||
continue | ||
} | ||
- pattern-not-inside: | | ||
if tfresource.NotFound($ERR) { | ||
... | ||
} else if err != nil { | ||
... | ||
} | ||
- pattern-not-inside: | | ||
tfresource.RetryWhen(...) | ||
severity: ERROR |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I do wonder if we could combine these into steps of a single job, rather than distinct ones, but it's not something I'd hold off on merging for.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I considered validating all of the Semgrep files in this job, and then making all the jobs depend on it, but figured this was fewer changes