Skip to content

Commit bd1a44a

Browse files
address comment + test
1 parent 05afa2a commit bd1a44a

File tree

2 files changed

+80
-1
lines changed

2 files changed

+80
-1
lines changed

zeppelin-zengine/src/main/java/org/apache/zeppelin/notebook/repo/NotebookRepoSync.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,10 @@ public void remove(String noteId, AuthenticationInfo subject) throws IOException
166166
/* TODO(khalid): handle case when removing from secondary storage fails */
167167
}
168168

169+
void remove(int repoIndex, String noteId, AuthenticationInfo subject) throws IOException {
170+
getRepo(repoIndex).remove(noteId, subject);
171+
}
172+
169173
/**
170174
* Copies new/updated notes from source to destination storage
171175
*
@@ -228,7 +232,7 @@ private void pushNotes(AuthenticationInfo subject, List<String> ids, NotebookRep
228232
for (String id : ids) {
229233
try {
230234
remoteRepo.save(localRepo.get(id, subject), subject);
231-
if (setPermissions) {
235+
if (setPermissions && emptyNoteAcl(id)) {
232236
makePrivate(id, subject);
233237
}
234238
} catch (IOException e) {
@@ -237,6 +241,13 @@ private void pushNotes(AuthenticationInfo subject, List<String> ids, NotebookRep
237241
}
238242
}
239243

244+
private boolean emptyNoteAcl(String noteId) {
245+
NotebookAuthorization notebookAuthorization = NotebookAuthorization.getInstance();
246+
return notebookAuthorization.getOwners(noteId).isEmpty()
247+
&& notebookAuthorization.getReaders(noteId).isEmpty()
248+
&& notebookAuthorization.getWriters(noteId).isEmpty();
249+
}
250+
240251
private void makePrivate(String noteId, AuthenticationInfo subject) {
241252
if (AuthenticationInfo.isAnonymous(subject)) {
242253
LOG.info("User is anonymous, permissions are not set for pulled notes");

zeppelin-zengine/src/test/java/org/apache/zeppelin/notebook/repo/NotebookRepoSyncTest.java

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@
2424

2525
import java.io.File;
2626
import java.io.IOException;
27+
import java.util.HashSet;
2728
import java.util.Map;
29+
import java.util.Set;
2830

2931
import org.apache.commons.io.FileUtils;
3032
import org.apache.zeppelin.conf.ZeppelinConfiguration;
@@ -314,6 +316,72 @@ public void testCheckpointOneStorage() throws IOException, SchedulerException {
314316
notebookRepoSync.remove(note.getId(), anonymous);
315317
}
316318

319+
@Test
320+
public void testSyncWithAcl() throws IOException {
321+
/* scenario 1 - note exists with acl on main storage */
322+
AuthenticationInfo user1 = new AuthenticationInfo("user1");
323+
Note note = notebookSync.createNote(user1);
324+
assertEquals(0, note.getParagraphs().size());
325+
326+
// saved on both storages
327+
assertEquals(1, notebookRepoSync.list(0, null).size());
328+
assertEquals(1, notebookRepoSync.list(1, null).size());
329+
330+
/* check that user1 is the only owner */
331+
NotebookAuthorization authInfo = NotebookAuthorization.getInstance();
332+
Set<String> entity = new HashSet<String>();
333+
entity.add(user1.getUser());
334+
assertEquals(true, authInfo.isOwner(note.getId(), entity));
335+
assertEquals(1, authInfo.getOwners(note.getId()).size());
336+
assertEquals(0, authInfo.getReaders(note.getId()).size());
337+
assertEquals(0, authInfo.getWriters(note.getId()).size());
338+
339+
/* update note and save on secondary storage */
340+
Paragraph p1 = note.addParagraph();
341+
p1.setText("hello world");
342+
assertEquals(1, note.getParagraphs().size());
343+
notebookRepoSync.save(1, note, null);
344+
345+
/* check paragraph isn't saved into first storage */
346+
assertEquals(0, notebookRepoSync.get(0,
347+
notebookRepoSync.list(0, null).get(0).getId(), null).getParagraphs().size());
348+
/* check paragraph is saved into second storage */
349+
assertEquals(1, notebookRepoSync.get(1,
350+
notebookRepoSync.list(1, null).get(0).getId(), null).getParagraphs().size());
351+
352+
/* now sync by user1 */
353+
notebookRepoSync.sync(user1);
354+
355+
/* check that note updated and acl are same on main storage*/
356+
assertEquals(1, notebookRepoSync.get(0,
357+
notebookRepoSync.list(0, null).get(0).getId(), null).getParagraphs().size());
358+
assertEquals(true, authInfo.isOwner(note.getId(), entity));
359+
assertEquals(1, authInfo.getOwners(note.getId()).size());
360+
assertEquals(0, authInfo.getReaders(note.getId()).size());
361+
assertEquals(0, authInfo.getWriters(note.getId()).size());
362+
363+
/* scenario 2 - note doesn't exist on main storage */
364+
/* remove from main storage */
365+
notebookRepoSync.remove(0, note.getId(), user1);
366+
assertEquals(0, notebookRepoSync.list(0, null).size());
367+
assertEquals(1, notebookRepoSync.list(1, null).size());
368+
authInfo.removeNote(note.getId());
369+
assertEquals(0, authInfo.getOwners(note.getId()).size());
370+
assertEquals(0, authInfo.getReaders(note.getId()).size());
371+
assertEquals(0, authInfo.getWriters(note.getId()).size());
372+
373+
/* now sync - should bring note from secondary storage with added acl */
374+
notebookRepoSync.sync(user1);
375+
assertEquals(1, notebookRepoSync.list(0, null).size());
376+
assertEquals(1, notebookRepoSync.list(1, null).size());
377+
assertEquals(1, authInfo.getOwners(note.getId()).size());
378+
assertEquals(1, authInfo.getReaders(note.getId()).size());
379+
assertEquals(1, authInfo.getWriters(note.getId()).size());
380+
assertEquals(true, authInfo.isOwner(note.getId(), entity));
381+
assertEquals(true, authInfo.isReader(note.getId(), entity));
382+
assertEquals(true, authInfo.isWriter(note.getId(), entity));
383+
}
384+
317385
static void delete(File file){
318386
if(file.isFile()) file.delete();
319387
else if(file.isDirectory()){

0 commit comments

Comments
 (0)