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