Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add auth to the zk client. #50

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
13 changes: 7 additions & 6 deletions src/main/java/com/deem/zkui/utils/ServletUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,20 @@
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;

import org.apache.zookeeper.ZooKeeper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.zookeeper.ZooKeeper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public enum ServletUtil {

Expand Down Expand Up @@ -98,7 +99,7 @@ public ZooKeeper getZookeeper(HttpServletRequest request, HttpServletResponse re
//Converting seconds to ms.
zkSessionTimeout = zkSessionTimeout * 1000;
zk = ZooKeeperUtil.INSTANCE.createZKConnection(zkServer, zkSessionTimeout);
ZooKeeperUtil.INSTANCE.setDefaultAcl(globalProps.getProperty("defaultAcl"));
ZooKeeperUtil.INSTANCE.setDefaultAcl(globalProps.getProperty("defaultAcl"), zk);
if (zk.getState() != ZooKeeper.States.CONNECTED) {
session.setAttribute("zk", null);
} else {
Expand Down
31 changes: 22 additions & 9 deletions src/main/java/com/deem/zkui/utils/ZooKeeperUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,7 @@

import com.deem.zkui.vo.LeafBean;
import com.deem.zkui.vo.ZKNode;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
Expand All @@ -42,6 +35,15 @@
import org.json.simple.parser.ParseException;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

public enum ZooKeeperUtil {

INSTANCE;
Expand Down Expand Up @@ -81,7 +83,12 @@ private ArrayList<ACL> defaultAcl() {
return defaultAcl;
}

public void setDefaultAcl(String jsonAcl) {
/**
* set default ACL
* @param jsonAcl
* @param zk if zk is not null, add the corresponding ACL to the zk
*/
public void setDefaultAcl(String jsonAcl, ZooKeeper zk) {
if (jsonAcl == null || jsonAcl.trim().length() == 0) {
logger.trace("Using UNSAFE ACL. Anyone on your LAN can change your Zookeeper data");
defaultAcl = ZooDefs.Ids.OPEN_ACL_UNSAFE;
Expand All @@ -96,6 +103,12 @@ public void setDefaultAcl(String jsonAcl) {
JSONObject acl = (JSONObject) it.next();
String scheme = ((String) acl.get("scheme")).trim();
String id = ((String) acl.get("id")).trim();

//add the corresponding ACL to the zk
if(zk != null){
zk.addAuthInfo(scheme, id.getBytes());
}

int perms = 0;
String permStr = ((String) acl.get("perms")).toLowerCase().trim();
for (char c : permStr.toCharArray()) {
Expand Down