Skip to content
Closed
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
af30890
Update SecurityUtils.java
wind0727 May 6, 2016
76fb0d0
Update SecurityUtils.java
wind0727 May 6, 2016
9f3b850
Update SecurityUtils.java
wind0727 May 11, 2016
d91c933
Update NotebookRestApi.java
wind0727 May 11, 2016
7f3c690
Update SecurityUtils.java
wind0727 May 11, 2016
5139934
Update SecurityUtils.java
wind0727 May 11, 2016
245f55e
Update SecurityUtils.java
wind0727 May 11, 2016
86476e6
Update NotebookRestApi.java
wind0727 May 11, 2016
7b36eef
Update SecurityUtils.java
wind0727 May 11, 2016
ff08437
Update SecurityUtils.java
wind0727 May 11, 2016
7e8310c
Update SecurityUtils.java
wind0727 May 12, 2016
1112d0a
Update SecurityUtils.java
wind0727 May 12, 2016
97dfad7
Update NotebookRestApi.java
wind0727 May 12, 2016
978379c
Update NotebookRestApi.java
wind0727 May 12, 2016
d427773
Update NotebookRestApi.java
wind0727 May 12, 2016
f813058
Update NotebookRestApi.java
wind0727 May 19, 2016
425ec36
Update NotebookAuthorization.java
wind0727 May 19, 2016
882ddf4
Update NotebookRestApi.java
wind0727 May 19, 2016
0b1bcd8
Update NotebookAuthorization.java
wind0727 May 19, 2016
311db8c
Update NotebookAuthorization.java
wind0727 May 19, 2016
51ca536
Update NotebookAuthorization.java
wind0727 May 19, 2016
4bd8e5f
Update NotebookRestApi.java
wind0727 May 19, 2016
6739605
Update NotebookRestApi.java
wind0727 May 19, 2016
8db8948
Update NotebookRestApi.java
wind0727 May 19, 2016
d6348c7
Update NotebookRestApi.java
wind0727 May 19, 2016
fdb581f
Update NotebookAuthorization.java
wind0727 May 19, 2016
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.collect.Sets;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.google.gson.GsonBuilder;
Expand Down Expand Up @@ -99,6 +100,10 @@ String ownerPermissionError(Set<String> current,
"Allowed owners: " + allowed.toString() + "\n\n" +
"User belongs to: " + current.toString();
}

String userNamePermissionError(String userName) throws IOException {
return "User: " + userName + " not Exists,Please Check !";

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do we need a separate method here?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prompt input user does not exist,No common method calls

}

