Search in sources :

Example 51 with AuthenticationInfo

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

the class ParagraphTest method credentialReplacement.

// (TODO zjffdu) temporary disable it.
// https://github.com/apache/zeppelin/pull/3416
@Ignore
@Test
public void credentialReplacement() throws Throwable {
    Note mockNote = mock(Note.class);
    Credentials creds = mock(Credentials.class);
    when(mockNote.getCredentials()).thenReturn(creds);
    Paragraph spyParagraph = spy(new Paragraph("para_1", mockNote, null));
    UserCredentials uc = mock(UserCredentials.class);
    when(creds.getUserCredentials(anyString())).thenReturn(uc);
    UsernamePassword up = new UsernamePassword("user", "pwd");
    when(uc.getUsernamePassword("ent")).thenReturn(up);
    Interpreter mockInterpreter = mock(Interpreter.class);
    spyParagraph.setInterpreter(mockInterpreter);
    doReturn(mockInterpreter).when(spyParagraph).getBindedInterpreter();
    ManagedInterpreterGroup mockInterpreterGroup = mock(ManagedInterpreterGroup.class);
    when(mockInterpreter.getInterpreterGroup()).thenReturn(mockInterpreterGroup);
    when(mockInterpreterGroup.getId()).thenReturn("mock_id_1");
    when(mockInterpreterGroup.getAngularObjectRegistry()).thenReturn(mock(AngularObjectRegistry.class));
    when(mockInterpreterGroup.getResourcePool()).thenReturn(mock(ResourcePool.class));
    when(mockInterpreter.getFormType()).thenReturn(FormType.NONE);
    ParagraphJobListener mockJobListener = mock(ParagraphJobListener.class);
    doReturn(mockJobListener).when(spyParagraph).getListener();
    InterpreterResult mockInterpreterResult = mock(InterpreterResult.class);
    when(mockInterpreter.interpret(anyString(), Mockito.<InterpreterContext>any())).thenReturn(mockInterpreterResult);
    when(mockInterpreterResult.code()).thenReturn(Code.SUCCESS);
    AuthenticationInfo user1 = new AuthenticationInfo("user1");
    spyParagraph.setAuthenticationInfo(user1);
    spyParagraph.setText("val x = \"usr={user.ent}&pass={password.ent}\"");
    // Credentials should only be injected when it is enabled for an interpreter or when specified in a local property
    when(mockInterpreter.getProperty(Constants.INJECT_CREDENTIALS, "false")).thenReturn("false");
    spyParagraph.jobRun();
    verify(mockInterpreter).interpret(eq("val x = \"usr={user.ent}&pass={password.ent}\""), any(InterpreterContext.class));
    when(mockInterpreter.getProperty(Constants.INJECT_CREDENTIALS, "false")).thenReturn("true");
    mockInterpreter.setProperty(Constants.INJECT_CREDENTIALS, "true");
    spyParagraph.jobRun();
    verify(mockInterpreter).interpret(eq("val x = \"usr=user&pass=pwd\""), any(InterpreterContext.class));
    // Check if local property override works
    when(mockInterpreter.getProperty(Constants.INJECT_CREDENTIALS, "false")).thenReturn("true");
    spyParagraph.getLocalProperties().put(Constants.INJECT_CREDENTIALS, "true");
    spyParagraph.jobRun();
    verify(mockInterpreter).interpret(eq("val x = \"usr=user&pass=pwd\""), any(InterpreterContext.class));
}
Also used : Interpreter(org.apache.zeppelin.interpreter.Interpreter) InterpreterResult(org.apache.zeppelin.interpreter.InterpreterResult) ResourcePool(org.apache.zeppelin.resource.ResourcePool) AuthenticationInfo(org.apache.zeppelin.user.AuthenticationInfo) UsernamePassword(org.apache.zeppelin.user.UsernamePassword) ManagedInterpreterGroup(org.apache.zeppelin.interpreter.ManagedInterpreterGroup) UserCredentials(org.apache.zeppelin.user.UserCredentials) InterpreterContext(org.apache.zeppelin.interpreter.InterpreterContext) UserCredentials(org.apache.zeppelin.user.UserCredentials) Credentials(org.apache.zeppelin.user.Credentials) AngularObjectRegistry(org.apache.zeppelin.display.AngularObjectRegistry) Ignore(org.junit.Ignore) AbstractInterpreterTest(org.apache.zeppelin.interpreter.AbstractInterpreterTest) Test(org.junit.Test)

