From patchwork Wed Jun 27 02:22:10 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael-Doyle Hudson X-Patchwork-Id: 9637 Return-Path: X-Original-To: patchwork@peony.canonical.com Delivered-To: patchwork@peony.canonical.com Received: from fiordland.canonical.com (fiordland.canonical.com [91.189.94.145]) by peony.canonical.com (Postfix) with ESMTP id AB1B423E55 for ; Wed, 27 Jun 2012 02:22:12 +0000 (UTC) Received: from mail-lb0-f180.google.com (mail-lb0-f180.google.com [209.85.217.180]) by fiordland.canonical.com (Postfix) with ESMTP id 78226A188A8 for ; Wed, 27 Jun 2012 02:22:12 +0000 (UTC) Received: by lbbgj3 with SMTP id gj3so1045756lbb.11 for ; Tue, 26 Jun 2012 19:22:12 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=x-forwarded-to:x-forwarded-for:delivered-to:received-spf :content-type:mime-version:x-launchpad-project:x-launchpad-branch :x-launchpad-message-rationale:x-launchpad-branch-revision-number :x-launchpad-notification-type:to:from:subject:message-id:date :reply-to:sender:errors-to:precedence:x-generated-by :x-launchpad-hash:x-gm-message-state; bh=q+n0Ai09z6r2zY38b52ZoeNMpQByavH3ZFcqcYcUEqE=; b=Sagnt3CVzrDctbbibC3REonPgHljogM6JMtxg81uMrw0Lk57ZodTj/SiViVUMjgwOk nl0w2JqMCm+IeQn94K5YFICoBuYfwtugEqabq1bTnTddPg/Eb9iN5oZ+ZimPd7Wxvbhs shegErw7Ox15bYjSPRhEwUS4LgK/rOCaZbirM4t/6DFCE+v/aNJsLfBACVJLxZrBkMCo hNOL3SkJhhyCbp8zsUz9NyNgyxSmbHPp1/7RyrSmsoTzZ4e0W1R7zpz6T6I20HJ3AgM8 hU8d2osnuDHCsEVEukwkZ73i8rEWx80wEiVz6pwZy/Qg9fIMxHgp5IHRLtcAlrwgmE3j eJyA== Received: by 10.112.42.34 with SMTP id k2mr8763910lbl.0.1340763731964; Tue, 26 Jun 2012 19:22:11 -0700 (PDT) X-Forwarded-To: linaro-patchwork@canonical.com X-Forwarded-For: patch@linaro.org linaro-patchwork@canonical.com Delivered-To: patches@linaro.org Received: by 10.152.148.101 with SMTP id tr5csp35047lab; Tue, 26 Jun 2012 19:22:11 -0700 (PDT) Received: by 10.216.145.24 with SMTP id o24mr8618919wej.110.1340763731241; Tue, 26 Jun 2012 19:22:11 -0700 (PDT) Received: from indium.canonical.com (indium.canonical.com. [91.189.90.7]) by mx.google.com with ESMTPS id x74si21704009weq.98.2012.06.26.19.22.11 (version=TLSv1/SSLv3 cipher=OTHER); Tue, 26 Jun 2012 19:22:11 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of bounces@canonical.com designates 91.189.90.7 as permitted sender) client-ip=91.189.90.7; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of bounces@canonical.com designates 91.189.90.7 as permitted sender) smtp.mail=bounces@canonical.com Received: from ackee.canonical.com ([91.189.89.26]) by indium.canonical.com with esmtp (Exim 4.71 #1 (Debian)) id 1SjhtS-0005ml-Tw for ; Wed, 27 Jun 2012 02:22:10 +0000 Received: from ackee.canonical.com (localhost [127.0.0.1]) by ackee.canonical.com (Postfix) with ESMTP id D62A6E00A9 for ; Wed, 27 Jun 2012 02:22:10 +0000 (UTC) MIME-Version: 1.0 X-Launchpad-Project: lava-tool X-Launchpad-Branch: ~linaro-validation/lava-tool/trunk X-Launchpad-Message-Rationale: Subscriber X-Launchpad-Branch-Revision-Number: 176 X-Launchpad-Notification-Type: branch-revision To: Linaro Patch Tracker From: noreply@launchpad.net Subject: [Branch ~linaro-validation/lava-tool/trunk] Rev 176: support XML-RPC operations behind a proxy Message-Id: <20120627022210.18482.30553.launchpad@ackee.canonical.com> Date: Wed, 27 Jun 2012 02:22:10 -0000 Reply-To: noreply@launchpad.net Sender: bounces@canonical.com Errors-To: bounces@canonical.com Precedence: bulk X-Generated-By: Launchpad (canonical.com); Revision="15482"; Instance="launchpad-lazr.conf" X-Launchpad-Hash: f576c5716cf3e4b462ec3e028ce41fad380a098d X-Gm-Message-State: ALoCoQkb5iq4s2q1mwNnbmSFpNbjUE3q4gGKMOaQnRdC9iV1Kv0vvNxdRqBIaGHKNXg5qm74/f9p Merge authors: Michael Hudson-Doyle (mwhudson) Related merge proposals: https://code.launchpad.net/~mwhudson/lava-tool/support-proxy-bug-898174/+merge/111990 proposed by: Michael Hudson-Doyle (mwhudson) review: Approve - Andy Doan (doanac) ------------------------------------------------------------ revno: 176 [merge] committer: Michael Hudson-Doyle branch nick: trunk timestamp: Wed 2012-06-27 14:21:32 +1200 message: support XML-RPC operations behind a proxy modified: lava_tool/authtoken.py --- lp:lava-tool https://code.launchpad.net/~linaro-validation/lava-tool/trunk You are subscribed to branch lp:lava-tool. To unsubscribe from this branch go to https://code.launchpad.net/~linaro-validation/lava-tool/trunk/+edit-subscription === modified file 'lava_tool/authtoken.py' --- lava_tool/authtoken.py 2011-07-10 23:39:36 +0000 +++ lava_tool/authtoken.py 2012-06-26 02:04:30 +0000 @@ -18,6 +18,8 @@ import base64 import urllib +import urllib2 +import os import xmlrpclib import keyring.core @@ -59,53 +61,39 @@ return self._tokens.get((username, endpoint_url)) -class AuthenticatingTransportMixin: - - def send_request(self, connection, handler, request_body): - xmlrpclib.Transport.send_request( - self, connection, handler, request_body) - if self._auth is None: - return - user, token = urllib.splitpasswd(self._auth) - if token is None: - endpoint_url = '%s://%s%s' % (self._scheme, self._host, handler) - token = self.auth_backend.get_token_for_endpoint( - user, endpoint_url) +class XMLRPCTransport(xmlrpclib.Transport): + + def __init__(self, scheme, auth_backend): + xmlrpclib.Transport.__init__(self) + self._scheme = scheme + self.auth_backend = auth_backend + self._opener = urllib2.build_opener() + self.verbose = 0 + + def request(self, host, handler, request_body, verbose=0): + self.verbose = verbose + token = None + user = None + auth, host = urllib.splituser(host) + if auth: + user, password = urllib.splitpasswd(auth) + url = self._scheme + "://" + host + handler + if user is not None and token is None: + token = self.auth_backend.get_token_for_endpoint(user, url) if token is None: raise LavaCommandError( "Username provided but no token found.") - auth = base64.b64encode(urllib.unquote(user + ':' + token)) - connection.putheader("Authorization", "Basic " + auth) - - def get_host_info(self, host): - # We override stash the auth part away and never send any - # authorization header based soley on the host; we do all that in - # send_request above. - x509 = {} - if isinstance(host, tuple): - host, x509 = host - self._auth, self._host = urllib.splituser(host) - return self._host, None, x509 - - -class AuthenticatingTransport( - AuthenticatingTransportMixin, xmlrpclib.Transport): - - _scheme = 'http' - - def __init__(self, use_datetime=0, auth_backend=None): - xmlrpclib.Transport.__init__(self, use_datetime) - self.auth_backend = auth_backend - - -class AuthenticatingSafeTransport( - AuthenticatingTransportMixin, xmlrpclib.SafeTransport): - - _scheme = 'https' - - def __init__(self, use_datetime=0, auth_backend=None): - xmlrpclib.SafeTransport.__init__(self, use_datetime) - self.auth_backend = auth_backend + request = urllib2.Request(url, request_body) + request.add_header("Content-Type", "text/xml") + if token: + auth = base64.b64encode(urllib.unquote(user + ':' + token)) + request.add_header("Authorization", "Basic " + auth) + try: + response = self._opener.open(request) + except urllib2.HTTPError as e: + raise xmlrpclib.ProtocolError( + host + handler, e.code, e.msg, e.info()) + return self.parse_response(response) class AuthenticatingServerProxy(xmlrpclib.ServerProxy): @@ -113,11 +101,7 @@ def __init__(self, uri, transport=None, encoding=None, verbose=0, allow_none=0, use_datetime=0, auth_backend=None): if transport is None: - if urllib.splittype(uri)[0] == "https": - transport = AuthenticatingSafeTransport( - use_datetime=use_datetime, auth_backend=auth_backend) - else: - transport = AuthenticatingTransport( - use_datetime=use_datetime, auth_backend=auth_backend) + scheme = urllib.splittype(uri)[0] + transport = XMLRPCTransport(scheme, auth_backend=auth_backend) xmlrpclib.ServerProxy.__init__( self, uri, transport, encoding, verbose, allow_none, use_datetime)