Search in sources :

Example 56 with AuthenticationInfo

use of org.apache.zeppelin.user.AuthenticationInfo in project zeppelin by apache.

the class NotebookTest method testNotebookEventListener.

public void testNotebookEventListener() throws IOException {
    final AtomicInteger onNoteRemove = new AtomicInteger(0);
    final AtomicInteger onNoteCreate = new AtomicInteger(0);
    final AtomicInteger onParagraphRemove = new AtomicInteger(0);
    final AtomicInteger onParagraphCreate = new AtomicInteger(0);
    notebook.addNotebookEventListener(new NoteEventListener() {

        @Override
        public void onNoteRemove(Note note, AuthenticationInfo subject) {
            onNoteRemove.incrementAndGet();
        }

        @Override
        public void onNoteCreate(Note note, AuthenticationInfo subject) {
            onNoteCreate.incrementAndGet();
        }

        @Override
        public void onNoteUpdate(Note note, AuthenticationInfo subject) {
        }

        @Override
        public void onParagraphRemove(Paragraph p) {
            onParagraphRemove.incrementAndGet();
        }

        @Override
        public void onParagraphCreate(Paragraph p) {
            onParagraphCreate.incrementAndGet();
        }

        @Override
        public void onParagraphUpdate(Paragraph p) {
        }

        @Override
        public void onParagraphStatusChange(Paragraph p, Status status) {
        }
    });
    String note1Id = notebook.createNote("note1", anonymous);
    assertEquals(1, onNoteCreate.get());
    notebook.processNote(note1Id, note1 -> {
        Paragraph p1 = note1.addNewParagraph(AuthenticationInfo.ANONYMOUS);
        assertEquals(1, onParagraphCreate.get());
        note1.addCloneParagraph(p1, AuthenticationInfo.ANONYMOUS);
        assertEquals(2, onParagraphCreate.get());
        note1.removeParagraph(anonymous.getUser(), p1.getId());
        assertEquals(1, onParagraphRemove.get());
        return null;
    });
    notebook.removeNote(note1Id, anonymous);
    assertEquals(1, onNoteRemove.get());
    assertEquals(1, onParagraphRemove.get());
}
Also used : Status(org.apache.zeppelin.scheduler.Job.Status) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AuthenticationInfo(org.apache.zeppelin.user.AuthenticationInfo)

Example 57 with AuthenticationInfo

use of org.apache.zeppelin.user.AuthenticationInfo in project zeppelin by apache.

the class NotebookRepoSyncTest method setUp.