/**
* set note authorization information
Expand All @@ -119,6 +124,14 @@ public Response putNotePermissions(@PathParam("noteId") String noteId, String re
permMap.get("readers"),
permMap.get("writers")
);

String noExistUser = checkUser(permMap);

if (!"".equals(noExistUser)) {

return new JsonResponse<>(Status.FORBIDDEN, userNamePermissionError(noExistUser)).build();

}

HashSet<String> userAndRoles = new HashSet<String>();
userAndRoles.add(principal);
Expand All @@ -127,9 +140,29 @@ public Response putNotePermissions(@PathParam("noteId") String noteId, String re
return new JsonResponse<>(Status.FORBIDDEN, ownerPermissionError(userAndRoles,
notebookAuthorization.getOwners(noteId))).build();
}
notebookAuthorization.setOwners(noteId, permMap.get("owners"));
notebookAuthorization.setReaders(noteId, permMap.get("readers"));
notebookAuthorization.setWriters(noteId, permMap.get("writers"));

HashSet readers = permMap.get("readers");
HashSet owners = permMap.get("owners");
HashSet writers = permMap.get("writers");
// Set readers, if writers and owners is empty -> set to user requesting the change
if (readers != null && !readers.isEmpty()) {
if (writers.isEmpty()) {
writers = Sets.newHashSet(SecurityUtils.getPrincipal());
}
if (owners.isEmpty()) {
owners = Sets.newHashSet(SecurityUtils.getPrincipal());
}
}
// Set writers, if owners is empty -> set to user requesting the change
if ( writers != null && !writers.isEmpty()) {
if (owners.isEmpty()) {
owners = Sets.newHashSet(SecurityUtils.getPrincipal());
}
}

notebookAuthorization.setReaders(noteId, readers);
notebookAuthorization.setWriters(noteId, writers);
notebookAuthorization.setOwners(noteId, owners);
LOG.debug("After set permissions {} {} {}",
notebookAuthorization.getOwners(noteId),
notebookAuthorization.getReaders(noteId),
Expand All @@ -138,6 +171,37 @@ public Response putNotePermissions(@PathParam("noteId") String noteId, String re
notebookServer.broadcastNote(note);
return new JsonResponse<>(Status.OK).build();
}

private static String checkUser(HashMap<String, HashSet> permMap) {

String userName = "";

HashSet<String> owners = permMap.get("owners");
HashSet<String> readers = permMap.get("readers");
HashSet<String> writers = permMap.get("writers");

HashSet<String> users = new HashSet<String>();
users.addAll(owners);
users.addAll(readers);
users.addAll(writers);

for (String tmpUser : users) {

if (!"*".equals(tmpUser)) {
if (!org.apache.zeppelin.utils.SecurityUtils.hasUser(tmpUser)) {

userName = tmpUser;

break;

}
}

}

return userName;

}

/**
* bind a setting to note
Expand Down Expand Up @@ -644,14 +708,29 @@ public Response getCronJob(@PathParam("notebookId") String notebookId) throws
}

/**
* Search for a Notes
* Search for a Notes with permissions
*/
@GET
@Path("search")
public Response search(@QueryParam("q") String queryTerm) {
LOG.info("Searching notebooks for: {}", queryTerm);
String principal = SecurityUtils.getPrincipal();
HashSet<String> roles = SecurityUtils.getRoles();
HashSet<String> userAndRoles = new HashSet<String>();
userAndRoles.add(principal);
userAndRoles.addAll(roles);
List<Map<String, String>> notebooksFound = notebookIndex.query(queryTerm);
LOG.info("{} notbooks found", notebooksFound.size());
for (int i = 0; i < notebooksFound.size(); i++) {
String[] Id = notebooksFound.get(i).get("id").split("/", 2);
String noteId = Id[0];
if (!notebookAuthorization.isOwner(noteId, userAndRoles) &&
!notebookAuthorization.isReader(noteId, userAndRoles) &&
!notebookAuthorization.isWriter(noteId, userAndRoles)) {
notebooksFound.remove(i);
i--;
}
}
LOG.info("{} notebooks found", notebooksFound.size());
return new JsonResponse<>(Status.OK, notebooksFound).build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
package org.apache.zeppelin.utils;

import org.apache.shiro.subject.Subject;
import org.apache.shiro.realm.Realm;
import org.apache.shiro.realm.SimpleAccountRealm;
import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.zeppelin.conf.ZeppelinConfiguration;

import java.net.InetAddress;
Expand All @@ -25,6 +29,8 @@
import java.net.UnknownHostException;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;

/**
* Tools for securing Zeppelin
Expand Down Expand Up @@ -83,5 +89,51 @@ public static HashSet<String> getRoles() {
}
return roles;
}

public static boolean hasUser(String userName) {

boolean state = false;

SecurityManager sm = (SecurityManager) org.apache.shiro.SecurityUtils.getSecurityManager();

DefaultSecurityManager defSecurityManager = null;
if (sm instanceof DefaultSecurityManager) {

defSecurityManager = (DefaultSecurityManager) sm;

} else {

return true;

}

List realms = (List) defSecurityManager.getRealms();

org.apache.shiro.realm.SimpleAccountRealm simpleRealm = null;
Iterator iter = realms.iterator();

while (iter.hasNext()) {

Realm realm = (Realm) iter.next();

if (realm instanceof SimpleAccountRealm) {

simpleRealm = (SimpleAccountRealm) realm;

break;

}

}

if (simpleRealm != null) {

state = simpleRealm.accountExists(userName);

}

return state;

}

}