Skip to content

Commit

Permalink
perf(misconf): Improve cause performance (aquasecurity#6586)
Browse files Browse the repository at this point in the history
Signed-off-by: Simar <simar@linux.com>
  • Loading branch information
simar7 authored and fl0pp5 committed May 6, 2024
1 parent c7ffd3d commit 8311f8b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 15 deletions.
11 changes: 10 additions & 1 deletion pkg/iac/scan/code.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package scan

import (
"bufio"
"bytes"
"fmt"
"io/fs"
"path/filepath"
Expand Down Expand Up @@ -149,7 +151,14 @@ func (r *Result) GetCode(opts ...CodeOption) (*Code, error) {
Lines: nil,
}

rawLines := strings.Split(string(content), "\n")
var rawLines []string
bs := bufio.NewScanner(bytes.NewReader(content))
for bs.Scan() {
rawLines = append(rawLines, bs.Text())
}
if bs.Err() != nil {
return nil, fmt.Errorf("failed to scan file : %w", err)
}

var highlightedLines []string
if settings.includeHighlighted {
Expand Down
34 changes: 20 additions & 14 deletions pkg/misconf/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -503,20 +503,26 @@ func NewCauseWithCode(underlying scan.Result) types.CauseMetadata {
},
})
}
if code, err := underlying.GetCode(); err == nil {
cause.Code = types.Code{
Lines: lo.Map(code.Lines, func(l scan.Line, i int) types.Line {
return types.Line{
Number: l.Number,
Content: l.Content,
IsCause: l.IsCause,
Annotation: l.Annotation,
Truncated: l.Truncated,
Highlighted: l.Highlighted,
FirstCause: l.FirstCause,
LastCause: l.LastCause,
}
}),

// only failures have a code cause
// failures can happen either due to lack of
// OR misconfiguration of something
if underlying.Status() == scan.StatusFailed {
if code, err := underlying.GetCode(); err == nil {
cause.Code = types.Code{
Lines: lo.Map(code.Lines, func(l scan.Line, i int) types.Line {
return types.Line{
Number: l.Number,
Content: l.Content,
IsCause: l.IsCause,
Annotation: l.Annotation,
Truncated: l.Truncated,
Highlighted: l.Highlighted,
FirstCause: l.FirstCause,
LastCause: l.LastCause,
}
}),
}
}
}
return cause
Expand Down

0 comments on commit 8311f8b

Please sign in to comment.