Skip to content

Commit e41b9b9

Browse files
address comment + test
1 parent 1c11c2d commit e41b9b9

File tree

2 files changed

+81
-1
lines changed

2 files changed

+81
-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: 69 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;
@@ -42,6 +44,7 @@
4244
import org.apache.zeppelin.scheduler.SchedulerFactory;
4345
import org.apache.zeppelin.search.SearchService;
4446
import org.apache.zeppelin.search.LuceneSearch;
47+
import org.apache.zeppelin.user.AuthenticationInfo;
4548
import org.apache.zeppelin.user.Credentials;
4649
import org.junit.After;
4750
import org.junit.Before;
@@ -308,6 +311,72 @@ public void testCheckpointOneStorage() throws IOException, SchedulerException {
308311
assertThat(gitRepo.revisionHistory(noteId, null).size()).isEqualTo(vCount + 1);
309312
}
310313

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

0 commit comments

Comments
 (0)