|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.zeppelin.rest; |
| 19 | + |
| 20 | +import org.apache.commons.lang3.reflect.FieldUtils; |
| 21 | +import org.apache.shiro.realm.jdbc.JdbcRealm; |
| 22 | +import org.apache.shiro.realm.ldap.JndiLdapContextFactory; |
| 23 | +import org.apache.shiro.realm.ldap.JndiLdapRealm; |
| 24 | +import org.apache.shiro.realm.text.IniRealm; |
| 25 | +import org.apache.shiro.util.JdbcUtils; |
| 26 | +import org.slf4j.Logger; |
| 27 | +import org.slf4j.LoggerFactory; |
| 28 | + |
| 29 | +import javax.naming.NamingEnumeration; |
| 30 | +import javax.naming.directory.Attributes; |
| 31 | +import javax.naming.directory.SearchControls; |
| 32 | +import javax.naming.directory.SearchResult; |
| 33 | +import javax.naming.ldap.LdapContext; |
| 34 | +import javax.sql.DataSource; |
| 35 | +import java.sql.Connection; |
| 36 | +import java.sql.PreparedStatement; |
| 37 | +import java.sql.ResultSet; |
| 38 | +import java.util.ArrayList; |
| 39 | +import java.util.Iterator; |
| 40 | +import java.util.List; |
| 41 | +import java.util.Map; |
| 42 | + |
| 43 | +/** |
| 44 | + * This is class which help fetching users from different realms. |
| 45 | + * getUserList() function is overloaded and according to the realm passed to the function it |
| 46 | + * extracts users from its respective realm |
| 47 | + */ |
| 48 | +public class GetUserList { |
| 49 | + |
| 50 | + private static final Logger LOG = LoggerFactory.getLogger(GetUserList.class); |
| 51 | + |
| 52 | + /** |
| 53 | + * function to extract users from shiro.ini |
| 54 | + */ |
| 55 | + public List<String> getUserList(IniRealm r) { |
| 56 | + List<String> userList = new ArrayList<>(); |
| 57 | + Map getIniUser = r.getIni().get("users"); |
| 58 | + Iterator it = getIniUser.entrySet().iterator(); |
| 59 | + while (it.hasNext()) { |
| 60 | + Map.Entry pair = (Map.Entry) it.next(); |
| 61 | + userList.add(pair.getKey().toString()); |
| 62 | + } |
| 63 | + return userList; |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * function to extract users from LDAP |
| 68 | + */ |
| 69 | + public List<String> getUserList(JndiLdapRealm r) { |
| 70 | + List<String> userList = new ArrayList<>(); |
| 71 | + String userDnTemplate = r.getUserDnTemplate(); |
| 72 | + String userDn[] = userDnTemplate.split(",", 2); |
| 73 | + String userDnPrefix = userDn[0].split("=")[0]; |
| 74 | + String userDnSuffix = userDn[1]; |
| 75 | + JndiLdapContextFactory CF = (JndiLdapContextFactory) r.getContextFactory(); |
| 76 | + try { |
| 77 | + LdapContext ctx = CF.getSystemLdapContext(); |
| 78 | + SearchControls constraints = new SearchControls(); |
| 79 | + constraints.setSearchScope(SearchControls.SUBTREE_SCOPE); |
| 80 | + String[] attrIDs = {userDnPrefix}; |
| 81 | + constraints.setReturningAttributes(attrIDs); |
| 82 | + NamingEnumeration result = ctx.search(userDnSuffix, "(objectclass=*)", constraints); |
| 83 | + while (result.hasMore()) { |
| 84 | + Attributes attrs = ((SearchResult) result.next()).getAttributes(); |
| 85 | + if (attrs.get(userDnPrefix) != null) { |
| 86 | + String currentUser = attrs.get(userDnPrefix).toString(); |
| 87 | + userList.add(currentUser.split(":")[1]); |
| 88 | + } |
| 89 | + } |
| 90 | + } catch (Exception e) { |
| 91 | + LOG.error("Error retrieving User list from Ldap Realm", e); |
| 92 | + } |
| 93 | + return userList; |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * function to extract users from JDBCs |
| 98 | + */ |
| 99 | + public List<String> getUserList(JdbcRealm obj) { |
| 100 | + List<String> userlist = new ArrayList<>(); |
| 101 | + PreparedStatement ps = null; |
| 102 | + ResultSet rs = null; |
| 103 | + DataSource dataSource = null; |
| 104 | + String authQuery = ""; |
| 105 | + String retval[]; |
| 106 | + String tablename = ""; |
| 107 | + String username = ""; |
| 108 | + String userquery = ""; |
| 109 | + try { |
| 110 | + dataSource = (DataSource) FieldUtils.readField(obj, "dataSource", true); |
| 111 | + authQuery = (String) FieldUtils.readField(obj, "DEFAULT_AUTHENTICATION_QUERY", true); |
| 112 | + LOG.info(authQuery); |
| 113 | + String authQueryLowerCase = authQuery.toLowerCase(); |
| 114 | + retval = authQueryLowerCase.split("from", 2); |
| 115 | + if (retval.length >= 2) { |
| 116 | + retval = retval[1].split("with|where", 2); |
| 117 | + tablename = retval[0]; |
| 118 | + retval = retval[1].split("where", 2); |
| 119 | + if (retval.length >= 2) |
| 120 | + retval = retval[1].split("=", 2); |
| 121 | + else |
| 122 | + retval = retval[0].split("=", 2); |
| 123 | + username = retval[0]; |
| 124 | + } |
| 125 | + |
| 126 | + if (username.equals("") || tablename.equals("")){ |
| 127 | + return userlist; |
| 128 | + } |
| 129 | + |
| 130 | + userquery = "select " + username + " from " + tablename; |
| 131 | + |
| 132 | + } catch (IllegalAccessException e) { |
| 133 | + LOG.error("Error while accessing dataSource for JDBC Realm", e); |
| 134 | + return null; |
| 135 | + } |
| 136 | + |
| 137 | + try { |
| 138 | + Connection con = dataSource.getConnection(); |
| 139 | + ps = con.prepareStatement(userquery); |
| 140 | + rs = ps.executeQuery(); |
| 141 | + while (rs.next()) { |
| 142 | + userlist.add(rs.getString(1)); |
| 143 | + } |
| 144 | + } catch (Exception e) { |
| 145 | + LOG.error("Error retrieving User list from JDBC Realm", e); |
| 146 | + } finally { |
| 147 | + JdbcUtils.closeResultSet(rs); |
| 148 | + JdbcUtils.closeStatement(ps); |
| 149 | + } |
| 150 | + return userlist; |
| 151 | + } |
| 152 | + |
| 153 | +} |
0 commit comments