20
20
21
21
import okta
22
22
import six
23
+ from okta .framework .OktaError import OktaError
23
24
24
25
import user_sync .config
25
26
import user_sync .connector .helper
26
- import user_sync .error
27
27
import user_sync .helper
28
28
import user_sync .identity_type
29
29
from user_sync .error import AssertionException
@@ -44,15 +44,16 @@ def connector_initialize(options):
44
44
return state
45
45
46
46
47
- def connector_load_users_and_groups (state , groups , extended_attributes ):
47
+ def connector_load_users_and_groups (state , groups , extended_attributes , all_users ):
48
48
"""
49
49
:type state: OktaDirectoryConnector
50
50
:type groups: list(str)
51
51
:type extended_attributes: list(str)
52
+ :type all_users: bool
52
53
:rtype (bool, iterable(dict))
53
54
"""
54
55
55
- return state .load_users_and_groups (groups , extended_attributes )
56
+ return state .load_users_and_groups (groups , extended_attributes , all_users )
56
57
57
58
58
59
class OktaDirectoryConnector (object ):
@@ -93,17 +94,20 @@ def __init__(self, caller_options):
93
94
try :
94
95
self .users_client = okta .UsersClient (host , api_token )
95
96
self .groups_client = okta .UserGroupsClient (host , api_token )
96
- except Exception as e :
97
- raise user_sync . error . AssertionException (repr ( e ) )
97
+ except OktaError as e :
98
+ raise AssertionException ("Error connecting to Okta: %s" % e )
98
99
99
100
logger .info ('Connected' )
100
101
101
- def load_users_and_groups (self , groups , extended_attributes ):
102
+ def load_users_and_groups (self , groups , extended_attributes , all_users ):
102
103
"""
103
104
:type groups: list(str)
104
105
:type extended_attributes: list(str)
106
+ :type all_users: bool
105
107
:rtype (bool, iterable(dict))
106
108
"""
109
+ if all_users :
110
+ raise AssertionException ("Okta connector has no notion of all users, please specify a --users group" )
107
111
108
112
options = self .options
109
113
all_users_filter = options ['all_users_filter' ]
@@ -140,9 +144,9 @@ def find_group(self, group):
140
144
group_filter_format = options ['group_filter_format' ]
141
145
try :
142
146
results = self .groups_client .get_groups (query = group_filter_format .format (group = group ))
143
- except Exception as e :
147
+ except OktaError as e :
144
148
self .logger .warning ("Unable to query group" )
145
- raise user_sync . error . AssertionException (repr ( e ) )
149
+ raise AssertionException ("Okta error querying for group: %s" % e )
146
150
147
151
if results is None :
148
152
self .logger .warning ("No group found for: %s" , group )
@@ -156,6 +160,7 @@ def find_group(self, group):
156
160
def iter_group_members (self , group , filter_string , extended_attributes ):
157
161
"""
158
162
:type group: str
163
+ :type filter_string: str
159
164
:type extended_attributes: list
160
165
:rtype iterator(str, str)
161
166
"""
@@ -169,9 +174,9 @@ def iter_group_members(self, group, filter_string, extended_attributes):
169
174
try :
170
175
attr_dict = OKTAValueFormatter .get_extended_attribute_dict (user_attribute_names )
171
176
members = self .groups_client .get_group_all_users (res_group .id , attr_dict )
172
- except Exception as e :
177
+ except OktaError as e :
173
178
self .logger .warning ("Unable to get_group_users" )
174
- raise user_sync . error . AssertionException (repr ( e ) )
179
+ raise AssertionException ("Okta error querying for group users: %s" % e )
175
180
# Filtering users based all_users_filter query in config
176
181
for member in self .filter_users (members , filter_string ):
177
182
profile = member .profile
@@ -201,7 +206,7 @@ def convert_user(self, record, extended_attributes):
201
206
else :
202
207
try :
203
208
user ['identity_type' ] = user_sync .identity_type .parse_identity_type (user_identity_type )
204
- except user_sync . error . AssertionException as e :
209
+ except AssertionException as e :
205
210
self .logger .warning ('Skipping user %s: %s' , profile .login , e )
206
211
return None
207
212
@@ -250,17 +255,18 @@ def iter_search_result(self, filter_string, attributes):
250
255
users = self .users_client .get_all_users (query = filter_string , extended_attribute = attr_dict )
251
256
else :
252
257
users = self .users_client .get_all_users (query = filter_string )
253
- except Exception as e :
258
+ except OktaError as e :
254
259
self .logger .warning ("Unable to query users" )
255
- raise user_sync . error . AssertionException (repr ( e ) )
260
+ raise AssertionException ("Okta error querying for users: %s" % e )
256
261
return users
257
262
258
263
def filter_users (self , users , filter_string ):
259
264
try :
260
- result = filter (lambda user : eval (filter_string ), users )
265
+ return list (filter (lambda user : eval (filter_string ), users ))
266
+ except SyntaxError as e :
267
+ raise AssertionException ("Invalid syntax in predicate (%s): cannot evaluate" % filter_string )
261
268
except Exception as e :
262
- raise AssertionException ("Error filtering users: %s" % e )
263
- return result
269
+ raise AssertionException ("Error filtering with predicate (%s): %s" % (filter_string , e ))
264
270
265
271
266
272
class OKTAValueFormatter (object ):
0 commit comments