591b7e
From f9fcf18105845fbb933925ae7b0a2f1033f75127 Mon Sep 17 00:00:00 2001
591b7e
From: Eduardo Otubo <otubo@redhat.com>
591b7e
Date: Wed, 20 May 2020 10:11:14 +0200
591b7e
Subject: [PATCH] url_helper: read_file_or_url should pass headers param into
591b7e
 readurl (#66)
591b7e
591b7e
RH-Author: Eduardo Otubo <otubo@redhat.com>
591b7e
Message-id: <20200519105653.20249-1-otubo@redhat.com>
591b7e
Patchwork-id: 96613
591b7e
O-Subject: [RHEL-7.8.z cloud-init PATCH] url_helper: read_file_or_url should pass headers param into readurl (#66)
591b7e
Bugzilla: 1832177
591b7e
RH-Acked-by: Cathy Avery <cavery@redhat.com>
591b7e
RH-Acked-by: Mohammed Gamal <mgamal@redhat.com>
591b7e
RH-Acked-by: Vitaly Kuznetsov <vkuznets@redhat.com>
591b7e
591b7e
commit f69d33a723b805fec3ee70c3a6127c8cadcb02d8
591b7e
Author: Chad Smith <chad.smith@canonical.com>
591b7e
Date:   Mon Dec 2 16:24:18 2019 -0700
591b7e
591b7e
    url_helper: read_file_or_url should pass headers param into readurl (#66)
591b7e
591b7e
    Headers param was accidentally omitted and no longer passed through to
591b7e
    readurl due to a previous commit.
591b7e
591b7e
    To avoid this omission of params in the future, drop positional param
591b7e
    definitions from read_file_or_url and pass all kwargs through to readurl
591b7e
    when we are not operating on a file.
591b7e
591b7e
    In util:read_seeded, correct the case where invalid positional param
591b7e
    file_retries was being passed into read_file_or_url.
591b7e
591b7e
    Also drop duplicated file:// prefix addition from read_seeded because
591b7e
    read_file_or_url does that work anyway.
591b7e
591b7e
    LP: #1854084
591b7e
591b7e
Signed-off-by: Eduardo Otubo <otubo@redhat.com>
591b7e
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
591b7e
---
591b7e
 cloudinit/sources/helpers/azure.py                 |  6 ++-
591b7e
 cloudinit/tests/test_url_helper.py                 | 52 ++++++++++++++++++++++
591b7e
 cloudinit/url_helper.py                            | 47 +++++++++++++++----
591b7e
 cloudinit/user_data.py                             |  2 +-
591b7e
 cloudinit/util.py                                  | 15 ++-----
591b7e
 .../unittests/test_datasource/test_azure_helper.py | 18 +++++---
591b7e
 6 files changed, 112 insertions(+), 28 deletions(-)
591b7e
591b7e
diff --git a/cloudinit/sources/helpers/azure.py b/cloudinit/sources/helpers/azure.py
591b7e
index c2a57cc..b99c484 100755
591b7e
--- a/cloudinit/sources/helpers/azure.py
591b7e
+++ b/cloudinit/sources/helpers/azure.py
591b7e
@@ -103,14 +103,16 @@ class AzureEndpointHttpClient(object):
591b7e
         if secure:
591b7e
             headers = self.headers.copy()
591b7e
             headers.update(self.extra_secure_headers)
591b7e
-        return url_helper.read_file_or_url(url, headers=headers)
591b7e
+        return url_helper.read_file_or_url(url, headers=headers, timeout=5,
591b7e
+                                           retries=10)
591b7e
 
591b7e
     def post(self, url, data=None, extra_headers=None):
591b7e
         headers = self.headers
591b7e
         if extra_headers is not None:
591b7e
             headers = self.headers.copy()
591b7e
             headers.update(extra_headers)
591b7e
-        return url_helper.read_file_or_url(url, data=data, headers=headers)
591b7e
+        return url_helper.read_file_or_url(url, data=data, headers=headers,
591b7e
+                                           timeout=5, retries=10)
591b7e
 
591b7e
 
591b7e
 class GoalState(object):
591b7e
diff --git a/cloudinit/tests/test_url_helper.py b/cloudinit/tests/test_url_helper.py
591b7e
index aa9f3ec..e883ddc 100644
591b7e
--- a/cloudinit/tests/test_url_helper.py
591b7e
+++ b/cloudinit/tests/test_url_helper.py
591b7e
@@ -4,6 +4,7 @@ from cloudinit.url_helper import (
591b7e
     NOT_FOUND, UrlError, oauth_headers, read_file_or_url, retry_on_url_exc)
591b7e
 from cloudinit.tests.helpers import CiTestCase, mock, skipIf
591b7e
 from cloudinit import util
591b7e
+from cloudinit import version
591b7e
 
591b7e
 import httpretty
591b7e
 import requests
591b7e
@@ -17,6 +18,9 @@ except ImportError:
591b7e
     _missing_oauthlib_dep = True
591b7e
 
591b7e
 
591b7e
+M_PATH = 'cloudinit.url_helper.'
591b7e
+
591b7e
+
591b7e
 class TestOAuthHeaders(CiTestCase):
591b7e
 
591b7e
     def test_oauth_headers_raises_not_implemented_when_oathlib_missing(self):
591b7e
@@ -67,6 +71,54 @@ class TestReadFileOrUrl(CiTestCase):
591b7e
         self.assertEqual(result.contents, data)
591b7e
         self.assertEqual(str(result), data.decode('utf-8'))
591b7e
 
591b7e
+    @mock.patch(M_PATH + 'readurl')
591b7e
+    def test_read_file_or_url_passes_params_to_readurl(self, m_readurl):
591b7e
+        """read_file_or_url passes all params through to readurl."""
591b7e
+        url = 'http://hostname/path'
591b7e
+        response = 'This is my url content\n'
591b7e
+        m_readurl.return_value = response
591b7e
+        params = {'url': url, 'timeout': 1, 'retries': 2,
591b7e
+                  'headers': {'somehdr': 'val'},
591b7e
+                  'data': 'data', 'sec_between': 1,
591b7e
+                  'ssl_details': {'cert_file': '/path/cert.pem'},
591b7e
+                  'headers_cb': 'headers_cb', 'exception_cb': 'exception_cb'}
591b7e
+        self.assertEqual(response, read_file_or_url(**params))
591b7e
+        params.pop('url')  # url is passed in as a positional arg
591b7e
+        self.assertEqual([mock.call(url, **params)], m_readurl.call_args_list)
591b7e
+
591b7e
+    def test_wb_read_url_defaults_honored_by_read_file_or_url_callers(self):
591b7e
+        """Readurl param defaults used when unspecified by read_file_or_url
591b7e
+
591b7e
+        Param defaults tested are as follows:
591b7e
+            retries: 0, additional headers None beyond default, method: GET,
591b7e
+            data: None, check_status: True and allow_redirects: True
591b7e
+        """
591b7e
+        url = 'http://hostname/path'
591b7e
+
591b7e
+        m_response = mock.MagicMock()
591b7e
+
591b7e
+        class FakeSession(requests.Session):
591b7e
+            def request(cls, **kwargs):
591b7e
+                self.assertEqual(
591b7e
+                    {'url': url, 'allow_redirects': True, 'method': 'GET',
591b7e
+                     'headers': {
591b7e
+                         'User-Agent': 'Cloud-Init/%s' % (
591b7e
+                             version.version_string())}},
591b7e
+                    kwargs)
591b7e
+                return m_response
591b7e
+
591b7e
+        with mock.patch(M_PATH + 'requests.Session') as m_session:
591b7e
+            error = requests.exceptions.HTTPError('broke')
591b7e
+            m_session.side_effect = [error, FakeSession()]
591b7e
+            # assert no retries and check_status == True
591b7e
+            with self.assertRaises(UrlError) as context_manager:
591b7e
+                response = read_file_or_url(url)
591b7e
+            self.assertEqual('broke', str(context_manager.exception))
591b7e
+            # assert default headers, method, url and allow_redirects True
591b7e
+            # Success on 2nd call with FakeSession
591b7e
+            response = read_file_or_url(url)
591b7e
+        self.assertEqual(m_response, response._response)
591b7e
+
591b7e
 
591b7e
 class TestRetryOnUrlExc(CiTestCase):
591b7e
 
591b7e
diff --git a/cloudinit/url_helper.py b/cloudinit/url_helper.py
591b7e
index a951b8b..beb6873 100644
591b7e
--- a/cloudinit/url_helper.py
591b7e
+++ b/cloudinit/url_helper.py
591b7e
@@ -81,14 +81,19 @@ def combine_url(base, *add_ons):
591b7e
     return url
591b7e
 
591b7e
 
591b7e
-def read_file_or_url(url, timeout=5, retries=10,
591b7e
-                     headers=None, data=None, sec_between=1, ssl_details=None,
591b7e
-                     headers_cb=None, exception_cb=None):
591b7e
+def read_file_or_url(url, **kwargs):
591b7e
+    """Wrapper function around readurl to allow passing a file path as url.
591b7e
+
591b7e
+    When url is not a local file path, passthrough any kwargs to readurl.
591b7e
+
591b7e
+    In the case of parameter passthrough to readurl, default values for some
591b7e
+    parameters. See: call-signature of readurl in this module for param docs.
591b7e
+    """
591b7e
     url = url.lstrip()
591b7e
     if url.startswith("/"):
591b7e
         url = "file://%s" % url
591b7e
     if url.lower().startswith("file://"):
591b7e
-        if data:
591b7e
+        if kwargs.get("data"):
591b7e
             LOG.warning("Unable to post data to file resource %s", url)
591b7e
         file_path = url[len("file://"):]
591b7e
         try:
591b7e
@@ -101,10 +106,7 @@ def read_file_or_url(url, timeout=5, retries=10,
591b7e
             raise UrlError(cause=e, code=code, headers=None, url=url)
591b7e
         return FileResponse(file_path, contents=contents)
591b7e
     else:
591b7e
-        return readurl(url, timeout=timeout, retries=retries,
591b7e
-                       headers_cb=headers_cb, data=data,
591b7e
-                       sec_between=sec_between, ssl_details=ssl_details,
591b7e
-                       exception_cb=exception_cb)
591b7e
+        return readurl(url, **kwargs)
591b7e
 
591b7e
 
591b7e
 # Made to have same accessors as UrlResponse so that the
591b7e
@@ -201,6 +203,35 @@ def readurl(url, data=None, timeout=None, retries=0, sec_between=1,
591b7e
             check_status=True, allow_redirects=True, exception_cb=None,
591b7e
             session=None, infinite=False, log_req_resp=True,
591b7e
             request_method=None):
591b7e
+    """Wrapper around requests.Session to read the url and retry if necessary
591b7e
+
591b7e
+    :param url: Mandatory url to request.
591b7e
+    :param data: Optional form data to post the URL. Will set request_method
591b7e
+        to 'POST' if present.
591b7e
+    :param timeout: Timeout in seconds to wait for a response
591b7e
+    :param retries: Number of times to retry on exception if exception_cb is
591b7e
+        None or exception_cb returns True for the exception caught. Default is
591b7e
+        to fail with 0 retries on exception.
591b7e
+    :param sec_between: Default 1: amount of seconds passed to time.sleep
591b7e
+        between retries. None or -1 means don't sleep.
591b7e
+    :param headers: Optional dict of headers to send during request
591b7e
+    :param headers_cb: Optional callable returning a dict of values to send as
591b7e
+        headers during request
591b7e
+    :param ssl_details: Optional dict providing key_file, ca_certs, and
591b7e
+        cert_file keys for use on in ssl connections.
591b7e
+    :param check_status: Optional boolean set True to raise when HTTPError
591b7e
+        occurs. Default: True.
591b7e
+    :param allow_redirects: Optional boolean passed straight to Session.request
591b7e
+        as 'allow_redirects'. Default: True.
591b7e
+    :param exception_cb: Optional callable which accepts the params
591b7e
+        msg and exception and returns a boolean True if retries are permitted.
591b7e
+    :param session: Optional exiting requests.Session instance to reuse.
591b7e
+    :param infinite: Bool, set True to retry indefinitely. Default: False.
591b7e
+    :param log_req_resp: Set False to turn off verbose debug messages.
591b7e
+    :param request_method: String passed as 'method' to Session.request.
591b7e
+        Typically GET, or POST. Default: POST if data is provided, GET
591b7e
+        otherwise.
591b7e
+    """
591b7e
     url = _cleanurl(url)
591b7e
     req_args = {
591b7e
         'url': url,
591b7e
diff --git a/cloudinit/user_data.py b/cloudinit/user_data.py
591b7e
index ed83d2d..15af1da 100644
591b7e
--- a/cloudinit/user_data.py
591b7e
+++ b/cloudinit/user_data.py
591b7e
@@ -224,7 +224,7 @@ class UserDataProcessor(object):
591b7e
                 content = util.load_file(include_once_fn)
591b7e
             else:
591b7e
                 try:
591b7e
-                    resp = read_file_or_url(include_url,
591b7e
+                    resp = read_file_or_url(include_url, timeout=5, retries=10,
591b7e
                                             ssl_details=self.ssl_details)
591b7e
                     if include_once_on and resp.ok():
591b7e
                         util.write_file(include_once_fn, resp.contents,
591b7e
diff --git a/cloudinit/util.py b/cloudinit/util.py
591b7e
index 2c9ac66..db9a229 100644
591b7e
--- a/cloudinit/util.py
591b7e
+++ b/cloudinit/util.py
591b7e
@@ -966,13 +966,6 @@ def load_yaml(blob, default=None, allowed=(dict,)):
591b7e
 
591b7e
 
591b7e
 def read_seeded(base="", ext="", timeout=5, retries=10, file_retries=0):
591b7e
-    if base.startswith("/"):
591b7e
-        base = "file://%s" % base
591b7e
-
591b7e
-    # default retries for file is 0. for network is 10
591b7e
-    if base.startswith("file://"):
591b7e
-        retries = file_retries
591b7e
-
591b7e
     if base.find("%s") >= 0:
591b7e
         ud_url = base % ("user-data" + ext)
591b7e
         md_url = base % ("meta-data" + ext)
591b7e
@@ -980,14 +973,14 @@ def read_seeded(base="", ext="", timeout=5, retries=10, file_retries=0):
591b7e
         ud_url = "%s%s%s" % (base, "user-data", ext)
591b7e
         md_url = "%s%s%s" % (base, "meta-data", ext)
591b7e
 
591b7e
-    md_resp = url_helper.read_file_or_url(md_url, timeout, retries,
591b7e
-                                          file_retries)
591b7e
+    md_resp = url_helper.read_file_or_url(md_url, timeout=timeout,
591b7e
+                                          retries=retries)
591b7e
     md = None
591b7e
     if md_resp.ok():
591b7e
         md = load_yaml(decode_binary(md_resp.contents), default={})
591b7e
 
591b7e
-    ud_resp = url_helper.read_file_or_url(ud_url, timeout, retries,
591b7e
-                                          file_retries)
591b7e
+    ud_resp = url_helper.read_file_or_url(ud_url, timeout=timeout,
591b7e
+                                          retries=retries)
591b7e
     ud = None
591b7e
     if ud_resp.ok():
591b7e
         ud = ud_resp.contents
591b7e
diff --git a/tests/unittests/test_datasource/test_azure_helper.py b/tests/unittests/test_datasource/test_azure_helper.py
591b7e
index 7ad5cc1..007df09 100644
591b7e
--- a/tests/unittests/test_datasource/test_azure_helper.py
591b7e
+++ b/tests/unittests/test_datasource/test_azure_helper.py
591b7e
@@ -231,8 +231,10 @@ class TestAzureEndpointHttpClient(CiTestCase):
591b7e
         response = client.get(url, secure=False)
591b7e
         self.assertEqual(1, self.read_file_or_url.call_count)
591b7e
         self.assertEqual(self.read_file_or_url.return_value, response)
591b7e
-        self.assertEqual(mock.call(url, headers=self.regular_headers),
591b7e
-                         self.read_file_or_url.call_args)
591b7e
+        self.assertEqual(
591b7e
+            mock.call(url, headers=self.regular_headers, retries=10,
591b7e
+                      timeout=5),
591b7e
+            self.read_file_or_url.call_args)
591b7e
 
591b7e
     def test_secure_get(self):
591b7e
         url = 'MyTestUrl'
591b7e
@@ -246,8 +248,10 @@ class TestAzureEndpointHttpClient(CiTestCase):
591b7e
         response = client.get(url, secure=True)
591b7e
         self.assertEqual(1, self.read_file_or_url.call_count)
591b7e
         self.assertEqual(self.read_file_or_url.return_value, response)
591b7e
-        self.assertEqual(mock.call(url, headers=expected_headers),
591b7e
-                         self.read_file_or_url.call_args)
591b7e
+        self.assertEqual(
591b7e
+            mock.call(url, headers=expected_headers, retries=10,
591b7e
+                      timeout=5),
591b7e
+            self.read_file_or_url.call_args)
591b7e
 
591b7e
     def test_post(self):
591b7e
         data = mock.MagicMock()
591b7e
@@ -257,7 +261,8 @@ class TestAzureEndpointHttpClient(CiTestCase):
591b7e
         self.assertEqual(1, self.read_file_or_url.call_count)
591b7e
         self.assertEqual(self.read_file_or_url.return_value, response)
591b7e
         self.assertEqual(
591b7e
-            mock.call(url, data=data, headers=self.regular_headers),
591b7e
+            mock.call(url, data=data, headers=self.regular_headers, retries=10,
591b7e
+                      timeout=5),
591b7e
             self.read_file_or_url.call_args)
591b7e
 
591b7e
     def test_post_with_extra_headers(self):
591b7e
@@ -269,7 +274,8 @@ class TestAzureEndpointHttpClient(CiTestCase):
591b7e
         expected_headers = self.regular_headers.copy()
591b7e
         expected_headers.update(extra_headers)
591b7e
         self.assertEqual(
591b7e
-            mock.call(mock.ANY, data=mock.ANY, headers=expected_headers),
591b7e
+            mock.call(mock.ANY, data=mock.ANY, headers=expected_headers,
591b7e
+                      retries=10, timeout=5),
591b7e
             self.read_file_or_url.call_args)
591b7e
 
591b7e
 
591b7e
-- 
591b7e
1.8.3.1
591b7e