|  | 
| 9 | 9 | import sys | 
| 10 | 10 | import signal | 
| 11 | 11 | import socket | 
|  | 12 | +import ssl | 
| 12 | 13 | from distutils.util import strtobool | 
| 13 | 14 | 
 | 
| 14 | 15 | import nbformat | 
|  | 
| 52 | 53 |     'seed_uri': 'KernelGatewayApp.seed_uri', | 
| 53 | 54 |     'keyfile': 'KernelGatewayApp.keyfile', | 
| 54 | 55 |     'certfile': 'KernelGatewayApp.certfile', | 
| 55 |  | -    'client-ca': 'KernelGatewayApp.client_ca' | 
|  | 56 | +    'client-ca': 'KernelGatewayApp.client_ca', | 
|  | 57 | +    'ssl_version': 'KernelGatewayApp.ssl_version' | 
| 56 | 58 | }) | 
| 57 | 59 | 
 | 
| 58 | 60 | 
 | 
| @@ -298,6 +300,16 @@ def keyfile_default(self): | 
| 298 | 300 |     def client_ca_default(self): | 
| 299 | 301 |         return os.getenv(self.client_ca_env) | 
| 300 | 302 | 
 | 
|  | 303 | +    ssl_version_env = 'KG_SSL_VERSION' | 
|  | 304 | +    ssl_version_default_value = ssl.PROTOCOL_TLSv1_2 | 
|  | 305 | +    ssl_version = Integer(None, config=True, allow_none=True, | 
|  | 306 | +                        help="""Sets the SSL version to use for the web socket connection. (KG_SSL_VERSION env var)""") | 
|  | 307 | +     | 
|  | 308 | +    @default('ssl_version') | 
|  | 309 | +    def ssl_version_default(self): | 
|  | 310 | +        ssl_from_env = os.getenv(self.ssl_version_env) | 
|  | 311 | +        return ssl_from_env if ssl_from_env is None else int(ssl_from_env) | 
|  | 312 | + | 
| 301 | 313 |     kernel_spec_manager = Instance(KernelSpecManager, allow_none=True) | 
| 302 | 314 | 
 | 
| 303 | 315 |     kernel_spec_manager_class = Type( | 
| @@ -494,14 +506,13 @@ def _build_ssl_options(self): | 
| 494 | 506 |             ssl_options['keyfile'] = self.keyfile | 
| 495 | 507 |         if self.client_ca: | 
| 496 | 508 |             ssl_options['ca_certs'] = self.client_ca | 
|  | 509 | +        if self.ssl_version: | 
|  | 510 | +            ssl_options['ssl_version'] = self.ssl_version | 
| 497 | 511 |         if not ssl_options: | 
| 498 | 512 |             # None indicates no SSL config | 
| 499 | 513 |             ssl_options = None | 
| 500 | 514 |         else: | 
| 501 |  | -            # SSL may be missing, so only import it if it's to be used | 
| 502 |  | -            import ssl | 
| 503 |  | -            # Disable SSLv3 by default, since its use is discouraged. | 
| 504 |  | -            ssl_options.setdefault('ssl_version', ssl.PROTOCOL_TLSv1) | 
|  | 515 | +            ssl_options.setdefault('ssl_version', self.ssl_version_default_value) | 
| 505 | 516 |             if ssl_options.get('ca_certs', False): | 
| 506 | 517 |                 ssl_options.setdefault('cert_reqs', ssl.CERT_REQUIRED) | 
| 507 | 518 | 
 | 
|  | 
0 commit comments