-
Notifications
You must be signed in to change notification settings - Fork 1
/
keycloak_to_azure.py
executable file
·402 lines (366 loc) · 14.5 KB
/
keycloak_to_azure.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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
#!/usr/bin/env python
# vim: set syn=on ts=4 sw=4 sts=0 et foldmethod=indent:
# purpose: sync users/groups from keycloak to azure AD federated accounts
# - invite keycloak users that are in azure keycloak groups to azure
# - put all the users in the azure keycloak groups into their equivalents in azure
# - remove users from azure groups if they are not in their equivalents in keycloak
# copyright: B1 Systems GmbH <[email protected]>, 2023.
# license: GPLv3+, http://www.gnu.org/licenses/gpl-3.0.html
# author: Eike Waldt <[email protected]>, 2023.
# the keycloak user needs these roles:
# - query-clients
# - query-groups
# - query-users
# - view-clients
# - view-groups
# - view-users
# To use it set these environment variables:
# - KEYCLOAK_USER
# - KEYCLOAK_PASSWORD
# the azure "app registration" can be created at
# https://portal.azure.com/#view/Microsoft_AAD_RegisteredApps/CreateApplicationBlade
# It needs these API permissions:
# - Microsoft Graph -> User.*
# - Microsoft Graph -> Group.*
# - Microsoft Graph -> GroupMember.*
# To use it set these environment variables:
# - AZURE_TENANT_ID
# - AZURE_CLIENT_ID
# - AZURE_CLIENT_SECRET (max expiry is 24 month!!!)
# Have a look at the defined config file for customization.
import os
import json
import logging
import requests
import keycloak
import yaml
import strictyaml
import path
CONFIGFILE = "keycloak_to_azure.config.yaml"
CONFIG_SCHEMA = strictyaml.Map({
"logfile": strictyaml.Str(),
"keycloak_url": strictyaml.Str(),
"keycloak_realm": strictyaml.Str(),
"groups": strictyaml.Seq(strictyaml.Map({
"keycloak": strictyaml.Str(),
"azure": strictyaml.Str()}
))})
# Authenticate to azure graph API
# https://learn.microsoft.com/en-us/graph/?view=graph-rest-1.0
AZURE_TENANT_ID = os.getenv("AZURE_TENANT_ID")
AZURE_CLIENT_ID = os.getenv("AZURE_CLIENT_ID")
AZURE_CLIENT_SECRET = os.getenv("AZURE_CLIENT_SECRET")
AZURE_HEADERS = {"Content-Type": "application/json"}
# Authenticate to keycloak
# https://python-keycloak.readthedocs.io/en/latest/
# https://github.com/marcospereirampj/python-keycloak/blob/master/src/keycloak/keycloak_admin.py
KEYCLOAK_ADMIN_USER = os.getenv("KEYCLOAK_USER")
KEYCLOAK_ADMIN_PASSWORD = os.getenv("KEYCLOAK_PASSWORD")
# default azure URLs
AZURE_LOGIN_URL = "https://login.microsoftonline.com"
AZURE_PORTAL_URL = "https://portal.azure.com"
AZURE_GRAPH_HOST = "graph.microsoft.com"
AZURE_GRAPH_API_URL = "https://" + AZURE_GRAPH_HOST + "/v1.0/"
# load config
def load_config():
config = strictyaml.load(path.Path(CONFIGFILE).text(), CONFIG_SCHEMA).data
return config
# get azure access token
def get_azure_token():
url = AZURE_LOGIN_URL + "/" + AZURE_TENANT_ID + "/oauth2/token"
body = {
"grant_type": "client_credentials",
"client_secret": AZURE_CLIENT_SECRET,
"client_id": AZURE_CLIENT_ID,
"resource": "https://" + AZURE_GRAPH_HOST
}
try:
response = requests.post(url, data=body)
response.raise_for_status()
except requests.exceptions.RequestException as err:
logging.error("error getting azure access token: error: %s", err)
raise err
data = response.json()
out = data["access_token"]
return out
# add access token to headers
def get_azure_headers():
azure_token = get_azure_token()
#global AZURE_HEADERS
AZURE_HEADERS["Authorization"] = "Bearer " + azure_token
return AZURE_HEADERS
# get azure groups
def get_azure_groups():
url = AZURE_GRAPH_API_URL + "/groups"
try:
response = requests.get(url, headers=AZURE_HEADERS)
response.raise_for_status()
except requests.exceptions.RequestException as err:
logging.error("error getting azure groups: error: %s", err)
raise err
data = response.json()['value']
out = []
for entry in data:
out.append(
{
"id": entry["id"],
"name": entry["displayName"] or "",
}
)
return out
# get azure users
def get_azure_users():
url = AZURE_GRAPH_API_URL + "/users"
try:
response = requests.get(url, headers=AZURE_HEADERS)
response.raise_for_status()
except requests.exceptions.RequestException as err:
logging.error("error getting azure users: error: %s", err)
raise err
data = response.json()['value']
out = []
for entry in data:
out.append(
{
"id": entry["id"],
"email": entry["mail"] or "",
"firstname": entry["givenName"] or "",
"lastname": entry["surname"] or "",
"name": entry["displayName"] or "",
}
)
return out
# get azure group members
def get_azure_group_members(id):
url = AZURE_GRAPH_API_URL + "/groups/" + id + "/members"
try:
response = requests.get(url, headers=AZURE_HEADERS)
response.raise_for_status()
except requests.exceptions.RequestException as err:
logging.error(
"error getting azure group members: %s, error: %s",
id,
err)
raise err
data = response.json()['value']
out = []
for entry in data:
out.append(entry["id"])
return out
# invite keycloak users that are in azure keycloak groups to azure
def invite_user(email):
logging.debug("inviting azure user: %s", email)
invite_redirect_url = AZURE_PORTAL_URL + "/" + AZURE_TENANT_ID
url = AZURE_GRAPH_API_URL + "/invitations"
body = {"invitedUserEmailAddress": email,
"inviteRedirectUrl": invite_redirect_url}
body = json.dumps(body)
try:
response = requests.post(url, data=body, headers=AZURE_HEADERS)
response.raise_for_status()
except requests.exceptions.RequestException as err:
logging.error("error inviting azure user: %s, error: %s", email, err)
raise err
data = response.json()
out = data['invitedUser']['id']
logging.info("invited azure user: %s", email)
return out
# update user
def update_user(id, firstname, lastname):
url = AZURE_GRAPH_API_URL + "/directoryObjects/" + id
body = {
"userType": "Member",
"givenName": firstname,
"surname": lastname,
"displayName": firstname + " " + lastname,
}
body = json.dumps(body)
try:
response = requests.patch(url, data=body, headers=AZURE_HEADERS)
response.raise_for_status()
except requests.exceptions.RequestException as err:
logging.error(
"error updating azure user: %s - %s %s, error: %s",
id,
firstname,
lastname,
err)
raise err
logging.info("updated azure user: %s - %s %s", id, firstname, lastname)
# add user to azure group
def groupadd_user(user_id, group_id):
logging.debug("adding user to group: %s %s", user_id, group_id)
user_url = AZURE_GRAPH_API_URL + "/directoryObjects/" + user_id
url = AZURE_GRAPH_API_URL + "/groups/" + group_id + "/members/$ref"
body = {"@odata.id": user_url}
body = json.dumps(body)
try:
response = requests.post(url, data=body, headers=AZURE_HEADERS)
response.raise_for_status()
except requests.exceptions.RequestException as err:
logging.error(
"error adding user to group: %s %s, error: %s",
user_id,
group_id,
err)
raise err
logging.info("added user to group: %s %s", user_id, group_id)
# remove user from azure group
def groupremove_user(user_id, group_id):
logging.debug("removing user from group: %s %s", user_id, group_id)
url = AZURE_GRAPH_API_URL + "/groups/" + \
group_id + "/members/" + user_id + "/$ref"
try:
response = requests.delete(url, headers=AZURE_HEADERS)
response.raise_for_status()
except requests.exceptions.RequestException as err:
logging.error(
"error adding user to group: %s %s, error: %s",
user_id,
group_id,
err)
raise err
logging.info("removed user from group: %s %s", user_id, group_id)
def main():
config = load_config()
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s [%(levelname)s] %(message)s",
handlers=[
logging.FileHandler(config['logfile']),
logging.StreamHandler()
]
)
logging.info("start run")
# authenticate to keycloak
keycloak_admin = keycloak.KeycloakAdmin(
server_url=config['keycloak_url'],
realm_name=config['keycloak_realm'],
username=KEYCLOAK_ADMIN_USER,
password=KEYCLOAK_ADMIN_PASSWORD,
user_realm_name=config['keycloak_realm'],
client_secret_key=None,
verify=True,
)
# authenticate to azure
get_azure_headers()
# cache azure groups
azure_all_groups = get_azure_groups()
# loop over all groups from groups variable
for group in config['groups']:
keycloak_group = group["keycloak"]
azure_group = group["azure"]
# get keycloak group id
keycloak_group_path = "/" + keycloak_group
keycloak_group_object = keycloak_admin.get_group_by_path(
path=keycloak_group_path
)
keycloak_group_id = keycloak_group_object["id"]
# get azure group id
for azure_group_entry in azure_all_groups:
if azure_group_entry["name"] == azure_group:
azure_group_id = azure_group_entry["id"]
break
# get keycloak group members
keycloak_group_members = keycloak_admin.get_group_members(
keycloak_group_id)
keycloak_group_members_emails = [
sub["email"]
for sub in keycloak_group_members
]
# get current azure group members
azure_group_member_ids_current = get_azure_group_members(
azure_group_id)
# make sure cache is up to date
azure_all_users = get_azure_users()
azure_all_user_emails = [sub["email"] for sub in azure_all_users]
# loop keycloak users
for keycloak_group_member in keycloak_group_members:
# get name and email address of user
keycloak_group_member_firstname = keycloak_group_member["firstName"]
keycloak_group_member_lastname = keycloak_group_member["lastName"]
keycloak_group_member_name = (
keycloak_group_member_firstname + " " + keycloak_group_member_lastname
)
keycloak_group_member_email = keycloak_group_member["email"]
# invite user
if keycloak_group_member_email not in azure_all_user_emails:
keycloak_group_member_id = invite_user(
keycloak_group_member_email)
# update initial user data
update_user(
keycloak_group_member_id,
keycloak_group_member_firstname,
keycloak_group_member_lastname,
)
# make sure cache is up to date
azure_all_users = get_azure_users()
azure_all_user_ids = [sub["id"] for sub in azure_all_users]
azure_all_user_emails = [sub["email"]
for sub in azure_all_users]
# update existing users
for azure_user_entry in azure_all_users:
if azure_user_entry["email"] == keycloak_group_member_email:
if (
azure_user_entry["firstname"] != keycloak_group_member_firstname
or azure_user_entry["lastname"]
!= keycloak_group_member_lastname
or azure_user_entry["name"] != keycloak_group_member_name
):
logging.info(
"user firstname/lastname differs in keycloak vs. azure:"
" azure firstname: "
+ azure_user_entry["firstname"]
+ ", keycloak firstname: "
+ keycloak_group_member_firstname
+ ", azure lastname: "
+ azure_user_entry["lastname"]
+ ", keycloak lastname: "
+ keycloak_group_member_lastname
+ ", azure fullname: "
+ azure_user_entry["name"]
+ ", keycloak fullname: "
+ keycloak_group_member_name
)
update_user(
azure_user_entry["id"],
keycloak_group_member_firstname,
keycloak_group_member_lastname,
)
# make sure cache is up to date
azure_all_users = get_azure_users()
azure_all_user_ids = [sub["id"]
for sub in azure_all_users]
azure_all_user_emails = [
sub["email"] for sub in azure_all_users
]
break
# add user to group
for azure_user_entry in azure_all_users:
if azure_user_entry["email"] == keycloak_group_member_email:
# check if not already a group member
if azure_user_entry["id"] not in azure_group_member_ids_current:
groupadd_user(azure_user_entry["id"], azure_group_id)
# make sure cache is up to date
azure_group_member_ids_current = get_azure_group_members(
azure_group_id
)
break
# remove user from group
for azure_user_entry in azure_all_users:
# check if currently a group member
if azure_user_entry["id"] in azure_group_member_ids_current:
# check if user does not belong into group
if (
azure_user_entry["email"]
and azure_user_entry["email"] not in keycloak_group_members_emails
):
groupremove_user(azure_user_entry["id"], azure_group_id)
# make sure cache is up to date
azure_group_member_ids_current = get_azure_group_members(
azure_group_id
)
break
logging.info("end run")
if __name__ == "__main__":
main()