33
33
from builtins import input
34
34
from getpass import getpass
35
35
import datetime
36
+ import errno
36
37
import getopt
37
38
import os
38
39
import sys
@@ -91,8 +92,7 @@ def usage(name='createuser.py'):
91
92
""" % {'name' : name , 'cert_warn' : cert_warn })
92
93
93
94
94
- if '__main__' == __name__ :
95
- (args , app_dir , db_path ) = init_user_adm ()
95
+ def main (_main , args , cwd , db_path = keyword_auto ):
96
96
conf_path = None
97
97
auth_type = 'custom'
98
98
expire = None
@@ -111,6 +111,7 @@ def usage(name='createuser.py'):
111
111
user_dict = {}
112
112
override_fields = {}
113
113
opt_args = 'a:c:d:e:fhi:o:p:rR:s:u:v'
114
+
114
115
try :
115
116
(opts , args ) = getopt .getopt (args , opt_args )
116
117
except getopt .GetoptError as err :
@@ -138,13 +139,8 @@ def usage(name='createuser.py'):
138
139
parsed = True
139
140
break
140
141
except ValueError :
141
- pass
142
- if parsed :
143
- override_fields ['expire' ] = expire
144
- override_fields ['status' ] = 'temporal'
145
- else :
146
- print ('Failed to parse expire value: %s' % val )
147
- sys .exit (1 )
142
+ print ('Failed to parse expire value: %s' % val )
143
+ sys .exit (1 )
148
144
elif opt == '-f' :
149
145
force = True
150
146
elif opt == '-h' :
@@ -154,17 +150,13 @@ def usage(name='createuser.py'):
154
150
user_id = val
155
151
elif opt == '-o' :
156
152
short_id = val
157
- override_fields ['short_id' ] = short_id
158
153
elif opt == '-p' :
159
154
peer_pattern = val
160
- override_fields ['peer_pattern' ] = peer_pattern
161
- override_fields ['status' ] = 'temporal'
162
155
elif opt == '-r' :
163
156
default_renew = True
164
157
ask_renew = False
165
158
elif opt == '-R' :
166
159
role = val
167
- override_fields ['role' ] = role
168
160
elif opt == '-s' :
169
161
# Translate slack days into seconds as
170
162
slack_secs = int (float (val )* 24 * 3600 )
@@ -178,7 +170,12 @@ def usage(name='createuser.py'):
178
170
print ('Error: %s not supported!' % opt )
179
171
sys .exit (1 )
180
172
181
- if conf_path and not os .path .isfile (conf_path ):
173
+ if not conf_path :
174
+ # explicitly set the default value of keyword_auto if no option was
175
+ # provided since it is unconditionally passed inward as a keyword arg
176
+ # and thus the fallback would accidentally be ignored
177
+ conf_path = keyword_auto
178
+ elif not os .path .isfile (conf_path ):
182
179
print ('Failed to read configuration file: %s' % conf_path )
183
180
sys .exit (1 )
184
181
@@ -190,30 +187,76 @@ def usage(name='createuser.py'):
190
187
if verbose :
191
188
print ('using configuration from MIG_CONF (or default)' )
192
189
193
- configuration = get_configuration_object (config_file = conf_path )
190
+ ret = _main (None , args ,
191
+ conf_path = conf_path ,
192
+ db_path = db_path ,
193
+ expire = expire ,
194
+ force = force ,
195
+ verbose = verbose ,
196
+ ask_renew = ask_renew ,
197
+ default_renew = default_renew ,
198
+ ask_change_pw = ask_change_pw ,
199
+ user_file = user_file ,
200
+ user_id = user_id ,
201
+ short_id = short_id ,
202
+ role = role ,
203
+ peer_pattern = peer_pattern ,
204
+ slack_secs = slack_secs ,
205
+ hash_password = hash_password
206
+ )
207
+
208
+ if ret == errno .ENOTSUP :
209
+ usage ()
210
+ sys .exit (1 )
211
+
212
+ sys .exit (ret )
213
+
214
+
215
+ def _main (configuration , args ,
216
+ conf_path = keyword_auto ,
217
+ db_path = keyword_auto ,
218
+ auth_type = 'custom' ,
219
+ expire = None ,
220
+ force = False ,
221
+ verbose = False ,
222
+ ask_renew = True ,
223
+ default_renew = False ,
224
+ ask_change_pw = True ,
225
+ user_file = None ,
226
+ user_id = None ,
227
+ short_id = None ,
228
+ role = None ,
229
+ peer_pattern = None ,
230
+ slack_secs = 0 ,
231
+ hash_password = True ,
232
+ _generate_salt = None
233
+ ):
234
+ if configuration is None :
235
+ if conf_path == keyword_auto :
236
+ config_file = None
237
+ else :
238
+ config_file = conf_path
239
+ configuration = get_configuration_object (config_file = config_file )
240
+
194
241
logger = configuration .logger
242
+
195
243
# NOTE: we need explicit db_path lookup here for load_user_dict call
196
244
if db_path == keyword_auto :
197
245
db_path = default_db_path (configuration )
198
246
199
247
if user_file and args :
200
248
print ('Error: Only one kind of user specification allowed at a time' )
201
- usage ()
202
- sys .exit (1 )
249
+ return errno .ENOTSUP
203
250
204
251
if auth_type not in valid_auth_types :
205
252
print ('Error: invalid account auth type %r requested (allowed: %s)' %
206
253
(auth_type , ', ' .join (valid_auth_types )))
207
- usage ()
208
- sys .exit (1 )
254
+ return errno .ENOTSUP
209
255
210
256
# NOTE: renew requires original password
211
257
if auth_type == 'cert' :
212
258
hash_password = False
213
259
214
- if expire is None :
215
- expire = default_account_expire (configuration , auth_type )
216
-
217
260
raw_user = {}
218
261
if args :
219
262
try :
@@ -229,8 +272,7 @@ def usage(name='createuser.py'):
229
272
except IndexError :
230
273
print ('Error: too few arguments given (expected 7 got %d)'
231
274
% len (args ))
232
- usage ()
233
- sys .exit (1 )
275
+ return errno .ENOTSUP
234
276
# Force user ID fields to canonical form for consistency
235
277
# Title name, lowercase email, uppercase country and state, etc.
236
278
user_dict = canonical_user (configuration , raw_user , raw_user .keys ())
@@ -239,14 +281,12 @@ def usage(name='createuser.py'):
239
281
user_dict = load (user_file )
240
282
except Exception as err :
241
283
print ('Error in user name extraction: %s' % err )
242
- usage ()
243
- sys .exit (1 )
284
+ return errno .ENOTSUP
244
285
elif default_renew and user_id :
245
286
saved = load_user_dict (logger , user_id , db_path , verbose )
246
287
if not saved :
247
288
print ('Error: no such user in user db: %s' % user_id )
248
- usage ()
249
- sys .exit (1 )
289
+ return errno .ENOTSUP
250
290
user_dict .update (saved )
251
291
del user_dict ['expire' ]
252
292
elif not configuration .site_enable_gdp :
@@ -268,13 +308,13 @@ def usage(name='createuser.py'):
268
308
print ("Error: Missing one or more of the arguments: "
269
309
+ "[FULL_NAME] [ORGANIZATION] [STATE] [COUNTRY] "
270
310
+ "[EMAIL] [COMMENT] [PASSWORD]" )
271
- sys . exit ( 1 )
311
+ return 1
272
312
273
313
# Encode password if set but not already encoded
274
314
275
315
if user_dict ['password' ]:
276
316
if hash_password :
277
- user_dict ['password_hash' ] = make_hash (user_dict ['password' ])
317
+ user_dict ['password_hash' ] = make_hash (user_dict ['password' ], _generate_salt = _generate_salt )
278
318
user_dict ['password' ] = ''
279
319
else :
280
320
salt = configuration .site_password_salt
@@ -291,9 +331,19 @@ def usage(name='createuser.py'):
291
331
292
332
fill_user (user_dict )
293
333
294
- # Make sure account expire is set with local certificate or OpenID login
295
-
334
+ # assemble the fields to be explicitly overriden
335
+ override_fields = {}
336
+ if peer_pattern :
337
+ override_fields ['peer_pattern' ] = peer_pattern
338
+ override_fields ['status' ] = 'temporal'
339
+ if role :
340
+ override_fields ['role' ] = role
341
+ if short_id :
342
+ override_fields ['short_id' ] = short_id
296
343
if 'expire' not in user_dict :
344
+ # Make sure account expire is set with local certificate or OpenID login
345
+ if not expire :
346
+ expire = default_account_expire (configuration , auth_type )
297
347
override_fields ['expire' ] = expire
298
348
299
349
# NOTE: let non-ID command line values override loaded values
@@ -305,8 +355,10 @@ def usage(name='createuser.py'):
305
355
if verbose :
306
356
print ('using user dict: %s' % user_dict )
307
357
try :
308
- create_user (user_dict , conf_path , db_path , force , verbose , ask_renew ,
309
- default_renew , verify_peer = peer_pattern ,
358
+ conf_path = configuration .config_file
359
+ create_user (user_dict , conf_path , db_path , configuration , force , verbose , ask_renew ,
360
+ default_renew ,
361
+ verify_peer = peer_pattern ,
310
362
peer_expire_slack = slack_secs , ask_change_pw = ask_change_pw )
311
363
if configuration .site_enable_gdp :
312
364
(success_here , msg ) = ensure_gdp_user (configuration ,
@@ -319,10 +371,17 @@ def usage(name='createuser.py'):
319
371
print ("Error creating user: %s" % exc )
320
372
import traceback
321
373
logger .warning ("Error creating user: %s" % traceback .format_exc ())
322
- sys . exit ( 1 )
374
+ return 1
323
375
print ('Created or updated %s in user database and in file system' %
324
376
user_dict ['distinguished_name' ])
325
377
if user_file :
326
378
if verbose :
327
379
print ('Cleaning up tmp file: %s' % user_file )
328
380
os .remove (user_file )
381
+
382
+ return 0
383
+
384
+
385
+ if __name__ == '__main__' :
386
+ (args , cwd , db_path ) = init_user_adm ()
387
+ main (_main , args , cwd , db_path = db_path )
0 commit comments