1 |
spiga |
1.1 |
import os,sys
|
2 |
|
|
import commands
|
3 |
|
|
import traceback
|
4 |
|
|
import time
|
5 |
|
|
|
6 |
|
|
from ProdCommon.BossLite.Common.System import executeCommand
|
7 |
|
|
|
8 |
|
|
class Proxy:
|
9 |
|
|
"""
|
10 |
|
|
basic class to handle user Token
|
11 |
|
|
"""
|
12 |
|
|
def __init__( self, **args ):
|
13 |
|
|
self.timeout = args.get( "timeout", None )
|
14 |
|
|
self.myproxyServer = args.get( "myProxySvr", '')
|
15 |
|
|
self.serverDN = args.get( "serverDN", '')
|
16 |
|
|
self.shareDir = args.get( "shareDir", '')
|
17 |
|
|
self.userName = args.get( "userName", '')
|
18 |
|
|
self.debug = args.get("debug",False)
|
19 |
|
|
self.args = args
|
20 |
|
|
|
21 |
|
|
def ExecuteCommand( self, command ):
|
22 |
|
|
"""
|
23 |
|
|
_ExecuteCommand_
|
24 |
|
|
|
25 |
|
|
Util it execute the command provided in a popen object with a timeout
|
26 |
|
|
"""
|
27 |
|
|
|
28 |
|
|
return executeCommand( command, self.timeout )
|
29 |
|
|
|
30 |
|
|
|
31 |
|
|
def getUserProxy(self):
|
32 |
|
|
"""
|
33 |
|
|
"""
|
34 |
|
|
try:
|
35 |
|
|
proxy = os.path.expandvars('$X509_USER_PROXY')
|
36 |
|
|
except Exception,ex:
|
37 |
|
|
msg = ('Error %s in getUserProxy search\n' %str(ex))
|
38 |
|
|
if self.debug : msg += traceback.format_exc()
|
39 |
|
|
raise Exception(msg)
|
40 |
|
|
|
41 |
|
|
return proxy.strip()
|
42 |
|
|
|
43 |
|
|
def getSubject(self, proxy = None):
|
44 |
|
|
"""
|
45 |
|
|
"""
|
46 |
|
|
subject = None
|
47 |
|
|
if proxy == None: proxy=self.getUserProxy()
|
48 |
|
|
|
49 |
|
|
cmd = 'openssl x509 -in '+proxy+' -subject -noout'
|
50 |
|
|
|
51 |
|
|
out, ret = self.ExecuteCommand(cmd)
|
52 |
|
|
if ret != 0 :
|
53 |
|
|
msg = "Error while checking proxy subject for %s"%proxy
|
54 |
|
|
raise Exception(msg)
|
55 |
|
|
lines = out.split('\n')[0]
|
56 |
|
|
|
57 |
|
|
return subject.strip()
|
58 |
|
|
|
59 |
|
|
def getUserName(self, proxy = None ):
|
60 |
|
|
"""
|
61 |
|
|
"""
|
62 |
|
|
uName = None
|
63 |
|
|
if proxy == None: proxy=self.getUserProxy()
|
64 |
|
|
|
65 |
|
|
cmd = "voms-proxy-info -file "+proxy+" -subject"
|
66 |
|
|
|
67 |
|
|
out, ret = self.ExecuteCommand(cmd)
|
68 |
|
|
if ret != 0 :
|
69 |
|
|
msg = "Error while extracting User Name from proxy %s"%proxy
|
70 |
|
|
raise Exception(msg)
|
71 |
|
|
|
72 |
|
|
emelments = out.split('/')
|
73 |
|
|
uName = elements[-1:][0].split('CN=')[1]
|
74 |
|
|
|
75 |
|
|
return uName.strip()
|
76 |
|
|
|
77 |
|
|
def checkCredential(self, proxy=None, Time=10):
|
78 |
|
|
"""
|
79 |
|
|
Function to check the Globus proxy.
|
80 |
|
|
"""
|
81 |
|
|
valid = True
|
82 |
|
|
if proxy == None: proxy=self.getUserProxy()
|
83 |
|
|
minTimeLeft=int(Time)*3600 # in seconds
|
84 |
|
|
|
85 |
|
|
cmd = 'voms-proxy-info -file '+proxy+' -timeleft '
|
86 |
|
|
|
87 |
|
|
out, ret
|
88 |
|
|
|
89 |
|
|
timeLeftLocal =
|
90 |
|
|
|
91 |
|
|
## if no valid proxy
|
92 |
|
|
if timeLeftLocal == None or int(timeLeftLocal)<minTimeLeft :
|
93 |
|
|
valid = False
|
94 |
|
|
|
95 |
|
|
return valid
|
96 |
|
|
|
97 |
|
|
def renewCredential( self, proxy=None ):
|
98 |
|
|
"""
|
99 |
|
|
"""
|
100 |
|
|
if proxy == None: proxy=self.getUserProxy()
|
101 |
|
|
# check
|
102 |
|
|
if not self.checkCredential():
|
103 |
|
|
# ask for proxy delegation
|
104 |
|
|
# using myproxy
|
105 |
|
|
pass
|
106 |
|
|
return
|
107 |
|
|
|
108 |
|
|
def checkAttribute( self, proxy=None ):
|
109 |
|
|
"""
|
110 |
|
|
"""
|
111 |
|
|
if proxy == None: proxy=self.getUserProxy()
|
112 |
|
|
|
113 |
|
|
## check first attribute
|
114 |
|
|
# cmd = 'voms-proxy-info -fqan | head -1'
|
115 |
|
|
|
116 |
|
|
# reg="/%s/"%self.VO
|
117 |
|
|
# if self.group:
|
118 |
|
|
# reg+=self.group
|
119 |
|
|
# if self.role:
|
120 |
|
|
# reg+="/Role=%s"%self.role
|
121 |
|
|
|
122 |
|
|
return
|
123 |
|
|
|
124 |
|
|
def ManualRenewCredential( self, VO='cms', group=None, role=None ):
|
125 |
|
|
"""
|
126 |
|
|
"""
|
127 |
|
|
# ## you always have at least /cms/Role=NULL/Capability=NULL
|
128 |
|
|
# if not re.compile(r"^"+reg).search(att):
|
129 |
|
|
# if not mustRenew:
|
130 |
|
|
# common.logger.message( "Valid proxy found, but with wrong VO group/role.\n")
|
131 |
|
|
# mustRenew = 1
|
132 |
|
|
######
|
133 |
|
|
|
134 |
|
|
if not self.checkCredential:
|
135 |
|
|
cmd = 'voms-proxy-init -voms '+VO
|
136 |
|
|
if group:
|
137 |
|
|
cmd += ':/'+VO+'/'+group
|
138 |
|
|
if role:
|
139 |
|
|
cmd += '/role='+role
|
140 |
|
|
cmd += ' -valid 192:00'
|
141 |
|
|
try:
|
142 |
|
|
out = os.system(cmd)
|
143 |
|
|
if (out>0): raise Exception("Unable to create a valid proxy!\n")
|
144 |
|
|
except:
|
145 |
|
|
msg = "Unable to create a valid proxy!\n"
|
146 |
|
|
raise Exception(msg)
|
147 |
|
|
|
148 |
|
|
def checkMyProxy( self, proxyServer ):
|
149 |
|
|
"""
|
150 |
|
|
"""
|
151 |
|
|
## check the myproxy server
|
152 |
|
|
valid = True
|
153 |
|
|
cmd = 'myproxy-info -d -s %s'%proxyServer
|
154 |
|
|
|
155 |
|
|
if not out:
|
156 |
|
|
print 'No credential delegated to myproxy server %s will do now'%proxyServer
|
157 |
|
|
valid = False
|
158 |
|
|
else:
|
159 |
|
|
## minimum time: 5 days
|
160 |
|
|
minTime = 4 * 24 * 3600
|
161 |
|
|
## regex to extract the right information
|
162 |
|
|
myproxyRE = re.compile("timeleft: (?P<hours>[\\d]*):(?P<minutes>[\\d]*):(?P<seconds>[\\d]*)")
|
163 |
|
|
for row in out.split("\n"):
|
164 |
|
|
g = myproxyRE.search(row)
|
165 |
|
|
if g:
|
166 |
|
|
hours = g.group("hours")
|
167 |
|
|
minutes = g.group("minutes")
|
168 |
|
|
seconds = g.group("seconds")
|
169 |
|
|
timeleft = int(hours)*3600 + int(minutes)*60 + int(seconds)
|
170 |
|
|
if timeleft < minTime:
|
171 |
|
|
print 'Your proxy will expire in:\n\t%s hours %s minutes %s seconds\n'%(hours,minutes,seconds)
|
172 |
|
|
valid = False
|
173 |
|
|
return valid
|
174 |
|
|
|
175 |
|
|
def ManualRenewMyProxy( self ):
|
176 |
|
|
"""
|
177 |
|
|
"""
|
178 |
|
|
if not self.checkMyProxy:
|
179 |
|
|
cmd = 'myproxy-init -d -n -s '+self.proxyServer
|
180 |
|
|
out = os.system(cmd)
|
181 |
|
|
if (out>0):
|
182 |
|
|
raise CrabException("Unable to delegate the proxy to myproxyserver "+self.proxyServer+" !\n")
|
183 |
|
|
pass
|
184 |
|
|
return
|
185 |
|
|
|
186 |
|
|
def logonProxy( self ):
|
187 |
|
|
"""
|
188 |
|
|
To be implemented
|
189 |
|
|
"""
|
190 |
|
|
#
|
191 |
|
|
return
|