Skip to content
This repository has been archived by the owner on Sep 3, 2024. It is now read-only.

Commit

Permalink
fix panic in EnrichResources (#362)
Browse files Browse the repository at this point in the history
  • Loading branch information
jaspervdj-luminal authored Oct 13, 2022
1 parent b41673e commit 299dea3
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
25 changes: 19 additions & 6 deletions pkg/regulatf/moduletree.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,16 +300,29 @@ func walkResource(v Visitor, moduleName ModuleName, resource *configs.Resource,
v.VisitExpr(name.AddKey("count"), resource.Count)
}

body, ok := resource.Config.(*hclsyntax.Body)
if !ok {
logrus.Warningf("Missing body for resource %s", name.ToString())
return
}
walkBody(v, name, resource.Config)

walkBlock(v, name, body)
v.LeaveResource()
}

func walkBody(v Visitor, name FullName, body hcl.Body) {
switch b := body.(type) {
case *hclsyntax.Body:
walkBlock(v, name, b)
default:
walkJustAttributes(v, name, body)
}
}

func walkJustAttributes(v Visitor, name FullName, body hcl.Body) {
v.VisitBlock(name)

attributes, _ := body.JustAttributes()
for _, attribute := range attributes {
v.VisitExpr(name.AddKey(attribute.Name), attribute.Expr)
}
}

func walkBlock(v Visitor, name FullName, body *hclsyntax.Body) {
v.VisitBlock(name)

Expand Down
2 changes: 1 addition & 1 deletion pkg/reporter/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ func (s *ScanInput) EnrichResources(conf loader.LoadedConfigurations) {
filepath = s.Filepath
}
location, err := conf.Location(filepath, []string{resourceId})
if err == nil && location != nil {
if err == nil && location != nil && s.Resources[resourceId] != nil {
s.Resources[resourceId]["_source_location"] = location
}
}
Expand Down

0 comments on commit 299dea3

Please sign in to comment.