1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.hbase;
20
21 import java.io.IOException;
22 import java.net.UnknownHostException;
23
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26 import org.apache.hadoop.conf.Configuration;
27 import org.apache.hadoop.hbase.classification.InterfaceAudience;
28 import org.apache.hadoop.hbase.classification.InterfaceStability;
29 import org.apache.hadoop.hbase.security.UserProvider;
30 import org.apache.hadoop.hbase.util.Strings;
31 import org.apache.hadoop.net.DNS;
32 import org.apache.hadoop.security.UserGroupInformation;
33
34
35
36
37 @InterfaceAudience.Private
38 @InterfaceStability.Evolving
39 public class AuthUtil {
40 private static final Log LOG = LogFactory.getLog(AuthUtil.class);
41
42 private AuthUtil() {
43 super();
44 }
45
46
47
48
49 public static ScheduledChore getAuthChore(Configuration conf) throws IOException {
50 UserProvider userProvider = UserProvider.instantiate(conf);
51
52 boolean securityEnabled =
53 userProvider.isHadoopSecurityEnabled() && userProvider.isHBaseSecurityEnabled();
54 if (!securityEnabled) return null;
55 String host = null;
56 try {
57 host = Strings.domainNamePointerToHostName(DNS.getDefaultHost(
58 conf.get("hbase.client.dns.interface", "default"),
59 conf.get("hbase.client.dns.nameserver", "default")));
60 userProvider.login("hbase.client.keytab.file", "hbase.client.kerberos.principal", host);
61 } catch (UnknownHostException e) {
62 LOG.error("Error resolving host name: " + e.getMessage(), e);
63 throw e;
64 } catch (IOException e) {
65 LOG.error("Error while trying to perform the initial login: " + e.getMessage(), e);
66 throw e;
67 }
68
69 final UserGroupInformation ugi = userProvider.getCurrent().getUGI();
70 Stoppable stoppable = new Stoppable() {
71 private volatile boolean isStopped = false;
72
73 @Override
74 public void stop(String why) {
75 isStopped = true;
76 }
77
78 @Override
79 public boolean isStopped() {
80 return isStopped;
81 }
82 };
83
84
85
86
87 final int CHECK_TGT_INTERVAL = 30 * 1000;
88
89 ScheduledChore refreshCredentials =
90 new ScheduledChore("RefreshCredentials", stoppable, CHECK_TGT_INTERVAL) {
91 @Override
92 protected void chore() {
93 try {
94 ugi.checkTGTAndReloginFromKeytab();
95 } catch (IOException e) {
96 LOG.error("Got exception while trying to refresh credentials: " + e.getMessage(), e);
97 }
98 }
99 };
100
101 return refreshCredentials;
102 }
103 }