Skip to content

Find, verify, and analyze leaked credentials

Switch to retryableHttpClient for GitHub AuthN API Client + More Logs (#995)

Yassine Ilmid720c0c0f342
diff --git a/README.md b/README.md
index 4614dc4e8..ef41f63d4 100644
--- a/README.md
+++ b/README.md
@@ -173,27 +173,19 @@ Find credentials in git repositories.
Flags:
--help Show context-sensitive help (also try --help-long and --help-man).
- --debug Run in debug mode
- --version Prints trufflehog version.
+ --debug Run in debug mode.
+ --trace Run in trace mode.
-j, --json Output in JSON format.
--json-legacy Use the pre-v3.0 JSON format. Only works with git, gitlab, and github sources.
- --concurrency=1 Number of concurrent workers.
+ --concurrency=10 Number of concurrent workers.
--no-verification Don't verify the results.
--only-verified Only output verified results.
--filter-unverified Only output first unverified result per chunk per detector if there are more than one results.
+ --config=CONFIG Path to configuration file.
--print-avg-detector-time Print the average time spent on each detector.
--no-update Don't check for updates.
- -i, --include-paths=INCLUDE-PATHS
- Path to file with newline separated regexes for files to include in scan.
- -x, --exclude-paths=EXCLUDE-PATHS
- Path to file with newline separated regexes for files to exclude in scan.
- --since-commit=SINCE-COMMIT
- Commit to start scan from.
- --branch=BRANCH Branch to scan.
- --max-depth=MAX-DEPTH Maximum depth of commits to scan.
- --allow No-op flag for backwards compat.
- --entropy No-op flag for backwards compat.
- --regex No-op flag for backwards compat.
+ --fail Exit with code 183 if results are found.
+ --version Show application version.
Args:
<uri> Git repository URL. https://, file://, or ssh:// schema expected.
diff --git a/pkg/common/http.go b/pkg/common/http.go
index 6b75b4666..22775cff4 100644
--- a/pkg/common/http.go
+++ b/pkg/common/http.go
@@ -9,6 +9,7 @@ import (
"time"
"github.com/hashicorp/go-retryablehttp"
+ "github.com/sirupsen/logrus"
)
var caCerts = []string{
@@ -93,7 +94,7 @@ func NewCustomTransport(T http.RoundTripper) *CustomTransport {
func PinnedRetryableHttpClient() *http.Client {
httpClient := retryablehttp.NewClient()
- httpClient.Logger = nil
+ httpClient.Logger = logrus.WithField("name", "PinnedRetryableHttpClient").WithField("level", logrus.DebugLevel)
httpClient.HTTPClient.Transport = NewCustomTransport(&http.Transport{
TLSClientConfig: &tls.Config{
RootCAs: PinnedCertPool(),
@@ -115,7 +116,7 @@ func PinnedRetryableHttpClient() *http.Client {
func RetryableHttpClient() *http.Client {
httpClient := retryablehttp.NewClient()
httpClient.RetryMax = 3
- httpClient.Logger = nil
+ httpClient.Logger = logrus.WithField("name", "RetryableHttpClient").WithField("level", logrus.DebugLevel)
httpClient.HTTPClient.Timeout = 3 * time.Second
httpClient.HTTPClient.Transport = NewCustomTransport(nil)
return httpClient.StandardClient()
@@ -124,7 +125,7 @@ func RetryableHttpClient() *http.Client {
func RetryableHttpClientTimeout(timeOutSeconds int64) *http.Client {
httpClient := retryablehttp.NewClient()
httpClient.RetryMax = 3
- httpClient.Logger = nil
+ httpClient.Logger = logrus.WithField("name", "RetryableHttpClientTimeout").WithField("level", logrus.DebugLevel)
httpClient.HTTPClient.Timeout = time.Duration(timeOutSeconds) * time.Second
httpClient.HTTPClient.Transport = NewCustomTransport(nil)
return httpClient.StandardClient()
diff --git a/pkg/sources/git/git.go b/pkg/sources/git/git.go
index 192d257dc..1bdc3e12a 100644
--- a/pkg/sources/git/git.go
+++ b/pkg/sources/git/git.go
@@ -560,8 +560,16 @@ func (s *Git) ScanRepo(ctx context.Context, repo *git.Repository, repoPath strin
ctx.Logger().V(1).Info("error scanning unstaged changes", "error", err)
}
+ // We're logging time, but the repoPath is usally a dynamically generated folder in /tmp
+ // To make this duration logging useful, we need to log the remote as well
+ remotes, _ := repo.Remotes()
+ repoUrl := "Could not get remote for repo"
+ if len(remotes) != 0 {
+ repoUrl = getSafeRemoteURL(repo, remotes[0].Config().Name)
+ }
+
scanTime := time.Now().UnixNano() - start
- ctx.Logger().V(1).Info("scanning git repo complete", "path", repoPath, "time", scanTime)
+ ctx.Logger().V(1).Info("scanning git repo complete", "Repo", repoUrl, "path", repoPath, "time", scanTime)
return nil
}
diff --git a/pkg/sources/github/github.go b/pkg/sources/github/github.go
index 9f1bd1e24..2c332dc39 100644
--- a/pkg/sources/github/github.go
+++ b/pkg/sources/github/github.go
@@ -134,7 +134,7 @@ func (s *Source) Init(aCtx context.Context, name string, jobID, sourceID int64,
s.jobPool = &errgroup.Group{}
s.jobPool.SetLimit(concurrency)
- s.httpClient = common.SaneHttpClient()
+ s.httpClient = common.RetryableHttpClientTimeout(60)
s.apiClient = github.NewClient(s.httpClient)
var conn sourcespb.GitHub
@@ -280,17 +280,20 @@ func (s *Source) enumerateWithToken(ctx context.Context, apiEndpoint, token stri
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: token},
)
- tc := oauth2.NewClient(context.TODO(), ts)
+ s.httpClient.Transport = &oauth2.Transport{
+ Base: s.httpClient.Transport,
+ Source: oauth2.ReuseTokenSource(nil, ts),
+ }
var err error
// If we're using public Github, make a regular client.
// Otherwise, make an enterprise client.
var isGHE bool
if apiEndpoint == "https://api.github.com" {
- s.apiClient = github.NewClient(tc)
+ s.apiClient = github.NewClient(s.httpClient)
} else {
isGHE = true
- s.apiClient, err = github.NewEnterpriseClient(apiEndpoint, apiEndpoint, tc)
+ s.apiClient, err = github.NewEnterpriseClient(apiEndpoint, apiEndpoint, s.httpClient)
if err != nil {
return errors.New(err)
}