Skip to content

Commit 4cffadb

Browse files
authored
feat: allow input source from pipe (#4088)
* feat: allow input source from pipe While performing a bit of experimentation, I found it was easier to allow data to flow from other segments of my machine via piping it in, rather than flushing it to disk. This can speed up the process when pre-processing large amounts of data. This allow allowed me to keep disk space low and just utilize a large ram machine. * (chore) update readme for new source pipe * (fix) pipe -> stdin per PR
1 parent 21126db commit 4cffadb

File tree

11 files changed

+807
-300
lines changed

11 files changed

+807
-300
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,12 @@ trufflehog huggingface --org <orgname> --user <username>
369369
trufflehog huggingface --model <model_id> --include-discussions --include-prs
370370
```
371371

372+
## 18. Scan stdin Input
373+
374+
```bash
375+
aws s3 cp s3://example/gzipped/data.gz - | gunzip -c | trufflehog stdin
376+
```
377+
372378
# :question: FAQ
373379

374380
- All I see is `🐷🔑🐷 TruffleHog. Unearth your secrets. 🐷🔑🐷` and the program exits, what gives?
@@ -411,6 +417,7 @@ TruffleHog has a sub-command for each source of data that you may want to scan:
411417
- postman
412418
- jenkins
413419
- elasticsearch
420+
- stdin
414421

415422
Each subcommand can have options that you can see with the `--help` flag provided to the sub command:
416423

main.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,8 @@ var (
252252
huggingfaceIncludeDiscussions = huggingfaceScan.Flag("include-discussions", "Include discussions in scan.").Bool()
253253
huggingfaceIncludePrs = huggingfaceScan.Flag("include-prs", "Include pull requests in scan.").Bool()
254254

255+
stdinInputScan = cli.Command("stdin", "Find credentials from stdin.")
256+
255257
analyzeCmd = analyzer.Command(cli)
256258
usingTUI = false
257259
)
@@ -942,6 +944,11 @@ func runSingleScan(ctx context.Context, cmd string, cfg engine.Config) (metrics,
942944
if ref, err = eng.ScanHuggingface(ctx, cfg); err != nil {
943945
return scanMetrics, fmt.Errorf("failed to scan HuggingFace: %v", err)
944946
}
947+
case stdinInputScan.FullCommand():
948+
cfg := sources.StdinConfig{}
949+
if ref, err = eng.ScanStdinInput(ctx, cfg); err != nil {
950+
return scanMetrics, fmt.Errorf("failed to scan stdin input: %v", err)
951+
}
945952
default:
946953
return scanMetrics, fmt.Errorf("invalid command: %s", cmd)
947954
}

pkg/engine/stdin.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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/stdin"
13+
)
14+
15+
// ScanStdinInput scans input that is piped into the application
16+
func (e *Engine) ScanStdinInput(ctx context.Context, c sources.StdinConfig) (sources.JobProgressRef, error) {
17+
connection := &sourcespb.Stdin{}
18+
var conn anypb.Any
19+
err := anypb.MarshalFrom(&conn, connection, proto.MarshalOptions{})
20+
if err != nil {
21+
ctx.Logger().Error(err, "failed to marshal stdin connection")
22+
return sources.JobProgressRef{}, err
23+
}
24+
25+
sourceName := "trufflehog - stdin"
26+
sourceID, jobID, _ := e.sourceManager.GetIDs(ctx, sourceName, stdin.SourceType)
27+
28+
stdinSource := &stdin.Source{}
29+
if err := stdinSource.Init(ctx, sourceName, jobID, sourceID, true, &conn, runtime.NumCPU()); err != nil {
30+
return sources.JobProgressRef{}, err
31+
}
32+
return e.sourceManager.EnumerateAndScan(ctx, sourceName, stdinSource)
33+
}

pkg/pb/source_metadatapb/source_metadata.pb.go

Lines changed: 231 additions & 161 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: 139 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)