Skip to content

Commit

Permalink
updates to plain and json printing to include verification error (#2335)
Browse files Browse the repository at this point in the history
  • Loading branch information
0x1 authored Jan 29, 2024
1 parent 76fcdae commit fa1c5fa
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 27 deletions.
39 changes: 24 additions & 15 deletions pkg/output/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand All @@ -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 {
Expand Down
33 changes: 21 additions & 12 deletions pkg/output/plain.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,25 @@ 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.
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)
Expand All @@ -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)
Expand Down Expand Up @@ -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
}

0 comments on commit fa1c5fa

Please # to comment.