Skip to content

Commit

Permalink
fix: account for multiple chars in resource id[IAC-3015] (#298)
Browse files Browse the repository at this point in the history
* fix: account for multiple digit/chars in the brackets of a resource id [IAC-3015]

* chore: add new tests

* chore: add changelog
  • Loading branch information
sergiu-snyk committed Jul 25, 2024
1 parent 134e7dc commit 8527d10
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 2 deletions.
3 changes: 3 additions & 0 deletions changes/unreleased/Fixed-20240725-160823.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
kind: Fixed
body: fix location computation for resources with multi-char ID inside square brackets
time: 2024-07-25T16:08:23.452724+03:00
2 changes: 1 addition & 1 deletion pkg/hcl_interpreter/source_locations.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
)

// Utility to strip out "[x]" parts from resource IDs.
var resourceIdBracketPattern = regexp.MustCompile(`\[[^[*]\]`)
var resourceIdBracketPattern = regexp.MustCompile(`\[[^[*]+\]`)

func (v *Evaluation) Location(
resourceId string,
Expand Down
35 changes: 35 additions & 0 deletions pkg/input/golden_location_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,41 @@ var goldenLocationTests = []goldenLocationTest{
},
},
},
{
path: []interface{}{
"golden_test/tf/count-simple",
"aws_s3_bucket",
"aws_s3_bucket.example[10]", // test also double digit resource id
"bucket_prefix",
},
expected: LocationStack{
{
Path: "main.tf",
Line: 17,
Col: 3,
},
},
},
},
},
{
directory: "golden_test/tf/for-each",
cases: []goldenLocationTestCase{
{
path: []interface{}{
"golden_test/tf/for-each",
"aws_s3_bucket",
"aws_s3_bucket.mybuckets[prodabc123]",
"bucket_prefix",
},
expected: LocationStack{
{
Path: "main.tf",
Line: 27,
Col: 3,
},
},
},
},
},
}
Expand Down
72 changes: 72 additions & 0 deletions pkg/input/golden_test/tf/count-simple.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@
"bucket_prefix": "example_0"
}
},
"aws_s3_bucket.example[10]": {
"id": "aws_s3_bucket.example[10]",
"resource_type": "aws_s3_bucket",
"namespace": "golden_test/tf/count-simple/main.tf",
"meta": {},
"attributes": {
"bucket_prefix": "example_10"
}
},
"aws_s3_bucket.example[1]": {
"id": "aws_s3_bucket.example[1]",
"resource_type": "aws_s3_bucket",
Expand All @@ -34,6 +43,69 @@
"attributes": {
"bucket_prefix": "example_2"
}
},
"aws_s3_bucket.example[3]": {
"id": "aws_s3_bucket.example[3]",
"resource_type": "aws_s3_bucket",
"namespace": "golden_test/tf/count-simple/main.tf",
"meta": {},
"attributes": {
"bucket_prefix": "example_3"
}
},
"aws_s3_bucket.example[4]": {
"id": "aws_s3_bucket.example[4]",
"resource_type": "aws_s3_bucket",
"namespace": "golden_test/tf/count-simple/main.tf",
"meta": {},
"attributes": {
"bucket_prefix": "example_4"
}
},
"aws_s3_bucket.example[5]": {
"id": "aws_s3_bucket.example[5]",
"resource_type": "aws_s3_bucket",
"namespace": "golden_test/tf/count-simple/main.tf",
"meta": {},
"attributes": {
"bucket_prefix": "example_5"
}
},
"aws_s3_bucket.example[6]": {
"id": "aws_s3_bucket.example[6]",
"resource_type": "aws_s3_bucket",
"namespace": "golden_test/tf/count-simple/main.tf",
"meta": {},
"attributes": {
"bucket_prefix": "example_6"
}
},
"aws_s3_bucket.example[7]": {
"id": "aws_s3_bucket.example[7]",
"resource_type": "aws_s3_bucket",
"namespace": "golden_test/tf/count-simple/main.tf",
"meta": {},
"attributes": {
"bucket_prefix": "example_7"
}
},
"aws_s3_bucket.example[8]": {
"id": "aws_s3_bucket.example[8]",
"resource_type": "aws_s3_bucket",
"namespace": "golden_test/tf/count-simple/main.tf",
"meta": {},
"attributes": {
"bucket_prefix": "example_8"
}
},
"aws_s3_bucket.example[9]": {
"id": "aws_s3_bucket.example[9]",
"resource_type": "aws_s3_bucket",
"namespace": "golden_test/tf/count-simple/main.tf",
"meta": {},
"attributes": {
"bucket_prefix": "example_9"
}
}
}
},
Expand Down
2 changes: 1 addition & 1 deletion pkg/input/golden_test/tf/count-simple/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@
# limitations under the License.

resource "aws_s3_bucket" "example" {
count = 3
count = 11
bucket_prefix = "example_${count.index}"
}

0 comments on commit 8527d10

Please sign in to comment.