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