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.security;
19
20 import java.io.IOException;
21
22 import org.apache.hadoop.hbase.classification.InterfaceAudience;
23 import org.apache.hadoop.conf.Configuration;
24 import org.apache.hadoop.hbase.BaseConfigurable;
25 import org.apache.hadoop.security.UserGroupInformation;
26 import org.apache.hadoop.util.ReflectionUtils;
27
28 /**
29 * Provide an instance of a user. Allows custom {@link User} creation.
30 */
31 @InterfaceAudience.Private
32 public class UserProvider extends BaseConfigurable {
33
34 private static final String USER_PROVIDER_CONF_KEY = "hbase.client.userprovider.class";
35
36 /**
37 * Instantiate the {@link UserProvider} specified in the configuration and set the passed
38 * configuration via {@link UserProvider#setConf(Configuration)}
39 * @param conf to read and set on the created {@link UserProvider}
40 * @return a {@link UserProvider} ready for use.
41 */
42 public static UserProvider instantiate(Configuration conf) {
43 Class<? extends UserProvider> clazz =
44 conf.getClass(USER_PROVIDER_CONF_KEY, UserProvider.class, UserProvider.class);
45 return ReflectionUtils.newInstance(clazz, conf);
46 }
47
48 /**
49 * Set the {@link UserProvider} in the given configuration that should be instantiated
50 * @param conf to update
51 * @param provider class of the provider to set
52 */
53 public static void setUserProviderForTesting(Configuration conf,
54 Class<? extends UserProvider> provider) {
55 conf.set(USER_PROVIDER_CONF_KEY, provider.getName());
56 }
57
58 /**
59 * @return the userName for the current logged-in user.
60 * @throws IOException if the underlying user cannot be obtained
61 */
62 public String getCurrentUserName() throws IOException {
63 User user = getCurrent();
64 return user == null ? null : user.getName();
65 }
66
67 /**
68 * @return <tt>true</tt> if security is enabled, <tt>false</tt> otherwise
69 */
70 public boolean isHBaseSecurityEnabled() {
71 return User.isHBaseSecurityEnabled(this.getConf());
72 }
73
74 /**
75 * @return whether or not Kerberos authentication is configured for Hadoop. For non-secure Hadoop,
76 * this always returns <code>false</code>. For secure Hadoop, it will return the value
77 * from {@code UserGroupInformation.isSecurityEnabled()}.
78 */
79 public boolean isHadoopSecurityEnabled() {
80 return User.isSecurityEnabled();
81 }
82
83 /**
84 * @return the current user within the current execution context
85 * @throws IOException if the user cannot be loaded
86 */
87 public User getCurrent() throws IOException {
88 return User.getCurrent();
89 }
90
91 /**
92 * Wraps an underlying {@code UserGroupInformation} instance.
93 * @param ugi The base Hadoop user
94 * @return User
95 */
96 public User create(UserGroupInformation ugi) {
97 return User.create(ugi);
98 }
99
100 /**
101 * Log in the current process using the given configuration keys for the credential file and login
102 * principal.
103 * <p>
104 * <strong>This is only applicable when running on secure Hadoop</strong> -- see
105 * org.apache.hadoop.security.SecurityUtil#login(Configuration,String,String,String). On regular
106 * Hadoop (without security features), this will safely be ignored.
107 * </p>
108 * @param fileConfKey Property key used to configure path to the credential file
109 * @param principalConfKey Property key used to configure login principal
110 * @param localhost Current hostname to use in any credentials
111 * @throws IOException underlying exception from SecurityUtil.login() call
112 */
113 public void login(String fileConfKey, String principalConfKey, String localhost)
114 throws IOException {
115 User.login(getConf(), fileConfKey, principalConfKey, localhost);
116 }
117 }