@Before
public void setUp() throws Exception {
    System.setProperty("zeppelin.isTest", "true");
    ZEPPELIN_HOME = Files.createTempDir();
    new File(ZEPPELIN_HOME, "conf").mkdirs();
    String mainNotePath = ZEPPELIN_HOME.getAbsolutePath() + "/notebook";
    String secNotePath = ZEPPELIN_HOME.getAbsolutePath() + "/notebook_secondary";
    mainNotebookDir = new File(mainNotePath);
    secNotebookDir = new File(secNotePath);
    mainNotebookDir.mkdirs();
    secNotebookDir.mkdirs();
    System.setProperty(ConfVars.ZEPPELIN_HOME.getVarName(), ZEPPELIN_HOME.getAbsolutePath());
    System.setProperty(ConfVars.ZEPPELIN_NOTEBOOK_DIR.getVarName(), mainNotebookDir.getAbsolutePath());
    System.setProperty(ConfVars.ZEPPELIN_NOTEBOOK_STORAGE.getVarName(), "org.apache.zeppelin.notebook.repo.VFSNotebookRepo,org.apache.zeppelin.notebook.repo.mock.VFSNotebookRepoMock");
    System.setProperty(ConfVars.ZEPPELIN_NOTEBOOK_ONE_WAY_SYNC.getVarName(), "false");
    System.setProperty(ConfVars.ZEPPELIN_CONFIG_FS_DIR.getVarName(), ZEPPELIN_HOME.getAbsolutePath() + "/conf");
    System.setProperty(ConfVars.ZEPPELIN_PLUGINS_DIR.getVarName(), new File("../../../plugins").getAbsolutePath());
    LOG.info("main Note dir : " + mainNotePath);
    LOG.info("secondary note dir : " + secNotePath);
    conf = ZeppelinConfiguration.create();
    ConfigStorage.reset();
    interpreterSettingManager = new InterpreterSettingManager(conf, mock(AngularObjectRegistryListener.class), mock(RemoteInterpreterProcessListener.class), mock(ApplicationEventListener.class));
    factory = new InterpreterFactory(interpreterSettingManager);
    notebookRepoSync = new NotebookRepoSync(conf);
    noteManager = new NoteManager(notebookRepoSync, conf);
    authorizationService = new AuthorizationService(noteManager, conf);
    credentials = new Credentials(conf);
    notebook = new Notebook(conf, authorizationService, notebookRepoSync, noteManager, factory, interpreterSettingManager, credentials, null);
    anonymous = new AuthenticationInfo("anonymous");
}
Also used : Notebook(org.apache.zeppelin.notebook.Notebook) AuthorizationService(org.apache.zeppelin.notebook.AuthorizationService) NoteManager(org.apache.zeppelin.notebook.NoteManager) InterpreterSettingManager(org.apache.zeppelin.interpreter.InterpreterSettingManager) File(java.io.File) InterpreterFactory(org.apache.zeppelin.interpreter.InterpreterFactory) Credentials(org.apache.zeppelin.user.Credentials) AuthenticationInfo(org.apache.zeppelin.user.AuthenticationInfo) Before(org.junit.Before)

Example 58 with AuthenticationInfo

use of org.apache.zeppelin.user.AuthenticationInfo in project zeppelin by apache.

the class NotebookRepoSyncTest method testSyncWithAcl.

@Test
public void testSyncWithAcl() throws IOException {
    /* scenario 1 - note exists with acl on main storage */
    AuthenticationInfo user1 = new AuthenticationInfo("user1");
    String noteId = notebook.createNote("/test", "test", user1);
    notebook.processNote(noteId, note -> {
        assertEquals(0, note.getParagraphs().size());
        return null;
    });
    // saved on both storages
    assertEquals(1, notebookRepoSync.list(0, null).size());
    assertEquals(1, notebookRepoSync.list(1, null).size());
    /* check that user1 is the only owner */
    Set<String> entity = new HashSet<String>();
    entity.add(user1.getUser());
    assertEquals(true, authorizationService.isOwner(noteId, entity));
    assertEquals(1, authorizationService.getOwners(noteId).size());
    assertEquals(0, authorizationService.getReaders(noteId).size());
    assertEquals(0, authorizationService.getRunners(noteId).size());
    assertEquals(0, authorizationService.getWriters(noteId).size());
    /* update note and save on secondary storage */
    notebook.processNote(noteId, note -> {
        note.setInterpreterFactory(mock(InterpreterFactory.class));
        Paragraph p1 = note.addNewParagraph(AuthenticationInfo.ANONYMOUS);
        p1.setText("hello world");
        assertEquals(1, note.getParagraphs().size());
        notebookRepoSync.save(1, note, null);
        return null;
    });
    /* check paragraph isn't saved into first storage */
    assertEquals(0, notebookRepoSync.get(0, notebookRepoSync.list(0, null).get(0).getId(), notebookRepoSync.list(0, null).get(0).getPath(), null).getParagraphs().size());
    /* check paragraph is saved into second storage */
    assertEquals(1, notebookRepoSync.get(1, notebookRepoSync.list(1, null).get(0).getId(), notebookRepoSync.list(1, null).get(0).getPath(), null).getParagraphs().size());
    /* now sync by user1 */
    notebookRepoSync.sync(user1);
    /* check that note updated and acl are same on main storage*/
    assertEquals(1, notebookRepoSync.get(0, notebookRepoSync.list(0, null).get(0).getId(), notebookRepoSync.list(0, null).get(0).getPath(), null).getParagraphs().size());
    assertEquals(true, authorizationService.isOwner(noteId, entity));
    assertEquals(1, authorizationService.getOwners(noteId).size());
    assertEquals(0, authorizationService.getReaders(noteId).size());
    assertEquals(0, authorizationService.getRunners(noteId).size());
    assertEquals(0, authorizationService.getWriters(noteId).size());
    /* scenario 2 - note doesn't exist on main storage */
    /* remove from main storage */
    notebook.processNote(noteId, note -> {
        notebookRepoSync.remove(0, noteId, note.getPath(), user1);
        return null;
    });
    assertEquals(0, notebookRepoSync.list(0, null).size());
    assertEquals(1, notebookRepoSync.list(1, null).size());
    /* now sync - should bring note from secondary storage with added acl */
    notebookRepoSync.sync(user1);
    assertEquals(1, notebookRepoSync.list(0, null).size());
    assertEquals(1, notebookRepoSync.list(1, null).size());
    assertEquals(1, authorizationService.getOwners(noteId).size());
    assertEquals(0, authorizationService.getReaders(noteId).size());
    assertEquals(0, authorizationService.getRunners(noteId).size());
    assertEquals(0, authorizationService.getWriters(noteId).size());
}
Also used : InterpreterFactory(org.apache.zeppelin.interpreter.InterpreterFactory) AuthenticationInfo(org.apache.zeppelin.user.AuthenticationInfo) HashSet(java.util.HashSet) Paragraph(org.apache.zeppelin.notebook.Paragraph) Test(org.junit.Test)

