| 
 | 1 | +/**  | 
 | 2 | + * Licensed to the Apache Software Foundation (ASF) under one  | 
 | 3 | + * or more contributor license agreements.  See the NOTICE file  | 
 | 4 | + * distributed with this work for additional information  | 
 | 5 | + * regarding copyright ownership.  The ASF licenses this file  | 
 | 6 | + * to you under the Apache License, Version 2.0 (the  | 
 | 7 | + * "License"); you may not use this file except in compliance  | 
 | 8 | + * with the License.  You may obtain a copy of the License at  | 
 | 9 | + *  | 
 | 10 | + *     http://www.apache.org/licenses/LICENSE-2.0  | 
 | 11 | + *  | 
 | 12 | + * Unless required by applicable law or agreed to in writing, software  | 
 | 13 | + * distributed under the License is distributed on an "AS IS" BASIS,  | 
 | 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  | 
 | 15 | + * See the License for the specific language governing permissions and  | 
 | 16 | + * limitations under the License.  | 
 | 17 | + */  | 
 | 18 | +package org.apache.hadoop.security;  | 
 | 19 | + | 
 | 20 | +import java.io.IOException;  | 
 | 21 | +import java.util.ArrayList;  | 
 | 22 | +import java.util.Iterator;  | 
 | 23 | +import java.util.List;  | 
 | 24 | +import java.util.Map;  | 
 | 25 | +import java.util.Set;  | 
 | 26 | +import java.util.TreeSet;  | 
 | 27 | + | 
 | 28 | +import org.apache.commons.logging.Log;  | 
 | 29 | +import org.apache.commons.logging.LogFactory;  | 
 | 30 | +import org.apache.hadoop.classification.InterfaceAudience;  | 
 | 31 | +import org.apache.hadoop.classification.InterfaceStability;  | 
 | 32 | +import org.apache.hadoop.conf.Configurable;  | 
 | 33 | +import org.apache.hadoop.conf.Configuration;  | 
 | 34 | +import org.apache.hadoop.util.ReflectionUtils;  | 
 | 35 | + | 
 | 36 | +/**  | 
 | 37 | + * An implementation of {@link GroupMappingServiceProvider} which  | 
 | 38 | + * composites other group mapping providers for determining group membership.  | 
 | 39 | + * This allows to combine existing provider implementations and composite   | 
 | 40 | + * a virtually new provider without customized development to deal with complex situation.   | 
 | 41 | + */  | 
 | 42 | +@InterfaceAudience.LimitedPrivate({"HDFS", "MapReduce"})  | 
 | 43 | +@InterfaceStability.Evolving  | 
 | 44 | +public class CompositeGroupsMapping  | 
 | 45 | +    implements GroupMappingServiceProvider, Configurable {  | 
 | 46 | +    | 
 | 47 | +  public static final String MAPPING_PROVIDERS_CONFIG_KEY = GROUP_MAPPING_CONFIG_PREFIX + ".providers";  | 
 | 48 | +  public static final String MAPPING_PROVIDERS_COMBINED_CONFIG_KEY = MAPPING_PROVIDERS_CONFIG_KEY + ".combined";  | 
 | 49 | +  public static final String MAPPING_PROVIDER_CONFIG_PREFIX = GROUP_MAPPING_CONFIG_PREFIX + ".provider";  | 
 | 50 | +    | 
 | 51 | +  private static final Log LOG = LogFactory.getLog(CompositeGroupsMapping.class);  | 
 | 52 | + | 
 | 53 | +  private List<GroupMappingServiceProvider> providersList =   | 
 | 54 | +		  new ArrayList<GroupMappingServiceProvider>();  | 
 | 55 | +    | 
 | 56 | +  private Configuration conf;  | 
 | 57 | +  private boolean combined;  | 
 | 58 | + | 
 | 59 | + | 
 | 60 | + | 
 | 61 | +  /**  | 
 | 62 | +   * Returns list of groups for a user.  | 
 | 63 | +   *   | 
 | 64 | +   * @param user get groups for this user  | 
 | 65 | +   * @return list of groups for a given user  | 
 | 66 | +   */  | 
 | 67 | +  @Override  | 
 | 68 | +  public synchronized List<String> getGroups(String user) throws IOException {  | 
 | 69 | +    Set<String> groupSet = new TreeSet<String>();  | 
 | 70 | + | 
 | 71 | +    List<String> groups = null;  | 
 | 72 | +    for (GroupMappingServiceProvider provider : providersList) {  | 
 | 73 | +      try {  | 
 | 74 | +        groups = provider.getGroups(user);  | 
 | 75 | +      } catch (Exception e) {  | 
 | 76 | +        //LOG.warn("Exception trying to get groups for user " + user, e);        | 
 | 77 | +      }          | 
 | 78 | +      if (groups != null && ! groups.isEmpty()) {  | 
 | 79 | +        groupSet.addAll(groups);  | 
 | 80 | +        if (!combined) break;  | 
 | 81 | +      }  | 
 | 82 | +    }  | 
 | 83 | + | 
 | 84 | +    List<String> results = new ArrayList<String>(groupSet.size());  | 
 | 85 | +    results.addAll(groupSet);  | 
 | 86 | +    return results;  | 
 | 87 | +  }  | 
 | 88 | +    | 
 | 89 | +  /**  | 
 | 90 | +   * Caches groups, no need to do that for this provider  | 
 | 91 | +   */  | 
 | 92 | +  @Override  | 
 | 93 | +  public void cacheGroupsRefresh() throws IOException {  | 
 | 94 | +    // does nothing in this provider of user to groups mapping  | 
 | 95 | +  }  | 
 | 96 | + | 
 | 97 | +  /**   | 
 | 98 | +   * Adds groups to cache, no need to do that for this provider  | 
 | 99 | +   *  | 
 | 100 | +   * @param groups unused  | 
 | 101 | +   */  | 
 | 102 | +  @Override  | 
 | 103 | +  public void cacheGroupsAdd(List<String> groups) throws IOException {  | 
 | 104 | +    // does nothing in this provider of user to groups mapping  | 
 | 105 | +  }  | 
 | 106 | + | 
 | 107 | +  @Override  | 
 | 108 | +  public synchronized Configuration getConf() {  | 
 | 109 | +    return conf;  | 
 | 110 | +  }  | 
 | 111 | + | 
 | 112 | +  @Override  | 
 | 113 | +  public synchronized void setConf(Configuration conf) {  | 
 | 114 | +    this.conf = conf;  | 
 | 115 | +      | 
 | 116 | +    this.combined = conf.getBoolean(MAPPING_PROVIDERS_COMBINED_CONFIG_KEY, true);  | 
 | 117 | +      | 
 | 118 | +    loadMappingProviders();  | 
 | 119 | +  }  | 
 | 120 | +    | 
 | 121 | +  private void loadMappingProviders() {  | 
 | 122 | +    String[] providerNames = conf.getStrings(MAPPING_PROVIDERS_CONFIG_KEY, new String[]{});  | 
 | 123 | + | 
 | 124 | +    String providerKey;  | 
 | 125 | +    for (String name : providerNames) {  | 
 | 126 | +      providerKey = MAPPING_PROVIDER_CONFIG_PREFIX + "." + name;  | 
 | 127 | +      Class<?> providerClass = conf.getClass(providerKey, null);  | 
 | 128 | +      if (providerClass == null) {  | 
 | 129 | +        LOG.error("The mapping provider, " + name + " does not have a valid class");  | 
 | 130 | +      } else {  | 
 | 131 | +        addMappingProvider(name, providerClass);  | 
 | 132 | +      }  | 
 | 133 | +    }  | 
 | 134 | +  }  | 
 | 135 | +      | 
 | 136 | +  private void addMappingProvider(String providerName, Class<?> providerClass) {  | 
 | 137 | +    Configuration newConf = prepareConf(providerName);  | 
 | 138 | +    GroupMappingServiceProvider provider =   | 
 | 139 | +        (GroupMappingServiceProvider) ReflectionUtils.newInstance(providerClass, newConf);  | 
 | 140 | +    providersList.add(provider);  | 
 | 141 | + | 
 | 142 | +  }  | 
 | 143 | + | 
 | 144 | +  /*  | 
 | 145 | +   * For any provider specific configuration properties, such as "hadoop.security.group.mapping.ldap.url"   | 
 | 146 | +   * and the like, allow them to be configured as "hadoop.security.group.mapping.provider.PROVIDER-X.ldap.url",  | 
 | 147 | +   * so that a provider such as LdapGroupsMapping can be used to composite a complex one with other providers.  | 
 | 148 | +   */  | 
 | 149 | +  private Configuration prepareConf(String providerName) {  | 
 | 150 | +    Configuration newConf = new Configuration();  | 
 | 151 | +    Iterator<Map.Entry<String, String>> entries = conf.iterator();  | 
 | 152 | +    String providerKey = MAPPING_PROVIDER_CONFIG_PREFIX + "." + providerName;  | 
 | 153 | +    while (entries.hasNext()) {  | 
 | 154 | +      Map.Entry<String, String> entry = entries.next();  | 
 | 155 | +      String key = entry.getKey();  | 
 | 156 | +      // get a property like "hadoop.security.group.mapping.provider.PROVIDER-X.ldap.url"  | 
 | 157 | +      if (key.startsWith(providerKey) && !key.equals(providerKey)) {  | 
 | 158 | +        // restore to be the one like "hadoop.security.group.mapping.ldap.url"   | 
 | 159 | +        // so that can be used by original provider.  | 
 | 160 | +        key = key.replace(".provider." + providerName, "");  | 
 | 161 | +        newConf.set(key, entry.getValue());  | 
 | 162 | +      }  | 
 | 163 | +    }  | 
 | 164 | +    return newConf;  | 
 | 165 | +  }    | 
 | 166 | +}  | 
0 commit comments