-
Notifications
You must be signed in to change notification settings - Fork 3
/
fork_audit.py
240 lines (201 loc) · 8.13 KB
/
fork_audit.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
import argparse
import json
import os
import shutil
import requests
MEMBERS_URL = 'https://api.github.com/orgs/{}/members'
REPOS_URL = 'https://api.github.com/orgs/{}/repos?type=private'
FORKS_URL = 'https://api.github.com/repos/{}/forks'
FOUND_MESSAGE = 'Found {} {} for the {} organization'
FOUND_IN_CACHE_MESSAGE = '{} (Cached)'.format(FOUND_MESSAGE)
def find_forked_repos(**kwargs):
if kwargs.get('clearcache'):
_clear_cache()
organization = kwargs.get('organization')
github_repos, repos_cached = organization_repos(organization)
message = FOUND_IN_CACHE_MESSAGE if repos_cached else FOUND_MESSAGE
print(message.format(len(github_repos), 'repos', organization))
org_members, org_members_cached = organization_members(organization)
message = FOUND_IN_CACHE_MESSAGE if org_members_cached else FOUND_MESSAGE
print(message.format(len(org_members), 'members', organization))
for repo_name in github_repos:
forks, forks_cached = forked_repos(repo_name, organization)
if len(forks) == 0:
continue
print(
'Found {} forks of repo: {}{}'.format(
len(forks),
repo_name,
' (Cached)' if forks_cached else ''
)
)
if not kwargs.get('shield'):
print(' Forkers:')
for fork in forks:
print(' - {}'.format(fork.get('owner')))
fork_collaborators = fetch_fork_collaborators(
fork,
organization
)
determine_foreign_collaborators(
fork_collaborators,
org_members,
organization
)
def organization_repos(organization):
CACHE_NAME = '{}/organization_repos'.format(organization)
cached_data = _cached_data(CACHE_NAME)
if cached_data:
return cached_data, True
repos = get(REPOS_URL.format(organization))
organization_repos = [repo.get('full_name') for repo in repos]
_cache_data(CACHE_NAME, organization_repos)
return organization_repos, False
def organization_members(organization):
CACHE_NAME = '{}/organization_members'.format(organization)
cached_data = _cached_data(CACHE_NAME)
if cached_data:
return cached_data, True
members = get(MEMBERS_URL.format(organization))
organization_members = [member.get('login') for member in members]
_cache_data(CACHE_NAME, organization_members)
return organization_members, False
def forked_repos(repo_name, organization):
CACHE_NAME = '{}/repos/{}'.format(organization, repo_name)
cached_data = _cached_data(CACHE_NAME)
if cached_data is not None:
return cached_data, True
forks = []
all_forks = get(FORKS_URL.format(repo_name))
for fork in all_forks:
forks.append({
'name': repo_name,
'owner': fork.get('owner', {}).get('login'),
'collabortors_url': fork.get(
'collaborators_url'
).rstrip('{/collaborator}')
})
_cache_data(CACHE_NAME, forks)
return forks, False
def fetch_fork_collaborators(fork, organization):
# CACHE_NAME = '{}/fork_collaborators/{}'.format(
# organization,
# fork.get('name')
# )
# cached_data = _cached_data(CACHE_NAME)
# if cached_data:
# print(' Using cached collaborators for {}'.format(
# fork.get('name'))
# )
# return cached_data
try:
collaborators = get(fork.get('collabortors_url'))
except Exception as e:
print (' {}'.format(e))
collaborators = []
collaborators = [
collaborator.get('login') for collaborator in collaborators
]
# _cache_data(CACHE_NAME, collaborators)
return collaborators
def determine_foreign_collaborators(fork_collaborators, org_members, org):
illegal_collaborators = []
for collaborator in fork_collaborators:
if collaborator not in org_members:
illegal_collaborators.append(collaborator)
if illegal_collaborators:
print(' - Potential security vulnerability: found {} collaborators not in the {} organization.'.format(len(illegal_collaborators), org)) # noqa
for illegal_collaborator in illegal_collaborators:
print(' - {}'.format(illegal_collaborator))
def _cached_data(filename):
filename = 'cache/{}.txt'.format(filename)
try:
with open(filename, 'r+') as f:
contents = f.read()
if contents:
return json.loads(contents)
except IOError:
pass
def _cache_data(filename, data):
filename = 'cache/{}.txt'.format(filename)
try:
os.makedirs(filename[:filename.index(filename.split('/')[-1])])
except OSError as e:
if e.errno != 17:
print('Could not create cache for: {}'.format(filename))
with open(filename, 'w+') as f:
f.write(json.dumps(data))
def _clear_cache():
try:
shutil.rmtree('cache')
except OSError:
pass
def get(url, pagenate=True):
github_session = _github_session()
response_data = []
if not pagenate:
response = github_session.get(url)
text = response.text
if response.status_code != 200:
print('Encountered error during GET request: {}'.format(
response.text
))
text = {}
return json.loads(text)
for x in xrange(10000):
if '?' not in url:
paged_url = '{}?page={}'.format(url, x+1)
else:
paged_url = '{}&page={}'.format(url, x+1)
response = github_session.get(paged_url)
paged_response = json.loads(response.text)
if not paged_response or response.status_code != 200:
if response.status_code != 200:
raise Exception(
'Encountered error during GET request to {} : {}'.format(
paged_url,
response.text
)
)
break
response_data.extend(paged_response)
return response_data
def _github_session():
"""Generates a session with valid headers for GitHub."""
github_token = os.environ.get('GITHUB_TOKEN')
if not github_token:
raise EnvironmentError('GITHUB_TOKEN missing from environment')
session = requests.Session()
session.headers.update({
'Authorization': 'token {}'.format(github_token),
'Accept': 'application/vnd.github.v3+json'
})
return session
if __name__ == '__main__':
defaut_organization = os.environ.get('ORGANIZATION')
parser = argparse.ArgumentParser(description='Audit GitHub forks')
parser.add_argument('--clearcache', '-cc', default=False,
action='store_true',
help='Clear all of the cached data.')
# TODO: Implement additional caching logic
# parser.add_argument('--clearcacheorg', '-cco',
# default=defaut_organization, type=str,
# help='Clear all cached data for an organization.')
# parser.add_argument('--clearcacherepos', '-ccr', default=False,
# action='store_true',
# help='Clear cached repo data.')
# parser.add_argument('--clearcachemembers', '-ccm', default=False,
# action='store_true',
# help='Clear cached members data.')
# parser.add_argument('--clearcacheforks', '-ccf', default=False,
# action='store_true',
# help='Clear cached fork data.')
# parser.add_argument('--clearcacheforkcollab', '-ccfc', default=False,
# action='store_true',
# help='Clear cached fork collaborators data.')
parser.add_argument('--organization', '-o', default=defaut_organization,
type=str, help='The GitHub organization to audit.')
parser.add_argument('--shield', '-s', default=False, action='store_true',
help='The GitHub organization to audit.')
args = parser.parse_args()
find_forked_repos(**vars(args))