Example 52 with AuthenticationInfo

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

the class NotebookTest method testAuthorizationRoles.

@Test
public void testAuthorizationRoles() throws IOException {
    String user1 = "user1";
    String user2 = "user2";
    Set<String> roles = new HashSet<>(Arrays.asList("admin"));
    // set admin roles for both user1 and user2
    authorizationService.setRoles(user1, roles);
    authorizationService.setRoles(user2, roles);
    String noteId = notebook.createNote("note1", new AuthenticationInfo(user1));
    // check that user1 is owner, reader, runner and writer
    assertTrue(authorizationService.isOwner(noteId, new HashSet<>(Arrays.asList(user1))));
    assertTrue(authorizationService.isReader(noteId, new HashSet<>(Arrays.asList(user1))));
    assertTrue(authorizationService.isRunner(noteId, new HashSet<>(Arrays.asList(user2))));
    assertTrue(authorizationService.isWriter(noteId, new HashSet<>(Arrays.asList(user1))));
    // since user1 and user2 both have admin role, user2 will be reader and writer as well
    assertFalse(authorizationService.isOwner(noteId, new HashSet<>(Arrays.asList(user2))));
    assertTrue(authorizationService.isReader(noteId, new HashSet<>(Arrays.asList(user2))));
    assertTrue(authorizationService.isRunner(noteId, new HashSet<>(Arrays.asList(user2))));
    assertTrue(authorizationService.isWriter(noteId, new HashSet<>(Arrays.asList(user2))));
    // check that user1 has note listed in his workbench
    Set<String> user1AndRoles = authorizationService.getRoles(user1);
    user1AndRoles.add(user1);
    List<NoteInfo> user1Notes = notebook.getNotesInfo(noteIdTmp -> authorizationService.isReader(noteIdTmp, user1AndRoles));
    assertEquals(1, user1Notes.size());
    assertEquals(user1Notes.get(0).getId(), noteId);
    // check that user2 has note listed in his workbench because of admin role
    Set<String> user2AndRoles = authorizationService.getRoles(user2);
    user2AndRoles.add(user2);
    List<NoteInfo> user2Notes = notebook.getNotesInfo(noteIdTmp -> authorizationService.isReader(noteIdTmp, user2AndRoles));
    assertEquals(1, user2Notes.size());
    assertEquals(user2Notes.get(0).getId(), noteId);
}
Also used : AuthenticationInfo(org.apache.zeppelin.user.AuthenticationInfo) HashSet(java.util.HashSet) AbstractInterpreterTest(org.apache.zeppelin.interpreter.AbstractInterpreterTest) Test(org.junit.Test)

Example 53 with AuthenticationInfo

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

the class NotebookTest method testGetAllNotesWithDifferentPermissions.

