-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathtasks.py
301 lines (256 loc) · 8.48 KB
/
tasks.py
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
import glob
from shutil import rmtree
from time import sleep
from git import Repo as GitRepo
from github import Github
from gitlab import Gitlab
from atlassian import bitbucket
import requests
from base64 import b64encode
from repos import (
BitbucketRepo,
GithubRepo,
GitlabRepo,
ADORepo,
RepoCredentials,
FilesystemRepo,
)
def onerror(func, path, exc_info):
import stat, os
# Is the error an access error?
if not os.access(path, os.W_OK):
os.chmod(path, stat.S_IWUSR)
func(path)
else:
raise
def get_branches(
path, max_branch_count, repo_name, threshold_date=None, single_branch=False
):
r = GitRepo.init(path)
branches = []
if single_branch:
branches = ["HEAD"]
else:
if len(r.remotes) > 0:
branches.extend(
[
"remotes/" + x.name
for x in r.remotes[0].refs
if x.is_detached == True
]
)
branches.extend(
[
head.name
for head in r.heads
if head.is_detached == True and not head.is_remote()
]
)
if threshold_date != None or len(branches) > max_branch_count:
branches_t = []
for branch in branches:
try:
latest_commit = r.commit(branch)
branches_t.append((branch, latest_commit.committed_date))
except: # nosec B112
continue # skip this branch
if threshold_date != None:
branches_t = [(b, v) for b, v in branches_t if v >= threshold_date]
if len(branches_t) > max_branch_count:
print(
f"Repo '{repo_name}' has {len(branches)} branches, only scanning the freshest {max_branch_count}. You can increase this limit"
)
branches_t.sort(key=lambda t: t[1], reverse=True)
branches_t = branches_t[0:max_branch_count]
branches = [b for b, v in branches_t]
return branches
class ProcessRepoResult(object):
def __init__(self, repo, status, message, findings=[]):
self.repo = repo
self.status = status
self.message = message
self.findings = findings
def __repr__(self):
if self.status == "SUCCESS":
return (
f"{self.status}::{self.repo.name}::{self.message}::{len(self.findings)}"
)
else:
return f"{self.status}::{self.repo.name}::{self.message}"
def process_repo(
repo,
conf,
functions,
single_branch=False,
extra_context=False,
cleanup=True,
threshold_date=None,
validate_https=True,
to_scan_list=None,
max_branch_count=50,
):
if to_scan_list is not None:
print(repo.html_url)
if repo.html_url not in to_scan_list:
return []
out = []
try:
path = repo.clone_repo(validate_https=validate_https)
except:
return [ProcessRepoResult(repo, "FAIL", "Could not clone")]
branches = get_branches(
path,
threshold_date=threshold_date,
single_branch=single_branch,
max_branch_count=max_branch_count,
repo_name=repo.name,
)
for branch in branches:
for function in functions:
try:
out.append(
ProcessRepoResult(
repo,
"SUCCESS",
function.__name__,
function(path, repo, branch, extra_context, conf),
)
)
except:
out.append(
ProcessRepoResult(repo, "FAIL", f"Could not {function.__name__}")
)
if cleanup:
for i in range(3):
try:
rmtree(path, onerror=onerror)
break
except:
sleep(10)
pass
ret = []
for function in functions:
deduped = {}
for result in out:
if result.status != "SUCCESS":
ret.append(result)
if result.status == "SUCCESS" and result.message == function.__name__:
for f in result.findings:
key = f"{f.commit}{f.file}{f.line}{f.hashed_secret}"
deduped[key] = f
ret.append(
ProcessRepoResult(repo, "SUCCESS", function.__name__, deduped.values())
)
return ret
def get_repos_from_bitbucket(workspace, username, password, dont_validate_https):
instance = bitbucket.Cloud(
username=username,
password=password,
cloud=True,
verify_ssl=not dont_validate_https,
)
workspace = instance.workspaces.get(workspace)
for repo in workspace.repositories.each():
clone_url = [
x["href"] for x in repo.data["links"]["clone"] if "https" == x["name"]
][0]
at = clone_url.index("@") + 1
clone_url = "https://" + clone_url[at:]
yield BitbucketRepo(
clone_url=clone_url,
name=repo.data["name"],
html_url=repo.data["links"]["html"]["href"],
credentials=RepoCredentials(password, username),
)
def get_repos_from_github(org, pat, dont_validate_https):
g = Github(pat, verify=not dont_validate_https)
organisation = g.get_organization(org)
repos = organisation.get_repos()
for repo in repos:
yield GithubRepo(
clone_url=repo.clone_url,
name=repo.name,
html_url=repo.html_url,
credentials=RepoCredentials(pat),
)
def get_repos_from_gitlab(org, pat, url, dont_validate_https):
def get_projects_from_group(g, group):
for project in group.projects.list(all=True):
yield project
for group in group.subgroups.list(all=True, all_available=True):
group = g.groups.get(group.id, lazy=True)
for project in get_projects_from_group(g, group):
yield project
g = Gitlab(private_token=pat, url=url, ssl_verify=not dont_validate_https)
group = g.groups.get(org, lazy=True)
repos = get_projects_from_group(g, group)
for repo in repos:
yield GitlabRepo(
clone_url=repo.http_url_to_repo,
name=repo.name,
html_url=repo.web_url,
credentials=RepoCredentials(pat, username="oauth2"),
)
def get_repos_from_ado(org, pat, dont_validate_https):
headers = {
"Accept": "application/json",
"Authorization": f"Basic {b64encode(f':{pat}'.encode('ascii')).decode()}",
}
response = requests.get(
f"https://dev.azure.com/{org}/_apis/projects",
headers=headers,
verify=not dont_validate_https,
timeout=60,
)
if response.content == b"":
return []
projects = [x["name"] for x in response.json()["value"]]
for project in projects:
response = requests.get(
f"https://dev.azure.com/{org}/{project}/_apis/git/repositories",
headers=headers,
verify=not dont_validate_https,
timeout=60,
)
if response.content == b"":
continue
for repo in response.json()["value"]:
yield ADORepo(
clone_url=repo["webUrl"],
name=repo["name"],
html_url=repo["webUrl"],
credentials=RepoCredentials(pat),
)
def get_repos_from_filesystem(path):
for repo_path in glob.glob(path + "/*", recursive=False):
yield FilesystemRepo(repo_path)
def get_repos(provider, **kwargs):
if "github" == provider:
return get_repos_from_github(
kwargs["org"],
kwargs["pat"],
kwargs["dont_validate_https"],
)
if "gitlab" == provider:
return get_repos_from_gitlab(
kwargs["group"],
kwargs["access_token"],
kwargs["gitlab_url"],
kwargs["dont_validate_https"],
)
if "azuredevops" == provider:
return get_repos_from_ado(
kwargs["org"],
kwargs["pat"],
kwargs["dont_validate_https"],
)
if "bitbucket" == provider:
return get_repos_from_bitbucket(
kwargs["workspace"],
kwargs["username"],
kwargs["password"],
kwargs["dont_validate_https"],
)
if "filesystem" == provider:
return get_repos_from_filesystem(kwargs["path"])
raise NotImplementedError("Unsupported provider: " + provider)