-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathgroup_fixup.py
executable file
·243 lines (177 loc) · 6.88 KB
/
group_fixup.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
#!/usr/bin/env python3
import os
import re
import sys
import getopt
import collections
import urllib.error
import urllib.request
import comanage_utils as utils
SCRIPT = os.path.basename(__file__)
ENDPOINT = "https://registry.cilogon.org/registry/"
USER = "co_7.group_fixup"
OSG_CO_ID = 7
LDAP_PROV_ID = 6
_usage = f"""\
usage: [PASS=...] {SCRIPT} [OPTIONS]
OPTIONS:
-u USER[:PASS] specify USER and optionally PASS on command line
-c OSG_CO_ID specify OSG CO ID (default = {OSG_CO_ID})
-d passfd specify open fd to read PASS
-f passfile specify path to file to open and read PASS
-e ENDPOINT specify REST endpoint
(default = {ENDPOINT})
-p LDAP_PROV_ID LDAP Provisioning Target ID (default = {LDAP_PROV_ID})
-a show all UnixCluster autogroups, not just misnamed ones
-i COGroupId show fixup info for a specific CO Group
-x COGroupId run UnixCluster Group fixups for given CO Group Id
--fix-all run UnixCluster Group fixups for all misnamed groups
-h display this help text
Run without options to display misnamed UnixCluster autogroups.
Run with -a to include UnixCluster autogroups with fixed names, too.
Run with -i to display only a given CO Group.
Run with -x to fixup a given CO Group.
PASS for USER is taken from the first of:
1. -u USER:PASS
2. -d passfd (read from fd)
3. -f passfile (read from file)
4. read from $PASS env var
"""
def usage(msg=None):
if msg:
print(msg + "\n", file=sys.stderr)
print(_usage, file=sys.stderr)
sys.exit()
class Options:
endpoint = ENDPOINT
osg_co_id = OSG_CO_ID
prov_id = LDAP_PROV_ID
user = USER
authstr = None
fix_gid = None
info_gid = None
showall = False
fix_all = False
options = Options()
# api call results massagers
def get_unixcluster_autogroups():
groups = utils.get_osg_co_groups(options.osg_co_id, options.endpoint, options.authstr)
return [ g for g in groups["CoGroups"]
if "automatically by UnixCluster" in g["Description"] ]
def get_misnamed_unixcluster_groups():
groups = utils.get_osg_co_groups(options.osg_co_id, options.endpoint, options.authstr)
return [ g for g in groups["CoGroups"]
if "UnixCluster Group" in g["Name"] ]
def _osgid_sortkey(i):
return int(i["Identifier"])
def get_identifiers_to_delete(identifiers):
by_type = collections.defaultdict(list)
ids_to_delete = []
for i in identifiers:
by_type[i["Type"]].append(i)
if len(by_type["osggid"]) == 2:
min_identifier = min(by_type["osggid"], key=_osgid_sortkey)
ids_to_delete.append(min_identifier["Id"])
for i in by_type["osggroup"]:
if i["Identifier"].endswith("unixclustergroup"):
ids_to_delete.append(i["Id"])
return ids_to_delete
def get_fixed_unixcluster_group_name(name):
m = re.search(r'^(.*) UnixCluster Group', name)
return m.group(1) if m else name
# display functions
def show_misnamed_unixcluster_group(group):
print('CO {CoId} Group {Id}: "{Name}"'.format(**group))
oldname = group["Name"]
newname = get_fixed_unixcluster_group_name(oldname)
if oldname != newname:
print(' ** Rename group to: "%s"' % newname)
show_group_identifiers(group["Id"])
print("")
def show_all_unixcluster_groups():
groups = get_unixcluster_autogroups()
for group in groups:
show_misnamed_unixcluster_group(group)
def show_one_unixcluster_group(gid):
group = utils.get_co_group(gid, options.endpoint, options.authstr)
show_misnamed_unixcluster_group(group)
def show_misnamed_unixcluster_groups():
groups = get_misnamed_unixcluster_groups()
for group in groups:
show_misnamed_unixcluster_group(group)
def show_group_identifiers(gid):
resp_data = utils.get_co_group_identifiers(gid, options.endpoint, options.authstr)
identifiers = utils.get_datalist(resp_data, "Identifiers")
for i in identifiers:
print(' - Identifier {Id}: ({Type}) "{Identifier}"'.format(**i))
ids_to_delete = get_identifiers_to_delete(identifiers)
if ids_to_delete:
print(' ** Identifier Ids to delete: %s' % ', '.join(ids_to_delete))
# fixup functions
def fixup_unixcluster_group(gid):
group = utils.get_co_group(gid, options.endpoint, options.authstr)
oldname = group["Name"]
newname = get_fixed_unixcluster_group_name(oldname)
resp_data = utils.get_co_group_identifiers(gid, options.endpoint, options.authstr)
identifiers = utils.get_datalist(resp_data, "Identifiers")
ids_to_delete = get_identifiers_to_delete(identifiers)
show_misnamed_unixcluster_group(group)
if oldname != newname:
utils.rename_co_group(gid, group, newname, options.endpoint, options.authstr)
for id_ in ids_to_delete:
utils.delete_identifier(id_, options.endpoint, options.authstr)
utils.provision_group(gid, options.prov_id, options.endpoint, options.authstr)
utils.provision_group_members(gid, options.prov_id, options.endpoint, options.authstr)
# http errors raise exceptions, so at this point we apparently succeeded
print(":thumbsup:")
return 0
def fixup_all_unixcluster_groups():
groups = get_misnamed_unixcluster_groups()
for group in groups:
fixup_unixcluster_group(group["Id"])
# CLI
def parse_options(args):
try:
ops, args = getopt.getopt(args, 'u:c:d:f:e:x:i:p:ah', ["fix-all"])
except getopt.GetoptError:
usage()
if args:
usage("Extra arguments: %s" % repr(args))
passfd = None
passfile = None
for op, arg in ops:
if op == '-h': usage()
if op == '-u': options.user = arg
if op == '-c': options.osg_co_id = int(arg)
if op == '-d': passfd = int(arg)
if op == '-f': passfile = arg
if op == '-e': options.endpoint = arg
if op == '-x': options.fix_gid = int(arg)
if op == '-i': options.info_gid = int(arg)
if op == '-p': options.prov_id = int(arg)
if op == '-a': options.showall = True
if op == '--fix-all': options.fix_all = True
try:
user, passwd = utils.getpw(options.user, passfd, passfile)
options.authstr = utils.mkauthstr(user, passwd)
except PermissionError:
usage("PASS required")
def main(args):
parse_options(args)
if options.fix_gid:
return fixup_unixcluster_group(options.fix_gid)
elif options.fix_all:
return fixup_all_unixcluster_groups()
elif options.showall:
show_all_unixcluster_groups()
elif options.info_gid:
show_one_unixcluster_group(options.info_gid)
else:
show_misnamed_unixcluster_groups()
return 0
if __name__ == "__main__":
try:
sys.exit(main(sys.argv[1:]))
except (RuntimeError, urllib.error.HTTPError) as e:
print(e, file=sys.stderr)
sys.exit(1)