Example 59 with AuthenticationInfo

use of org.apache.zeppelin.user.AuthenticationInfo in project zeppelin by apache.

the class NotebookTest method testCloneNote.

@Test
public void testCloneNote() throws Exception {
    String noteId = notebook.createNote("note1", anonymous);
    notebook.processNote(noteId, note -> {
        final Paragraph p = note.addNewParagraph(AuthenticationInfo.ANONYMOUS);
        p.setText("hello world");
        try {
            note.runAll(anonymous, true, false, new HashMap<>());
        } catch (Exception e) {
            fail();
        }
        p.setStatus(Status.RUNNING);
        return null;
    });
    String cloneNoteId = notebook.cloneNote(noteId, "clone note", anonymous);
    notebook.processNote(noteId, note -> {
        Paragraph p = note.getParagraph(0);
        notebook.processNote(cloneNoteId, cloneNote -> {
            Paragraph cp = cloneNote.getParagraph(0);
            assertEquals(Status.READY, cp.getStatus());
            // Keep same ParagraphId
            assertEquals(cp.getId(), p.getId());
            assertEquals(cp.getText(), p.getText());
            assertEquals(cp.getReturn().message().get(0).getData(), p.getReturn().message().get(0).getData());
            return null;
        });
        return null;
    });
    // Verify clone note with subject
    AuthenticationInfo subject = new AuthenticationInfo("user1");
    String cloneNote2Id = notebook.cloneNote(noteId, "clone note2", subject);
    assertNotNull(authorizationService.getOwners(cloneNote2Id));
    assertEquals(1, authorizationService.getOwners(cloneNote2Id).size());
    Set<String> owners = new HashSet<>();
    owners.add("user1");
    assertEquals(owners, authorizationService.getOwners(cloneNote2Id));
    notebook.removeNote(noteId, anonymous);
    notebook.removeNote(cloneNoteId, anonymous);
    notebook.removeNote(cloneNote2Id, anonymous);
}
Also used : InterpreterException(org.apache.zeppelin.interpreter.InterpreterException) InterpreterNotFoundException(org.apache.zeppelin.interpreter.InterpreterNotFoundException) SchedulerException(org.quartz.SchedulerException) IOException(java.io.IOException) RepositoryException(org.eclipse.aether.RepositoryException) AuthenticationInfo(org.apache.zeppelin.user.AuthenticationInfo) HashSet(java.util.HashSet) AbstractInterpreterTest(org.apache.zeppelin.interpreter.AbstractInterpreterTest) Test(org.junit.Test)

