Find, verify, and analyze leaked credentials
Go credentialsdevsecopsdynamic-analysisprecommitscanningsecretsecret-managementsecretssecuritysecurity-toolstrufflehogverification
Clean up GitLab source logging for per-source debug (#5485)
Kashif Khan7238e0ec9b14
diff --git a/pkg/sources/gitlab/README.md b/pkg/sources/gitlab/README.mdindex 90c0ad8bd..7319ac64b 100644--- a/pkg/sources/gitlab/README.md+++ b/pkg/sources/gitlab/README.md@@ -211,6 +211,18 @@ trufflehog gitlab --token glpat-xxxxxxxxxxxxxxxxxxxx --group-id 12345678trufflehog gitlab --token glpat-xxxxxxxxxxxxxxxxxxxx --endpoint https://gitlab.mycompany.com```+## Debug Logs++With debug on for this source (`debug: true` in the config), the scan logs:++- `V(1)`: `GitLab source initialized` with the config choices (the endpoint's host only, never the token or credentials), and the start of enumeration.+- `V(2)`: `processing group` for each group, and the start and end of each repo scan, with its duration.+- `V(3)`: `accepting project` for each project found.+- `V(4)`: `skipping project` and `Skipping repo` with a `reason`, a line for each page of projects or groups, `GitLab API request failed` for any API response that is not a 2xx (with the status, path and rate limit headers), and `Retrying GitLab API request` for each retry.+- `V(5)`: project metadata cache hits and misses.++Lines carry `group_id`, `project_id`, `project_name`, `page` and `repo` where they apply. Clone and scan lines from the git source carry their own `repo` field, with any password removed from the URL.+## Troubleshooting### Common Issuesdiff --git a/pkg/sources/gitlab/apilog.go b/pkg/sources/gitlab/apilog.gonew file mode 100644index 000000000..1817cb32d--- /dev/null+++ b/pkg/sources/gitlab/apilog.go@@ -0,0 +1,40 @@+package gitlab++import (+ "net/http"++ "github.com/hashicorp/go-retryablehttp"++ "github.com/trufflesecurity/trufflehog/v3/pkg/context"+)++// logAPIRequest is a retryablehttp.RequestLogHook. It runs before every attempt+// and logs the ones that are retries. It logs through the request's context, so+// API calls must pass gitlab.WithContext(ctx) for the line to carry the source+// fields and follow per-source debug.+func logAPIRequest(_ retryablehttp.Logger, req *http.Request, attempt int) {+ if attempt == 0 {+ return+ }+ context.AddLogger(req.Context()).Logger().V(4).Info("Retrying GitLab API request",+ "method", req.Method,+ "path", req.URL.Path,+ "attempt", attempt,+ )+}++// logAPIResponse is a retryablehttp.ResponseLogHook. It logs every response that+// is not a 2xx, which covers permission errors, rate limits and server errors.+// Only the path is logged, not the query.+func logAPIResponse(_ retryablehttp.Logger, resp *http.Response) {+ if resp.StatusCode >= 200 && resp.StatusCode < 300 {+ return+ }+ context.AddLogger(resp.Request.Context()).Logger().V(4).Info("GitLab API request failed",+ "method", resp.Request.Method,+ "path", resp.Request.URL.Path,+ "status", resp.StatusCode,+ "retry_after", resp.Header.Get("Retry-After"),+ "ratelimit_remaining", resp.Header.Get("Ratelimit-Remaining"),+ )+}diff --git a/pkg/sources/gitlab/gitlab.go b/pkg/sources/gitlab/gitlab.goindex d4a531376..be2edc520 100644--- a/pkg/sources/gitlab/gitlab.go+++ b/pkg/sources/gitlab/gitlab.go@@ -201,9 +201,6 @@ func (s *Source) Init(ctx context.Context, name string, jobId sources.JobID, sou// configuration uses the inverse logic of the `useAuthInUrl` flag.s.useAuthInUrl = !conn.RemoveAuthInUrl- ctx.Logger().V(3).Info("setting ignore repos patterns", "patterns", s.ignoreRepos)- ctx.Logger().V(3).Info("setting include repos patterns", "patterns", s.includeRepos)-switch cred := conn.GetCredential().(type) {case *sourcespb.GitLab_Token:s.authMethod = "TOKEN"@@ -230,6 +227,22 @@ func (s *Source) Init(ctx context.Context, name string, jobId sources.JobID, soureturn err}+ // log only the endpoint's host, since the URL can hold user info+ var endpointHost string+ if u, err := url.Parse(s.url); err == nil {+ endpointHost = u.Host+ }+ ctx.Logger().V(1).Info("GitLab source initialized",+ "auth_method", s.authMethod,+ "endpoint_host", endpointHost,+ "repo_count", len(s.repos),+ "group_count", len(s.groupIds),+ "include_repos", s.includeRepos,+ "ignore_repos", s.ignoreRepos,+ "enumerate_shared_projects", s.enumerateSharedProjects,+ "projects_per_page", s.projectsPerPage,+ )+cfg := &git.Config{SourceName: s.name,JobID: s.jobID,@@ -292,7 +305,7 @@ func (s *Source) Init(ctx context.Context, name string, jobId sources.JobID, sou// Chunks emits chunks of bytes over a channel.func (s *Source) Chunks(ctx context.Context, chunksChan chan *sources.Chunk, targets ...sources.ChunkingTarget) error {// Start client.- apiClient, err := s.newClient()+ apiClient, err := s.newClient(ctx)if err != nil {return err}@@ -308,7 +321,7 @@ func (s *Source) Chunks(ctx context.Context, chunksChan chan *sources.Chunk, tar// Get repo within target.repos, errs := normalizeRepos(s.repos)for _, repoErr := range errs {- ctx.Logger().Info("error normalizing repo", "error", repoErr)+ ctx.Logger().Error(repoErr, "error normalizing repo")}// End early if we had errors getting specified repos but none were validated.@@ -318,7 +331,7 @@ func (s *Source) Chunks(ctx context.Context, chunksChan chan *sources.Chunk, tar// Get all repos if not specified.if len(repos) == 0 {- ctx.Logger().Info("no repositories configured, enumerating")+ ctx.Logger().V(1).Info("no repositories configured, enumerating")ignoreRepo := buildIgnorer(s.includeRepos, s.ignoreRepos, func(err error, pattern string) {ctx.Logger().Error(err, "could not compile include/exclude repo glob", "glob", pattern)})@@ -427,12 +440,12 @@ func (s *Source) Validate(ctx context.Context) []error {// The client is only used to query Gitlab for a repo list - it's not used to actually clone anything. Thus, we// don't use it if there is a list of explicitly configured repos. However, constructing it validates that the// configured authentication method is sensible, so we'll do it here.- apiClient, err := s.newClient()+ apiClient, err := s.newClient(ctx)if err != nil {return []error{err}}- _, _, err = apiClient.Users.CurrentUser()+ _, _, err = apiClient.Users.CurrentUser(gitlab.WithContext(ctx))if err != nil {return []error{fmt.Errorf("gitlab authentication failed using method %v: %w", s.authMethod, err)}}@@ -493,7 +506,7 @@ func (s *Source) Validate(ctx context.Context) []error {return errs}-func (s *Source) newClient() (*gitlab.Client, error) {+func (s *Source) newClient(ctx context.Context) (*gitlab.Client, error) {// Initialize a new api instance.switch s.authMethod {case "OAUTH":@@ -503,6 +516,8 @@ func (s *Source) newClient() (*gitlab.Client, error) {gitlab.WithBaseURL(s.url),gitlab.WithCustomRetryWaitMinMax(time.Second, 5*time.Second),gitlab.WithCustomRetryMax(3),+ gitlab.WithRequestLogHook(logAPIRequest),+ gitlab.WithResponseLogHook(logAPIResponse),)if err != nil {return nil, fmt.Errorf("could not create Gitlab OAUTH client for %q: %w", s.url, err)@@ -514,6 +529,8 @@ func (s *Source) newClient() (*gitlab.Client, error) {gitlab.WithBaseURL(s.url),gitlab.WithCustomRetryWaitMinMax(time.Second, 5*time.Second),gitlab.WithCustomRetryMax(3),+ gitlab.WithRequestLogHook(logAPIRequest),+ gitlab.WithResponseLogHook(logAPIResponse),)if err != nil {return nil, fmt.Errorf("could not create Gitlab BASICAUTH client for %q: %w", s.url, err)@@ -522,7 +539,7 @@ func (s *Source) newClient() (*gitlab.Client, error) {// will not work. In this case, we test to see if basic auth would work, and if it does not,// we proceed with an OAuth client using the access_token (s.password) as the token.// At this point, s.token is already set to s.password- if s.basicAuthSuccessful(apiClient) {+ if s.basicAuthSuccessful(ctx, apiClient) {return apiClient, nil}fallthrough@@ -533,6 +550,8 @@ func (s *Source) newClient() (*gitlab.Client, error) {gitlab.WithBaseURL(s.url),gitlab.WithCustomRetryWaitMinMax(time.Second, 5*time.Second),gitlab.WithCustomRetryMax(3),+ gitlab.WithRequestLogHook(logAPIRequest),+ gitlab.WithResponseLogHook(logAPIResponse),)if err != nil {return nil, fmt.Errorf("could not create Gitlab TOKEN client for %q: %w", s.url, err)@@ -544,8 +563,8 @@ func (s *Source) newClient() (*gitlab.Client, error) {}}-func (s *Source) basicAuthSuccessful(apiClient *gitlab.Client) bool {- user, resp, err := apiClient.Users.CurrentUser()+func (s *Source) basicAuthSuccessful(ctx context.Context, apiClient *gitlab.Client) bool {+ user, resp, err := apiClient.Users.CurrentUser(gitlab.WithContext(ctx))if err != nil {return false}@@ -568,7 +587,7 @@ func (s *Source) getAllProjectRepos() error {gitlabReposEnumerated.WithLabelValues(s.name).Set(0)// Projects without repo will get user projects, groups projects, and subgroup projects.- user, _, err := apiClient.Users.CurrentUser()+ user, _, err := apiClient.Users.CurrentUser(gitlab.WithContext(ctx))if err != nil {return fmt.Errorf("unable to authenticate using %s: %w", s.authMethod, err)}@@ -585,19 +604,19 @@ func (s *Source) getAllProjectRepos("project_name", proj.NameWithNamespace)// Skip projects we've already seen.if _, exists := uniqueProjects[proj.ID]; exists {- ctx.Logger().V(3).Info("skipping project", "reason", "ID already seen")+ ctx.Logger().V(4).Info("skipping project", "reason", "ID already seen")continue}// Skip projects configured to be ignored.if ignoreRepo(proj.PathWithNamespace) {- ctx.Logger().V(3).Info("skipping project", "reason", "ignored in config")+ ctx.Logger().V(4).Info("skipping project", "reason", "ignored in config")continue}// Record that we've seen this project.uniqueProjects[proj.ID] = proj// Report an error if we could not convert the project into a URL.if _, err := url.Parse(proj.HTTPURLToRepo); err != nil {- ctx.Logger().V(3).Info("skipping project",+ ctx.Logger().V(4).Info("skipping project","reason", "URL parse failure","url", proj.HTTPURLToRepo,"parse_error", err)@@ -631,16 +650,21 @@ func (s *Source) getAllProjectRepos(listOpts := gitlab.ListOptions{PerPage: s.projectsPerPage}projectQueryOptions := &gitlab.ListProjectsOptions{OrderBy: gitlab.Ptr(orderBy), ListOptions: listOpts}- for {- userProjects, res, err := apiClient.Projects.ListUserProjects(user.ID, projectQueryOptions)+ for page := 1; ; page++ {+ userProjects, res, err := apiClient.Projects.ListUserProjects(user.ID, projectQueryOptions, gitlab.WithContext(ctx))if err != nil {+ // a cancelled scan fails every call left, so stop instead of+ // reporting each one as a listing error+ if ctx.Err() != nil {+ return ctx.Err()+ }err = fmt.Errorf("received error on listing user projects: %w", err)if err := reporter.UnitErr(ctx, err); err != nil {return err}break}- ctx.Logger().V(3).Info("listed user projects", "count", len(userProjects))+ ctx.Logger().V(4).Info("listed user projects", "page", page, "count", len(userProjects))if err := processProjects(ctx, userProjects); err != nil {return err}@@ -661,22 +685,25 @@ func (s *Source) getAllProjectRepos(listGroupsOptions.AllAvailable = gitlab.Ptr(true)}- ctx.Logger().Info("beginning group enumeration",+ ctx.Logger().V(1).Info("beginning group enumeration","list_options", listOpts,"all_available", *listGroupsOptions.AllAvailable)gitlabGroupsEnumerated.WithLabelValues(s.name).Set(0)var groups []*gitlab.Group- for {- groupList, res, err := apiClient.Groups.ListGroups(&listGroupsOptions)+ for page := 1; ; page++ {+ groupList, res, err := apiClient.Groups.ListGroups(&listGroupsOptions, gitlab.WithContext(ctx))if err != nil {+ if ctx.Err() != nil {+ return ctx.Err()+ }err = fmt.Errorf("received error on listing groups, you probably don't have permissions to do that: %w", err)if err := reporter.UnitErr(ctx, err); err != nil {return err}break}- ctx.Logger().V(3).Info("listed groups", "count", len(groupList))+ ctx.Logger().V(4).Info("listed groups", "page", page, "count", len(groupList))groups = append(groups, groupList...)gitlabGroupsEnumerated.WithLabelValues(s.name).Add(float64(len(groupList)))listGroupsOptions.Page = res.NextPage@@ -685,19 +712,22 @@ func (s *Source) getAllProjectRepos(}}- ctx.Logger().Info("got groups", "group_count", len(groups))+ ctx.Logger().V(1).Info("got groups", "group_count", len(groups))for _, group := range groups {- ctx := context.WithValue(ctx, "group_id", group.ID)+ ctx := context.WithValues(ctx, "group_id", group.ID)listGroupProjectOptions := &gitlab.ListGroupProjectsOptions{ListOptions: listOpts,OrderBy: gitlab.Ptr(orderBy),IncludeSubGroups: gitlab.Ptr(true),WithShared: gitlab.Ptr(s.enumerateSharedProjects),}- for {- grpPrjs, res, err := apiClient.Groups.ListGroupProjects(group.ID, listGroupProjectOptions)+ for page := 1; ; page++ {+ grpPrjs, res, err := apiClient.Groups.ListGroupProjects(group.ID, listGroupProjectOptions, gitlab.WithContext(ctx))if err != nil {+ if ctx.Err() != nil {+ return ctx.Err()+ }err = fmt.Errorf("received error on listing group projects for %q, you probably don't have permissions to do that: %w",group.FullPath, err,@@ -707,7 +737,7 @@ func (s *Source) getAllProjectRepos(}break}- ctx.Logger().V(3).Info("listed group projects", "count", len(grpPrjs))+ ctx.Logger().V(4).Info("listed group projects", "page", page, "count", len(grpPrjs))if err := processProjects(ctx, grpPrjs); err != nil {return err}@@ -753,7 +783,7 @@ func (s *Source) getAllProjectReposV2(projectQueryOptions.Membership = gitlab.Ptr(true)}- ctx.Logger().Info("starting projects enumeration",+ ctx.Logger().V(1).Info("starting projects enumeration","list_options", listOpts)// totalCount tracks the total number of projects processed by this enumeration.@@ -763,7 +793,7 @@ func (s *Source) getAllProjectReposV2(requestOptions := []gitlab.RequestOptionFunc{gitlab.WithContext(ctx)}// Pagination loop: Continue fetching pages until the API indicates there are no more.- for {+ for page := 1; ; page++ {// Fetch a page of projects from the GitLab API using the current query options.projects, resp, err := apiClient.Projects.ListProjects(projectQueryOptions, requestOptions...)if err != nil {@@ -776,7 +806,7 @@ func (s *Source) getAllProjectReposV2(}// Log the batch size for debugging and monitoring.- ctx.Logger().V(3).Info("listed projects batch", "batch_size", len(projects), "running_total", totalCount)+ ctx.Logger().V(4).Info("listed projects batch", "page", page, "batch_size", len(projects), "running_total", totalCount)// Process each project in the current page.for _, project := range projects {projCtx := context.WithValues(ctx,@@ -787,14 +817,14 @@ func (s *Source) getAllProjectReposV2(// skip projects configured to be ignored.if ignoreRepo(project.PathWithNamespace) {- projCtx.Logger().V(3).Info("skipping project", "reason", "ignored in config")+ projCtx.Logger().V(4).Info("skipping project", "reason", "ignored in config")continue}// report an error if we could not convert the project into a URL.if _, err := url.Parse(project.HTTPURLToRepo); err != nil {- projCtx.Logger().V(3).Info("skipping project",+ projCtx.Logger().V(4).Info("skipping project","reason", "URL parse failure","url", project.HTTPURLToRepo,"parse_error", err)@@ -870,7 +900,7 @@ func (s *Source) getAllProjectReposInGroups(projectOpts.MinAccessLevel = gitlab.Ptr(gitlab.GuestPermissions)}- ctx.Logger().Info("starting group projects enumeration",+ ctx.Logger().V(1).Info("starting group projects enumeration","group_ids", s.groupIds,"include_subgroups", true,"list_options", listOpts)@@ -879,11 +909,14 @@ func (s *Source) getAllProjectReposInGroups(groupCtx := context.WithValues(ctx, "group_id", groupID)projectOpts.Page = 0- groupCtx.Logger().V(2).Info("processing group", "group_id", groupID)+ groupCtx.Logger().V(2).Info("processing group")- for {- projects, res, err := apiClient.Groups.ListGroupProjects(groupID, projectOpts)+ for page := 1; ; page++ {+ projects, res, err := apiClient.Groups.ListGroupProjects(groupID, projectOpts, gitlab.WithContext(groupCtx))if err != nil {+ if ctx.Err() != nil {+ return ctx.Err()+ }err = fmt.Errorf("received error on listing projects for group %s: %w", groupID, err)if err := reporter.UnitErr(ctx, err); err != nil {return err@@ -891,29 +924,28 @@ func (s *Source) getAllProjectReposInGroups(break}- groupCtx.Logger().V(3).Info("listed group projects", "count", len(projects))+ groupCtx.Logger().V(4).Info("listed group projects", "page", page, "count", len(projects))for _, proj := range projects {- projCtx := context.WithValues(ctx,+ projCtx := context.WithValues(groupCtx,"project_id", proj.ID,- "project_name", proj.NameWithNamespace,- "group_id", groupID)+ "project_name", proj.NameWithNamespace)if processedProjects[proj.HTTPURLToRepo] {- projCtx.Logger().V(3).Info("skipping project", "reason", "already processed")+ projCtx.Logger().V(4).Info("skipping project", "reason", "already processed")continue}processedProjects[proj.HTTPURLToRepo] = true// skip projects configured to be ignored.if ignoreRepo(proj.PathWithNamespace) {- projCtx.Logger().V(3).Info("skipping project", "reason", "ignored in config")+ projCtx.Logger().V(4).Info("skipping project", "reason", "ignored in config")continue}// report an error if we could not convert the project into a URL.if _, err := url.Parse(proj.HTTPURLToRepo); err != nil {- projCtx.Logger().V(3).Info("skipping project",+ projCtx.Logger().V(4).Info("skipping project","reason", "URL parse failure","url", proj.HTTPURLToRepo,"parse_error", err)@@ -963,16 +995,18 @@ func (s *Source) scanRepos(ctx context.Context, chunksChan chan *sources.Chunk)for i, repo := range s.repos {repoURL := repos.jobPool.Go(func() error {- logger := ctx.Logger().WithValues("repo", repoURL)+ // only for this source's own lines: the git calls below get ctx,+ // since git adds its own repo field with any password stripped+ repoCtx := context.WithValues(ctx, "repo", repoURL)if common.IsDone(ctx) {// We are returning nil instead of the scanErrors slice here because// we don't want to mark this scan as errored if we cancelled it.- logger.V(2).Info("Skipping repo because context was cancelled")+ repoCtx.Logger().V(4).Info("Skipping repo", "reason", "scan cancelled")return nil}if len(repoURL) == 0 {- logger.V(2).Info("Skipping empty repo")+ repoCtx.Logger().V(4).Info("Skipping repo", "reason", "empty repo URL")return nil}@@ -1017,14 +1051,15 @@ func (s *Source) scanRepos(ctx context.Context, chunksChan chan *sources.Chunk)// large orgs, longer than the cache's TTL.s.ensureProjectInCache(ctx, repoURL)- logger.V(2).Info("starting scan", "num", i+1, "total", len(s.repos))+ repoCtx.Logger().V(2).Info("starting scan", "num", i+1, "total", len(s.repos))+ start := time.Now()if err = s.git.ScanRepo(ctx, repo, path, s.scanOptions, sources.ChanReporter{Ch: chunksChan}); err != nil {scanErrs.Add(err)return nil}gitlabReposScanned.WithLabelValues(s.name).Inc()- logger.V(2).Info("completed scan", "num", i+1, "total", len(s.repos))+ repoCtx.Logger().V(2).Info("completed scan", "num", i+1, "total", len(s.repos), "duration", time.Since(start))return nil})}@@ -1064,7 +1099,7 @@ func (s *Source) WithScanOptions(scanOptions *git.ScanOptions) {// rules.func (s *Source) Enumerate(ctx context.Context, reporter sources.UnitReporter) error {// Start client.- apiClient, err := s.newClient()+ apiClient, err := s.newClient(ctx)if err != nil {return err}@@ -1072,7 +1107,7 @@ func (s *Source) Enumerate(ctx context.Context, reporter sources.UnitReporter) e// Get repos within target.repos, errs := normalizeRepos(s.repos)for _, repoErr := range errs {- ctx.Logger().Info("error normalizing repo", "error", repoErr)+ ctx.Logger().Error(repoErr, "error normalizing repo")if err := reporter.UnitErr(ctx, repoErr); err != nil {return err}@@ -1113,12 +1148,15 @@ func (s *Source) Enumerate(ctx context.Context, reporter sources.UnitReporter) e// ChunkUnit downloads and reports chunks for the given GitLab repository unit.func (s *Source) ChunkUnit(ctx context.Context, unit sources.SourceUnit, reporter sources.ChunkReporter) error {repoURL, _ := unit.SourceUnitID()+ // only for this source's own lines: the git calls below get ctx, since git+ // adds its own repo field with any password stripped+ repoCtx := context.WithValues(ctx, "repo", repoURL)ignoreRepo := buildIgnorer(s.includeRepos, s.ignoreRepos, func(err error, pattern string) {ctx.Logger().Error(err, "could not compile include/exclude repo glob", "glob", pattern)})if ignoreRepo(repoURL) {- ctx.Logger().V(3).Info("skipping project", "reason", "ignored in config")+ repoCtx.Logger().V(4).Info("skipping project", "reason", "ignored in config")return nil}@@ -1127,10 +1165,12 @@ func (s *Source) ChunkUnit(ctx context.Context, unit sources.SourceUnit, reporte// project details below.normalizedRepoURL, err := giturl.NormalizeGitlabRepo(repoURL)if err != nil {- ctx.Logger().Error(err, "failed to normalize GitLab Repo", "repo", repoURL)return err}+ repoCtx.Logger().V(2).Info("Scanning repo")+ start := time.Now()+var path stringvar repo *gogit.Repositoryif s.authMethod == "UNAUTHENTICATED" {@@ -1161,7 +1201,11 @@ func (s *Source) ChunkUnit(ctx context.Context, unit sources.SourceUnit, reporte// this is required to populate metadata during chunkings.ensureProjectInCache(ctx, normalizedRepoURL)- return s.git.ScanRepo(ctx, repo, path, s.scanOptions, reporter)+ if err := s.git.ScanRepo(ctx, repo, path, s.scanOptions, reporter); err != nil {+ return err+ }+ repoCtx.Logger().V(2).Info("Finished scanning repo", "duration", time.Since(start))+ return nil}// ensureProjectInCache ensures that the project for the given repository URL@@ -1186,7 +1230,7 @@ func (s *Source) ensureProjectInCache(ctx context.Context, repoURL string) {}func (s *Source) getGitlabProject(ctx context.Context, repoUrl string) (*gitlab.Project, error) {- apiClient, err := s.newClient()+ apiClient, err := s.newClient(ctx)if err != nil {return nil, fmt.Errorf("could not create api client: %w", err)}diff --git a/pkg/sources/gitlab/gitlab_test.go b/pkg/sources/gitlab/gitlab_test.goindex 8eb4642a3..fdb639c21 100644--- a/pkg/sources/gitlab/gitlab_test.go+++ b/pkg/sources/gitlab/gitlab_test.go@@ -1,14 +1,29 @@package gitlabimport (+ stdctx "context"+ "encoding/json"+ "fmt"+ "net/http"+ "net/http/httptest""reflect"+ "sync/atomic""testing"+ "github.com/go-logr/zapr""github.com/stretchr/testify/assert"+ "github.com/stretchr/testify/require"+ "go.uber.org/zap"+ "go.uber.org/zap/zapcore"+ "go.uber.org/zap/zaptest/observer""golang.org/x/sync/errgroup"+ "google.golang.org/protobuf/types/known/anypb""github.com/trufflesecurity/trufflehog/v3/pkg/context"+ "github.com/trufflesecurity/trufflehog/v3/pkg/log/logtest"+ "github.com/trufflesecurity/trufflehog/v3/pkg/pb/sourcespb""github.com/trufflesecurity/trufflehog/v3/pkg/sources/git"+ "github.com/trufflesecurity/trufflehog/v3/pkg/sourcestest")func Test_setProgressCompleteWithRepo_resumeInfo(t *testing.T) {@@ -187,3 +202,160 @@ func Test_normalizeGitlabEndpoint(t *testing.T) {})}}++func TestSource_DebugLogging(t *testing.T) {+ const testToken = "glpat-debuglogtestsecret"++ // fakeGitLab serves two pages of group projects. The first request for+ // page 1 fails with a 503, so the client retries it.+ newFakeGitLab := func(t *testing.T) *httptest.Server {+ var failed atomic.Bool+ srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {+ if r.URL.Path != "/api/v4/groups/1/projects" {+ http.NotFound(w, r)+ return+ }+ if !failed.Swap(true) {+ w.WriteHeader(http.StatusServiceUnavailable)+ return+ }+ project := func(id int, path string) map[string]any {+ return map[string]any{+ "id": id,+ "name_with_namespace": path,+ "path_with_namespace": path,+ "http_url_to_repo": "https://gitlab.example.com/" + path + ".git",+ }+ }+ w.Header().Set("Content-Type", "application/json")+ if r.URL.Query().Get("page") == "2" {+ _ = json.NewEncoder(w).Encode([]any{project(3, "org/third")})+ return+ }+ w.Header().Set("X-Next-Page", "2")+ _ = json.NewEncoder(w).Encode([]any{project(1, "org/first"), project(2, "org/ignored")})+ }))+ t.Cleanup(srv.Close)+ return srv+ }++ runSource := func(t *testing.T, ctx context.Context) sourcestest.TestReporter {+ srv := newFakeGitLab(t)+ conn, err := anypb.New(&sourcespb.GitLab{+ Credential: &sourcespb.GitLab_Token{Token: testToken},+ Endpoint: "https://gitlab.example.com",+ GroupIds: []string{"1"},+ IgnoreRepos: []string{"*ignored*"},+ })+ require.NoError(t, err)++ s := Source{}+ require.NoError(t, s.Init(ctx, "test debug logging", 1, 1, false, conn, 1))+ // Init only accepts https, so point the client at the fake server afterwards.+ s.url = srv.URL + "/"++ reporter := sourcestest.TestReporter{}+ require.NoError(t, s.Enumerate(ctx, &reporter))+ require.Empty(t, reporter.UnitErrs)+ require.Len(t, reporter.Units, 2)++ // an ignored unit returns before cloning+ ignored := git.SourceUnit{Kind: git.UnitRepo, ID: "https://gitlab.example.com/org/ignored.git"}+ require.NoError(t, s.ChunkUnit(ctx, ignored, &reporter))+ return reporter+ }++ t.Run("debug on", func(t *testing.T) {+ ctx, logs := logtest.DebugContext()+ ctx = context.WithValues(ctx, "source_name", "test debug logging", "job_id", 1)+ runSource(t, ctx)++ // lines from each level show up+ for _, msg := range []string{+ "GitLab source initialized", // V(1)+ "starting group projects enumeration", // V(1)+ "processing group", // V(2)+ "accepting project", // V(3)+ "listed group projects", // V(4)+ "skipping project", // V(4)+ "GitLab API request failed", // V(4)+ "Retrying GitLab API request", // V(4)+ "cache set: added project metadata in the cache", // V(5)+ } {+ assert.NotZero(t, logs.FilterMessage(msg).Len(), "missing log line %q", msg)+ }++ // both pages were logged, with their group+ pages := logs.FilterMessage("listed group projects").All()+ require.Len(t, pages, 2)+ for i, entry := range pages {+ assert.EqualValues(t, i+1, entry.ContextMap()["page"])+ assert.Equal(t, "1", entry.ContextMap()["group_id"])+ }++ // the failed request says what failed+ failed := logs.FilterMessage("GitLab API request failed").All()+ require.Len(t, failed, 1)+ assert.EqualValues(t, http.StatusServiceUnavailable, failed[0].ContextMap()["status"])+ assert.Equal(t, "/api/v4/groups/1/projects", failed[0].ContextMap()["path"])++ // both skips (in enumeration and in ChunkUnit) say why+ skips := logs.FilterMessage("skipping project").All()+ require.Len(t, skips, 2)+ for _, entry := range skips {+ assert.Equal(t, "ignored in config", entry.ContextMap()["reason"])+ }+ assert.Equal(t, "https://gitlab.example.com/org/ignored.git", skips[1].ContextMap()["repo"])++ // every line came from the context logger, so it carries the source fields,+ // and none of them contain the token+ for _, entry := range logs.All() {+ fields := entry.ContextMap()+ assert.Contains(t, fields, "source_name", "line %q", entry.Message)+ assert.Contains(t, fields, "job_id", "line %q", entry.Message)+ assert.NotContains(t, entry.Message, testToken)+ assert.NotContains(t, fmt.Sprint(fields), testToken)+ }+ })++ t.Run("debug off", func(t *testing.T) {+ core, logs := observer.New(zapcore.InfoLevel)+ ctx := context.WithLogger(context.Background(), zapr.NewLogger(zap.New(core)))+ runSource(t, ctx)++ assert.Zero(t, logs.FilterMessage("skipping project").Len())+ assert.Zero(t, logs.FilterMessage("listed group projects").Len())+ assert.Zero(t, logs.FilterMessage("GitLab API request failed").Len())+ assert.Zero(t, logs.FilterMessage("Retrying GitLab API request").Len())+ assert.Zero(t, logs.FilterMessage("cache set: added project metadata in the cache").Len())+ })+}++func TestSource_EnumerateCancelled(t *testing.T) {+ ctx, cancel := context.WithCancel(context.Background())+ defer cancel()++ // the first request cancels the scan and hangs until the client gives up+ srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {+ cancel()+ <-r.Context().Done()+ }))+ t.Cleanup(srv.Close)++ conn, err := anypb.New(&sourcespb.GitLab{+ Credential: &sourcespb.GitLab_Token{Token: "glpat-cancelledtestsecret"},+ Endpoint: "https://gitlab.example.com",+ GroupIds: []string{"1", "2", "3"},+ })+ require.NoError(t, err)++ s := Source{}+ require.NoError(t, s.Init(ctx, "test cancelled", 1, 1, false, conn, 1))+ s.url = srv.URL + "/"++ reporter := sourcestest.TestReporter{}+ err = s.Enumerate(ctx, &reporter)+ assert.ErrorIs(t, err, stdctx.Canceled)+ // the groups left are not reported as listing errors+ assert.Empty(t, reporter.UnitErrs)+}