-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaudit_out.py
72 lines (68 loc) · 2.94 KB
/
audit_out.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
from bitbucket import BitBucket
import argparse
from datetime import datetime as dt
def check_input(param, types):
escape_char = ('\n', '\t', ' ', '\r', '\a', '\f', '\v', '\b')
for char in escape_char:
param = param.replace(char, '')
if len(param) == 0:
print("Your {type} doesn't correct".format(type = types))
exit()
else:
return param
parser = argparse.ArgumentParser()
parser.add_argument(
'user', type=str, help='Stash admin username')
parser.add_argument(
'password', type=str, help='Stash admin password')
parser.add_argument(
'url', type=str, help='Full URL for sonar'
)
args = parser.parse_args()
user = args.user
user = check_input(user, 'login')
password = args.password
password = check_input(password, 'password')
url = args.url
url = check_input(url, 'url')
print(str(dt.now()))
bb = BitBucket(url, user, password)
projects = bb.get_project_ids()
for project in projects:
try:
repos = bb.get_repo_ids(project)
except Exception:
repos = []
try:
project_permissions = bb.get_permissions_users_in_project(project)[1]['values']
except Exception:
project_permissions = []
for permission in project_permissions:
if permission['permission'] == 'PROJECT_ADMIN':
if 'out-' in permission['user']['name'].lower():
with open('audit_result.txt' , 'a') as result_file:
try:
result_file.write(project + ':' + permission['user']['name'] + ';' + permission['user']['emailAddress'] + '\n')
except Exception:
result_file.write(project + ':' + permission['user']['name'] + '\n')
result = bb.delete_permission_user_in_project(project, permission['user']['name'])
try:
result = bb.create_permission_user_in_project(project,'PROJECT_WRITE',permission['user']['name'])
except:
result = ''
for repo in repos:
try:
repos_repmissions = bb.get_permissions(project, repo)[1]['values']
except Exception:
repos_repmissions = []
for permission in repos_repmissions:
if permission['permission'] == 'REPOS_ADMIN':
if 'out-' in permission['user']['name'].lower():
with open('audit_result.txt', 'a') as result_file:
try:
result_file.write(project + ':' + repo + ':' + permission['user']['name'] + ';' + permission['user']['emailAddress'] + '\n')
except Exception:
result_file.write(project + ':' + repo + ':' + permission['user']['name'] + '\n')
result = bb.delete_permission_user(project, repo, permission['user']['name'])
result = bb.create_permission_user(project, repo, {'name':permission['user']['name'], 'permission':'REPO_WRITE'})
print(str(dt.now()))