Azkaban的LDAP身份验证

时间:2015-07-15 00:08:33

标签: ldap azkaban

我们正在尝试在我们的生产环境中使用LDAP身份验证设置Azkaban。有关如何做到这一点的任何线索?文档说它可以通过扩展UserManager类添加插件jar文件来完成。我是azkaban的新手,正在为此寻找一些示例代码

2 个答案:

答案 0 :(得分:2)

您需要安装自定义"用户管理器"插入。它可以在github上找到:https://github.com/researchgate/azkaban-ldap-usermanager

有关如何配置用户管理器插件的说明,请参阅github repo的首页。

实质上你需要:

  1. 下载并构建插件
  2. 将您构建的.jar文件复制到Azkaban安装的./extlib目录中
  3. 编辑azkaban.properties文件,指定user.manager.class和许多user.manager.ldap属性。

答案 1 :(得分:1)

我们还想在azkaban中设置LDAP身份验证 - 但第一个答案中提到的开源项目功能非常有限,并且在建立与LDAP服务器的连接后不允许启动TLS协商。

我们编写了一个全新的java类来处理以下场景:

  • 不安全地建立LDAP连接(在端口389上)
  • 启动TLS响应和TLS协商
  • 然后验证用户凭据。

使用这种方法,我们也不必在LDAP中为azkaban创建服务用户。

查看示例代码块

@Override
public User getUser(String username, String password) throws UserManagerException {
    if (username == null || username.trim().isEmpty()) {
        throw new UserManagerException("Username is empty.");
    } else if (password == null || password.trim().isEmpty()) {
        throw new UserManagerException("Password is empty.");
    }

    String email = null;
    String groups = null;
    StringBuilder url = new StringBuilder("ldap://").append(ldapHost).append(":").append(ldapPort);

    Hashtable<String, String> env = new Hashtable<String, String>();
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.PROVIDER_URL, url.toString());

    try {
        // Create initial context
        LdapContext ctx = new InitialLdapContext(env, null);
        // Start TLS
        StartTlsResponse tls = (StartTlsResponse) ctx.extendedOperation(new StartTlsRequest());
        SSLSession sess = tls.negotiate();

        // Adding username and password to environment
        StringBuilder sb = new StringBuilder("uid=").append(username).append(",").append(ldapUserBase);
        ctx.addToEnvironment(Context.SECURITY_AUTHENTICATION, "simple");
        ctx.addToEnvironment(Context.SECURITY_PRINCIPAL, sb.toString());
        ctx.addToEnvironment(Context.SECURITY_CREDENTIALS, password);

        // Search the user in a LDAP directory
        final LdapCtx x = (LdapCtx) ctx.lookup(sb.toString());

        // Lookup is successful - creating an Azkaban user
        User user = new User(username);

        // Searching LDAP directory to get email and group
        SearchControls ctls = new SearchControls();
        String[] attributes = { "cn", "memberOf", "mail" };
        ctls.setReturningAttributes(attributes);

        NamingEnumeration<?> answer = ctx.search(ldapUserBase, "(uid=" + username + ")", ctls);

        Boolean isAdmin = false;
        // Search user email and groups
        while (answer.hasMore()) {
            SearchResult rslt = (SearchResult) answer.next();
            Attributes attrs = rslt.getAttributes();
            groups = attrs.get("memberof").toString().split(":")[1].trim();
            if (attrs.get("memberof") != null && attrs.get("memberof").toString().split(":").length > 0) {
                groups = attrs.get("memberof").toString().split(":")[1].trim();
                for (String group : groups.split(",")) {
                    if (ldapAdminGroups.contains(group))
                        isAdmin = true;
                }
            }
            if (attrs.get("mail") != null) {
                email = attrs.get("mail").toString().split(":")[1].trim();
                user.setEmail(email);
            }
        }

        // Assign the correct role
        if (isAdmin)
            user.addRole("admin");
        else
            user.addRole("read");
        ctx.close();
        return user;
    } catch (NamingException e) {
        throw new UserManagerException("LDAP error: " + e.getMessage(), e);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        throw new UserManagerException("IO error", e);
    }
}

注意:我没有做过很多异常处理 - 您需要根据自己的需要进行处理。

如何让它在Azkaban中运作:

  • 构建maven或gradle项目。
  • 不需要额外的图书馆(Azkaban除外 - 即com.linkedin.azkaban)
  • 有一个新类,它将继承'azkaban.user.UserManager'
  • 在azkaban / extlibs中构建并复制jar
  • 在azkaban.properties中 - 设置“user.manager.class =”以及所有必需的属性,如host,port和ldap userbase(ou = Users,dc = stackoverflow,dc = com)详细信息。

你应该善于通过LDAP验证用户。

快乐编码!!

谢谢, Hussain Bohra

相关问题