From 7bc80a4ebc95ac6be64032867f5bd6d70425a493 Mon Sep 17 00:00:00 2001 From: ahmed Date: Wed, 24 Jan 2024 13:48:09 -0500 Subject: [PATCH] updates to plain and json printing to include verification error --- pkg/output/json.go | 39 ++++++++++++++++++++++++--------------- pkg/output/plain.go | 33 +++++++++++++++++++++------------ 2 files changed, 45 insertions(+), 27 deletions(-) diff --git a/pkg/output/json.go b/pkg/output/json.go index 6fdb76e944a9..49a2974027bb 100644 --- a/pkg/output/json.go +++ b/pkg/output/json.go @@ -17,6 +17,13 @@ import ( type JSONPrinter struct{ mu sync.Mutex } func (p *JSONPrinter) Print(_ context.Context, r *detectors.ResultWithMetadata) error { + verificationErr := func(err error) string { + if err != nil { + return err.Error() + } + return "" + }(r.VerificationError()) + v := &struct { // SourceMetadata contains source-specific contextual information. SourceMetadata *source_metadatapb.MetaData @@ -31,8 +38,9 @@ func (p *JSONPrinter) Print(_ context.Context, r *detectors.ResultWithMetadata) // DetectorName is the string name of the DetectorType. DetectorName string // DecoderName is the string name of the DecoderType. - DecoderName string - Verified bool + DecoderName string + Verified bool + VerificationError string `json:",omitempty"` // Raw contains the raw secret data. Raw string // RawV2 contains the raw secret identifier that is a combination of both the ID and the secret. @@ -44,19 +52,20 @@ func (p *JSONPrinter) Print(_ context.Context, r *detectors.ResultWithMetadata) ExtraData map[string]string StructuredData *detectorspb.StructuredData }{ - SourceMetadata: r.SourceMetadata, - SourceID: r.SourceID, - SourceType: r.SourceType, - SourceName: r.SourceName, - DetectorType: r.DetectorType, - DetectorName: r.DetectorType.String(), - DecoderName: r.DecoderType.String(), - Verified: r.Verified, - Raw: string(r.Raw), - RawV2: string(r.RawV2), - Redacted: r.Redacted, - ExtraData: r.ExtraData, - StructuredData: r.StructuredData, + SourceMetadata: r.SourceMetadata, + SourceID: r.SourceID, + SourceType: r.SourceType, + SourceName: r.SourceName, + DetectorType: r.DetectorType, + DetectorName: r.DetectorType.String(), + DecoderName: r.DecoderType.String(), + Verified: r.Verified, + VerificationError: verificationErr, + Raw: string(r.Raw), + RawV2: string(r.RawV2), + Redacted: r.Redacted, + ExtraData: r.ExtraData, + StructuredData: r.StructuredData, } out, err := json.Marshal(v) if err != nil { diff --git a/pkg/output/plain.go b/pkg/output/plain.go index 4dc2c0a27f1e..fe2ce3778fe4 100644 --- a/pkg/output/plain.go +++ b/pkg/output/plain.go @@ -17,9 +17,12 @@ import ( ) var ( - yellowPrinter = color.New(color.FgYellow) - greenPrinter = color.New(color.FgHiGreen) - whitePrinter = color.New(color.FgWhite) + boldYellowPrinter = color.New(color.Bold, color.FgYellow) + yellowPrinter = color.New(color.FgHiYellow) + greenPrinter = color.New(color.FgHiGreen) + boldGreenPrinter = color.New(color.Bold, color.FgHiGreen) + whitePrinter = color.New(color.FgWhite) + boldWhitePrinter = color.New(color.Bold, color.FgWhite) ) // PlainPrinter is a printer that prints results in plain text format. @@ -27,11 +30,12 @@ type PlainPrinter struct{ mu sync.Mutex } func (p *PlainPrinter) Print(_ context.Context, r *detectors.ResultWithMetadata) error { out := outputFormat{ - DetectorType: r.Result.DetectorType.String(), - DecoderType: r.Result.DecoderType.String(), - Verified: r.Result.Verified, - MetaData: r.SourceMetadata, - Raw: strings.TrimSpace(string(r.Result.Raw)), + DetectorType: r.Result.DetectorType.String(), + DecoderType: r.Result.DecoderType.String(), + Verified: r.Result.Verified, + VerificationError: r.Result.VerificationError(), + MetaData: r.SourceMetadata, + Raw: strings.TrimSpace(string(r.Result.Raw)), } meta, err := structToMap(out.MetaData.Data) @@ -44,10 +48,14 @@ func (p *PlainPrinter) Print(_ context.Context, r *detectors.ResultWithMetadata) defer p.mu.Unlock() if out.Verified { - yellowPrinter.Print("Found verified result 🐷🔑\n") + boldGreenPrinter.Print("✅ Found verified result 🐷🔑\n") + } else if out.VerificationError != nil { + printer = yellowPrinter + boldYellowPrinter.Print("⚠️ Found result - unable to verify due to error 🐷🔑❗️\n") + printer.Printf("Verification Error: %s\n", out.VerificationError) } else { printer = whitePrinter - whitePrinter.Print("Found unverified result 🐷🔑❓\n") + boldWhitePrinter.Print("Found unverified result 🐷🔑❓\n") } printer.Printf("Detector Type: %s\n", out.DetectorType) printer.Printf("Decoder Type: %s\n", out.DecoderType) @@ -105,7 +113,8 @@ func structToMap(obj any) (m map[string]map[string]any, err error) { type outputFormat struct { DetectorType, DecoderType string - Verified bool - Raw string + Verified bool + VerificationError error + Raw string *source_metadatapb.MetaData }