From 8a70f9ec41f4f9dd0752076a6bd9affacd511db9 Mon Sep 17 00:00:00 2001 From: Paul Bergeron Date: Thu, 19 Nov 2020 14:17:33 +0100 Subject: [PATCH 1/3] Fix infinite loop on variable resolution --- pkg/iac-providers/terraform/v12/variable-references.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/pkg/iac-providers/terraform/v12/variable-references.go b/pkg/iac-providers/terraform/v12/variable-references.go index a5f4e8f56..0945ca564 100644 --- a/pkg/iac-providers/terraform/v12/variable-references.go +++ b/pkg/iac-providers/terraform/v12/variable-references.go @@ -148,6 +148,10 @@ func (r *RefResolver) ResolveVarRefFromParentModuleCall(varRef string) interface if reflect.TypeOf(val).Kind() == reflect.String { valStr := val.(string) resolvedVal := strings.Replace(varRef, varExpr, valStr, 1) + if varRef == resolvedVal { + zap.S().Debugf("resolved str variable ref refers to self: '%v'", varRef) + return val + } zap.S().Debugf("resolved str variable ref: '%v', value: '%v'", varRef, string(resolvedVal)) return r.ResolveStrRef(resolvedVal) } From 8b644b5a6c2ec51f522a0fae202d1e6a379bcdfe Mon Sep 17 00:00:00 2001 From: Paul Bergeron Date: Tue, 24 Nov 2020 11:12:21 +0100 Subject: [PATCH 2/3] Return `varRef` instead of `val` --- pkg/iac-providers/terraform/v12/variable-references.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/iac-providers/terraform/v12/variable-references.go b/pkg/iac-providers/terraform/v12/variable-references.go index 0945ca564..7d7f5aa29 100644 --- a/pkg/iac-providers/terraform/v12/variable-references.go +++ b/pkg/iac-providers/terraform/v12/variable-references.go @@ -150,7 +150,7 @@ func (r *RefResolver) ResolveVarRefFromParentModuleCall(varRef string) interface resolvedVal := strings.Replace(varRef, varExpr, valStr, 1) if varRef == resolvedVal { zap.S().Debugf("resolved str variable ref refers to self: '%v'", varRef) - return val + return varRef } zap.S().Debugf("resolved str variable ref: '%v', value: '%v'", varRef, string(resolvedVal)) return r.ResolveStrRef(resolvedVal) From d356d5c9df9211a6ef2cd3f70a27ffa112c44678 Mon Sep 17 00:00:00 2001 From: Paul Bergeron Date: Tue, 24 Nov 2020 11:17:11 +0100 Subject: [PATCH 3/3] Apply fix for self referential variables to non-parent modules --- pkg/iac-providers/terraform/v12/variable-references.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/pkg/iac-providers/terraform/v12/variable-references.go b/pkg/iac-providers/terraform/v12/variable-references.go index 7d7f5aa29..24ab27b1c 100644 --- a/pkg/iac-providers/terraform/v12/variable-references.go +++ b/pkg/iac-providers/terraform/v12/variable-references.go @@ -87,6 +87,10 @@ func (r *RefResolver) ResolveVarRef(varRef string) interface{} { if reflect.TypeOf(val).Kind() == reflect.String { valStr := val.(string) resolvedVal := strings.Replace(varRef, varExpr, valStr, 1) + if varRef == resolvedVal { + zap.S().Debugf("resolved str variable ref refers to self: '%v'", varRef) + return varRef + } zap.S().Debugf("resolved str variable ref: '%v', value: '%v'", varRef, resolvedVal) return r.ResolveStrRef(resolvedVal) }