|
| 1 | +package scanpublic |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "time" |
| 6 | + |
| 7 | + "github.com/CompassSecurity/pipeleek/internal/cmd/flags" |
| 8 | + "github.com/CompassSecurity/pipeleek/pkg/config" |
| 9 | + gitlabscan "github.com/CompassSecurity/pipeleek/pkg/gitlab/scan" |
| 10 | + "github.com/CompassSecurity/pipeleek/pkg/logging" |
| 11 | + "github.com/rs/zerolog" |
| 12 | + "github.com/rs/zerolog/log" |
| 13 | + "github.com/spf13/cobra" |
| 14 | +) |
| 15 | + |
| 16 | +type ScanPublicOptions struct { |
| 17 | + config.CommonScanOptions |
| 18 | + ProjectSearchQuery string |
| 19 | + Repository string |
| 20 | + Namespace string |
| 21 | + JobLimit int |
| 22 | + QueueFolder string |
| 23 | +} |
| 24 | + |
| 25 | +var options = ScanPublicOptions{ |
| 26 | + CommonScanOptions: config.DefaultCommonScanOptions(), |
| 27 | +} |
| 28 | + |
| 29 | +var maxArtifactSize string |
| 30 | + |
| 31 | +func NewScanPublicCmd() *cobra.Command { |
| 32 | + scanCmd := &cobra.Command{ |
| 33 | + Use: "scan", |
| 34 | + Short: "Scan public GitLab pipelines without an account", |
| 35 | + Long: `Scan public GitLab project pipelines for secrets in job traces and optionally artifacts. |
| 36 | +
|
| 37 | +This command does not require an API token and only covers resources that are publicly accessible. |
| 38 | +Dotenv artifacts are intentionally not scanned in this mode because they require a UI session cookie.`, |
| 39 | + Example: ` |
| 40 | +# Scan public project pipelines and traces |
| 41 | +pipeleek gluna scan --gitlab https://gitlab.example.com |
| 42 | +
|
| 43 | +# Scan public pipelines with artifacts and tuned performance |
| 44 | +pipeleek gluna scan --gitlab https://gitlab.example.com --artifacts --job-limit 10 --max-artifact-size 200Mb --threads 8 |
| 45 | +
|
| 46 | +# Scan one public repository |
| 47 | +pipeleek gluna scan --gitlab https://gitlab.example.com --repo mygroup/myproject |
| 48 | +
|
| 49 | +# Scan all public repositories in a namespace |
| 50 | +pipeleek gluna scan --gitlab https://gitlab.example.com --namespace mygroup |
| 51 | + `, |
| 52 | + Run: ScanPublic, |
| 53 | + } |
| 54 | + |
| 55 | + scanCmd.Flags().StringP("gitlab", "g", "", "GitLab instance URL") |
| 56 | + flags.AddCommonScanFlagsNoArtifacts(scanCmd, &options.CommonScanOptions) |
| 57 | + scanCmd.Flags().BoolVarP(&options.Artifacts, "artifacts", "a", false, "Scan artifacts") |
| 58 | + scanCmd.Flags().StringVarP(&maxArtifactSize, "max-artifact-size", "", "500Mb", |
| 59 | + "Maximum artifact size to scan. Larger files are skipped. Format: https://pkg.go.dev/github.com/docker/go-units#FromHumanSize") |
| 60 | + scanCmd.Flags().StringVarP(&options.ProjectSearchQuery, "search", "s", "", "Query string for searching public projects") |
| 61 | + scanCmd.Flags().StringVarP(&options.Repository, "repo", "r", "", "Single public repository to scan, format: namespace/repo") |
| 62 | + scanCmd.Flags().StringVarP(&options.Namespace, "namespace", "n", "", "Namespace to scan (all public repos in the namespace will be scanned)") |
| 63 | + scanCmd.Flags().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.") |
| 64 | + scanCmd.Flags().StringVarP(&options.QueueFolder, "queue", "q", "", "Relative or absolute folderpath where the queue files will be stored. Defaults to system tmp. Non-existing folders will be created.") |
| 65 | + |
| 66 | + return scanCmd |
| 67 | +} |
| 68 | + |
| 69 | +func ScanPublic(cmd *cobra.Command, args []string) { |
| 70 | + if err := config.AutoBindFlags(cmd, map[string]string{ |
| 71 | + "gitlab": "gitlab.url", |
| 72 | + "search": "gitlab.scan_public.search", |
| 73 | + "repo": "gitlab.scan_public.repo", |
| 74 | + "namespace": "gitlab.scan_public.namespace", |
| 75 | + "job-limit": "gitlab.scan_public.job_limit", |
| 76 | + "queue": "gitlab.scan_public.queue", |
| 77 | + "artifacts": "gitlab.scan_public.artifacts", |
| 78 | + "threads": "common.threads", |
| 79 | + "truffle-hog-verification": "common.trufflehog_verification", |
| 80 | + "max-artifact-size": "common.max_artifact_size", |
| 81 | + "confidence": "common.confidence_filter", |
| 82 | + "hit-timeout": "common.hit_timeout", |
| 83 | + }); err != nil { |
| 84 | + log.Fatal().Err(err).Msg("Failed to bind command flags to configuration keys") |
| 85 | + } |
| 86 | + |
| 87 | + if err := config.RequireConfigKeys("gitlab.url"); err != nil { |
| 88 | + log.Fatal().Err(err).Msg("required configuration missing") |
| 89 | + } |
| 90 | + |
| 91 | + gitlabURL := config.GetString("gitlab.url") |
| 92 | + projectSearchQuery := config.GetString("gitlab.scan_public.search") |
| 93 | + repository := config.GetString("gitlab.scan_public.repo") |
| 94 | + namespace := config.GetString("gitlab.scan_public.namespace") |
| 95 | + jobLimit := config.GetInt("gitlab.scan_public.job_limit") |
| 96 | + queueFolder := config.GetString("gitlab.scan_public.queue") |
| 97 | + artifacts := config.GetBool("gitlab.scan_public.artifacts") |
| 98 | + threads := config.GetInt("common.threads") |
| 99 | + truffleHogVerification := config.GetBool("common.trufflehog_verification") |
| 100 | + maxArtifactSize = config.GetString("common.max_artifact_size") |
| 101 | + confidenceFilter := config.GetStringSlice("common.confidence_filter") |
| 102 | + hitTimeoutRaw := config.GetString("common.hit_timeout") |
| 103 | + hitTimeout, err := time.ParseDuration(hitTimeoutRaw) |
| 104 | + if err != nil { |
| 105 | + log.Fatal().Err(fmt.Errorf("invalid hit-timeout %q: %w", hitTimeoutRaw, err)).Msg("Invalid hit timeout") |
| 106 | + } |
| 107 | + |
| 108 | + if err := config.ValidateURL(gitlabURL, "GitLab URL"); err != nil { |
| 109 | + log.Fatal().Err(err).Msg("Invalid GitLab URL") |
| 110 | + } |
| 111 | + if err := config.ValidateThreadCount(threads); err != nil { |
| 112 | + log.Fatal().Err(err).Msg("Invalid thread count") |
| 113 | + } |
| 114 | + |
| 115 | + scanOpts, err := gitlabscan.InitializeOptions( |
| 116 | + gitlabURL, |
| 117 | + "", |
| 118 | + "", |
| 119 | + projectSearchQuery, |
| 120 | + repository, |
| 121 | + namespace, |
| 122 | + queueFolder, |
| 123 | + maxArtifactSize, |
| 124 | + artifacts, |
| 125 | + false, |
| 126 | + false, |
| 127 | + truffleHogVerification, |
| 128 | + jobLimit, |
| 129 | + threads, |
| 130 | + confidenceFilter, |
| 131 | + hitTimeout, |
| 132 | + ) |
| 133 | + if err != nil { |
| 134 | + log.Fatal().Err(err).Msg("Failed initializing public scan options") |
| 135 | + } |
| 136 | + |
| 137 | + scanner := gitlabscan.NewScanner(scanOpts) |
| 138 | + logging.RegisterStatusHook(func() *zerolog.Event { |
| 139 | + queueLength := scanner.GetQueueStatus() |
| 140 | + return log.Info().Int("pendingjobs", queueLength) |
| 141 | + }) |
| 142 | + |
| 143 | + if err := scanner.Scan(); err != nil { |
| 144 | + log.Fatal().Err(err).Msg("Public scan failed") |
| 145 | + } |
| 146 | +} |
0 commit comments