Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: added null check before value conversion to avoid panic #1020

Merged
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
5 changes: 5 additions & 0 deletions pkg/iac-providers/terraform/commons/cty-converters.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,11 @@ func ctyToMap(ctyVal cty.Value) (interface{}, error) {

// convertCtyToGoNative converts a cty.Value to its go native type
func convertCtyToGoNative(ctyVal cty.Value) (interface{}, error) {
// no need to convert variable to any type in case value is null
// added check here since this function is been called in recursive manner
if ctyVal.IsNull() {
return nil, nil
}
if ctyVal.Type().IsPrimitiveType() {
return convertPrimitiveType(ctyVal)
}
Expand Down
13 changes: 10 additions & 3 deletions pkg/iac-providers/terraform/commons/extract-container-images.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,12 +135,19 @@ func getContainersFromhclBody(hclBody *hclsyntax.Body) (results []output.Contain
it := re.ElementIterator()
for it.Next() {
_, val := it.Element()
containerTemp, err := ctyToMap(val)
containerTemp, err := convertCtyToGoNative(val)
if err != nil {
zap.S().Errorf("error fetching containers from aws resource: %v", err)
return
}
containerMap := containerTemp.(map[string]interface{})
var (
containerMap map[string]interface{}
isMap bool
)

if containerMap, isMap = containerTemp.(map[string]interface{}); !isMap {
break
}
tempContainer := output.ContainerDetails{}
if image, iok := containerMap[image]; iok {
if imageName, ok := image.(string); ok {
Expand Down Expand Up @@ -261,7 +268,7 @@ func getValueFromCtyExpr(expr hclsyntax.Expression) (value string) {
zap.S().Errorf("error fetching containers from k8s resource: %v", getErrorMessagesFromDiagnostics(diags))
return
}
valInterface, err := ctyToStr(val)
valInterface, err := convertCtyToGoNative(val)
if err != nil {
zap.S().Errorf("error fetching containers from k8s resource: %v", err)
return
Expand Down