@Test
public void testGetAllNotesWithDifferentPermissions() throws IOException {
    List<NoteInfo> notes1 = notebook.getNotesInfo(noteId -> authorizationService.isReader(noteId, new HashSet<>(Arrays.asList("user1"))));
    List<NoteInfo> notes2 = notebook.getNotesInfo(noteId -> authorizationService.isReader(noteId, new HashSet<>(Arrays.asList("user2"))));
    assertEquals(0, notes1.size());
    assertEquals(0, notes2.size());
    // creates note and sets user1 owner
    String note1Id = notebook.createNote("note1", new AuthenticationInfo("user1"));
    // note is public since readers and writers empty
    notes1 = notebook.getNotesInfo(noteId -> authorizationService.isReader(noteId, new HashSet<>(Arrays.asList("user1"))));
    notes2 = notebook.getNotesInfo(noteId -> authorizationService.isReader(noteId, new HashSet<>(Arrays.asList("user2"))));
    assertEquals(1, notes1.size());
    assertEquals(1, notes2.size());
    authorizationService.setReaders(note1Id, new HashSet<>(Arrays.asList("user1")));
    // note is public since writers empty
    notes1 = notebook.getNotesInfo(noteId -> authorizationService.isReader(noteId, new HashSet<>(Arrays.asList("user1"))));
    notes2 = notebook.getNotesInfo(noteId -> authorizationService.isReader(noteId, new HashSet<>(Arrays.asList("user2"))));
    assertEquals(1, notes1.size());
    assertEquals(1, notes2.size());
    authorizationService.setRunners(note1Id, new HashSet<>(Arrays.asList("user1")));
    notes1 = notebook.getNotesInfo(noteId -> authorizationService.isReader(noteId, new HashSet<>(Arrays.asList("user1"))));
    notes2 = notebook.getNotesInfo(noteId -> authorizationService.isReader(noteId, new HashSet<>(Arrays.asList("user2"))));
    assertEquals(1, notes1.size());
    assertEquals(1, notes2.size());
    authorizationService.setWriters(note1Id, new HashSet<>(Arrays.asList("user1")));
    notes1 = notebook.getNotesInfo(noteId -> authorizationService.isReader(noteId, new HashSet<>(Arrays.asList("user1"))));
    notes2 = notebook.getNotesInfo(noteId -> authorizationService.isReader(noteId, new HashSet<>(Arrays.asList("user2"))));
    assertEquals(1, notes1.size());
    assertEquals(0, notes2.size());
}
Also used : ZeppelinConfiguration(org.apache.zeppelin.conf.ZeppelinConfiguration) Arrays(java.util.Arrays) AbstractInterpreterTest(org.apache.zeppelin.interpreter.AbstractInterpreterTest) Date(java.util.Date) LoggerFactory(org.slf4j.LoggerFactory) RemoteInterpreter(org.apache.zeppelin.interpreter.remote.RemoteInterpreter) NotebookRepoSettingsInfo(org.apache.zeppelin.notebook.repo.NotebookRepoSettingsInfo) AuthenticationInfo(org.apache.zeppelin.user.AuthenticationInfo) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ConfVars(org.apache.zeppelin.conf.ZeppelinConfiguration.ConfVars) Job(org.apache.zeppelin.scheduler.Job) After(org.junit.After) Map(java.util.Map) InterpreterSetting(org.apache.zeppelin.interpreter.InterpreterSetting) NotebookRepoWithVersionControl(org.apache.zeppelin.notebook.repo.NotebookRepoWithVersionControl) Assert.fail(org.junit.Assert.fail) ExecutionContext(org.apache.zeppelin.interpreter.ExecutionContext) InterpreterException(org.apache.zeppelin.interpreter.InterpreterException) InterpreterResult(org.apache.zeppelin.interpreter.InterpreterResult) Set(java.util.Set) StandardCharsets(java.nio.charset.StandardCharsets) IOUtils(org.apache.commons.io.IOUtils) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) InterpreterNotFoundException(org.apache.zeppelin.interpreter.InterpreterNotFoundException) Assert.assertFalse(org.junit.Assert.assertFalse) Status(org.apache.zeppelin.scheduler.Job.Status) Mockito.mock(org.mockito.Mockito.mock) AngularObjectRegistry(org.apache.zeppelin.display.AngularObjectRegistry) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) QuartzSchedulerService(org.apache.zeppelin.notebook.scheduler.QuartzSchedulerService) NotebookRepo(org.apache.zeppelin.notebook.repo.NotebookRepo) SchedulerException(org.quartz.SchedulerException) InterpreterGroup(org.apache.zeppelin.interpreter.InterpreterGroup) Before(org.junit.Before) Logger(org.slf4j.Logger) Assert.assertNotNull(org.junit.Assert.assertNotNull) LocalResourcePool(org.apache.zeppelin.resource.LocalResourcePool) FileWriter(java.io.FileWriter) Assert.assertTrue(org.junit.Assert.assertTrue) Credentials(org.apache.zeppelin.user.Credentials) Test(org.junit.Test) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) VFSNotebookRepo(org.apache.zeppelin.notebook.repo.VFSNotebookRepo) File(java.io.File) Assert.assertNotEquals(org.junit.Assert.assertNotEquals) TimeUnit(java.util.concurrent.TimeUnit) Assert.assertNull(org.junit.Assert.assertNull) RepositoryException(org.eclipse.aether.RepositoryException) InterpreterOption(org.apache.zeppelin.interpreter.InterpreterOption) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) AuthenticationInfo(org.apache.zeppelin.user.AuthenticationInfo) HashSet(java.util.HashSet) AbstractInterpreterTest(org.apache.zeppelin.interpreter.AbstractInterpreterTest) Test(org.junit.Test)

