|
| 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.hbase.http; |
| 19 | + |
| 20 | +import java.io.IOException; |
| 21 | +import java.util.HashMap; |
| 22 | +import java.util.Map; |
| 23 | +import org.apache.hadoop.conf.Configuration; |
| 24 | +import org.apache.hadoop.hbase.HBaseInterfaceAudience; |
| 25 | +import org.apache.hadoop.security.SecurityUtil; |
| 26 | +import org.apache.hadoop.security.authentication.server.AuthenticationFilter; |
| 27 | +import org.apache.hadoop.security.authentication.server.KerberosAuthenticationHandler; |
| 28 | +import org.apache.yetus.audience.InterfaceAudience; |
| 29 | + |
| 30 | +/** |
| 31 | + * Initializes hadoop-auth AuthenticationFilter which provides support for Kerberos HTTP SPNEGO |
| 32 | + * authentication. |
| 33 | + * <p> |
| 34 | + * It enables anonymous access, simple/pseudo and Kerberos HTTP SPNEGO authentication for Hadoop |
| 35 | + * JobTracker, NameNode, DataNodes and TaskTrackers. |
| 36 | + * <p> |
| 37 | + * Refer to the <code>core-default.xml</code> file, after the comment 'HTTP Authentication' for |
| 38 | + * details on the configuration options. All related configuration properties have |
| 39 | + * 'hadoop.http.authentication.' as prefix. |
| 40 | + */ |
| 41 | +@InterfaceAudience.LimitedPrivate(HBaseInterfaceAudience.CONFIG) |
| 42 | +public class AuthenticationFilterInitializer extends FilterInitializer { |
| 43 | + |
| 44 | + static final String PREFIX = "hadoop.http.authentication."; |
| 45 | + |
| 46 | + /** |
| 47 | + * Initializes hadoop-auth AuthenticationFilter. |
| 48 | + * <p> |
| 49 | + * Propagates to hadoop-auth AuthenticationFilter configuration all Hadoop configuration |
| 50 | + * properties prefixed with "hadoop.http.authentication." |
| 51 | + * @param container The filter container |
| 52 | + * @param conf Configuration for run-time parameters |
| 53 | + */ |
| 54 | + @Override |
| 55 | + public void initFilter(FilterContainer container, Configuration conf) { |
| 56 | + Map<String, String> filterConfig = getFilterConfigMap(conf, PREFIX); |
| 57 | + |
| 58 | + container.addFilter("authentication", AuthenticationFilter.class.getName(), filterConfig); |
| 59 | + } |
| 60 | + |
| 61 | + public static Map<String, String> getFilterConfigMap(Configuration conf, String prefix) { |
| 62 | + Map<String, String> filterConfig = new HashMap<String, String>(); |
| 63 | + |
| 64 | + // setting the cookie path to root '/' so it is used for all resources. |
| 65 | + filterConfig.put(AuthenticationFilter.COOKIE_PATH, "/"); |
| 66 | + Map<String, String> propsWithPrefix = conf.getPropsWithPrefix(prefix); |
| 67 | + |
| 68 | + for (Map.Entry<String, String> entry : propsWithPrefix.entrySet()) { |
| 69 | + filterConfig.put(entry.getKey(), entry.getValue()); |
| 70 | + } |
| 71 | + |
| 72 | + // Resolve _HOST into bind address |
| 73 | + String bindAddress = conf.get(HttpServer.BIND_ADDRESS); |
| 74 | + String principal = filterConfig.get(KerberosAuthenticationHandler.PRINCIPAL); |
| 75 | + if (principal != null) { |
| 76 | + try { |
| 77 | + principal = SecurityUtil.getServerPrincipal(principal, bindAddress); |
| 78 | + } catch (IOException ex) { |
| 79 | + throw new RuntimeException("Could not resolve Kerberos principal name: " + ex.toString(), |
| 80 | + ex); |
| 81 | + } |
| 82 | + filterConfig.put(KerberosAuthenticationHandler.PRINCIPAL, principal); |
| 83 | + } |
| 84 | + return filterConfig; |
| 85 | + } |
| 86 | + |
| 87 | +} |
0 commit comments