Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-04-09 07:58:22

0001 #!/usr/bin/env python
0002 #
0003 # Licensed under the Apache License, Version 2.0 (the "License");
0004 # You may not use this file except in compliance with the License.
0005 # You may obtain a copy of the License at
0006 # http://www.apache.org/licenses/LICENSE-2.0OA
0007 #
0008 # Authors:
0009 # - Wen Guan, <wen.guan@@cern.ch>, 2021 - 2022
0010 
0011 import re
0012 
0013 from idds.common import authentication
0014 
0015 
0016 # /DC=ch/DC=cern/OU=Organic Units/OU=Users/CN=wguan/CN=667815/CN=Wen Guan/CN=1883443395
0017 def get_user_name_from_dn1(dn):
0018     try:
0019         up = re.compile('/(DC|O|OU|C|L)=[^\/]+')        # noqa W605
0020         username = up.sub('', dn)
0021         up2 = re.compile('/CN=[0-9]+')
0022         username = up2.sub('', username)
0023         up4 = re.compile(' [0-9]+')
0024         username = up4.sub('', username)
0025         up5 = re.compile('_[0-9]+')
0026         username = up5.sub('', username)
0027         username = username.replace('/CN=proxy', '')
0028         username = username.replace('/CN=limited proxy', '')
0029         username = username.replace('limited proxy', '')
0030         username = re.sub('/CN=Robot:[^/]+,', ',', username)
0031         username = re.sub('/CN=Robot:[^/]+', '', username)
0032         username = re.sub('/CN=Robot[^/]+,', ',', username)
0033         username = re.sub('/CN=Robot[^/]+', '', username)
0034         username = re.sub('/CN=nickname:[^/]+,', ',', username)
0035         username = re.sub('/CN=nickname:[^/]+', '', username)
0036         pat = re.compile('.*/CN=([^\/]+)/CN=([^\/]+)')         # noqa W605
0037         mat = pat.match(username)
0038         if mat:
0039             username = mat.group(2)
0040         else:
0041             username = username.replace('/CN=', '')
0042         if username.lower().find('/email') > 0:
0043             username = username[:username.lower().find('/email')]
0044         pat = re.compile('.*(limited.*proxy).*')
0045         mat = pat.match(username)
0046         if mat:
0047             username = mat.group(1)
0048         username = username.replace('(', '')
0049         username = username.replace(')', '')
0050         username = username.replace("'", '')
0051         return username
0052     except Exception:
0053         return dn
0054 
0055 
0056 # 'CN=203633261,CN=Wen Guan,CN=667815,CN=wguan,OU=Users,OU=Organic Units,DC=cern,DC=ch'
0057 def get_user_name_from_dn2(dn):
0058     try:
0059         up = re.compile(',(DC|O|OU|C|L)=[^\,]+')        # noqa W605
0060         username = up.sub('', dn)
0061         up2 = re.compile(',CN=[0-9]+')
0062         username = up2.sub('', username)
0063         up3 = re.compile('CN=[0-9]+,')
0064         username = up3.sub(',', username)
0065         up4 = re.compile(' [0-9]+')
0066         username = up4.sub('', username)
0067         up5 = re.compile('_[0-9]+')
0068         username = up5.sub('', username)
0069         username = username.replace(',CN=proxy', '')
0070         username = username.replace(',CN=limited proxy', '')
0071         username = username.replace('limited proxy', '')
0072         username = re.sub(',CN=Robot:[^/]+,', ',', username)
0073         username = re.sub(',CN=Robot:[^/]+', '', username)
0074         username = re.sub(',CN=Robot[^/]+,', ',', username)
0075         username = re.sub(',CN=Robot[^/]+', '', username)
0076         username = re.sub(',CN=nickname:[^/]+,', ',', username)
0077         username = re.sub(',CN=nickname:[^/]+', '', username)
0078         pat = re.compile('.*,CN=([^\,]+),CN=([^\,]+)')         # noqa W605
0079         mat = pat.match(username)
0080         if mat:
0081             username = mat.group(1)
0082         else:
0083             username = username.replace(',CN=', '')
0084         if username.lower().find(',email') > 0:
0085             username = username[:username.lower().find(',email')]
0086         pat = re.compile('.*(limited.*proxy).*')
0087         mat = pat.match(username)
0088         if mat:
0089             username = mat.group(1)
0090         username = username.replace('(', '')
0091         username = username.replace(')', '')
0092         username = username.replace("'", '')
0093         return username
0094     except Exception:
0095         return dn
0096 
0097 
0098 def get_user_name_from_dn(dn):
0099     dn = get_user_name_from_dn1(dn)
0100     dn = get_user_name_from_dn2(dn)
0101     return dn
0102 
0103 
0104 if __name__ == '__main__':
0105     dn = "/DC=ch/DC=cern/OU=Organic Units/OU=Users/CN=wguan/CN=667815/CN=Wen Guan/CN=1883443395"
0106     username = get_user_name_from_dn(dn)
0107     print(username)
0108     username = authentication.get_user_name_from_dn(dn)
0109     print("auth: " + username)
0110 
0111     dn = 'CN=203633261,CN=Wen Guan,CN=667815,CN=wguan,OU=Users,OU=Organic Units,DC=cern,DC=ch'
0112     username = get_user_name_from_dn(dn)
0113     print(username)
0114     username = authentication.get_user_name_from_dn(dn)
0115     print("auth: " + username)
0116 
0117     dn = "/DC=ch/DC=cern/OU=Organic+Units/OU=Users/CN=atlpilo1/CN=614260/CN=Robot%3A+ATLAS+Pilot1"
0118     username = get_user_name_from_dn(dn)
0119     print(username)
0120     username = authentication.get_user_name_from_dn(dn)
0121     print("auth: " + username)
0122 
0123     dn = "CN=1316551436,CN=Robot: ATLAS Panda Server1,CN=663551,CN=pandasv1,OU=Users,OU=Organic Units,DC=cern,DC=ch"
0124     username = get_user_name_from_dn(dn)
0125     print(username)
0126     username = authentication.get_user_name_from_dn(dn)
0127     print("auth: " + username)