Commit acdd9146 authored by ayllon's avatar ayllon
Browse files

Client logging

parent 892a4ea3
#!/usr/bin/env python
from fts3.rest.client import ClientV1
from fts3.rest.client import Client
from fts3.rest.client import setDefaultLogging
import getopt
import logging
import os
import sys
setDefaultLogging()
logging.getLogger().setLevel(logging.DEBUG)
if 'X509_USER_PROXY' not in os.environ:
raise Exception("X509_USER_PROXY must be set")
......@@ -12,11 +20,7 @@ if 'X509_USER_PROXY' not in os.environ:
proxy = os.environ['X509_USER_PROXY']
endpoint = sys.argv[1]
print "Using endpoint", endpoint
print "Using proxy", proxy
client = ClientV1(endpoint, proxy, proxy)
client = Client(endpoint, proxy, proxy)
delegationId = client.delegate()
print "Got delegation ID", delegationId
#!/usr/bin/env python
from fts3.rest.client import ClientV1
from fts3.rest.client import Client, setDefaultLogging
import getopt
import logging
import sys
import traceback
setDefaultLogging()
# Parameters
def usage():
print "Usage: %s [-h|--help] -s|--endpoint <endpoint> job-id" % sys.argv[0]
print "Usage: %s [-v|--verbose] [-h|--help] -s|--endpoint <endpoint> job-id" % sys.argv[0]
sys.exit(0)
endpoint = None
opt, args = getopt.getopt(sys.argv[1:], 'hs:', ['--help', '--endpoint'])
opt, args = getopt.getopt(sys.argv[1:], 'hs:v', ['--help', '--endpoint', '--verbose'])
for o, v in opt:
if o in ('-h', '--help'):
usage()
elif o in ('-s', '--endpoint'):
endpoint = v
elif o in ('-v', '--verbose'):
logging.getLogger().setLevel(logging.DEBUG)
if endpoint is None:
print >>sys.stderr, "Need an endpoint"
logging.critical("Need an endpoint")
sys.exit(1)
if len(args) == 0:
print >>sys.stderr, "Need a job id"
logging.critical("Need a job id")
sys.exit(1)
jobId = args[0]
try:
client = ClientV1(endpoint)
info = client.getEndpointInfo()
client = Client(endpoint, logger = logging.getLogger())
job = client.getJobStatus(jobId)
print "# Using endpoint: %s" % info['url']
print "# REST API version: %s" % info['api']
print "# Schema version: %(major)d.%(minor)d.%(patch)d" % info['schema']
print "# Delegation version: %(major)d.%(minor)d.%(patch)d" % info['delegation']
print "Request ID: %s" % job['job_id']
print "Status: %s" % job['job_state']
print "Client DN: %s" % job['user_dn']
......@@ -48,5 +55,7 @@ try:
print "\tReason: %s" % f['reason']
except Exception, e:
print >>sys.stderr, e
print >>sys.stderr, str(e)
if logging.getLogger().getEffectiveLevel() == logging.DEBUG:
traceback.print_exc()
sys.exit(1)
import json
import logging
import os
import pytz
import sys
import requests
import pycurl
from datetime import datetime, timedelta
from M2Crypto import X509, RSA, EVP, ASN1
from request import RequestFactory
from exceptions import *
def setDefaultLogging():
logging.basicConfig(format='# %(message)s', level = logging.INFO)
# Return a list of certificates from the file
def getX509List(file):
def getX509List(file, logger):
SEEKING_CERT = 0
LOADING_CERT = 1
......@@ -19,13 +29,12 @@ def getX509List(file):
for line in fd:
if line == '-----BEGIN CERTIFICATE-----\n':
status = LOADING_CERT
print "Found certificate in chain"
elif line == '-----END CERTIFICATE-----\n':
buffer += line
x509 = X509.load_cert_string(buffer, X509.FORMAT_PEM)
x509List.append(x509)
print "Loaded", x509.get_subject().as_text()
logger.debug("Loaded " + x509.get_subject().as_text())
buffer = ""
status = SEEKING_CERT
......@@ -39,15 +48,32 @@ def getX509List(file):
class ClientV1(object):
class Client(object):
def __init__(self, endpoint, ucert = None, ukey = None):
def __init__(self, endpoint, ucert = None, ukey = None, logger = None):
if logger:
self.logger = logger
else:
self.logger = logging.getLogger()
self.endpoint = endpoint
if self.endpoint.endswith('/'):
self.endpoint = self.endpoint[:-1]
if not ucert:
if 'X509_USER_PROXY' in os.environ:
ucert = os.environ['X509_USER_PROXY']
elif 'X509_USER_CERT' in os.environ:
ucert = os.environ['X509_USER_CERT']
if not ukey:
if 'X509_USER_PROXY' in os.environ:
ukey = os.environ['X509_USER_PROXY']
elif 'X509_USER_KEY' in os.environ:
ukey = os.environ['X509_USER_KEY']
if ucert:
self.x509List = getX509List(ucert)
if ucert:
self.x509List = getX509List(ucert, self.logger)
self.x509 = self.x509List[0]
if self.x509.get_not_after().get_datetime() < datetime.now(pytz.UTC):
raise Exception("Proxy expired!")
......@@ -56,17 +82,30 @@ class ClientV1(object):
self.rsaKey = RSA.load_key(ukey)
self.evpKey = EVP.PKey()
self.evpKey.assign_rsa(self.rsaKey)
def getEndpointInfo(self):
self.requester = RequestFactory(ucert, ukey)
# Validate the endpoint
try:
r = requests.get(self.endpoint)
j = r.json
j['url'] = self.endpoint
return j
self.endpointInfo = json.loads(self.requester.get(self.endpoint))
self.endpointInfo['url'] = self.endpoint
if self.endpointInfo['api'] != 'Mk.1':
raise ValueError("Wrong API version")
except Exception, e:
raise IOError(e)
raise BadEndpoint, "%s (%s)" % (self.endpoint, str(e)), sys.exc_info()[2]
self.logger.info("Using endpoint: %s" % self.endpointInfo['url'])
self.logger.info("REST API version: %s" % self.endpointInfo['api'])
self.logger.info("Schema version: %(major)d.%(minor)d.%(patch)d" % self.endpointInfo['schema'])
self.logger.info("Delegation version: %(major)d.%(minor)d.%(patch)d" % self.endpointInfo['delegation'])
def getEndpointInfo(self):
return self.endpointInfo
......@@ -74,34 +113,40 @@ class ClientV1(object):
url = "%s/jobs/%s" % (self.endpoint, jobId)
try:
r = requests.get(url)
return r.json
except Exception, e:
raise IOError(e)
def getJobTransfers(self, jobId):
url = "%s/job/%s/files/" % (self.endpoint, jobId)
try:
r = requests.get(url)
return r.json['objects']
except Exception, e:
raise IOError(e)
return json.loads(self.requester.get(url))
except NotFound:
raise NotFound(jobId)
def delegate(self):
urls = {'getRequest': "%s/delegation/request" % (self.endpoint),
'putSigned': "%s/delegation/credential" % (self.endpoint),
}
try:
r = requests.get(urls['getRequest'])
delegationId = r.headers['X-Delegation-ID']
requestPEM = r.content
whoami = "%s/whoami" % self.endpoint
delegationRoot = "%s/delegation" % self.endpoint
try:
r = json.loads(self.requester.get(whoami))
delegationId = r['delegation_id']
self.logger.debug("Delegation ID: " + delegationId)
r = json.loads(self.requester.get(delegationRoot + '/' + delegationId))
if r is not None:
expirationTime = datetime.strptime(r['termination_time'], '%Y-%m-%dT%H:%M:%S%z')
if expiratiomTime > datetime.now() + timedelta(hours = 1):
self.logger.info("Not bothering doing the delegation")
return delegationId
else:
self.logger.info("Expiration time passed: " + str(expirationTime))
else:
self.logger.info("No previous delegation found")
self.logger.info("Delegating")
requestPEM = self.requester.get(delegationRoot + '/' + delegationId + '/request')
x509Request = X509.load_request_string(requestPEM)
print >>sys.stderr, "Signing request for %s" % x509Request.get_subject().as_text()
self.logger.debug("Signing request for %s" % x509Request.get_subject().as_text())
notBefore = ASN1.ASN1_UTCTIME()
notBefore.set_datetime(datetime.now(pytz.UTC))
......@@ -120,13 +165,15 @@ class ClientV1(object):
certPEM = cert.as_pem()
r = requests.put(urls['putSigned'] + '/' + delegationId, certPEM)
r = requests.put(delegationRoot + '/' + delegationId + '/request', certPEM, cert = (self.ucert, self.ukey))
if r.status_code != 201:
raise Exception(r.text)
return delegationId
except FTS3ClientException, e:
raise e
except Exception, e:
raise IOError(e)
raise ClientError, str(e), sys.exc_info()[2]
class FTS3ClientException(Exception):
pass
class BadEndpoint(FTS3ClientException):
def __init__(self, endpoint):
self.endpoint = endpoint
def __str__(self):
return "Bad endpoint: %s" % self.endpoint
class Unauthorized(FTS3ClientException):
def __str__(self):
return "Unauthorized"
class ClientError(FTS3ClientException):
def __init__(self, reason):
self.reason = reason
def __str__(self):
return "Client error: %s" % self.reason
class ServerError(FTS3ClientException):
def __init__(self, reason):
self.reason = reason
def __str__(self):
return "Server error: %s" % self.reason
class NotFound(FTS3ClientException):
def __init__(self, resource):
self.resource = resource
def __str__(self):
return "Not found: %s" % self.resource
import httplib
import sys
import urllib2
from exceptions import *
class HTTPSWithCertHandler(urllib2.HTTPSHandler):
def __init__(self, cert, key):
urllib2.HTTPSHandler.__init__(self)
self.cert = cert
self.key = key
def https_open(self, req):
return self.do_open(self.getConnection, req)
def getConnection(self, host, timeout = 1000):
return httplib.HTTPSConnection(host, cert_file = self.cert, key_file = self.key)
class RequestFactory(object):
def __init__(self, ucert, ukey, cafile = None, capath = None, verify = False):
self.ucert = ucert
self.ukey = ukey
self.verify = verify
if cafile:
self.cafile = cafile
else:
self.cafile = ucert
if capath:
self.capath = capath
else:
self.capath = '/etc/grid-security/certificates'
def get(self, url, headers = {}):
opener = urllib2.build_opener(HTTPSWithCertHandler(self.ucert, self.ukey))
try:
request = urllib2.Request(url, headers = headers)
response = opener.open(request)
return response.read()
except urllib2.HTTPError, e:
if e.code == 400:
raise ClientError(e.reason)
elif e.code >= 401 and e.code <= 403:
raise Unauthorized()
elif e.code == 404:
raise NotFound(url)
elif e.code > 404 and e.code < 500:
raise ClientError(e.reason)
elif e.code >= 500:
raise ServerError(e.reason)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment