trufflehog/pkg/output/plain.go

119 lines
3.2 KiB
Go
Raw Normal View History

2022-01-21 02:14:16 +00:00
package output
import (
"encoding/json"
"fmt"
"sort"
2022-01-21 02:14:16 +00:00
"strings"
2023-08-02 18:38:10 +00:00
"sync"
2022-01-21 02:14:16 +00:00
"github.com/fatih/color"
"golang.org/x/text/cases"
"golang.org/x/text/language"
2022-10-06 18:55:07 +00:00
"github.com/trufflesecurity/trufflehog/v3/pkg/context"
2022-02-10 18:54:33 +00:00
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors"
"github.com/trufflesecurity/trufflehog/v3/pkg/pb/source_metadatapb"
2022-01-21 02:14:16 +00:00
)
var (
yellowPrinter = color.New(color.FgYellow)
greenPrinter = color.New(color.FgHiGreen)
boldGreenPrinter = color.New(color.Bold, color.FgHiGreen)
whitePrinter = color.New(color.FgWhite)
boldWhitePrinter = color.New(color.Bold, color.FgWhite)
2022-01-21 02:14:16 +00:00
)
// PlainPrinter is a printer that prints results in plain text format.
2023-08-02 18:38:10 +00:00
type PlainPrinter struct{ mu sync.Mutex }
func (p *PlainPrinter) Print(_ context.Context, r *detectors.ResultWithMetadata) error {
2022-01-21 02:14:16 +00:00
out := outputFormat{
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)),
2022-01-21 02:14:16 +00:00
}
meta, err := structToMap(out.MetaData.Data)
if err != nil {
return fmt.Errorf("could not marshal result: %w", err)
2022-01-21 02:14:16 +00:00
}
printer := greenPrinter
2023-08-02 18:38:10 +00:00
p.mu.Lock()
defer p.mu.Unlock()
2022-01-21 02:14:16 +00:00
if out.Verified {
boldGreenPrinter.Print("✅ Found verified result 🐷🔑\n")
2022-01-21 02:14:16 +00:00
} else {
printer = whitePrinter
boldWhitePrinter.Print("Found unverified result 🐷🔑❓\n")
if out.VerificationError != nil {
yellowPrinter.Printf("Verification issue: %s\n", out.VerificationError)
}
2022-01-21 02:14:16 +00:00
}
printer.Printf("Detector Type: %s\n", out.DetectorType)
2022-10-06 18:55:07 +00:00
printer.Printf("Decoder Type: %s\n", out.DecoderType)
printer.Printf("Raw result: %s\n", whitePrinter.Sprint(out.Raw))
for k, v := range r.Result.ExtraData {
printer.Printf(
"%s: %v\n",
cases.Title(language.AmericanEnglish).String(k),
v)
}
if r.Result.StructuredData != nil {
for idx, v := range r.Result.StructuredData.GithubSshKey {
printer.Printf("GithubSshKey %d User: %s\n", idx, v.User)
if v.PublicKeyFingerprint != "" {
printer.Printf("GithubSshKey %d Fingerprint: %s\n", idx, v.PublicKeyFingerprint)
}
}
for idx, v := range r.Result.StructuredData.TlsPrivateKey {
printer.Printf("TlsPrivateKey %d Fingerprint: %s\n", idx, v.CertificateFingerprint)
printer.Printf("TlsPrivateKey %d Verification URL: %s\n", idx, v.VerificationUrl)
printer.Printf("TlsPrivateKey %d Expiration: %d\n", idx, v.ExpirationTimestamp)
}
}
2023-09-15 11:35:15 +00:00
aggregateData := make(map[string]any)
var aggregateDataKeys []string
2022-01-21 02:14:16 +00:00
for _, data := range meta {
for k, v := range data {
aggregateDataKeys = append(aggregateDataKeys, k)
aggregateData[k] = v
2022-01-21 02:14:16 +00:00
}
}
sort.Strings(aggregateDataKeys)
for _, k := range aggregateDataKeys {
printer.Printf("%s: %v\n", cases.Title(language.AmericanEnglish).String(k), aggregateData[k])
}
2022-01-21 02:14:16 +00:00
fmt.Println("")
return nil
2022-01-21 02:14:16 +00:00
}
2023-09-15 11:35:15 +00:00
func structToMap(obj any) (m map[string]map[string]any, err error) {
2022-01-21 02:14:16 +00:00
data, err := json.Marshal(obj)
if err != nil {
return
}
err = json.Unmarshal(data, &m)
return
}
type outputFormat struct {
2022-10-06 18:55:07 +00:00
DetectorType,
DecoderType string
Verified bool
VerificationError error
Raw string
2022-01-21 02:14:16 +00:00
*source_metadatapb.MetaData
}