Skip to content

Commit

Permalink
Update to requests~=2.30 and urllib3~=2.0, use sessions for ciphe…
Browse files Browse the repository at this point in the history
…rs (#718)

* Updated to `requests~=2.30` and `urllib3~=2.0` with SSL context factory

* Use sessions in `UrlScheme`
  • Loading branch information
Helveg committed May 10, 2023
1 parent 0d1ab9e commit 2d1d7b3
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 26 deletions.
67 changes: 42 additions & 25 deletions bsb/storage/_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,8 @@ def resolve_uri(self, file: FileDependency):
return file.uri

def find(self, file: FileDependency):
response = _rq.head(self.resolve_uri(file))
with self.create_session() as session:
response = session.head(self.resolve_uri(file))
return response.status_code == 200

def should_update(self, file: FileDependency, stored_file):
Expand All @@ -244,23 +245,29 @@ def should_update(self, file: FileDependency, stored_file):
return _t.time() > mtime + 360000

def get_content(self, file: FileDependency):
response = _rq.get(self.resolve_uri(file))
with self.create_session() as session:
response = session.get(self.resolve_uri(file))
return (response.content, response.encoding)

def get_meta(self, file: FileDependency):
response = _rq.head(self.resolve_uri(file))
with self.create_session() as session:
response = session.head(self.resolve_uri(file))
return {"headers": dict(response.headers)}

def get_local_path(self, file: FileDependency):
raise TypeError("URL schemes don't have a local path")

@_cl.contextmanager
def provide_stream(self, file):
response = _rq.get(self.resolve_uri(file), stream=True)
with self.create_session() as session:
response = session.get(self.resolve_uri(file), stream=True)
response.raw.decode_content = True
response.raw.auto_close = False
yield (response.raw, response.encoding)

def create_session(self):
return _rq.Session()


class NeuroMorphoScheme(UrlScheme):
_nm_url = "https://neuromorpho.org/"
Expand All @@ -274,28 +281,16 @@ def resolve_uri(self, file: FileDependency):
@_ft.cache
def get_nm_meta(self, file: FileDependency):
name = _up.urlparse(file.uri).hostname
# Weak DH key on neuromorpho.org
# https://stackoverflow.com/questions/38015537/python-requests-exceptions-sslerror-dh-key-too-small
_rq.packages.urllib3.util.ssl_.DEFAULT_CIPHERS += ":HIGH:!DH:!aNULL"
try:
_rq.packages.urllib3.contrib.pyopenssl.util.ssl_.DEFAULT_CIPHERS += (
":HIGH:!DH:!aNULL"
)
except AttributeError:
# no pyopenssl support used / needed / available
pass
with warnings.catch_warnings():
warnings.simplefilter("ignore")
# Certificate issues with neuromorpho --> verify=False
try:
res = _rq.get(self._nm_url + self._meta + name, verify=False)
except Exception as e:
return {"archive": "none", "neuron_name": "none"}
if res.status_code == 404:
raise IOError(f"'{name}' is not a valid NeuroMorpho name.")
elif res.status_code != 200:
raise IOError("NeuroMorpho API error: " + res.message)
return res.json()
with self.create_session() as session:
res = session.get(self._nm_url + self._meta + name)
except Exception as e:
return {"archive": "none", "neuron_name": "none"}
if res.status_code == 404:
raise IOError(f"'{name}' is not a valid NeuroMorpho name.")
elif res.status_code != 200:
raise IOError("NeuroMorpho API error: " + res.text)
return res.json()

def get_meta(self, file: FileDependency):
meta = super().get_meta(file)
Expand All @@ -307,6 +302,28 @@ def _swc_url(cls, archive, name):
base_url = f"{cls._nm_url}{cls._files}{_up.quote(archive.lower())}"
return f"{base_url}/CNG%20version/{name}.CNG.swc"

def create_session(self):
# Weak DH key on neuromorpho.org
# https://stackoverflow.com/a/76217135/1016004
from requests.adapters import HTTPAdapter
from urllib3.util import create_urllib3_context
from urllib3 import PoolManager

class DHAdapter(HTTPAdapter):
def init_poolmanager(self, connections, maxsize, block=False, **kwargs):
ctx = create_urllib3_context(ciphers=":HIGH:!DH:!aNULL")
self.poolmanager = PoolManager(
num_pools=connections,
maxsize=maxsize,
block=block,
ssl_context=ctx,
**kwargs,
)

session = _rq.Session()
session.mount(self._nm_url, DHAdapter())
return session


@_ft.cache
def _get_schemes() -> _tp.Mapping[str, FileScheme]:
Expand Down
4 changes: 4 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,7 @@ nrn-subprocess==1.3.4

# Plugins
bsb-hdf5==0.7.4

# HTTP
requests==2.30.0
urllib3==2.0.2
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
"PyYAML~=6.0",
"morphio~=3.3",
"toml",
"requests",
"requests~=2.30",
"urllib3~=2.0",
"appdirs~=1.4",
"neo[nixio]",
"tqdm~=4.50",
Expand Down

0 comments on commit 2d1d7b3

Please sign in to comment.