diff --git a/poetry.lock b/poetry.lock index d83a13dd..389046e9 100644 --- a/poetry.lock +++ b/poetry.lock @@ -690,7 +690,6 @@ files = [ {file = "PyYAML-6.0.1-cp311-cp311-win_amd64.whl", hash = "sha256:bf07ee2fef7014951eeb99f56f39c9bb4af143d8aa3c21b1677805985307da34"}, {file = "PyYAML-6.0.1-cp312-cp312-macosx_10_9_x86_64.whl", hash = "sha256:855fb52b0dc35af121542a76b9a84f8d1cd886ea97c84703eaa6d88e37a2ad28"}, {file = "PyYAML-6.0.1-cp312-cp312-macosx_11_0_arm64.whl", hash = "sha256:40df9b996c2b73138957fe23a16a4f0ba614f4c0efce1e9406a184b6d07fa3a9"}, - {file = "PyYAML-6.0.1-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:a08c6f0fe150303c1c6b71ebcd7213c2858041a7e01975da3a99aed1e7a378ef"}, {file = "PyYAML-6.0.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:6c22bec3fbe2524cde73d7ada88f6566758a8f7227bfbf93a408a9d86bcc12a0"}, {file = "PyYAML-6.0.1-cp312-cp312-musllinux_1_1_x86_64.whl", hash = "sha256:8d4e9c88387b0f5c7d5f281e55304de64cf7f9c0021a3525bd3b1c542da3b0e4"}, {file = "PyYAML-6.0.1-cp312-cp312-win32.whl", hash = "sha256:d483d2cdf104e7c9fa60c544d92981f12ad66a457afae824d146093b8c294c54"}, @@ -987,4 +986,4 @@ testing = ["big-O", "flake8 (<5)", "jaraco.functools", "jaraco.itertools", "more [metadata] lock-version = "2.0" python-versions = "^3.7" -content-hash = "cc7429f4b20b51e8c6148a9ba7a851d7f823bccf0a2e1aae58b761e159d4021b" +content-hash = "59b190cdf2cc28d66f4e8a38d21e480142984311bee9050a304d32aa36acad4f" diff --git a/pyproject.toml b/pyproject.toml index 76add752..dfa08081 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -24,8 +24,6 @@ repository = "https://github.com/splunk/addonfactory-solutions-library-python" [tool.poetry.dependencies] python = "^3.7" -requests = "^2.31.0" -urllib3 = "<2" sortedcontainers = ">=2" defusedxml = ">=0.7" splunk-sdk = ">=1.6" diff --git a/solnlib/splunk_rest_client.py b/solnlib/splunk_rest_client.py index c83c2545..9a686027 100644 --- a/solnlib/splunk_rest_client.py +++ b/solnlib/splunk_rest_client.py @@ -25,6 +25,7 @@ import os import traceback from io import BytesIO +from urllib.error import HTTPError from urllib.parse import quote from splunklib import binding, client @@ -72,18 +73,16 @@ def _request_handler(context): """ try: - import requests + import urllib.request + import urllib.parse + import urllib.error + import ssl except ImportError: # FIXME proxy ? return binding.handler( key_file=context.get("key_file"), cert_file=context.get("cert_file") ) - try: - requests.urllib3.disable_warnings() - except AttributeError: - pass - proxies = _get_proxy_info(context) verify = context.get("verify", False) @@ -98,17 +97,7 @@ def _request_handler(context): else: cert = None - if context.get("pool_connections", 0): - logging.info("Use HTTP connection pooling") - session = requests.Session() - adapter = requests.adapters.HTTPAdapter( - pool_connections=context.get("pool_connections", 10), - pool_maxsize=context.get("pool_maxsize", 10), - ) - session.mount("https://", adapter) - req_func = session.request - else: - req_func = requests.request + req_func = urllib.request.urlopen def request(url, message, **kwargs): """ @@ -130,6 +119,8 @@ def request(url, message, **kwargs): } if body: + if not isinstance(body, bytes): + body = body.encode("utf-8") headers["Content-Length"] = str(len(body)) for key, value in message["headers"]: @@ -138,17 +129,26 @@ def request(url, message, **kwargs): method = message.get("method", "GET") try: - resp = req_func( - method, - url, - data=body, - headers=headers, - stream=False, - verify=verify, - proxies=proxies, - cert=cert, - **kwargs, - ) + req = urllib.request.Request(url, body, headers, method=method) + + proxy_support = urllib.request.ProxyHandler(proxies) + opener = urllib.request.build_opener(proxy_support) + urllib.request.install_opener(opener) + + context = ssl.create_default_context() + if not verify: + context.check_hostname = False + context.verify_mode = ssl.CERT_NONE + resp = req_func(req, cafile=cert, context=context) + + except HTTPError as err: + return { + "status": err.code, + "reason": err.reason, + "headers": dict(err.headers), + "body": BytesIO(err.fp.read()), + } + except Exception: logging.error( "Failed to issue http request=%s to url=%s, error=%s", @@ -159,10 +159,10 @@ def request(url, message, **kwargs): raise return { - "status": resp.status_code, + "status": resp.status, "reason": resp.reason, "headers": dict(resp.headers), - "body": BytesIO(resp.content), + "body": BytesIO(resp.fp.read()), } return request