Skip to content

Commit ca61026

Browse files
committed
🎨 Wire up TLS cert option interfaces in settings
Usage:: import pygit2 pygit2.settings.ssl_cert_file = '/path/to/file' pygit2.settings.ssl_cert_dir = '/path/to/folder' del pygit2.settings.ssl_cert_file pygit2.settings.set_ssl_cert_locations( '/path/to/new/file', '/path/to/new/folder', ) Co-authored-by: Sriram Raghu <[email protected]> Closes #876 Superseeds and closes #879
1 parent a1505e6 commit ca61026

File tree

1 file changed

+46
-2
lines changed

1 file changed

+46
-2
lines changed

‎pygit2/settings.py

+46-2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
# Boston, MA 02110-1301, USA.
2727
"""Settings mapping."""
2828

29+
from ssl import get_default_verify_paths
30+
2931
from _pygit2 import option
3032
from _pygit2 import GIT_OPT_GET_SEARCH_PATH, GIT_OPT_SET_SEARCH_PATH
3133
from _pygit2 import GIT_OPT_GET_MWINDOW_SIZE, GIT_OPT_SET_MWINDOW_SIZE
@@ -34,7 +36,7 @@
3436
from _pygit2 import GIT_OPT_GET_CACHED_MEMORY
3537
from _pygit2 import GIT_OPT_ENABLE_CACHING
3638
from _pygit2 import GIT_OPT_SET_CACHE_MAX_SIZE
37-
39+
from _pygit2 import GIT_OPT_SET_SSL_CERT_LOCATIONS
3840

3941

4042
__metaclass__ = type # make all classes new-style by default
@@ -52,10 +54,17 @@ def __setitem__(self, key, value):
5254
class Settings:
5355
"""Library-wide settings interface."""
5456

55-
__slots__ = []
57+
__slots__ = '_default_tls_verify_paths', '_ssl_cert_dir', '_ssl_cert_file'
5658

5759
_search_path = SearchPathList()
5860

61+
def __init__(self):
62+
self._default_tls_verify_paths = get_default_verify_paths()
63+
self.set_ssl_cert_locations(
64+
self._default_tls_verify_paths.cafile,
65+
self._default_tls_verify_paths.capath,
66+
)
67+
5968
@property
6069
def search_path(self):
6170
"""Configuration file search path.
@@ -106,4 +115,39 @@ def cache_object_limit(self, object_type, value):
106115
"""
107116
return option(GIT_OPT_SET_CACHE_OBJECT_LIMIT, object_type, value)
108117

118+
@property
119+
def ssl_cert_file(self):
120+
"""TLS certificate file path."""
121+
return self._ssl_cert_file
122+
123+
@ssl_cert_file.setter
124+
def ssl_cert_file(self, value):
125+
"""Set the TLS cert file path."""
126+
self.set_ssl_cert_locations(value, self._ssl_cert_dir)
127+
128+
@ssl_cert_file.deleter
129+
def ssl_cert_file(self):
130+
"""Reset the TLS cert file path."""
131+
self.ssl_cert_file = self._default_tls_verify_paths.cafile
109132

133+
@property
134+
def ssl_cert_dir(self):
135+
"""TLS certificates lookup directory path."""
136+
return self._ssl_cert_dir
137+
138+
@ssl_cert_dir.setter
139+
def ssl_cert_dir(self, value):
140+
"""Set the TLS certificate lookup folder."""
141+
self.set_ssl_cert_locations(self._ssl_cert_file, value)
142+
143+
@ssl_cert_dir.deleter
144+
def ssl_cert_dir(self):
145+
"""Reset the TLS certificate lookup folder."""
146+
self.ssl_cert_dir = self._default_tls_verify_paths.capath
147+
148+
def set_ssl_cert_locations(self, ssl_cert_file, ssl_cert_dir):
149+
"""Set both file path and lookup dir for TLS certs in libgit2.
150+
"""
151+
option(GIT_OPT_SET_SSL_CERT_LOCATIONS, ssl_cert_file, ssl_cert_dir)
152+
self._ssl_cert_file = ssl_cert_file
153+
self._ssl_cert_dir = ssl_cert_dir

0 commit comments

Comments
 (0)