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

updates to plain and json printing to include verification error #2335

Merged
merged 1 commit into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
Loading