Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/45202.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
resource/aws_accessanalyzer_analyzer: Fix `interface conversion: interface {} is nil, not map[string]interface {}` panics when `configuration.unused_access.analysis_rule.exclusion.resource_tags` contains `null` values
```
1 change: 1 addition & 0 deletions internal/service/accessanalyzer/accessanalyzer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ func TestAccAccessAnalyzer_serial(t *testing.T) {
"tags": testAccAccessAnalyzerAnalyzer_tagsSerial,
"type_Organization": testAccAnalyzer_typeOrganization,
"upgradeV5_95_0": testAccAnalyzer_upgradeV5_95_0,
"nullInResourceTags": testAccAnalyzer_nullInResourceTags,
},
"ArchiveRule": {
acctest.CtBasic: testAccAnalyzerArchiveRule_basic,
Expand Down
27 changes: 7 additions & 20 deletions internal/service/accessanalyzer/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -397,25 +397,15 @@ func expandInternalAccessAnalysisRuleCriteria(tfMap map[string]any) *types.Inter
apiObject := &types.InternalAccessAnalysisRuleCriteria{}

if tfList, ok := tfMap["account_ids"].([]any); ok && len(tfList) > 0 {
for _, v := range tfList {
accountID, ok := v.(string)
if !ok {
continue
}
apiObject.AccountIds = append(apiObject.AccountIds, accountID)
}
apiObject.AccountIds = flex.ExpandStringValueList(tfList)
}

if tfList, ok := tfMap["resource_arns"].([]any); ok && len(tfList) > 0 {
for _, v := range tfList {
apiObject.ResourceArns = append(apiObject.ResourceArns, v.(string))
}
apiObject.ResourceArns = flex.ExpandStringValueList(tfList)
}

if tfList, ok := tfMap["resource_types"].([]any); ok && len(tfList) > 0 {
for _, v := range tfList {
apiObject.ResourceTypes = append(apiObject.ResourceTypes, types.ResourceType(v.(string)))
}
apiObject.ResourceTypes = flex.ExpandStringyValueList[types.ResourceType](tfList)
}

return apiObject
Expand Down Expand Up @@ -478,17 +468,14 @@ func expandAnalysisRuleCriteria(tfMap map[string]any) *types.AnalysisRuleCriteri
apiObject := &types.AnalysisRuleCriteria{}

if tfList, ok := tfMap["account_ids"].([]any); ok && len(tfList) > 0 {
for _, v := range tfList {
accountID, ok := v.(string)
if !ok {
continue
}
apiObject.AccountIds = append(apiObject.AccountIds, accountID)
}
apiObject.AccountIds = flex.ExpandStringValueList(tfList)
}

if tfList, ok := tfMap[names.AttrResourceTags].([]any); ok && len(tfList) > 0 {
for _, v := range tfList {
if v == nil {
continue
}
apiObject.ResourceTags = append(apiObject.ResourceTags, flex.ExpandStringValueMap(v.(map[string]any)))
}
}
Expand Down
51 changes: 51 additions & 0 deletions internal/service/accessanalyzer/analyzer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"fmt"
"testing"

"github.com/YakDriver/regexache"
"github.com/aws/aws-sdk-go-v2/service/accessanalyzer/types"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/plancheck"
Expand Down Expand Up @@ -358,6 +359,29 @@ func testAccAnalyzer_upgradeV5_95_0(t *testing.T) {
})
}

// https://github.com/hashicorp/terraform-provider-aws/issues/45136.
func testAccAnalyzer_nullInResourceTags(t *testing.T) {
ctx := acctest.Context(t)
rName := acctest.RandomWithPrefix(t, acctest.ResourcePrefix)

acctest.Test(ctx, t, resource.TestCase{
PreCheck: func() {
acctest.PreCheck(ctx, t)
testAccPreCheck(ctx, t)
},
ErrorCheck: acctest.ErrorCheck(t, names.AccessAnalyzerServiceID),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
CheckDestroy: testAccCheckAnalyzerDestroy(ctx, t),
Steps: []resource.TestStep{
{
Config: testAccAnalyzerConfig_nullInResourceTags(rName),
// Error, but no crash.
ExpectError: regexache.MustCompile(`External Access analyzers cannot be created with the configuration`),
},
},
})
}

func testAccCheckAnalyzerDestroy(ctx context.Context, t *testing.T) resource.TestCheckFunc {
return func(s *terraform.State) error {
conn := acctest.ProviderMeta(ctx, t).AccessAnalyzerClient(ctx)
Expand Down Expand Up @@ -603,3 +627,30 @@ resource "aws_accessanalyzer_analyzer" "test" {
}
`, rName)
}

func testAccAnalyzerConfig_nullInResourceTags(rName string) string {
return fmt.Sprintf(`
resource "aws_accessanalyzer_analyzer" "test" {
analyzer_name = %[1]q

configuration {
unused_access {
unused_access_age = 180
analysis_rule {
exclusion {
account_ids = [
"123456789012",
"234567890123",
]
}
exclusion {
resource_tags = [
{ key1 = null },
]
}
}
}
}
}
`, rName)
}
Loading