Add decoder type to results. (#835)

This commit is contained in:
ahrav
2022-10-06 11:55:07 -07:00
committed by GitHub
parent 3f6e5b44c9
commit 128002885a
6 changed files with 1050 additions and 960 deletions
+3 -1
View File
@@ -26,7 +26,9 @@ type Detector interface {
type Result struct {
// DetectorType is the type of Detector.
DetectorType detectorspb.DetectorType
Verified bool
// DecoderType is the type of Decoder.
DecoderType detectorspb.DecoderType
Verified bool
// Raw contains the raw secret identifier data. Prefer IDs over secrets since it is used for deduping after hashing.
Raw []byte
// RawV2 contains the raw secret identifier that is a combination of both the ID and the secret.
+12
View File
@@ -14,6 +14,7 @@ import (
"github.com/trufflesecurity/trufflehog/v3/pkg/context"
"github.com/trufflesecurity/trufflehog/v3/pkg/decoders"
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors"
"github.com/trufflesecurity/trufflehog/v3/pkg/pb/detectorspb"
"github.com/trufflesecurity/trufflehog/v3/pkg/pb/source_metadatapb"
"github.com/trufflesecurity/trufflehog/v3/pkg/pb/sourcespb"
"github.com/trufflesecurity/trufflehog/v3/pkg/sources"
@@ -163,6 +164,16 @@ func (e *Engine) detectorWorker(ctx context.Context) {
for chunk := range e.chunks {
fragStart, mdLine := fragmentFirstLine(chunk)
for _, decoder := range e.decoders {
var decoderType detectorspb.DecoderType
switch decoder.(type) {
case *decoders.Plain:
decoderType = detectorspb.DecoderType_PLAIN
case *decoders.Base64:
decoderType = detectorspb.DecoderType_BASE64
default:
logrus.Warnf("unknown decoder type: %T", decoder)
decoderType = detectorspb.DecoderType_UNKNOWN
}
decoded := decoder.FromChunk(chunk)
if decoded == nil {
continue
@@ -201,6 +212,7 @@ func (e *Engine) detectorWorker(ctx context.Context) {
offset := FragmentLineOffset(chunk, &result)
*mdLine = fragStart + offset
}
result.DecoderType = decoderType
e.results <- detectors.CopyMetadata(chunk, result)
}
+5 -1
View File
@@ -5,6 +5,7 @@ import (
"fmt"
"github.com/sirupsen/logrus"
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors"
"github.com/trufflesecurity/trufflehog/v3/pkg/pb/detectorspb"
"github.com/trufflesecurity/trufflehog/v3/pkg/pb/source_metadatapb"
@@ -25,7 +26,9 @@ func PrintJSON(r *detectors.ResultWithMetadata) {
DetectorType detectorspb.DetectorType
// DetectorName is the string name of the DetectorType.
DetectorName string
Verified bool
// DecoderName is the string name of the DecoderType.
DecoderName string
Verified bool
// Raw contains the raw secret data.
Raw string
// Redacted contains the redacted version of the raw secret identification data for display purposes.
@@ -40,6 +43,7 @@ func PrintJSON(r *detectors.ResultWithMetadata) {
SourceName: r.SourceName,
DetectorType: r.DetectorType,
DetectorName: r.DetectorType.String(),
DecoderName: r.DecoderType.String(),
Verified: r.Verified,
Raw: string(r.Raw),
Redacted: r.Redacted,
+7 -3
View File
@@ -7,6 +7,7 @@ import (
"github.com/fatih/color"
"github.com/sirupsen/logrus"
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors"
"github.com/trufflesecurity/trufflehog/v3/pkg/pb/source_metadatapb"
)
@@ -20,6 +21,7 @@ var (
func PrintPlainOutput(r *detectors.ResultWithMetadata) {
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)),
@@ -39,6 +41,7 @@ func PrintPlainOutput(r *detectors.ResultWithMetadata) {
whitePrinter.Print("Found unverified result 🐷🔑❓\n")
}
printer.Printf("Detector Type: %s\n", out.DetectorType)
printer.Printf("Decoder Type: %s\n", out.DecoderType)
printer.Printf("Raw result: %s\n", whitePrinter.Sprint(out.Raw))
for _, data := range meta {
for k, v := range data {
@@ -58,8 +61,9 @@ func structToMap(obj interface{}) (m map[string]map[string]interface{}, err erro
}
type outputFormat struct {
DetectorType string
Verified bool
Raw string
DetectorType,
DecoderType string
Verified bool
Raw string
*source_metadatapb.MetaData
}
File diff suppressed because it is too large Load Diff
@@ -97,6 +97,8 @@ func (m *Result) validate(all bool) error {
// no validation rules for HashV2
// no validation rules for Decoder
if len(errors) > 0 {
return ResultMultiError(errors)
}