hashicorp / terraform-provider-aws

The AWS Provider enables Terraform to manage AWS resources.
https://registry.terraform.io/providers/hashicorp/aws
Mozilla Public License 2.0
9.76k stars 9.11k forks source link

Disallow use of `SingleNestedBlock` in resource schemas #35813

Open ewbankkit opened 7 months ago

ewbankkit commented 7 months ago

The use of SingleNestedBlock resource schemas for resources implemented using Terraform Plugin Framework is problematic. NestedBlocks have no Required/Optional/Computed flags – they are effectively always there, so an “Optional” SingleNestedBlock doesn’t work as expected if the block isn’t present in configuration (e.g. Required attributes). The pattern with Terraform Plugin SDK v2 is to use ListNestedBlock with SizeAtMost(1) and then a missing block is equivalent to an empty list. In fact SingleNestedBlocks are not available in Terraform Plugin SDK v2.

Relates https://github.com/hashicorp/terraform-provider-aws/issues/34664.

There are only 3 uses in the current codebase:

% grep SingleNestedBlock internal/service/*/*.go
internal/service/bedrock/model_invocation_logging_configuration.go:         "logging_config": schema.SingleNestedBlock{
internal/service/bedrock/model_invocation_logging_configuration.go:                 "cloudwatch_config": schema.SingleNestedBlock{
internal/service/bedrock/model_invocation_logging_configuration.go:                         "large_data_delivery_s3_config": schema.SingleNestedBlock{
internal/service/bedrock/model_invocation_logging_configuration.go:                 "s3_config": schema.SingleNestedBlock{
internal/service/opensearchserverless/security_config.go:           "saml_options": schema.SingleNestedBlock{
internal/service/opensearchserverless/security_config_data_source.go:           "saml_options": schema.SingleNestedBlock{
internal/service/verifiedpermissions/schema.go:         "definition": schema.SingleNestedBlock{

Add a semgrep rule to prevent additional occurrences and migrate the existing occurrences to ListNestedBlocks.

github-actions[bot] commented 7 months ago

Community Note

Voting for Prioritization

Volunteering to Work on This Issue