Repository: incubator-zeppelin Updated Branches: refs/heads/master 4df083dca -> 738c10e21
Notebook Authorization ### What is this PR for? The goal of the PR is to add authorization for notebooks according to the design document [here] (https://gist.github.com/prasadwagle/712b7ca1e0f1f4f1aa20). The PR uses Shiro authentication. ### What type of PR is it? Feature ### Todos * [x] - Find way to get roles for a user in SecurityUtils (Not possible at the moment, see SHIRO-492) * [x] - Investigate how to use Shiro authorization * [x] - Use groups associated with user to determine if operation is permitted * [x] - Check if user has permissions to modify note permissions * [x] - Add checks in more NotebookServer operations * [x] - Improve UI (explain permissions, error messages) * [x] - Add unit tests * [x] - Documentation ### Is there a relevant Jira issue? ZEPPELIN-549 ### How should this be tested? 1. Enable Basic Auth Security by changing conf/shiro.ini. 1. Create a note. By default all operations are allowed by any authenticated user. 1. Update readers, writers and owners by clicking on the lock icon in the top right area. 1. Check if users can or cannot perform operations according to the permissions. ### Screenshots (if appropriate)  ### Questions: * Does the licenses files need update? No * Is there breaking changes for older versions? No * Does this needs documentation? No Author: Prasad Wagle <[email protected]> Closes #681 from prasadwagle/notebook_authorization and squashes the following commits: e7cffd8 [Prasad Wagle] Restore anon default in shiro.ini 24e8de4 [Prasad Wagle] Remove duplicate imports 29ebf48 [Prasad Wagle] Merge with master 52f4914 [Prasad Wagle] Check whether roles is non-empty before adding to userAndRoles 733530f [Prasad Wagle] Minor doc fix 28ea697 [Prasad Wagle] Fixed issues with security documentation reported by @AhyoungRyu 1ac076e [Prasad Wagle] Fixed typo in _navigation.html and updated interpreter_authorization.md 6c89dbe [Prasad Wagle] Implement Moon's suggestions on note permissions background and wildcard placeholder 2554315 [Prasad Wagle] Use user and roles for checking note permissions 3a5e5c0 [Prasad Wagle] Check if user has permissions to modify note permissions fbbd04b [Prasad Wagle] Make insufficient privileges error message easier to read 06c5e07 [Prasad Wagle] Update navigation.html for security docs 6b9e274 [Prasad Wagle] Add unit test for note permissions a8d0ecb [Prasad Wagle] Add security documentation 6e85730 [Prasad Wagle] Notebook Authorization Project: http://git-wip-us.apache.org/repos/asf/incubator-zeppelin/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-zeppelin/commit/738c10e2 Tree: http://git-wip-us.apache.org/repos/asf/incubator-zeppelin/tree/738c10e2 Diff: http://git-wip-us.apache.org/repos/asf/incubator-zeppelin/diff/738c10e2 Branch: refs/heads/master Commit: 738c10e211f851f4ced7b4e6b2f2368b3885f734 Parents: 4df083d Author: Prasad Wagle <[email protected]> Authored: Thu Feb 25 16:54:47 2016 -0800 Committer: Lee moon soo <[email protected]> Committed: Sun Feb 28 08:11:24 2016 -0800 ---------------------------------------------------------------------- conf/shiro.ini | 5 +- docs/_includes/themes/zeppelin/_navigation.html | 6 + docs/security/authentication.md | 31 ++++ docs/security/interpreter_authorization.md | 34 ++++ docs/security/notebook_authorization.md | 37 ++++ docs/security/overview.md | 28 +++ .../apache/zeppelin/rest/NotebookRestApi.java | 63 +++++++ .../apache/zeppelin/rest/SecurityRestApi.java | 8 + .../org/apache/zeppelin/socket/Message.java | 2 + .../apache/zeppelin/socket/NotebookServer.java | 173 ++++++++++++++----- .../apache/zeppelin/utils/SecurityUtils.java | 23 +++ .../zeppelin/socket/NotebookServerTest.java | 2 +- .../src/app/notebook/notebook-actionBar.html | 5 + .../src/app/notebook/notebook.controller.js | 72 ++++++++ zeppelin-web/src/app/notebook/notebook.css | 22 +++ zeppelin-web/src/app/notebook/notebook.html | 24 +++ .../websocketEvents/websocketEvents.factory.js | 7 +- .../java/org/apache/zeppelin/notebook/Note.java | 48 +++++ .../apache/zeppelin/notebook/NotebookTest.java | 27 +++ 19 files changed, 572 insertions(+), 45 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-zeppelin/blob/738c10e2/conf/shiro.ini ---------------------------------------------------------------------- diff --git a/conf/shiro.ini b/conf/shiro.ini index 1cc3cf8..8d803d0 100644 --- a/conf/shiro.ini +++ b/conf/shiro.ini @@ -19,8 +19,9 @@ # List of users with their password allowed to access Zeppelin. # To use a different strategy (LDAP / Database / ...) check the shiro doc at http://shiro.apache.org/configuration.html#Configuration-INISections admin = password1 -user1 = password2 -user2 = password3 +user1 = password2, role1, role2 +user2 = password3, role3 +user3 = password4, role2 # Sample LDAP configuration, for user Authentication, currently tested for single Realm [main] http://git-wip-us.apache.org/repos/asf/incubator-zeppelin/blob/738c10e2/docs/_includes/themes/zeppelin/_navigation.html ---------------------------------------------------------------------- diff --git a/docs/_includes/themes/zeppelin/_navigation.html b/docs/_includes/themes/zeppelin/_navigation.html index 59c2728..6dcc450 100644 --- a/docs/_includes/themes/zeppelin/_navigation.html +++ b/docs/_includes/themes/zeppelin/_navigation.html @@ -86,6 +86,12 @@ <li><a href="{{BASE_PATH}}/rest-api/rest-notebook.html">Notebook API</a></li> <li><a href="{{BASE_PATH}}/rest-api/rest-configuration.html">Configuration API</a></li> <li role="separator" class="divider"></li> + <!-- li><span><b>Security</b><span></li --> + <li><a href="{{BASE_PATH}}/security/overview.html">Security Overview</a></li> + <li><a href="{{BASE_PATH}}/security/authentication.html">Authentication</a></li> + <li><a href="{{BASE_PATH}}/security/notebook_authorization.html">Notebook Authorization</a></li> + <li><a href="{{BASE_PATH}}/security/interpreter_authorization.html">Interpreter Authorization</a></li> + <li role="separator" class="divider"></li> <!-- li><span><b>Development</b><span></li --> <li><a href="{{BASE_PATH}}/development/writingzeppelininterpreter.html">Writing Zeppelin Interpreter</a></li> <li><a href="{{BASE_PATH}}/development/howtocontribute.html">How to contribute (code)</a></li> http://git-wip-us.apache.org/repos/asf/incubator-zeppelin/blob/738c10e2/docs/security/authentication.md ---------------------------------------------------------------------- diff --git a/docs/security/authentication.md b/docs/security/authentication.md new file mode 100644 index 0000000..081d419 --- /dev/null +++ b/docs/security/authentication.md @@ -0,0 +1,31 @@ +--- +layout: page +title: "Authentication" +description: "Authentication" +group: security +--- +<!-- +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +--> +# Authentication + +Authentication is company-specific. + +One option is to use [Basic Access Authentication](https://en.wikipedia.org/wiki/Basic_access_authentication) + +Another option is to have an authentication server that can verify user credentials in an LDAP server. +If an incoming request to the Zeppelin server does not have a cookie with user information encrypted with the authentication server public key, the user +is redirected to the authentication server. Once the user is verified, the authentication server redirects the browser to a specific +URL in the Zeppelin server which sets the authentication cookie in the browser. +The end result is that all requests to the Zeppelin +web server have the authentication cookie which contains user and groups information. http://git-wip-us.apache.org/repos/asf/incubator-zeppelin/blob/738c10e2/docs/security/interpreter_authorization.md ---------------------------------------------------------------------- diff --git a/docs/security/interpreter_authorization.md b/docs/security/interpreter_authorization.md new file mode 100644 index 0000000..d1c792d --- /dev/null +++ b/docs/security/interpreter_authorization.md @@ -0,0 +1,34 @@ +--- +layout: page +title: "Notebook Authorization" +description: "Notebook Authorization" +group: security +--- +<!-- +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +--> +# Interpreter and Data Source Authorization + +## Interpreter Authorization + +Interpreter authorization involves permissions like creating an interpreter and execution queries using it. + +## Data Source Authorization + +Data source authorization involves authenticating to the data source like a Mysql database and letting it determine user permissions. + +For the Hive interpreter, we need to maintain per-user connection pools. +The interpret method takes the user string as parameter and executes the jdbc call using a connection in the user's connection pool. + +In case of Presto, we don't need password if the Presto DB server runs backend code using HDFS authorization for the user. +For databases like Vertica and Mysql we have to store password information for users. http://git-wip-us.apache.org/repos/asf/incubator-zeppelin/blob/738c10e2/docs/security/notebook_authorization.md ---------------------------------------------------------------------- diff --git a/docs/security/notebook_authorization.md b/docs/security/notebook_authorization.md new file mode 100644 index 0000000..b9521b1 --- /dev/null +++ b/docs/security/notebook_authorization.md @@ -0,0 +1,37 @@ +--- +layout: page +title: "Notebook Authorization" +description: "Notebook Authorization" +group: security +--- +<!-- +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +--> +# Notebook Authorization + +We assume that there is an authentication component that associates a user string and a set of group strings with every NotebookSocket. + +Each note has the following: +* set of owner entities (users or groups) +* set of reader entities (users or groups) +* set of writer entities (users or groups) + +If a set is empty, it means that any user can perform that operation. + +The NotebookServer classifies every Note operation into three categories: read, write, manage. +Before executing a Note operation, it checks if the user and the groups associated with the NotebookSocket have permissions. For example, before executing an read +operation, it checks if the user and the groups have at least one entity that belongs to the reader entities. + +To initialize and modify note permissions, we provide UI like "Interpreter binding". The user inputs comma separated entities for owners, readers and writers. +We execute a rest api call with this information. In the backend we get the user information for the connection and allow the operation if the user and groups +associated with the current user have at least one entity that belongs to owner entities for the note. http://git-wip-us.apache.org/repos/asf/incubator-zeppelin/blob/738c10e2/docs/security/overview.md ---------------------------------------------------------------------- diff --git a/docs/security/overview.md b/docs/security/overview.md new file mode 100644 index 0000000..e76410d --- /dev/null +++ b/docs/security/overview.md @@ -0,0 +1,28 @@ +--- +layout: page +title: "Security Overview" +description: "Security Overview" +group: security +--- +<!-- +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +--> +{% include JB/setup %} + +# Security Overview + +There are three aspects to Zeppelin security: + +* Authentication: is the user who they say they are? [More](authentication.html) +* Notebook authorization: does the user have permissions to read or write to a note? [More](notebook_authorization.html) +* Interpreter and data source authorization: does the user have permissions to perform interpreter operations or access data source objects? [More](interpreter_authorization.html) http://git-wip-us.apache.org/repos/asf/incubator-zeppelin/blob/738c10e2/zeppelin-server/src/main/java/org/apache/zeppelin/rest/NotebookRestApi.java ---------------------------------------------------------------------- diff --git a/zeppelin-server/src/main/java/org/apache/zeppelin/rest/NotebookRestApi.java b/zeppelin-server/src/main/java/org/apache/zeppelin/rest/NotebookRestApi.java index 486e5b1..be46f13 100644 --- a/zeppelin-server/src/main/java/org/apache/zeppelin/rest/NotebookRestApi.java +++ b/zeppelin-server/src/main/java/org/apache/zeppelin/rest/NotebookRestApi.java @@ -18,6 +18,8 @@ package org.apache.zeppelin.rest; import java.io.IOException; +import java.util.HashMap; +import java.util.HashSet; import java.util.LinkedList; import java.util.List; import java.util.Map; @@ -46,6 +48,7 @@ import org.apache.zeppelin.rest.message.RunParagraphWithParametersRequest; import org.apache.zeppelin.search.SearchService; import org.apache.zeppelin.server.JsonResponse; import org.apache.zeppelin.socket.NotebookServer; +import org.apache.zeppelin.utils.SecurityUtils; import org.quartz.CronExpression; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -76,6 +79,66 @@ public class NotebookRestApi { } /** + * list note owners + */ + @GET + @Path("{noteId}/permissions") + public Response getNotePermissions(@PathParam("noteId") String noteId) { + Note note = notebook.getNote(noteId); + HashMap<String, HashSet> permissionsMap = new HashMap<String, HashSet>(); + permissionsMap.put("owners", note.getOwners()); + permissionsMap.put("readers", note.getReaders()); + permissionsMap.put("writers", note.getWriters()); + return new JsonResponse<>(Status.OK, "", permissionsMap).build(); + } + + String ownerPermissionError(HashSet<String> current, + HashSet<String> allowed) throws IOException { + LOG.info("Cannot change permissions. Connection owners {}. Allowed owners {}", + current.toString(), allowed.toString()); + return "Insufficient privileges to change permissions.\n\n" + + "Allowed owners: " + allowed.toString() + "\n\n" + + "User belongs to: " + current.toString(); + } + + /** + * Set note owners + */ + @PUT + @Path("{noteId}/permissions") + public Response putNotePermissions(@PathParam("noteId") String noteId, String req) + throws IOException { + HashMap<String, HashSet> permMap = gson.fromJson(req, + new TypeToken<HashMap<String, HashSet>>(){}.getType()); + Note note = notebook.getNote(noteId); + String principal = SecurityUtils.getPrincipal(); + HashSet<String> roles = SecurityUtils.getRoles(); + LOG.info("Set permissions {} {} {} {} {}", + noteId, + principal, + permMap.get("owners"), + permMap.get("readers"), + permMap.get("writers") + ); + + HashSet<String> userAndRoles = new HashSet<String>(); + userAndRoles.add(principal); + userAndRoles.addAll(roles); + if (!note.isOwner(userAndRoles)) { + return new JsonResponse<>(Status.FORBIDDEN, ownerPermissionError(userAndRoles, + note.getOwners())).build(); + } + note.setOwners(permMap.get("owners")); + note.setReaders(permMap.get("readers")); + note.setWriters(permMap.get("writers")); + LOG.debug("After set permissions {} {} {}", note.getOwners(), note.getReaders(), + note.getWriters()); + note.persist(); + notebookServer.broadcastNote(note); + return new JsonResponse<>(Status.OK).build(); + } + + /** * bind a setting to note * @throws IOException */ http://git-wip-us.apache.org/repos/asf/incubator-zeppelin/blob/738c10e2/zeppelin-server/src/main/java/org/apache/zeppelin/rest/SecurityRestApi.java ---------------------------------------------------------------------- diff --git a/zeppelin-server/src/main/java/org/apache/zeppelin/rest/SecurityRestApi.java b/zeppelin-server/src/main/java/org/apache/zeppelin/rest/SecurityRestApi.java index d6f3dec..905eb2b 100644 --- a/zeppelin-server/src/main/java/org/apache/zeppelin/rest/SecurityRestApi.java +++ b/zeppelin-server/src/main/java/org/apache/zeppelin/rest/SecurityRestApi.java @@ -21,12 +21,15 @@ import org.apache.zeppelin.conf.ZeppelinConfiguration; import org.apache.zeppelin.server.JsonResponse; import org.apache.zeppelin.ticket.TicketContainer; import org.apache.zeppelin.utils.SecurityUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.core.Response; import java.util.HashMap; +import java.util.HashSet; import java.util.Map; /** @@ -36,6 +39,8 @@ import java.util.Map; @Path("/security") @Produces("application/json") public class SecurityRestApi { + private static final Logger LOG = LoggerFactory.getLogger(SecurityRestApi.class); + /** * Required by Swagger. */ @@ -56,6 +61,7 @@ public class SecurityRestApi { public Response ticket() { ZeppelinConfiguration conf = ZeppelinConfiguration.create(); String principal = SecurityUtils.getPrincipal(); + HashSet<String> roles = SecurityUtils.getRoles(); JsonResponse response; // ticket set to anonymous for anonymous user. Simplify testing. String ticket; @@ -66,9 +72,11 @@ public class SecurityRestApi { Map<String, String> data = new HashMap<>(); data.put("principal", principal); + data.put("roles", roles.toString()); data.put("ticket", ticket); response = new JsonResponse(Response.Status.OK, "", data); + LOG.warn(response.toString()); return response.build(); } } http://git-wip-us.apache.org/repos/asf/incubator-zeppelin/blob/738c10e2/zeppelin-server/src/main/java/org/apache/zeppelin/socket/Message.java ---------------------------------------------------------------------- diff --git a/zeppelin-server/src/main/java/org/apache/zeppelin/socket/Message.java b/zeppelin-server/src/main/java/org/apache/zeppelin/socket/Message.java index 54d9ab2..7da23ec 100644 --- a/zeppelin-server/src/main/java/org/apache/zeppelin/socket/Message.java +++ b/zeppelin-server/src/main/java/org/apache/zeppelin/socket/Message.java @@ -96,6 +96,7 @@ public class Message { PARAGRAPH_APPEND_OUTPUT, // [s-c] append output PARAGRAPH_UPDATE_OUTPUT, // [s-c] update (replace) output PING, + AUTH_INFO, ANGULAR_OBJECT_UPDATE, // [s-c] add/update angular object ANGULAR_OBJECT_REMOVE, // [s-c] add angular object del @@ -116,6 +117,7 @@ public class Message { public Map<String, Object> data = new HashMap<String, Object>(); public String ticket = "anonymous"; public String principal = "anonymous"; + public String roles = ""; public Message(OP op) { this.op = op; http://git-wip-us.apache.org/repos/asf/incubator-zeppelin/blob/738c10e2/zeppelin-server/src/main/java/org/apache/zeppelin/socket/NotebookServer.java ---------------------------------------------------------------------- diff --git a/zeppelin-server/src/main/java/org/apache/zeppelin/socket/NotebookServer.java b/zeppelin-server/src/main/java/org/apache/zeppelin/socket/NotebookServer.java index 00e4858..3b7da73 100644 --- a/zeppelin-server/src/main/java/org/apache/zeppelin/socket/NotebookServer.java +++ b/zeppelin-server/src/main/java/org/apache/zeppelin/socket/NotebookServer.java @@ -18,6 +18,8 @@ package org.apache.zeppelin.socket; import com.google.common.base.Strings; import com.google.gson.Gson; +import com.google.gson.reflect.TypeToken; + import org.apache.zeppelin.conf.ZeppelinConfiguration; import org.apache.zeppelin.conf.ZeppelinConfiguration.ConfVars; import org.apache.zeppelin.display.AngularObject; @@ -96,6 +98,7 @@ public class NotebookServer extends WebSocketServlet implements LOG.debug("RECEIVE << " + messagereceived.op); LOG.debug("RECEIVE PRINCIPAL << " + messagereceived.principal); LOG.debug("RECEIVE TICKET << " + messagereceived.ticket); + LOG.debug("RECEIVE ROLES << " + messagereceived.roles); String ticket = TicketContainer.instance.getTicket(messagereceived.principal); if (ticket != null && !ticket.equals(messagereceived.ticket)) throw new Exception("Invalid ticket " + messagereceived.ticket + " != " + ticket); @@ -107,6 +110,16 @@ public class NotebookServer extends WebSocketServlet implements throw new Exception("Anonymous access not allowed "); } + HashSet<String> userAndRoles = new HashSet<String>(); + userAndRoles.add(messagereceived.principal); + if (!messagereceived.roles.equals("")) { + HashSet<String> roles = gson.fromJson(messagereceived.roles, + new TypeToken<HashSet<String>>(){}.getType()); + if (roles != null) { + userAndRoles.addAll(roles); + } + } + /** Lets be elegant here */ switch (messagereceived.op) { case LIST_NOTES: @@ -116,57 +129,57 @@ public class NotebookServer extends WebSocketServlet implements broadcastReloadedNoteList(); break; case GET_HOME_NOTE: - sendHomeNote(conn, notebook); + sendHomeNote(conn, userAndRoles, notebook); break; case GET_NOTE: - sendNote(conn, notebook, messagereceived); + sendNote(conn, userAndRoles, notebook, messagereceived); break; case NEW_NOTE: - createNote(conn, notebook, messagereceived); + createNote(conn, userAndRoles, notebook, messagereceived); break; case DEL_NOTE: - removeNote(conn, notebook, messagereceived); + removeNote(conn, userAndRoles, notebook, messagereceived); break; case CLONE_NOTE: - cloneNote(conn, notebook, messagereceived); + cloneNote(conn, userAndRoles, notebook, messagereceived); break; case IMPORT_NOTE: - importNote(conn, notebook, messagereceived); + importNote(conn, userAndRoles, notebook, messagereceived); break; case COMMIT_PARAGRAPH: - updateParagraph(conn, notebook, messagereceived); + updateParagraph(conn, userAndRoles, notebook, messagereceived); break; case RUN_PARAGRAPH: - runParagraph(conn, notebook, messagereceived); + runParagraph(conn, userAndRoles, notebook, messagereceived); break; case CANCEL_PARAGRAPH: - cancelParagraph(conn, notebook, messagereceived); + cancelParagraph(conn, userAndRoles, notebook, messagereceived); break; case MOVE_PARAGRAPH: - moveParagraph(conn, notebook, messagereceived); + moveParagraph(conn, userAndRoles, notebook, messagereceived); break; case INSERT_PARAGRAPH: - insertParagraph(conn, notebook, messagereceived); + insertParagraph(conn, userAndRoles, notebook, messagereceived); break; case PARAGRAPH_REMOVE: - removeParagraph(conn, notebook, messagereceived); + removeParagraph(conn, userAndRoles, notebook, messagereceived); break; case PARAGRAPH_CLEAR_OUTPUT: - clearParagraphOutput(conn, notebook, messagereceived); + clearParagraphOutput(conn, userAndRoles, notebook, messagereceived); break; case NOTE_UPDATE: - updateNote(conn, notebook, messagereceived); + updateNote(conn, userAndRoles, notebook, messagereceived); break; case COMPLETION: - completion(conn, notebook, messagereceived); + completion(conn, userAndRoles, notebook, messagereceived); break; case PING: break; //do nothing case ANGULAR_OBJECT_UPDATED: - angularObjectUpdated(conn, notebook, messagereceived); + angularObjectUpdated(conn, userAndRoles, notebook, messagereceived); break; case LIST_CONFIGURATIONS: - sendAllConfigurations(conn, notebook); + sendAllConfigurations(conn, userAndRoles, notebook); break; case CHECKPOINT_NOTEBOOK: checkpointNotebook(conn, notebook, messagereceived); @@ -358,8 +371,24 @@ public class NotebookServer extends WebSocketServlet implements broadcastAll(new Message(OP.NOTES_INFO).put("notes", notesInfo)); } - private void sendNote(NotebookSocket conn, Notebook notebook, + void permissionError(NotebookSocket conn, String op, HashSet<String> current, + HashSet<String> allowed) throws IOException { + LOG.info("Cannot {}. Connection readers {}. Allowed readers {}", + op, current, allowed); + conn.send(serializeMessage(new Message(OP.AUTH_INFO).put("info", + "Insufficient privileges to " + op + " note.\n\n" + + "Allowed users or roles: " + allowed.toString() + "\n\n" + + "User belongs to: " + current.toString()))); + } + + private void sendNote(NotebookSocket conn, HashSet<String> userAndRoles, Notebook notebook, Message fromMessage) throws IOException { + + LOG.info("New operation from {} : {} : {} : {} : {}", conn.getRequest().getRemoteAddr(), + conn.getRequest().getRemotePort(), + fromMessage.principal, fromMessage.op, fromMessage.get("id") + ); + String noteId = (String) fromMessage.get("id"); if (noteId == null) { return; @@ -367,13 +396,19 @@ public class NotebookServer extends WebSocketServlet implements Note note = notebook.getNote(noteId); if (note != null) { + if (!note.isReader(userAndRoles)) { + permissionError(conn, "read", userAndRoles, note.getReaders()); + broadcastNoteList(); + return; + } addConnectionToNote(note.id(), conn); conn.send(serializeMessage(new Message(OP.NOTE).put("note", note))); sendAllAngularObjects(note, conn); } } - private void sendHomeNote(NotebookSocket conn, Notebook notebook) throws IOException { + private void sendHomeNote(NotebookSocket conn, HashSet<String> userAndRoles, + Notebook notebook) throws IOException { String noteId = notebook.getConf().getString(ConfVars.ZEPPELIN_NOTEBOOK_HOMESCREEN); Note note = null; @@ -382,6 +417,11 @@ public class NotebookServer extends WebSocketServlet implements } if (note != null) { + if (!note.isReader(userAndRoles)) { + permissionError(conn, "read", userAndRoles, note.getReaders()); + broadcastNoteList(); + return; + } addConnectionToNote(note.id(), conn); conn.send(serializeMessage(new Message(OP.NOTE).put("note", note))); sendAllAngularObjects(note, conn); @@ -391,7 +431,8 @@ public class NotebookServer extends WebSocketServlet implements } } - private void updateNote(WebSocket conn, Notebook notebook, Message fromMessage) + private void updateNote(WebSocket conn, HashSet<String> userAndRoles, + Notebook notebook, Message fromMessage) throws SchedulerException, IOException { String noteId = (String) fromMessage.get("id"); String name = (String) fromMessage.get("name"); @@ -433,7 +474,8 @@ public class NotebookServer extends WebSocketServlet implements return cronUpdated; } - private void createNote(NotebookSocket conn, Notebook notebook, Message message) + private void createNote(NotebookSocket conn, HashSet<String> userAndRoles, + Notebook notebook, Message message) throws IOException { Note note = notebook.createNote(); note.addParagraph(); // it's an empty note. so add one paragraph @@ -451,7 +493,8 @@ public class NotebookServer extends WebSocketServlet implements broadcastNoteList(); } - private void removeNote(WebSocket conn, Notebook notebook, Message fromMessage) + private void removeNote(NotebookSocket conn, HashSet<String> userAndRoles, + Notebook notebook, Message fromMessage) throws IOException { String noteId = (String) fromMessage.get("id"); if (noteId == null) { @@ -459,13 +502,19 @@ public class NotebookServer extends WebSocketServlet implements } Note note = notebook.getNote(noteId); + + if (!note.isOwner(userAndRoles)) { + permissionError(conn, "remove", userAndRoles, note.getOwners()); + return; + } + notebook.removeNote(noteId); removeNote(noteId); broadcastNoteList(); } - private void updateParagraph(NotebookSocket conn, Notebook notebook, - Message fromMessage) throws IOException { + private void updateParagraph(NotebookSocket conn, HashSet<String> userAndRoles, + Notebook notebook, Message fromMessage) throws IOException { String paragraphId = (String) fromMessage.get("id"); if (paragraphId == null) { return; @@ -476,6 +525,12 @@ public class NotebookServer extends WebSocketServlet implements Map<String, Object> config = (Map<String, Object>) fromMessage .get("config"); final Note note = notebook.getNote(getOpenNoteId(conn)); + + if (!note.isWriter(userAndRoles)) { + permissionError(conn, "write", userAndRoles, note.getWriters()); + return; + } + Paragraph p = note.getParagraph(paragraphId); p.settings.setParams(params); p.setConfig(config); @@ -485,7 +540,8 @@ public class NotebookServer extends WebSocketServlet implements broadcast(note.id(), new Message(OP.PARAGRAPH).put("paragraph", p)); } - private void cloneNote(NotebookSocket conn, Notebook notebook, Message fromMessage) + private void cloneNote(NotebookSocket conn, HashSet<String> userAndRoles, + Notebook notebook, Message fromMessage) throws IOException, CloneNotSupportedException { String noteId = getOpenNoteId(conn); String name = (String) fromMessage.get("name"); @@ -495,7 +551,8 @@ public class NotebookServer extends WebSocketServlet implements broadcastNoteList(); } - protected Note importNote(NotebookSocket conn, Notebook notebook, Message fromMessage) + protected Note importNote(NotebookSocket conn, HashSet<String> userAndRoles, + Notebook notebook, Message fromMessage) throws IOException { Note note = null; if (fromMessage != null) { @@ -509,14 +566,20 @@ public class NotebookServer extends WebSocketServlet implements return note; } - private void removeParagraph(NotebookSocket conn, Notebook notebook, - Message fromMessage) throws IOException { + private void removeParagraph(NotebookSocket conn, HashSet<String> userAndRoles, + Notebook notebook, Message fromMessage) throws IOException { final String paragraphId = (String) fromMessage.get("id"); if (paragraphId == null) { return; } final Note note = notebook.getNote(getOpenNoteId(conn)); + + if (!note.isWriter(userAndRoles)) { + permissionError(conn, "write", userAndRoles, note.getWriters()); + return; + } + /** We dont want to remove the last paragraph */ if (!note.isLastParagraph(paragraphId)) { note.removeParagraph(paragraphId); @@ -525,19 +588,25 @@ public class NotebookServer extends WebSocketServlet implements } } - private void clearParagraphOutput(NotebookSocket conn, Notebook notebook, - Message fromMessage) throws IOException { + private void clearParagraphOutput(NotebookSocket conn, HashSet<String> userAndRoles, + Notebook notebook, Message fromMessage) throws IOException { final String paragraphId = (String) fromMessage.get("id"); if (paragraphId == null) { return; } final Note note = notebook.getNote(getOpenNoteId(conn)); + + if (!note.isWriter(userAndRoles)) { + permissionError(conn, "write", userAndRoles, note.getWriters()); + return; + } + note.clearParagraphOutput(paragraphId); broadcastNote(note); } - private void completion(NotebookSocket conn, Notebook notebook, + private void completion(NotebookSocket conn, HashSet<String> userAndRoles, Notebook notebook, Message fromMessage) throws IOException { String paragraphId = (String) fromMessage.get("id"); String buffer = (String) fromMessage.get("buf"); @@ -561,8 +630,8 @@ public class NotebookServer extends WebSocketServlet implements * @param notebook the notebook. * @param fromMessage the message. */ - private void angularObjectUpdated(NotebookSocket conn, Notebook notebook, - Message fromMessage) { + private void angularObjectUpdated(NotebookSocket conn, HashSet<String> userAndRoles, + Notebook notebook, Message fromMessage) { String noteId = (String) fromMessage.get("noteId"); String paragraphId = (String) fromMessage.get("paragraphId"); String interpreterGroupId = (String) fromMessage.get("interpreterGroupId"); @@ -644,7 +713,7 @@ public class NotebookServer extends WebSocketServlet implements } } - private void moveParagraph(NotebookSocket conn, Notebook notebook, + private void moveParagraph(NotebookSocket conn, HashSet<String> userAndRoles, Notebook notebook, Message fromMessage) throws IOException { final String paragraphId = (String) fromMessage.get("id"); if (paragraphId == null) { @@ -654,22 +723,34 @@ public class NotebookServer extends WebSocketServlet implements final int newIndex = (int) Double.parseDouble(fromMessage.get("index") .toString()); final Note note = notebook.getNote(getOpenNoteId(conn)); + + if (!note.isWriter(userAndRoles)) { + permissionError(conn, "write", userAndRoles, note.getWriters()); + return; + } + note.moveParagraph(paragraphId, newIndex); note.persist(); broadcastNote(note); } - private void insertParagraph(NotebookSocket conn, Notebook notebook, - Message fromMessage) throws IOException { + private void insertParagraph(NotebookSocket conn, HashSet<String> userAndRoles, + Notebook notebook, Message fromMessage) throws IOException { final int index = (int) Double.parseDouble(fromMessage.get("index") .toString()); final Note note = notebook.getNote(getOpenNoteId(conn)); + + if (!note.isWriter(userAndRoles)) { + permissionError(conn, "write", userAndRoles, note.getWriters()); + return; + } + note.insertParagraph(index); note.persist(); broadcastNote(note); } - private void cancelParagraph(NotebookSocket conn, Notebook notebook, + private void cancelParagraph(NotebookSocket conn, HashSet<String> userAndRoles, Notebook notebook, Message fromMessage) throws IOException { final String paragraphId = (String) fromMessage.get("id"); if (paragraphId == null) { @@ -677,11 +758,17 @@ public class NotebookServer extends WebSocketServlet implements } final Note note = notebook.getNote(getOpenNoteId(conn)); + + if (!note.isWriter(userAndRoles)) { + permissionError(conn, "write", userAndRoles, note.getWriters()); + return; + } + Paragraph p = note.getParagraph(paragraphId); p.abort(); } - private void runParagraph(NotebookSocket conn, Notebook notebook, + private void runParagraph(NotebookSocket conn, HashSet<String> userAndRoles, Notebook notebook, Message fromMessage) throws IOException { final String paragraphId = (String) fromMessage.get("id"); if (paragraphId == null) { @@ -689,6 +776,12 @@ public class NotebookServer extends WebSocketServlet implements } final Note note = notebook.getNote(getOpenNoteId(conn)); + + if (!note.isWriter(userAndRoles)) { + permissionError(conn, "write", userAndRoles, note.getWriters()); + return; + } + Paragraph p = note.getParagraph(paragraphId); String text = (String) fromMessage.get("paragraph"); p.setText(text); @@ -729,8 +822,8 @@ public class NotebookServer extends WebSocketServlet implements } } - private void sendAllConfigurations(NotebookSocket conn, Notebook notebook) - throws IOException { + private void sendAllConfigurations(NotebookSocket conn, HashSet<String> userAndRoles, + Notebook notebook) throws IOException { ZeppelinConfiguration conf = notebook.getConf(); Map<String, String> configurations = conf.dumpConfigurations(conf, http://git-wip-us.apache.org/repos/asf/incubator-zeppelin/blob/738c10e2/zeppelin-server/src/main/java/org/apache/zeppelin/utils/SecurityUtils.java ---------------------------------------------------------------------- diff --git a/zeppelin-server/src/main/java/org/apache/zeppelin/utils/SecurityUtils.java b/zeppelin-server/src/main/java/org/apache/zeppelin/utils/SecurityUtils.java index 1d06e3a..e7e39f2 100644 --- a/zeppelin-server/src/main/java/org/apache/zeppelin/utils/SecurityUtils.java +++ b/zeppelin-server/src/main/java/org/apache/zeppelin/utils/SecurityUtils.java @@ -23,6 +23,8 @@ import java.net.InetAddress; import java.net.URI; import java.net.URISyntaxException; import java.net.UnknownHostException; +import java.util.Arrays; +import java.util.HashSet; /** * Tools for securing Zeppelin @@ -52,6 +54,7 @@ public class SecurityUtils { */ public static String getPrincipal() { Subject subject = org.apache.shiro.SecurityUtils.getSubject(); + String principal; if (subject.isAuthenticated()) { principal = subject.getPrincipal().toString(); @@ -61,4 +64,24 @@ public class SecurityUtils { } return principal; } + + /** + * Return the roles associated with the authenticated user if any otherwise returns empty set + * TODO(prasadwagle) Find correct way to get user roles (see SHIRO-492) + * @return shiro roles + */ + public static HashSet<String> getRoles() { + Subject subject = org.apache.shiro.SecurityUtils.getSubject(); + HashSet<String> roles = new HashSet<>(); + + if (subject.isAuthenticated()) { + for (String role : Arrays.asList("role1", "role2", "role3")) { + if (subject.hasRole(role)) { + roles.add(role); + } + } + } + return roles; + } + } http://git-wip-us.apache.org/repos/asf/incubator-zeppelin/blob/738c10e2/zeppelin-server/src/test/java/org/apache/zeppelin/socket/NotebookServerTest.java ---------------------------------------------------------------------- diff --git a/zeppelin-server/src/test/java/org/apache/zeppelin/socket/NotebookServerTest.java b/zeppelin-server/src/test/java/org/apache/zeppelin/socket/NotebookServerTest.java index 7d8f3cf..774f30a 100644 --- a/zeppelin-server/src/test/java/org/apache/zeppelin/socket/NotebookServerTest.java +++ b/zeppelin-server/src/test/java/org/apache/zeppelin/socket/NotebookServerTest.java @@ -140,7 +140,7 @@ public class NotebookServerTest extends AbstractTestRestApi { Message messageReceived = notebookServer.deserializeMessage(msg); Note note = null; try { - note = notebookServer.importNote(null, notebook, messageReceived); + note = notebookServer.importNote(null, null, notebook, messageReceived); } catch (NullPointerException e) { //broadcastNoteList(); failed nothing to worry. LOG.error("Exception in NotebookServerTest while testImportNotebook, failed nothing to " + http://git-wip-us.apache.org/repos/asf/incubator-zeppelin/blob/738c10e2/zeppelin-web/src/app/notebook/notebook-actionBar.html ---------------------------------------------------------------------- diff --git a/zeppelin-web/src/app/notebook/notebook-actionBar.html b/zeppelin-web/src/app/notebook/notebook-actionBar.html index f69a087..66e497b 100644 --- a/zeppelin-web/src/app/notebook/notebook-actionBar.html +++ b/zeppelin-web/src/app/notebook/notebook-actionBar.html @@ -155,6 +155,11 @@ limitations under the License. tooltip-placement="bottom" tooltip="Interpreter binding"> <i class="fa fa-cog" ng-style="{color: showSetting ? '#3071A9' : 'black' }"></i> </span> + <span style="position:relative; top:2px; margin-right:4px; cursor:pointer;" + ng-click="togglePermissions()" + tooltip-placement="bottom" tooltip="Note permissions"> + <i class="fa fa-lock" ng-style="{color: showSetting ? '#3071A9' : 'black' }"></i> + </span> <span class="btn-group"> <button type="button" class="btn btn-default btn-xs dropdown-toggle" http://git-wip-us.apache.org/repos/asf/incubator-zeppelin/blob/738c10e2/zeppelin-web/src/app/notebook/notebook.controller.js ---------------------------------------------------------------------- diff --git a/zeppelin-web/src/app/notebook/notebook.controller.js b/zeppelin-web/src/app/notebook/notebook.controller.js index 859ea33..194cd77 100644 --- a/zeppelin-web/src/app/notebook/notebook.controller.js +++ b/zeppelin-web/src/app/notebook/notebook.controller.js @@ -632,6 +632,69 @@ angular.module('zeppelinWebApp').controller('NotebookCtrl', } }; + var getPermissions = function(callback) { + $http.get(baseUrlSrv.getRestApiBase()+ '/notebook/' +$scope.note.id + '/permissions'). + success(function(data, status, headers, config) { + $scope.permissions = data.body; + $scope.permissionsOrig = angular.copy($scope.permissions); // to check dirty + if (callback) { + callback(); + } + }). + error(function(data, status, headers, config) { + if (status !== 0) { + console.log('Error %o %o', status, data.message); + } + }); + }; + + $scope.openPermissions = function() { + $scope.showPermissions = true; + getPermissions(); + }; + + + $scope.closePermissions = function() { + if (isPermissionsDirty()) { + BootstrapDialog.confirm({ + closable: true, + title: '', + message: 'Changes will be discarded.', + callback: function(result) { + if (result) { + $scope.$apply(function() { + $scope.showPermissions = false; + }); + } + } + }); + } else { + $scope.showPermissions = false; + } + }; + + $scope.savePermissions = function() { + $http.put(baseUrlSrv.getRestApiBase() + '/notebook/' +$scope.note.id + '/permissions', + $scope.permissions, {withCredentials: true}). + success(function(data, status, headers, config) { + console.log('Note permissions %o saved', $scope.permissions); + $scope.showPermissions = false; + }). + error(function(data, status, headers, config) { + console.log('Error %o %o', status, data.message); + alert(data.message); + }); + }; + + $scope.togglePermissions = function() { + if ($scope.showPermissions) { + $scope.closePermissions(); + } else { + $scope.openPermissions(); + } + }; + + var isSettingDirty = function() { if (angular.equals($scope.interpreterBindings, $scope.interpreterBindingsOrig)) { return false; @@ -639,4 +702,13 @@ angular.module('zeppelinWebApp').controller('NotebookCtrl', return true; } }; + + var isPermissionsDirty = function() { + if (angular.equals($scope.permissions, $scope.permissionsOrig)) { + return false; + } else { + return true; + } + }; + }); http://git-wip-us.apache.org/repos/asf/incubator-zeppelin/blob/738c10e2/zeppelin-web/src/app/notebook/notebook.css ---------------------------------------------------------------------- diff --git a/zeppelin-web/src/app/notebook/notebook.css b/zeppelin-web/src/app/notebook/notebook.css index b41bdb4..b128593 100644 --- a/zeppelin-web/src/app/notebook/notebook.css +++ b/zeppelin-web/src/app/notebook/notebook.css @@ -133,6 +133,28 @@ } /* + Note Permissions Panel +*/ + +.permissions { + background: white; + padding: 10px 15px 15px 15px; + margin-left: -10px; + margin-right: -10px; + font-family: 'Roboto', sans-serif; + box-shadow: 0 2px 4px rgba(0, 0, 0, 0.15); + border-bottom: 1px solid #E5E5E5; +} + +.permissions .permissionsForm { + list-style-type: none; + background: #EFEFEF; + padding: 10px 10px 10px 10px; + box-shadow: 0 1px 1px rgba(0, 0, 0, 0.15); + border: 1px solid #E5E5E5; +} + +/* Note Setting Panel */ http://git-wip-us.apache.org/repos/asf/incubator-zeppelin/blob/738c10e2/zeppelin-web/src/app/notebook/notebook.html ---------------------------------------------------------------------- diff --git a/zeppelin-web/src/app/notebook/notebook.html b/zeppelin-web/src/app/notebook/notebook.html index 41d1e1c..8c114dd 100644 --- a/zeppelin-web/src/app/notebook/notebook.html +++ b/zeppelin-web/src/app/notebook/notebook.html @@ -14,6 +14,30 @@ limitations under the License. <!-- Here the controller <NotebookCtrl> is not needed because explicitly set in the app.js (route) --> <div ng-include src="'app/notebook/notebook-actionBar.html'"></div> <div style="padding-top: 36px;"> + <!-- permissions --> + <div ng-if="showPermissions" class="permissions"> + <div> + <h4>Note Permissions (Only note owners can change)</h4> + </div> + <hr /> + <div> + <p> + Enter comma separated users and groups in the fields. <br /> + Empty field (*) implies anyone can do the operation. + </p> + <div class="permissionsForm" + data-ng-model="permissions"> + <p>Owners : <input ng-list ng-model="permissions.owners" placeholder="*"> Owners can change permissions, read and write the note. </p> + <p>Readers : <input ng-list ng-model="permissions.readers" placeholder="*"> Readers can only read the note.</p> + <p>Writers : <input ng-list ng-model="permissions.writers" placeholder="*"> Writers can read and write the note.</p> + </div> + </div> + <br /> + <div> + <button class="btn btn-primary" ng-click="savePermissions()">Save</button> + <button class="btn btn-default" ng-click="closePermissions()">Cancel</button> + </div> + </div> <!-- settings --> <div ng-if="showSetting" class="setting"> <div> http://git-wip-us.apache.org/repos/asf/incubator-zeppelin/blob/738c10e2/zeppelin-web/src/components/websocketEvents/websocketEvents.factory.js ---------------------------------------------------------------------- diff --git a/zeppelin-web/src/components/websocketEvents/websocketEvents.factory.js b/zeppelin-web/src/components/websocketEvents/websocketEvents.factory.js index 800d450..f40c47f 100644 --- a/zeppelin-web/src/components/websocketEvents/websocketEvents.factory.js +++ b/zeppelin-web/src/components/websocketEvents/websocketEvents.factory.js @@ -30,7 +30,8 @@ angular.module('zeppelinWebApp').factory('websocketEvents', function($rootScope, websocketCalls.sendNewEvent = function(data) { data.principal = $rootScope.ticket.principal; data.ticket = $rootScope.ticket.ticket; - console.log('Send >> %o, %o, %o, %o', data.op, data.principal, data.ticket, data); + data.roles = $rootScope.ticket.roles; + console.log('Send >> %o, %o, %o, %o, %o', data.op, data.principal, data.ticket, data.roles, data); websocketCalls.ws.send(JSON.stringify(data)); }; @@ -52,12 +53,14 @@ angular.module('zeppelinWebApp').factory('websocketEvents', function($rootScope, $location.path('notebook/' + data.note.id); } else if (op === 'NOTES_INFO') { $rootScope.$broadcast('setNoteMenu', data.notes); + } else if (op === 'AUTH_INFO') { + alert(data.info.toString()); } else if (op === 'PARAGRAPH') { $rootScope.$broadcast('updateParagraph', data); } else if (op === 'PARAGRAPH_APPEND_OUTPUT') { $rootScope.$broadcast('appendParagraphOutput', data); } else if (op === 'PARAGRAPH_UPDATE_OUTPUT') { - $rootScope.$broadcast('updateParagraphOutput', data); + $rootScope.$broadcast('updateParagraphOutput', data); } else if (op === 'PROGRESS') { $rootScope.$broadcast('updateProgress', data); } else if (op === 'COMPLETION_LIST') { http://git-wip-us.apache.org/repos/asf/incubator-zeppelin/blob/738c10e2/zeppelin-zengine/src/main/java/org/apache/zeppelin/notebook/Note.java ---------------------------------------------------------------------- diff --git a/zeppelin-zengine/src/main/java/org/apache/zeppelin/notebook/Note.java b/zeppelin-zengine/src/main/java/org/apache/zeppelin/notebook/Note.java index b0470c8..3765b32 100644 --- a/zeppelin-zengine/src/main/java/org/apache/zeppelin/notebook/Note.java +++ b/zeppelin-zengine/src/main/java/org/apache/zeppelin/notebook/Note.java @@ -59,6 +59,9 @@ public class Note implements Serializable, JobListener { private String name = ""; private String id; + private HashSet<String> owners = new HashSet<String>(); + private HashSet<String> readers = new HashSet<String>(); + private HashSet<String> writers = new HashSet<String>(); @SuppressWarnings("rawtypes") Map<String, List<AngularObject>> angularObjects = new HashMap<>(); @@ -115,6 +118,51 @@ public class Note implements Serializable, JobListener { this.name = name; } + public HashSet<String> getOwners() { + return (new HashSet<String>(owners)); + } + + public void setOwners(HashSet<String> owners) { + this.owners = new HashSet<String>(owners); + } + + public HashSet<String> getReaders() { + return (new HashSet<String>(readers)); + } + + public void setReaders(HashSet<String> readers) { + this.readers = new HashSet<String>(readers); + } + + public HashSet<String> getWriters() { + return (new HashSet<String>(writers)); + } + + public void setWriters(HashSet<String> writers) { + this.writers = new HashSet<String>(writers); + } + + public boolean isOwner(HashSet<String> entities) { + return isMember(entities, this.owners); + } + + public boolean isWriter(HashSet<String> entities) { + return isMember(entities, this.writers) || isMember(entities, this.owners); + } + + public boolean isReader(HashSet<String> entities) { + return isMember(entities, this.readers) || + isMember(entities, this.owners) || + isMember(entities, this.writers); + } + + // return true if b is empty or if (a intersection b) is non-empty + private boolean isMember(HashSet<String> a, HashSet<String> b) { + Set<String> intersection = new HashSet<String>(b); + intersection.retainAll(a); + return (b.isEmpty() || (intersection.size() > 0)); + } + public NoteInterpreterLoader getNoteReplLoader() { return replLoader; } http://git-wip-us.apache.org/repos/asf/incubator-zeppelin/blob/738c10e2/zeppelin-zengine/src/test/java/org/apache/zeppelin/notebook/NotebookTest.java ---------------------------------------------------------------------- diff --git a/zeppelin-zengine/src/test/java/org/apache/zeppelin/notebook/NotebookTest.java b/zeppelin-zengine/src/test/java/org/apache/zeppelin/notebook/NotebookTest.java index d96f7a9..0b5af2c 100644 --- a/zeppelin-zengine/src/test/java/org/apache/zeppelin/notebook/NotebookTest.java +++ b/zeppelin-zengine/src/test/java/org/apache/zeppelin/notebook/NotebookTest.java @@ -27,10 +27,12 @@ import static org.mockito.Mockito.mock; import java.io.File; import java.io.IOException; import java.util.ArrayList; +import java.util.Arrays; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.HashSet; import org.apache.commons.io.FileUtils; import org.apache.zeppelin.conf.ZeppelinConfiguration; @@ -426,6 +428,31 @@ public class NotebookTest implements JobListenerFactory{ } @Test + public void testPermissions() throws IOException { + // create a note and a paragraph + Note note = notebook.createNote(); + // empty owners, readers and writers means note is public + assertEquals(note.isOwner(new HashSet<String>(Arrays.asList("user2"))), true); + assertEquals(note.isReader(new HashSet<String>(Arrays.asList("user2"))), true); + assertEquals(note.isWriter(new HashSet<String>(Arrays.asList("user2"))), true); + + note.setOwners(new HashSet<String>(Arrays.asList("user1"))); + note.setReaders(new HashSet<String>(Arrays.asList("user1", "user2"))); + note.setWriters(new HashSet<String>(Arrays.asList("user1"))); + + assertEquals(note.isOwner(new HashSet<String>(Arrays.asList("user2"))), false); + assertEquals(note.isOwner(new HashSet<String>(Arrays.asList("user1"))), true); + + assertEquals(note.isReader(new HashSet<String>(Arrays.asList("user3"))), false); + assertEquals(note.isReader(new HashSet<String>(Arrays.asList("user2"))), true); + + assertEquals(note.isWriter(new HashSet<String>(Arrays.asList("user2"))), false); + assertEquals(note.isWriter(new HashSet<String>(Arrays.asList("user1"))), true); + + notebook.removeNote(note.id()); + } + + @Test public void testAbortParagraphStatusOnInterpreterRestart() throws InterruptedException, IOException { Note note = notebook.createNote();
