This repository has been archived by the owner on Sep 23, 2020. It is now read-only.
/
nimbus_import_users.py
243 lines (185 loc) · 6.86 KB
/
nimbus_import_users.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
#!/usr/bin/env python
"""
Import users from a flat file
"""
import os
import sys
from fileinput import FileInput
import csv
from itertools import izip
import tempfile
import traceback
import pynimbusauthz
import pycb.tools
from pynimbusauthz.cmd_opts import cbOpts
from nimbusweb.setup.setuperrors import CLIError
import nimbus_list_users
import nimbus_edit_user
import nimbus_remove_user
import nimbus_new_user
_fields = ["dn", "canonical_id", "access_id", "access_secret",
"display_name", "group"]
_fields_csv = ','.join(_fields)
def setup_options(argv):
u = """[options] [file]
Import Nimbus users
If file is not specified, standard input will be used"""
(parser, all_opts) = pynimbusauthz.get_default_options(u)
opt = cbOpts('dryrun', 'd', 'Don\'t actually do anything, just print', False,
flag=True)
all_opts.append(opt)
opt = cbOpts('delim', 'D', 'Character between columns of input data', ',')
all_opts.append(opt)
#opt = cbOpts('fields', 'f', 'Specify field order in output, from the following: '+
# _fields_csv, None)
#all_opts.append(opt)
#opt = cbOpts('ignore', 'i', 'Ignore specified user(s)')
#all_opts.append(opt)
opt = cbOpts('update', 'u', 'Perform updates of existing users', False, flag=True)
all_opts.append(opt)
opt = cbOpts('remove', 'r', 'Remove users that exist locally but not in input file',
False, flag=True)
all_opts.append(opt)
opt = cbOpts('changes_only', 'c', 'Only report changed and unmatched users',
False, flag=True)
all_opts.append(opt)
(o, args) = pynimbusauthz.parse_args(parser, all_opts, argv)
return (o, args, parser)
def read_users(input, delimiter=',', fields=_fields):
"""Read in users from flat text input.
If input is None or an empty list, STDIN is used.
"""
f = FileInput(input)
reader = csv.reader(f, delimiter=delimiter)
users = {}
for row in reader:
if len(row) != len(fields):
raise CLIError('EUSER',
'Line %s of input has incorrect number of fields. Expecting "%s"' %
(f.lineno(), delimiter.join(fields)))
u = dict([(k, v) for k, v in izip(fields, row)])
name = u['display_name']
grp = _fix_group(u['group'])
u['group'] = grp
if name in users:
raise CLIError('EUSER', 'Duplicate entry present for user "%s"' % name)
users[name] = u
return users
class printer_obj(object):
def __init__(self):
pass
def _fix_group(group):
#workaround bug in 2.5 nimbus-edit-user. expects zero padded group number
if group.isdigit() and group[0] != '0':
return '0'+group
return group
def update_user(current, desired, opts):
args = []
if current['dn'] != desired['dn']:
args.extend(['-s', desired['dn']])
if current['access_id'] != desired['access_id']:
args.extend(['-a', desired['access_id']])
if current['access_secret'] != desired['access_secret']:
args.extend(['-p', desired['access_secret']])
if current['group'] != desired['group']:
args.extend(['-g', _fix_group(desired['group'])])
if current['canonical_id'] != desired['canonical_id']:
# canonical ID cannot be updated (is used in cumulus paths)
return "CANNOT_UPDATE"
if args:
name = current['display_name']
args.extend(['-q', name])
if not opts.update:
pynimbusauthz.print_msg(opts, 2,
"Not updating mismatched user %s: --update is not specified" %
name)
return "MISMATCHED"
pynimbusauthz.print_msg(opts, 2, "Calling nimbus-edit-user with args: " +
str(args))
if opts.dryrun:
return "UPDATED"
ok = False
try:
ok = nimbus_edit_user.main(args) == 0
except:
pynimbusauthz.print_msg(opts, 2, "Error: " + traceback.format_exc())
return ok and "UPDATED" or "UPDATE_FAILED"
return "UNCHANGED"
def new_user(user, opts):
args = ['-s', user['dn'], '-i', user['canonical_id'], '-a', user['access_id'],
'-p', user['access_secret'], '-g', _fix_group(user['group']),
'-P', '-q', user['display_name']]
pynimbusauthz.print_msg(opts, 2, "Calling nimbus-new-user with args: " +
str(args))
if opts.dryrun:
return "ADDED"
ok = False
try:
ok = nimbus_new_user.main(args) == 0
except:
pynimbusauthz.print_msg(opts, 2, "Error: " + traceback.format_exc())
return ok and "ADDED" or "ADD_FAILED"
def remove_user(user_name, opts):
args = [user_name]
if not opts.remove:
pynimbusauthz.print_msg(opts, 2,
"Not removing extra user %s: --remove is not specified" %
user_name)
return "EXTRA"
pynimbusauthz.print_msg(opts, 2, "Calling nimbus-remove-user with args: " +
str(args))
if opts.dryrun:
return "REMOVED"
ok = False
try:
ok = nimbus_remove_user.main(args) == 0
except:
pynimbusauthz.print_msg(opts, 2, "Error: " + traceback.format_exc())
return ok and "REMOVED" or "REMOVE_FAILED"
def walk_users(current, desired, opts):
def print_record(o):
if not opts.changes_only or o.state != 'UNCHANGED':
pycb.tools.print_report(o, 'display_name,state', opts)
all_ok = True
for name,user in desired.iteritems():
o = printer_obj()
o.display_name = name
current_user = current.pop(name, None)
if current_user:
o.state = update_user(current_user, user, opts)
if o.state == 'UPDATE_FAILED':
all_ok = False
else:
o.state = new_user(user, opts)
if o.state == 'ADD_FAILED':
all_ok = False
print_record(o)
# anything left in current users is potentially to be removed
for name,user in current.iteritems():
o = printer_obj()
o.display_name = name
o.state = remove_user(name, opts)
if o.state == 'REMOVE_FAILED':
all_ok = False
print_record(o)
return all_ok
def main(argv=sys.argv[1:]):
try:
(opts, args, p) = setup_options(argv)
file_users = read_users(args, delimiter=opts.delim)
file,path = tempfile.mkstemp()
pynimbusauthz.print_msg(opts, 2, "Using temp file: " + path)
try:
nimbus_list_users.main(['-b', '-D', opts.delim, '-r', _fields_csv,
'-O', path, '%'])
current_users = read_users(path, delimiter=opts.delim)
finally:
os.remove(path)
if not walk_users(current_users, file_users, opts):
return 1
except CLIError, clie:
print clie
return clie.get_rc()
return 0
if __name__ == '__main__':
sys.exit(main())