Skip to content

Commit

Permalink
limit nr of jobs
Browse files Browse the repository at this point in the history
  • Loading branch information
frjcomp committed Aug 12, 2024
1 parent 6ef1053 commit 0d0d9bc
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
2 changes: 2 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ Filter the scanned projects by using the `--search` flag and provide a search qu

Filter the scanned projects by using the `--owned` flag to only process projects owned by you.

Limit the scanned nr of jobs by using the `--job-limit` flag.

## Customizing Scan Rules

When you run Pipeleak for the first time, it generates a `rules.yml` file based on [this repository](https://github.com/mazen160/secrets-patterns-db/blob/master/db/rules-stable.yml). You can customize your scan rules by modifying this file as needed.
4 changes: 3 additions & 1 deletion src/pipeleak/cmd/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ var (
projectSearchQuery string
artifacts bool
owned bool
jobLimit int
verbose bool
)

Expand Down Expand Up @@ -45,6 +46,7 @@ func NewScanCmd() *cobra.Command {

scanCmd.PersistentFlags().BoolVarP(&artifacts, "artifacts", "a", false, "Scan Job Artifacts")
scanCmd.PersistentFlags().BoolVarP(&owned, "owned", "o", false, "Scan Onwed Projects Only")
scanCmd.PersistentFlags().IntVarP(&jobLimit, "job-limit", "j", 0, "Scan a max number of pipeline jobs - trade speed vs coverage. 0 scans all and is the default.")

scanCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "Verbose Logging")

Expand All @@ -60,7 +62,7 @@ func Scan(cmd *cobra.Command, args []string) {
os.Exit(1)
}

scanner.ScanGitLabPipelines(gitlabUrl, gitlabApiToken, gitlabCookie, artifacts, owned, projectSearchQuery)
scanner.ScanGitLabPipelines(gitlabUrl, gitlabApiToken, gitlabCookie, artifacts, owned, projectSearchQuery, jobLimit)
}

func setLogLevel() {
Expand Down
17 changes: 13 additions & 4 deletions src/pipeleak/scanner/gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"github.com/xanzy/go-gitlab"
)

func ScanGitLabPipelines(gitlabUrl string, apiToken string, cookie string, scanArtifacts bool, scanOwnedOnly bool, query string) {
func ScanGitLabPipelines(gitlabUrl string, apiToken string, cookie string, scanArtifacts bool, scanOwnedOnly bool, query string, jobLimit int) {
log.Info().Msg("Fetching projects")
git, err := gitlab.NewClient(apiToken, gitlab.WithBaseURL(gitlabUrl))
if err != nil {
Expand Down Expand Up @@ -49,7 +49,7 @@ func ScanGitLabPipelines(gitlabUrl string, apiToken string, cookie string, scanA

for _, project := range projects {
log.Debug().Msg("Scan Project jobs: " + project.Name)
getAllJobs(git, project, scanArtifacts, cookie, gitlabUrl)
getAllJobs(git, project, scanArtifacts, cookie, gitlabUrl, jobLimit)
}

if resp.NextPage == 0 {
Expand All @@ -60,7 +60,7 @@ func ScanGitLabPipelines(gitlabUrl string, apiToken string, cookie string, scanA
}
}

func getAllJobs(git *gitlab.Client, project *gitlab.Project, scanArtifacts bool, cookie string, gitlabUrl string) {
func getAllJobs(git *gitlab.Client, project *gitlab.Project, scanArtifacts bool, cookie string, gitlabUrl string, jobLimit int) {

opts := &gitlab.ListJobsOptions{
ListOptions: gitlab.ListOptions{
Expand All @@ -69,6 +69,9 @@ func getAllJobs(git *gitlab.Client, project *gitlab.Project, scanArtifacts bool,
},
}

currentJobCtr := 0

jobOut:
for {
jobs, resp, err := git.Jobs.ListProjectJobs(project.ID, opts)

Expand All @@ -77,11 +80,17 @@ func getAllJobs(git *gitlab.Client, project *gitlab.Project, scanArtifacts bool,
}

for _, job := range jobs {
currentJobCtr += 1
getJobTrace(git, project, job)

if scanArtifacts {
getJobArtifacts(git, project, job, cookie, gitlabUrl)
}

if jobLimit > 0 && currentJobCtr >= jobLimit {
log.Debug().Msg("Skipping jobs as job-limit is reached")
break jobOut
}
}

if resp.NextPage == 0 {
Expand Down Expand Up @@ -252,6 +261,6 @@ func SessionValid(gitlabUrl string, cookieVal string) {
if statCode != 200 {
log.Fatal().Msg("Negative _gitlab_session test, HTTP " + strconv.Itoa(statCode))
} else {
log.Info().Msg("Provided GitLab Session is valid")
log.Info().Msg("Provided GitLab session cookie is valid")
}
}

0 comments on commit 0d0d9bc

Please sign in to comment.