Skip to content

Commit c563a06

Browse files
authored
Add a new NDJSON / JSONL input source (trufflesecurity#4721)
This adds a new input source to TruffleHog, accessible via `trufflehog json-enumerator`. This input source requires a list of filenames, each of which is an NDJSON-formatted sequence of objects that take one of two forms: Form 1: `{"data": "utf-8 string", "metadata": <non-null JSON value>}` Form 2: `{"data_b64": "base64-encoded bytestring", "metadata": <non-null JSON value>}` The `data` / `data_b64` field specifies the content to be scanned. The `metadata` field is arbitrary, and is simply propagated downstream with scan results from the corresponding content. Note that although `trufflehog json-enumerator` requires a list of filenames to be given, the NDJSON data that you wish to scan may not need to be first written to disk. On Linux and macOS, at least, you can use shell process substitution to set up a named pipe from a producer process, like `trufflehog json-enumerator <(some-program-that-emits-ndjson)`.
1 parent 6961f2b commit c563a06

File tree

11 files changed

+1129
-331
lines changed

11 files changed

+1129
-331
lines changed

main.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,9 @@ var (
270270
stdinInputScan = cli.Command("stdin", "Find credentials from stdin.")
271271
multiScanScan = cli.Command("multi-scan", "Find credentials in multiple sources defined in configuration.")
272272

273+
jsonEnumeratorScan = cli.Command("json-enumerator", "Find credentials from a JSON enumerator input.")
274+
jsonEnumeratorPaths = jsonEnumeratorScan.Arg("path", "Path to JSON enumerator file to scan.").Strings()
275+
273276
analyzeCmd = analyzer.Command(cli)
274277
usingTUI = false
275278
)
@@ -1114,6 +1117,13 @@ func runSingleScan(ctx context.Context, cmd string, cfg engine.Config) (metrics,
11141117
} else {
11151118
refs = []sources.JobProgressRef{ref}
11161119
}
1120+
case jsonEnumeratorScan.FullCommand():
1121+
cfg := sources.JSONEnumeratorConfig{Paths: *jsonEnumeratorPaths}
1122+
if ref, err := eng.ScanJSONEnumeratorInput(ctx, cfg); err != nil {
1123+
return scanMetrics, fmt.Errorf("failed to scan JSON enumerator input: %v", err)
1124+
} else {
1125+
refs = []sources.JobProgressRef{ref}
1126+
}
11171127
default:
11181128
return scanMetrics, fmt.Errorf("invalid command: %s", cmd)
11191129
}

pkg/engine/json_enumerator.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package engine
2+
3+
import (
4+
"runtime"
5+
6+
"google.golang.org/protobuf/proto"
7+
"google.golang.org/protobuf/types/known/anypb"
8+
9+
"github.com/trufflesecurity/trufflehog/v3/pkg/context"
10+
"github.com/trufflesecurity/trufflehog/v3/pkg/pb/sourcespb"
11+
"github.com/trufflesecurity/trufflehog/v3/pkg/sources"
12+
"github.com/trufflesecurity/trufflehog/v3/pkg/sources/json_enumerator"
13+
)
14+
15+
// ScanJSONEnumeratorInput scans input that is in JSON Enumerator format
16+
func (e *Engine) ScanJSONEnumeratorInput(
17+
ctx context.Context,
18+
c sources.JSONEnumeratorConfig,
19+
) (sources.JobProgressRef, error) {
20+
connection := &sourcespb.JSONEnumerator{
21+
Paths: c.Paths,
22+
}
23+
var conn anypb.Any
24+
err := anypb.MarshalFrom(&conn, connection, proto.MarshalOptions{})
25+
if err != nil {
26+
ctx.Logger().Error(err, "failed to marshal JSON enumerator connection")
27+
return sources.JobProgressRef{}, err
28+
}
29+
30+
sourceName := "trufflehog - JSON enumerator"
31+
sourceID, jobID, err := e.sourceManager.GetIDs(ctx, sourceName, json_enumerator.SourceType)
32+
if err != nil {
33+
ctx.Logger().Error(err, "failed to get IDs from source manager")
34+
return sources.JobProgressRef{}, err
35+
}
36+
37+
source := &json_enumerator.Source{}
38+
err = source.Init(ctx, sourceName, jobID, sourceID, true, &conn, runtime.NumCPU())
39+
if err != nil {
40+
return sources.JobProgressRef{}, err
41+
}
42+
return e.sourceManager.EnumerateAndScan(ctx, sourceName, source)
43+
}

pkg/pb/source_metadatapb/source_metadata.pb.go

Lines changed: 251 additions & 168 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/pb/source_metadatapb/source_metadata.pb.validate.go

Lines changed: 143 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)