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(Tracker): Add Resolved File Lines' counter #6501

Merged
merged 11 commits into from
Jul 19, 2023
8 changes: 8 additions & 0 deletions pkg/kics/sink.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ func (s *Service) sink(ctx context.Context, filename, scanID string, rc io.Reade
return nil
}

linesResolved := 0
for _, ref := range documents.ResolvedFiles {
if ref.Path != filename {
linesResolved += len(*ref.LinesContent)
}
}
s.Tracker.TrackFileFoundCountLines(linesResolved)

fileCommands := s.Parser.CommentsCommands(filename, *content)

for _, document := range documents.Docs {
Expand Down
21 changes: 14 additions & 7 deletions pkg/resolver/file/file.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package file

import (
"encoding/json"
"errors"
"fmt"
"io"
Expand Down Expand Up @@ -67,7 +68,7 @@ func (r *Resolver) Resolve(fileContent []byte, path string, resolveCount int, re
// resolve the paths
obj, _ = r.walk(fileContent, obj, obj, path, resolveCount, resolvedFilesCache, false)

b, err := r.marshler(obj)
b, err := json.MarshalIndent(obj, "", "")
if err == nil {
return b
}
Expand Down Expand Up @@ -236,9 +237,7 @@ func (r *Resolver) resolveYamlPath(
value = checkServerlessFileReference(value)

path := filepath.Join(filepath.Dir(filePath), value)
splitPath = strings.Split(path, "#") // splitting by removing the section to look for in the file
// index 0 contains the path of the file while the other indexes contain the sections (e.g. path = "./definitions.json#User/schema")
onlyFilePath := splitPath[0]
onlyFilePath := getPathFromString(path)
_, err := os.Stat(path)
if err != nil {
return *v, false
Expand All @@ -261,7 +260,7 @@ func (r *Resolver) resolveYamlPath(
}
}

r.ResolvedFiles[value] = model.ResolvedFile{
r.ResolvedFiles[getPathFromString(value)] = model.ResolvedFile{
Content: resolvedFilesCache[filename].fileContent,
Path: path,
LinesContent: utils.SplitLines(string(resolvedFilesCache[filename].fileContent)),
Expand All @@ -273,7 +272,7 @@ func (r *Resolver) resolveYamlPath(
if strings.Contains(strings.ToLower(value), "!ref") { // Cloudformation !Ref check
return *obj, false
}
if len(splitPath) == 1 {
if !strings.Contains(path, "#") {
return *obj, true
}
}
Expand Down Expand Up @@ -353,6 +352,14 @@ func (r *Resolver) resolveFile(
return nil, false
}

func getPathFromString(path string) string {
lastIndex := strings.LastIndex(path, "#")
if lastIndex == -1 {
return path
}
return path[:lastIndex]
}

// isPath returns true if the value is a valid path
func (r *Resolver) resolvePath(
originalFileContent []byte,
Expand Down Expand Up @@ -391,7 +398,7 @@ func (r *Resolver) resolvePath(
}
}

r.ResolvedFiles[value] = model.ResolvedFile{
r.ResolvedFiles[getPathFromString(value)] = model.ResolvedFile{
Content: resolvedFilesCache[onlyFilePath].fileContent,
Path: path,
LinesContent: utils.SplitLines(string(resolvedFilesCache[onlyFilePath].fileContent)),
Expand Down
Loading