-
Notifications
You must be signed in to change notification settings - Fork 2
GitHub Action #283
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
frjcomp
wants to merge
13
commits into
main
Choose a base branch
from
github-action
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
GitHub Action #283
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
c61ecf2
test
frjcomp 5b6caf9
trigger test joib
frjcomp 698b2f2
added workflow permissions
frjcomp 59f88e5
debug
frjcomp af1ec67
fix client
frjcomp 903507a
wip
frjcomp 37e2052
run scan
frjcomp e7d004c
fix nil pinter
frjcomp 5968daf
end the game
frjcomp f2e6d57
end
frjcomp 8ee92ee
end correctly
frjcomp 1f7d89d
try again
frjcomp 37dcbd8
brr
frjcomp File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
|
|
||
| name: Wait for Other Runs testing | ||
|
|
||
| on: | ||
| push: | ||
|
|
||
| permissions: | ||
| actions: read | ||
| contents: read | ||
|
|
||
| jobs: | ||
| scan-secrets: | ||
| runs-on: ubuntu-latest | ||
| steps: | ||
| - uses: actions/checkout@v4 | ||
| - uses: actions/setup-go@v5 | ||
| with: | ||
| go-version: '1.22' | ||
| - name: Wait for other runs on same commit | ||
| working-directory: src/pipeleak | ||
| env: | ||
| GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | ||
| GITHUB_REPOSITORY: ${{ github.repository }} | ||
| GITHUB_SHA: ${{ github.sha }} | ||
| GITHUB_RUN_ID: ${{ github.run_id }} | ||
| run: go run main.go gh action | ||
| dummy-wait: | ||
| runs-on: ubuntu-latest | ||
| steps: | ||
| - name: Wait 3 minutes, echo test, then wait 2 more minutes | ||
| run: | | ||
| sleep 20 | ||
| echo test | ||
| sleep 120 | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,149 @@ | ||
| package github | ||
|
|
||
| import ( | ||
| "context" | ||
| "os" | ||
| "strconv" | ||
| "strings" | ||
| "sync" | ||
| "time" | ||
|
|
||
| "github.com/CompassSecurity/pipeleak/helper" | ||
| "github.com/CompassSecurity/pipeleak/scanner" | ||
| "github.com/google/go-github/v69/github" | ||
| "github.com/rs/zerolog/log" | ||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| func NewActionScanCmd() *cobra.Command { | ||
| scanActionCmd := &cobra.Command{ | ||
| Use: "action", | ||
| Short: "Scan all jobs of the action workflow running", | ||
| Long: `Scan GitHub Actions workflow runs and artifacts for secrets`, | ||
| Example: `pipeleak gh action -t $GH_TOKEN`, | ||
| Run: ScanAction, | ||
| } | ||
|
|
||
| scanActionCmd.PersistentFlags().BoolVarP(&options.Verbose, "verbose", "v", false, "Verbose logging") | ||
|
|
||
| return scanActionCmd | ||
| } | ||
|
|
||
| func ScanAction(cmd *cobra.Command, args []string) { | ||
| options.HttpClient = helper.GetPipeleakHTTPClient() | ||
| helper.SetLogLevel(options.Verbose) | ||
| scanner.InitRules(options.ConfidenceFilter) | ||
| scanWorkflowRuns() | ||
| log.Info().Msg("Scan Finished, Bye Bye 🏳️🌈🔥") | ||
| } | ||
|
|
||
| func scanWorkflowRuns() { | ||
| log.Info().Msg("Scanning GitHub Actions workflow runs for secrets") | ||
| options.Context = context.WithValue(context.Background(), github.BypassRateLimitCheck, true) | ||
|
|
||
| var wg sync.WaitGroup | ||
| scannedRuns := make(map[int64]struct{}) | ||
|
|
||
| token := os.Getenv("GITHUB_TOKEN") | ||
| if token == "" { | ||
| log.Fatal().Msg("GITHUB_TOKEN not set") | ||
| } | ||
|
|
||
| client := setupClient(token) | ||
| options.Client = client | ||
|
|
||
| repoFull := os.Getenv("GITHUB_REPOSITORY") | ||
| if repoFull == "" { | ||
| log.Fatal().Msg("GITHUB_REPOSITORY not set") | ||
| } | ||
|
|
||
| parts := strings.Split(repoFull, "/") | ||
| if len(parts) != 2 { | ||
| log.Fatal().Str("repository", repoFull).Msg("invalid GITHUB_REPOSITORY") | ||
| } | ||
|
|
||
| owner, repo := parts[0], parts[1] | ||
| log.Info().Str("owner", owner).Str("repo", repo).Msg("Repository to scan") | ||
|
|
||
| repository, _, err := client.Repositories.Get(options.Context, owner, repo) | ||
| if err != nil { | ||
| log.Fatal().Err(err).Msg("Failed to fetch repository") | ||
| } | ||
|
|
||
| sha := os.Getenv("GITHUB_SHA") | ||
| if sha == "" { | ||
| log.Fatal().Msg("GITHUB_SHA not set") | ||
| } else { | ||
| log.Info().Str("sha", sha).Msg("Current commit sha") | ||
| } | ||
|
|
||
| runIDStr := os.Getenv("GITHUB_RUN_ID") | ||
| if runIDStr == "" { | ||
| log.Fatal().Msg("GITHUB_RUN_ID not set") | ||
| } else { | ||
| log.Info().Str("runID", runIDStr).Msg("Current run ID") | ||
| } | ||
|
|
||
| currentRunID, _ := strconv.ParseInt(runIDStr, 10, 64) | ||
| opts := &github.ListWorkflowRunsOptions{ | ||
| ListOptions: github.ListOptions{PerPage: 100}, | ||
| HeadSHA: sha, | ||
| } | ||
|
|
||
| for { | ||
| allRunsCompleted := true | ||
|
|
||
| for { | ||
| runs, resp, err := client.Actions.ListRepositoryWorkflowRuns(options.Context, owner, repo, opts) | ||
|
|
||
| log.Info().Int("count", len(runs.WorkflowRuns)).Msg("Fetched workflow runs") | ||
|
|
||
| if err != nil { | ||
| log.Fatal().Stack().Err(err).Msg("Failed listing workflow runs") | ||
| } | ||
|
|
||
| for _, run := range runs.WorkflowRuns { | ||
| status := run.GetStatus() | ||
| if status != "completed" { | ||
| allRunsCompleted = false | ||
| } | ||
|
|
||
| if run.GetID() != currentRunID { | ||
| log.Info().Int64("run", run.GetID()).Str("status", status).Str("name", run.GetName()).Str("url", *run.HTMLURL).Msgf("Workflow run") | ||
|
|
||
| if status == "completed" { | ||
| if _, scanned := scannedRuns[run.GetID()]; !scanned { | ||
| scannedRuns[run.GetID()] = struct{}{} | ||
| wg.Add(1) | ||
| go func(runCopy *github.WorkflowRun) { | ||
| defer wg.Done() | ||
| log.Warn().Int64("run", run.GetID()).Str("status", status).Str("name", run.GetName()).Str("url", *run.HTMLURL).Msgf("Running scan for workflow run") | ||
| scanRun(client, repository, runCopy) | ||
| }(run) | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if resp.NextPage == 0 { | ||
| break | ||
| } | ||
| opts.Page = resp.NextPage | ||
| } | ||
|
|
||
| if allRunsCompleted { | ||
| log.Info().Msg("⏳ Waiting for any remaining scans to finish...") | ||
| break | ||
| } else { | ||
| log.Info().Msg("⏳ Some runs are still running") | ||
| time.Sleep(3 * time.Second) | ||
| } | ||
| } | ||
|
|
||
| wg.Wait() | ||
| } | ||
|
|
||
| func scanRun(client *github.Client, repo *github.Repository, workflowRun *github.WorkflowRun) { | ||
| downloadWorkflowRunLog(client, repo, workflowRun) | ||
| listArtifacts(client, workflowRun) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.