View Javadoc

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 org.apache.hadoop.hbase.classification.InterfaceAudience;
21  import org.apache.hadoop.conf.Configuration;
22  import org.apache.hadoop.fs.CommonConfigurationKeys;
23  import org.apache.hadoop.hbase.HBaseConfiguration;
24  
25  import com.google.common.base.Strings;
26  
27  @InterfaceAudience.Private
28  class HBaseKerberosUtils {
29    public static final String KRB_PRINCIPAL = "hbase.regionserver.kerberos.principal";
30    public static final String KRB_KEYTAB_FILE = "hbase.regionserver.keytab.file";
31  
32    static boolean isKerberosPropertySetted() {
33      String krbPrincipal = System.getProperty(KRB_PRINCIPAL);
34      String krbKeytab = System.getProperty(KRB_KEYTAB_FILE);
35      if (Strings.isNullOrEmpty(krbPrincipal) || Strings.isNullOrEmpty(krbKeytab)) {
36        return false;
37      }
38      return true;
39    }
40  
41    static void setPrincipalForTesting(String principal) {
42      setSystemProperty(KRB_PRINCIPAL, principal);
43    }
44  
45    static void setKeytabFileForTesting(String keytabFile) {
46      setSystemProperty(KRB_KEYTAB_FILE, keytabFile);
47    }
48  
49    static void setSystemProperty(String propertyName, String propertyValue) {
50      System.setProperty(propertyName, propertyValue);
51    }
52  
53    static String getKeytabFileForTesting() {
54      return System.getProperty(KRB_KEYTAB_FILE);
55    }
56  
57    static String getPrincipalForTesting() {
58      return System.getProperty(KRB_PRINCIPAL);
59    }
60  
61    static Configuration getConfigurationWoPrincipal() {
62      Configuration conf = HBaseConfiguration.create();
63      conf.set(CommonConfigurationKeys.HADOOP_SECURITY_AUTHENTICATION, "kerberos");
64      conf.set("hbase.security.authentication", "kerberos");
65      conf.setBoolean("hbase.security.authorization", true);
66      return conf;
67    }
68  
69    static Configuration getSecuredConfiguration() {
70      Configuration conf = HBaseConfiguration.create();
71      conf.set(CommonConfigurationKeys.HADOOP_SECURITY_AUTHENTICATION, "kerberos");
72      conf.set("hbase.security.authentication", "kerberos");
73      conf.setBoolean("hbase.security.authorization", true);
74      conf.set(KRB_KEYTAB_FILE, System.getProperty(KRB_KEYTAB_FILE));
75      conf.set(KRB_PRINCIPAL, System.getProperty(KRB_PRINCIPAL));
76      return conf;
77    }
78  }