Skip to content

Find, verify, and analyze leaked credentials

Add source fields to Init logs in single-source CLI commands (#5466)

Kashif Khan87adc84b3f12
diff --git a/pkg/engine/circleci.go b/pkg/engine/circleci.go
index c2ca134e5..21366d82a 100644
--- a/pkg/engine/circleci.go
+++ b/pkg/engine/circleci.go
@@ -29,6 +29,7 @@ func (e *Engine) ScanCircleCI(ctx context.Context, token string) (sources.JobPro
sourceName := "trufflehog - Circle CI"
sourceID, jobID, _ := e.sourceManager.GetIDs(ctx, sourceName, circleci.SourceType)
+ ctx = withSourceFields(ctx, sourceName, circleci.SourceType, sourceID, jobID)
circleSource := &circleci.Source{}
if err := circleSource.Init(ctx, "trufflehog - Circle CI", jobID, sourceID, true, &conn, runtime.NumCPU()); err != nil {
diff --git a/pkg/engine/docker.go b/pkg/engine/docker.go
index c269d0564..d17a930da 100644
--- a/pkg/engine/docker.go
+++ b/pkg/engine/docker.go
@@ -39,6 +39,7 @@ func (e *Engine) ScanDocker(ctx context.Context, c sources.DockerConfig) (source
sourceName := "trufflehog - docker"
sourceID, jobID, _ := e.sourceManager.GetIDs(ctx, sourceName, docker.SourceType)
+ ctx = withSourceFields(ctx, sourceName, docker.SourceType, sourceID, jobID)
dockerSource := &docker.Source{}
if err := dockerSource.Init(ctx, sourceName, jobID, sourceID, true, &conn, runtime.NumCPU()); err != nil {
diff --git a/pkg/engine/elasticsearch.go b/pkg/engine/elasticsearch.go
index 10bdf7193..252edf9f2 100644
--- a/pkg/engine/elasticsearch.go
+++ b/pkg/engine/elasticsearch.go
@@ -36,6 +36,7 @@ func (e *Engine) ScanElasticsearch(ctx context.Context, c sources.ElasticsearchC
sourceName := "trufflehog - Elasticsearch"
sourceID, jobID, _ := e.sourceManager.GetIDs(ctx, sourceName, elasticsearch.SourceType)
+ ctx = withSourceFields(ctx, sourceName, elasticsearch.SourceType, sourceID, jobID)
elasticsearchSource := &elasticsearch.Source{}
if err := elasticsearchSource.Init(ctx, sourceName, jobID, sourceID, true, &conn, runtime.NumCPU()); err != nil {
diff --git a/pkg/engine/filesystem.go b/pkg/engine/filesystem.go
index 126424060..673e88075 100644
--- a/pkg/engine/filesystem.go
+++ b/pkg/engine/filesystem.go
@@ -29,6 +29,7 @@ func (e *Engine) ScanFileSystem(ctx context.Context, c sources.FilesystemConfig)
sourceName := "trufflehog - filesystem"
sourceID, jobID, _ := e.sourceManager.GetIDs(ctx, sourceName, filesystem.SourceType)
+ ctx = withSourceFields(ctx, sourceName, filesystem.SourceType, sourceID, jobID)
fileSystemSource := &filesystem.Source{}
if err := fileSystemSource.Init(ctx, sourceName, jobID, sourceID, true, &conn, runtime.NumCPU()); err != nil {
diff --git a/pkg/engine/gcs.go b/pkg/engine/gcs.go
index 7ad54f273..d9f793473 100644
--- a/pkg/engine/gcs.go
+++ b/pkg/engine/gcs.go
@@ -46,6 +46,7 @@ func (e *Engine) ScanGCS(ctx context.Context, c sources.GCSConfig) (sources.JobP
sourceName := "trufflehog - gcs"
sourceID, jobID, _ := e.sourceManager.GetIDs(ctx, sourceName, gcs.SourceType)
+ ctx = withSourceFields(ctx, sourceName, gcs.SourceType, sourceID, jobID)
gcsSource := &gcs.Source{}
if err := gcsSource.Init(ctx, sourceName, jobID, sourceID, true, &conn, int(c.Concurrency)); err != nil {
diff --git a/pkg/engine/git.go b/pkg/engine/git.go
index e232312f4..150733153 100644
--- a/pkg/engine/git.go
+++ b/pkg/engine/git.go
@@ -38,6 +38,7 @@ func (e *Engine) ScanGit(ctx context.Context, c sources.GitConfig) (sources.JobP
sourceName := "trufflehog - git"
sourceID, jobID, _ := e.sourceManager.GetIDs(ctx, sourceName, git.SourceType)
+ ctx = withSourceFields(ctx, sourceName, git.SourceType, sourceID, jobID)
gitSource := &git.Source{}
if err := gitSource.Init(ctx, sourceName, jobID, sourceID, true, &conn, runtime.NumCPU()); err != nil {
diff --git a/pkg/engine/github.go b/pkg/engine/github.go
index 9ccc966ba..98cde433a 100644
--- a/pkg/engine/github.go
+++ b/pkg/engine/github.go
@@ -60,6 +60,7 @@ func (e *Engine) ScanGitHub(ctx context.Context, c sources.GithubConfig) (source
sourceName := "trufflehog - github"
sourceID, jobID, _ := e.sourceManager.GetIDs(ctx, sourceName, github.SourceType)
+ ctx = withSourceFields(ctx, sourceName, github.SourceType, sourceID, jobID)
githubSource := &github.Source{}
if err := githubSource.Init(ctx, sourceName, jobID, sourceID, true, &conn, c.Concurrency); err != nil {
diff --git a/pkg/engine/github_experimental.go b/pkg/engine/github_experimental.go
index 0354a3637..a663d1f92 100644
--- a/pkg/engine/github_experimental.go
+++ b/pkg/engine/github_experimental.go
@@ -12,7 +12,6 @@ import (
"github.com/trufflesecurity/trufflehog/v3/pkg/pb/sourcespb"
"github.com/trufflesecurity/trufflehog/v3/pkg/sources"
"github.com/trufflesecurity/trufflehog/v3/pkg/sources/git"
- "github.com/trufflesecurity/trufflehog/v3/pkg/sources/github"
"github.com/trufflesecurity/trufflehog/v3/pkg/sources/github_experimental"
)
@@ -53,7 +52,8 @@ func (e *Engine) ScanGitHubExperimental(ctx context.Context, c sources.GitHubExp
scanOptions := git.NewScanOptions(opts...)
sourceName := "trufflehog - github experimental (alpha release)"
- sourceID, jobID, _ := e.sourceManager.GetIDs(ctx, sourceName, github.SourceType)
+ sourceID, jobID, _ := e.sourceManager.GetIDs(ctx, sourceName, github_experimental.SourceType)
+ ctx = withSourceFields(ctx, sourceName, github_experimental.SourceType, sourceID, jobID)
githubExperimentalSource := &github_experimental.Source{}
if err := githubExperimentalSource.Init(ctx, sourceName, jobID, sourceID, true, &conn, runtime.NumCPU()); err != nil {
diff --git a/pkg/engine/gitlab.go b/pkg/engine/gitlab.go
index 810057058..36fe84818 100644
--- a/pkg/engine/gitlab.go
+++ b/pkg/engine/gitlab.go
@@ -75,6 +75,7 @@ func (e *Engine) ScanGitLab(ctx context.Context, c sources.GitlabConfig) (source
sourceName := "trufflehog - gitlab"
sourceID, jobID, _ := e.sourceManager.GetIDs(ctx, sourceName, gitlab.SourceType)
+ ctx = withSourceFields(ctx, sourceName, gitlab.SourceType, sourceID, jobID)
gitlabSource := &gitlab.Source{}
if err := gitlabSource.Init(ctx, sourceName, jobID, sourceID, true, &conn, runtime.NumCPU()); err != nil {
diff --git a/pkg/engine/huggingface.go b/pkg/engine/huggingface.go
index e735be54f..0c990ba26 100644
--- a/pkg/engine/huggingface.go
+++ b/pkg/engine/huggingface.go
@@ -79,6 +79,7 @@ func (e *Engine) ScanHuggingface(ctx context.Context, c HuggingfaceConfig) (sour
sourceName := "trufflehog - huggingface"
sourceID, jobID, _ := e.sourceManager.GetIDs(ctx, sourceName, sourcespb.SourceType_SOURCE_TYPE_HUGGINGFACE)
+ ctx = withSourceFields(ctx, sourceName, sourcespb.SourceType_SOURCE_TYPE_HUGGINGFACE, sourceID, jobID)
huggingfaceSource := &huggingface.Source{}
if err := huggingfaceSource.Init(ctx, sourceName, jobID, sourceID, true, &conn, c.Concurrency); err != nil {
diff --git a/pkg/engine/jenkins.go b/pkg/engine/jenkins.go
index b488951a2..1beec54fd 100644
--- a/pkg/engine/jenkins.go
+++ b/pkg/engine/jenkins.go
@@ -72,6 +72,7 @@ func (e *Engine) ScanJenkins(ctx context.Context, jenkinsConfig JenkinsConfig) (
sourceName := "trufflehog - Jenkins"
sourceID, jobID, _ := e.sourceManager.GetIDs(ctx, sourceName, jenkins.SourceType)
+ ctx = withSourceFields(ctx, sourceName, jenkins.SourceType, sourceID, jobID)
jenkinsSource := &jenkins.Source{}
if err := jenkinsSource.Init(ctx, "trufflehog - Jenkins", jobID, sourceID, true, &conn, runtime.NumCPU()); err != nil {
diff --git a/pkg/engine/json_enumerator.go b/pkg/engine/json_enumerator.go
index be30dbaf5..24829dc34 100644
--- a/pkg/engine/json_enumerator.go
+++ b/pkg/engine/json_enumerator.go
@@ -33,6 +33,7 @@ func (e *Engine) ScanJSONEnumeratorInput(
ctx.Logger().Error(err, "failed to get IDs from source manager")
return sources.JobProgressRef{}, err
}
+ ctx = withSourceFields(ctx, sourceName, json_enumerator.SourceType, sourceID, jobID)
source := &json_enumerator.Source{}
err = source.Init(ctx, sourceName, jobID, sourceID, true, &conn, runtime.NumCPU())
diff --git a/pkg/engine/postman.go b/pkg/engine/postman.go
index fc5e6ae08..8e2450d50 100644
--- a/pkg/engine/postman.go
+++ b/pkg/engine/postman.go
@@ -48,6 +48,7 @@ func (e *Engine) ScanPostman(ctx context.Context, c sources.PostmanConfig) (sour
sourceName := "trufflehog - postman"
sourceID, jobID, _ := e.sourceManager.GetIDs(ctx, sourceName, postman.SourceType)
+ ctx = withSourceFields(ctx, sourceName, postman.SourceType, sourceID, jobID)
postmanSource := &postman.Source{
DetectorKeywords: e.AhoCorasickCoreKeywords(),
diff --git a/pkg/engine/s3.go b/pkg/engine/s3.go
index fdee09da8..15e77b703 100644
--- a/pkg/engine/s3.go
+++ b/pkg/engine/s3.go
@@ -71,6 +71,7 @@ func (e *Engine) ScanS3(ctx context.Context, c sources.S3Config) (sources.JobPro
sourceName := "trufflehog - s3"
sourceID, jobID, _ := e.sourceManager.GetIDs(ctx, sourceName, s3.SourceType)
+ ctx = withSourceFields(ctx, sourceName, s3.SourceType, sourceID, jobID)
s3Source := &s3.Source{}
if err := s3Source.Init(ctx, sourceName, jobID, sourceID, true, &conn, runtime.NumCPU()); err != nil {
diff --git a/pkg/engine/scan.go b/pkg/engine/scan.go
index 85e1e0184..e50a829f3 100644
--- a/pkg/engine/scan.go
+++ b/pkg/engine/scan.go
@@ -16,15 +16,7 @@ func (e *Engine) ScanConfig(ctx context.Context, configuredSources ...sources.Co
for _, configuredSource := range configuredSources {
sourceID, jobID, _ := e.sourceManager.GetIDs(ctx, configuredSource.Name, configuredSource.SourceType())
- // Add the source fields here, before Init, so lines logged during Init
- // say which source and job they came from too. The source manager adds
- // the same fields later for the scan, but skips any already set here.
- sourceCtx := context.WithValues(ctx,
- "source_name", configuredSource.Name,
- "source_type", configuredSource.SourceType().String(),
- "source_id", sourceID,
- "job_id", jobID,
- )
+ sourceCtx := withSourceFields(ctx, configuredSource.Name, configuredSource.SourceType(), sourceID, jobID)
// A source with debug on gets a context that writes all of its debug
// logs. Both Init and the scan use it, so every log from this source is
diff --git a/pkg/engine/source_fields.go b/pkg/engine/source_fields.go
new file mode 100644
index 000000000..9ed93077c
--- /dev/null
+++ b/pkg/engine/source_fields.go
@@ -0,0 +1,26 @@
+package engine
+
+import (
+ "github.com/trufflesecurity/trufflehog/v3/pkg/context"
+ "github.com/trufflesecurity/trufflehog/v3/pkg/pb/sourcespb"
+ "github.com/trufflesecurity/trufflehog/v3/pkg/sources"
+)
+
+// withSourceFields adds the source fields to the context. Call it before Init,
+// so lines a source logs during Init say which source and job they came from too.
+// The source manager adds the same fields later for the scan, but skips any
+// already set here.
+func withSourceFields(
+ ctx context.Context,
+ sourceName string,
+ sourceType sourcespb.SourceType,
+ sourceID sources.SourceID,
+ jobID sources.JobID,
+) context.Context {
+ return context.WithValues(ctx,
+ "source_name", sourceName,
+ "source_type", sourceType.String(),
+ "source_id", sourceID,
+ "job_id", jobID,
+ )
+}
diff --git a/pkg/engine/source_fields_test.go b/pkg/engine/source_fields_test.go
new file mode 100644
index 000000000..39ff2d0bc
--- /dev/null
+++ b/pkg/engine/source_fields_test.go
@@ -0,0 +1,68 @@
+package engine
+
+import (
+ "os/exec"
+ "testing"
+ "time"
+
+ "github.com/stretchr/testify/assert"
+ "github.com/stretchr/testify/require"
+
+ "github.com/trufflesecurity/trufflehog/v3/pkg/context"
+ "github.com/trufflesecurity/trufflehog/v3/pkg/decoders"
+ "github.com/trufflesecurity/trufflehog/v3/pkg/log/logtest"
+ "github.com/trufflesecurity/trufflehog/v3/pkg/sources"
+)
+
+func TestScanGit_InitLogsHaveSourceFields(t *testing.T) {
+ // make a small local repo with one commit to scan
+ repoPath := t.TempDir()
+ for _, args := range [][]string{
+ {"init", "-q"},
+ {"-c", "user.name=test", "-c", "[email protected]", "commit", "-q", "--allow-empty", "-m", "first"},
+ } {
+ out, err := exec.Command("git", append([]string{"-C", repoPath}, args...)...).CombinedOutput()
+ require.NoError(t, err, string(out))
+ }
+
+ ctx, logs := logtest.DebugContext()
+ ctx, cancel := context.WithTimeout(ctx, 30*time.Second)
+ defer cancel()
+
+ e, err := NewEngine(ctx, &Config{
+ Concurrency: 1,
+ Decoders: decoders.DefaultDecoders(),
+ SourceManager: sources.NewManager(),
+ Dispatcher: NewPrinterDispatcher(new(discardPrinter)),
+ })
+ require.NoError(t, err)
+ e.Start(ctx)
+
+ _, err = e.ScanGit(ctx, sources.GitConfig{URI: "file://" + repoPath})
+ require.NoError(t, err)
+ require.NoError(t, e.Finish(ctx))
+
+ sourceFields := []string{"source_name", "source_type", "source_id", "job_id"}
+
+ // the line the git source logs during Init has all the source fields
+ initLines := logs.FilterMessage("git source initialized").All()
+ require.Len(t, initLines, 1)
+ for _, key := range sourceFields {
+ assert.Contains(t, initLines[0].ContextMap(), key)
+ }
+ assert.Equal(t, "trufflehog - git", initLines[0].ContextMap()["source_name"])
+
+ // the source manager adds the same fields for the scan,
+ // so check no line ends up with any of them twice
+ for _, entry := range logs.All() {
+ for _, key := range sourceFields {
+ count := 0
+ for _, field := range entry.Context {
+ if field.Key == key {
+ count++
+ }
+ }
+ assert.LessOrEqual(t, count, 1, "%s on %q", key, entry.Message)
+ }
+ }
+}
diff --git a/pkg/engine/stdin.go b/pkg/engine/stdin.go
index 65e336241..581346489 100644
--- a/pkg/engine/stdin.go
+++ b/pkg/engine/stdin.go
@@ -24,6 +24,7 @@ func (e *Engine) ScanStdinInput(ctx context.Context, c sources.StdinConfig) (sou
sourceName := "trufflehog - stdin"
sourceID, jobID, _ := e.sourceManager.GetIDs(ctx, sourceName, stdin.SourceType)
+ ctx = withSourceFields(ctx, sourceName, stdin.SourceType, sourceID, jobID)
stdinSource := &stdin.Source{}
if err := stdinSource.Init(ctx, sourceName, jobID, sourceID, true, &conn, runtime.NumCPU()); err != nil {
diff --git a/pkg/engine/syslog.go b/pkg/engine/syslog.go
index a857fd90c..6ff783020 100644
--- a/pkg/engine/syslog.go
+++ b/pkg/engine/syslog.go
@@ -43,6 +43,7 @@ func (e *Engine) ScanSyslog(ctx context.Context, c sources.SyslogConfig) (source
sourceName := "trufflehog - syslog"
sourceID, jobID, _ := e.sourceManager.GetIDs(ctx, sourceName, syslog.SourceType)
+ ctx = withSourceFields(ctx, sourceName, syslog.SourceType, sourceID, jobID)
syslogSource := &syslog.Source{}
if err := syslogSource.Init(ctx, sourceName, jobID, sourceID, true, &conn, c.Concurrency); err != nil {
return sources.JobProgressRef{}, err
diff --git a/pkg/engine/travisci.go b/pkg/engine/travisci.go
index 790ced12d..0990b393f 100644
--- a/pkg/engine/travisci.go
+++ b/pkg/engine/travisci.go
@@ -29,6 +29,7 @@ func (e *Engine) ScanTravisCI(ctx context.Context, token string) (sources.JobPro
sourceName := "trufflehog - Travis CI"
sourceID, jobID, _ := e.sourceManager.GetIDs(ctx, sourceName, travisci.SourceType)
+ ctx = withSourceFields(ctx, sourceName, travisci.SourceType, sourceID, jobID)
travisSource := &travisci.Source{}
if err := travisSource.Init(ctx, sourceName, jobID, sourceID, true, &conn, runtime.NumCPU()); err != nil {