Example 60 with AuthenticationInfo

use of org.apache.zeppelin.user.AuthenticationInfo in project zeppelin by apache.

the class Notebook method loadNoteFromRepo.

@SuppressWarnings("rawtypes")
public Note loadNoteFromRepo(String id, AuthenticationInfo subject) {
    Note note = null;
    try {
        // note can be safely returned here, because it's just broadcasted in json later on
        note = processNote(id, n -> n);
    } catch (IOException e) {
        LOGGER.error("Fail to get note: {}", id, e);
        return null;
    }
    if (note == null) {
        return null;
    }
    // Manually inject ALL dependencies, as DI constructor was NOT used
    note.setCredentials(this.credentials);
    note.setInterpreterFactory(replFactory);
    note.setInterpreterSettingManager(interpreterSettingManager);
    note.setParagraphJobListener(this.paragraphJobListener);
    note.setCronSupported(getConf());
    if (note.getDefaultInterpreterGroup() == null) {
        note.setDefaultInterpreterGroup(conf.getString(ConfVars.ZEPPELIN_INTERPRETER_GROUP_DEFAULT));
    }
    Map<String, SnapshotAngularObject> angularObjectSnapshot = new HashMap<>();
    // restore angular object --------------
    Date lastUpdatedDate = new Date(0);
    for (Paragraph p : note.getParagraphs()) {
        p.setNote(note);
        if (p.getDateFinished() != null && lastUpdatedDate.before(p.getDateFinished())) {
            lastUpdatedDate = p.getDateFinished();
        }
    }
    Map<String, List<AngularObject>> savedObjects = note.getAngularObjects();
    if (savedObjects != null) {
        for (Entry<String, List<AngularObject>> intpGroupNameEntry : savedObjects.entrySet()) {
            String intpGroupName = intpGroupNameEntry.getKey();
            List<AngularObject> objectList = intpGroupNameEntry.getValue();
            for (AngularObject object : objectList) {
                SnapshotAngularObject snapshot = angularObjectSnapshot.get(object.getName());
                if (snapshot == null || snapshot.getLastUpdate().before(lastUpdatedDate)) {
                    angularObjectSnapshot.put(object.getName(), new SnapshotAngularObject(intpGroupName, object, lastUpdatedDate));
                }
            }
        }
    }
    note.setNoteEventListeners(this.noteEventListeners);
    for (Entry<String, SnapshotAngularObject> angularObjectSnapshotEntry : angularObjectSnapshot.entrySet()) {
        String name = angularObjectSnapshotEntry.getKey();
        SnapshotAngularObject snapshot = angularObjectSnapshotEntry.getValue();
        List<InterpreterSetting> settings = interpreterSettingManager.get();
        for (InterpreterSetting setting : settings) {
            InterpreterGroup intpGroup = setting.getInterpreterGroup(note.getExecutionContext());
            if (intpGroup != null && intpGroup.getId().equals(snapshot.getIntpGroupId())) {
                AngularObjectRegistry registry = intpGroup.getAngularObjectRegistry();
                String noteId = snapshot.getAngularObject().getNoteId();
                String paragraphId = snapshot.getAngularObject().getParagraphId();
                // at this point, remote interpreter process is not created.
                // so does not make sense add it to the remote.
                // 
                // therefore instead of addAndNotifyRemoteProcess(), need to use add()
                // that results add angularObject only in ZeppelinServer side not remoteProcessSide
                registry.add(name, snapshot.getAngularObject().get(), noteId, paragraphId);
            }
        }
    }
    return note;
}
Also used : ZeppelinConfiguration(org.apache.zeppelin.conf.ZeppelinConfiguration) AngularObjectRegistry(org.apache.zeppelin.display.AngularObjectRegistry) NoteNode(org.apache.zeppelin.notebook.NoteManager.NoteNode) Date(java.util.Date) NotebookRepoSync(org.apache.zeppelin.notebook.repo.NotebookRepoSync) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) AuthenticationInfo(org.apache.zeppelin.user.AuthenticationInfo) Function(java.util.function.Function) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Inject(javax.inject.Inject) NotebookRepo(org.apache.zeppelin.notebook.repo.NotebookRepo) SchedulerException(org.quartz.SchedulerException) Map(java.util.Map) ConfVars(org.apache.zeppelin.conf.ZeppelinConfiguration.ConfVars) Folder(org.apache.zeppelin.notebook.NoteManager.Folder) Job(org.apache.zeppelin.scheduler.Job) InterpreterSetting(org.apache.zeppelin.interpreter.InterpreterSetting) NotebookRepoWithVersionControl(org.apache.zeppelin.notebook.repo.NotebookRepoWithVersionControl) InterpreterFactory(org.apache.zeppelin.interpreter.InterpreterFactory) LinkedList(java.util.LinkedList) AngularObject(org.apache.zeppelin.display.AngularObject) Revision(org.apache.zeppelin.notebook.repo.NotebookRepoWithVersionControl.Revision) Interpreter(org.apache.zeppelin.interpreter.Interpreter) InterpreterGroup(org.apache.zeppelin.interpreter.InterpreterGroup) InterpreterSettingManager(org.apache.zeppelin.interpreter.InterpreterSettingManager) Logger(org.slf4j.Logger) Set(java.util.Set) IOException(java.io.IOException) Credentials(org.apache.zeppelin.user.Credentials) ManagedInterpreterGroup(org.apache.zeppelin.interpreter.ManagedInterpreterGroup) Collectors(java.util.stream.Collectors) List(java.util.List) InterpreterNotFoundException(org.apache.zeppelin.interpreter.InterpreterNotFoundException) Entry(java.util.Map.Entry) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) HashMap(java.util.HashMap) InterpreterSetting(org.apache.zeppelin.interpreter.InterpreterSetting) AngularObject(org.apache.zeppelin.display.AngularObject) IOException(java.io.IOException) Date(java.util.Date) InterpreterGroup(org.apache.zeppelin.interpreter.InterpreterGroup) ManagedInterpreterGroup(org.apache.zeppelin.interpreter.ManagedInterpreterGroup) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) AngularObjectRegistry(org.apache.zeppelin.display.AngularObjectRegistry)

