Skip to content

Commit dfd61e0

Browse files
Mass import script: fixup for U-Chicago dev ingest
1 parent 8130146 commit dfd61e0

File tree

2 files changed

+78
-18
lines changed

2 files changed

+78
-18
lines changed

comanage_utils.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,10 +190,37 @@ def get_unix_cluster_groups_ids(ucid, endpoint, authstr):
190190
return set(group["CoGroupId"] for group in unix_cluster_groups["UnixClusterGroups"])
191191

192192

193+
def update_co_person_identifier(id_, type, identifier, person_id, endpoint, authstr, provisioning_target):
194+
id_data = {
195+
"RequestType":"Identifiers",
196+
"Version":"1.0",
197+
"Identifiers":
198+
[
199+
{
200+
"Version":"1.0",
201+
"Type":type,
202+
"Identifier":identifier,
203+
"Login":False,
204+
"Person":{"Type":"CO","Id":person_id},
205+
"CoProvisioningTargetId":provisioning_target,
206+
"Status":"Active"
207+
}
208+
]
209+
}
210+
return call_api3(PUT, "/api/v2/identifiers" % id_, id_data, endpoint, authstr, )
211+
#return call_api3(PUT, "identifiers/%s.json" % id_, id_data, endpoint, authstr)
212+
213+
193214
def delete_identifier(id_, endpoint, authstr):
194215
return call_api2(DELETE, "identifiers/%s.json" % id_, endpoint, authstr)
195216

196217

218+
def get_co_group_members_pids(gid, endpoint, authstr):
219+
resp_data = get_co_group_members(gid, endpoint, authstr)
220+
data = get_datalist(resp_data, "CoGroupMembers")
221+
return [m["Person"]["Id"] for m in data]
222+
223+
197224
def get_datalist(data, listname):
198225
return data[listname] if data else []
199226

@@ -216,6 +243,14 @@ def identifier_from_list(id_list, id_type):
216243
except ValueError:
217244
return None
218245

246+
def full_identifier_from_list(id_list, id_type):
247+
id_type_list = [id["Type"] for id in id_list]
248+
try:
249+
id_index = id_type_list.index(id_type)
250+
return id_list[id_index]
251+
except ValueError:
252+
return None
253+
219254

220255
def identifier_matches(id_list, id_type, regex_string):
221256
pattern = re.compile(regex_string)

mass_person_create_modify.py

Lines changed: 43 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010
SCRIPT = os.path.basename(__file__)
1111
ENDPOINT = "https://registry.cilogon.org/registry/"
1212
OSG_CO_ID = 7
13-
CMS_GROUP_ID = 4622
14-
CMS_COU_ID = 1785
1513
LDAP_TARGET_ID = 9
1614

