From e1b8b085a7924a96f59c694398cc632d726bc7ac Mon Sep 17 00:00:00 2001 From: Kavin Date: Thu, 1 Sep 2016 16:49:41 +0530 Subject: [PATCH 1/3] Store owner information on creating a note and added integration test cases for the relevant scenarios. --- .../apache/zeppelin/notebook/Notebook.java | 7 +++ .../zeppelin/notebook/NotebookTest.java | 44 ++++++++++++++++++- 2 files changed, 49 insertions(+), 2 deletions(-) diff --git a/zeppelin-zengine/src/main/java/org/apache/zeppelin/notebook/Notebook.java b/zeppelin-zengine/src/main/java/org/apache/zeppelin/notebook/Notebook.java index 9acb156a3a3..9c54f4c1c21 100644 --- a/zeppelin-zengine/src/main/java/org/apache/zeppelin/notebook/Notebook.java +++ b/zeppelin-zengine/src/main/java/org/apache/zeppelin/notebook/Notebook.java @@ -24,10 +24,12 @@ import java.util.Comparator; import java.util.Date; import java.util.HashMap; +import java.util.HashSet; import java.util.LinkedHashMap; import java.util.LinkedList; import java.util.List; import java.util.Map; +import java.util.Set; import java.util.concurrent.TimeUnit; import com.google.gson.Gson; @@ -158,6 +160,11 @@ public Note createNote(List interpreterIds, AuthenticationInfo subject) note.putDefaultReplName(); } + if (subject != null && !"anonymous".equals(subject.getUser())) { + Set owners = new HashSet(); + owners.add(subject.getUser()); + notebookAuthorization.setOwners(note.getId(), owners); + } notebookIndex.addIndexDoc(note); note.persist(subject); fireNoteCreateEvent(note); diff --git a/zeppelin-zengine/src/test/java/org/apache/zeppelin/notebook/NotebookTest.java b/zeppelin-zengine/src/test/java/org/apache/zeppelin/notebook/NotebookTest.java index 648062eda2e..e166932315e 100644 --- a/zeppelin-zengine/src/test/java/org/apache/zeppelin/notebook/NotebookTest.java +++ b/zeppelin-zengine/src/test/java/org/apache/zeppelin/notebook/NotebookTest.java @@ -42,6 +42,7 @@ import org.apache.zeppelin.scheduler.Job.Status; import org.apache.zeppelin.scheduler.SchedulerFactory; import org.apache.zeppelin.search.SearchService; +import org.apache.zeppelin.user.AuthenticationInfo; import org.apache.zeppelin.user.Credentials; import org.junit.After; import org.junit.Before; @@ -209,6 +210,21 @@ public void testPersist() throws IOException, SchedulerException, RepositoryExce assertEquals(1, notebook2.getAllNotes().size()); } + @Test + public void testCreateNoteWithSubject() throws IOException, SchedulerException, RepositoryException { + AuthenticationInfo subject = new AuthenticationInfo("user1"); + Note note = notebook.createNote(subject); + + Notebook notebook = new Notebook( + conf, notebookRepo, schedulerFactory, + new InterpreterFactory(conf, null, null, null, depResolver), this, null, notebookAuthorization, null); + assertNotNull(notebook.getNotebookAuthorization().getOwners(note.getId())); + assertEquals(1, notebook.getNotebookAuthorization().getOwners(note.getId()).size()); + Set owners = new HashSet<>(); + owners.add("user1"); + assertEquals(owners, notebook.getNotebookAuthorization().getOwners(note.getId())); + } + @Test public void testClearParagraphOutput() throws IOException, SchedulerException{ Note note = notebook.createNote(null); @@ -351,7 +367,7 @@ public void testAutoRestartInterpreterAfterSchedule() throws InterruptedExceptio @Test public void testExportAndImportNote() throws IOException, CloneNotSupportedException, - InterruptedException { + InterruptedException, InterpreterException, SchedulerException, RepositoryException { Note note = notebook.createNote(null); factory.setInterpreters(note.getId(), factory.getDefaultInterpreterSettingList()); @@ -374,11 +390,23 @@ public void testExportAndImportNote() throws IOException, CloneNotSupportedExcep assertEquals(p.getId(), p2.getId()); assertEquals(p.text, p2.text); assertEquals(p.getResult().message(), p2.getResult().message()); + + // Verify import note with subject + AuthenticationInfo subject = new AuthenticationInfo("user1"); + Note importedNote2 = notebook.importNote(exportedNoteJson, "Title2", subject); + Notebook notebook = new Notebook( + conf, notebookRepo, schedulerFactory, + new InterpreterFactory(conf, null, null, null, depResolver), this, null, notebookAuthorization, null); + assertNotNull(notebook.getNotebookAuthorization().getOwners(importedNote2.getId())); + assertEquals(1, notebook.getNotebookAuthorization().getOwners(importedNote2.getId()).size()); + Set owners = new HashSet<>(); + owners.add("user1"); + assertEquals(owners, notebook.getNotebookAuthorization().getOwners(importedNote2.getId())); } @Test public void testCloneNote() throws IOException, CloneNotSupportedException, - InterruptedException { + InterruptedException, InterpreterException, SchedulerException, RepositoryException { Note note = notebook.createNote(null); factory.setInterpreters(note.getId(), factory.getDefaultInterpreterSettingList()); @@ -396,6 +424,18 @@ public void testCloneNote() throws IOException, CloneNotSupportedException, assertEquals(cp.getId(), p.getId()); assertEquals(cp.text, p.text); assertEquals(cp.getResult().message(), p.getResult().message()); + + // Verify clone note with subject + AuthenticationInfo subject = new AuthenticationInfo("user1"); + Note cloneNote2 = notebook.cloneNote(note.getId(), "clone note2", subject); + Notebook notebook = new Notebook( + conf, notebookRepo, schedulerFactory, + new InterpreterFactory(conf, null, null, null, depResolver), this, null, notebookAuthorization, null); + assertNotNull(notebook.getNotebookAuthorization().getOwners(cloneNote2.getId())); + assertEquals(1, notebook.getNotebookAuthorization().getOwners(cloneNote2.getId()).size()); + Set owners = new HashSet<>(); + owners.add("user1"); + assertEquals(owners, notebook.getNotebookAuthorization().getOwners(cloneNote2.getId())); } @Test From 7642f633dd1482deb4df09ead55aed99c5d51ebd Mon Sep 17 00:00:00 2001 From: Kavin Date: Tue, 6 Sep 2016 14:50:14 +0530 Subject: [PATCH 2/3] Removed the duplicate instance of notebook variable and reused the existing one. --- .../java/org/apache/zeppelin/notebook/NotebookTest.java | 9 --------- 1 file changed, 9 deletions(-) diff --git a/zeppelin-zengine/src/test/java/org/apache/zeppelin/notebook/NotebookTest.java b/zeppelin-zengine/src/test/java/org/apache/zeppelin/notebook/NotebookTest.java index e166932315e..7718ec4535e 100644 --- a/zeppelin-zengine/src/test/java/org/apache/zeppelin/notebook/NotebookTest.java +++ b/zeppelin-zengine/src/test/java/org/apache/zeppelin/notebook/NotebookTest.java @@ -215,9 +215,6 @@ public void testCreateNoteWithSubject() throws IOException, SchedulerException, AuthenticationInfo subject = new AuthenticationInfo("user1"); Note note = notebook.createNote(subject); - Notebook notebook = new Notebook( - conf, notebookRepo, schedulerFactory, - new InterpreterFactory(conf, null, null, null, depResolver), this, null, notebookAuthorization, null); assertNotNull(notebook.getNotebookAuthorization().getOwners(note.getId())); assertEquals(1, notebook.getNotebookAuthorization().getOwners(note.getId()).size()); Set owners = new HashSet<>(); @@ -394,9 +391,6 @@ public void testExportAndImportNote() throws IOException, CloneNotSupportedExcep // Verify import note with subject AuthenticationInfo subject = new AuthenticationInfo("user1"); Note importedNote2 = notebook.importNote(exportedNoteJson, "Title2", subject); - Notebook notebook = new Notebook( - conf, notebookRepo, schedulerFactory, - new InterpreterFactory(conf, null, null, null, depResolver), this, null, notebookAuthorization, null); assertNotNull(notebook.getNotebookAuthorization().getOwners(importedNote2.getId())); assertEquals(1, notebook.getNotebookAuthorization().getOwners(importedNote2.getId()).size()); Set owners = new HashSet<>(); @@ -428,9 +422,6 @@ public void testCloneNote() throws IOException, CloneNotSupportedException, // Verify clone note with subject AuthenticationInfo subject = new AuthenticationInfo("user1"); Note cloneNote2 = notebook.cloneNote(note.getId(), "clone note2", subject); - Notebook notebook = new Notebook( - conf, notebookRepo, schedulerFactory, - new InterpreterFactory(conf, null, null, null, depResolver), this, null, notebookAuthorization, null); assertNotNull(notebook.getNotebookAuthorization().getOwners(cloneNote2.getId())); assertEquals(1, notebook.getNotebookAuthorization().getOwners(cloneNote2.getId()).size()); Set owners = new HashSet<>(); From 5a45c9e09dccd85cdf9926d31ce7a3c6d9cb6c4b Mon Sep 17 00:00:00 2001 From: Kavin Date: Wed, 7 Sep 2016 12:40:09 +0530 Subject: [PATCH 3/3] Ensure that the authentication instance is created only when the input prinicipal is not null. --- .../main/java/org/apache/zeppelin/socket/NotebookServer.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/zeppelin-server/src/main/java/org/apache/zeppelin/socket/NotebookServer.java b/zeppelin-server/src/main/java/org/apache/zeppelin/socket/NotebookServer.java index 19bfba10b54..4a90b929624 100644 --- a/zeppelin-server/src/main/java/org/apache/zeppelin/socket/NotebookServer.java +++ b/zeppelin-server/src/main/java/org/apache/zeppelin/socket/NotebookServer.java @@ -719,7 +719,10 @@ protected Note importNote(NotebookSocket conn, HashSet userAndRoles, if (fromMessage != null) { String noteName = (String) ((Map) fromMessage.get("notebook")).get("name"); String noteJson = gson.toJson(fromMessage.get("notebook")); - AuthenticationInfo subject = new AuthenticationInfo(fromMessage.principal); + AuthenticationInfo subject = null; + if (fromMessage.principal != null) { + subject = new AuthenticationInfo(fromMessage.principal); + } note = notebook.importNote(noteJson, noteName, subject); note.persist(subject); broadcastNote(note);