-
Notifications
You must be signed in to change notification settings - Fork 1
/
sync.py
executable file
·311 lines (256 loc) · 9.52 KB
/
sync.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
302
303
304
305
306
307
308
309
310
311
#!/usr/bin/env python3
import os
import time
import giteapy
from giteapy.rest import ApiException
from pprint import pprint
import random
import string
from keycloak import KeycloakAdmin
PROTECTED_USERS=['gitea']
SYNC_INTERVAL_SECONDS=int(os.environ.get('SYNC_INTERVAL_SECONDS'))
KEYCLOAK_USERNAME=os.environ.get('KEYCLOAK_USERNAME')
KEYCLOAK_PASSWORD=os.environ.get('KEYCLOAK_PASSWORD')
KEYCLOAK_URL=os.environ.get('KEYCLOAK_URL')
KEYCLOAK_REALM=os.environ.get('KEYCLOAK_REALM')
KEYCLOAK_CLIENT_SECRET=os.environ.get('KEYCLOAK_CLIENT_SECRET')
GITEA_URL=os.environ.get('GITEA_URL')
GITEA_API_KEY=os.environ.get('GITEA_API_KEY')
keycloak_admin = None
# Configure API key authorization: AccessToken
configuration = giteapy.Configuration()
configuration.host = GITEA_URL
configuration.api_key['access_token'] = GITEA_API_KEY
# create an instance of the API class
gitea_admin = giteapy.AdminApi(giteapy.ApiClient(configuration))
gitea_org = giteapy.OrganizationApi(giteapy.ApiClient(configuration))
def get_random_string(length):
letters = string.ascii_lowercase
result_str = ''.join(random.choice(letters) for i in range(length))
return result_str
def get_keycloak_users():
retval = []
users = keycloak_admin.get_users({})
for user in users:
user_groups = keycloak_admin.get_user_groups(user_id=user['id'])
user['groups'] = []
for group in user_groups:
user['groups'].append(group['name'])
if 'gitea' in user['groups']:
retval.append(user)
return retval
def get_gitea_users():
try:
api_response = gitea_admin.admin_get_all_users()
return api_response
except ApiException as e:
print("Exception when calling AdminApi->admin_get_all_users: %s\n" % e)
return None
def disable_gitea_user(gitea_user):
body = giteapy.EditUserOption(
active=False,
admin=False,
prohibit_login=True,
email=gitea_user.email,
login_name=gitea_user.login
)
api_response = gitea_admin.admin_edit_user(gitea_user.login, body=body)
def update_gitea_user(keycloak_user):
try:
body = giteapy.EditUserOption(
active=True,
prohibit_login=False,
admin='gitea-admin' in keycloak_user['groups'],
allow_create_organization='gitea-admin' in keycloak_user['groups'],
email=keycloak_user['email'],
full_name=keycloak_user['attributes']['name'][0],
login_name=keycloak_user['id'],
password=get_random_string(20),
must_change_password=False,
source_id=2
)
except KeyError:
return
api_response = gitea_admin.admin_edit_user(keycloak_user['username'], body=body)
def create_gitea_user(keycloak_user):
try:
body = giteapy.CreateUserOption(
email=keycloak_user['email'],
full_name=keycloak_user['attributes']['name'][0],
login_name=keycloak_user['id'],
password=get_random_string(20),
username=keycloak_user['username'],
must_change_password=False,
source_id=2
)
except KeyError:
return
api_response = gitea_admin.admin_create_user(body=body)
def get_gitea_members_team(organization):
teams = gitea_org.org_list_teams(organization)
for team in teams:
if team.name == 'Members':
return team.id
return None
def get_gitea_organizations():
limit = 10
page = 0
organizations = []
while True:
page += 1
api_response = gitea_admin.admin_get_all_orgs(page=page, limit=limit)
organizations.extend(api_response)
if (len(api_response) < limit):
break
retval = []
for organization in organizations:
members_id = get_gitea_members_team(organization.username)
members = gitea_org.org_list_team_members(members_id)
usernames = []
for member in members:
usernames.append(member.login)
retval.append({'organization': organization, 'members': usernames})
return retval
def create_gitea_organization(keycloak_group):
body = giteapy.CreateOrgOption(
username=keycloak_group['name'],
visibility='private',
)
api_response = gitea_admin.admin_create_org('gitea', body)
body = giteapy.CreateTeamOption(
can_create_org_repo=True,
includes_all_repositories=True,
name="Members",
permission='write',
units=['repo.code',
'repo.issues',
'repo.pulls',
'repo.releases',
'repo.wiki',
'repo.ext_wiki',
'repo.ext_issues',
'repo.projects']
)
api_response = gitea_org.org_create_team(keycloak_group['name'], body=body)
def update_gitea_organization(keycloak_group):
team_id = get_gitea_members_team(keycloak_group['name'])
body = giteapy.EditTeamOption(
can_create_org_repo=True,
includes_all_repositories=True,
name="Members",
permission='write',
units=['repo.code',
'repo.issues',
'repo.pulls',
'repo.releases',
'repo.wiki',
'repo.ext_wiki',
'repo.ext_issues',
'repo.projects']
)
api_response = gitea_org.org_edit_team(team_id, body=body)
def add_user_to_gitea_organization(organization, login):
team_id = get_gitea_members_team(organization)
if team_id == None:
print(f"Couldn't find the 'Members' team for organization {organization}")
return
gitea_org.org_add_team_member(team_id, login)
def delete_user_from_gitea_organization(organization, login):
team_id = get_gitea_members_team(organization)
if team_id == None:
print(f"Couldn't find the 'Members' team for organization {organization}")
return
gitea_org.org_remove_team_member(team_id, login)
def get_keycloak_groups():
groups = keycloak_admin.get_groups()
retval = []
for group in groups:
g = keycloak_admin.get_group(group['id'])
retval.append(g)
return retval
def sync():
global keycloak_admin
keycloak_admin = KeycloakAdmin(
server_url=KEYCLOAK_URL,
username=KEYCLOAK_USERNAME,
password=KEYCLOAK_PASSWORD,
realm_name=KEYCLOAK_REALM,
client_secret_key=KEYCLOAK_CLIENT_SECRET,
verify=True
)
ku = get_keycloak_users()
gu = get_gitea_users()
for keycloak_user in ku:
found = False
disabled = False
for gitea_user in gu:
if gitea_user.login == keycloak_user['username']:
found = True
if not keycloak_user['enabled']:
disabled = True
break
if not found:
print(f"User {keycloak_user['username']} does not exist in gitea")
create_gitea_user(keycloak_user)
update_gitea_user(keycloak_user)
elif not disabled:
print(f"Updating user {keycloak_user['username']}")
if keycloak_user['enabled']:
update_gitea_user(keycloak_user)
gu = get_gitea_users()
for gitea_user in gu:
found = False
disabled = False
for keycloak_user in ku:
if gitea_user.login == keycloak_user['username']:
found = True
if not keycloak_user['enabled']:
disabled = True
break
if not found and gitea_user.login not in PROTECTED_USERS:
print(f"User {gitea_user.login} found in gitea but does not exist in keycloak")
disable_gitea_user(gitea_user)
if disabled:
print(f"User {gitea_user.login} disabled in keycloak")
disable_gitea_user(gitea_user)
organizations = get_gitea_organizations()
keycloak_groups = get_keycloak_groups()
for group in keycloak_groups:
found = False
invalid = False
try:
if 'customer' in group['attributes']['businessCategory']:
for org in organizations:
if org['organization'].username == group['name']:
found = True
else:
invalid = True
except KeyError:
invalid = True
if not found and not invalid:
print(f"Organization {group['name']} not found, creating")
create_gitea_organization(group)
if found and not invalid:
print(f"Updating organization {group['name']}")
update_gitea_organization(group)
organizations = get_gitea_organizations()
for organization in organizations:
expected_members = []
for keycloak_user in ku:
if organization['organization'].username in keycloak_user['groups']:
expected_members.append(keycloak_user['username'])
for expected_member in expected_members:
if not expected_member in organization['members']:
print(f"Adding user {expected_member} to organization {organization['organization'].username}")
add_user_to_gitea_organization(organization['organization'].username, expected_member)
for member in organization['members']:
if member not in expected_members:
print(f"Removing user {member} from organziation {organization['organization'].username}")
delete_user_from_gitea_organization(organization['organization'].username, member)
while True:
try:
sync()
except Exception as e:
print(e)
print("Sleeping")
time.sleep(SYNC_INTERVAL_SECONDS)