From 481d650c0a6a11962aff2d891cca7edb8776a077 Mon Sep 17 00:00:00 2001 From: Matt Hughes Date: Wed, 29 Jan 2025 15:49:12 +0000 Subject: [PATCH] Fix panic on non-string topic name A panic results in an unhelpful message when running the plugin: Failed to check ruleset; error reading from server: EOF So report an error here instead. There is potential for similar behaviour if `consume_topics` is not a list, in which case `AsValueSlice` would panic, but I'm content to not be too defensive here if no-ones needing that change. Ticket: DENA-1175 --- rules/msk_app_topics.go | 16 ++++++++++++++++ rules/msk_app_topics_test.go | 25 +++++++++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/rules/msk_app_topics.go b/rules/msk_app_topics.go index 583f495..c1a84b7 100644 --- a/rules/msk_app_topics.go +++ b/rules/msk_app_topics.go @@ -153,6 +153,22 @@ func (r *MSKAppTopicsRule) reportExternalTopics( return fmt.Errorf("evaluating topic names: %w", diags) } for _, v := range val.AsValueSlice() { + if v.Type() != cty.String { + err := runner.EmitIssue( + r, + fmt.Sprintf( + "value for '%s' should be a string, but it is a: %s", + attrName, + v.Type().FriendlyName(), + ), + topicAttr.Range, + ) + if err != nil { + return fmt.Errorf("emitting issue: %w", err) + } + continue + } + name := v.AsString() if _, ok := moduleTopicNames[name]; !ok { err := runner.EmitIssue( diff --git a/rules/msk_app_topics_test.go b/rules/msk_app_topics_test.go index e20c2f6..d8408c2 100644 --- a/rules/msk_app_topics_test.go +++ b/rules/msk_app_topics_test.go @@ -89,6 +89,31 @@ module "consumer" { }, }, }, + { + name: "topic name is not string", + files: map[string]string{ + "file.tf": ` +resource "kafka_topic" "my_topic" { + name = "my_topic" +} + +module "consumer" { + consume_topics = [kafka_topic.my_topic] +} +`, + }, + expected: []*helper.Issue{ + { + Rule: rule, + Message: "value for 'consume_topics' should be a string, but it is a: object", + Range: hcl.Range{ + Filename: "file.tf", + Start: hcl.Pos{Line: 7, Column: 2}, + End: hcl.Pos{Line: 7, Column: 41}, + }, + }, + }, + }, { name: "external topic defined outside of consumer/producer", files: map[string]string{