trufflehog/pkg/output/plain.go

112 lines
2.9 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)
whitePrinter = color.New(color.FgWhite)
)
// 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(),
2022-10-06 18:55:07 +00:00
DecoderType: r.Result.DecoderType.String(),
2022-01-21 02:14:16 +00:00
Verified: r.Result.Verified,
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 {
yellowPrinter.Print("Found verified result 🐷🔑\n")
} else {
printer = whitePrinter
whitePrinter.Print("Found unverified result 🐷🔑❓\n")
}
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)
}
}
aggregateData := make(map[string]interface{})
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
}
func structToMap(obj interface{}) (m map[string]map[string]interface{}, err error) {
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
Raw string
2022-01-21 02:14:16 +00:00
*source_metadatapb.MetaData
}