CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
authentication.py
Go to the documentation of this file.
1 ################################################################################
2 # RelMon: a tool for automatic Release Comparison
3 # https://twiki.cern.ch/twiki/bin/view/CMSPublic/RelMon
4 #
5 # This is a code manipulation of a component of the DQMCompare tool of
6 # Marco Rovere and Luca Malgeri.
7 #
8 # $Author: dpiparo $
9 # $Date: 2012/06/12 12:25:27 $
10 # $Revision: 1.1 $
11 #
12 #
13 # Danilo Piparo CERN - danilo.piparo@cern.ch
14 #
15 ################################################################################
16 
17 from os import getenv
18 from os.path import exists
19 from httplib import HTTPSConnection
20 from urllib2 import AbstractHTTPHandler
21 #-------------------------------------------------------------------------------
22 
23 class X509CertAuth(HTTPSConnection):
24  '''Class to authenticate via Grid Certificate'''
25  def __init__(self, host, *args, **kwargs):
26  key_file = None
27  cert_file = None
28 
29  x509_path = getenv("X509_USER_PROXY", None)
30  if x509_path and exists(x509_path):
31  key_file = cert_file = x509_path
32 
33  if not key_file:
34  x509_path = getenv("X509_USER_KEY", None)
35  if x509_path and exists(x509_path):
36  key_file = x509_path
37 
38  if not cert_file:
39  x509_path = getenv("X509_USER_CERT", None)
40  if x509_path and exists(x509_path):
41  cert_file = x509_path
42 
43  if not key_file:
44  x509_path = getenv("HOME") + "/.globus/userkey.pem"
45  if exists(x509_path):
46  key_file = x509_path
47 
48  if not cert_file:
49  x509_path = getenv("HOME") + "/.globus/usercert.pem"
50  if exists(x509_path):
51  cert_file = x509_path
52 
53  if not key_file or not exists(key_file):
54  print >>stderr, "No certificate private key file found"
55  exit(1)
56 
57  if not cert_file or not exists(cert_file):
58  print >>stderr, "No certificate public key file found"
59  exit(1)
60 
61  #print "Using SSL private key", key_file
62  #print "Using SSL public key", cert_file
63 
64  HTTPSConnection.__init__(self,
65  host,
66  key_file = key_file,
67  cert_file = cert_file,
68  **kwargs)
69 
70 #-----------------------------------------------------------------------------
71 
72 class X509CertOpen(AbstractHTTPHandler):
73  def default_open(self, req):
74  return self.do_open(X509CertAuth, req)