From 82d25a812d848ad24442e7cfaed9c89275c95f76 Mon Sep 17 00:00:00 2001
From: Lukas Holecek <hluk@email.cz>
Date: Nov 20 2019 13:22:13 +0000
Subject: Use timeout and verify request arguments consistently


JIRA: FACTORY-5484
Signed-off-by: Lukas Holecek <hluk@email.cz>

---

diff --git a/greenwave/api_v1.py b/greenwave/api_v1.py
index 2ca5387..ba95205 100644
--- a/greenwave/api_v1.py
+++ b/greenwave/api_v1.py
@@ -436,10 +436,7 @@ def make_decision():
     answers = []
     verbose_results = []
     applicable_policies = []
-    retriever_args = dict(
-        when=when,
-        timeout=current_app.config['REQUESTS_TIMEOUT'],
-        verify=current_app.config['REQUESTS_VERIFY'])
+    retriever_args = {'when': when}
     results_retriever = ResultsRetriever(
         ignore_ids=ignore_results,
         url=current_app.config['RESULTSDB_API_URL'],
diff --git a/greenwave/request_session.py b/greenwave/request_session.py
index c256afb..4817bdb 100644
--- a/greenwave/request_session.py
+++ b/greenwave/request_session.py
@@ -6,6 +6,8 @@ from requests.exceptions import ConnectionError, ConnectTimeout, RetryError
 from urllib3.util.retry import Retry
 from urllib3.exceptions import ProxyError, SSLError
 
+from flask import current_app, has_app_context
+
 from greenwave import __version__
 
 
@@ -25,6 +27,12 @@ class ErrorResponse(requests.Response):
 class RequestsSession(requests.Session):
     def request(self, *args, **kwargs):  # pylint:disable=arguments-differ
         req_url = kwargs.get('url', args[1])
+
+        kwargs.setdefault('headers', {'Content-Type': 'application/json'})
+        if has_app_context():
+            kwargs.setdefault('timeout', current_app.config['REQUESTS_TIMEOUT'])
+            kwargs.setdefault('verify', current_app.config['REQUESTS_VERIFY'])
+
         try:
             return super().request(*args, **kwargs)
         except (ConnectTimeout, RetryError) as e:
diff --git a/greenwave/resources.py b/greenwave/resources.py
index 50ecf2a..8242d13 100644
--- a/greenwave/resources.py
+++ b/greenwave/resources.py
@@ -8,7 +8,6 @@ waiverdb, etc..).
 
 import logging
 import re
-import json
 import socket
 
 from urllib.parse import urlparse
@@ -25,10 +24,8 @@ requests_session = get_requests_session()
 
 
 class BaseRetriever:
-    def __init__(self, ignore_ids, when, timeout, verify, url):
+    def __init__(self, ignore_ids, when, url):
         self.ignore_ids = ignore_ids
-        self.timeout = timeout
-        self.verify = verify
         self.url = url
 
         if when:
@@ -41,7 +38,7 @@ class BaseRetriever:
         return [item for item in items if item['id'] not in self.ignore_ids]
 
     def _retrieve_data(self, params):
-        response = self._make_request(params, verify=self.verify, timeout=self.timeout)
+        response = self._make_request(params)
         response.raise_for_status()
         return response.json()['data']
 
@@ -105,8 +102,7 @@ class WaiversRetriever(BaseRetriever):
     def _make_request(self, params, **request_args):
         return requests_session.post(
             self.url + '/waivers/+filtered',
-            headers={'Content-Type': 'application/json'},
-            data=json.dumps({'filters': params}),
+            json={'filters': params},
             **request_args)
 
 
@@ -176,9 +172,7 @@ def retrieve_yaml_remote_rule(rev, pkg_name, pkg_namespace):
         "rev": rev
     }
     url = current_app.config['DIST_GIT_URL_TEMPLATE'].format(**data)
-    response = requests_session.request('HEAD', url,
-                                        headers={'Content-Type': 'application/json'},
-                                        timeout=60)
+    response = requests_session.request('HEAD', url)
     if response.status_code == 404:
         return None
 
@@ -186,19 +180,13 @@ def retrieve_yaml_remote_rule(rev, pkg_name, pkg_namespace):
         raise BadGateway('Error occurred looking for gating.yaml file in the dist-git repo.')
 
     # gating.yaml found...
-    response = requests_session.request('GET', url,
-                                        headers={'Content-Type': 'application/json'},
-                                        timeout=60)
+    response = requests_session.request('GET', url)
     response.raise_for_status()
     return response.content
 
 
 # NOTE - not cached.
 def retrieve_decision(greenwave_url, data):
-    timeout = current_app.config['REQUESTS_TIMEOUT']
-    verify = current_app.config['REQUESTS_VERIFY']
-    headers = {'Content-Type': 'application/json'}
-    response = requests_session.post(greenwave_url, headers=headers, data=json.dumps(data),
-                                     timeout=timeout, verify=verify)
+    response = requests_session.post(greenwave_url, json=data)
     response.raise_for_status()
     return response.json()
diff --git a/greenwave/tests/test_policies.py b/greenwave/tests/test_policies.py
index b1c15c0..396ac35 100644
--- a/greenwave/tests/test_policies.py
+++ b/greenwave/tests/test_policies.py
@@ -37,8 +37,6 @@ class DummyResultsRetriever(ResultsRetriever):
         super(DummyResultsRetriever, self).__init__(
             ignore_ids=[],
             when='',
-            timeout=0,
-            verify=False,
             url='')
         self.subject = subject
         self.testcase = testcase
diff --git a/greenwave/tests/test_retrieve_gating_yaml.py b/greenwave/tests/test_retrieve_gating_yaml.py
index 6212ac1..222d335 100644
--- a/greenwave/tests/test_retrieve_gating_yaml.py
+++ b/greenwave/tests/test_retrieve_gating_yaml.py
@@ -123,9 +123,7 @@ def test_retrieve_yaml_remote_rule_no_namespace():
             retrieve_yaml_remote_rule("deadbeaf", "pkg", "")
 
             expected_call = mock.call(
-                'HEAD',
-                'https://src.fedoraproject.org/pkg/raw/deadbeaf/f/gating.yaml',
-                headers={'Content-Type': 'application/json'}, timeout=60)
+                'HEAD', 'https://src.fedoraproject.org/pkg/raw/deadbeaf/f/gating.yaml')
             assert session.request.mock_calls == [expected_call]
 
 
diff --git a/greenwave/tests/test_waivers_retriever.py b/greenwave/tests/test_waivers_retriever.py
index f1c2766..ea44be6 100644
--- a/greenwave/tests/test_waivers_retriever.py
+++ b/greenwave/tests/test_waivers_retriever.py
@@ -7,8 +7,6 @@ from greenwave.resources import WaiversRetriever
 _DUMMY_RETRIEVER_ARGUMENTS = dict(
     ignore_ids=[],
     when=None,
-    timeout=None,
-    verify=None,
     url=None,
 )
 