Example 54 with AuthenticationInfo

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

the class NotebookTest method testCloneImportCheck.

@Test
public void testCloneImportCheck() throws IOException {
    String sourceNoteId = notebook.createNote("note1", new AuthenticationInfo("user"));
    notebook.processNote(sourceNoteId, sourceNote -> {
        sourceNote.setName("TestNote");
        assertEquals("TestNote", sourceNote.getName());
        Paragraph sourceParagraph = sourceNote.addNewParagraph(AuthenticationInfo.ANONYMOUS);
        assertEquals("anonymous", sourceParagraph.getUser());
        String destNoteId = notebook.createNote("note2", new AuthenticationInfo("user"));
        notebook.processNote(destNoteId, destNote -> {
            destNote.setName("ClonedNote");
            assertEquals("ClonedNote", destNote.getName());
            List<Paragraph> paragraphs = sourceNote.getParagraphs();
            for (Paragraph p : paragraphs) {
                destNote.addCloneParagraph(p, AuthenticationInfo.ANONYMOUS);
                assertEquals("anonymous", p.getUser());
            }
            return null;
        });
        return null;
    });
}
Also used : AuthenticationInfo(org.apache.zeppelin.user.AuthenticationInfo) AbstractInterpreterTest(org.apache.zeppelin.interpreter.AbstractInterpreterTest) Test(org.junit.Test)

Example 55 with AuthenticationInfo

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

the class NotebookTest method testPublicPrivateNewNote.

