Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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 @@ -119,6 +120,16 @@ public Response putNotePermissions(@PathParam("noteId") String noteId, String re
permMap.get("readers"),
permMap.get("writers")
);

String noExistUser = checkUser(permMap);

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

String message = "User: " + noExistUser + " not Exists,Please Check !";

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

}

HashSet<String> userAndRoles = new HashSet<String>();
userAndRoles.add(principal);
Expand All @@ -127,9 +138,27 @@ 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");
String princial = SecurityUtils.getPrincipal();
if (!"anonymous".equals(princial)) {
if (owners.isEmpty()) {
owners = Sets.newHashSet(principal);
}
// Set writers, if owners is empty -> set to user requesting the change
if (readers != null && !readers.isEmpty()) {
if (writers.isEmpty()) {
writers = Sets.newHashSet(principal);
}
}
}


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 +167,35 @@ 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 (!org.apache.zeppelin.utils.SecurityUtils.hasUser(tmpUser)) {

userName = tmpUser;

break;

}

}

return userName;

}

/**
* bind a setting to note
Expand Down Expand Up @@ -650,8 +708,21 @@ public Response getCronJob(@PathParam("notebookId") String notebookId) throws
@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.checkNoteUser(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;

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -110,16 +110,10 @@ public void setOwners(String noteId, Set<String> entities) {
noteAuthInfo.put("owners", new LinkedHashSet(entities));
noteAuthInfo.put("readers", new LinkedHashSet());
noteAuthInfo.put("writers", new LinkedHashSet());
authInfo.put(noteId, noteAuthInfo);
} else {
Set<String> existingEntities = noteAuthInfo.get("owners");
if (existingEntities == null) {
noteAuthInfo.put("owners", new LinkedHashSet(entities));
} else {
existingEntities.clear();
existingEntities.addAll(entities);
}
noteAuthInfo.put("owners", new LinkedHashSet(entities));
}
authInfo.put(noteId, noteAuthInfo);
saveToFile();
}

Expand All @@ -130,16 +124,10 @@ public void setReaders(String noteId, Set<String> entities) {
noteAuthInfo.put("owners", new LinkedHashSet());
noteAuthInfo.put("readers", new LinkedHashSet(entities));
noteAuthInfo.put("writers", new LinkedHashSet());
authInfo.put(noteId, noteAuthInfo);
} else {
Set<String> existingEntities = noteAuthInfo.get("readers");
if (existingEntities == null) {
noteAuthInfo.put("readers", new LinkedHashSet(entities));
} else {
existingEntities.clear();
existingEntities.addAll(entities);
}
noteAuthInfo.put("readers", new LinkedHashSet(entities));
}
authInfo.put(noteId, noteAuthInfo);
saveToFile();
}

Expand All @@ -150,18 +138,31 @@ public void setWriters(String noteId, Set<String> entities) {
noteAuthInfo.put("owners", new LinkedHashSet());
noteAuthInfo.put("readers", new LinkedHashSet());
noteAuthInfo.put("writers", new LinkedHashSet(entities));
authInfo.put(noteId, noteAuthInfo);
} else {
Set<String> existingEntities = noteAuthInfo.get("writers");
if (existingEntities == null) {
noteAuthInfo.put("writers", new LinkedHashSet(entities));
} else {
existingEntities.clear();
existingEntities.addAll(entities);
}
noteAuthInfo.put("writers", new LinkedHashSet(entities));
}
authInfo.put(noteId, noteAuthInfo);
saveToFile();
}

public boolean checkNoteUser(String noteId, HashSet<String> userAndRoles) {
boolean state = true;
Map<String, Set<String>> noteAuthInfo = authInfo.get(noteId);
Set<String> entities = null;
if (noteAuthInfo == null) {
entities = new HashSet<String>();
} else {
entities.addAll(noteAuthInfo.get("owners"));
entities.addAll(noteAuthInfo.get("readers"));
entities.addAll(noteAuthInfo.get("writers"));
}

if (!entities.isEmpty()) {
state = isReader(noteId, entities);
}

return state;
}

public Set<String> getOwners(String noteId) {
Map<String, Set<String>> noteAuthInfo = authInfo.get(noteId);
Expand Down