Skip to content

Commit

Permalink
Reduce noisy logging messages (#117)
Browse files Browse the repository at this point in the history
* Reduce noisy logging messages

* Push missing commit
  • Loading branch information
tstromberg authored Apr 12, 2024
1 parent fc37181 commit 1284b51
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 51 deletions.
15 changes: 3 additions & 12 deletions pkg/action/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
)

func relFileReport(ctx context.Context, c Config, path string) (map[string]bincapz.FileReport, error) {
logger := clog.FromContext(ctx).With("path", path)
fromPath := path
fromConfig := c
fromConfig.Renderer = nil
Expand All @@ -25,24 +24,22 @@ func relFileReport(ctx context.Context, c Config, path string) (map[string]binca
return nil, err
}
fromRelPath := map[string]bincapz.FileReport{}
for fname, f := range fromReport.Files {
for _, f := range fromReport.Files {
if f.Skipped != "" || f.Error != "" {
continue
}
logger.Info("file report", slog.String("file", fname), slog.Any("report", f))

rel, err := filepath.Rel(fromPath, f.Path)
if err != nil {
return nil, fmt.Errorf("rel(%q,%q): %w", fromPath, f.Path, err)
}
fromRelPath[rel] = f
logger.Info("relative file report", slog.String("relpath", rel), slog.Any("report", f))
}

return fromRelPath, nil
}

func Diff(ctx context.Context, c Config) (*bincapz.Report, error) {
clog.InfoContext(ctx, "diffing", slog.Any("scanpaths", c.ScanPaths))
if len(c.ScanPaths) != 2 {
return nil, fmt.Errorf("diff mode requires 2 paths, you passed in %d path(s)", len(c.ScanPaths))
}
Expand Down Expand Up @@ -190,11 +187,5 @@ func Diff(ctx context.Context, c Config) (*bincapz.Report, error) {
}
}

clog.FromContext(ctx).Info("diff result", slog.Any("diff", d))

r := &bincapz.Report{
Diff: d,
}

return r, err
return &bincapz.Report{Diff: d}, err
}
5 changes: 1 addition & 4 deletions pkg/render/markdown.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,10 @@ import (
"context"
"fmt"
"io"
"log/slog"
"sort"
"strings"

"github.com/chainguard-dev/bincapz/pkg/bincapz"
"github.com/chainguard-dev/clog"
"github.com/olekukonko/tablewriter"
)

Expand Down Expand Up @@ -60,7 +58,7 @@ func (r Markdown) Full(ctx context.Context, rep bincapz.Report) error {
return nil
}

