1
2
3
4
5
6
7
8
9
10
11 import sys
12 import os
13 import re
14 from Commands import submitCmd
15
16 DEBUG = False
17
18
19
20
22 """Returns certificate subject.
23 If certPath is provided, than it looks for the certificate in that path.
24 If certFile is provided, than takes it instead of default name 'usercert.pem'.
25 """
26 subject = ''
27
28 if not certFile:
29 certFile = "usercert.pem"
30 if certPath:
31 certFile = os.path.join(certPath, certFile)
32
33
34 cmd = 'openssl x509 -subject -in %s -nameopt oneline -noout' %certFile
35
36
37 executed, output = submitCmd(cmd)
38
39 if DEBUG:
40 print executed, output
41
42
43 if executed:
44 if len(output) > 0:
45 subject = output[0]
46 r = re.match('(subject\s*= )(.*)', subject)
47 if r:
48 subject = r.group(2)
49 return subject
50 return subject
51
52
54 """Returns path to the grid proxy certificate of current user.
55 A valid proxy must be created independently."""
56
57 proxyPath = ""
58 if os.environ.has_key( "X509_USER_PROXY" ):
59 proxyPath = os.environ[ "X509_USER_PROXY" ]
60 else:
61 proxyPath = "".join( [ "/tmp/x509up_u", str( os.getuid() ) ] )
62
63 if not os.path.exists( proxyPath ):
64 proxyPath = ""
65
66 return proxyPath
67
68
69 usage = """
70 certificate.py <option>/ <certPath> <sertFile>
71 option = -i -- interactive mode
72 option = -h -- show command usage
73 certPath -- path to the certificate file
74 certFile -- name of the certificate file
75 """
76
77
78
79 if __name__ == '__main__':
80 if len(sys.argv) > 1:
81 if sys.argv[1] == '-i':
82 while 1:
83 certPath = raw_input('Enter path to the certificate (If different from current directory)--->')
84 certFile = raw_input('Enter name of the certificate file (If different from "usercert.pem") --->')
85 print getCertificateSubject(certPath, certFile)
86 quit = raw_input('Enter "Q" to exit--->')
87 if quit.lower() == 'q':
88 break
89 elif sys.argv[1] == '-h':
90 print usage
91 else:
92 if len(sys.argv) > 2:
93 print getCertificateSubject(sys.argv[1], sys.argv[2])
94 else:
95 print getCertificateSubject(sys.argv[1])
96 else:
97 print getCertificateSubject()
98