1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.omid.tools.hbase;
19
20 import org.apache.hadoop.security.UserGroupInformation;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24 import javax.annotation.Nullable;
25 import java.io.IOException;
26
27 public final class HBaseLogin {
28
29 private static final Logger LOG = LoggerFactory.getLogger(HBaseLogin.class);
30
31 private static volatile UserGroupInformation ugi;
32
33 @Nullable
34 public static UserGroupInformation loginIfNeeded(SecureHBaseConfig config) throws IOException {
35
36 if (UserGroupInformation.isSecurityEnabled()) {
37 LOG.info("Security enabled when connecting to HBase");
38 if (ugi == null) {
39 synchronized (HBaseLogin.class) {
40 if (ugi == null) {
41 LOG.info("Login with Kerberos. User={}, keytab={}", config.getPrincipal(), config.getKeytab());
42 UserGroupInformation.loginUserFromKeytab(config.getPrincipal(), config.getKeytab());
43 ugi = UserGroupInformation.getCurrentUser();
44 }
45 }
46 } else {
47 LOG.info("User {}, already trusted (Kerberos). Avoiding 2nd login as it causes problems", ugi.toString());
48 }
49 } else {
50 LOG.warn("Security NOT enabled when connecting to HBase. Act at your own risk. NULL UGI returned");
51 }
52 return ugi;
53 }
54
55 }