• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1"""Utilities for certificate management."""
2
3import os
4
5certifi_available = False
6certifi_where = None
7try:
8    from certifi import where as certifi_where
9    certifi_available = True
10except ImportError:
11    pass
12
13custom_ca_locater_available = False
14custom_ca_locater_where = None
15try:
16    from ca_certs_locater import get as custom_ca_locater_where
17    custom_ca_locater_available = True
18except ImportError:
19    pass
20
21
22BUILTIN_CA_CERTS = os.path.join(
23    os.path.dirname(os.path.abspath(__file__)), "cacerts.txt"
24)
25
26
27def where():
28    env = os.environ.get("HTTPLIB2_CA_CERTS")
29    if env is not None:
30        if os.path.isfile(env):
31            return env
32        else:
33            raise RuntimeError("Environment variable HTTPLIB2_CA_CERTS not a valid file")
34    if custom_ca_locater_available:
35        return custom_ca_locater_where()
36    if certifi_available:
37        return certifi_where()
38    return BUILTIN_CA_CERTS
39
40
41if __name__ == "__main__":
42    print(where())
43