Skip to content

Find, verify, and analyze leaked credentials

Fix how we scan orgs (#1327)

ahrav030c093392a0
diff --git a/pkg/sources/github/github.go b/pkg/sources/github/github.go
index 770f90ef9..98520c653 100644
--- a/pkg/sources/github/github.go
+++ b/pkg/sources/github/github.go
@@ -52,7 +52,6 @@ type Source struct {
jobID int64
verify bool
repos []string
- orgs []string
members []string
orgsCache cache.Cache
filteredRepoCache *filteredRepoCache
@@ -226,9 +225,8 @@ func (s *Source) Init(aCtx context.Context, name string, jobID, sourceID int64,
s.filteredRepoCache.Set(r, r)
}
- s.orgs = s.conn.Organizations
s.orgsCache = memory.New()
- for _, org := range s.orgs {
+ for _, org := range s.conn.Organizations {
s.orgsCache.Set(org, org)
}
@@ -373,6 +371,7 @@ func (s *Source) enumerate(ctx context.Context, apiEndpoint string) (*github.Cli
}
s.repos = s.filteredRepoCache.Values()
+ s.log.Info("Completed enumeration", "num_repos", len(s.repos), "num_orgs", s.orgsCache.Count(), "num_members", len(s.memberCache))
// We must sort the repos so we can resume later if necessary.
sort.Strings(s.repos)
@@ -381,11 +380,11 @@ func (s *Source) enumerate(ctx context.Context, apiEndpoint string) (*github.Cli
func (s *Source) enumerateUnauthenticated(ctx context.Context) {
s.apiClient = github.NewClient(s.httpClient)
- if len(s.orgs) > unauthGithubOrgRateLimt {
+ if s.orgsCache.Count() > unauthGithubOrgRateLimt {
s.log.Info("You may experience rate limiting when using the unauthenticated GitHub api. Consider using an authenticated scan instead.")
}
- for _, org := range s.orgs {
+ for _, org := range s.orgsCache.Keys() {
if err := s.getReposByOrg(ctx, org); err != nil {
s.log.Error(err, "error fetching repos for org or user")
}
@@ -453,9 +452,9 @@ func (s *Source) enumerateWithToken(ctx context.Context, apiEndpoint, token stri
break
}
- if len(s.orgs) > 0 {
+ if s.orgsCache.Count() > 0 {
specificScope = true
- for _, org := range s.orgs {
+ for _, org := range s.orgsCache.Keys() {
logger := s.log.WithValues("org", org)
if err := s.getReposByOrg(ctx, org); err != nil {
logger.Error(err, "error fetching repos for org")
@@ -509,12 +508,11 @@ func (s *Source) enumerateWithToken(ctx context.Context, apiEndpoint, token stri
s.log.Error(err, "error fetching gists", "user", ghUser.GetLogin())
}
- s.log.Info("Completed enumeration", "num_repos", len(s.repos), "num_orgs", len(s.orgs), "num_members", len(s.members))
return nil
}
if s.conn.ScanUsers {
- s.log.Info("Adding repos", "members", len(s.members), "orgs", len(s.orgs))
+ s.log.Info("Adding repos", "members", len(s.members), "orgs", s.orgsCache.Count())
s.addReposForMembers(ctx)
return nil
}
diff --git a/pkg/sources/github/github_integration_test.go b/pkg/sources/github/github_integration_test.go
index 341de98fe..8fc7dd80f 100644
--- a/pkg/sources/github/github_integration_test.go
+++ b/pkg/sources/github/github_integration_test.go
@@ -562,8 +562,8 @@ func gistsCheckFunc(expected string, minRepos int, s *Source) sources.ChunkFunc
func basicCheckFunc(minOrg, minRepo int, wantChunk *sources.Chunk, s *Source) sources.ChunkFunc {
return func(chunk *sources.Chunk) error {
- if minOrg != 0 && minOrg > len(s.orgs) {
- return fmt.Errorf("incorrect number of orgs. expected at least: %d, got %d", minOrg, len(s.orgs))
+ if minOrg != 0 && minOrg > s.orgsCache.Count() {
+ return fmt.Errorf("incorrect number of orgs. expected at least: %d, got %d", minOrg, s.orgsCache.Count())
}
if minRepo != 0 && minRepo > len(s.repos) {
return fmt.Errorf("incorrect number of repos. expected at least: %d, got %d", minRepo, len(s.repos))
diff --git a/pkg/sources/github/github_test.go b/pkg/sources/github/github_test.go
index 5b1ff486c..8a52fd961 100644
--- a/pkg/sources/github/github_test.go
+++ b/pkg/sources/github/github_test.go
@@ -21,6 +21,7 @@ import (
"google.golang.org/protobuf/types/known/anypb"
"gopkg.in/h2non/gock.v1"
+ "github.com/trufflesecurity/trufflehog/v3/pkg/cache/memory"
"github.com/trufflesecurity/trufflehog/v3/pkg/context"
"github.com/trufflesecurity/trufflehog/v3/pkg/pb/credentialspb"
"github.com/trufflesecurity/trufflehog/v3/pkg/pb/sourcespb"
@@ -344,7 +345,8 @@ func TestEnumerateUnauthenticated(t *testing.T) {
JSON([]map[string]string{{"clone_url": "https://github.com/super-secret-repo.git", "full_name": "super-secret-repo"}})
s := initTestSource(nil)
- s.orgs = []string{"super-secret-org"}
+ s.orgsCache = memory.New()
+ s.orgsCache.Set("super-secret-org", "super-secret-org")
s.enumerateUnauthenticated(context.TODO())
assert.Equal(t, 1, s.filteredRepoCache.Count())
ok := s.filteredRepoCache.Exists("super-secret-repo")