Aggregations

AuthenticationInfo (org.apache.zeppelin.user.AuthenticationInfo)128 Test (org.junit.Test)44 HashMap (java.util.HashMap)40 AngularObjectRegistry (org.apache.zeppelin.display.AngularObjectRegistry)29 Properties (java.util.Properties)28 Note (org.apache.zeppelin.notebook.Note)27 LocalResourcePool (org.apache.zeppelin.resource.LocalResourcePool)23 LinkedList (java.util.LinkedList)22 GUI (org.apache.zeppelin.display.GUI)22 Map (java.util.Map)21 ZeppelinApi (org.apache.zeppelin.annotation.ZeppelinApi)20 AngularObject (org.apache.zeppelin.display.AngularObject)19 InterpreterResultMessage (org.apache.zeppelin.interpreter.InterpreterResultMessage)19 IOException (java.io.IOException)18 InterpreterContext (org.apache.zeppelin.interpreter.InterpreterContext)18 InterpreterResult (org.apache.zeppelin.interpreter.InterpreterResult)18 Paragraph (org.apache.zeppelin.notebook.Paragraph)18 InterpreterOutput (org.apache.zeppelin.interpreter.InterpreterOutput)16 Path (javax.ws.rs.Path)15 HashSet (java.util.HashSet)13