@Test
public void testPublicPrivateNewNote() throws IOException {
    // case of public note
    assertTrue(conf.isNotebookPublic());
    assertTrue(authorizationService.isPublic());
    List<NoteInfo> notes1 = notebook.getNotesInfo(noteId -> authorizationService.isReader(noteId, new HashSet<>(Arrays.asList("user1"))));
    List<NoteInfo> notes2 = notebook.getNotesInfo(noteId -> authorizationService.isReader(noteId, new HashSet<>(Arrays.asList("user2"))));
    assertEquals(0, notes1.size());
    assertEquals(0, notes2.size());
    // user1 creates note
    String notePublicId = notebook.createNote("note1", new AuthenticationInfo("user1"));
    // both users have note
    notes1 = notebook.getNotesInfo(noteId -> authorizationService.isReader(noteId, new HashSet<>(Arrays.asList("user1"))));
    notes2 = notebook.getNotesInfo(noteId -> authorizationService.isReader(noteId, new HashSet<>(Arrays.asList("user2"))));
    assertEquals(1, notes1.size());
    assertEquals(1, notes2.size());
    assertEquals(notes1.get(0).getId(), notePublicId);
    assertEquals(notes2.get(0).getId(), notePublicId);
    // user1 is only owner
    assertEquals(1, authorizationService.getOwners(notePublicId).size());
    assertEquals(0, authorizationService.getReaders(notePublicId).size());
    assertEquals(0, authorizationService.getRunners(notePublicId).size());
    assertEquals(0, authorizationService.getWriters(notePublicId).size());
    // case of private note
    System.setProperty(ConfVars.ZEPPELIN_NOTEBOOK_PUBLIC.getVarName(), "false");
    ZeppelinConfiguration conf2 = ZeppelinConfiguration.create();
    assertFalse(conf2.isNotebookPublic());
    // notebook authorization reads from conf, so no need to re-initilize
    assertFalse(authorizationService.isPublic());
    // check that still 1 note per user
    notes1 = notebook.getNotesInfo(noteId -> authorizationService.isReader(noteId, new HashSet<>(Arrays.asList("user1"))));
    notes2 = notebook.getNotesInfo(noteId -> authorizationService.isReader(noteId, new HashSet<>(Arrays.asList("user2"))));
    assertEquals(1, notes1.size());
    assertEquals(1, notes2.size());
    // create private note
    String notePrivateId = notebook.createNote("note2", new AuthenticationInfo("user1"));
    // only user1 have notePrivate right after creation
    notes1 = notebook.getNotesInfo(noteId -> authorizationService.isReader(noteId, new HashSet<>(Arrays.asList("user1"))));
    notes2 = notebook.getNotesInfo(noteId -> authorizationService.isReader(noteId, new HashSet<>(Arrays.asList("user2"))));
    assertEquals(2, notes1.size());
    assertEquals(1, notes2.size());
    boolean found = false;
    for (NoteInfo info : notes1) {
        if (info.getId() == notePrivateId) {
            found = true;
            break;
        }
    }
    assertEquals(true, found);
    // user1 have all rights
    assertEquals(1, authorizationService.getOwners(notePrivateId).size());
    assertEquals(1, authorizationService.getReaders(notePrivateId).size());
    assertEquals(1, authorizationService.getRunners(notePrivateId).size());
    assertEquals(1, authorizationService.getWriters(notePrivateId).size());
    // set back public to true
    System.setProperty(ConfVars.ZEPPELIN_NOTEBOOK_PUBLIC.getVarName(), "true");
    ZeppelinConfiguration.create();
}
Also used : ZeppelinConfiguration(org.apache.zeppelin.conf.ZeppelinConfiguration) Arrays(java.util.Arrays) AbstractInterpreterTest(org.apache.zeppelin.interpreter.AbstractInterpreterTest) Date(java.util.Date) LoggerFactory(org.slf4j.LoggerFactory) RemoteInterpreter(org.apache.zeppelin.interpreter.remote.RemoteInterpreter) NotebookRepoSettingsInfo(org.apache.zeppelin.notebook.repo.NotebookRepoSettingsInfo) AuthenticationInfo(org.apache.zeppelin.user.AuthenticationInfo) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ConfVars(org.apache.zeppelin.conf.ZeppelinConfiguration.ConfVars) Job(org.apache.zeppelin.scheduler.Job) After(org.junit.After) Map(java.util.Map) InterpreterSetting(org.apache.zeppelin.interpreter.InterpreterSetting) NotebookRepoWithVersionControl(org.apache.zeppelin.notebook.repo.NotebookRepoWithVersionControl) Assert.fail(org.junit.Assert.fail) ExecutionContext(org.apache.zeppelin.interpreter.ExecutionContext) InterpreterException(org.apache.zeppelin.interpreter.InterpreterException) InterpreterResult(org.apache.zeppelin.interpreter.InterpreterResult) Set(java.util.Set) StandardCharsets(java.nio.charset.StandardCharsets) IOUtils(org.apache.commons.io.IOUtils) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) InterpreterNotFoundException(org.apache.zeppelin.interpreter.InterpreterNotFoundException) Assert.assertFalse(org.junit.Assert.assertFalse) Status(org.apache.zeppelin.scheduler.Job.Status) Mockito.mock(org.mockito.Mockito.mock) AngularObjectRegistry(org.apache.zeppelin.display.AngularObjectRegistry) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) QuartzSchedulerService(org.apache.zeppelin.notebook.scheduler.QuartzSchedulerService) NotebookRepo(org.apache.zeppelin.notebook.repo.NotebookRepo) SchedulerException(org.quartz.SchedulerException) InterpreterGroup(org.apache.zeppelin.interpreter.InterpreterGroup) Before(org.junit.Before) Logger(org.slf4j.Logger) Assert.assertNotNull(org.junit.Assert.assertNotNull) LocalResourcePool(org.apache.zeppelin.resource.LocalResourcePool) FileWriter(java.io.FileWriter) Assert.assertTrue(org.junit.Assert.assertTrue) Credentials(org.apache.zeppelin.user.Credentials) Test(org.junit.Test) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) VFSNotebookRepo(org.apache.zeppelin.notebook.repo.VFSNotebookRepo) File(java.io.File) Assert.assertNotEquals(org.junit.Assert.assertNotEquals) TimeUnit(java.util.concurrent.TimeUnit) Assert.assertNull(org.junit.Assert.assertNull) RepositoryException(org.eclipse.aether.RepositoryException) InterpreterOption(org.apache.zeppelin.interpreter.InterpreterOption) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) ZeppelinConfiguration(org.apache.zeppelin.conf.ZeppelinConfiguration) AuthenticationInfo(org.apache.zeppelin.user.AuthenticationInfo) HashSet(java.util.HashSet) AbstractInterpreterTest(org.apache.zeppelin.interpreter.AbstractInterpreterTest) Test(org.junit.Test)

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