Package: lava-dispatcher
Severity: normal
Tags: patch

For private testing it can be useful to specify an image with a username &
password. Attached patch implements this in what seems to be the least evasive
way possible. A nicer way potentially be to have seperate fields for the
username & password, but that requires threading those variables through quite
a few functions

-- System Information:
Debian Release: jessie/sid
  APT prefers unstable
  APT policy: (500, 'unstable'), (500, 'testing'), (500, 'stable'), (101, 
'experimental')
Architecture: amd64 (x86_64)
Foreign Architectures: i386

Kernel: Linux 3.16-1-amd64 (SMP w/4 CPU cores)
Locale: LANG=en_US.utf8, LC_CTYPE=en_US.utf8 (charmap=UTF-8)
Shell: /bin/sh linked to /bin/dash
>From 442463f556979fedcfabc522d543fdc39e4d3f0e Mon Sep 17 00:00:00 2001
From: Sjoerd Simons <[email protected]>
Date: Tue, 23 Sep 2014 11:02:35 +0200
Subject: [PATCH] Downloader: Support http://user:pass@host urls

For private images it can be useful to specify the download urls with a
username and password. Unfortunately urllib2 doesn't support those
directly so some manual work is required to get those going.
---
 lava_dispatcher/downloader.py | 19 +++++++++++++++++--
 1 file changed, 17 insertions(+), 2 deletions(-)

diff --git a/lava_dispatcher/downloader.py b/lava_dispatcher/downloader.py
index f2f66f8..b6ce685 100644
--- a/lava_dispatcher/downloader.py
+++ b/lava_dispatcher/downloader.py
@@ -60,14 +60,29 @@ def _http_stream(url, proxy=None, cookies=None):
     handlers = []
     if proxy:
         handlers = [urllib2.ProxyHandler({'http': '%s' % proxy})]
+
+    if url.username != None and url.password !=  None:
+        # HACK, urllib2 doesn't like urls with username and pass
+        url_string = urlparse.urlunparse([ url.scheme,
+            url.netloc.partition("@")[2],
+            url.path,
+            url.params,
+            url.query,
+            url.fragment])
+        passmgr = urllib2.HTTPPasswordMgrWithDefaultRealm()
+        passmgr.add_password(None, url_string, url.username, url.password)
+        handlers.append(urllib2.HTTPBasicAuthHandler(passmgr))
+    else:
+        url_string = url.geturl()
+
     opener = urllib2.build_opener(*handlers)
 
     if cookies:
         opener.addheaders.append(('Cookie', cookies))
 
     try:
-        url = urllib2.quote(url.geturl(), safe=":/")
-        resp = opener.open(url, timeout=30)
+        url_quoted = urllib2.quote(url_string, safe=":/")
+        resp = opener.open(url_quoted, timeout=30)
         yield resp
     finally:
         if resp:
-- 
2.1.0

Reply via email to