Skip to content

Commit 18c7b1f

Browse files
Added feature flag to configure projects per page in gitlab enumeration (#4437)
* Added flag to configure projects per page in gitlab enumeration * changed comments wording * Resolved conflicts * use features instead of flag * updated comment
1 parent ce7a092 commit 18c7b1f

File tree

4 files changed

+22
-13
lines changed

4 files changed

+22
-13
lines changed

main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,7 @@ func run(state overseer.State) {
463463

464464
// OSS Default simplified gitlab enumeration
465465
feature.UseSimplifiedGitlabEnumeration.Store(true)
466+
feature.GitlabProjectsPerPage.Store(100)
466467

467468
conf := &config.Config{}
468469
if *configFilename != "" {

pkg/engine/gitlab.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ func (e *Engine) ScanGitLab(ctx context.Context, c sources.GitlabConfig) (source
6363
}
6464

6565
connection.NoCleanup = c.NoCleanup
66+
6667
connection.PrintLegacyJson = c.PrintLegacyJSON
6768

6869
var conn anypb.Any

pkg/feature/feature.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ var (
99
EnableAPKHandler atomic.Bool
1010
UserAgentSuffix AtomicString
1111
UseSimplifiedGitlabEnumeration atomic.Bool
12-
UseGitMirror atomic.Bool
12+
UseGitMirror atomic.Bool
13+
GitlabProjectsPerPage atomic.Int64
1314
)
1415

1516
type AtomicString struct {

pkg/sources/gitlab/gitlab.go

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,12 @@ type Source struct {
6666

6767
useAuthInUrl bool
6868

69-
clonePath string
70-
noCleanup bool
69+
clonePath string
70+
noCleanup bool
71+
7172
printLegacyJSON bool
73+
74+
projectsPerPage int
7275
}
7376

7477
// WithCustomContentWriter sets the useCustomContentWriter flag on the source.
@@ -171,6 +174,11 @@ func (s *Source) Init(ctx context.Context, name string, jobId sources.JobID, sou
171174
s.clonePath = conn.GetClonePath()
172175
s.noCleanup = conn.GetNoCleanup()
173176
s.printLegacyJSON = conn.GetPrintLegacyJson()
177+
s.projectsPerPage = int(feature.GitlabProjectsPerPage.Load())
178+
179+
if s.projectsPerPage > 100 {
180+
return fmt.Errorf("invalid config: maximum allowed projects per page for gitlab is 100")
181+
}
174182

175183
// configuration uses the inverse logic of the `useAuthInUrl` flag.
176184
s.useAuthInUrl = !conn.RemoveAuthInUrl
@@ -546,10 +554,10 @@ func (s *Source) getAllProjectRepos(
546554
}
547555

548556
const (
549-
orderBy = "id" // TODO: Use keyset pagination (https://docs.gitlab.com/ee/api/rest/index.html#keyset-based-pagination)
550-
paginationLimit = 100 // Default is 20, max is 100.
557+
orderBy = "id"
551558
)
552-
listOpts := gitlab.ListOptions{PerPage: paginationLimit}
559+
// Trufflehog default per page 100 unless set to other value through feature flag. If 0 provided in feature flag gitlab default it to 20
560+
listOpts := gitlab.ListOptions{PerPage: s.projectsPerPage}
553561

554562
projectQueryOptions := &gitlab.ListProjectsOptions{OrderBy: gitlab.Ptr(orderBy), ListOptions: listOpts}
555563
for {
@@ -654,14 +662,13 @@ func (s *Source) getAllProjectReposV2(
654662
) error {
655663
gitlabReposEnumerated.WithLabelValues(s.name).Set(0)
656664

657-
const paginationLimit = 100 // default is 20, max is 100.
658-
659665
// example: https://gitlab.com/gitlab-org/api/client-go/-/blob/main/examples/pagination.go#L55
660666
listOpts := gitlab.ListOptions{
661667
OrderBy: "id",
662668
Pagination: "keyset", // https://docs.gitlab.com/api/rest/#keyset-based-pagination
663-
PerPage: paginationLimit,
664-
Sort: "asc",
669+
// Trufflehog default per page 100 unless set to other value through feature flag. If 0 provided in feature flag gitlab default it to 20
670+
PerPage: s.projectsPerPage,
671+
Sort: "asc",
665672
}
666673

667674
projectQueryOptions := &gitlab.ListProjectsOptions{
@@ -756,11 +763,10 @@ func (s *Source) getAllProjectReposInGroups(
756763

757764
var projectsWithNamespace []string
758765
const (
759-
orderBy = "id"
760-
paginationLimit = 100
766+
orderBy = "id"
761767
)
762768

763-
listOpts := gitlab.ListOptions{PerPage: paginationLimit}
769+
listOpts := gitlab.ListOptions{PerPage: s.projectsPerPage}
764770
projectOpts := &gitlab.ListGroupProjectsOptions{
765771
ListOptions: listOpts,
766772
OrderBy: gitlab.Ptr(orderBy),

0 commit comments

Comments
 (0)