55import sys
66import time
77import getopt
8- import urllib .error
9- import urllib .request
108import requests
119import comanage_utils as utils
1210
1311
1412SCRIPT = os .path .basename (__file__ )
15- ENDPOINT = "https://registry-test.cilogon.org/registry/"
1613TOPOLOGY_ENDPOINT = "https://topology.opensciencegrid.org/"
1714LDAP_SERVER = "ldaps://ldap-test.cilogon.org"
1815LDAP_USER = "uid=registry_user,ou=system,o=OSG,o=CO,dc=cilogon,dc=org"
19- OSG_CO_ID = 8
20- CACHE_FILENAME = "COmanage_Projects_cache.txt"
21- CACHE_LIFETIME_HOURS = 0.5
2216
2317
2418_usage = f"""\
2519 usage: [PASS=...] { SCRIPT } [OPTIONS]
2620
2721OPTIONS:
28- -u USER[:PASS] specify USER and optionally PASS on command line
29- -c OSG_CO_ID specify OSG CO ID (default = { OSG_CO_ID } )
22+ -u LDAP_USER[:PASS] specify LDAP user for reading data from LDAP server
3023 -s LDAP_SERVER specify LDAP server to read data from
31- -l LDAP_USER specify LDAP user for reading data from LDAP server
3224 -a ldap_authfile specify path to file to open and read LDAP authtok
33- -d passfd specify open fd to read PASS
34- -f passfile specify path to file to open and read PASS
35- -e ENDPOINT specify REST endpoint
36- (default = { ENDPOINT } )
3725 -o outfile specify output file (default: write to stdout)
3826 -g filter_group filter users by group name (eg, 'ap1-login')
3927 -l localmaps specify a comma-delimited list of local HTCondor mapfiles to merge into outfile
4028 -h display this help text
4129
42- PASS for USER is taken from the first of:
43- 1. -u USER:PASS
44- 2. -d passfd (read from fd)
45- 3. -f passfile (read from file)
46- 4. read from $PASS env var
4730"""
4831
4932def usage (msg = None ):
@@ -55,11 +38,7 @@ def usage(msg=None):
5538
5639
5740class Options :
58- endpoint = ENDPOINT
59- user = "co_7.project_script"
60- osg_co_id = OSG_CO_ID
6141 outfile = None
62- authstr = None
6342 ldap_server = LDAP_SERVER
6443 ldap_user = LDAP_USER
6544 ldap_authtok = None
@@ -69,37 +48,6 @@ class Options:
6948
7049options = Options ()
7150
72-
73- # api call results massagers
74-
75- def get_osg_co_groups__map ():
76- #print("get_osg_co_groups__map()")
77- resp_data = utils .get_osg_co_groups (options .osg_co_id , options .endpoint , options .authstr )
78- data = utils .get_datalist (resp_data , "CoGroups" )
79- return { g ["Id" ]: g ["Name" ] for g in data }
80-
81-
82- def co_group_is_project (gid ):
83- #print(f"co_group_is_ospool({gid})")
84- resp_data = utils .get_co_group_identifiers (gid , options .endpoint , options .authstr )
85- data = utils .get_datalist (resp_data , "Identifiers" )
86- return any ( i ["Type" ] == "ospoolproject" for i in data )
87-
88-
89- def get_co_group_osggid (gid ):
90- resp_data = utils .get_co_group_identifiers (gid , options .endpoint , options .authstr )
91- data = utils .get_datalist (resp_data , "Identifiers" )
92- return list (filter (lambda x : x ["Type" ] == "osggid" , data ))[0 ]["Identifier" ]
93-
94-
95- def get_co_group_members__pids (gid ):
96- #print(f"get_co_group_members__pids({gid})")
97- resp_data = utils .get_co_group_members (gid , options .endpoint , options .authstr )
98- data = utils .get_datalist (resp_data , "CoGroupMembers" )
99- # For INF-1060: Temporary Fix until "The Great Project Provisioning" is finished
100- return [ m ["Person" ]["Id" ] for m in data if m ["Member" ] == True ]
101-
102-
10351def get_co_person_osguser (pid ):
10452 #print(f"get_co_person_osguser({pid})")
10553 resp_data = utils .get_co_person_identifiers (pid , options .endpoint , options .authstr )
@@ -110,100 +58,35 @@ def get_co_person_osguser(pid):
11058
11159def parse_options (args ):
11260 try :
113- ops , args = getopt .getopt (args , 'u:c: s:l:a:d:f:g:e :o:h' )
61+ ops , args = getopt .getopt (args , 'u:s:l:a:g :o:h' )
11462 except getopt .GetoptError :
11563 usage ()
11664
11765 if args :
11866 usage ("Extra arguments: %s" % repr (args ))
11967
120- passfd = None
121- passfile = None
12268 ldap_authfile = None
12369
12470 for op , arg in ops :
12571 if op == '-h' : usage ()
126- if op == '-u' : options .user = arg
127- if op == '-c' : options .osg_co_id = int (arg )
12872 if op == '-s' : options .ldap_server = arg
129- if op == '-l ' : options .ldap_user = arg
73+ if op == '-u ' : options .ldap_user = arg
13074 if op == '-a' : ldap_authfile = arg
131- if op == '-d' : passfd = int (arg )
132- if op == '-f' : passfile = arg
133- if op == '-e' : options .endpoint = arg
13475 if op == '-o' : options .outfile = arg
13576 if op == '-g' : options .filtergrp = arg
136- if op == '-l' : options .localmaps = arg .split ("," )
77+ if op == '-l' : options .localmaps = arg .split ("," )
13778
13879 try :
139- user , passwd = utils .getpw (options .user , passfd , passfile )
140- options .authstr = utils .mkauthstr (user , passwd )
14180 options .ldap_authtok = utils .get_ldap_authtok (ldap_authfile )
14281 except PermissionError :
143- usage ("PASS required" )
82+ usage ("-a required" )
14483
14584def _deduplicate_list (items ):
14685 """ Deduplicate a list while maintaining order by converting it to a dictionary and then back to a list.
14786 Used to ensure a consistent ordering for output group lists, since sets are unordered.
14887 """
14988 return list (dict .fromkeys (items ))
15089
151- def get_ldap_group_members_dict ():
152- group_data_dict = dict ()
153- for group_gid in utils .get_ldap_groups (options .ldap_server , options .ldap_user , options .ldap_authtok ):
154- group_members = utils .get_ldap_group_members (group_gid , options .ldap_server , options .ldap_user , options .ldap_authtok )
155- group_data_dict [group_gid ] = group_members
156-
157- return group_data_dict
158-
159-
160- def create_user_to_projects_map (project_to_user_map , active_users , osggids_to_names ):
161- users_to_projects_map = dict ()
162- for osggid in project_to_user_map :
163- for user in project_to_user_map [osggid ]:
164- if user in active_users :
165- if user not in users_to_projects_map :
166- users_to_projects_map [user ] = [osggids_to_names [osggid ]]
167- else :
168- users_to_projects_map [user ].append (osggids_to_names [osggid ])
169-
170- return users_to_projects_map
171-
172-
173- def get_groups_data_from_api ():
174- groups = get_osg_co_groups__map ()
175- project_osggids_to_name = dict ()
176- for id ,name in groups .items ():
177- if co_group_is_project (id ):
178- project_osggids_to_name [get_co_group_osggid (id )] = name
179- return project_osggids_to_name
180-
181-
182- def get_co_api_data ():
183- try :
184- r = open (CACHE_FILENAME , "r" )
185- lines = r .readlines ()
186- if float (lines [0 ]) >= (time .time () - (60 * 60 * CACHE_LIFETIME_HOURS )):
187- entries = lines [1 :len (lines )]
188- project_osggids_to_name = dict ()
189- for entry in entries :
190- osggid_name_pair = entry .split (":" )
191- if len (osggid_name_pair ) == 2 :
192- project_osggids_to_name [int (osggid_name_pair [0 ])] = osggid_name_pair [1 ].strip ()
193- r .close ()
194- else :
195- r .close ()
196- raise OSError
197- except OSError :
198- with open (CACHE_FILENAME , "w" ) as w :
199- project_osggids_to_name = get_groups_data_from_api ()
200- print (time .time (), file = w )
201- for osggid , name in project_osggids_to_name .items ():
202- print (f"{ osggid } :{ name } " , file = w )
203-
204- return project_osggids_to_name
205-
206-
20790def get_osguser_groups (filter_group_name = None ):
20891 ldap_users = utils .get_ldap_active_users_and_groups (options .ldap_server , options .ldap_user , options .ldap_authtok , filter_group_name )
20992 topology_projects = requests .get (f"{ TOPOLOGY_ENDPOINT } /miscproject/json" ).json ()
@@ -264,7 +147,4 @@ def main(args):
264147
265148
266149if __name__ == "__main__" :
267- try :
268- main (sys .argv [1 :])
269- except Exception as e :
270- sys .exit (e )
150+ main (sys .argv [1 :])
0 commit comments