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

This commit is contained in:
āh̳̕mͭͭͨͩ̐e̘ͬ́͋ͬ̊̓͂d
2024-01-29 11:57:28 -05:00
committed by GitHub
parent 76fcdae3a0
commit fa1c5fa583
2 changed files with 45 additions and 27 deletions
+24 -15
View File
@@ -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 {
+21 -12
View File
@@ -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
}