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));
}
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);
}
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());
}
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;
});
}
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();
}
Aggregations