From 861e6c7cefe45f7d6a7e2472cf4bfb0a600f00ec Mon Sep 17 00:00:00 2001
From: Tomas Kopecek <tkopecek@redhat.com>
Date: Sep 21 2017 12:43:43 +0000
Subject: [PATCH 1/2] honour no_ssl_verify in download_file


Related: https://pagure.io/koji/issue/594

---

diff --git a/cli/koji_cli/commands.py b/cli/koji_cli/commands.py
index 56a0446..0f23ff6 100644
--- a/cli/koji_cli/commands.py
+++ b/cli/koji_cli/commands.py
@@ -6596,7 +6596,8 @@ def anon_handle_download_build(options, session, args):
             urls.append((url, os.path.basename(fname)))
 
     for url, relpath in urls:
-        download_file(url, relpath, suboptions.quiet, suboptions.noprogress)
+        download_file(url, relpath, suboptions.quiet, suboptions.noprogress,
+                      no_ssl_verify=options.no_ssl_verify)
 
 
 def anon_handle_download_logs(options, session, args):
@@ -6802,7 +6803,8 @@ def anon_handle_download_task(options, session, args):
         if '..' in filename:
             error(_('Invalid file name: %s') % filename)
         url = '%s/%s/%s' % (pathinfo.work(volume), pathinfo.taskrelpath(task["id"]), filename)
-        download_file(url, new_filename, suboptions.quiet, suboptions.noprogress, len(downloads), number)
+        download_file(url, new_filename, suboptions.quiet, suboptions.noprogress,
+                      len(downloads), number, no_ssl_verify=options.no_ssl_verify)
 
 
 def anon_handle_wait_repo(options, session, args):
diff --git a/cli/koji_cli/lib.py b/cli/koji_cli/lib.py
index 124780a..df03a51 100644
--- a/cli/koji_cli/lib.py
+++ b/cli/koji_cli/lib.py
@@ -471,7 +471,7 @@ def linked_upload(localfile, path, name=None):
         os.umask(old_umask)
 
 
-def download_file(url, relpath, quiet=False, noprogress=False, size=None, num=None):
+def download_file(url, relpath, quiet=False, noprogress=False, size=None, num=None, no_ssl_verify=False):
     """Download files from remote"""
 
     if '/' in relpath:
@@ -486,6 +486,9 @@ def download_file(url, relpath, quiet=False, noprogress=False, size=None, num=No
     # allow 301/302 redirect
     c.setopt(pycurl.FOLLOWLOCATION, 1)
     c.setopt(c.WRITEDATA, open(relpath, 'wb'))
+    if no_ssl_verify:
+        c.setopt(pycurl.SSL_VERIFYHOST, 0)
+        c.setopt(pycurl.SSL_VERIFYPEER, 0)
     if not (quiet or noprogress):
         proc_func_param = getattr(c, 'XFERINFOFUNCTION', None)
         if proc_func_param is None:

From 4090aa244b1b13fa9cb92b3f491b152379e2c951 Mon Sep 17 00:00:00 2001
From: Tomas Kopecek <tkopecek@redhat.com>
Date: Sep 21 2017 12:52:50 +0000
Subject: [PATCH 2/2] fix test for no_ssl_verify


---

diff --git a/tests/test_cli/test_download_task.py b/tests/test_cli/test_download_task.py
index d698a4e..ea7137c 100644
--- a/tests/test_cli/test_download_task.py
+++ b/tests/test_cli/test_download_task.py
@@ -33,13 +33,15 @@ class TestDownloadTask(unittest.TestCase):
             url = pattern % (subpath, k)
             if target.endswith('.log') and arch is not None:
                 target = "%s.%s.log" % (target.rstrip(".log"), arch)
-            calls.append(call(url, target, None, None, total, i + 1))
+            calls.append(call(url, target, None, None, total, i + 1,
+                              no_ssl_verify=self.options.no_ssl_verify))
         return calls
 
     def setUp(self):
         # Mock out the options parsed in main
         self.options = mock.MagicMock()
         self.options.quiet = None
+        self.options.no_ssl_verify = False
         self.options.topurl = 'https://topurl'
         # Mock out the xmlrpc server
         self.session = mock.MagicMock()
@@ -133,11 +135,14 @@ class TestDownloadTask(unittest.TestCase):
             call(self.session, 44444)])
         self.assertListEqual(self.download_file.mock_calls, [
             call('https://topurl/work/tasks/3333/33333/somerpm.x86_64.rpm',
-                 'somerpm.x86_64.rpm', None, None, 3, 1),
+                 'somerpm.x86_64.rpm', None, None, 3, 1,
+                 no_ssl_verify=self.options.no_ssl_verify),
             call('https://topurl/vol/vol2/work/tasks/3333/33333/somerpm.x86_64.rpm',
-                 'vol2/somerpm.x86_64.rpm', None, None, 3, 2),
+                 'vol2/somerpm.x86_64.rpm', None, None, 3, 2,
+                 no_ssl_verify=self.options.no_ssl_verify),
             call('https://topurl/vol/vol3/work/tasks/4444/44444/somerpm.noarch.rpm',
-                 'vol3/somerpm.noarch.rpm', None, None, 3, 3)])
+                 'vol3/somerpm.noarch.rpm', None, None, 3, 3,
+                 no_ssl_verify=self.options.no_ssl_verify)])
         self.assertIsNone(rv)
 
     def test_handle_download_task_log(self):

