Skip to content

Commit f78451c

Browse files
authored
Merge pull request #37 from boringtools/33-suport-scanning-list-of-usernames
Added support for custom users scan
2 parents becb0c4 + a1c1bf7 commit f78451c

8 files changed

Lines changed: 99 additions & 16 deletions

File tree

README.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,19 @@ Scan and generate report with custom path
5757
git-alerts scan --org your-org-name --report-path /your/file/path/
5858
```
5959

60+
Scan custom list of GitHub users
61+
62+
```bash
63+
git-alerts scan --org your-org-name --users-file-path /path/to/csv/file
64+
```
65+
> Ensure to pass CSV file with the list of GitHub usernames
66+
67+
```csv
68+
username01
69+
username02
70+
username03
71+
```
72+
6073
### Monitor
6174

6275
Monitor new public repositories being created by your organization users
@@ -89,6 +102,19 @@ Monitor new public repositories being created by your organization users along w
89102
git-alerts monitor --org your-org-name --gitleaks --slack-alert
90103
```
91104

105+
Monitor custom list of GitHub users
106+
107+
```bash
108+
git-alerts monitor --org your-org-name --users-file-path /path/to/csv/file
109+
```
110+
> Ensure to pass CSV file with the list of GitHub usernames
111+
112+
```csv
113+
username01
114+
username02
115+
username03
116+
```
117+
92118
### Secrets
93119

94120
Scan with secrets detection using Trufflehog
@@ -105,6 +131,19 @@ Scan with secrets detection using Gitleaks
105131
git-alerts detect --org your-org-name --gitleaks
106132
```
107133

134+
Scan with secrets detection using custom list of GitHub users
135+
136+
```bash
137+
git-alerts detect --org your-org-name --users-file-path /path/to/csv/file --gitleaks
138+
```
139+
> Ensure to pass CSV file with the list of GitHub usernames
140+
141+
```csv
142+
username01
143+
username02
144+
username03
145+
```
146+
108147
## Documentation
109148

110149
[docs](https://github.com/boringtools/git-alerts/tree/main/docs)

cmd/root.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,5 @@ func init() {
2424
rootCmd.PersistentFlags().StringVarP(&common.GitHubOrg, "org", "o", "", "GitHub organization name")
2525
rootCmd.MarkPersistentFlagRequired("org")
2626
rootCmd.PersistentFlags().StringVarP(&common.ReportPath, "report-path", "r", "/tmp/", "Report file path")
27+
rootCmd.PersistentFlags().StringVarP(&common.UsersFilePath, "users-file-path", "u", "", "Users file path (CSV)")
2728
}

pkg/common/config.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ import (
44
"github.com/boringtools/git-alerts/pkg/models"
55
)
66

7-
func GetGitHubAPIEndPoints() *models.GitHubAPIEndPoints {
7+
func GetGitHubAPIEndPoints(username string) *models.GitHubAPIEndPoints {
88
return &models.GitHubAPIEndPoints{
99
GetUsers: GitHubAPIBaseURL + "/orgs/" + GitHubOrg + "/members",
10+
GetUsersRepo: GitHubAPIBaseURL + "/users/" + username + "/repos",
1011
}
1112
}
1213

pkg/common/read_data.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package common
22

33
import (
4+
"encoding/csv"
45
"io"
56
"os"
67
)
@@ -22,3 +23,28 @@ func GetJSONFileContent(filePath string) ([]byte, error) {
2223

2324
return byteData, nil
2425
}
26+
27+
func GetCSVFileContent(filePath string) ([]string, error) {
28+
openFile, errOpenFile := os.Open(filePath)
29+
30+
if errOpenFile != nil {
31+
return nil, errOpenFile
32+
}
33+
34+
defer openFile.Close()
35+
36+
reader := csv.NewReader(openFile)
37+
records, errRecords := reader.ReadAll()
38+
39+
if errRecords != nil {
40+
return nil, errRecords
41+
}
42+
43+
var lines []string
44+
45+
for _, record := range records {
46+
lines = append(lines, record[0])
47+
}
48+
49+
return lines, nil
50+
}

pkg/common/variables.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ var (
88
TrufflehogScan bool
99
TrufflehogVerifiedScan bool
1010
GitleaksScan bool
11+
UsersFilePath string
1112
)
1213

1314
var (

pkg/github/get_org_users.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ var (
1717
func GetGitHubUsers() ([]byte, error) {
1818
ui.PrintMsg("Fetching %s users...", common.GitHubOrg)
1919

20-
url := common.GetGitHubAPIEndPoints().GetUsers
20+
url := common.GetGitHubAPIEndPoints("").GetUsers
2121
parameters := map[string]string{
2222
"per_page": "100",
2323
}

pkg/github/get_users_repos.go

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,30 +8,44 @@ import (
88
"github.com/boringtools/git-alerts/pkg/models"
99
)
1010

11-
type RepoURL struct {
12-
URL string `json:"repos_url"`
13-
}
14-
1511
var (
16-
rURL []RepoURL
12+
users []models.GitHubUser
1713
repos []models.GitHubRepository
1814
allRepos []models.GitHubRepository
1915
)
2016

2117
func GetGitHubUsersRepos() ([]byte, error) {
22-
ui.PrintMsg("Fetching " + common.GitHubOrg + " users public repositories...")
23-
24-
users, _ := common.GetJSONFileContent(common.GetReportFilePaths().GitHubOrgUsers)
25-
json.Unmarshal(users, &rURL)
18+
ui.PrintMsg("Fetching %s users public repositories...", common.GitHubOrg)
2619

2720
parameters := map[string]string{
2821
"per_page": "100",
2922
}
30-
for _, value := range rURL {
31-
usersRepo, _, _ := GetGitHubResponse(value.URL, common.AuthenticatedScan, parameters)
3223

33-
json.Unmarshal(usersRepo, &repos)
34-
allRepos = append(allRepos, repos...)
24+
if common.UsersFilePath == "" {
25+
data, _ := common.GetJSONFileContent(common.GetReportFilePaths().GitHubOrgUsers)
26+
json.Unmarshal(data, &users)
27+
28+
for _, value := range users {
29+
usersRepo, _, _ := GetGitHubResponse(value.ReposUrl, common.AuthenticatedScan, parameters)
30+
31+
json.Unmarshal(usersRepo, &repos)
32+
allRepos = append(allRepos, repos...)
33+
}
34+
35+
} else {
36+
37+
users, errUsers := common.GetCSVFileContent(common.UsersFilePath)
38+
39+
if errUsers != nil {
40+
return nil, errUsers
41+
}
42+
43+
for _, username := range users {
44+
repos_url := common.GetGitHubAPIEndPoints(username).GetUsersRepo
45+
usersRepo, _, _ := GetGitHubResponse(repos_url, common.AuthenticatedScan, parameters)
46+
json.Unmarshal(usersRepo, &repos)
47+
allRepos = append(allRepos, repos...)
48+
}
3549
}
3650

3751
jsonData, err := json.Marshal(allRepos)

pkg/models/models.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ type Limits struct {
3636
}
3737

3838
type GitHubAPIEndPoints struct {
39-
GetUsers string
39+
GetUsers string
40+
GetUsersRepo string
4041
}
4142

4243
type ReportFileNames struct {

0 commit comments

Comments
 (0)