1715
_usage = f"""\
@@ -32,16 +30,18 @@ class Options:
3230
authstr = None
3331
input_file = None
3432
mapping_file = None
35-
ssh_key_authenticator = 5
36-
unix_cluster_id = 10
33+
ssh_key_authenticator = 5 # 1
34+
unix_cluster_id = 10 # 1
3735
provisioning_target = LDAP_TARGET_ID
36+
import_group_id = None
37+
import_cou_id = None
3838

3939

4040
options = Options()
4141

4242
def parse_options(args):
4343
try:
44-
ops, args = getopt.getopt(args, 'u:c:d:f:e:i:m:h')
44+
ops, args = getopt.getopt(args, 'u:c:d:f:e:i:m:g:o:h')
4545
except getopt.GetoptError:
4646
usage()
4747

@@ -60,6 +60,8 @@ def parse_options(args):
6060
if op == '-e': options.endpoint = arg
6161
if op == '-i': options.input_file = arg
6262
if op == '-m': options.mapping_file = arg
63+
if op == '-g': options.import_group_id = arg
64+
if op == '-o': options.import_cou_id = arg
6365

6466
try:
6567
user, passwd = utils.getpw(options.user, passfd, passfile)
@@ -82,10 +84,10 @@ def read_data_dump():
8284
data_json[entry]["public_keys"][key_index].update({"authenticator" : key_sections[2]})
8385
with open(options.mapping_file, 'r', encoding='utf-8') as mapping_file:
8486
mapping_json = json.load(mapping_file)
85-
return data_json
87+
return data_json, mapping_json
8688

8789

88-
def build_co_person_record(entry):
90+
def build_co_person_record(entry, mapping_json : dict):
8991
record = {}
9092
record.update({"CoPerson" : schema_utils.co_person_schema(options.osg_co_id, status="A")})
9193

@@ -95,29 +97,37 @@ def build_co_person_record(entry):
9597

9698
identifiers = []
9799

98-
# CMS Username
99-
identifiers.append(schema_utils.co_person_identifier(entry["username"], "cmsuser", status="A"))
100-
# CMS UID
101-
identifiers.append(schema_utils.co_person_identifier(entry["uid"], "cmsuid", status="A"))
100+
# UC Connect Username
101+
identifiers.append(schema_utils.co_person_identifier(entry["username"], "ucconnectuser", status="A"))
102+
# UC Connect UID
103+
identifiers.append(schema_utils.co_person_identifier(entry["uid"], "ucconnectuid", status="A"))
102104
#globus id
103-
identifiers.append(schema_utils.co_person_identifier(entry["globus_id"], "cmsglobusid", status="A"))
105+
identifiers.append(schema_utils.co_person_identifier(entry["globus_id"], "ucconnectglobusid", status="A"))
104106
#cilogon id
105-
if not entry["cilogon_id"] is None:
106-
identifiers.append(schema_utils.co_person_identifier(entry["cilogon_id"], "oidcsub", status="A"))
107+
if not (entry["cilogon_oidc_sub"] is None or entry["cilogon_oidc_sub"] == ""):
108+
identifiers.append(schema_utils.co_person_identifier(entry["cilogon_oidc_sub"], "oidcsub", status="A"))
107109
else:
108110
print(f"Warning: user {entry['username']} lacks a cilogon id.")
111+
# With our current LDAP Provisioner configuration, the CO Person still needs an oidcsub identifier, even if it's junk
112+
identifiers.append(schema_utils.co_person_identifier(f"dummy-text-for-provisioning-{entry['username']}", "oidcsub", status="A", login=False))
109113

110114
record.update({"Identifier" : identifiers })
111115

112116
group_memberships = []
113-
group_memberships.append(schema_utils.co_person_group_member(CMS_GROUP_ID))
117+
group_memberships.append(schema_utils.co_person_group_member(options.import_group_id))
118+
119+
for user_membership in entry["groups"]:
120+
if not mapping_json.get(user_membership) is None:
121+
group_memberships.append(schema_utils.co_person_group_member(mapping_json.get(user_membership)))
122+
else:
123+
print(f"Warning: could not find group id for group {user_membership}, user {entry['username']}.")
114124

115125
# Group Memberships
116126
record.update({"CoGroupMember" : group_memberships })
117127

118128
roles = []
119129

120-
roles.append(schema_utils.co_person_role(CMS_COU_ID, "CMS User", "member", 1))
130+
roles.append(schema_utils.co_person_role(options.import_cou_id, "UC Connect User", "member", 1))
121131
record.update({"CoPersonRole" : roles })
122132

123133
emails = []
@@ -144,6 +154,15 @@ def build_co_person_record(entry):
144154

145155
return record
146156

157+
def fix_username(co_person_record, new_username):
158+
record = co_person_record
159+
160+
for identifier in co_person_record["Identifier"]:
161+
if identifier["type"] == "osguser":
162+
identifier["identifier"] = new_username
163+
164+
return record
165+
147166

148167
def create_unix_cluster_group(co_person_record):
149168
identifiers_list = co_person_record["Identifier"]
@@ -193,9 +212,12 @@ def main(args):
193212

194213
co_person_records = dict()
195214

196-
data_dump_json = read_data_dump()
215+
data_dump_json, mapping_json = read_data_dump()
216+
217+
#data_dump_json = [data_dump_json[0], data_dump_json[1], data_dump_json[2], data_dump_json[3], data_dump_json[8], data_dump_json[9]]
218+
197219
for entry in data_dump_json:
198-
co_person_records.update({entry["username"] : build_co_person_record(entry)})
220+
co_person_records.update({entry["username"] : build_co_person_record(entry, mapping_json)})
199221

200222
usernames = list(co_person_records.keys())
201223

@@ -219,6 +241,9 @@ def main(args):
219241

220242
co_person_data = utils.core_api_co_person_read(user, options.osg_co_id, options.endpoint, options.authstr)
221243

244+
co_person_data = fix_username(co_person_data, user)
245+
utils.core_api_co_person_update(user, options.osg_co_id, co_person_data, options.endpoint, options.authstr)
246+
222247
co_person_data, gid = add_unix_cluster_account(co_person_data)
223248

224249
utils.core_api_co_person_update(user, options.osg_co_id, co_person_data, options.endpoint, options.authstr)

0 commit comments

Comments
 (0)