Repository: bgpkit/pybgpkit (language: python)

Issue:
allow querying broker service without doing SSL checking
In some cases, the client is sitting behind a network that cannot verify SSL certificate when querying the broker service. In such cases, user will get some error similar to the following:
```
requests.exceptions.ConnectTimeout: HTTPSConnectionPool(host='api.broker.bgpkit.com', port=443): Max retries exceeded with url: /v2/search?ts_start=1634693400&ts_end=2021-10-20T01:30:00&page_size=100 (Caused by ConnectTimeoutError(<urllib3.connection.HTTPSConnection object at 0x7f621b015ea0>, 'Connection to api.broker.bgpkit.com timed out. (connect timeout=None)'))
```

We should add a flag that passes to `requests` to disable SSL verification should the user needs to do so. https://requests.readthedocs.io/en/latest/user/advanced/#ssl-cert-verification


Discussion / hints:

Fix patch:
diff --git a/bgpkit/bgpkit_broker.py b/bgpkit/bgpkit_broker.py
index 848ec21..e7449d1 100644
--- a/bgpkit/bgpkit_broker.py
+++ b/bgpkit/bgpkit_broker.py
@@ -1,6 +1,7 @@
 from dataclasses import dataclass
 
 import requests as requests
+import urllib3
 
 
 def check_type(value: any, ty: type) -> bool:
@@ -24,9 +25,13 @@ class BrokerItem:
 
 class Broker:
 
-    def __init__(self, api_url: str = "https://api.broker.bgpkit.com/v2", page_size: int = 100):
+    def __init__(self, api_url: str = "https://api.broker.bgpkit.com/v2", page_size: int = 100, verify=True):
         self.base_url = api_url.strip()
         self.page_size = int(page_size)
+        self.verify = verify
+        if not verify:
+            # if a user disable SSL verification on-purpose, do not warn the user
+            urllib3.disable_warnings()
 
     def query(self,
               ts_start: str = None,
@@ -58,7 +63,7 @@ def query(self,
         data_items = []
         if print_url:
             print(api_url)
-        res = requests.get(api_url).json()
+        res = requests.get(api_url, verify=self.verify).json()
         while res:
             if res["count"] > 0:
                 data_items.extend([BrokerItem(**i) for i in res["data"]])
@@ -70,7 +75,7 @@ def query(self,
                 query_url = f"{api_url}&page={page}"
                 if print_url:
                     print(query_url)
-                res = requests.get(query_url).json()
+                res = requests.get(query_url, verify=self.verify).json()
             else:
                 break
 
diff --git a/setup.py b/setup.py
index 09cc9d6..d94232d 100644
--- a/setup.py
+++ b/setup.py
@@ -7,7 +7,7 @@
 
 setuptools.setup(
     name='pybgpkit',
-    version='0.4.0',
+    version='0.4.3',
     description='BGPKIT tools Python bindings',
     url='https://github.com/bgpkit/pybgpkit',
     author='Mingwei Zhang',
@@ -19,7 +19,7 @@
     install_requires=[
         # available on pip
         'dataclasses_json',
-        'pybgpkit-parser==0.4.0',
+        'pybgpkit-parser==0.4.2',
         'requests',
     ]
 )


Test patch:
diff --git a/bgpkit/test_integration.py b/bgpkit/test_integration.py
index 6b2dc3f..7e956fc 100644
--- a/bgpkit/test_integration.py
+++ b/bgpkit/test_integration.py
@@ -31,6 +31,14 @@ def test_broker_2(self):
         print(len(items))
         assert len(items) == 7
 
+    def test_broker_no_verify(self):
+        broker = bgpkit.Broker(verify=False)
+        items = broker.query(ts_start="1643760000", ts_end="2022-02-02T00:20:00", collector_id="rrc00")
+        for item in items:
+            print(json.dumps(item.__dict__))
+        print(len(items))
+        assert len(items) == 7
+
     def test_roas(self):
         roas = bgpkit.Roas()
         data = roas.query(debug=True, asn=3333, date="2018-01-01")
diff --git a/test.py b/test.py
index 9d34437..3c309d9 100644
--- a/test.py
+++ b/test.py
@@ -1,6 +1,8 @@
 import bgpkit
 parser = bgpkit.Parser(url="https://spaces.bgpkit.org/parser/update-example",
-                       filters={"peer_ips": "185.1.8.65, 2001:7f8:73:0:3:fa4:0:1"})
+                       filters={"peer_ips": "185.1.8.65, 2001:7f8:73:0:3:fa4:0:1"},
+                       cache_dir="cache"
+                       )
 elems = parser.parse_all()
 assert len(elems) == 4227
 import json