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

Linting for Unsafe Root TypeList to AWS Structure Expansion #16953

Closed
bflad opened this issue Jan 4, 2021 · 3 comments
Closed

Linting for Unsafe Root TypeList to AWS Structure Expansion #16953

bflad opened this issue Jan 4, 2021 · 3 comments
Labels
bug Addresses a defect in current functionality. crash Results from or addresses a Terraform crash or kernel panic. linter Pertains to changes to or issues with the various linters. provider Pertains to the provider itself, rather than any interaction with AWS. stale Old or inactive issues managed by automation, if no further action taken these will get closed.

Comments

@bflad
Copy link
Member

bflad commented Jan 4, 2021

Community Note

  • Please vote on this issue by adding a 👍 reaction to the original issue to help the community and maintainers prioritize this request
  • Please do not leave "+1" or other comments that do not add relevant new information or questions, they generate extra noise for issue followers and do not help prioritize the request
  • If you are interested in working on this issue or have submitted a pull request, please leave a comment

Description

When setting up resource logic to read an AWS structure-like field (can only be represented in the Terraform Plugin SDK as a TypeList currently), the desired convention is the following: https://github.com/hashicorp/terraform-provider-aws/blob/master/docs/contributing/data-handling-and-conversion.md#root-typelist-of-resource-and-aws-structure

if v, ok := d.GetOk("attribute_name"); ok && len(v.([]interface{})) > 0 && v.([]interface{})[0] != nil {
    input.AttributeName = expandServiceStructure(v.([]interface{})[0].(map[string]interface{}))
}

Currently there are quite a few resources that perform this operation in an unsafe way (either slice index reference or type assertion panics), e.g.

	if v, ok := d.GetOk("filter"); ok {
		filterList := v.([]interface{})
		filterMap := filterList[0].(map[string]interface{}) // panic: interface conversion: interface {} is nil, not map[string]interface {}
		inventoryConfiguration.Filter = expandS3InventoryFilter(filterMap)
	}

In this case, we may be able to setup semgrep rules to discover and report these, e.g. a pattern like this would directly catch this particular logic (the $TFMAP line is likely extraneous unless there are a lot of initial reports):

if $VALUE, $OK := d.GetOk($KEY); $OK {
  $TFLIST := $VALUE.([]interface{})
  $TFMAP := $TFLIST[0].(map[string]interface{})
  ...
}

However there are likely other similar patterns that will need to be created as well.

References

@bflad bflad added bug Addresses a defect in current functionality. crash Results from or addresses a Terraform crash or kernel panic. provider Pertains to the provider itself, rather than any interaction with AWS. linter Pertains to changes to or issues with the various linters. labels Jan 4, 2021
@bflad
Copy link
Member Author

bflad commented Jan 12, 2021

Potentially requires some semgrep adjustments before this can be implemented in that tool: semgrep/semgrep#2376

bflad added a commit that referenced this issue Jan 12, 2021
…ion, filter, and schedule configuration blocks

Reference: #16952
Reference: #16953

Output from acceptance testing:

```
--- PASS: TestAccAWSS3BucketInventory_encryptWithSSEKMS (26.70s)
--- PASS: TestAccAWSS3BucketInventory_basic (26.73s)
--- PASS: TestAccAWSS3BucketInventory_encryptWithSSES3 (27.01s)
```
bflad added a commit that referenced this issue Jan 12, 2021
…ion, filter, and schedule configuration blocks (#17055)

Reference: #16952
Reference: #16953

Output from acceptance testing:

```
--- PASS: TestAccAWSS3BucketInventory_encryptWithSSEKMS (26.70s)
--- PASS: TestAccAWSS3BucketInventory_basic (26.73s)
--- PASS: TestAccAWSS3BucketInventory_encryptWithSSES3 (27.01s)
```
@github-actions
Copy link

github-actions bot commented Jan 2, 2023

Marking this issue as stale due to inactivity. This helps our maintainers find and focus on the active issues. If this issue receives no comments in the next 30 days it will automatically be closed. Maintainers can also remove the stale label.

If this issue was automatically closed and you feel this issue should be reopened, we encourage creating a new issue linking back to this one for added context. Thank you!

@github-actions github-actions bot added the stale Old or inactive issues managed by automation, if no further action taken these will get closed. label Jan 2, 2023
@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Feb 1, 2023
@github-actions
Copy link

github-actions bot commented Mar 4, 2023

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.
If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 4, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Addresses a defect in current functionality. crash Results from or addresses a Terraform crash or kernel panic. linter Pertains to changes to or issues with the various linters. provider Pertains to the provider itself, rather than any interaction with AWS. stale Old or inactive issues managed by automation, if no further action taken these will get closed.
Projects
None yet
Development

No branches or pull requests

1 participant