Skip to content

Commit 5273205

Browse files
committed
check the SSLCALISTLOC environment variable for SSL CAs
1 parent 93de005 commit 5273205

File tree

4 files changed

+36
-9
lines changed

4 files changed

+36
-9
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ Unreleased
44
**Improvements**
55
- public_model task also defines methods mapped to MAS module steps when publishing to MAS.
66
- SSL verification can be disable with `SSLREQCERT` environment variable.
7+
- CAs to use for validating SSL certificates can also be specified through the `SSLCALISTLOC` environment variable.
78
- Added `execute_performance_task`
89

910
**Changes**

doc/index.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,11 @@ Environment Variables
272272

273273
.. envvar:: CAS_CLIENT_SSL_CA_LIST
274274

275+
Client-side path to a certificate file containing :abbr:`CA (Certificate Authority)` certificates to be trusted. Used by the :mod:`swat` module. This
276+
will take precedence over :envvar:`SSLCALISTLOC` and :envvar:`REQUESTS_CA_BUNDLE`.
277+
278+
.. envvar:: SSLCALISTLOC
279+
275280
Client-side path to a certificate file containing :abbr:`CA (Certificate Authority)` certificates to be trusted. Used by the :mod:`swat` module. This
276281
will take precedence over :envvar:`REQUESTS_CA_BUNDLE`.
277282

src/sasctl/core.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -207,10 +207,16 @@ class Session(requests.Session):
207207
filters
208208
209209
"""
210-
def __init__(self, host, user=None, password=None, authinfo=None,
211-
protocol=None, port=None, verify_ssl=None):
210+
def __init__(self, host,
211+
user=None,
212+
password=None,
213+
authinfo=None,
214+
protocol=None,
215+
port=None,
216+
verify_ssl=None):
212217
super(Session, self).__init__()
213218

219+
# Determine whether or not server SSL certificates should be verified.
214220
if verify_ssl is None:
215221
verify_ssl = os.environ.get('SSLREQCERT', 'yes')
216222
verify_ssl = str(verify_ssl).lower() not in ('no', 'false')
@@ -220,9 +226,10 @@ def __init__(self, host, user=None, password=None, authinfo=None,
220226

221227
# If certificate path has already been set for SWAT package, make
222228
# Requests module reuse it.
223-
if 'CAS_CLIENT_SSL_CA_LIST' in os.environ:
224-
os.environ['REQUESTS_CA_BUNDLE'] = os.environ[
225-
'CAS_CLIENT_SSL_CA_LIST']
229+
for k in ['SSLCALISTLOC', 'CAS_CLIENT_SSL_CA_LIST']:
230+
if k in os.environ:
231+
os.environ['REQUESTS_CA_BUNDLE'] = os.environ[k]
232+
break
226233

227234
# If certificate path hasn't been specified in either environment
228235
# variable, replace the default adapter with one that will use the

tests/unit/test_session.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,7 @@ def test_ssl_context():
176176
import os
177177
from sasctl.core import SSLContextAdapter
178178

179-
if 'CAS_CLIENT_SSL_CA_LIST' in os.environ: del os.environ[
180-
'CAS_CLIENT_SSL_CA_LIST']
179+
if 'CAS_CLIENT_SSL_CA_LIST' in os.environ: del os.environ['CAS_CLIENT_SSL_CA_LIST']
181180
if 'REQUESTS_CA_BUNDLE' in os.environ: del os.environ['REQUESTS_CA_BUNDLE']
182181

183182
# Should default to SSLContextAdapter if no certificate paths are set
@@ -196,10 +195,25 @@ def test_ssl_context():
196195
os.environ['CAS_CLIENT_SSL_CA_LIST'] = 'path_for_swat'
197196
with mock.patch('sasctl.core.Session.get_token', return_value='token'):
198197
s = Session('hostname', 'username', 'password')
199-
assert os.environ['CAS_CLIENT_SSL_CA_LIST'] == os.environ[
200-
'REQUESTS_CA_BUNDLE']
198+
assert os.environ['CAS_CLIENT_SSL_CA_LIST'] == os.environ['REQUESTS_CA_BUNDLE']
201199
assert not isinstance(s.get_adapter('https://'), SSLContextAdapter)
202200

201+
# Cleanup
202+
del os.environ['CAS_CLIENT_SSL_CA_LIST']
203+
del os.environ['REQUESTS_CA_BUNDLE']
204+
205+
# If SWAT env variable is set, it should override the Requests variable
206+
os.environ['SSLCALISTLOC'] = 'path_for_swat'
207+
with mock.patch('sasctl.core.Session.get_token', return_value='token'):
208+
s = Session('hostname', 'username', 'password')
209+
assert os.environ['SSLCALISTLOC'] == os.environ['REQUESTS_CA_BUNDLE']
210+
assert 'CAS_CLIENT_SSL_CA_LIST' not in os.environ
211+
assert not isinstance(s.get_adapter('https://'), SSLContextAdapter)
212+
213+
# Cleanup
214+
del os.environ['SSLCALISTLOC']
215+
del os.environ['REQUESTS_CA_BUNDLE']
216+
203217

204218
def test_verify_ssl():
205219

0 commit comments

Comments
 (0)