From eb208b2d77b7299438898cf067094d9336d0d650 Mon Sep 17 00:00:00 2001 From: MinRK Date: Mon, 27 Jan 2014 11:26:14 -0800 Subject: [PATCH] move signature checking to base NotebookManager so that subclasses have less to duplicate --- .../html/services/notebooks/filenbmanager.py | 13 +++--------- IPython/html/services/notebooks/nbmanager.py | 20 +++++++++++++++++++ 2 files changed, 23 insertions(+), 10 deletions(-) diff --git a/IPython/html/services/notebooks/filenbmanager.py b/IPython/html/services/notebooks/filenbmanager.py index 8bcee5759..bac4de245 100644 --- a/IPython/html/services/notebooks/filenbmanager.py +++ b/IPython/html/services/notebooks/filenbmanager.py @@ -26,7 +26,7 @@ import shutil from tornado import web from .nbmanager import NotebookManager -from IPython.nbformat import current, sign +from IPython.nbformat import current from IPython.utils.traitlets import Unicode, Dict, Bool, TraitError from IPython.utils import tz @@ -213,11 +213,8 @@ class FileNotebookManager(NotebookManager): nb = current.read(f, u'json') except Exception as e: raise web.HTTPError(400, u"Unreadable Notebook: %s %s" % (os_path, e)) + self.mark_trusted_cells(nb, path, name) model['content'] = nb - trusted = self.notary.check_signature(nb) - if not trusted: - self.log.warn("Notebook %s/%s is not trusted", model['path'], model['name']) - self.notary.mark_cells(nb, trusted) return model def save_notebook_model(self, model, name='', path=''): @@ -241,11 +238,7 @@ class FileNotebookManager(NotebookManager): os_path = self.get_os_path(new_name, new_path) nb = current.to_notebook_json(model['content']) - if self.notary.check_cells(nb): - self.notary.sign(nb) - else: - self.log.warn("Saving untrusted notebook %s/%s", new_path, new_name) - + self.check_and_sign(nb, new_path, new_name) if 'name' in nb['metadata']: nb['metadata']['name'] = u'' diff --git a/IPython/html/services/notebooks/nbmanager.py b/IPython/html/services/notebooks/nbmanager.py index 43f9b917c..f86e914e3 100644 --- a/IPython/html/services/notebooks/nbmanager.py +++ b/IPython/html/services/notebooks/nbmanager.py @@ -46,6 +46,26 @@ class NotebookManager(LoggingConfigurable): def _notary_default(self): return sign.NotebookNotary(parent=self) + def check_and_sign(self, nb, path, name): + """Check for trusted cells, and sign the notebook. + + Called as a part of saving notebooks. + """ + if self.notary.check_cells(nb): + self.notary.sign(nb) + else: + self.log.warn("Saving untrusted notebook %s/%s", path, name) + + def mark_trusted_cells(self, nb, path, name): + """Mark cells as trusted if the notebook signature matches. + + Called as a part of loading notebooks. + """ + trusted = self.notary.check_signature(nb) + if not trusted: + self.log.warn("Notebook %s/%s is not trusted", path, name) + self.notary.mark_cells(nb, trusted) + def path_exists(self, path): """Does the API-style path (directory) actually exist?