From 1cd807640ad8effa2b89c24ca54ea02d78289306 Mon Sep 17 00:00:00 2001 From: trek333 Date: Fri, 9 Sep 2022 13:30:20 -0500 Subject: [PATCH 1/2] Added test case for cookie persistence --- tests/test_binding.py | 50 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/tests/test_binding.py b/tests/test_binding.py index c101b19c..01d7e138 100755 --- a/tests/test_binding.py +++ b/tests/test_binding.py @@ -491,6 +491,56 @@ def test_handlers(self): body = context.get(path).body.read() self.assertTrue(isatom(body)) +def urllib2_insert_cookie_handler(url, message, **kwargs): + method = message['method'].lower() + data = message.get('body', b"") if method == 'post' else None + headers = dict(message.get('headers', [])) + req = Request(url, data, headers) + try: + # If running Python 2.7.9+, disable SSL certificate validation + if sys.version_info >= (2, 7, 9): + response = urlopen(req, context=ssl._create_unverified_context()) + else: + response = urlopen(req) + except HTTPError as response: + pass # Propagate HTTP errors via the returned response message + + # Mimic the insertion of 3rd party cookies into the response. + # An example is "sticky session"/"insert cookie" persistence + # of a load balancer for a SHC. + header_list = response.getheaders() + header_list.append(("Set-Cookie", "BIGipServer_splunk-shc-8089=1234567890.12345.0000; path=/; Httponly; Secure")) + header_list.append(("Set-Cookie", "home_made=yummy")) + + return { + 'status': response.code, + 'reason': response.msg, + 'headers': header_list, + 'body': BytesIO(response.read()) + } + +class TestCookiePersistence(testlib.SDKTestCase): + # Verify persistence of 3rd party inserted cookies. + def test_3rdPartyInsertedCookiePersistence(self): + paths = ["/services", "authentication/users", + "search/jobs"] + logging.debug("Connecting with urllib2_insert_cookie_handler %s", urllib2_insert_cookie_handler) + context = binding.connect( + handler=urllib2_insert_cookie_handler, + **self.opts.kwargs) + + persisted_cookies = context.get_cookies() + + splunk_token_found = False + for k, v in persisted_cookies.items(): + if k[:8] == "splunkd_": + splunk_token_found = True + break + + self.assertEqual(splunk_token_found, True) + self.assertEqual(persisted_cookies['BIGipServer_splunk-shc-8089'], "1234567890.12345.0000") + self.assertEqual(persisted_cookies['home_made'], "yummy") + @pytest.mark.smoke class TestLogout(BindingTestCase): def test_logout(self): From 2fa7723d00cbb85e8de34a8b13eb530e57b3d079 Mon Sep 17 00:00:00 2001 From: trek333 Date: Mon, 24 Oct 2022 10:05:32 -0500 Subject: [PATCH 2/2] fixed get headers logic --- tests/test_binding.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_binding.py b/tests/test_binding.py index 01d7e138..aa3a1391 100755 --- a/tests/test_binding.py +++ b/tests/test_binding.py @@ -508,7 +508,7 @@ def urllib2_insert_cookie_handler(url, message, **kwargs): # Mimic the insertion of 3rd party cookies into the response. # An example is "sticky session"/"insert cookie" persistence # of a load balancer for a SHC. - header_list = response.getheaders() + header_list = [(k, v) for k, v in response.info().items()] header_list.append(("Set-Cookie", "BIGipServer_splunk-shc-8089=1234567890.12345.0000; path=/; Httponly; Secure")) header_list.append(("Set-Cookie", "home_made=yummy"))