func markdownTable(ctx context.Context, fr *bincapz.FileReport, w io.Writer, rc tableConfig) {
func markdownTable(_ context.Context, fr *bincapz.FileReport, w io.Writer, rc tableConfig) {
path := fr.Path
if fr.Error != "" {
fmt.Printf("⚠️ %s - error: %s\n", path, fr.Error)
Expand Down Expand Up @@ -120,7 +118,6 @@ func markdownTable(ctx context.Context, fr *bincapz.FileReport, w io.Writer, rc
}

if len(k.Behavior.Values) > 0 {
clog.InfoContext(ctx, "", slog.String("description", k.Behavior.Description), slog.Any("values", k.Behavior.Values))
values := strings.Join(k.Behavior.Values, "\n")
before := " \""
after := "\""
Expand Down
13 changes: 4 additions & 9 deletions pkg/render/terminal.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,11 @@ type tableConfig struct {
DiffAdded bool
}

func forceWrap(_ context.Context, s string, x int) string {
func forceWrap(s string, x int) string {
words, _ := tablewriter.WrapString(s, x)
fw := []string{}
for _, w := range words {
if len(w) > x-2 {
clog.Info("wrapping", slog.Any("word", w), slog.Int("length", len(w)), slog.Int("max", x-2))
w = w[0:x-2] + ".."
}
fw = append(fw, w)
Expand Down Expand Up @@ -188,7 +187,7 @@ func renderTable(ctx context.Context, fr *bincapz.FileReport, w io.Writer, rc ta
maxKeyLen := 0

for _, k := range kbs {
key := forceWrap(ctx, k.Key, keyWidth)
key := forceWrap(k.Key, keyWidth)
if len(key) > maxKeyLen {
maxKeyLen = len(key)
}
Expand All @@ -199,8 +198,6 @@ func renderTable(ctx context.Context, fr *bincapz.FileReport, w io.Writer, rc ta
descWidth = 120
}

clog.InfoContext(ctx, "terminal width", slog.Int("width", tWidth), slog.Int("descWidth", descWidth))

for _, k := range kbs {
desc := k.Behavior.Description
before, _, found := strings.Cut(desc, ". ")
Expand All @@ -215,19 +212,18 @@ func renderTable(ctx context.Context, fr *bincapz.FileReport, w io.Writer, rc ta
}
}

key := forceWrap(ctx, k.Key, keyWidth)
key := forceWrap(k.Key, keyWidth)
words, _ := tablewriter.WrapString(desc, descWidth)
desc = strings.Join(words, "\n")
if len(k.Behavior.Values) > 0 {
clog.InfoContext(ctx, "Values", slog.String("description", k.Behavior.Description), slog.Any("values", k.Behavior.Values))
values := strings.Join(k.Behavior.Values, "\n")
before := " \""
after := "\""
if (len(desc) + len(values) + 3) > descWidth {
before = "\n"
after = ""
}
desc = fmt.Sprintf("%s:%s%s%s", desc, before, forceWrap(ctx, strings.Join(k.Behavior.Values, "\n"), descWidth), after)
desc = fmt.Sprintf("%s:%s%s%s", desc, before, forceWrap(strings.Join(k.Behavior.Values, "\n"), descWidth), after)
}

// lowercase first character for consistency
Expand Down Expand Up @@ -265,7 +261,6 @@ func renderTable(ctx context.Context, fr *bincapz.FileReport, w io.Writer, rc ta
}

tableWidth := maxKeyLen + maxDescWidth + padding + maxRiskWidth
clog.InfoContextf(ctx, "table width: %d", tableWidth)
fmt.Fprintf(w, "%s\n", strings.Repeat("-", tableWidth))
table.SetNoWhiteSpace(true)
table.SetTablePadding(" ")
Expand Down
24 changes: 9 additions & 15 deletions pkg/report/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ func unprintableString(s string) bool {
return false
}

func longestUnique(ctx context.Context, raw []string) []string {
func longestUnique(raw []string) []string {
longest := []string{}

// inefficiently remove substring matches
Expand All @@ -174,7 +174,6 @@ func longestUnique(ctx context.Context, raw []string) []string {
isLongest := true
for _, o := range raw {
if o != s && strings.Contains(o, s) {
clog.InfoContextf(ctx, "%s contains %s", o, s)
isLongest = false
break
}
Expand Down Expand Up @@ -208,7 +207,7 @@ func matchToString(ruleName string, m yara.MatchString) string {
}

// extract important values.
func matchValues(ctx context.Context, key string, ruleName string, ms []yara.MatchString) []string {
func matchValues(key string, ruleName string, ms []yara.MatchString) []string {
raw := []string{}

for _, m := range ms {
Expand All @@ -234,24 +233,23 @@ func matchValues(ctx context.Context, key string, ruleName string, ms []yara.Mat
continue
}

clog.InfoContextf(ctx, "keeping: %s - %s - %s: %s", key, ruleName, m.Name, m.Data)
raw = append(raw, matchToString(ruleName, m))
}

slices.Sort(raw)
return longestUnique(ctx, raw)
return longestUnique(raw)
}

// extract match strings.
func matchStrings(ctx context.Context, ruleName string, ms []yara.MatchString) []string {
func matchStrings(ruleName string, ms []yara.MatchString) []string {
raw := []string{}

for _, m := range ms {
raw = append(raw, matchToString(ruleName, m))
}

slices.Sort(raw)
return longestUnique(ctx, raw)
return longestUnique(raw)
}

func pathChecksum(path string) (string, error) {
Expand Down Expand Up @@ -296,26 +294,22 @@ func Generate(ctx context.Context, path string, mrs yara.MatchRules, ignoreTags
overallRiskScore := 0
riskCounts := map[int]int{}

for x, m := range mrs {
clog.InfoContextf(ctx, "yara match[%d]: %+v", x, m)

for _, m := range mrs {
risk := behaviorRisk(m.Namespace, m.Tags)
if risk > overallRiskScore {
overallRiskScore = risk
}
riskCounts[risk]++
if risk < minScore {
clog.InfoContextf(ctx, "dropping %s (risk %d is too low)", m.Namespace, risk)
continue
}
key := generateKey(m.Namespace, m.Rule)
clog.InfoContextf(ctx, "%s key=%s", m.Namespace, key)

b := bincapz.Behavior{
RiskScore: risk,
RiskLevel: riskLevels[risk],
Values: matchValues(ctx, key, m.Rule, m.Strings),
MatchStrings: matchStrings(ctx, m.Rule, m.Strings),
Values: matchValues(key, m.Rule, m.Strings),
MatchStrings: matchStrings(m.Rule, m.Strings),
}

for _, meta := range m.Metas {
Expand Down Expand Up @@ -362,7 +356,7 @@ func Generate(ctx context.Context, path string, mrs yara.MatchRules, ignoreTags

if ignoreMatch(m.Tags, ignore) {
fr.FilteredBehaviors++
clog.InfoContextf(ctx, "dropping %s (tags match ignore list)", m.Namespace)
clog.DebugContextf(ctx, "dropping %s (tags match ignore list)", m.Namespace)
continue
}

Expand Down
11 changes: 0 additions & 11 deletions pkg/rules/rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@ import (

var FS = rules.FS

var skipFiles = map[string]bool{
"third_party/Neo23x0/yara/configured_vulns_ext_vars.yar": true,
}

func Compile(ctx context.Context, root fs.FS, thirdParty bool) (*yara.Rules, error) {
yc, err := yara.NewCompiler()
if err != nil {
Expand All @@ -47,14 +43,7 @@ func Compile(ctx context.Context, root fs.FS, thirdParty bool) (*yara.Rules, err
return nil
}

if skipFiles[path] {
logger.Info("skipping (skipFiles)")
return nil
}

if !d.IsDir() && filepath.Ext(path) == ".yara" || filepath.Ext(path) == ".yar" {
logger.Info("reading")

bs, err := fs.ReadFile(root, path)
if err != nil {
return fmt.Errorf("readfile: %w", err)
Expand Down

0 comments on commit 1284b51

Please sign in to comment.