Skip to content

Commit 403ca7e

Browse files
authored
[2.7] bpo-38338, test.pythoninfo: add more ssl infos (GH-16543)
test.pythoninfo now logs environment variables used by OpenSSL and Python ssl modules, and logs attributes of 3 SSL contexts (SSLContext, default HTTPS context, stdlib context). (cherry picked from commit 1df1c2f)
1 parent 8eb6415 commit 403ca7e

1 file changed

Lines changed: 60 additions & 0 deletions

File tree

Lib/test/pythoninfo.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,10 +439,15 @@ def collect_sysconfig(info_add):
439439

440440

441441
def collect_ssl(info_add):
442+
import os
442443
try:
443444
import ssl
444445
except ImportError:
445446
return
447+
try:
448+
import _ssl
449+
except ImportError:
450+
_ssl = None
446451

447452
def format_attr(attr, value):
448453
if attr.startswith('OP_'):
@@ -459,6 +464,61 @@ def format_attr(attr, value):
459464
)
460465
copy_attributes(info_add, ssl, 'ssl.%s', attributes, formatter=format_attr)
461466

467+
options_names = []
468+
protocol_names = {}
469+
verify_modes = {}
470+
for name in dir(ssl):
471+
if name.startswith('OP_'):
472+
options_names.append((name, getattr(ssl, name)))
473+
elif name.startswith('PROTOCOL_'):
474+
protocol_names[getattr(ssl, name)] = name
475+
elif name.startswith('CERT_'):
476+
verify_modes[getattr(ssl, name)] = name
477+
options_names.sort(key=lambda item: item[1], reverse=True)
478+
479+
def formatter(attr_name, value):
480+
if attr_name == 'options':
481+
options_text = []
482+
for opt_name, opt_value in options_names:
483+
if value & opt_value:
484+
options_text.append(opt_name)
485+
value &= ~opt_value
486+
if value:
487+
options_text.append(str(value))
488+
return '|' .join(options_text)
489+
elif attr_name == 'verify_mode':
490+
return verify_modes.get(value, value)
491+
elif attr_name == 'protocol':
492+
return protocol_names.get(value, value)
493+
else:
494+
return value
495+
496+
for name, ctx in (
497+
('SSLContext(PROTOCOL_TLS)', ssl.SSLContext(ssl.PROTOCOL_TLS)),
498+
('default_https_context', ssl._create_default_https_context()),
499+
('stdlib_context', ssl._create_stdlib_context()),
500+
):
501+
attributes = (
502+
'minimum_version',
503+
'maximum_version',
504+
'protocol',
505+
'options',
506+
'verify_mode',
507+
)
508+
copy_attributes(info_add, ctx, 'ssl.%s.%%s' % name, attributes, formatter=formatter)
509+
510+
env_names = ["OPENSSL_CONF", "SSLKEYLOGFILE"]
511+
if _ssl is not None and hasattr(_ssl, 'get_default_verify_paths'):
512+
parts = _ssl.get_default_verify_paths()
513+
env_names.extend((parts[0], parts[2]))
514+
515+
for name in env_names:
516+
try:
517+
value = os.environ[name]
518+
except KeyError:
519+
continue
520+
info_add('ssl.environ[%s]' % name, value)
521+
462522

463523
def collect_socket(info_add):
464524
import socket

0 commit comments

Comments
 (0)