|
| 1 | +# Copyright (c) Jupyter Development Team. |
| 2 | +# Distributed under the terms of the Modified BSD License. |
| 3 | + |
| 4 | +from jupyter_core.paths import jupyter_data_dir |
| 5 | +import subprocess |
| 6 | +import os |
| 7 | +import errno |
| 8 | +import stat |
| 9 | + |
| 10 | +c = get_config() # noqa: F821 |
| 11 | +c.NotebookApp.ip = '0.0.0.0' |
| 12 | +c.NotebookApp.port = 8888 |
| 13 | +c.NotebookApp.open_browser = False |
| 14 | + |
| 15 | +# https://github.com/jupyter/notebook/issues/3130 |
| 16 | +c.FileContentsManager.delete_to_trash = False |
| 17 | + |
| 18 | +# Generate a self-signed certificate |
| 19 | +if 'GEN_CERT' in os.environ: |
| 20 | + dir_name = jupyter_data_dir() |
| 21 | + pem_file = os.path.join(dir_name, 'notebook.pem') |
| 22 | + try: |
| 23 | + os.makedirs(dir_name) |
| 24 | + except OSError as exc: # Python >2.5 |
| 25 | + if exc.errno == errno.EEXIST and os.path.isdir(dir_name): |
| 26 | + pass |
| 27 | + else: |
| 28 | + raise |
| 29 | + |
| 30 | + # Generate an openssl.cnf file to set the distinguished name |
| 31 | + cnf_file = os.path.join(os.getenv('CONDA_DIR', '/usr/lib'), 'ssl', 'openssl.cnf') |
| 32 | + if not os.path.isfile(cnf_file): |
| 33 | + with open(cnf_file, 'w') as fh: |
| 34 | + fh.write('''\ |
| 35 | +[req] |
| 36 | +distinguished_name = req_distinguished_name |
| 37 | +[req_distinguished_name] |
| 38 | +''') |
| 39 | + |
| 40 | + # Generate a certificate if one doesn't exist on disk |
| 41 | + subprocess.check_call(['openssl', 'req', '-new', |
| 42 | + '-newkey', 'rsa:2048', |
| 43 | + '-days', '365', |
| 44 | + '-nodes', '-x509', |
| 45 | + '-subj', '/C=XX/ST=XX/L=XX/O=generated/CN=generated', |
| 46 | + '-keyout', pem_file, |
| 47 | + '-out', pem_file]) |
| 48 | + # Restrict access to the file |
| 49 | + os.chmod(pem_file, stat.S_IRUSR | stat.S_IWUSR) |
| 50 | + c.NotebookApp.certfile = pem_file |
| 51 | + |
| 52 | +# Change default umask for all subprocesses of the notebook server if set in |
| 53 | +# the environment |
| 54 | +if 'NB_UMASK' in os.environ: |
| 55 | + os.umask(int(os.environ['NB_UMASK'], 8)) |
0 commit comments