-
Notifications
You must be signed in to change notification settings - Fork 0
/
github.go
84 lines (70 loc) · 1.81 KB
/
github.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package main
import (
"context"
"log"
"os"
"os/exec"
"path/filepath"
"github.com/go-git/go-git/v5"
"github.com/google/go-github/v50/github"
"github.com/urfave/cli/v2"
"golang.org/x/oauth2"
)
const DEFAULT_REPOS_DIR = "gitscanner_repos_tmp"
const DEFAULT_OUTPUT_DIR = "findings"
/**
* ScanGitHubOrganization scans a GitHub organization for vulnerabilities
* @param cCtx cli context
* @return error
*/
func ScanGitHubOrganization(cCtx *cli.Context) error {
var token string = cCtx.String("token")
var org string = cCtx.String("org")
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: token},
)
ctx := context.Background()
tc := oauth2.NewClient(ctx, ts)
client := github.NewClient(tc)
repos, _, err := client.Repositories.List(ctx, org, nil)
if err != nil {
return err
}
for repo := range repos {
println(repos[repo].GetCloneURL())
}
cloneAndScanAllRepositories(repos)
return nil
}
func cloneAndScanAllRepositories(repos []*github.Repository) error {
tmpDir := os.TempDir()
tmpReposDir := filepath.Join(tmpDir, DEFAULT_REPOS_DIR)
os.MkdirAll(tmpReposDir, os.ModePerm)
defer os.RemoveAll(tmpReposDir)
for repo := range repos {
var repoPath string = filepath.Join(tmpReposDir, repos[repo].GetName())
_, err := git.PlainClone(
repoPath,
false, // isBare
&git.CloneOptions{
URL: repos[repo].GetCloneURL(),
Progress: nil, // TODO: Make this configurable using verbose flag
})
if err != nil {
return err
}
os.MkdirAll(DEFAULT_OUTPUT_DIR, os.ModePerm)
out, err := exec.Command(
"gitleaks",
"detect",
"--source", repoPath,
"--exit-code", "0",
"--report-path", filepath.Join(DEFAULT_OUTPUT_DIR, repos[repo].GetName()+"-report.json")).Output()
if err != nil {
println(err.Error())
log.Fatal(err)
}
println(string(out))
}
return nil
}