Skip to content

Commit

Permalink
add queue db folder path flag
Browse files Browse the repository at this point in the history
  • Loading branch information
frjcomp committed Sep 26, 2024
1 parent d330458 commit f268d7a
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 13 deletions.
1 change: 1 addition & 0 deletions src/pipeleak/cmd/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ func NewScanCmd() *cobra.Command {
scanCmd.PersistentFlags().IntVarP(&options.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().StringVarP(&maxArtifactSize, "max-artifact-size", "", "500Mb", "Max file size of an artifact to be included in scanning. Larger files are skipped. Format: https://pkg.go.dev/github.com/docker/go-units#FromHumanSize")
scanCmd.PersistentFlags().IntVarP(&options.MaxScanGoRoutines, "threads", "", 4, "Nr of threads used to scan")
scanCmd.PersistentFlags().StringVarP(&options.QueueFolder, "queue", "q", "", "Relative folderpath where the queue files will be stored. Defaults to system tmp")

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

Expand Down
43 changes: 30 additions & 13 deletions src/pipeleak/scanner/pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"net/http"
"net/url"
"os"
"path"
"path/filepath"
"strconv"
"time"
Expand Down Expand Up @@ -39,18 +40,11 @@ type ScanOptions struct {
ConfidenceFilter []string
MaxArtifactSize int64
MaxScanGoRoutines int
QueueFolder string
}

func ScanGitLabPipelines(options *ScanOptions) {
log.Debug().Msg("Setting up queue on disk")
tmpfile, err := os.CreateTemp("", "pipeleak-queue-db")
if err != nil {
log.Fatal().Err(err).Msg("Creating Temp DB file failed")
}
defer os.Remove(tmpfile.Name())
queueFileName = tmpfile.Name()

setupQueue(tmpfile.Name(), options.MaxScanGoRoutines)
setupQueue(options)
helper.RegisterGracefulShutdownHandler(cleanUp)

r := jobs.NewRunner(jobs.NewRunnerOpts{
Expand All @@ -74,12 +68,35 @@ func ScanGitLabPipelines(options *ScanOptions) {
r.Start(queueCtx)
}

func setupQueue(fileName string, maxReceive int) {
sqlUri := `file://` + fileName + `:?_journal=WAL&_timeout=5000&_fk=true`
func setupQueue(options *ScanOptions) {
log.Debug().Msg("Setting up queue on disk")

queueDirectory := options.QueueFolder
if len(options.QueueFolder) > 0 {
cwd, err := os.Getwd()
if err != nil {
log.Fatal().Err(err).Msg("Could not determine CWD")
}
relative := path.Join(cwd, queueDirectory)
absPath, err := filepath.Abs(relative)
if err != nil {
log.Fatal().Err(err).Msg("Failed parsing absolute path")
}
queueDirectory = absPath
}

tmpfile, err := os.CreateTemp(queueDirectory, "pipeleak-queue-db-")
if err != nil {
log.Fatal().Err(err).Msg("Creating Temp DB file failed")
}
defer os.Remove(tmpfile.Name())
queueFileName = tmpfile.Name()

sqlUri := `file://` + queueFileName + `:?_journal=WAL&_timeout=5000&_fk=true`
db, err := sql.Open("sqlite3", sqlUri)
log.Debug().Str("file", sqlUri).Msg("Using DB file")
if err != nil {
log.Fatal().Err(err).Str("file", fileName).Msg("Opening Temp DB file failed")
log.Fatal().Err(err).Str("file", queueFileName).Msg("Opening Temp DB file failed")
}
db.SetMaxOpenConns(1)
db.SetMaxIdleConns(1)
Expand All @@ -91,7 +108,7 @@ func setupQueue(fileName string, maxReceive int) {
queue = goqite.New(goqite.NewOpts{
DB: db,
Name: "jobs",
MaxReceive: maxReceive,
MaxReceive: options.MaxScanGoRoutines,
})
}

Expand Down

0 comments on commit f268d7a

Please sign in to comment.